1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.4.1.  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 ** The previous paragraph was written in 2005.  (This paragraph is written
64 ** on 2008-11-28.) These days, all Linux kernels support large files, so
65 ** you should probably leave LFS enabled.  But some embedded platforms might
66 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
67 **
68 ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
69 */
70 #ifndef SQLITE_DISABLE_LFS
71 # define _LARGE_FILE       1
72 # ifndef _FILE_OFFSET_BITS
73 #   define _FILE_OFFSET_BITS 64
74 # endif
75 # define _LARGEFILE_SOURCE 1
76 #endif
77 
78 /*
79 ** For MinGW, check to see if we can include the header file containing its
80 ** version information, among other things.  Normally, this internal MinGW
81 ** header file would [only] be included automatically by other MinGW header
82 ** files; however, the contained version information is now required by this
83 ** header file to work around binary compatibility issues (see below) and
84 ** this is the only known way to reliably obtain it.  This entire #if block
85 ** would be completely unnecessary if there was any other way of detecting
86 ** MinGW via their preprocessor (e.g. if they customized their GCC to define
87 ** some MinGW-specific macros).  When compiling for MinGW, either the
88 ** _HAVE_MINGW_H or _HAVE__MINGW_H (note the extra underscore) macro must be
89 ** defined; otherwise, detection of conditions specific to MinGW will be
90 ** disabled.
91 */
92 #if defined(_HAVE_MINGW_H)
93 # include "mingw.h"
94 #elif defined(_HAVE__MINGW_H)
95 # include "_mingw.h"
96 #endif
97 
98 /*
99 ** For MinGW version 4.x (and higher), check to see if the _USE_32BIT_TIME_T
100 ** define is required to maintain binary compatibility with the MSVC runtime
101 ** library in use (e.g. for Windows XP).
102 */
103 #if !defined(_USE_32BIT_TIME_T) && !defined(_USE_64BIT_TIME_T) && \
104     defined(_WIN32) && !defined(_WIN64) && \
105     defined(__MINGW_MAJOR_VERSION) && __MINGW_MAJOR_VERSION >= 4 && \
106     defined(__MSVCRT__)
107 # define _USE_32BIT_TIME_T
108 #endif
109 
110 /* The public SQLite interface.  The _FILE_OFFSET_BITS macro must appear
111 ** first in QNX.  Also, the _USE_32BIT_TIME_T macro must appear first for
112 ** MinGW.
113 */
114 /************** Include sqlite3.h in the middle of sqliteInt.h ***************/
115 /************** Begin file sqlite3.h *****************************************/
116 /*
117 ** 2001 September 15
118 **
119 ** The author disclaims copyright to this source code.  In place of
120 ** a legal notice, here is a blessing:
121 **
122 **    May you do good and not evil.
123 **    May you find forgiveness for yourself and forgive others.
124 **    May you share freely, never taking more than you give.
125 **
126 *************************************************************************
127 ** This header file defines the interface that the SQLite library
128 ** presents to client programs.  If a C-function, structure, datatype,
129 ** or constant definition does not appear in this file, then it is
130 ** not a published API of SQLite, is subject to change without
131 ** notice, and should not be referenced by programs that use SQLite.
132 **
133 ** Some of the definitions that are in this file are marked as
134 ** "experimental".  Experimental interfaces are normally new
135 ** features recently added to SQLite.  We do not anticipate changes
136 ** to experimental interfaces but reserve the right to make minor changes
137 ** if experience from use "in the wild" suggest such changes are prudent.
138 **
139 ** The official C-language API documentation for SQLite is derived
140 ** from comments in this file.  This file is the authoritative source
141 ** on how SQLite interfaces are suppose to operate.
142 **
143 ** The name of this file under configuration management is "sqlite.h.in".
144 ** The makefile makes some minor changes to this file (such as inserting
145 ** the version number) and changes its name to "sqlite3.h" as
146 ** part of the build process.
147 */
148 #ifndef _SQLITE3_H_
149 #define _SQLITE3_H_
150 #include <stdarg.h>     /* Needed for the definition of va_list */
151 
152 /*
153 ** Make sure we can call this stuff from C++.
154 */
155 #if 0
156 extern "C" {
157 #endif
158 
159 
160 /*
161 ** Add the ability to override 'extern'
162 */
163 #ifndef SQLITE_EXTERN
164 # define SQLITE_EXTERN extern
165 #endif
166 
167 #ifndef SQLITE_API
168 # define SQLITE_API
169 #endif
170 
171 
172 /*
173 ** These no-op macros are used in front of interfaces to mark those
174 ** interfaces as either deprecated or experimental.  New applications
175 ** should not use deprecated interfaces - they are support for backwards
176 ** compatibility only.  Application writers should be aware that
177 ** experimental interfaces are subject to change in point releases.
178 **
179 ** These macros used to resolve to various kinds of compiler magic that
180 ** would generate warning messages when they were used.  But that
181 ** compiler magic ended up generating such a flurry of bug reports
182 ** that we have taken it all out and gone back to using simple
183 ** noop macros.
184 */
185 #define SQLITE_DEPRECATED
186 #define SQLITE_EXPERIMENTAL
187 
188 /*
189 ** Ensure these symbols were not defined by some previous header file.
190 */
191 #ifdef SQLITE_VERSION
192 # undef SQLITE_VERSION
193 #endif
194 #ifdef SQLITE_VERSION_NUMBER
195 # undef SQLITE_VERSION_NUMBER
196 #endif
197 
198 /*
199 ** CAPI3REF: Compile-Time Library Version Numbers
200 **
201 ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
202 ** evaluates to a string literal that is the SQLite version in the
203 ** format "X.Y.Z" where X is the major version number (always 3 for
204 ** SQLite3) and Y is the minor version number and Z is the release number.)^
205 ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
206 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
207 ** numbers used in [SQLITE_VERSION].)^
208 ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
209 ** be larger than the release from which it is derived.  Either Y will
210 ** be held constant and Z will be incremented or else Y will be incremented
211 ** and Z will be reset to zero.
212 **
213 ** Since version 3.6.18, SQLite source code has been stored in the
214 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
215 ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
216 ** a string which identifies a particular check-in of SQLite
217 ** within its configuration management system.  ^The SQLITE_SOURCE_ID
218 ** string contains the date and time of the check-in (UTC) and an SHA1
219 ** hash of the entire source tree.
220 **
221 ** See also: [sqlite3_libversion()],
222 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
223 ** [sqlite_version()] and [sqlite_source_id()].
224 */
225 #define SQLITE_VERSION        "3.8.4.1"
226 #define SQLITE_VERSION_NUMBER 3008004
227 #define SQLITE_SOURCE_ID      "2014-03-11 15:27:36 018d317b1257ce68a92908b05c9c7cf1494050d0"
228 
229 /*
230 ** CAPI3REF: Run-Time Library Version Numbers
231 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
232 **
233 ** These interfaces provide the same information as the [SQLITE_VERSION],
234 ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
235 ** but are associated with the library instead of the header file.  ^(Cautious
236 ** programmers might include assert() statements in their application to
237 ** verify that values returned by these interfaces match the macros in
238 ** the header, and thus insure that the application is
239 ** compiled with matching library and header files.
240 **
241 ** <blockquote><pre>
242 ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
243 ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
244 ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
245 ** </pre></blockquote>)^
246 **
247 ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
248 ** macro.  ^The sqlite3_libversion() function returns a pointer to the
249 ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
250 ** function is provided for use in DLLs since DLL users usually do not have
251 ** direct access to string constants within the DLL.  ^The
252 ** sqlite3_libversion_number() function returns an integer equal to
253 ** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns
254 ** a pointer to a string constant whose value is the same as the
255 ** [SQLITE_SOURCE_ID] C preprocessor macro.
256 **
257 ** See also: [sqlite_version()] and [sqlite_source_id()].
258 */
259 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
260 SQLITE_API const char *sqlite3_libversion(void);
261 SQLITE_API const char *sqlite3_sourceid(void);
262 SQLITE_API int sqlite3_libversion_number(void);
263 
264 /*
265 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
266 **
267 ** ^The sqlite3_compileoption_used() function returns 0 or 1
268 ** indicating whether the specified option was defined at
269 ** compile time.  ^The SQLITE_ prefix may be omitted from the
270 ** option name passed to sqlite3_compileoption_used().
271 **
272 ** ^The sqlite3_compileoption_get() function allows iterating
273 ** over the list of options that were defined at compile time by
274 ** returning the N-th compile time option string.  ^If N is out of range,
275 ** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
276 ** prefix is omitted from any strings returned by
277 ** sqlite3_compileoption_get().
278 **
279 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
280 ** and sqlite3_compileoption_get() may be omitted by specifying the
281 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
282 **
283 ** See also: SQL functions [sqlite_compileoption_used()] and
284 ** [sqlite_compileoption_get()] and the [compile_options pragma].
285 */
286 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
287 SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
288 SQLITE_API const char *sqlite3_compileoption_get(int N);
289 #endif
290 
291 /*
292 ** CAPI3REF: Test To See If The Library Is Threadsafe
293 **
294 ** ^The sqlite3_threadsafe() function returns zero if and only if
295 ** SQLite was compiled with mutexing code omitted due to the
296 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
297 **
298 ** SQLite can be compiled with or without mutexes.  When
299 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
300 ** are enabled and SQLite is threadsafe.  When the
301 ** [SQLITE_THREADSAFE] macro is 0,
302 ** the mutexes are omitted.  Without the mutexes, it is not safe
303 ** to use SQLite concurrently from more than one thread.
304 **
305 ** Enabling mutexes incurs a measurable performance penalty.
306 ** So if speed is of utmost importance, it makes sense to disable
307 ** the mutexes.  But for maximum safety, mutexes should be enabled.
308 ** ^The default behavior is for mutexes to be enabled.
309 **
310 ** This interface can be used by an application to make sure that the
311 ** version of SQLite that it is linking against was compiled with
312 ** the desired setting of the [SQLITE_THREADSAFE] macro.
313 **
314 ** This interface only reports on the compile-time mutex setting
315 ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
316 ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
317 ** can be fully or partially disabled using a call to [sqlite3_config()]
318 ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
319 ** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
320 ** sqlite3_threadsafe() function shows only the compile-time setting of
321 ** thread safety, not any run-time changes to that setting made by
322 ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
323 ** is unchanged by calls to sqlite3_config().)^
324 **
325 ** See the [threading mode] documentation for additional information.
326 */
327 SQLITE_API int sqlite3_threadsafe(void);
328 
329 /*
330 ** CAPI3REF: Database Connection Handle
331 ** KEYWORDS: {database connection} {database connections}
332 **
333 ** Each open SQLite database is represented by a pointer to an instance of
334 ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
335 ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
336 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
337 ** and [sqlite3_close_v2()] are its destructors.  There are many other
338 ** interfaces (such as
339 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
340 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
341 ** sqlite3 object.
342 */
343 typedef struct sqlite3 sqlite3;
344 
345 /*
346 ** CAPI3REF: 64-Bit Integer Types
347 ** KEYWORDS: sqlite_int64 sqlite_uint64
348 **
349 ** Because there is no cross-platform way to specify 64-bit integer types
350 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
351 **
352 ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
353 ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
354 ** compatibility only.
355 **
356 ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
357 ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
358 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
359 ** between 0 and +18446744073709551615 inclusive.
360 */
361 #ifdef SQLITE_INT64_TYPE
362   typedef SQLITE_INT64_TYPE sqlite_int64;
363   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
364 #elif defined(_MSC_VER) || defined(__BORLANDC__)
365   typedef __int64 sqlite_int64;
366   typedef unsigned __int64 sqlite_uint64;
367 #else
368   typedef long long int sqlite_int64;
369   typedef unsigned long long int sqlite_uint64;
370 #endif
371 typedef sqlite_int64 sqlite3_int64;
372 typedef sqlite_uint64 sqlite3_uint64;
373 
374 /*
375 ** If compiling for a processor that lacks floating point support,
376 ** substitute integer for floating-point.
377 */
378 #ifdef SQLITE_OMIT_FLOATING_POINT
379 # define double sqlite3_int64
380 #endif
381 
382 /*
383 ** CAPI3REF: Closing A Database Connection
384 **
385 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
386 ** for the [sqlite3] object.
387 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
388 ** the [sqlite3] object is successfully destroyed and all associated
389 ** resources are deallocated.
390 **
391 ** ^If the database connection is associated with unfinalized prepared
392 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
393 ** will leave the database connection open and return [SQLITE_BUSY].
394 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
395 ** and unfinished sqlite3_backups, then the database connection becomes
396 ** an unusable "zombie" which will automatically be deallocated when the
397 ** last prepared statement is finalized or the last sqlite3_backup is
398 ** finished.  The sqlite3_close_v2() interface is intended for use with
399 ** host languages that are garbage collected, and where the order in which
400 ** destructors are called is arbitrary.
401 **
402 ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
403 ** [sqlite3_blob_close | close] all [BLOB handles], and
404 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
405 ** with the [sqlite3] object prior to attempting to close the object.  ^If
406 ** sqlite3_close_v2() is called on a [database connection] that still has
407 ** outstanding [prepared statements], [BLOB handles], and/or
408 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
409 ** of resources is deferred until all [prepared statements], [BLOB handles],
410 ** and [sqlite3_backup] objects are also destroyed.
411 **
412 ** ^If an [sqlite3] object is destroyed while a transaction is open,
413 ** the transaction is automatically rolled back.
414 **
415 ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
416 ** must be either a NULL
417 ** pointer or an [sqlite3] object pointer obtained
418 ** from [sqlite3_open()], [sqlite3_open16()], or
419 ** [sqlite3_open_v2()], and not previously closed.
420 ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
421 ** argument is a harmless no-op.
422 */
423 SQLITE_API int sqlite3_close(sqlite3*);
424 SQLITE_API int sqlite3_close_v2(sqlite3*);
425 
426 /*
427 ** The type for a callback function.
428 ** This is legacy and deprecated.  It is included for historical
429 ** compatibility and is not documented.
430 */
431 typedef int (*sqlite3_callback)(void*,int,char**, char**);
432 
433 /*
434 ** CAPI3REF: One-Step Query Execution Interface
435 **
436 ** The sqlite3_exec() interface is a convenience wrapper around
437 ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
438 ** that allows an application to run multiple statements of SQL
439 ** without having to use a lot of C code.
440 **
441 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
442 ** semicolon-separate SQL statements passed into its 2nd argument,
443 ** in the context of the [database connection] passed in as its 1st
444 ** argument.  ^If the callback function of the 3rd argument to
445 ** sqlite3_exec() is not NULL, then it is invoked for each result row
446 ** coming out of the evaluated SQL statements.  ^The 4th argument to
447 ** sqlite3_exec() is relayed through to the 1st argument of each
448 ** callback invocation.  ^If the callback pointer to sqlite3_exec()
449 ** is NULL, then no callback is ever invoked and result rows are
450 ** ignored.
451 **
452 ** ^If an error occurs while evaluating the SQL statements passed into
453 ** sqlite3_exec(), then execution of the current statement stops and
454 ** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
455 ** is not NULL then any error message is written into memory obtained
456 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
457 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
458 ** on error message strings returned through the 5th parameter of
459 ** of sqlite3_exec() after the error message string is no longer needed.
460 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
461 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
462 ** NULL before returning.
463 **
464 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
465 ** routine returns SQLITE_ABORT without invoking the callback again and
466 ** without running any subsequent SQL statements.
467 **
468 ** ^The 2nd argument to the sqlite3_exec() callback function is the
469 ** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
470 ** callback is an array of pointers to strings obtained as if from
471 ** [sqlite3_column_text()], one for each column.  ^If an element of a
472 ** result row is NULL then the corresponding string pointer for the
473 ** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
474 ** sqlite3_exec() callback is an array of pointers to strings where each
475 ** entry represents the name of corresponding result column as obtained
476 ** from [sqlite3_column_name()].
477 **
478 ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
479 ** to an empty string, or a pointer that contains only whitespace and/or
480 ** SQL comments, then no SQL statements are evaluated and the database
481 ** is not changed.
482 **
483 ** Restrictions:
484 **
485 ** <ul>
486 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
487 **      is a valid and open [database connection].
488 ** <li> The application must not close the [database connection] specified by
489 **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
490 ** <li> The application must not modify the SQL statement text passed into
491 **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
492 ** </ul>
493 */
494 SQLITE_API int sqlite3_exec(
495   sqlite3*,                                  /* An open database */
496   const char *sql,                           /* SQL to be evaluated */
497   int (*callback)(void*,int,char**,char**),  /* Callback function */
498   void *,                                    /* 1st argument to callback */
499   char **errmsg                              /* Error msg written here */
500 );
501 
502 /*
503 ** CAPI3REF: Result Codes
504 ** KEYWORDS: SQLITE_OK {error code} {error codes}
505 ** KEYWORDS: {result code} {result codes}
506 **
507 ** Many SQLite functions return an integer result code from the set shown
508 ** here in order to indicate success or failure.
509 **
510 ** New error codes may be added in future versions of SQLite.
511 **
512 ** See also: [SQLITE_IOERR_READ | extended result codes],
513 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
514 */
515 #define SQLITE_OK           0   /* Successful result */
516 /* beginning-of-error-codes */
517 #define SQLITE_ERROR        1   /* SQL error or missing database */
518 #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
519 #define SQLITE_PERM         3   /* Access permission denied */
520 #define SQLITE_ABORT        4   /* Callback routine requested an abort */
521 #define SQLITE_BUSY         5   /* The database file is locked */
522 #define SQLITE_LOCKED       6   /* A table in the database is locked */
523 #define SQLITE_NOMEM        7   /* A malloc() failed */
524 #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
525 #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
526 #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
527 #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
528 #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
529 #define SQLITE_FULL        13   /* Insertion failed because database is full */
530 #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
531 #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
532 #define SQLITE_EMPTY       16   /* Database is empty */
533 #define SQLITE_SCHEMA      17   /* The database schema changed */
534 #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
535 #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
536 #define SQLITE_MISMATCH    20   /* Data type mismatch */
537 #define SQLITE_MISUSE      21   /* Library used incorrectly */
538 #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
539 #define SQLITE_AUTH        23   /* Authorization denied */
540 #define SQLITE_FORMAT      24   /* Auxiliary database format error */
541 #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
542 #define SQLITE_NOTADB      26   /* File opened that is not a database file */
543 #define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
544 #define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
545 #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
546 #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
547 /* end-of-error-codes */
548 
549 /*
550 ** CAPI3REF: Extended Result Codes
551 ** KEYWORDS: {extended error code} {extended error codes}
552 ** KEYWORDS: {extended result code} {extended result codes}
553 **
554 ** In its default configuration, SQLite API routines return one of 26 integer
555 ** [SQLITE_OK | result codes].  However, experience has shown that many of
556 ** these result codes are too coarse-grained.  They do not provide as
557 ** much information about problems as programmers might like.  In an effort to
558 ** address this, newer versions of SQLite (version 3.3.8 and later) include
559 ** support for additional result codes that provide more detailed information
560 ** about errors. The extended result codes are enabled or disabled
561 ** on a per database connection basis using the
562 ** [sqlite3_extended_result_codes()] API.
563 **
564 ** Some of the available extended result codes are listed here.
565 ** One may expect the number of extended result codes will increase
566 ** over time.  Software that uses extended result codes should expect
567 ** to see new result codes in future releases of SQLite.
568 **
569 ** The SQLITE_OK result code will never be extended.  It will always
570 ** be exactly zero.
571 */
572 #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
573 #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
574 #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
575 #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
576 #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
577 #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
578 #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
579 #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
580 #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
581 #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
582 #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
583 #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
584 #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
585 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
586 #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
587 #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
588 #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
589 #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
590 #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
591 #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
592 #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
593 #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
594 #define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
595 #define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
596 #define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
597 #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
598 #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
599 #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
600 #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
601 #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
602 #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
603 #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
604 #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
605 #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
606 #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
607 #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
608 #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
609 #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
610 #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
611 #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
612 #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
613 #define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
614 #define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
615 #define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
616 #define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
617 #define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
618 #define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
619 #define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
620 #define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
621 #define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
622 #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
623 #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
624 
625 /*
626 ** CAPI3REF: Flags For File Open Operations
627 **
628 ** These bit values are intended for use in the
629 ** 3rd parameter to the [sqlite3_open_v2()] interface and
630 ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
631 */
632 #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
633 #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
634 #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
635 #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
636 #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
637 #define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
638 #define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
639 #define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
640 #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
641 #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
642 #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
643 #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
644 #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
645 #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
646 #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
647 #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
648 #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
649 #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
650 #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
651 #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
652 
653 /* Reserved:                         0x00F00000 */
654 
655 /*
656 ** CAPI3REF: Device Characteristics
657 **
658 ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
659 ** object returns an integer which is a vector of these
660 ** bit values expressing I/O characteristics of the mass storage
661 ** device that holds the file that the [sqlite3_io_methods]
662 ** refers to.
663 **
664 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
665 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
666 ** mean that writes of blocks that are nnn bytes in size and
667 ** are aligned to an address which is an integer multiple of
668 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
669 ** that when data is appended to a file, the data is appended
670 ** first then the size of the file is extended, never the other
671 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
672 ** information is written to disk in the same order as calls
673 ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
674 ** after reboot following a crash or power loss, the only bytes in a
675 ** file that were written at the application level might have changed
676 ** and that adjacent bytes, even bytes within the same sector are
677 ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
678 ** flag indicate that a file cannot be deleted when open.
679 */
680 #define SQLITE_IOCAP_ATOMIC                 0x00000001
681 #define SQLITE_IOCAP_ATOMIC512              0x00000002
682 #define SQLITE_IOCAP_ATOMIC1K               0x00000004
683 #define SQLITE_IOCAP_ATOMIC2K               0x00000008
684 #define SQLITE_IOCAP_ATOMIC4K               0x00000010
685 #define SQLITE_IOCAP_ATOMIC8K               0x00000020
686 #define SQLITE_IOCAP_ATOMIC16K              0x00000040
687 #define SQLITE_IOCAP_ATOMIC32K              0x00000080
688 #define SQLITE_IOCAP_ATOMIC64K              0x00000100
689 #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
690 #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
691 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
692 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
693 
694 /*
695 ** CAPI3REF: File Locking Levels
696 **
697 ** SQLite uses one of these integer values as the second
698 ** argument to calls it makes to the xLock() and xUnlock() methods
699 ** of an [sqlite3_io_methods] object.
700 */
701 #define SQLITE_LOCK_NONE          0
702 #define SQLITE_LOCK_SHARED        1
703 #define SQLITE_LOCK_RESERVED      2
704 #define SQLITE_LOCK_PENDING       3
705 #define SQLITE_LOCK_EXCLUSIVE     4
706 
707 /*
708 ** CAPI3REF: Synchronization Type Flags
709 **
710 ** When SQLite invokes the xSync() method of an
711 ** [sqlite3_io_methods] object it uses a combination of
712 ** these integer values as the second argument.
713 **
714 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
715 ** sync operation only needs to flush data to mass storage.  Inode
716 ** information need not be flushed. If the lower four bits of the flag
717 ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
718 ** If the lower four bits equal SQLITE_SYNC_FULL, that means
719 ** to use Mac OS X style fullsync instead of fsync().
720 **
721 ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
722 ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
723 ** settings.  The [synchronous pragma] determines when calls to the
724 ** xSync VFS method occur and applies uniformly across all platforms.
725 ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
726 ** energetic or rigorous or forceful the sync operations are and
727 ** only make a difference on Mac OSX for the default SQLite code.
728 ** (Third-party VFS implementations might also make the distinction
729 ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
730 ** operating systems natively supported by SQLite, only Mac OSX
731 ** cares about the difference.)
732 */
733 #define SQLITE_SYNC_NORMAL        0x00002
734 #define SQLITE_SYNC_FULL          0x00003
735 #define SQLITE_SYNC_DATAONLY      0x00010
736 
737 /*
738 ** CAPI3REF: OS Interface Open File Handle
739 **
740 ** An [sqlite3_file] object represents an open file in the
741 ** [sqlite3_vfs | OS interface layer].  Individual OS interface
742 ** implementations will
743 ** want to subclass this object by appending additional fields
744 ** for their own use.  The pMethods entry is a pointer to an
745 ** [sqlite3_io_methods] object that defines methods for performing
746 ** I/O operations on the open file.
747 */
748 typedef struct sqlite3_file sqlite3_file;
749 struct sqlite3_file {
750   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
751 };
752 
753 /*
754 ** CAPI3REF: OS Interface File Virtual Methods Object
755 **
756 ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
757 ** [sqlite3_file] object (or, more commonly, a subclass of the
758 ** [sqlite3_file] object) with a pointer to an instance of this object.
759 ** This object defines the methods used to perform various operations
760 ** against the open file represented by the [sqlite3_file] object.
761 **
762 ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
763 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
764 ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
765 ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
766 ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
767 ** to NULL.
768 **
769 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
770 ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
771 ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
772 ** flag may be ORed in to indicate that only the data of the file
773 ** and not its inode needs to be synced.
774 **
775 ** The integer values to xLock() and xUnlock() are one of
776 ** <ul>
777 ** <li> [SQLITE_LOCK_NONE],
778 ** <li> [SQLITE_LOCK_SHARED],
779 ** <li> [SQLITE_LOCK_RESERVED],
780 ** <li> [SQLITE_LOCK_PENDING], or
781 ** <li> [SQLITE_LOCK_EXCLUSIVE].
782 ** </ul>
783 ** xLock() increases the lock. xUnlock() decreases the lock.
784 ** The xCheckReservedLock() method checks whether any database connection,
785 ** either in this process or in some other process, is holding a RESERVED,
786 ** PENDING, or EXCLUSIVE lock on the file.  It returns true
787 ** if such a lock exists and false otherwise.
788 **
789 ** The xFileControl() method is a generic interface that allows custom
790 ** VFS implementations to directly control an open file using the
791 ** [sqlite3_file_control()] interface.  The second "op" argument is an
792 ** integer opcode.  The third argument is a generic pointer intended to
793 ** point to a structure that may contain arguments or space in which to
794 ** write return values.  Potential uses for xFileControl() might be
795 ** functions to enable blocking locks with timeouts, to change the
796 ** locking strategy (for example to use dot-file locks), to inquire
797 ** about the status of a lock, or to break stale locks.  The SQLite
798 ** core reserves all opcodes less than 100 for its own use.
799 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
800 ** Applications that define a custom xFileControl method should use opcodes
801 ** greater than 100 to avoid conflicts.  VFS implementations should
802 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
803 ** recognize.
804 **
805 ** The xSectorSize() method returns the sector size of the
806 ** device that underlies the file.  The sector size is the
807 ** minimum write that can be performed without disturbing
808 ** other bytes in the file.  The xDeviceCharacteristics()
809 ** method returns a bit vector describing behaviors of the
810 ** underlying device:
811 **
812 ** <ul>
813 ** <li> [SQLITE_IOCAP_ATOMIC]
814 ** <li> [SQLITE_IOCAP_ATOMIC512]
815 ** <li> [SQLITE_IOCAP_ATOMIC1K]
816 ** <li> [SQLITE_IOCAP_ATOMIC2K]
817 ** <li> [SQLITE_IOCAP_ATOMIC4K]
818 ** <li> [SQLITE_IOCAP_ATOMIC8K]
819 ** <li> [SQLITE_IOCAP_ATOMIC16K]
820 ** <li> [SQLITE_IOCAP_ATOMIC32K]
821 ** <li> [SQLITE_IOCAP_ATOMIC64K]
822 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
823 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
824 ** </ul>
825 **
826 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
827 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
828 ** mean that writes of blocks that are nnn bytes in size and
829 ** are aligned to an address which is an integer multiple of
830 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
831 ** that when data is appended to a file, the data is appended
832 ** first then the size of the file is extended, never the other
833 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
834 ** information is written to disk in the same order as calls
835 ** to xWrite().
836 **
837 ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
838 ** in the unread portions of the buffer with zeros.  A VFS that
839 ** fails to zero-fill short reads might seem to work.  However,
840 ** failure to zero-fill short reads will eventually lead to
841 ** database corruption.
842 */
843 typedef struct sqlite3_io_methods sqlite3_io_methods;
844 struct sqlite3_io_methods {
845   int iVersion;
846   int (*xClose)(sqlite3_file*);
847   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
848   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
849   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
850   int (*xSync)(sqlite3_file*, int flags);
851   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
852   int (*xLock)(sqlite3_file*, int);
853   int (*xUnlock)(sqlite3_file*, int);
854   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
855   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
856   int (*xSectorSize)(sqlite3_file*);
857   int (*xDeviceCharacteristics)(sqlite3_file*);
858   /* Methods above are valid for version 1 */
859   int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
860   int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
861   void (*xShmBarrier)(sqlite3_file*);
862   int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
863   /* Methods above are valid for version 2 */
864   int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
865   int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
866   /* Methods above are valid for version 3 */
867   /* Additional methods may be added in future releases */
868 };
869 
870 /*
871 ** CAPI3REF: Standard File Control Opcodes
872 **
873 ** These integer constants are opcodes for the xFileControl method
874 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
875 ** interface.
876 **
877 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
878 ** opcode causes the xFileControl method to write the current state of
879 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
880 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
881 ** into an integer that the pArg argument points to. This capability
882 ** is used during testing and only needs to be supported when SQLITE_TEST
883 ** is defined.
884 ** <ul>
885 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
886 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
887 ** layer a hint of how large the database file will grow to be during the
888 ** current transaction.  This hint is not guaranteed to be accurate but it
889 ** is often close.  The underlying VFS might choose to preallocate database
890 ** file space based on this hint in order to help writes to the database
891 ** file run faster.
892 **
893 ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
894 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
895 ** extends and truncates the database file in chunks of a size specified
896 ** by the user. The fourth argument to [sqlite3_file_control()] should
897 ** point to an integer (type int) containing the new chunk-size to use
898 ** for the nominated database. Allocating database file space in large
899 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
900 ** improve performance on some systems.
901 **
902 ** <li>[[SQLITE_FCNTL_FILE_POINTER]]
903 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
904 ** to the [sqlite3_file] object associated with a particular database
905 ** connection.  See the [sqlite3_file_control()] documentation for
906 ** additional information.
907 **
908 ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
909 ** No longer in use.
910 **
911 ** <li>[[SQLITE_FCNTL_SYNC]]
912 ** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
913 ** sent to the VFS immediately before the xSync method is invoked on a
914 ** database file descriptor. Or, if the xSync method is not invoked
915 ** because the user has configured SQLite with
916 ** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
917 ** of the xSync method. In most cases, the pointer argument passed with
918 ** this file-control is NULL. However, if the database file is being synced
919 ** as part of a multi-database commit, the argument points to a nul-terminated
920 ** string containing the transactions master-journal file name. VFSes that
921 ** do not need this signal should silently ignore this opcode. Applications
922 ** should not call [sqlite3_file_control()] with this opcode as doing so may
923 ** disrupt the operation of the specialized VFSes that do require it.
924 **
925 ** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
926 ** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
927 ** and sent to the VFS after a transaction has been committed immediately
928 ** but before the database is unlocked. VFSes that do not need this signal
929 ** should silently ignore this opcode. Applications should not call
930 ** [sqlite3_file_control()] with this opcode as doing so may disrupt the
931 ** operation of the specialized VFSes that do require it.
932 **
933 ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
934 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
935 ** retry counts and intervals for certain disk I/O operations for the
936 ** windows [VFS] in order to provide robustness in the presence of
937 ** anti-virus programs.  By default, the windows VFS will retry file read,
938 ** file write, and file delete operations up to 10 times, with a delay
939 ** of 25 milliseconds before the first retry and with the delay increasing
940 ** by an additional 25 milliseconds with each subsequent retry.  This
941 ** opcode allows these two values (10 retries and 25 milliseconds of delay)
942 ** to be adjusted.  The values are changed for all database connections
943 ** within the same process.  The argument is a pointer to an array of two
944 ** integers where the first integer i the new retry count and the second
945 ** integer is the delay.  If either integer is negative, then the setting
946 ** is not changed but instead the prior value of that setting is written
947 ** into the array entry, allowing the current retry settings to be
948 ** interrogated.  The zDbName parameter is ignored.
949 **
950 ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
951 ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
952 ** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
953 ** write ahead log and shared memory files used for transaction control
954 ** are automatically deleted when the latest connection to the database
955 ** closes.  Setting persistent WAL mode causes those files to persist after
956 ** close.  Persisting the files is useful when other processes that do not
957 ** have write permission on the directory containing the database file want
958 ** to read the database file, as the WAL and shared memory files must exist
959 ** in order for the database to be readable.  The fourth parameter to
960 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
961 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
962 ** WAL mode.  If the integer is -1, then it is overwritten with the current
963 ** WAL persistence setting.
964 **
965 ** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
966 ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
967 ** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
968 ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
969 ** xDeviceCharacteristics methods. The fourth parameter to
970 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
971 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
972 ** mode.  If the integer is -1, then it is overwritten with the current
973 ** zero-damage mode setting.
974 **
975 ** <li>[[SQLITE_FCNTL_OVERWRITE]]
976 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
977 ** a write transaction to indicate that, unless it is rolled back for some
978 ** reason, the entire database file will be overwritten by the current
979 ** transaction. This is used by VACUUM operations.
980 **
981 ** <li>[[SQLITE_FCNTL_VFSNAME]]
982 ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
983 ** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
984 ** final bottom-level VFS are written into memory obtained from
985 ** [sqlite3_malloc()] and the result is stored in the char* variable
986 ** that the fourth parameter of [sqlite3_file_control()] points to.
987 ** The caller is responsible for freeing the memory when done.  As with
988 ** all file-control actions, there is no guarantee that this will actually
989 ** do anything.  Callers should initialize the char* variable to a NULL
990 ** pointer in case this file-control is not implemented.  This file-control
991 ** is intended for diagnostic use only.
992 **
993 ** <li>[[SQLITE_FCNTL_PRAGMA]]
994 ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
995 ** file control is sent to the open [sqlite3_file] object corresponding
996 ** to the database file to which the pragma statement refers. ^The argument
997 ** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
998 ** pointers to strings (char**) in which the second element of the array
999 ** is the name of the pragma and the third element is the argument to the
1000 ** pragma or NULL if the pragma has no argument.  ^The handler for an
1001 ** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
1002 ** of the char** argument point to a string obtained from [sqlite3_mprintf()]
1003 ** or the equivalent and that string will become the result of the pragma or
1004 ** the error message if the pragma fails. ^If the
1005 ** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
1006 ** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
1007 ** file control returns [SQLITE_OK], then the parser assumes that the
1008 ** VFS has handled the PRAGMA itself and the parser generates a no-op
1009 ** prepared statement.  ^If the [SQLITE_FCNTL_PRAGMA] file control returns
1010 ** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
1011 ** that the VFS encountered an error while handling the [PRAGMA] and the
1012 ** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
1013 ** file control occurs at the beginning of pragma statement analysis and so
1014 ** it is able to override built-in [PRAGMA] statements.
1015 **
1016 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1017 ** ^The [SQLITE_FCNTL_BUSYHANDLER]
1018 ** file-control may be invoked by SQLite on the database file handle
1019 ** shortly after it is opened in order to provide a custom VFS with access
1020 ** to the connections busy-handler callback. The argument is of type (void **)
1021 ** - an array of two (void *) values. The first (void *) actually points
1022 ** to a function of type (int (*)(void *)). In order to invoke the connections
1023 ** busy-handler, this function should be invoked with the second (void *) in
1024 ** the array as the only argument. If it returns non-zero, then the operation
1025 ** should be retried. If it returns zero, the custom VFS should abandon the
1026 ** current operation.
1027 **
1028 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1029 ** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1030 ** to have SQLite generate a
1031 ** temporary filename using the same algorithm that is followed to generate
1032 ** temporary filenames for TEMP tables and other internal uses.  The
1033 ** argument should be a char** which will be filled with the filename
1034 ** written into memory obtained from [sqlite3_malloc()].  The caller should
1035 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
1036 **
1037 ** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1038 ** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1039 ** maximum number of bytes that will be used for memory-mapped I/O.
1040 ** The argument is a pointer to a value of type sqlite3_int64 that
1041 ** is an advisory maximum number of bytes in the file to memory map.  The
1042 ** pointer is overwritten with the old value.  The limit is not changed if
1043 ** the value originally pointed to is negative, and so the current limit
1044 ** can be queried by passing in a pointer to a negative number.  This
1045 ** file-control is used internally to implement [PRAGMA mmap_size].
1046 **
1047 ** <li>[[SQLITE_FCNTL_TRACE]]
1048 ** The [SQLITE_FCNTL_TRACE] file control provides advisory information
1049 ** to the VFS about what the higher layers of the SQLite stack are doing.
1050 ** This file control is used by some VFS activity tracing [shims].
1051 ** The argument is a zero-terminated string.  Higher layers in the
1052 ** SQLite stack may generate instances of this file control if
1053 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
1054 **
1055 ** <li>[[SQLITE_FCNTL_HAS_MOVED]]
1056 ** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
1057 ** pointer to an integer and it writes a boolean into that integer depending
1058 ** on whether or not the file has been renamed, moved, or deleted since it
1059 ** was first opened.
1060 **
1061 ** </ul>
1062 */
1063 #define SQLITE_FCNTL_LOCKSTATE               1
1064 #define SQLITE_GET_LOCKPROXYFILE             2
1065 #define SQLITE_SET_LOCKPROXYFILE             3
1066 #define SQLITE_LAST_ERRNO                    4
1067 #define SQLITE_FCNTL_SIZE_HINT               5
1068 #define SQLITE_FCNTL_CHUNK_SIZE              6
1069 #define SQLITE_FCNTL_FILE_POINTER            7
1070 #define SQLITE_FCNTL_SYNC_OMITTED            8
1071 #define SQLITE_FCNTL_WIN32_AV_RETRY          9
1072 #define SQLITE_FCNTL_PERSIST_WAL            10
1073 #define SQLITE_FCNTL_OVERWRITE              11
1074 #define SQLITE_FCNTL_VFSNAME                12
1075 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
1076 #define SQLITE_FCNTL_PRAGMA                 14
1077 #define SQLITE_FCNTL_BUSYHANDLER            15
1078 #define SQLITE_FCNTL_TEMPFILENAME           16
1079 #define SQLITE_FCNTL_MMAP_SIZE              18
1080 #define SQLITE_FCNTL_TRACE                  19
1081 #define SQLITE_FCNTL_HAS_MOVED              20
1082 #define SQLITE_FCNTL_SYNC                   21
1083 #define SQLITE_FCNTL_COMMIT_PHASETWO        22
1084 
1085 /*
1086 ** CAPI3REF: Mutex Handle
1087 **
1088 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
1089 ** abstract type for a mutex object.  The SQLite core never looks
1090 ** at the internal representation of an [sqlite3_mutex].  It only
1091 ** deals with pointers to the [sqlite3_mutex] object.
1092 **
1093 ** Mutexes are created using [sqlite3_mutex_alloc()].
1094 */
1095 typedef struct sqlite3_mutex sqlite3_mutex;
1096 
1097 /*
1098 ** CAPI3REF: OS Interface Object
1099 **
1100 ** An instance of the sqlite3_vfs object defines the interface between
1101 ** the SQLite core and the underlying operating system.  The "vfs"
1102 ** in the name of the object stands for "virtual file system".  See
1103 ** the [VFS | VFS documentation] for further information.
1104 **
1105 ** The value of the iVersion field is initially 1 but may be larger in
1106 ** future versions of SQLite.  Additional fields may be appended to this
1107 ** object when the iVersion value is increased.  Note that the structure
1108 ** of the sqlite3_vfs object changes in the transaction between
1109 ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
1110 ** modified.
1111 **
1112 ** The szOsFile field is the size of the subclassed [sqlite3_file]
1113 ** structure used by this VFS.  mxPathname is the maximum length of
1114 ** a pathname in this VFS.
1115 **
1116 ** Registered sqlite3_vfs objects are kept on a linked list formed by
1117 ** the pNext pointer.  The [sqlite3_vfs_register()]
1118 ** and [sqlite3_vfs_unregister()] interfaces manage this list
1119 ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
1120 ** searches the list.  Neither the application code nor the VFS
1121 ** implementation should use the pNext pointer.
1122 **
1123 ** The pNext field is the only field in the sqlite3_vfs
1124 ** structure that SQLite will ever modify.  SQLite will only access
1125 ** or modify this field while holding a particular static mutex.
1126 ** The application should never modify anything within the sqlite3_vfs
1127 ** object once the object has been registered.
1128 **
1129 ** The zName field holds the name of the VFS module.  The name must
1130 ** be unique across all VFS modules.
1131 **
1132 ** [[sqlite3_vfs.xOpen]]
1133 ** ^SQLite guarantees that the zFilename parameter to xOpen
1134 ** is either a NULL pointer or string obtained
1135 ** from xFullPathname() with an optional suffix added.
1136 ** ^If a suffix is added to the zFilename parameter, it will
1137 ** consist of a single "-" character followed by no more than
1138 ** 11 alphanumeric and/or "-" characters.
1139 ** ^SQLite further guarantees that
1140 ** the string will be valid and unchanged until xClose() is
1141 ** called. Because of the previous sentence,
1142 ** the [sqlite3_file] can safely store a pointer to the
1143 ** filename if it needs to remember the filename for some reason.
1144 ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1145 ** must invent its own temporary name for the file.  ^Whenever the
1146 ** xFilename parameter is NULL it will also be the case that the
1147 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1148 **
1149 ** The flags argument to xOpen() includes all bits set in
1150 ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
1151 ** or [sqlite3_open16()] is used, then flags includes at least
1152 ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1153 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1154 ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
1155 **
1156 ** ^(SQLite will also add one of the following flags to the xOpen()
1157 ** call, depending on the object being opened:
1158 **
1159 ** <ul>
1160 ** <li>  [SQLITE_OPEN_MAIN_DB]
1161 ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
1162 ** <li>  [SQLITE_OPEN_TEMP_DB]
1163 ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
1164 ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
1165 ** <li>  [SQLITE_OPEN_SUBJOURNAL]
1166 ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
1167 ** <li>  [SQLITE_OPEN_WAL]
1168 ** </ul>)^
1169 **
1170 ** The file I/O implementation can use the object type flags to
1171 ** change the way it deals with files.  For example, an application
1172 ** that does not care about crash recovery or rollback might make
1173 ** the open of a journal file a no-op.  Writes to this journal would
1174 ** also be no-ops, and any attempt to read the journal would return
1175 ** SQLITE_IOERR.  Or the implementation might recognize that a database
1176 ** file will be doing page-aligned sector reads and writes in a random
1177 ** order and set up its I/O subsystem accordingly.
1178 **
1179 ** SQLite might also add one of the following flags to the xOpen method:
1180 **
1181 ** <ul>
1182 ** <li> [SQLITE_OPEN_DELETEONCLOSE]
1183 ** <li> [SQLITE_OPEN_EXCLUSIVE]
1184 ** </ul>
1185 **
1186 ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1187 ** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
1188 ** will be set for TEMP databases and their journals, transient
1189 ** databases, and subjournals.
1190 **
1191 ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1192 ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1193 ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1194 ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1195 ** SQLITE_OPEN_CREATE, is used to indicate that file should always
1196 ** be created, and that it is an error if it already exists.
1197 ** It is <i>not</i> used to indicate the file should be opened
1198 ** for exclusive access.
1199 **
1200 ** ^At least szOsFile bytes of memory are allocated by SQLite
1201 ** to hold the  [sqlite3_file] structure passed as the third
1202 ** argument to xOpen.  The xOpen method does not have to
1203 ** allocate the structure; it should just fill it in.  Note that
1204 ** the xOpen method must set the sqlite3_file.pMethods to either
1205 ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
1206 ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
1207 ** element will be valid after xOpen returns regardless of the success
1208 ** or failure of the xOpen call.
1209 **
1210 ** [[sqlite3_vfs.xAccess]]
1211 ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1212 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1213 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1214 ** to test whether a file is at least readable.   The file can be a
1215 ** directory.
1216 **
1217 ** ^SQLite will always allocate at least mxPathname+1 bytes for the
1218 ** output buffer xFullPathname.  The exact size of the output buffer
1219 ** is also passed as a parameter to both  methods. If the output buffer
1220 ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1221 ** handled as a fatal error by SQLite, vfs implementations should endeavor
1222 ** to prevent this by setting mxPathname to a sufficiently large value.
1223 **
1224 ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1225 ** interfaces are not strictly a part of the filesystem, but they are
1226 ** included in the VFS structure for completeness.
1227 ** The xRandomness() function attempts to return nBytes bytes
1228 ** of good-quality randomness into zOut.  The return value is
1229 ** the actual number of bytes of randomness obtained.
1230 ** The xSleep() method causes the calling thread to sleep for at
1231 ** least the number of microseconds given.  ^The xCurrentTime()
1232 ** method returns a Julian Day Number for the current date and time as
1233 ** a floating point value.
1234 ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1235 ** Day Number multiplied by 86400000 (the number of milliseconds in
1236 ** a 24-hour day).
1237 ** ^SQLite will use the xCurrentTimeInt64() method to get the current
1238 ** date and time if that method is available (if iVersion is 2 or
1239 ** greater and the function pointer is not NULL) and will fall back
1240 ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1241 **
1242 ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1243 ** are not used by the SQLite core.  These optional interfaces are provided
1244 ** by some VFSes to facilitate testing of the VFS code. By overriding
1245 ** system calls with functions under its control, a test program can
1246 ** simulate faults and error conditions that would otherwise be difficult
1247 ** or impossible to induce.  The set of system calls that can be overridden
1248 ** varies from one VFS to another, and from one version of the same VFS to the
1249 ** next.  Applications that use these interfaces must be prepared for any
1250 ** or all of these interfaces to be NULL or for their behavior to change
1251 ** from one release to the next.  Applications must not attempt to access
1252 ** any of these methods if the iVersion of the VFS is less than 3.
1253 */
1254 typedef struct sqlite3_vfs sqlite3_vfs;
1255 typedef void (*sqlite3_syscall_ptr)(void);
1256 struct sqlite3_vfs {
1257   int iVersion;            /* Structure version number (currently 3) */
1258   int szOsFile;            /* Size of subclassed sqlite3_file */
1259   int mxPathname;          /* Maximum file pathname length */
1260   sqlite3_vfs *pNext;      /* Next registered VFS */
1261   const char *zName;       /* Name of this virtual file system */
1262   void *pAppData;          /* Pointer to application-specific data */
1263   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1264                int flags, int *pOutFlags);
1265   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1266   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1267   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1268   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1269   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1270   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1271   void (*xDlClose)(sqlite3_vfs*, void*);
1272   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1273   int (*xSleep)(sqlite3_vfs*, int microseconds);
1274   int (*xCurrentTime)(sqlite3_vfs*, double*);
1275   int (*xGetLastError)(sqlite3_vfs*, int, char *);
1276   /*
1277   ** The methods above are in version 1 of the sqlite_vfs object
1278   ** definition.  Those that follow are added in version 2 or later
1279   */
1280   int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1281   /*
1282   ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1283   ** Those below are for version 3 and greater.
1284   */
1285   int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1286   sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1287   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1288   /*
1289   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1290   ** New fields may be appended in figure versions.  The iVersion
1291   ** value will increment whenever this happens.
1292   */
1293 };
1294 
1295 /*
1296 ** CAPI3REF: Flags for the xAccess VFS method
1297 **
1298 ** These integer constants can be used as the third parameter to
1299 ** the xAccess method of an [sqlite3_vfs] object.  They determine
1300 ** what kind of permissions the xAccess method is looking for.
1301 ** With SQLITE_ACCESS_EXISTS, the xAccess method
1302 ** simply checks whether the file exists.
1303 ** With SQLITE_ACCESS_READWRITE, the xAccess method
1304 ** checks whether the named directory is both readable and writable
1305 ** (in other words, if files can be added, removed, and renamed within
1306 ** the directory).
1307 ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1308 ** [temp_store_directory pragma], though this could change in a future
1309 ** release of SQLite.
1310 ** With SQLITE_ACCESS_READ, the xAccess method
1311 ** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
1312 ** currently unused, though it might be used in a future release of
1313 ** SQLite.
1314 */
1315 #define SQLITE_ACCESS_EXISTS    0
1316 #define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
1317 #define SQLITE_ACCESS_READ      2   /* Unused */
1318 
1319 /*
1320 ** CAPI3REF: Flags for the xShmLock VFS method
1321 **
1322 ** These integer constants define the various locking operations
1323 ** allowed by the xShmLock method of [sqlite3_io_methods].  The
1324 ** following are the only legal combinations of flags to the
1325 ** xShmLock method:
1326 **
1327 ** <ul>
1328 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1329 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1330 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1331 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1332 ** </ul>
1333 **
1334 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1335 ** was given no the corresponding lock.
1336 **
1337 ** The xShmLock method can transition between unlocked and SHARED or
1338 ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
1339 ** and EXCLUSIVE.
1340 */
1341 #define SQLITE_SHM_UNLOCK       1
1342 #define SQLITE_SHM_LOCK         2
1343 #define SQLITE_SHM_SHARED       4
1344 #define SQLITE_SHM_EXCLUSIVE    8
1345 
1346 /*
1347 ** CAPI3REF: Maximum xShmLock index
1348 **
1349 ** The xShmLock method on [sqlite3_io_methods] may use values
1350 ** between 0 and this upper bound as its "offset" argument.
1351 ** The SQLite core will never attempt to acquire or release a
1352 ** lock outside of this range
1353 */
1354 #define SQLITE_SHM_NLOCK        8
1355 
1356 
1357 /*
1358 ** CAPI3REF: Initialize The SQLite Library
1359 **
1360 ** ^The sqlite3_initialize() routine initializes the
1361 ** SQLite library.  ^The sqlite3_shutdown() routine
1362 ** deallocates any resources that were allocated by sqlite3_initialize().
1363 ** These routines are designed to aid in process initialization and
1364 ** shutdown on embedded systems.  Workstation applications using
1365 ** SQLite normally do not need to invoke either of these routines.
1366 **
1367 ** A call to sqlite3_initialize() is an "effective" call if it is
1368 ** the first time sqlite3_initialize() is invoked during the lifetime of
1369 ** the process, or if it is the first time sqlite3_initialize() is invoked
1370 ** following a call to sqlite3_shutdown().  ^(Only an effective call
1371 ** of sqlite3_initialize() does any initialization.  All other calls
1372 ** are harmless no-ops.)^
1373 **
1374 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
1375 ** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
1376 ** an effective call to sqlite3_shutdown() does any deinitialization.
1377 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1378 **
1379 ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1380 ** is not.  The sqlite3_shutdown() interface must only be called from a
1381 ** single thread.  All open [database connections] must be closed and all
1382 ** other SQLite resources must be deallocated prior to invoking
1383 ** sqlite3_shutdown().
1384 **
1385 ** Among other things, ^sqlite3_initialize() will invoke
1386 ** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
1387 ** will invoke sqlite3_os_end().
1388 **
1389 ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1390 ** ^If for some reason, sqlite3_initialize() is unable to initialize
1391 ** the library (perhaps it is unable to allocate a needed resource such
1392 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
1393 **
1394 ** ^The sqlite3_initialize() routine is called internally by many other
1395 ** SQLite interfaces so that an application usually does not need to
1396 ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
1397 ** calls sqlite3_initialize() so the SQLite library will be automatically
1398 ** initialized when [sqlite3_open()] is called if it has not be initialized
1399 ** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1400 ** compile-time option, then the automatic calls to sqlite3_initialize()
1401 ** are omitted and the application must call sqlite3_initialize() directly
1402 ** prior to using any other SQLite interface.  For maximum portability,
1403 ** it is recommended that applications always invoke sqlite3_initialize()
1404 ** directly prior to using any other SQLite interface.  Future releases
1405 ** of SQLite may require this.  In other words, the behavior exhibited
1406 ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1407 ** default behavior in some future release of SQLite.
1408 **
1409 ** The sqlite3_os_init() routine does operating-system specific
1410 ** initialization of the SQLite library.  The sqlite3_os_end()
1411 ** routine undoes the effect of sqlite3_os_init().  Typical tasks
1412 ** performed by these routines include allocation or deallocation
1413 ** of static resources, initialization of global variables,
1414 ** setting up a default [sqlite3_vfs] module, or setting up
1415 ** a default configuration using [sqlite3_config()].
1416 **
1417 ** The application should never invoke either sqlite3_os_init()
1418 ** or sqlite3_os_end() directly.  The application should only invoke
1419 ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
1420 ** interface is called automatically by sqlite3_initialize() and
1421 ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
1422 ** implementations for sqlite3_os_init() and sqlite3_os_end()
1423 ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1424 ** When [custom builds | built for other platforms]
1425 ** (using the [SQLITE_OS_OTHER=1] compile-time
1426 ** option) the application must supply a suitable implementation for
1427 ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
1428 ** implementation of sqlite3_os_init() or sqlite3_os_end()
1429 ** must return [SQLITE_OK] on success and some other [error code] upon
1430 ** failure.
1431 */
1432 SQLITE_API int sqlite3_initialize(void);
1433 SQLITE_API int sqlite3_shutdown(void);
1434 SQLITE_API int sqlite3_os_init(void);
1435 SQLITE_API int sqlite3_os_end(void);
1436 
1437 /*
1438 ** CAPI3REF: Configuring The SQLite Library
1439 **
1440 ** The sqlite3_config() interface is used to make global configuration
1441 ** changes to SQLite in order to tune SQLite to the specific needs of
1442 ** the application.  The default configuration is recommended for most
1443 ** applications and so this routine is usually not necessary.  It is
1444 ** provided to support rare applications with unusual needs.
1445 **
1446 ** The sqlite3_config() interface is not threadsafe.  The application
1447 ** must insure that no other SQLite interfaces are invoked by other
1448 ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
1449 ** may only be invoked prior to library initialization using
1450 ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1451 ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1452 ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1453 ** Note, however, that ^sqlite3_config() can be called as part of the
1454 ** implementation of an application-defined [sqlite3_os_init()].
1455 **
1456 ** The first argument to sqlite3_config() is an integer
1457 ** [configuration option] that determines
1458 ** what property of SQLite is to be configured.  Subsequent arguments
1459 ** vary depending on the [configuration option]
1460 ** in the first argument.
1461 **
1462 ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1463 ** ^If the option is unknown or SQLite is unable to set the option
1464 ** then this routine returns a non-zero [error code].
1465 */
1466 SQLITE_API int sqlite3_config(int, ...);
1467 
1468 /*
1469 ** CAPI3REF: Configure database connections
1470 **
1471 ** The sqlite3_db_config() interface is used to make configuration
1472 ** changes to a [database connection].  The interface is similar to
1473 ** [sqlite3_config()] except that the changes apply to a single
1474 ** [database connection] (specified in the first argument).
1475 **
1476 ** The second argument to sqlite3_db_config(D,V,...)  is the
1477 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1478 ** that indicates what aspect of the [database connection] is being configured.
1479 ** Subsequent arguments vary depending on the configuration verb.
1480 **
1481 ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1482 ** the call is considered successful.
1483 */
1484 SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1485 
1486 /*
1487 ** CAPI3REF: Memory Allocation Routines
1488 **
1489 ** An instance of this object defines the interface between SQLite
1490 ** and low-level memory allocation routines.
1491 **
1492 ** This object is used in only one place in the SQLite interface.
1493 ** A pointer to an instance of this object is the argument to
1494 ** [sqlite3_config()] when the configuration option is
1495 ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1496 ** By creating an instance of this object
1497 ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1498 ** during configuration, an application can specify an alternative
1499 ** memory allocation subsystem for SQLite to use for all of its
1500 ** dynamic memory needs.
1501 **
1502 ** Note that SQLite comes with several [built-in memory allocators]
1503 ** that are perfectly adequate for the overwhelming majority of applications
1504 ** and that this object is only useful to a tiny minority of applications
1505 ** with specialized memory allocation requirements.  This object is
1506 ** also used during testing of SQLite in order to specify an alternative
1507 ** memory allocator that simulates memory out-of-memory conditions in
1508 ** order to verify that SQLite recovers gracefully from such
1509 ** conditions.
1510 **
1511 ** The xMalloc, xRealloc, and xFree methods must work like the
1512 ** malloc(), realloc() and free() functions from the standard C library.
1513 ** ^SQLite guarantees that the second argument to
1514 ** xRealloc is always a value returned by a prior call to xRoundup.
1515 **
1516 ** xSize should return the allocated size of a memory allocation
1517 ** previously obtained from xMalloc or xRealloc.  The allocated size
1518 ** is always at least as big as the requested size but may be larger.
1519 **
1520 ** The xRoundup method returns what would be the allocated size of
1521 ** a memory allocation given a particular requested size.  Most memory
1522 ** allocators round up memory allocations at least to the next multiple
1523 ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
1524 ** Every memory allocation request coming in through [sqlite3_malloc()]
1525 ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
1526 ** that causes the corresponding memory allocation to fail.
1527 **
1528 ** The xInit method initializes the memory allocator.  For example,
1529 ** it might allocate any require mutexes or initialize internal data
1530 ** structures.  The xShutdown method is invoked (indirectly) by
1531 ** [sqlite3_shutdown()] and should deallocate any resources acquired
1532 ** by xInit.  The pAppData pointer is used as the only parameter to
1533 ** xInit and xShutdown.
1534 **
1535 ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
1536 ** the xInit method, so the xInit method need not be threadsafe.  The
1537 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
1538 ** not need to be threadsafe either.  For all other methods, SQLite
1539 ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
1540 ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
1541 ** it is by default) and so the methods are automatically serialized.
1542 ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
1543 ** methods must be threadsafe or else make their own arrangements for
1544 ** serialization.
1545 **
1546 ** SQLite will never invoke xInit() more than once without an intervening
1547 ** call to xShutdown().
1548 */
1549 typedef struct sqlite3_mem_methods sqlite3_mem_methods;
1550 struct sqlite3_mem_methods {
1551   void *(*xMalloc)(int);         /* Memory allocation function */
1552   void (*xFree)(void*);          /* Free a prior allocation */
1553   void *(*xRealloc)(void*,int);  /* Resize an allocation */
1554   int (*xSize)(void*);           /* Return the size of an allocation */
1555   int (*xRoundup)(int);          /* Round up request size to allocation size */
1556   int (*xInit)(void*);           /* Initialize the memory allocator */
1557   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
1558   void *pAppData;                /* Argument to xInit() and xShutdown() */
1559 };
1560 
1561 /*
1562 ** CAPI3REF: Configuration Options
1563 ** KEYWORDS: {configuration option}
1564 **
1565 ** These constants are the available integer configuration options that
1566 ** can be passed as the first argument to the [sqlite3_config()] interface.
1567 **
1568 ** New configuration options may be added in future releases of SQLite.
1569 ** Existing configuration options might be discontinued.  Applications
1570 ** should check the return code from [sqlite3_config()] to make sure that
1571 ** the call worked.  The [sqlite3_config()] interface will return a
1572 ** non-zero [error code] if a discontinued or unsupported configuration option
1573 ** is invoked.
1574 **
1575 ** <dl>
1576 ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
1577 ** <dd>There are no arguments to this option.  ^This option sets the
1578 ** [threading mode] to Single-thread.  In other words, it disables
1579 ** all mutexing and puts SQLite into a mode where it can only be used
1580 ** by a single thread.   ^If SQLite is compiled with
1581 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1582 ** it is not possible to change the [threading mode] from its default
1583 ** value of Single-thread and so [sqlite3_config()] will return
1584 ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
1585 ** configuration option.</dd>
1586 **
1587 ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
1588 ** <dd>There are no arguments to this option.  ^This option sets the
1589 ** [threading mode] to Multi-thread.  In other words, it disables
1590 ** mutexing on [database connection] and [prepared statement] objects.
1591 ** The application is responsible for serializing access to
1592 ** [database connections] and [prepared statements].  But other mutexes
1593 ** are enabled so that SQLite will be safe to use in a multi-threaded
1594 ** environment as long as no two threads attempt to use the same
1595 ** [database connection] at the same time.  ^If SQLite is compiled with
1596 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1597 ** it is not possible to set the Multi-thread [threading mode] and
1598 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1599 ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
1600 **
1601 ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
1602 ** <dd>There are no arguments to this option.  ^This option sets the
1603 ** [threading mode] to Serialized. In other words, this option enables
1604 ** all mutexes including the recursive
1605 ** mutexes on [database connection] and [prepared statement] objects.
1606 ** In this mode (which is the default when SQLite is compiled with
1607 ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
1608 ** to [database connections] and [prepared statements] so that the
1609 ** application is free to use the same [database connection] or the
1610 ** same [prepared statement] in different threads at the same time.
1611 ** ^If SQLite is compiled with
1612 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1613 ** it is not possible to set the Serialized [threading mode] and
1614 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1615 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1616 **
1617 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1618 ** <dd> ^(This option takes a single argument which is a pointer to an
1619 ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
1620 ** alternative low-level memory allocation routines to be used in place of
1621 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
1622 ** its own private copy of the content of the [sqlite3_mem_methods] structure
1623 ** before the [sqlite3_config()] call returns.</dd>
1624 **
1625 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1626 ** <dd> ^(This option takes a single argument which is a pointer to an
1627 ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
1628 ** structure is filled with the currently defined memory allocation routines.)^
1629 ** This option can be used to overload the default memory allocation
1630 ** routines with a wrapper that simulations memory allocation failure or
1631 ** tracks memory usage, for example. </dd>
1632 **
1633 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1634 ** <dd> ^This option takes single argument of type int, interpreted as a
1635 ** boolean, which enables or disables the collection of memory allocation
1636 ** statistics. ^(When memory allocation statistics are disabled, the
1637 ** following SQLite interfaces become non-operational:
1638 **   <ul>
1639 **   <li> [sqlite3_memory_used()]
1640 **   <li> [sqlite3_memory_highwater()]
1641 **   <li> [sqlite3_soft_heap_limit64()]
1642 **   <li> [sqlite3_status()]
1643 **   </ul>)^
1644 ** ^Memory allocation statistics are enabled by default unless SQLite is
1645 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1646 ** allocation statistics are disabled by default.
1647 ** </dd>
1648 **
1649 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1650 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1651 ** scratch memory.  There are three arguments:  A pointer an 8-byte
1652 ** aligned memory buffer from which the scratch allocations will be
1653 ** drawn, the size of each scratch allocation (sz),
1654 ** and the maximum number of scratch allocations (N).  The sz
1655 ** argument must be a multiple of 16.
1656 ** The first argument must be a pointer to an 8-byte aligned buffer
1657 ** of at least sz*N bytes of memory.
1658 ** ^SQLite will use no more than two scratch buffers per thread.  So
1659 ** N should be set to twice the expected maximum number of threads.
1660 ** ^SQLite will never require a scratch buffer that is more than 6
1661 ** times the database page size. ^If SQLite needs needs additional
1662 ** scratch memory beyond what is provided by this configuration option, then
1663 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
1664 **
1665 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1666 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1667 ** the database page cache with the default page cache implementation.
1668 ** This configuration should not be used if an application-define page
1669 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1670 ** There are three arguments to this option: A pointer to 8-byte aligned
1671 ** memory, the size of each page buffer (sz), and the number of pages (N).
1672 ** The sz argument should be the size of the largest database page
1673 ** (a power of two between 512 and 32768) plus a little extra for each
1674 ** page header.  ^The page header size is 20 to 40 bytes depending on
1675 ** the host architecture.  ^It is harmless, apart from the wasted memory,
1676 ** to make sz a little too large.  The first
1677 ** argument should point to an allocation of at least sz*N bytes of memory.
1678 ** ^SQLite will use the memory provided by the first argument to satisfy its
1679 ** memory needs for the first N pages that it adds to cache.  ^If additional
1680 ** page cache memory is needed beyond what is provided by this option, then
1681 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1682 ** The pointer in the first argument must
1683 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1684 ** will be undefined.</dd>
1685 **
1686 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1687 ** <dd> ^This option specifies a static memory buffer that SQLite will use
1688 ** for all of its dynamic memory allocation needs beyond those provided
1689 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1690 ** There are three arguments: An 8-byte aligned pointer to the memory,
1691 ** the number of bytes in the memory buffer, and the minimum allocation size.
1692 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1693 ** to using its default memory allocator (the system malloc() implementation),
1694 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
1695 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1696 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1697 ** allocator is engaged to handle all of SQLites memory allocation needs.
1698 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1699 ** boundary or subsequent behavior of SQLite will be undefined.
1700 ** The minimum allocation size is capped at 2**12. Reasonable values
1701 ** for the minimum allocation size are 2**5 through 2**8.</dd>
1702 **
1703 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1704 ** <dd> ^(This option takes a single argument which is a pointer to an
1705 ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
1706 ** alternative low-level mutex routines to be used in place
1707 ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
1708 ** content of the [sqlite3_mutex_methods] structure before the call to
1709 ** [sqlite3_config()] returns. ^If SQLite is compiled with
1710 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1711 ** the entire mutexing subsystem is omitted from the build and hence calls to
1712 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1713 ** return [SQLITE_ERROR].</dd>
1714 **
1715 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1716 ** <dd> ^(This option takes a single argument which is a pointer to an
1717 ** instance of the [sqlite3_mutex_methods] structure.  The
1718 ** [sqlite3_mutex_methods]
1719 ** structure is filled with the currently defined mutex routines.)^
1720 ** This option can be used to overload the default mutex allocation
1721 ** routines with a wrapper used to track mutex usage for performance
1722 ** profiling or testing, for example.   ^If SQLite is compiled with
1723 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1724 ** the entire mutexing subsystem is omitted from the build and hence calls to
1725 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1726 ** return [SQLITE_ERROR].</dd>
1727 **
1728 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1729 ** <dd> ^(This option takes two arguments that determine the default
1730 ** memory allocation for the lookaside memory allocator on each
1731 ** [database connection].  The first argument is the
1732 ** size of each lookaside buffer slot and the second is the number of
1733 ** slots allocated to each database connection.)^  ^(This option sets the
1734 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1735 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1736 ** configuration on individual connections.)^ </dd>
1737 **
1738 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1739 ** <dd> ^(This option takes a single argument which is a pointer to
1740 ** an [sqlite3_pcache_methods2] object.  This object specifies the interface
1741 ** to a custom page cache implementation.)^  ^SQLite makes a copy of the
1742 ** object and uses it for page cache memory allocations.</dd>
1743 **
1744 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1745 ** <dd> ^(This option takes a single argument which is a pointer to an
1746 ** [sqlite3_pcache_methods2] object.  SQLite copies of the current
1747 ** page cache implementation into that object.)^ </dd>
1748 **
1749 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1750 ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1751 ** global [error log].
1752 ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1753 ** function with a call signature of void(*)(void*,int,const char*),
1754 ** and a pointer to void. ^If the function pointer is not NULL, it is
1755 ** invoked by [sqlite3_log()] to process each logging event.  ^If the
1756 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
1757 ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
1758 ** passed through as the first parameter to the application-defined logger
1759 ** function whenever that function is invoked.  ^The second parameter to
1760 ** the logger function is a copy of the first parameter to the corresponding
1761 ** [sqlite3_log()] call and is intended to be a [result code] or an
1762 ** [extended result code].  ^The third parameter passed to the logger is
1763 ** log message after formatting via [sqlite3_snprintf()].
1764 ** The SQLite logging interface is not reentrant; the logger function
1765 ** supplied by the application must not invoke any SQLite interface.
1766 ** In a multi-threaded application, the application-defined logger
1767 ** function must be threadsafe. </dd>
1768 **
1769 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1770 ** <dd>^(This option takes a single argument of type int. If non-zero, then
1771 ** URI handling is globally enabled. If the parameter is zero, then URI handling
1772 ** is globally disabled.)^ ^If URI handling is globally enabled, all filenames
1773 ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1774 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1775 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1776 ** connection is opened. ^If it is globally disabled, filenames are
1777 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1778 ** database connection is opened. ^(By default, URI handling is globally
1779 ** disabled. The default value may be changed by compiling with the
1780 ** [SQLITE_USE_URI] symbol defined.)^
1781 **
1782 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1783 ** <dd>^This option takes a single integer argument which is interpreted as
1784 ** a boolean in order to enable or disable the use of covering indices for
1785 ** full table scans in the query optimizer.  ^The default setting is determined
1786 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1787 ** if that compile-time option is omitted.
1788 ** The ability to disable the use of covering indices for full table scans
1789 ** is because some incorrectly coded legacy applications might malfunction
1790 ** when the optimization is enabled.  Providing the ability to
1791 ** disable the optimization allows the older, buggy application code to work
1792 ** without change even with newer versions of SQLite.
1793 **
1794 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1795 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
1796 ** <dd> These options are obsolete and should not be used by new code.
1797 ** They are retained for backwards compatibility but are now no-ops.
1798 ** </dd>
1799 **
1800 ** [[SQLITE_CONFIG_SQLLOG]]
1801 ** <dt>SQLITE_CONFIG_SQLLOG
1802 ** <dd>This option is only available if sqlite is compiled with the
1803 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
1804 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
1805 ** The second should be of type (void*). The callback is invoked by the library
1806 ** in three separate circumstances, identified by the value passed as the
1807 ** fourth parameter. If the fourth parameter is 0, then the database connection
1808 ** passed as the second argument has just been opened. The third argument
1809 ** points to a buffer containing the name of the main database file. If the
1810 ** fourth parameter is 1, then the SQL statement that the third parameter
1811 ** points to has just been executed. Or, if the fourth parameter is 2, then
1812 ** the connection being passed as the second parameter is being closed. The
1813 ** third parameter is passed NULL In this case.  An example of using this
1814 ** configuration option can be seen in the "test_sqllog.c" source file in
1815 ** the canonical SQLite source tree.</dd>
1816 **
1817 ** [[SQLITE_CONFIG_MMAP_SIZE]]
1818 ** <dt>SQLITE_CONFIG_MMAP_SIZE
1819 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
1820 ** that are the default mmap size limit (the default setting for
1821 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1822 ** ^The default setting can be overridden by each database connection using
1823 ** either the [PRAGMA mmap_size] command, or by using the
1824 ** [SQLITE_FCNTL_MMAP_SIZE] file control.  ^(The maximum allowed mmap size
1825 ** cannot be changed at run-time.  Nor may the maximum allowed mmap size
1826 ** exceed the compile-time maximum mmap size set by the
1827 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1828 ** ^If either argument to this option is negative, then that argument is
1829 ** changed to its compile-time default.
1830 **
1831 ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1832 ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1833 ** <dd>^This option is only available if SQLite is compiled for Windows
1834 ** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1835 ** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1836 ** that specifies the maximum size of the created heap.
1837 ** </dl>
1838 */
1839 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
1840 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
1841 #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
1842 #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
1843 #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
1844 #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
1845 #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
1846 #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
1847 #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
1848 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
1849 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
1850 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
1851 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
1852 #define SQLITE_CONFIG_PCACHE       14  /* no-op */
1853 #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
1854 #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
1855 #define SQLITE_CONFIG_URI          17  /* int */
1856 #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
1857 #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
1858 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
1859 #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
1860 #define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
1861 #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
1862 
1863 /*
1864 ** CAPI3REF: Database Connection Configuration Options
1865 **
1866 ** These constants are the available integer configuration options that
1867 ** can be passed as the second argument to the [sqlite3_db_config()] interface.
1868 **
1869 ** New configuration options may be added in future releases of SQLite.
1870 ** Existing configuration options might be discontinued.  Applications
1871 ** should check the return code from [sqlite3_db_config()] to make sure that
1872 ** the call worked.  ^The [sqlite3_db_config()] interface will return a
1873 ** non-zero [error code] if a discontinued or unsupported configuration option
1874 ** is invoked.
1875 **
1876 ** <dl>
1877 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
1878 ** <dd> ^This option takes three additional arguments that determine the
1879 ** [lookaside memory allocator] configuration for the [database connection].
1880 ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
1881 ** pointer to a memory buffer to use for lookaside memory.
1882 ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
1883 ** may be NULL in which case SQLite will allocate the
1884 ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
1885 ** size of each lookaside buffer slot.  ^The third argument is the number of
1886 ** slots.  The size of the buffer in the first argument must be greater than
1887 ** or equal to the product of the second and third arguments.  The buffer
1888 ** must be aligned to an 8-byte boundary.  ^If the second argument to
1889 ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
1890 ** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
1891 ** configuration for a database connection can only be changed when that
1892 ** connection is not currently using lookaside memory, or in other words
1893 ** when the "current value" returned by
1894 ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
1895 ** Any attempt to change the lookaside memory configuration when lookaside
1896 ** memory is in use leaves the configuration unchanged and returns
1897 ** [SQLITE_BUSY].)^</dd>
1898 **
1899 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
1900 ** <dd> ^This option is used to enable or disable the enforcement of
1901 ** [foreign key constraints].  There should be two additional arguments.
1902 ** The first argument is an integer which is 0 to disable FK enforcement,
1903 ** positive to enable FK enforcement or negative to leave FK enforcement
1904 ** unchanged.  The second parameter is a pointer to an integer into which
1905 ** is written 0 or 1 to indicate whether FK enforcement is off or on
1906 ** following this call.  The second parameter may be a NULL pointer, in
1907 ** which case the FK enforcement setting is not reported back. </dd>
1908 **
1909 ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
1910 ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
1911 ** There should be two additional arguments.
1912 ** The first argument is an integer which is 0 to disable triggers,
1913 ** positive to enable triggers or negative to leave the setting unchanged.
1914 ** The second parameter is a pointer to an integer into which
1915 ** is written 0 or 1 to indicate whether triggers are disabled or enabled
1916 ** following this call.  The second parameter may be a NULL pointer, in
1917 ** which case the trigger setting is not reported back. </dd>
1918 **
1919 ** </dl>
1920 */
1921 #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
1922 #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
1923 #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
1924 
1925 
1926 /*
1927 ** CAPI3REF: Enable Or Disable Extended Result Codes
1928 **
1929 ** ^The sqlite3_extended_result_codes() routine enables or disables the
1930 ** [extended result codes] feature of SQLite. ^The extended result
1931 ** codes are disabled by default for historical compatibility.
1932 */
1933 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
1934 
1935 /*
1936 ** CAPI3REF: Last Insert Rowid
1937 **
1938 ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
1939 ** has a unique 64-bit signed
1940 ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
1941 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
1942 ** names are not also used by explicitly declared columns. ^If
1943 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
1944 ** is another alias for the rowid.
1945 **
1946 ** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the
1947 ** most recent successful [INSERT] into a rowid table or [virtual table]
1948 ** on database connection D.
1949 ** ^Inserts into [WITHOUT ROWID] tables are not recorded.
1950 ** ^If no successful [INSERT]s into rowid tables
1951 ** have ever occurred on the database connection D,
1952 ** then sqlite3_last_insert_rowid(D) returns zero.
1953 **
1954 ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
1955 ** method, then this routine will return the [rowid] of the inserted
1956 ** row as long as the trigger or virtual table method is running.
1957 ** But once the trigger or virtual table method ends, the value returned
1958 ** by this routine reverts to what it was before the trigger or virtual
1959 ** table method began.)^
1960 **
1961 ** ^An [INSERT] that fails due to a constraint violation is not a
1962 ** successful [INSERT] and does not change the value returned by this
1963 ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
1964 ** and INSERT OR ABORT make no changes to the return value of this
1965 ** routine when their insertion fails.  ^(When INSERT OR REPLACE
1966 ** encounters a constraint violation, it does not fail.  The
1967 ** INSERT continues to completion after deleting rows that caused
1968 ** the constraint problem so INSERT OR REPLACE will always change
1969 ** the return value of this interface.)^
1970 **
1971 ** ^For the purposes of this routine, an [INSERT] is considered to
1972 ** be successful even if it is subsequently rolled back.
1973 **
1974 ** This function is accessible to SQL statements via the
1975 ** [last_insert_rowid() SQL function].
1976 **
1977 ** If a separate thread performs a new [INSERT] on the same
1978 ** database connection while the [sqlite3_last_insert_rowid()]
1979 ** function is running and thus changes the last insert [rowid],
1980 ** then the value returned by [sqlite3_last_insert_rowid()] is
1981 ** unpredictable and might not equal either the old or the new
1982 ** last insert [rowid].
1983 */
1984 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
1985 
1986 /*
1987 ** CAPI3REF: Count The Number Of Rows Modified
1988 **
1989 ** ^This function returns the number of database rows that were changed
1990 ** or inserted or deleted by the most recently completed SQL statement
1991 ** on the [database connection] specified by the first parameter.
1992 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
1993 ** or [DELETE] statement are counted.  Auxiliary changes caused by
1994 ** triggers or [foreign key actions] are not counted.)^ Use the
1995 ** [sqlite3_total_changes()] function to find the total number of changes
1996 ** including changes caused by triggers and foreign key actions.
1997 **
1998 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
1999 ** are not counted.  Only real table changes are counted.
2000 **
2001 ** ^(A "row change" is a change to a single row of a single table
2002 ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
2003 ** are changed as side effects of [REPLACE] constraint resolution,
2004 ** rollback, ABORT processing, [DROP TABLE], or by any other
2005 ** mechanisms do not count as direct row changes.)^
2006 **
2007 ** A "trigger context" is a scope of execution that begins and
2008 ** ends with the script of a [CREATE TRIGGER | trigger].
2009 ** Most SQL statements are
2010 ** evaluated outside of any trigger.  This is the "top level"
2011 ** trigger context.  If a trigger fires from the top level, a
2012 ** new trigger context is entered for the duration of that one
2013 ** trigger.  Subtriggers create subcontexts for their duration.
2014 **
2015 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
2016 ** not create a new trigger context.
2017 **
2018 ** ^This function returns the number of direct row changes in the
2019 ** most recent INSERT, UPDATE, or DELETE statement within the same
2020 ** trigger context.
2021 **
2022 ** ^Thus, when called from the top level, this function returns the
2023 ** number of changes in the most recent INSERT, UPDATE, or DELETE
2024 ** that also occurred at the top level.  ^(Within the body of a trigger,
2025 ** the sqlite3_changes() interface can be called to find the number of
2026 ** changes in the most recently completed INSERT, UPDATE, or DELETE
2027 ** statement within the body of the same trigger.
2028 ** However, the number returned does not include changes
2029 ** caused by subtriggers since those have their own context.)^
2030 **
2031 ** See also the [sqlite3_total_changes()] interface, the
2032 ** [count_changes pragma], and the [changes() SQL function].
2033 **
2034 ** If a separate thread makes changes on the same database connection
2035 ** while [sqlite3_changes()] is running then the value returned
2036 ** is unpredictable and not meaningful.
2037 */
2038 SQLITE_API int sqlite3_changes(sqlite3*);
2039 
2040 /*
2041 ** CAPI3REF: Total Number Of Rows Modified
2042 **
2043 ** ^This function returns the number of row changes caused by [INSERT],
2044 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
2045 ** ^(The count returned by sqlite3_total_changes() includes all changes
2046 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
2047 ** [foreign key actions]. However,
2048 ** the count does not include changes used to implement [REPLACE] constraints,
2049 ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
2050 ** count does not include rows of views that fire an [INSTEAD OF trigger],
2051 ** though if the INSTEAD OF trigger makes changes of its own, those changes
2052 ** are counted.)^
2053 ** ^The sqlite3_total_changes() function counts the changes as soon as
2054 ** the statement that makes them is completed (when the statement handle
2055 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
2056 **
2057 ** See also the [sqlite3_changes()] interface, the
2058 ** [count_changes pragma], and the [total_changes() SQL function].
2059 **
2060 ** If a separate thread makes changes on the same database connection
2061 ** while [sqlite3_total_changes()] is running then the value
2062 ** returned is unpredictable and not meaningful.
2063 */
2064 SQLITE_API int sqlite3_total_changes(sqlite3*);
2065 
2066 /*
2067 ** CAPI3REF: Interrupt A Long-Running Query
2068 **
2069 ** ^This function causes any pending database operation to abort and
2070 ** return at its earliest opportunity. This routine is typically
2071 ** called in response to a user action such as pressing "Cancel"
2072 ** or Ctrl-C where the user wants a long query operation to halt
2073 ** immediately.
2074 **
2075 ** ^It is safe to call this routine from a thread different from the
2076 ** thread that is currently running the database operation.  But it
2077 ** is not safe to call this routine with a [database connection] that
2078 ** is closed or might close before sqlite3_interrupt() returns.
2079 **
2080 ** ^If an SQL operation is very nearly finished at the time when
2081 ** sqlite3_interrupt() is called, then it might not have an opportunity
2082 ** to be interrupted and might continue to completion.
2083 **
2084 ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
2085 ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
2086 ** that is inside an explicit transaction, then the entire transaction
2087 ** will be rolled back automatically.
2088 **
2089 ** ^The sqlite3_interrupt(D) call is in effect until all currently running
2090 ** SQL statements on [database connection] D complete.  ^Any new SQL statements
2091 ** that are started after the sqlite3_interrupt() call and before the
2092 ** running statements reaches zero are interrupted as if they had been
2093 ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
2094 ** that are started after the running statement count reaches zero are
2095 ** not effected by the sqlite3_interrupt().
2096 ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2097 ** SQL statements is a no-op and has no effect on SQL statements
2098 ** that are started after the sqlite3_interrupt() call returns.
2099 **
2100 ** If the database connection closes while [sqlite3_interrupt()]
2101 ** is running then bad things will likely happen.
2102 */
2103 SQLITE_API void sqlite3_interrupt(sqlite3*);
2104 
2105 /*
2106 ** CAPI3REF: Determine If An SQL Statement Is Complete
2107 **
2108 ** These routines are useful during command-line input to determine if the
2109 ** currently entered text seems to form a complete SQL statement or
2110 ** if additional input is needed before sending the text into
2111 ** SQLite for parsing.  ^These routines return 1 if the input string
2112 ** appears to be a complete SQL statement.  ^A statement is judged to be
2113 ** complete if it ends with a semicolon token and is not a prefix of a
2114 ** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
2115 ** string literals or quoted identifier names or comments are not
2116 ** independent tokens (they are part of the token in which they are
2117 ** embedded) and thus do not count as a statement terminator.  ^Whitespace
2118 ** and comments that follow the final semicolon are ignored.
2119 **
2120 ** ^These routines return 0 if the statement is incomplete.  ^If a
2121 ** memory allocation fails, then SQLITE_NOMEM is returned.
2122 **
2123 ** ^These routines do not parse the SQL statements thus
2124 ** will not detect syntactically incorrect SQL.
2125 **
2126 ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2127 ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2128 ** automatically by sqlite3_complete16().  If that initialization fails,
2129 ** then the return value from sqlite3_complete16() will be non-zero
2130 ** regardless of whether or not the input SQL is complete.)^
2131 **
2132 ** The input to [sqlite3_complete()] must be a zero-terminated
2133 ** UTF-8 string.
2134 **
2135 ** The input to [sqlite3_complete16()] must be a zero-terminated
2136 ** UTF-16 string in native byte order.
2137 */
2138 SQLITE_API int sqlite3_complete(const char *sql);
2139 SQLITE_API int sqlite3_complete16(const void *sql);
2140 
2141 /*
2142 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2143 **
2144 ** ^This routine sets a callback function that might be invoked whenever
2145 ** an attempt is made to open a database table that another thread
2146 ** or process has locked.
2147 **
2148 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2149 ** is returned immediately upon encountering the lock.  ^If the busy callback
2150 ** is not NULL, then the callback might be invoked with two arguments.
2151 **
2152 ** ^The first argument to the busy handler is a copy of the void* pointer which
2153 ** is the third argument to sqlite3_busy_handler().  ^The second argument to
2154 ** the busy handler callback is the number of times that the busy handler has
2155 ** been invoked for this locking event.  ^If the
2156 ** busy callback returns 0, then no additional attempts are made to
2157 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
2158 ** ^If the callback returns non-zero, then another attempt
2159 ** is made to open the database for reading and the cycle repeats.
2160 **
2161 ** The presence of a busy handler does not guarantee that it will be invoked
2162 ** when there is lock contention. ^If SQLite determines that invoking the busy
2163 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2164 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
2165 ** Consider a scenario where one process is holding a read lock that
2166 ** it is trying to promote to a reserved lock and
2167 ** a second process is holding a reserved lock that it is trying
2168 ** to promote to an exclusive lock.  The first process cannot proceed
2169 ** because it is blocked by the second and the second process cannot
2170 ** proceed because it is blocked by the first.  If both processes
2171 ** invoke the busy handlers, neither will make any progress.  Therefore,
2172 ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
2173 ** will induce the first process to release its read lock and allow
2174 ** the second process to proceed.
2175 **
2176 ** ^The default busy callback is NULL.
2177 **
2178 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2179 ** when SQLite is in the middle of a large transaction where all the
2180 ** changes will not fit into the in-memory cache.  SQLite will
2181 ** already hold a RESERVED lock on the database file, but it needs
2182 ** to promote this lock to EXCLUSIVE so that it can spill cache
2183 ** pages into the database file without harm to concurrent
2184 ** readers.  ^If it is unable to promote the lock, then the in-memory
2185 ** cache will be left in an inconsistent state and so the error
2186 ** code is promoted from the relatively benign [SQLITE_BUSY] to
2187 ** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
2188 ** forces an automatic rollback of the changes.  See the
2189 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2190 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2191 ** this is important.
2192 **
2193 ** ^(There can only be a single busy handler defined for each
2194 ** [database connection].  Setting a new busy handler clears any
2195 ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
2196 ** will also set or clear the busy handler.
2197 **
2198 ** The busy callback should not take any actions which modify the
2199 ** database connection that invoked the busy handler.  Any such actions
2200 ** result in undefined behavior.
2201 **
2202 ** A busy handler must not close the database connection
2203 ** or [prepared statement] that invoked the busy handler.
2204 */
2205 SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
2206 
2207 /*
2208 ** CAPI3REF: Set A Busy Timeout
2209 **
2210 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2211 ** for a specified amount of time when a table is locked.  ^The handler
2212 ** will sleep multiple times until at least "ms" milliseconds of sleeping
2213 ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
2214 ** the handler returns 0 which causes [sqlite3_step()] to return
2215 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2216 **
2217 ** ^Calling this routine with an argument less than or equal to zero
2218 ** turns off all busy handlers.
2219 **
2220 ** ^(There can only be a single busy handler for a particular
2221 ** [database connection] any any given moment.  If another busy handler
2222 ** was defined  (using [sqlite3_busy_handler()]) prior to calling
2223 ** this routine, that other busy handler is cleared.)^
2224 */
2225 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2226 
2227 /*
2228 ** CAPI3REF: Convenience Routines For Running Queries
2229 **
2230 ** This is a legacy interface that is preserved for backwards compatibility.
2231 ** Use of this interface is not recommended.
2232 **
2233 ** Definition: A <b>result table</b> is memory data structure created by the
2234 ** [sqlite3_get_table()] interface.  A result table records the
2235 ** complete query results from one or more queries.
2236 **
2237 ** The table conceptually has a number of rows and columns.  But
2238 ** these numbers are not part of the result table itself.  These
2239 ** numbers are obtained separately.  Let N be the number of rows
2240 ** and M be the number of columns.
2241 **
2242 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
2243 ** There are (N+1)*M elements in the array.  The first M pointers point
2244 ** to zero-terminated strings that  contain the names of the columns.
2245 ** The remaining entries all point to query results.  NULL values result
2246 ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
2247 ** string representation as returned by [sqlite3_column_text()].
2248 **
2249 ** A result table might consist of one or more memory allocations.
2250 ** It is not safe to pass a result table directly to [sqlite3_free()].
2251 ** A result table should be deallocated using [sqlite3_free_table()].
2252 **
2253 ** ^(As an example of the result table format, suppose a query result
2254 ** is as follows:
2255 **
2256 ** <blockquote><pre>
2257 **        Name        | Age
2258 **        -----------------------
2259 **        Alice       | 43
2260 **        Bob         | 28
2261 **        Cindy       | 21
2262 ** </pre></blockquote>
2263 **
2264 ** There are two column (M==2) and three rows (N==3).  Thus the
2265 ** result table has 8 entries.  Suppose the result table is stored
2266 ** in an array names azResult.  Then azResult holds this content:
2267 **
2268 ** <blockquote><pre>
2269 **        azResult&#91;0] = "Name";
2270 **        azResult&#91;1] = "Age";
2271 **        azResult&#91;2] = "Alice";
2272 **        azResult&#91;3] = "43";
2273 **        azResult&#91;4] = "Bob";
2274 **        azResult&#91;5] = "28";
2275 **        azResult&#91;6] = "Cindy";
2276 **        azResult&#91;7] = "21";
2277 ** </pre></blockquote>)^
2278 **
2279 ** ^The sqlite3_get_table() function evaluates one or more
2280 ** semicolon-separated SQL statements in the zero-terminated UTF-8
2281 ** string of its 2nd parameter and returns a result table to the
2282 ** pointer given in its 3rd parameter.
2283 **
2284 ** After the application has finished with the result from sqlite3_get_table(),
2285 ** it must pass the result table pointer to sqlite3_free_table() in order to
2286 ** release the memory that was malloced.  Because of the way the
2287 ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
2288 ** function must not try to call [sqlite3_free()] directly.  Only
2289 ** [sqlite3_free_table()] is able to release the memory properly and safely.
2290 **
2291 ** The sqlite3_get_table() interface is implemented as a wrapper around
2292 ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
2293 ** to any internal data structures of SQLite.  It uses only the public
2294 ** interface defined here.  As a consequence, errors that occur in the
2295 ** wrapper layer outside of the internal [sqlite3_exec()] call are not
2296 ** reflected in subsequent calls to [sqlite3_errcode()] or
2297 ** [sqlite3_errmsg()].
2298 */
2299 SQLITE_API int sqlite3_get_table(
2300   sqlite3 *db,          /* An open database */
2301   const char *zSql,     /* SQL to be evaluated */
2302   char ***pazResult,    /* Results of the query */
2303   int *pnRow,           /* Number of result rows written here */
2304   int *pnColumn,        /* Number of result columns written here */
2305   char **pzErrmsg       /* Error msg written here */
2306 );
2307 SQLITE_API void sqlite3_free_table(char **result);
2308 
2309 /*
2310 ** CAPI3REF: Formatted String Printing Functions
2311 **
2312 ** These routines are work-alikes of the "printf()" family of functions
2313 ** from the standard C library.
2314 **
2315 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2316 ** results into memory obtained from [sqlite3_malloc()].
2317 ** The strings returned by these two routines should be
2318 ** released by [sqlite3_free()].  ^Both routines return a
2319 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2320 ** memory to hold the resulting string.
2321 **
2322 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2323 ** the standard C library.  The result is written into the
2324 ** buffer supplied as the second parameter whose size is given by
2325 ** the first parameter. Note that the order of the
2326 ** first two parameters is reversed from snprintf().)^  This is an
2327 ** historical accident that cannot be fixed without breaking
2328 ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
2329 ** returns a pointer to its buffer instead of the number of
2330 ** characters actually written into the buffer.)^  We admit that
2331 ** the number of characters written would be a more useful return
2332 ** value but we cannot change the implementation of sqlite3_snprintf()
2333 ** now without breaking compatibility.
2334 **
2335 ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
2336 ** guarantees that the buffer is always zero-terminated.  ^The first
2337 ** parameter "n" is the total size of the buffer, including space for
2338 ** the zero terminator.  So the longest string that can be completely
2339 ** written will be n-1 characters.
2340 **
2341 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2342 **
2343 ** These routines all implement some additional formatting
2344 ** options that are useful for constructing SQL statements.
2345 ** All of the usual printf() formatting options apply.  In addition, there
2346 ** is are "%q", "%Q", and "%z" options.
2347 **
2348 ** ^(The %q option works like %s in that it substitutes a nul-terminated
2349 ** string from the argument list.  But %q also doubles every '\'' character.
2350 ** %q is designed for use inside a string literal.)^  By doubling each '\''
2351 ** character it escapes that character and allows it to be inserted into
2352 ** the string.
2353 **
2354 ** For example, assume the string variable zText contains text as follows:
2355 **
2356 ** <blockquote><pre>
2357 **  char *zText = "It's a happy day!";
2358 ** </pre></blockquote>
2359 **
2360 ** One can use this text in an SQL statement as follows:
2361 **
2362 ** <blockquote><pre>
2363 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2364 **  sqlite3_exec(db, zSQL, 0, 0, 0);
2365 **  sqlite3_free(zSQL);
2366 ** </pre></blockquote>
2367 **
2368 ** Because the %q format string is used, the '\'' character in zText
2369 ** is escaped and the SQL generated is as follows:
2370 **
2371 ** <blockquote><pre>
2372 **  INSERT INTO table1 VALUES('It''s a happy day!')
2373 ** </pre></blockquote>
2374 **
2375 ** This is correct.  Had we used %s instead of %q, the generated SQL
2376 ** would have looked like this:
2377 **
2378 ** <blockquote><pre>
2379 **  INSERT INTO table1 VALUES('It's a happy day!');
2380 ** </pre></blockquote>
2381 **
2382 ** This second example is an SQL syntax error.  As a general rule you should
2383 ** always use %q instead of %s when inserting text into a string literal.
2384 **
2385 ** ^(The %Q option works like %q except it also adds single quotes around
2386 ** the outside of the total string.  Additionally, if the parameter in the
2387 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2388 ** single quotes).)^  So, for example, one could say:
2389 **
2390 ** <blockquote><pre>
2391 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2392 **  sqlite3_exec(db, zSQL, 0, 0, 0);
2393 **  sqlite3_free(zSQL);
2394 ** </pre></blockquote>
2395 **
2396 ** The code above will render a correct SQL statement in the zSQL
2397 ** variable even if the zText variable is a NULL pointer.
2398 **
2399 ** ^(The "%z" formatting option works like "%s" but with the
2400 ** addition that after the string has been read and copied into
2401 ** the result, [sqlite3_free()] is called on the input string.)^
2402 */
2403 SQLITE_API char *sqlite3_mprintf(const char*,...);
2404 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2405 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2406 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
2407 
2408 /*
2409 ** CAPI3REF: Memory Allocation Subsystem
2410 **
2411 ** The SQLite core uses these three routines for all of its own
2412 ** internal memory allocation needs. "Core" in the previous sentence
2413 ** does not include operating-system specific VFS implementation.  The
2414 ** Windows VFS uses native malloc() and free() for some operations.
2415 **
2416 ** ^The sqlite3_malloc() routine returns a pointer to a block
2417 ** of memory at least N bytes in length, where N is the parameter.
2418 ** ^If sqlite3_malloc() is unable to obtain sufficient free
2419 ** memory, it returns a NULL pointer.  ^If the parameter N to
2420 ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
2421 ** a NULL pointer.
2422 **
2423 ** ^Calling sqlite3_free() with a pointer previously returned
2424 ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
2425 ** that it might be reused.  ^The sqlite3_free() routine is
2426 ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
2427 ** to sqlite3_free() is harmless.  After being freed, memory
2428 ** should neither be read nor written.  Even reading previously freed
2429 ** memory might result in a segmentation fault or other severe error.
2430 ** Memory corruption, a segmentation fault, or other severe error
2431 ** might result if sqlite3_free() is called with a non-NULL pointer that
2432 ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
2433 **
2434 ** ^(The sqlite3_realloc() interface attempts to resize a
2435 ** prior memory allocation to be at least N bytes, where N is the
2436 ** second parameter.  The memory allocation to be resized is the first
2437 ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
2438 ** is a NULL pointer then its behavior is identical to calling
2439 ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
2440 ** ^If the second parameter to sqlite3_realloc() is zero or
2441 ** negative then the behavior is exactly the same as calling
2442 ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
2443 ** ^sqlite3_realloc() returns a pointer to a memory allocation
2444 ** of at least N bytes in size or NULL if sufficient memory is unavailable.
2445 ** ^If M is the size of the prior allocation, then min(N,M) bytes
2446 ** of the prior allocation are copied into the beginning of buffer returned
2447 ** by sqlite3_realloc() and the prior allocation is freed.
2448 ** ^If sqlite3_realloc() returns NULL, then the prior allocation
2449 ** is not freed.
2450 **
2451 ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
2452 ** is always aligned to at least an 8 byte boundary, or to a
2453 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
2454 ** option is used.
2455 **
2456 ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
2457 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
2458 ** implementation of these routines to be omitted.  That capability
2459 ** is no longer provided.  Only built-in memory allocators can be used.
2460 **
2461 ** Prior to SQLite version 3.7.10, the Windows OS interface layer called
2462 ** the system malloc() and free() directly when converting
2463 ** filenames between the UTF-8 encoding used by SQLite
2464 ** and whatever filename encoding is used by the particular Windows
2465 ** installation.  Memory allocation errors were detected, but
2466 ** they were reported back as [SQLITE_CANTOPEN] or
2467 ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
2468 **
2469 ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
2470 ** must be either NULL or else pointers obtained from a prior
2471 ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
2472 ** not yet been released.
2473 **
2474 ** The application must not read or write any part of
2475 ** a block of memory after it has been released using
2476 ** [sqlite3_free()] or [sqlite3_realloc()].
2477 */
2478 SQLITE_API void *sqlite3_malloc(int);
2479 SQLITE_API void *sqlite3_realloc(void*, int);
2480 SQLITE_API void sqlite3_free(void*);
2481 
2482 /*
2483 ** CAPI3REF: Memory Allocator Statistics
2484 **
2485 ** SQLite provides these two interfaces for reporting on the status
2486 ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
2487 ** routines, which form the built-in memory allocation subsystem.
2488 **
2489 ** ^The [sqlite3_memory_used()] routine returns the number of bytes
2490 ** of memory currently outstanding (malloced but not freed).
2491 ** ^The [sqlite3_memory_highwater()] routine returns the maximum
2492 ** value of [sqlite3_memory_used()] since the high-water mark
2493 ** was last reset.  ^The values returned by [sqlite3_memory_used()] and
2494 ** [sqlite3_memory_highwater()] include any overhead
2495 ** added by SQLite in its implementation of [sqlite3_malloc()],
2496 ** but not overhead added by the any underlying system library
2497 ** routines that [sqlite3_malloc()] may call.
2498 **
2499 ** ^The memory high-water mark is reset to the current value of
2500 ** [sqlite3_memory_used()] if and only if the parameter to
2501 ** [sqlite3_memory_highwater()] is true.  ^The value returned
2502 ** by [sqlite3_memory_highwater(1)] is the high-water mark
2503 ** prior to the reset.
2504 */
2505 SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
2506 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
2507 
2508 /*
2509 ** CAPI3REF: Pseudo-Random Number Generator
2510 **
2511 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
2512 ** select random [ROWID | ROWIDs] when inserting new records into a table that
2513 ** already uses the largest possible [ROWID].  The PRNG is also used for
2514 ** the build-in random() and randomblob() SQL functions.  This interface allows
2515 ** applications to access the same PRNG for other purposes.
2516 **
2517 ** ^A call to this routine stores N bytes of randomness into buffer P.
2518 ** ^If N is less than one, then P can be a NULL pointer.
2519 **
2520 ** ^If this routine has not been previously called or if the previous
2521 ** call had N less than one, then the PRNG is seeded using randomness
2522 ** obtained from the xRandomness method of the default [sqlite3_vfs] object.
2523 ** ^If the previous call to this routine had an N of 1 or more then
2524 ** the pseudo-randomness is generated
2525 ** internally and without recourse to the [sqlite3_vfs] xRandomness
2526 ** method.
2527 */
2528 SQLITE_API void sqlite3_randomness(int N, void *P);
2529 
2530 /*
2531 ** CAPI3REF: Compile-Time Authorization Callbacks
2532 **
2533 ** ^This routine registers an authorizer callback with a particular
2534 ** [database connection], supplied in the first argument.
2535 ** ^The authorizer callback is invoked as SQL statements are being compiled
2536 ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
2537 ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
2538 ** points during the compilation process, as logic is being created
2539 ** to perform various actions, the authorizer callback is invoked to
2540 ** see if those actions are allowed.  ^The authorizer callback should
2541 ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
2542 ** specific action but allow the SQL statement to continue to be
2543 ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
2544 ** rejected with an error.  ^If the authorizer callback returns
2545 ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
2546 ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
2547 ** the authorizer will fail with an error message.
2548 **
2549 ** When the callback returns [SQLITE_OK], that means the operation
2550 ** requested is ok.  ^When the callback returns [SQLITE_DENY], the
2551 ** [sqlite3_prepare_v2()] or equivalent call that triggered the
2552 ** authorizer will fail with an error message explaining that
2553 ** access is denied.
2554 **
2555 ** ^The first parameter to the authorizer callback is a copy of the third
2556 ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
2557 ** to the callback is an integer [SQLITE_COPY | action code] that specifies
2558 ** the particular action to be authorized. ^The third through sixth parameters
2559 ** to the callback are zero-terminated strings that contain additional
2560 ** details about the action to be authorized.
2561 **
2562 ** ^If the action code is [SQLITE_READ]
2563 ** and the callback returns [SQLITE_IGNORE] then the
2564 ** [prepared statement] statement is constructed to substitute
2565 ** a NULL value in place of the table column that would have
2566 ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
2567 ** return can be used to deny an untrusted user access to individual
2568 ** columns of a table.
2569 ** ^If the action code is [SQLITE_DELETE] and the callback returns
2570 ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
2571 ** [truncate optimization] is disabled and all rows are deleted individually.
2572 **
2573 ** An authorizer is used when [sqlite3_prepare | preparing]
2574 ** SQL statements from an untrusted source, to ensure that the SQL statements
2575 ** do not try to access data they are not allowed to see, or that they do not
2576 ** try to execute malicious statements that damage the database.  For
2577 ** example, an application may allow a user to enter arbitrary
2578 ** SQL queries for evaluation by a database.  But the application does
2579 ** not want the user to be able to make arbitrary changes to the
2580 ** database.  An authorizer could then be put in place while the
2581 ** user-entered SQL is being [sqlite3_prepare | prepared] that
2582 ** disallows everything except [SELECT] statements.
2583 **
2584 ** Applications that need to process SQL from untrusted sources
2585 ** might also consider lowering resource limits using [sqlite3_limit()]
2586 ** and limiting database size using the [max_page_count] [PRAGMA]
2587 ** in addition to using an authorizer.
2588 **
2589 ** ^(Only a single authorizer can be in place on a database connection
2590 ** at a time.  Each call to sqlite3_set_authorizer overrides the
2591 ** previous call.)^  ^Disable the authorizer by installing a NULL callback.
2592 ** The authorizer is disabled by default.
2593 **
2594 ** The authorizer callback must not do anything that will modify
2595 ** the database connection that invoked the authorizer callback.
2596 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2597 ** database connections for the meaning of "modify" in this paragraph.
2598 **
2599 ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
2600 ** statement might be re-prepared during [sqlite3_step()] due to a
2601 ** schema change.  Hence, the application should ensure that the
2602 ** correct authorizer callback remains in place during the [sqlite3_step()].
2603 **
2604 ** ^Note that the authorizer callback is invoked only during
2605 ** [sqlite3_prepare()] or its variants.  Authorization is not
2606 ** performed during statement evaluation in [sqlite3_step()], unless
2607 ** as stated in the previous paragraph, sqlite3_step() invokes
2608 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
2609 */
2610 SQLITE_API int sqlite3_set_authorizer(
2611   sqlite3*,
2612   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
2613   void *pUserData
2614 );
2615 
2616 /*
2617 ** CAPI3REF: Authorizer Return Codes
2618 **
2619 ** The [sqlite3_set_authorizer | authorizer callback function] must
2620 ** return either [SQLITE_OK] or one of these two constants in order
2621 ** to signal SQLite whether or not the action is permitted.  See the
2622 ** [sqlite3_set_authorizer | authorizer documentation] for additional
2623 ** information.
2624 **
2625 ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2626 ** from the [sqlite3_vtab_on_conflict()] interface.
2627 */
2628 #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
2629 #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
2630 
2631 /*
2632 ** CAPI3REF: Authorizer Action Codes
2633 **
2634 ** The [sqlite3_set_authorizer()] interface registers a callback function
2635 ** that is invoked to authorize certain SQL statement actions.  The
2636 ** second parameter to the callback is an integer code that specifies
2637 ** what action is being authorized.  These are the integer action codes that
2638 ** the authorizer callback may be passed.
2639 **
2640 ** These action code values signify what kind of operation is to be
2641 ** authorized.  The 3rd and 4th parameters to the authorization
2642 ** callback function will be parameters or NULL depending on which of these
2643 ** codes is used as the second parameter.  ^(The 5th parameter to the
2644 ** authorizer callback is the name of the database ("main", "temp",
2645 ** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
2646 ** is the name of the inner-most trigger or view that is responsible for
2647 ** the access attempt or NULL if this access attempt is directly from
2648 ** top-level SQL code.
2649 */
2650 /******************************************* 3rd ************ 4th ***********/
2651 #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
2652 #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
2653 #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
2654 #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
2655 #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
2656 #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
2657 #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
2658 #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
2659 #define SQLITE_DELETE                9   /* Table Name      NULL            */
2660 #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
2661 #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
2662 #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
2663 #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
2664 #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
2665 #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
2666 #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
2667 #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
2668 #define SQLITE_INSERT               18   /* Table Name      NULL            */
2669 #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
2670 #define SQLITE_READ                 20   /* Table Name      Column Name     */
2671 #define SQLITE_SELECT               21   /* NULL            NULL            */
2672 #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
2673 #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
2674 #define SQLITE_ATTACH               24   /* Filename        NULL            */
2675 #define SQLITE_DETACH               25   /* Database Name   NULL            */
2676 #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
2677 #define SQLITE_REINDEX              27   /* Index Name      NULL            */
2678 #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
2679 #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
2680 #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
2681 #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
2682 #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
2683 #define SQLITE_COPY                  0   /* No longer used */
2684 #define SQLITE_RECURSIVE            33   /* NULL            NULL            */
2685 
2686 /*
2687 ** CAPI3REF: Tracing And Profiling Functions
2688 **
2689 ** These routines register callback functions that can be used for
2690 ** tracing and profiling the execution of SQL statements.
2691 **
2692 ** ^The callback function registered by sqlite3_trace() is invoked at
2693 ** various times when an SQL statement is being run by [sqlite3_step()].
2694 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
2695 ** SQL statement text as the statement first begins executing.
2696 ** ^(Additional sqlite3_trace() callbacks might occur
2697 ** as each triggered subprogram is entered.  The callbacks for triggers
2698 ** contain a UTF-8 SQL comment that identifies the trigger.)^
2699 **
2700 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
2701 ** the length of [bound parameter] expansion in the output of sqlite3_trace().
2702 **
2703 ** ^The callback function registered by sqlite3_profile() is invoked
2704 ** as each SQL statement finishes.  ^The profile callback contains
2705 ** the original statement text and an estimate of wall-clock time
2706 ** of how long that statement took to run.  ^The profile callback
2707 ** time is in units of nanoseconds, however the current implementation
2708 ** is only capable of millisecond resolution so the six least significant
2709 ** digits in the time are meaningless.  Future versions of SQLite
2710 ** might provide greater resolution on the profiler callback.  The
2711 ** sqlite3_profile() function is considered experimental and is
2712 ** subject to change in future versions of SQLite.
2713 */
2714 SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
2715 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
2716    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
2717 
2718 /*
2719 ** CAPI3REF: Query Progress Callbacks
2720 **
2721 ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
2722 ** function X to be invoked periodically during long running calls to
2723 ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
2724 ** database connection D.  An example use for this
2725 ** interface is to keep a GUI updated during a large query.
2726 **
2727 ** ^The parameter P is passed through as the only parameter to the
2728 ** callback function X.  ^The parameter N is the approximate number of
2729 ** [virtual machine instructions] that are evaluated between successive
2730 ** invocations of the callback X.  ^If N is less than one then the progress
2731 ** handler is disabled.
2732 **
2733 ** ^Only a single progress handler may be defined at one time per
2734 ** [database connection]; setting a new progress handler cancels the
2735 ** old one.  ^Setting parameter X to NULL disables the progress handler.
2736 ** ^The progress handler is also disabled by setting N to a value less
2737 ** than 1.
2738 **
2739 ** ^If the progress callback returns non-zero, the operation is
2740 ** interrupted.  This feature can be used to implement a
2741 ** "Cancel" button on a GUI progress dialog box.
2742 **
2743 ** The progress handler callback must not do anything that will modify
2744 ** the database connection that invoked the progress handler.
2745 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2746 ** database connections for the meaning of "modify" in this paragraph.
2747 **
2748 */
2749 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
2750 
2751 /*
2752 ** CAPI3REF: Opening A New Database Connection
2753 **
2754 ** ^These routines open an SQLite database file as specified by the
2755 ** filename argument. ^The filename argument is interpreted as UTF-8 for
2756 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
2757 ** order for sqlite3_open16(). ^(A [database connection] handle is usually
2758 ** returned in *ppDb, even if an error occurs.  The only exception is that
2759 ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
2760 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
2761 ** object.)^ ^(If the database is opened (and/or created) successfully, then
2762 ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
2763 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2764 ** an English language description of the error following a failure of any
2765 ** of the sqlite3_open() routines.
2766 **
2767 ** ^The default encoding for the database will be UTF-8 if
2768 ** sqlite3_open() or sqlite3_open_v2() is called and
2769 ** UTF-16 in the native byte order if sqlite3_open16() is used.
2770 **
2771 ** Whether or not an error occurs when it is opened, resources
2772 ** associated with the [database connection] handle should be released by
2773 ** passing it to [sqlite3_close()] when it is no longer required.
2774 **
2775 ** The sqlite3_open_v2() interface works like sqlite3_open()
2776 ** except that it accepts two additional parameters for additional control
2777 ** over the new database connection.  ^(The flags parameter to
2778 ** sqlite3_open_v2() can take one of
2779 ** the following three values, optionally combined with the
2780 ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
2781 ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
2782 **
2783 ** <dl>
2784 ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
2785 ** <dd>The database is opened in read-only mode.  If the database does not
2786 ** already exist, an error is returned.</dd>)^
2787 **
2788 ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
2789 ** <dd>The database is opened for reading and writing if possible, or reading
2790 ** only if the file is write protected by the operating system.  In either
2791 ** case the database must already exist, otherwise an error is returned.</dd>)^
2792 **
2793 ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
2794 ** <dd>The database is opened for reading and writing, and is created if
2795 ** it does not already exist. This is the behavior that is always used for
2796 ** sqlite3_open() and sqlite3_open16().</dd>)^
2797 ** </dl>
2798 **
2799 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
2800 ** combinations shown above optionally combined with other
2801 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
2802 ** then the behavior is undefined.
2803 **
2804 ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
2805 ** opens in the multi-thread [threading mode] as long as the single-thread
2806 ** mode has not been set at compile-time or start-time.  ^If the
2807 ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
2808 ** in the serialized [threading mode] unless single-thread was
2809 ** previously selected at compile-time or start-time.
2810 ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
2811 ** eligible to use [shared cache mode], regardless of whether or not shared
2812 ** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
2813 ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
2814 ** participate in [shared cache mode] even if it is enabled.
2815 **
2816 ** ^The fourth parameter to sqlite3_open_v2() is the name of the
2817 ** [sqlite3_vfs] object that defines the operating system interface that
2818 ** the new database connection should use.  ^If the fourth parameter is
2819 ** a NULL pointer then the default [sqlite3_vfs] object is used.
2820 **
2821 ** ^If the filename is ":memory:", then a private, temporary in-memory database
2822 ** is created for the connection.  ^This in-memory database will vanish when
2823 ** the database connection is closed.  Future versions of SQLite might
2824 ** make use of additional special filenames that begin with the ":" character.
2825 ** It is recommended that when a database filename actually does begin with
2826 ** a ":" character you should prefix the filename with a pathname such as
2827 ** "./" to avoid ambiguity.
2828 **
2829 ** ^If the filename is an empty string, then a private, temporary
2830 ** on-disk database will be created.  ^This private database will be
2831 ** automatically deleted as soon as the database connection is closed.
2832 **
2833 ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
2834 **
2835 ** ^If [URI filename] interpretation is enabled, and the filename argument
2836 ** begins with "file:", then the filename is interpreted as a URI. ^URI
2837 ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
2838 ** set in the fourth argument to sqlite3_open_v2(), or if it has
2839 ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
2840 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
2841 ** As of SQLite version 3.7.7, URI filename interpretation is turned off
2842 ** by default, but future releases of SQLite might enable URI filename
2843 ** interpretation by default.  See "[URI filenames]" for additional
2844 ** information.
2845 **
2846 ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
2847 ** authority, then it must be either an empty string or the string
2848 ** "localhost". ^If the authority is not an empty string or "localhost", an
2849 ** error is returned to the caller. ^The fragment component of a URI, if
2850 ** present, is ignored.
2851 **
2852 ** ^SQLite uses the path component of the URI as the name of the disk file
2853 ** which contains the database. ^If the path begins with a '/' character,
2854 ** then it is interpreted as an absolute path. ^If the path does not begin
2855 ** with a '/' (meaning that the authority section is omitted from the URI)
2856 ** then the path is interpreted as a relative path.
2857 ** ^On windows, the first component of an absolute path
2858 ** is a drive specification (e.g. "C:").
2859 **
2860 ** [[core URI query parameters]]
2861 ** The query component of a URI may contain parameters that are interpreted
2862 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
2863 ** SQLite interprets the following three query parameters:
2864 **
2865 ** <ul>
2866 **   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
2867 **     a VFS object that provides the operating system interface that should
2868 **     be used to access the database file on disk. ^If this option is set to
2869 **     an empty string the default VFS object is used. ^Specifying an unknown
2870 **     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
2871 **     present, then the VFS specified by the option takes precedence over
2872 **     the value passed as the fourth parameter to sqlite3_open_v2().
2873 **
2874 **   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
2875 **     "rwc", or "memory". Attempting to set it to any other value is
2876 **     an error)^.
2877 **     ^If "ro" is specified, then the database is opened for read-only
2878 **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
2879 **     third argument to sqlite3_open_v2(). ^If the mode option is set to
2880 **     "rw", then the database is opened for read-write (but not create)
2881 **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
2882 **     been set. ^Value "rwc" is equivalent to setting both
2883 **     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
2884 **     set to "memory" then a pure [in-memory database] that never reads
2885 **     or writes from disk is used. ^It is an error to specify a value for
2886 **     the mode parameter that is less restrictive than that specified by
2887 **     the flags passed in the third parameter to sqlite3_open_v2().
2888 **
2889 **   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
2890 **     "private". ^Setting it to "shared" is equivalent to setting the
2891 **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
2892 **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is
2893 **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2894 **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2895 **     a URI filename, its value overrides any behavior requested by setting
2896 **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2897 ** </ul>
2898 **
2899 ** ^Specifying an unknown parameter in the query component of a URI is not an
2900 ** error.  Future versions of SQLite might understand additional query
2901 ** parameters.  See "[query parameters with special meaning to SQLite]" for
2902 ** additional information.
2903 **
2904 ** [[URI filename examples]] <h3>URI filename examples</h3>
2905 **
2906 ** <table border="1" align=center cellpadding=5>
2907 ** <tr><th> URI filenames <th> Results
2908 ** <tr><td> file:data.db <td>
2909 **          Open the file "data.db" in the current directory.
2910 ** <tr><td> file:/home/fred/data.db<br>
2911 **          file:///home/fred/data.db <br>
2912 **          file://localhost/home/fred/data.db <br> <td>
2913 **          Open the database file "/home/fred/data.db".
2914 ** <tr><td> file://darkstar/home/fred/data.db <td>
2915 **          An error. "darkstar" is not a recognized authority.
2916 ** <tr><td style="white-space:nowrap">
2917 **          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
2918 **     <td> Windows only: Open the file "data.db" on fred's desktop on drive
2919 **          C:. Note that the %20 escaping in this example is not strictly
2920 **          necessary - space characters can be used literally
2921 **          in URI filenames.
2922 ** <tr><td> file:data.db?mode=ro&cache=private <td>
2923 **          Open file "data.db" in the current directory for read-only access.
2924 **          Regardless of whether or not shared-cache mode is enabled by
2925 **          default, use a private cache.
2926 ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
2927 **          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
2928 ** <tr><td> file:data.db?mode=readonly <td>
2929 **          An error. "readonly" is not a valid option for the "mode" parameter.
2930 ** </table>
2931 **
2932 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
2933 ** query components of a URI. A hexadecimal escape sequence consists of a
2934 ** percent sign - "%" - followed by exactly two hexadecimal digits
2935 ** specifying an octet value. ^Before the path or query components of a
2936 ** URI filename are interpreted, they are encoded using UTF-8 and all
2937 ** hexadecimal escape sequences replaced by a single byte containing the
2938 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
2939 ** the results are undefined.
2940 **
2941 ** <b>Note to Windows users:</b>  The encoding used for the filename argument
2942 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
2943 ** codepage is currently defined.  Filenames containing international
2944 ** characters must be converted to UTF-8 prior to passing them into
2945 ** sqlite3_open() or sqlite3_open_v2().
2946 **
2947 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
2948 ** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
2949 ** features that require the use of temporary files may fail.
2950 **
2951 ** See also: [sqlite3_temp_directory]
2952 */
2953 SQLITE_API int sqlite3_open(
2954   const char *filename,   /* Database filename (UTF-8) */
2955   sqlite3 **ppDb          /* OUT: SQLite db handle */
2956 );
2957 SQLITE_API int sqlite3_open16(
2958   const void *filename,   /* Database filename (UTF-16) */
2959   sqlite3 **ppDb          /* OUT: SQLite db handle */
2960 );
2961 SQLITE_API int sqlite3_open_v2(
2962   const char *filename,   /* Database filename (UTF-8) */
2963   sqlite3 **ppDb,         /* OUT: SQLite db handle */
2964   int flags,              /* Flags */
2965   const char *zVfs        /* Name of VFS module to use */
2966 );
2967 
2968 /*
2969 ** CAPI3REF: Obtain Values For URI Parameters
2970 **
2971 ** These are utility routines, useful to VFS implementations, that check
2972 ** to see if a database file was a URI that contained a specific query
2973 ** parameter, and if so obtains the value of that query parameter.
2974 **
2975 ** If F is the database filename pointer passed into the xOpen() method of
2976 ** a VFS implementation when the flags parameter to xOpen() has one or
2977 ** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
2978 ** P is the name of the query parameter, then
2979 ** sqlite3_uri_parameter(F,P) returns the value of the P
2980 ** parameter if it exists or a NULL pointer if P does not appear as a
2981 ** query parameter on F.  If P is a query parameter of F
2982 ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
2983 ** a pointer to an empty string.
2984 **
2985 ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
2986 ** parameter and returns true (1) or false (0) according to the value
2987 ** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
2988 ** value of query parameter P is one of "yes", "true", or "on" in any
2989 ** case or if the value begins with a non-zero number.  The
2990 ** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
2991 ** query parameter P is one of "no", "false", or "off" in any case or
2992 ** if the value begins with a numeric zero.  If P is not a query
2993 ** parameter on F or if the value of P is does not match any of the
2994 ** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
2995 **
2996 ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
2997 ** 64-bit signed integer and returns that integer, or D if P does not
2998 ** exist.  If the value of P is something other than an integer, then
2999 ** zero is returned.
3000 **
3001 ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
3002 ** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
3003 ** is not a database file pathname pointer that SQLite passed into the xOpen
3004 ** VFS method, then the behavior of this routine is undefined and probably
3005 ** undesirable.
3006 */
3007 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3008 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
3009 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
3010 
3011 
3012 /*
3013 ** CAPI3REF: Error Codes And Messages
3014 **
3015 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
3016 ** [extended result code] for the most recent failed sqlite3_* API call
3017 ** associated with a [database connection]. If a prior API call failed
3018 ** but the most recent API call succeeded, the return value from
3019 ** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
3020 ** interface is the same except that it always returns the
3021 ** [extended result code] even when extended result codes are
3022 ** disabled.
3023 **
3024 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3025 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
3026 ** ^(Memory to hold the error message string is managed internally.
3027 ** The application does not need to worry about freeing the result.
3028 ** However, the error string might be overwritten or deallocated by
3029 ** subsequent calls to other SQLite interface functions.)^
3030 **
3031 ** ^The sqlite3_errstr() interface returns the English-language text
3032 ** that describes the [result code], as UTF-8.
3033 ** ^(Memory to hold the error message string is managed internally
3034 ** and must not be freed by the application)^.
3035 **
3036 ** When the serialized [threading mode] is in use, it might be the
3037 ** case that a second error occurs on a separate thread in between
3038 ** the time of the first error and the call to these interfaces.
3039 ** When that happens, the second error will be reported since these
3040 ** interfaces always report the most recent result.  To avoid
3041 ** this, each thread can obtain exclusive use of the [database connection] D
3042 ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
3043 ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
3044 ** all calls to the interfaces listed here are completed.
3045 **
3046 ** If an interface fails with SQLITE_MISUSE, that means the interface
3047 ** was invoked incorrectly by the application.  In that case, the
3048 ** error code and message may or may not be set.
3049 */
3050 SQLITE_API int sqlite3_errcode(sqlite3 *db);
3051 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
3052 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
3053 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
3054 SQLITE_API const char *sqlite3_errstr(int);
3055 
3056 /*
3057 ** CAPI3REF: SQL Statement Object
3058 ** KEYWORDS: {prepared statement} {prepared statements}
3059 **
3060 ** An instance of this object represents a single SQL statement.
3061 ** This object is variously known as a "prepared statement" or a
3062 ** "compiled SQL statement" or simply as a "statement".
3063 **
3064 ** The life of a statement object goes something like this:
3065 **
3066 ** <ol>
3067 ** <li> Create the object using [sqlite3_prepare_v2()] or a related
3068 **      function.
3069 ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
3070 **      interfaces.
3071 ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
3072 ** <li> Reset the statement using [sqlite3_reset()] then go back
3073 **      to step 2.  Do this zero or more times.
3074 ** <li> Destroy the object using [sqlite3_finalize()].
3075 ** </ol>
3076 **
3077 ** Refer to documentation on individual methods above for additional
3078 ** information.
3079 */
3080 typedef struct sqlite3_stmt sqlite3_stmt;
3081 
3082 /*
3083 ** CAPI3REF: Run-time Limits
3084 **
3085 ** ^(This interface allows the size of various constructs to be limited
3086 ** on a connection by connection basis.  The first parameter is the
3087 ** [database connection] whose limit is to be set or queried.  The
3088 ** second parameter is one of the [limit categories] that define a
3089 ** class of constructs to be size limited.  The third parameter is the
3090 ** new limit for that construct.)^
3091 **
3092 ** ^If the new limit is a negative number, the limit is unchanged.
3093 ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
3094 ** [limits | hard upper bound]
3095 ** set at compile-time by a C preprocessor macro called
3096 ** [limits | SQLITE_MAX_<i>NAME</i>].
3097 ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
3098 ** ^Attempts to increase a limit above its hard upper bound are
3099 ** silently truncated to the hard upper bound.
3100 **
3101 ** ^Regardless of whether or not the limit was changed, the
3102 ** [sqlite3_limit()] interface returns the prior value of the limit.
3103 ** ^Hence, to find the current value of a limit without changing it,
3104 ** simply invoke this interface with the third parameter set to -1.
3105 **
3106 ** Run-time limits are intended for use in applications that manage
3107 ** both their own internal database and also databases that are controlled
3108 ** by untrusted external sources.  An example application might be a
3109 ** web browser that has its own databases for storing history and
3110 ** separate databases controlled by JavaScript applications downloaded
3111 ** off the Internet.  The internal databases can be given the
3112 ** large, default limits.  Databases managed by external sources can
3113 ** be given much smaller limits designed to prevent a denial of service
3114 ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
3115 ** interface to further control untrusted SQL.  The size of the database
3116 ** created by an untrusted script can be contained using the
3117 ** [max_page_count] [PRAGMA].
3118 **
3119 ** New run-time limit categories may be added in future releases.
3120 */
3121 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
3122 
3123 /*
3124 ** CAPI3REF: Run-Time Limit Categories
3125 ** KEYWORDS: {limit category} {*limit categories}
3126 **
3127 ** These constants define various performance limits
3128 ** that can be lowered at run-time using [sqlite3_limit()].
3129 ** The synopsis of the meanings of the various limits is shown below.
3130 ** Additional information is available at [limits | Limits in SQLite].
3131 **
3132 ** <dl>
3133 ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
3134 ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
3135 **
3136 ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
3137 ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
3138 **
3139 ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
3140 ** <dd>The maximum number of columns in a table definition or in the
3141 ** result set of a [SELECT] or the maximum number of columns in an index
3142 ** or in an ORDER BY or GROUP BY clause.</dd>)^
3143 **
3144 ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
3145 ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
3146 **
3147 ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
3148 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
3149 **
3150 ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
3151 ** <dd>The maximum number of instructions in a virtual machine program
3152 ** used to implement an SQL statement.  This limit is not currently
3153 ** enforced, though that might be added in some future release of
3154 ** SQLite.</dd>)^
3155 **
3156 ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
3157 ** <dd>The maximum number of arguments on a function.</dd>)^
3158 **
3159 ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
3160 ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
3161 **
3162 ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
3163 ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
3164 ** <dd>The maximum length of the pattern argument to the [LIKE] or
3165 ** [GLOB] operators.</dd>)^
3166 **
3167 ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
3168 ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
3169 ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
3170 **
3171 ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
3172 ** <dd>The maximum depth of recursion for triggers.</dd>)^
3173 ** </dl>
3174 */
3175 #define SQLITE_LIMIT_LENGTH                    0
3176 #define SQLITE_LIMIT_SQL_LENGTH                1
3177 #define SQLITE_LIMIT_COLUMN                    2
3178 #define SQLITE_LIMIT_EXPR_DEPTH                3
3179 #define SQLITE_LIMIT_COMPOUND_SELECT           4
3180 #define SQLITE_LIMIT_VDBE_OP                   5
3181 #define SQLITE_LIMIT_FUNCTION_ARG              6
3182 #define SQLITE_LIMIT_ATTACHED                  7
3183 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
3184 #define SQLITE_LIMIT_VARIABLE_NUMBER           9
3185 #define SQLITE_LIMIT_TRIGGER_DEPTH            10
3186 
3187 /*
3188 ** CAPI3REF: Compiling An SQL Statement
3189 ** KEYWORDS: {SQL statement compiler}
3190 **
3191 ** To execute an SQL query, it must first be compiled into a byte-code
3192 ** program using one of these routines.
3193 **
3194 ** The first argument, "db", is a [database connection] obtained from a
3195 ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
3196 ** [sqlite3_open16()].  The database connection must not have been closed.
3197 **
3198 ** The second argument, "zSql", is the statement to be compiled, encoded
3199 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
3200 ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
3201 ** use UTF-16.
3202 **
3203 ** ^If the nByte argument is less than zero, then zSql is read up to the
3204 ** first zero terminator. ^If nByte is non-negative, then it is the maximum
3205 ** number of  bytes read from zSql.  ^When nByte is non-negative, the
3206 ** zSql string ends at either the first '\000' or '\u0000' character or
3207 ** the nByte-th byte, whichever comes first. If the caller knows
3208 ** that the supplied string is nul-terminated, then there is a small
3209 ** performance advantage to be gained by passing an nByte parameter that
3210 ** is equal to the number of bytes in the input string <i>including</i>
3211 ** the nul-terminator bytes as this saves SQLite from having to
3212 ** make a copy of the input string.
3213 **
3214 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3215 ** past the end of the first SQL statement in zSql.  These routines only
3216 ** compile the first statement in zSql, so *pzTail is left pointing to
3217 ** what remains uncompiled.
3218 **
3219 ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
3220 ** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
3221 ** to NULL.  ^If the input text contains no SQL (if the input is an empty
3222 ** string or a comment) then *ppStmt is set to NULL.
3223 ** The calling procedure is responsible for deleting the compiled
3224 ** SQL statement using [sqlite3_finalize()] after it has finished with it.
3225 ** ppStmt may not be NULL.
3226 **
3227 ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
3228 ** otherwise an [error code] is returned.
3229 **
3230 ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
3231 ** recommended for all new programs. The two older interfaces are retained
3232 ** for backwards compatibility, but their use is discouraged.
3233 ** ^In the "v2" interfaces, the prepared statement
3234 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
3235 ** original SQL text. This causes the [sqlite3_step()] interface to
3236 ** behave differently in three ways:
3237 **
3238 ** <ol>
3239 ** <li>
3240 ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
3241 ** always used to do, [sqlite3_step()] will automatically recompile the SQL
3242 ** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
3243 ** retries will occur before sqlite3_step() gives up and returns an error.
3244 ** </li>
3245 **
3246 ** <li>
3247 ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
3248 ** [error codes] or [extended error codes].  ^The legacy behavior was that
3249 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
3250 ** and the application would have to make a second call to [sqlite3_reset()]
3251 ** in order to find the underlying cause of the problem. With the "v2" prepare
3252 ** interfaces, the underlying reason for the error is returned immediately.
3253 ** </li>
3254 **
3255 ** <li>
3256 ** ^If the specific value bound to [parameter | host parameter] in the
3257 ** WHERE clause might influence the choice of query plan for a statement,
3258 ** then the statement will be automatically recompiled, as if there had been
3259 ** a schema change, on the first  [sqlite3_step()] call following any change
3260 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3261 ** ^The specific value of WHERE-clause [parameter] might influence the
3262 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3263 ** or [GLOB] operator or if the parameter is compared to an indexed column
3264 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3265 ** </li>
3266 ** </ol>
3267 */
3268 SQLITE_API int sqlite3_prepare(
3269   sqlite3 *db,            /* Database handle */
3270   const char *zSql,       /* SQL statement, UTF-8 encoded */
3271   int nByte,              /* Maximum length of zSql in bytes. */
3272   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3273   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3274 );
3275 SQLITE_API int sqlite3_prepare_v2(
3276   sqlite3 *db,            /* Database handle */
3277   const char *zSql,       /* SQL statement, UTF-8 encoded */
3278   int nByte,              /* Maximum length of zSql in bytes. */
3279   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3280   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3281 );
3282 SQLITE_API int sqlite3_prepare16(
3283   sqlite3 *db,            /* Database handle */
3284   const void *zSql,       /* SQL statement, UTF-16 encoded */
3285   int nByte,              /* Maximum length of zSql in bytes. */
3286   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3287   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3288 );
3289 SQLITE_API int sqlite3_prepare16_v2(
3290   sqlite3 *db,            /* Database handle */
3291   const void *zSql,       /* SQL statement, UTF-16 encoded */
3292   int nByte,              /* Maximum length of zSql in bytes. */
3293   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3294   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3295 );
3296 
3297 /*
3298 ** CAPI3REF: Retrieving Statement SQL
3299 **
3300 ** ^This interface can be used to retrieve a saved copy of the original
3301 ** SQL text used to create a [prepared statement] if that statement was
3302 ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
3303 */
3304 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
3305 
3306 /*
3307 ** CAPI3REF: Determine If An SQL Statement Writes The Database
3308 **
3309 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3310 ** and only if the [prepared statement] X makes no direct changes to
3311 ** the content of the database file.
3312 **
3313 ** Note that [application-defined SQL functions] or
3314 ** [virtual tables] might change the database indirectly as a side effect.
3315 ** ^(For example, if an application defines a function "eval()" that
3316 ** calls [sqlite3_exec()], then the following SQL statement would
3317 ** change the database file through side-effects:
3318 **
3319 ** <blockquote><pre>
3320 **    SELECT eval('DELETE FROM t1') FROM t2;
3321 ** </pre></blockquote>
3322 **
3323 ** But because the [SELECT] statement does not change the database file
3324 ** directly, sqlite3_stmt_readonly() would still return true.)^
3325 **
3326 ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
3327 ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
3328 ** since the statements themselves do not actually modify the database but
3329 ** rather they control the timing of when other statements modify the
3330 ** database.  ^The [ATTACH] and [DETACH] statements also cause
3331 ** sqlite3_stmt_readonly() to return true since, while those statements
3332 ** change the configuration of a database connection, they do not make
3333 ** changes to the content of the database files on disk.
3334 */
3335 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3336 
3337 /*
3338 ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
3339 **
3340 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
3341 ** [prepared statement] S has been stepped at least once using
3342 ** [sqlite3_step(S)] but has not run to completion and/or has not
3343 ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
3344 ** interface returns false if S is a NULL pointer.  If S is not a
3345 ** NULL pointer and is not a pointer to a valid [prepared statement]
3346 ** object, then the behavior is undefined and probably undesirable.
3347 **
3348 ** This interface can be used in combination [sqlite3_next_stmt()]
3349 ** to locate all prepared statements associated with a database
3350 ** connection that are in need of being reset.  This can be used,
3351 ** for example, in diagnostic routines to search for prepared
3352 ** statements that are holding a transaction open.
3353 */
3354 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
3355 
3356 /*
3357 ** CAPI3REF: Dynamically Typed Value Object
3358 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3359 **
3360 ** SQLite uses the sqlite3_value object to represent all values
3361 ** that can be stored in a database table. SQLite uses dynamic typing
3362 ** for the values it stores.  ^Values stored in sqlite3_value objects
3363 ** can be integers, floating point values, strings, BLOBs, or NULL.
3364 **
3365 ** An sqlite3_value object may be either "protected" or "unprotected".
3366 ** Some interfaces require a protected sqlite3_value.  Other interfaces
3367 ** will accept either a protected or an unprotected sqlite3_value.
3368 ** Every interface that accepts sqlite3_value arguments specifies
3369 ** whether or not it requires a protected sqlite3_value.
3370 **
3371 ** The terms "protected" and "unprotected" refer to whether or not
3372 ** a mutex is held.  An internal mutex is held for a protected
3373 ** sqlite3_value object but no mutex is held for an unprotected
3374 ** sqlite3_value object.  If SQLite is compiled to be single-threaded
3375 ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
3376 ** or if SQLite is run in one of reduced mutex modes
3377 ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
3378 ** then there is no distinction between protected and unprotected
3379 ** sqlite3_value objects and they can be used interchangeably.  However,
3380 ** for maximum code portability it is recommended that applications
3381 ** still make the distinction between protected and unprotected
3382 ** sqlite3_value objects even when not strictly required.
3383 **
3384 ** ^The sqlite3_value objects that are passed as parameters into the
3385 ** implementation of [application-defined SQL functions] are protected.
3386 ** ^The sqlite3_value object returned by
3387 ** [sqlite3_column_value()] is unprotected.
3388 ** Unprotected sqlite3_value objects may only be used with
3389 ** [sqlite3_result_value()] and [sqlite3_bind_value()].
3390 ** The [sqlite3_value_blob | sqlite3_value_type()] family of
3391 ** interfaces require protected sqlite3_value objects.
3392 */
3393 typedef struct Mem sqlite3_value;
3394 
3395 /*
3396 ** CAPI3REF: SQL Function Context Object
3397 **
3398 ** The context in which an SQL function executes is stored in an
3399 ** sqlite3_context object.  ^A pointer to an sqlite3_context object
3400 ** is always first parameter to [application-defined SQL functions].
3401 ** The application-defined SQL function implementation will pass this
3402 ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
3403 ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
3404 ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
3405 ** and/or [sqlite3_set_auxdata()].
3406 */
3407 typedef struct sqlite3_context sqlite3_context;
3408 
3409 /*
3410 ** CAPI3REF: Binding Values To Prepared Statements
3411 ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
3412 ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
3413 **
3414 ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
3415 ** literals may be replaced by a [parameter] that matches one of following
3416 ** templates:
3417 **
3418 ** <ul>
3419 ** <li>  ?
3420 ** <li>  ?NNN
3421 ** <li>  :VVV
3422 ** <li>  @VVV
3423 ** <li>  $VVV
3424 ** </ul>
3425 **
3426 ** In the templates above, NNN represents an integer literal,
3427 ** and VVV represents an alphanumeric identifier.)^  ^The values of these
3428 ** parameters (also called "host parameter names" or "SQL parameters")
3429 ** can be set using the sqlite3_bind_*() routines defined here.
3430 **
3431 ** ^The first argument to the sqlite3_bind_*() routines is always
3432 ** a pointer to the [sqlite3_stmt] object returned from
3433 ** [sqlite3_prepare_v2()] or its variants.
3434 **
3435 ** ^The second argument is the index of the SQL parameter to be set.
3436 ** ^The leftmost SQL parameter has an index of 1.  ^When the same named
3437 ** SQL parameter is used more than once, second and subsequent
3438 ** occurrences have the same index as the first occurrence.
3439 ** ^The index for named parameters can be looked up using the
3440 ** [sqlite3_bind_parameter_index()] API if desired.  ^The index
3441 ** for "?NNN" parameters is the value of NNN.
3442 ** ^The NNN value must be between 1 and the [sqlite3_limit()]
3443 ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
3444 **
3445 ** ^The third argument is the value to bind to the parameter.
3446 ** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
3447 ** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
3448 ** is ignored and the end result is the same as sqlite3_bind_null().
3449 **
3450 ** ^(In those routines that have a fourth argument, its value is the
3451 ** number of bytes in the parameter.  To be clear: the value is the
3452 ** number of <u>bytes</u> in the value, not the number of characters.)^
3453 ** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
3454 ** is negative, then the length of the string is
3455 ** the number of bytes up to the first zero terminator.
3456 ** If the fourth parameter to sqlite3_bind_blob() is negative, then
3457 ** the behavior is undefined.
3458 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
3459 ** or sqlite3_bind_text16() then that parameter must be the byte offset
3460 ** where the NUL terminator would occur assuming the string were NUL
3461 ** terminated.  If any NUL characters occur at byte offsets less than
3462 ** the value of the fourth parameter then the resulting string value will
3463 ** contain embedded NULs.  The result of expressions involving strings
3464 ** with embedded NULs is undefined.
3465 **
3466 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3467 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3468 ** string after SQLite has finished with it.  ^The destructor is called
3469 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
3470 ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
3471 ** ^If the fifth argument is
3472 ** the special value [SQLITE_STATIC], then SQLite assumes that the
3473 ** information is in static, unmanaged space and does not need to be freed.
3474 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3475 ** SQLite makes its own private copy of the data immediately, before
3476 ** the sqlite3_bind_*() routine returns.
3477 **
3478 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
3479 ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
3480 ** (just an integer to hold its size) while it is being processed.
3481 ** Zeroblobs are intended to serve as placeholders for BLOBs whose
3482 ** content is later written using
3483 ** [sqlite3_blob_open | incremental BLOB I/O] routines.
3484 ** ^A negative value for the zeroblob results in a zero-length BLOB.
3485 **
3486 ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
3487 ** for the [prepared statement] or with a prepared statement for which
3488 ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
3489 ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
3490 ** routine is passed a [prepared statement] that has been finalized, the
3491 ** result is undefined and probably harmful.
3492 **
3493 ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
3494 ** ^Unbound parameters are interpreted as NULL.
3495 **
3496 ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
3497 ** [error code] if anything goes wrong.
3498 ** ^[SQLITE_RANGE] is returned if the parameter
3499 ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
3500 **
3501 ** See also: [sqlite3_bind_parameter_count()],
3502 ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
3503 */
3504 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
3505 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
3506 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
3507 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
3508 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
3509 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
3510 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
3511 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
3512 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
3513 
3514 /*
3515 ** CAPI3REF: Number Of SQL Parameters
3516 **
3517 ** ^This routine can be used to find the number of [SQL parameters]
3518 ** in a [prepared statement].  SQL parameters are tokens of the
3519 ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
3520 ** placeholders for values that are [sqlite3_bind_blob | bound]
3521 ** to the parameters at a later time.
3522 **
3523 ** ^(This routine actually returns the index of the largest (rightmost)
3524 ** parameter. For all forms except ?NNN, this will correspond to the
3525 ** number of unique parameters.  If parameters of the ?NNN form are used,
3526 ** there may be gaps in the list.)^
3527 **
3528 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3529 ** [sqlite3_bind_parameter_name()], and
3530 ** [sqlite3_bind_parameter_index()].
3531 */
3532 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
3533 
3534 /*
3535 ** CAPI3REF: Name Of A Host Parameter
3536 **
3537 ** ^The sqlite3_bind_parameter_name(P,N) interface returns
3538 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
3539 ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
3540 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
3541 ** respectively.
3542 ** In other words, the initial ":" or "$" or "@" or "?"
3543 ** is included as part of the name.)^
3544 ** ^Parameters of the form "?" without a following integer have no name
3545 ** and are referred to as "nameless" or "anonymous parameters".
3546 **
3547 ** ^The first host parameter has an index of 1, not 0.
3548 **
3549 ** ^If the value N is out of range or if the N-th parameter is
3550 ** nameless, then NULL is returned.  ^The returned string is
3551 ** always in UTF-8 encoding even if the named parameter was
3552 ** originally specified as UTF-16 in [sqlite3_prepare16()] or
3553 ** [sqlite3_prepare16_v2()].
3554 **
3555 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3556 ** [sqlite3_bind_parameter_count()], and
3557 ** [sqlite3_bind_parameter_index()].
3558 */
3559 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
3560 
3561 /*
3562 ** CAPI3REF: Index Of A Parameter With A Given Name
3563 **
3564 ** ^Return the index of an SQL parameter given its name.  ^The
3565 ** index value returned is suitable for use as the second
3566 ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
3567 ** is returned if no matching parameter is found.  ^The parameter
3568 ** name must be given in UTF-8 even if the original statement
3569 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
3570 **
3571 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3572 ** [sqlite3_bind_parameter_count()], and
3573 ** [sqlite3_bind_parameter_index()].
3574 */
3575 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
3576 
3577 /*
3578 ** CAPI3REF: Reset All Bindings On A Prepared Statement
3579 **
3580 ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
3581 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
3582 ** ^Use this routine to reset all host parameters to NULL.
3583 */
3584 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
3585 
3586 /*
3587 ** CAPI3REF: Number Of Columns In A Result Set
3588 **
3589 ** ^Return the number of columns in the result set returned by the
3590 ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
3591 ** statement that does not return data (for example an [UPDATE]).
3592 **
3593 ** See also: [sqlite3_data_count()]
3594 */
3595 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
3596 
3597 /*
3598 ** CAPI3REF: Column Names In A Result Set
3599 **
3600 ** ^These routines return the name assigned to a particular column
3601 ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
3602 ** interface returns a pointer to a zero-terminated UTF-8 string
3603 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
3604 ** UTF-16 string.  ^The first parameter is the [prepared statement]
3605 ** that implements the [SELECT] statement. ^The second parameter is the
3606 ** column number.  ^The leftmost column is number 0.
3607 **
3608 ** ^The returned string pointer is valid until either the [prepared statement]
3609 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3610 ** reprepared by the first call to [sqlite3_step()] for a particular run
3611 ** or until the next call to
3612 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3613 **
3614 ** ^If sqlite3_malloc() fails during the processing of either routine
3615 ** (for example during a conversion from UTF-8 to UTF-16) then a
3616 ** NULL pointer is returned.
3617 **
3618 ** ^The name of a result column is the value of the "AS" clause for
3619 ** that column, if there is an AS clause.  If there is no AS clause
3620 ** then the name of the column is unspecified and may change from
3621 ** one release of SQLite to the next.
3622 */
3623 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
3624 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
3625 
3626 /*
3627 ** CAPI3REF: Source Of Data In A Query Result
3628 **
3629 ** ^These routines provide a means to determine the database, table, and
3630 ** table column that is the origin of a particular result column in
3631 ** [SELECT] statement.
3632 ** ^The name of the database or table or column can be returned as
3633 ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
3634 ** the database name, the _table_ routines return the table name, and
3635 ** the origin_ routines return the column name.
3636 ** ^The returned string is valid until the [prepared statement] is destroyed
3637 ** using [sqlite3_finalize()] or until the statement is automatically
3638 ** reprepared by the first call to [sqlite3_step()] for a particular run
3639 ** or until the same information is requested
3640 ** again in a different encoding.
3641 **
3642 ** ^The names returned are the original un-aliased names of the
3643 ** database, table, and column.
3644 **
3645 ** ^The first argument to these interfaces is a [prepared statement].
3646 ** ^These functions return information about the Nth result column returned by
3647 ** the statement, where N is the second function argument.
3648 ** ^The left-most column is column 0 for these routines.
3649 **
3650 ** ^If the Nth column returned by the statement is an expression or
3651 ** subquery and is not a column value, then all of these functions return
3652 ** NULL.  ^These routine might also return NULL if a memory allocation error
3653 ** occurs.  ^Otherwise, they return the name of the attached database, table,
3654 ** or column that query result column was extracted from.
3655 **
3656 ** ^As with all other SQLite APIs, those whose names end with "16" return
3657 ** UTF-16 encoded strings and the other functions return UTF-8.
3658 **
3659 ** ^These APIs are only available if the library was compiled with the
3660 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
3661 **
3662 ** If two or more threads call one or more of these routines against the same
3663 ** prepared statement and column at the same time then the results are
3664 ** undefined.
3665 **
3666 ** If two or more threads call one or more
3667 ** [sqlite3_column_database_name | column metadata interfaces]
3668 ** for the same [prepared statement] and result column
3669 ** at the same time then the results are undefined.
3670 */
3671 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
3672 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
3673 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
3674 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
3675 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
3676 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
3677 
3678 /*
3679 ** CAPI3REF: Declared Datatype Of A Query Result
3680 **
3681 ** ^(The first parameter is a [prepared statement].
3682 ** If this statement is a [SELECT] statement and the Nth column of the
3683 ** returned result set of that [SELECT] is a table column (not an
3684 ** expression or subquery) then the declared type of the table
3685 ** column is returned.)^  ^If the Nth column of the result set is an
3686 ** expression or subquery, then a NULL pointer is returned.
3687 ** ^The returned string is always UTF-8 encoded.
3688 **
3689 ** ^(For example, given the database schema:
3690 **
3691 ** CREATE TABLE t1(c1 VARIANT);
3692 **
3693 ** and the following statement to be compiled:
3694 **
3695 ** SELECT c1 + 1, c1 FROM t1;
3696 **
3697 ** this routine would return the string "VARIANT" for the second result
3698 ** column (i==1), and a NULL pointer for the first result column (i==0).)^
3699 **
3700 ** ^SQLite uses dynamic run-time typing.  ^So just because a column
3701 ** is declared to contain a particular type does not mean that the
3702 ** data stored in that column is of the declared type.  SQLite is
3703 ** strongly typed, but the typing is dynamic not static.  ^Type
3704 ** is associated with individual values, not with the containers
3705 ** used to hold those values.
3706 */
3707 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
3708 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
3709 
3710 /*
3711 ** CAPI3REF: Evaluate An SQL Statement
3712 **
3713 ** After a [prepared statement] has been prepared using either
3714 ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
3715 ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
3716 ** must be called one or more times to evaluate the statement.
3717 **
3718 ** The details of the behavior of the sqlite3_step() interface depend
3719 ** on whether the statement was prepared using the newer "v2" interface
3720 ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
3721 ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
3722 ** new "v2" interface is recommended for new applications but the legacy
3723 ** interface will continue to be supported.
3724 **
3725 ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
3726 ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
3727 ** ^With the "v2" interface, any of the other [result codes] or
3728 ** [extended result codes] might be returned as well.
3729 **
3730 ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
3731 ** database locks it needs to do its job.  ^If the statement is a [COMMIT]
3732 ** or occurs outside of an explicit transaction, then you can retry the
3733 ** statement.  If the statement is not a [COMMIT] and occurs within an
3734 ** explicit transaction then you should rollback the transaction before
3735 ** continuing.
3736 **
3737 ** ^[SQLITE_DONE] means that the statement has finished executing
3738 ** successfully.  sqlite3_step() should not be called again on this virtual
3739 ** machine without first calling [sqlite3_reset()] to reset the virtual
3740 ** machine back to its initial state.
3741 **
3742 ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
3743 ** is returned each time a new row of data is ready for processing by the
3744 ** caller. The values may be accessed using the [column access functions].
3745 ** sqlite3_step() is called again to retrieve the next row of data.
3746 **
3747 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
3748 ** violation) has occurred.  sqlite3_step() should not be called again on
3749 ** the VM. More information may be found by calling [sqlite3_errmsg()].
3750 ** ^With the legacy interface, a more specific error code (for example,
3751 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
3752 ** can be obtained by calling [sqlite3_reset()] on the
3753 ** [prepared statement].  ^In the "v2" interface,
3754 ** the more specific error code is returned directly by sqlite3_step().
3755 **
3756 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
3757 ** Perhaps it was called on a [prepared statement] that has
3758 ** already been [sqlite3_finalize | finalized] or on one that had
3759 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
3760 ** be the case that the same database connection is being used by two or
3761 ** more threads at the same moment in time.
3762 **
3763 ** For all versions of SQLite up to and including 3.6.23.1, a call to
3764 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
3765 ** other than [SQLITE_ROW] before any subsequent invocation of
3766 ** sqlite3_step().  Failure to reset the prepared statement using
3767 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3768 ** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
3769 ** calling [sqlite3_reset()] automatically in this circumstance rather
3770 ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
3771 ** break because any application that ever receives an SQLITE_MISUSE error
3772 ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
3773 ** can be used to restore the legacy behavior.
3774 **
3775 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3776 ** API always returns a generic error code, [SQLITE_ERROR], following any
3777 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
3778 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
3779 ** specific [error codes] that better describes the error.
3780 ** We admit that this is a goofy design.  The problem has been fixed
3781 ** with the "v2" interface.  If you prepare all of your SQL statements
3782 ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
3783 ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
3784 ** then the more specific [error codes] are returned directly
3785 ** by sqlite3_step().  The use of the "v2" interface is recommended.
3786 */
3787 SQLITE_API int sqlite3_step(sqlite3_stmt*);
3788 
3789 /*
3790 ** CAPI3REF: Number of columns in a result set
3791 **
3792 ** ^The sqlite3_data_count(P) interface returns the number of columns in the
3793 ** current row of the result set of [prepared statement] P.
3794 ** ^If prepared statement P does not have results ready to return
3795 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3796 ** interfaces) then sqlite3_data_count(P) returns 0.
3797 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3798 ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
3799 ** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
3800 ** will return non-zero if previous call to [sqlite3_step](P) returned
3801 ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
3802 ** where it always returns zero since each step of that multi-step
3803 ** pragma returns 0 columns of data.
3804 **
3805 ** See also: [sqlite3_column_count()]
3806 */
3807 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3808 
3809 /*
3810 ** CAPI3REF: Fundamental Datatypes
3811 ** KEYWORDS: SQLITE_TEXT
3812 **
3813 ** ^(Every value in SQLite has one of five fundamental datatypes:
3814 **
3815 ** <ul>
3816 ** <li> 64-bit signed integer
3817 ** <li> 64-bit IEEE floating point number
3818 ** <li> string
3819 ** <li> BLOB
3820 ** <li> NULL
3821 ** </ul>)^
3822 **
3823 ** These constants are codes for each of those types.
3824 **
3825 ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
3826 ** for a completely different meaning.  Software that links against both
3827 ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
3828 ** SQLITE_TEXT.
3829 */
3830 #define SQLITE_INTEGER  1
3831 #define SQLITE_FLOAT    2
3832 #define SQLITE_BLOB     4
3833 #define SQLITE_NULL     5
3834 #ifdef SQLITE_TEXT
3835 # undef SQLITE_TEXT
3836 #else
3837 # define SQLITE_TEXT     3
3838 #endif
3839 #define SQLITE3_TEXT     3
3840 
3841 /*
3842 ** CAPI3REF: Result Values From A Query
3843 ** KEYWORDS: {column access functions}
3844 **
3845 ** These routines form the "result set" interface.
3846 **
3847 ** ^These routines return information about a single column of the current
3848 ** result row of a query.  ^In every case the first argument is a pointer
3849 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
3850 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
3851 ** and the second argument is the index of the column for which information
3852 ** should be returned. ^The leftmost column of the result set has the index 0.
3853 ** ^The number of columns in the result can be determined using
3854 ** [sqlite3_column_count()].
3855 **
3856 ** If the SQL statement does not currently point to a valid row, or if the
3857 ** column index is out of range, the result is undefined.
3858 ** These routines may only be called when the most recent call to
3859 ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
3860 ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
3861 ** If any of these routines are called after [sqlite3_reset()] or
3862 ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
3863 ** something other than [SQLITE_ROW], the results are undefined.
3864 ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
3865 ** are called from a different thread while any of these routines
3866 ** are pending, then the results are undefined.
3867 **
3868 ** ^The sqlite3_column_type() routine returns the
3869 ** [SQLITE_INTEGER | datatype code] for the initial data type
3870 ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
3871 ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
3872 ** returned by sqlite3_column_type() is only meaningful if no type
3873 ** conversions have occurred as described below.  After a type conversion,
3874 ** the value returned by sqlite3_column_type() is undefined.  Future
3875 ** versions of SQLite may change the behavior of sqlite3_column_type()
3876 ** following a type conversion.
3877 **
3878 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
3879 ** routine returns the number of bytes in that BLOB or string.
3880 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
3881 ** the string to UTF-8 and then returns the number of bytes.
3882 ** ^If the result is a numeric value then sqlite3_column_bytes() uses
3883 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
3884 ** the number of bytes in that string.
3885 ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
3886 **
3887 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
3888 ** routine returns the number of bytes in that BLOB or string.
3889 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
3890 ** the string to UTF-16 and then returns the number of bytes.
3891 ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
3892 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
3893 ** the number of bytes in that string.
3894 ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
3895 **
3896 ** ^The values returned by [sqlite3_column_bytes()] and
3897 ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
3898 ** of the string.  ^For clarity: the values returned by
3899 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
3900 ** bytes in the string, not the number of characters.
3901 **
3902 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3903 ** even empty strings, are always zero-terminated.  ^The return
3904 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3905 **
3906 ** ^The object returned by [sqlite3_column_value()] is an
3907 ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
3908 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
3909 ** If the [unprotected sqlite3_value] object returned by
3910 ** [sqlite3_column_value()] is used in any other way, including calls
3911 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
3912 ** or [sqlite3_value_bytes()], then the behavior is undefined.
3913 **
3914 ** These routines attempt to convert the value where appropriate.  ^For
3915 ** example, if the internal representation is FLOAT and a text result
3916 ** is requested, [sqlite3_snprintf()] is used internally to perform the
3917 ** conversion automatically.  ^(The following table details the conversions
3918 ** that are applied:
3919 **
3920 ** <blockquote>
3921 ** <table border="1">
3922 ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
3923 **
3924 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
3925 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
3926 ** <tr><td>  NULL    <td>   TEXT    <td> Result is a NULL pointer
3927 ** <tr><td>  NULL    <td>   BLOB    <td> Result is a NULL pointer
3928 ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
3929 ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
3930 ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
3931 ** <tr><td>  FLOAT   <td> INTEGER   <td> [CAST] to INTEGER
3932 ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
3933 ** <tr><td>  FLOAT   <td>   BLOB    <td> [CAST] to BLOB
3934 ** <tr><td>  TEXT    <td> INTEGER   <td> [CAST] to INTEGER
3935 ** <tr><td>  TEXT    <td>  FLOAT    <td> [CAST] to REAL
3936 ** <tr><td>  TEXT    <td>   BLOB    <td> No change
3937 ** <tr><td>  BLOB    <td> INTEGER   <td> [CAST] to INTEGER
3938 ** <tr><td>  BLOB    <td>  FLOAT    <td> [CAST] to REAL
3939 ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
3940 ** </table>
3941 ** </blockquote>)^
3942 **
3943 ** The table above makes reference to standard C library functions atoi()
3944 ** and atof().  SQLite does not really use these functions.  It has its
3945 ** own equivalent internal routines.  The atoi() and atof() names are
3946 ** used in the table for brevity and because they are familiar to most
3947 ** C programmers.
3948 **
3949 ** Note that when type conversions occur, pointers returned by prior
3950 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
3951 ** sqlite3_column_text16() may be invalidated.
3952 ** Type conversions and pointer invalidations might occur
3953 ** in the following cases:
3954 **
3955 ** <ul>
3956 ** <li> The initial content is a BLOB and sqlite3_column_text() or
3957 **      sqlite3_column_text16() is called.  A zero-terminator might
3958 **      need to be added to the string.</li>
3959 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
3960 **      sqlite3_column_text16() is called.  The content must be converted
3961 **      to UTF-16.</li>
3962 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
3963 **      sqlite3_column_text() is called.  The content must be converted
3964 **      to UTF-8.</li>
3965 ** </ul>
3966 **
3967 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
3968 ** not invalidate a prior pointer, though of course the content of the buffer
3969 ** that the prior pointer references will have been modified.  Other kinds
3970 ** of conversion are done in place when it is possible, but sometimes they
3971 ** are not possible and in those cases prior pointers are invalidated.
3972 **
3973 ** The safest and easiest to remember policy is to invoke these routines
3974 ** in one of the following ways:
3975 **
3976 ** <ul>
3977 **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
3978 **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
3979 **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
3980 ** </ul>
3981 **
3982 ** In other words, you should call sqlite3_column_text(),
3983 ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
3984 ** into the desired format, then invoke sqlite3_column_bytes() or
3985 ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
3986 ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
3987 ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
3988 ** with calls to sqlite3_column_bytes().
3989 **
3990 ** ^The pointers returned are valid until a type conversion occurs as
3991 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
3992 ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
3993 ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
3994 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3995 ** [sqlite3_free()].
3996 **
3997 ** ^(If a memory allocation error occurs during the evaluation of any
3998 ** of these routines, a default value is returned.  The default value
3999 ** is either the integer 0, the floating point number 0.0, or a NULL
4000 ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
4001 ** [SQLITE_NOMEM].)^
4002 */
4003 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
4004 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
4005 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
4006 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
4007 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
4008 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
4009 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
4010 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
4011 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
4012 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
4013 
4014 /*
4015 ** CAPI3REF: Destroy A Prepared Statement Object
4016 **
4017 ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
4018 ** ^If the most recent evaluation of the statement encountered no errors
4019 ** or if the statement is never been evaluated, then sqlite3_finalize() returns
4020 ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
4021 ** sqlite3_finalize(S) returns the appropriate [error code] or
4022 ** [extended error code].
4023 **
4024 ** ^The sqlite3_finalize(S) routine can be called at any point during
4025 ** the life cycle of [prepared statement] S:
4026 ** before statement S is ever evaluated, after
4027 ** one or more calls to [sqlite3_reset()], or after any call
4028 ** to [sqlite3_step()] regardless of whether or not the statement has
4029 ** completed execution.
4030 **
4031 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
4032 **
4033 ** The application must finalize every [prepared statement] in order to avoid
4034 ** resource leaks.  It is a grievous error for the application to try to use
4035 ** a prepared statement after it has been finalized.  Any use of a prepared
4036 ** statement after it has been finalized can result in undefined and
4037 ** undesirable behavior such as segfaults and heap corruption.
4038 */
4039 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
4040 
4041 /*
4042 ** CAPI3REF: Reset A Prepared Statement Object
4043 **
4044 ** The sqlite3_reset() function is called to reset a [prepared statement]
4045 ** object back to its initial state, ready to be re-executed.
4046 ** ^Any SQL statement variables that had values bound to them using
4047 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
4048 ** Use [sqlite3_clear_bindings()] to reset the bindings.
4049 **
4050 ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
4051 ** back to the beginning of its program.
4052 **
4053 ** ^If the most recent call to [sqlite3_step(S)] for the
4054 ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
4055 ** or if [sqlite3_step(S)] has never before been called on S,
4056 ** then [sqlite3_reset(S)] returns [SQLITE_OK].
4057 **
4058 ** ^If the most recent call to [sqlite3_step(S)] for the
4059 ** [prepared statement] S indicated an error, then
4060 ** [sqlite3_reset(S)] returns an appropriate [error code].
4061 **
4062 ** ^The [sqlite3_reset(S)] interface does not change the values
4063 ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
4064 */
4065 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
4066 
4067 /*
4068 ** CAPI3REF: Create Or Redefine SQL Functions
4069 ** KEYWORDS: {function creation routines}
4070 ** KEYWORDS: {application-defined SQL function}
4071 ** KEYWORDS: {application-defined SQL functions}
4072 **
4073 ** ^These functions (collectively known as "function creation routines")
4074 ** are used to add SQL functions or aggregates or to redefine the behavior
4075 ** of existing SQL functions or aggregates.  The only differences between
4076 ** these routines are the text encoding expected for
4077 ** the second parameter (the name of the function being created)
4078 ** and the presence or absence of a destructor callback for
4079 ** the application data pointer.
4080 **
4081 ** ^The first parameter is the [database connection] to which the SQL
4082 ** function is to be added.  ^If an application uses more than one database
4083 ** connection then application-defined SQL functions must be added
4084 ** to each database connection separately.
4085 **
4086 ** ^The second parameter is the name of the SQL function to be created or
4087 ** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
4088 ** representation, exclusive of the zero-terminator.  ^Note that the name
4089 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
4090 ** ^Any attempt to create a function with a longer name
4091 ** will result in [SQLITE_MISUSE] being returned.
4092 **
4093 ** ^The third parameter (nArg)
4094 ** is the number of arguments that the SQL function or
4095 ** aggregate takes. ^If this parameter is -1, then the SQL function or
4096 ** aggregate may take any number of arguments between 0 and the limit
4097 ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
4098 ** parameter is less than -1 or greater than 127 then the behavior is
4099 ** undefined.
4100 **
4101 ** ^The fourth parameter, eTextRep, specifies what
4102 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
4103 ** its parameters.  The application should set this parameter to
4104 ** [SQLITE_UTF16LE] if the function implementation invokes
4105 ** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
4106 ** implementation invokes [sqlite3_value_text16be()] on an input, or
4107 ** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
4108 ** otherwise.  ^The same SQL function may be registered multiple times using
4109 ** different preferred text encodings, with different implementations for
4110 ** each encoding.
4111 ** ^When multiple implementations of the same function are available, SQLite
4112 ** will pick the one that involves the least amount of data conversion.
4113 **
4114 ** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
4115 ** to signal that the function will always return the same result given
4116 ** the same inputs within a single SQL statement.  Most SQL functions are
4117 ** deterministic.  The built-in [random()] SQL function is an example of a
4118 ** function that is not deterministic.  The SQLite query planner is able to
4119 ** perform additional optimizations on deterministic functions, so use
4120 ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
4121 **
4122 ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
4123 ** function can gain access to this pointer using [sqlite3_user_data()].)^
4124 **
4125 ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
4126 ** pointers to C-language functions that implement the SQL function or
4127 ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
4128 ** callback only; NULL pointers must be passed as the xStep and xFinal
4129 ** parameters. ^An aggregate SQL function requires an implementation of xStep
4130 ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
4131 ** SQL function or aggregate, pass NULL pointers for all three function
4132 ** callbacks.
4133 **
4134 ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
4135 ** then it is destructor for the application data pointer.
4136 ** The destructor is invoked when the function is deleted, either by being
4137 ** overloaded or when the database connection closes.)^
4138 ** ^The destructor is also invoked if the call to
4139 ** sqlite3_create_function_v2() fails.
4140 ** ^When the destructor callback of the tenth parameter is invoked, it
4141 ** is passed a single argument which is a copy of the application data
4142 ** pointer which was the fifth parameter to sqlite3_create_function_v2().
4143 **
4144 ** ^It is permitted to register multiple implementations of the same
4145 ** functions with the same name but with either differing numbers of
4146 ** arguments or differing preferred text encodings.  ^SQLite will use
4147 ** the implementation that most closely matches the way in which the
4148 ** SQL function is used.  ^A function implementation with a non-negative
4149 ** nArg parameter is a better match than a function implementation with
4150 ** a negative nArg.  ^A function where the preferred text encoding
4151 ** matches the database encoding is a better
4152 ** match than a function where the encoding is different.
4153 ** ^A function where the encoding difference is between UTF16le and UTF16be
4154 ** is a closer match than a function where the encoding difference is
4155 ** between UTF8 and UTF16.
4156 **
4157 ** ^Built-in functions may be overloaded by new application-defined functions.
4158 **
4159 ** ^An application-defined function is permitted to call other
4160 ** SQLite interfaces.  However, such calls must not
4161 ** close the database connection nor finalize or reset the prepared
4162 ** statement in which the function is running.
4163 */
4164 SQLITE_API int sqlite3_create_function(
4165   sqlite3 *db,
4166   const char *zFunctionName,
4167   int nArg,
4168   int eTextRep,
4169   void *pApp,
4170   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4171   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4172   void (*xFinal)(sqlite3_context*)
4173 );
4174 SQLITE_API int sqlite3_create_function16(
4175   sqlite3 *db,
4176   const void *zFunctionName,
4177   int nArg,
4178   int eTextRep,
4179   void *pApp,
4180   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4181   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4182   void (*xFinal)(sqlite3_context*)
4183 );
4184 SQLITE_API int sqlite3_create_function_v2(
4185   sqlite3 *db,
4186   const char *zFunctionName,
4187   int nArg,
4188   int eTextRep,
4189   void *pApp,
4190   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4191   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4192   void (*xFinal)(sqlite3_context*),
4193   void(*xDestroy)(void*)
4194 );
4195 
4196 /*
4197 ** CAPI3REF: Text Encodings
4198 **
4199 ** These constant define integer codes that represent the various
4200 ** text encodings supported by SQLite.
4201 */
4202 #define SQLITE_UTF8           1
4203 #define SQLITE_UTF16LE        2
4204 #define SQLITE_UTF16BE        3
4205 #define SQLITE_UTF16          4    /* Use native byte order */
4206 #define SQLITE_ANY            5    /* Deprecated */
4207 #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
4208 
4209 /*
4210 ** CAPI3REF: Function Flags
4211 **
4212 ** These constants may be ORed together with the
4213 ** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
4214 ** to [sqlite3_create_function()], [sqlite3_create_function16()], or
4215 ** [sqlite3_create_function_v2()].
4216 */
4217 #define SQLITE_DETERMINISTIC    0x800
4218 
4219 /*
4220 ** CAPI3REF: Deprecated Functions
4221 ** DEPRECATED
4222 **
4223 ** These functions are [deprecated].  In order to maintain
4224 ** backwards compatibility with older code, these functions continue
4225 ** to be supported.  However, new applications should avoid
4226 ** the use of these functions.  To help encourage people to avoid
4227 ** using these functions, we are not going to tell you what they do.
4228 */
4229 #ifndef SQLITE_OMIT_DEPRECATED
4230 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4231 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4232 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
4233 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
4234 SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
4235 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
4236                       void*,sqlite3_int64);
4237 #endif
4238 
4239 /*
4240 ** CAPI3REF: Obtaining SQL Function Parameter Values
4241 **
4242 ** The C-language implementation of SQL functions and aggregates uses
4243 ** this set of interface routines to access the parameter values on
4244 ** the function or aggregate.
4245 **
4246 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4247 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4248 ** define callbacks that implement the SQL functions and aggregates.
4249 ** The 3rd parameter to these callbacks is an array of pointers to
4250 ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
4251 ** each parameter to the SQL function.  These routines are used to
4252 ** extract values from the [sqlite3_value] objects.
4253 **
4254 ** These routines work only with [protected sqlite3_value] objects.
4255 ** Any attempt to use these routines on an [unprotected sqlite3_value]
4256 ** object results in undefined behavior.
4257 **
4258 ** ^These routines work just like the corresponding [column access functions]
4259 ** except that  these routines take a single [protected sqlite3_value] object
4260 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
4261 **
4262 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
4263 ** in the native byte-order of the host machine.  ^The
4264 ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
4265 ** extract UTF-16 strings as big-endian and little-endian respectively.
4266 **
4267 ** ^(The sqlite3_value_numeric_type() interface attempts to apply
4268 ** numeric affinity to the value.  This means that an attempt is
4269 ** made to convert the value to an integer or floating point.  If
4270 ** such a conversion is possible without loss of information (in other
4271 ** words, if the value is a string that looks like a number)
4272 ** then the conversion is performed.  Otherwise no conversion occurs.
4273 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
4274 **
4275 ** Please pay particular attention to the fact that the pointer returned
4276 ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
4277 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
4278 ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
4279 ** or [sqlite3_value_text16()].
4280 **
4281 ** These routines must be called from the same thread as
4282 ** the SQL function that supplied the [sqlite3_value*] parameters.
4283 */
4284 SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
4285 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
4286 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
4287 SQLITE_API double sqlite3_value_double(sqlite3_value*);
4288 SQLITE_API int sqlite3_value_int(sqlite3_value*);
4289 SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
4290 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
4291 SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
4292 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
4293 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
4294 SQLITE_API int sqlite3_value_type(sqlite3_value*);
4295 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
4296 
4297 /*
4298 ** CAPI3REF: Obtain Aggregate Function Context
4299 **
4300 ** Implementations of aggregate SQL functions use this
4301 ** routine to allocate memory for storing their state.
4302 **
4303 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
4304 ** for a particular aggregate function, SQLite
4305 ** allocates N of memory, zeroes out that memory, and returns a pointer
4306 ** to the new memory. ^On second and subsequent calls to
4307 ** sqlite3_aggregate_context() for the same aggregate function instance,
4308 ** the same buffer is returned.  Sqlite3_aggregate_context() is normally
4309 ** called once for each invocation of the xStep callback and then one
4310 ** last time when the xFinal callback is invoked.  ^(When no rows match
4311 ** an aggregate query, the xStep() callback of the aggregate function
4312 ** implementation is never called and xFinal() is called exactly once.
4313 ** In those cases, sqlite3_aggregate_context() might be called for the
4314 ** first time from within xFinal().)^
4315 **
4316 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
4317 ** when first called if N is less than or equal to zero or if a memory
4318 ** allocate error occurs.
4319 **
4320 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
4321 ** determined by the N parameter on first successful call.  Changing the
4322 ** value of N in subsequent call to sqlite3_aggregate_context() within
4323 ** the same aggregate function instance will not resize the memory
4324 ** allocation.)^  Within the xFinal callback, it is customary to set
4325 ** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
4326 ** pointless memory allocations occur.
4327 **
4328 ** ^SQLite automatically frees the memory allocated by
4329 ** sqlite3_aggregate_context() when the aggregate query concludes.
4330 **
4331 ** The first parameter must be a copy of the
4332 ** [sqlite3_context | SQL function context] that is the first parameter
4333 ** to the xStep or xFinal callback routine that implements the aggregate
4334 ** function.
4335 **
4336 ** This routine must be called from the same thread in which
4337 ** the aggregate SQL function is running.
4338 */
4339 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
4340 
4341 /*
4342 ** CAPI3REF: User Data For Functions
4343 **
4344 ** ^The sqlite3_user_data() interface returns a copy of
4345 ** the pointer that was the pUserData parameter (the 5th parameter)
4346 ** of the [sqlite3_create_function()]
4347 ** and [sqlite3_create_function16()] routines that originally
4348 ** registered the application defined function.
4349 **
4350 ** This routine must be called from the same thread in which
4351 ** the application-defined function is running.
4352 */
4353 SQLITE_API void *sqlite3_user_data(sqlite3_context*);
4354 
4355 /*
4356 ** CAPI3REF: Database Connection For Functions
4357 **
4358 ** ^The sqlite3_context_db_handle() interface returns a copy of
4359 ** the pointer to the [database connection] (the 1st parameter)
4360 ** of the [sqlite3_create_function()]
4361 ** and [sqlite3_create_function16()] routines that originally
4362 ** registered the application defined function.
4363 */
4364 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
4365 
4366 /*
4367 ** CAPI3REF: Function Auxiliary Data
4368 **
4369 ** These functions may be used by (non-aggregate) SQL functions to
4370 ** associate metadata with argument values. If the same value is passed to
4371 ** multiple invocations of the same SQL function during query execution, under
4372 ** some circumstances the associated metadata may be preserved.  An example
4373 ** of where this might be useful is in a regular-expression matching
4374 ** function. The compiled version of the regular expression can be stored as
4375 ** metadata associated with the pattern string.
4376 ** Then as long as the pattern string remains the same,
4377 ** the compiled regular expression can be reused on multiple
4378 ** invocations of the same function.
4379 **
4380 ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
4381 ** associated by the sqlite3_set_auxdata() function with the Nth argument
4382 ** value to the application-defined function. ^If there is no metadata
4383 ** associated with the function argument, this sqlite3_get_auxdata() interface
4384 ** returns a NULL pointer.
4385 **
4386 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
4387 ** argument of the application-defined function.  ^Subsequent
4388 ** calls to sqlite3_get_auxdata(C,N) return P from the most recent
4389 ** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
4390 ** NULL if the metadata has been discarded.
4391 ** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
4392 ** SQLite will invoke the destructor function X with parameter P exactly
4393 ** once, when the metadata is discarded.
4394 ** SQLite is free to discard the metadata at any time, including: <ul>
4395 ** <li> when the corresponding function parameter changes, or
4396 ** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
4397 **      SQL statement, or
4398 ** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
4399 ** <li> during the original sqlite3_set_auxdata() call when a memory
4400 **      allocation error occurs. </ul>)^
4401 **
4402 ** Note the last bullet in particular.  The destructor X in
4403 ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
4404 ** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
4405 ** should be called near the end of the function implementation and the
4406 ** function implementation should not make any use of P after
4407 ** sqlite3_set_auxdata() has been called.
4408 **
4409 ** ^(In practice, metadata is preserved between function calls for
4410 ** function parameters that are compile-time constants, including literal
4411 ** values and [parameters] and expressions composed from the same.)^
4412 **
4413 ** These routines must be called from the same thread in which
4414 ** the SQL function is running.
4415 */
4416 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
4417 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
4418 
4419 
4420 /*
4421 ** CAPI3REF: Constants Defining Special Destructor Behavior
4422 **
4423 ** These are special values for the destructor that is passed in as the
4424 ** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
4425 ** argument is SQLITE_STATIC, it means that the content pointer is constant
4426 ** and will never change.  It does not need to be destroyed.  ^The
4427 ** SQLITE_TRANSIENT value means that the content will likely change in
4428 ** the near future and that SQLite should make its own private copy of
4429 ** the content before returning.
4430 **
4431 ** The typedef is necessary to work around problems in certain
4432 ** C++ compilers.
4433 */
4434 typedef void (*sqlite3_destructor_type)(void*);
4435 #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
4436 #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
4437 
4438 /*
4439 ** CAPI3REF: Setting The Result Of An SQL Function
4440 **
4441 ** These routines are used by the xFunc or xFinal callbacks that
4442 ** implement SQL functions and aggregates.  See
4443 ** [sqlite3_create_function()] and [sqlite3_create_function16()]
4444 ** for additional information.
4445 **
4446 ** These functions work very much like the [parameter binding] family of
4447 ** functions used to bind values to host parameters in prepared statements.
4448 ** Refer to the [SQL parameter] documentation for additional information.
4449 **
4450 ** ^The sqlite3_result_blob() interface sets the result from
4451 ** an application-defined function to be the BLOB whose content is pointed
4452 ** to by the second parameter and which is N bytes long where N is the
4453 ** third parameter.
4454 **
4455 ** ^The sqlite3_result_zeroblob() interfaces set the result of
4456 ** the application-defined function to be a BLOB containing all zero
4457 ** bytes and N bytes in size, where N is the value of the 2nd parameter.
4458 **
4459 ** ^The sqlite3_result_double() interface sets the result from
4460 ** an application-defined function to be a floating point value specified
4461 ** by its 2nd argument.
4462 **
4463 ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
4464 ** cause the implemented SQL function to throw an exception.
4465 ** ^SQLite uses the string pointed to by the
4466 ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
4467 ** as the text of an error message.  ^SQLite interprets the error
4468 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
4469 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
4470 ** byte order.  ^If the third parameter to sqlite3_result_error()
4471 ** or sqlite3_result_error16() is negative then SQLite takes as the error
4472 ** message all text up through the first zero character.
4473 ** ^If the third parameter to sqlite3_result_error() or
4474 ** sqlite3_result_error16() is non-negative then SQLite takes that many
4475 ** bytes (not characters) from the 2nd parameter as the error message.
4476 ** ^The sqlite3_result_error() and sqlite3_result_error16()
4477 ** routines make a private copy of the error message text before
4478 ** they return.  Hence, the calling function can deallocate or
4479 ** modify the text after they return without harm.
4480 ** ^The sqlite3_result_error_code() function changes the error code
4481 ** returned by SQLite as a result of an error in a function.  ^By default,
4482 ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
4483 ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
4484 **
4485 ** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
4486 ** error indicating that a string or BLOB is too long to represent.
4487 **
4488 ** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
4489 ** error indicating that a memory allocation failed.
4490 **
4491 ** ^The sqlite3_result_int() interface sets the return value
4492 ** of the application-defined function to be the 32-bit signed integer
4493 ** value given in the 2nd argument.
4494 ** ^The sqlite3_result_int64() interface sets the return value
4495 ** of the application-defined function to be the 64-bit signed integer
4496 ** value given in the 2nd argument.
4497 **
4498 ** ^The sqlite3_result_null() interface sets the return value
4499 ** of the application-defined function to be NULL.
4500 **
4501 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
4502 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4503 ** set the return value of the application-defined function to be
4504 ** a text string which is represented as UTF-8, UTF-16 native byte order,
4505 ** UTF-16 little endian, or UTF-16 big endian, respectively.
4506 ** ^SQLite takes the text result from the application from
4507 ** the 2nd parameter of the sqlite3_result_text* interfaces.
4508 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4509 ** is negative, then SQLite takes result text from the 2nd parameter
4510 ** through the first zero character.
4511 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4512 ** is non-negative, then as many bytes (not characters) of the text
4513 ** pointed to by the 2nd parameter are taken as the application-defined
4514 ** function result.  If the 3rd parameter is non-negative, then it
4515 ** must be the byte offset into the string where the NUL terminator would
4516 ** appear if the string where NUL terminated.  If any NUL characters occur
4517 ** in the string at a byte offset that is less than the value of the 3rd
4518 ** parameter, then the resulting string will contain embedded NULs and the
4519 ** result of expressions operating on strings with embedded NULs is undefined.
4520 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4521 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4522 ** function as the destructor on the text or BLOB result when it has
4523 ** finished using that result.
4524 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
4525 ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
4526 ** assumes that the text or BLOB result is in constant space and does not
4527 ** copy the content of the parameter nor call a destructor on the content
4528 ** when it has finished using that result.
4529 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4530 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4531 ** then SQLite makes a copy of the result into space obtained from
4532 ** from [sqlite3_malloc()] before it returns.
4533 **
4534 ** ^The sqlite3_result_value() interface sets the result of
4535 ** the application-defined function to be a copy the
4536 ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
4537 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4538 ** so that the [sqlite3_value] specified in the parameter may change or
4539 ** be deallocated after sqlite3_result_value() returns without harm.
4540 ** ^A [protected sqlite3_value] object may always be used where an
4541 ** [unprotected sqlite3_value] object is required, so either
4542 ** kind of [sqlite3_value] object can be used with this interface.
4543 **
4544 ** If these routines are called from within the different thread
4545 ** than the one containing the application-defined function that received
4546 ** the [sqlite3_context] pointer, the results are undefined.
4547 */
4548 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
4549 SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
4550 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
4551 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
4552 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
4553 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
4554 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
4555 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
4556 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
4557 SQLITE_API void sqlite3_result_null(sqlite3_context*);
4558 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
4559 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
4560 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
4561 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
4562 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
4563 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
4564 
4565 /*
4566 ** CAPI3REF: Define New Collating Sequences
4567 **
4568 ** ^These functions add, remove, or modify a [collation] associated
4569 ** with the [database connection] specified as the first argument.
4570 **
4571 ** ^The name of the collation is a UTF-8 string
4572 ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
4573 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
4574 ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
4575 ** considered to be the same name.
4576 **
4577 ** ^(The third argument (eTextRep) must be one of the constants:
4578 ** <ul>
4579 ** <li> [SQLITE_UTF8],
4580 ** <li> [SQLITE_UTF16LE],
4581 ** <li> [SQLITE_UTF16BE],
4582 ** <li> [SQLITE_UTF16], or
4583 ** <li> [SQLITE_UTF16_ALIGNED].
4584 ** </ul>)^
4585 ** ^The eTextRep argument determines the encoding of strings passed
4586 ** to the collating function callback, xCallback.
4587 ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
4588 ** force strings to be UTF16 with native byte order.
4589 ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
4590 ** on an even byte address.
4591 **
4592 ** ^The fourth argument, pArg, is an application data pointer that is passed
4593 ** through as the first argument to the collating function callback.
4594 **
4595 ** ^The fifth argument, xCallback, is a pointer to the collating function.
4596 ** ^Multiple collating functions can be registered using the same name but
4597 ** with different eTextRep parameters and SQLite will use whichever
4598 ** function requires the least amount of data transformation.
4599 ** ^If the xCallback argument is NULL then the collating function is
4600 ** deleted.  ^When all collating functions having the same name are deleted,
4601 ** that collation is no longer usable.
4602 **
4603 ** ^The collating function callback is invoked with a copy of the pArg
4604 ** application data pointer and with two strings in the encoding specified
4605 ** by the eTextRep argument.  The collating function must return an
4606 ** integer that is negative, zero, or positive
4607 ** if the first string is less than, equal to, or greater than the second,
4608 ** respectively.  A collating function must always return the same answer
4609 ** given the same inputs.  If two or more collating functions are registered
4610 ** to the same collation name (using different eTextRep values) then all
4611 ** must give an equivalent answer when invoked with equivalent strings.
4612 ** The collating function must obey the following properties for all
4613 ** strings A, B, and C:
4614 **
4615 ** <ol>
4616 ** <li> If A==B then B==A.
4617 ** <li> If A==B and B==C then A==C.
4618 ** <li> If A&lt;B THEN B&gt;A.
4619 ** <li> If A&lt;B and B&lt;C then A&lt;C.
4620 ** </ol>
4621 **
4622 ** If a collating function fails any of the above constraints and that
4623 ** collating function is  registered and used, then the behavior of SQLite
4624 ** is undefined.
4625 **
4626 ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
4627 ** with the addition that the xDestroy callback is invoked on pArg when
4628 ** the collating function is deleted.
4629 ** ^Collating functions are deleted when they are overridden by later
4630 ** calls to the collation creation functions or when the
4631 ** [database connection] is closed using [sqlite3_close()].
4632 **
4633 ** ^The xDestroy callback is <u>not</u> called if the
4634 ** sqlite3_create_collation_v2() function fails.  Applications that invoke
4635 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
4636 ** check the return code and dispose of the application data pointer
4637 ** themselves rather than expecting SQLite to deal with it for them.
4638 ** This is different from every other SQLite interface.  The inconsistency
4639 ** is unfortunate but cannot be changed without breaking backwards
4640 ** compatibility.
4641 **
4642 ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
4643 */
4644 SQLITE_API int sqlite3_create_collation(
4645   sqlite3*,
4646   const char *zName,
4647   int eTextRep,
4648   void *pArg,
4649   int(*xCompare)(void*,int,const void*,int,const void*)
4650 );
4651 SQLITE_API int sqlite3_create_collation_v2(
4652   sqlite3*,
4653   const char *zName,
4654   int eTextRep,
4655   void *pArg,
4656   int(*xCompare)(void*,int,const void*,int,const void*),
4657   void(*xDestroy)(void*)
4658 );
4659 SQLITE_API int sqlite3_create_collation16(
4660   sqlite3*,
4661   const void *zName,
4662   int eTextRep,
4663   void *pArg,
4664   int(*xCompare)(void*,int,const void*,int,const void*)
4665 );
4666 
4667 /*
4668 ** CAPI3REF: Collation Needed Callbacks
4669 **
4670 ** ^To avoid having to register all collation sequences before a database
4671 ** can be used, a single callback function may be registered with the
4672 ** [database connection] to be invoked whenever an undefined collation
4673 ** sequence is required.
4674 **
4675 ** ^If the function is registered using the sqlite3_collation_needed() API,
4676 ** then it is passed the names of undefined collation sequences as strings
4677 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
4678 ** the names are passed as UTF-16 in machine native byte order.
4679 ** ^A call to either function replaces the existing collation-needed callback.
4680 **
4681 ** ^(When the callback is invoked, the first argument passed is a copy
4682 ** of the second argument to sqlite3_collation_needed() or
4683 ** sqlite3_collation_needed16().  The second argument is the database
4684 ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
4685 ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
4686 ** sequence function required.  The fourth parameter is the name of the
4687 ** required collation sequence.)^
4688 **
4689 ** The callback function should register the desired collation using
4690 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
4691 ** [sqlite3_create_collation_v2()].
4692 */
4693 SQLITE_API int sqlite3_collation_needed(
4694   sqlite3*,
4695   void*,
4696   void(*)(void*,sqlite3*,int eTextRep,const char*)
4697 );
4698 SQLITE_API int sqlite3_collation_needed16(
4699   sqlite3*,
4700   void*,
4701   void(*)(void*,sqlite3*,int eTextRep,const void*)
4702 );
4703 
4704 #ifdef SQLITE_HAS_CODEC
4705 /*
4706 ** Specify the key for an encrypted database.  This routine should be
4707 ** called right after sqlite3_open().
4708 **
4709 ** The code to implement this API is not available in the public release
4710 ** of SQLite.
4711 */
4712 SQLITE_API int sqlite3_key(
4713   sqlite3 *db,                   /* Database to be rekeyed */
4714   const void *pKey, int nKey     /* The key */
4715 );
4716 SQLITE_API int sqlite3_key_v2(
4717   sqlite3 *db,                   /* Database to be rekeyed */
4718   const char *zDbName,           /* Name of the database */
4719   const void *pKey, int nKey     /* The key */
4720 );
4721 
4722 /*
4723 ** Change the key on an open database.  If the current database is not
4724 ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
4725 ** database is decrypted.
4726 **
4727 ** The code to implement this API is not available in the public release
4728 ** of SQLite.
4729 */
4730 SQLITE_API int sqlite3_rekey(
4731   sqlite3 *db,                   /* Database to be rekeyed */
4732   const void *pKey, int nKey     /* The new key */
4733 );
4734 SQLITE_API int sqlite3_rekey_v2(
4735   sqlite3 *db,                   /* Database to be rekeyed */
4736   const char *zDbName,           /* Name of the database */
4737   const void *pKey, int nKey     /* The new key */
4738 );
4739 
4740 /*
4741 ** Specify the activation key for a SEE database.  Unless
4742 ** activated, none of the SEE routines will work.
4743 */
4744 SQLITE_API void sqlite3_activate_see(
4745   const char *zPassPhrase        /* Activation phrase */
4746 );
4747 #endif
4748 
4749 #ifdef SQLITE_ENABLE_CEROD
4750 /*
4751 ** Specify the activation key for a CEROD database.  Unless
4752 ** activated, none of the CEROD routines will work.
4753 */
4754 SQLITE_API void sqlite3_activate_cerod(
4755   const char *zPassPhrase        /* Activation phrase */
4756 );
4757 #endif
4758 
4759 /*
4760 ** CAPI3REF: Suspend Execution For A Short Time
4761 **
4762 ** The sqlite3_sleep() function causes the current thread to suspend execution
4763 ** for at least a number of milliseconds specified in its parameter.
4764 **
4765 ** If the operating system does not support sleep requests with
4766 ** millisecond time resolution, then the time will be rounded up to
4767 ** the nearest second. The number of milliseconds of sleep actually
4768 ** requested from the operating system is returned.
4769 **
4770 ** ^SQLite implements this interface by calling the xSleep()
4771 ** method of the default [sqlite3_vfs] object.  If the xSleep() method
4772 ** of the default VFS is not implemented correctly, or not implemented at
4773 ** all, then the behavior of sqlite3_sleep() may deviate from the description
4774 ** in the previous paragraphs.
4775 */
4776 SQLITE_API int sqlite3_sleep(int);
4777 
4778 /*
4779 ** CAPI3REF: Name Of The Folder Holding Temporary Files
4780 **
4781 ** ^(If this global variable is made to point to a string which is
4782 ** the name of a folder (a.k.a. directory), then all temporary files
4783 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4784 ** will be placed in that directory.)^  ^If this variable
4785 ** is a NULL pointer, then SQLite performs a search for an appropriate
4786 ** temporary file directory.
4787 **
4788 ** It is not safe to read or modify this variable in more than one
4789 ** thread at a time.  It is not safe to read or modify this variable
4790 ** if a [database connection] is being used at the same time in a separate
4791 ** thread.
4792 ** It is intended that this variable be set once
4793 ** as part of process initialization and before any SQLite interface
4794 ** routines have been called and that this variable remain unchanged
4795 ** thereafter.
4796 **
4797 ** ^The [temp_store_directory pragma] may modify this variable and cause
4798 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
4799 ** the [temp_store_directory pragma] always assumes that any string
4800 ** that this variable points to is held in memory obtained from
4801 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4802 ** using [sqlite3_free].
4803 ** Hence, if this variable is modified directly, either it should be
4804 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4805 ** or else the use of the [temp_store_directory pragma] should be avoided.
4806 **
4807 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
4808 ** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
4809 ** features that require the use of temporary files may fail.  Here is an
4810 ** example of how to do this using C++ with the Windows Runtime:
4811 **
4812 ** <blockquote><pre>
4813 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
4814 ** &nbsp;     TemporaryFolder->Path->Data();
4815 ** char zPathBuf&#91;MAX_PATH + 1&#93;;
4816 ** memset(zPathBuf, 0, sizeof(zPathBuf));
4817 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
4818 ** &nbsp;     NULL, NULL);
4819 ** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
4820 ** </pre></blockquote>
4821 */
4822 SQLITE_API char *sqlite3_temp_directory;
4823 
4824 /*
4825 ** CAPI3REF: Name Of The Folder Holding Database Files
4826 **
4827 ** ^(If this global variable is made to point to a string which is
4828 ** the name of a folder (a.k.a. directory), then all database files
4829 ** specified with a relative pathname and created or accessed by
4830 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
4831 ** to be relative to that directory.)^ ^If this variable is a NULL
4832 ** pointer, then SQLite assumes that all database files specified
4833 ** with a relative pathname are relative to the current directory
4834 ** for the process.  Only the windows VFS makes use of this global
4835 ** variable; it is ignored by the unix VFS.
4836 **
4837 ** Changing the value of this variable while a database connection is
4838 ** open can result in a corrupt database.
4839 **
4840 ** It is not safe to read or modify this variable in more than one
4841 ** thread at a time.  It is not safe to read or modify this variable
4842 ** if a [database connection] is being used at the same time in a separate
4843 ** thread.
4844 ** It is intended that this variable be set once
4845 ** as part of process initialization and before any SQLite interface
4846 ** routines have been called and that this variable remain unchanged
4847 ** thereafter.
4848 **
4849 ** ^The [data_store_directory pragma] may modify this variable and cause
4850 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
4851 ** the [data_store_directory pragma] always assumes that any string
4852 ** that this variable points to is held in memory obtained from
4853 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4854 ** using [sqlite3_free].
4855 ** Hence, if this variable is modified directly, either it should be
4856 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4857 ** or else the use of the [data_store_directory pragma] should be avoided.
4858 */
4859 SQLITE_API char *sqlite3_data_directory;
4860 
4861 /*
4862 ** CAPI3REF: Test For Auto-Commit Mode
4863 ** KEYWORDS: {autocommit mode}
4864 **
4865 ** ^The sqlite3_get_autocommit() interface returns non-zero or
4866 ** zero if the given database connection is or is not in autocommit mode,
4867 ** respectively.  ^Autocommit mode is on by default.
4868 ** ^Autocommit mode is disabled by a [BEGIN] statement.
4869 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
4870 **
4871 ** If certain kinds of errors occur on a statement within a multi-statement
4872 ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
4873 ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
4874 ** transaction might be rolled back automatically.  The only way to
4875 ** find out whether SQLite automatically rolled back the transaction after
4876 ** an error is to use this function.
4877 **
4878 ** If another thread changes the autocommit status of the database
4879 ** connection while this routine is running, then the return value
4880 ** is undefined.
4881 */
4882 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
4883 
4884 /*
4885 ** CAPI3REF: Find The Database Handle Of A Prepared Statement
4886 **
4887 ** ^The sqlite3_db_handle interface returns the [database connection] handle
4888 ** to which a [prepared statement] belongs.  ^The [database connection]
4889 ** returned by sqlite3_db_handle is the same [database connection]
4890 ** that was the first argument
4891 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4892 ** create the statement in the first place.
4893 */
4894 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4895 
4896 /*
4897 ** CAPI3REF: Return The Filename For A Database Connection
4898 **
4899 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
4900 ** associated with database N of connection D.  ^The main database file
4901 ** has the name "main".  If there is no attached database N on the database
4902 ** connection D, or if database N is a temporary or in-memory database, then
4903 ** a NULL pointer is returned.
4904 **
4905 ** ^The filename returned by this function is the output of the
4906 ** xFullPathname method of the [VFS].  ^In other words, the filename
4907 ** will be an absolute pathname, even if the filename used
4908 ** to open the database originally was a URI or relative pathname.
4909 */
4910 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
4911 
4912 /*
4913 ** CAPI3REF: Determine if a database is read-only
4914 **
4915 ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
4916 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
4917 ** the name of a database on connection D.
4918 */
4919 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
4920 
4921 /*
4922 ** CAPI3REF: Find the next prepared statement
4923 **
4924 ** ^This interface returns a pointer to the next [prepared statement] after
4925 ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
4926 ** then this interface returns a pointer to the first prepared statement
4927 ** associated with the database connection pDb.  ^If no prepared statement
4928 ** satisfies the conditions of this routine, it returns NULL.
4929 **
4930 ** The [database connection] pointer D in a call to
4931 ** [sqlite3_next_stmt(D,S)] must refer to an open database
4932 ** connection and in particular must not be a NULL pointer.
4933 */
4934 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
4935 
4936 /*
4937 ** CAPI3REF: Commit And Rollback Notification Callbacks
4938 **
4939 ** ^The sqlite3_commit_hook() interface registers a callback
4940 ** function to be invoked whenever a transaction is [COMMIT | committed].
4941 ** ^Any callback set by a previous call to sqlite3_commit_hook()
4942 ** for the same database connection is overridden.
4943 ** ^The sqlite3_rollback_hook() interface registers a callback
4944 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
4945 ** ^Any callback set by a previous call to sqlite3_rollback_hook()
4946 ** for the same database connection is overridden.
4947 ** ^The pArg argument is passed through to the callback.
4948 ** ^If the callback on a commit hook function returns non-zero,
4949 ** then the commit is converted into a rollback.
4950 **
4951 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4952 ** return the P argument from the previous call of the same function
4953 ** on the same [database connection] D, or NULL for
4954 ** the first call for each function on D.
4955 **
4956 ** The commit and rollback hook callbacks are not reentrant.
4957 ** The callback implementation must not do anything that will modify
4958 ** the database connection that invoked the callback.  Any actions
4959 ** to modify the database connection must be deferred until after the
4960 ** completion of the [sqlite3_step()] call that triggered the commit
4961 ** or rollback hook in the first place.
4962 ** Note that running any other SQL statements, including SELECT statements,
4963 ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
4964 ** the database connections for the meaning of "modify" in this paragraph.
4965 **
4966 ** ^Registering a NULL function disables the callback.
4967 **
4968 ** ^When the commit hook callback routine returns zero, the [COMMIT]
4969 ** operation is allowed to continue normally.  ^If the commit hook
4970 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
4971 ** ^The rollback hook is invoked on a rollback that results from a commit
4972 ** hook returning non-zero, just as it would be with any other rollback.
4973 **
4974 ** ^For the purposes of this API, a transaction is said to have been
4975 ** rolled back if an explicit "ROLLBACK" statement is executed, or
4976 ** an error or constraint causes an implicit rollback to occur.
4977 ** ^The rollback callback is not invoked if a transaction is
4978 ** automatically rolled back because the database connection is closed.
4979 **
4980 ** See also the [sqlite3_update_hook()] interface.
4981 */
4982 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
4983 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
4984 
4985 /*
4986 ** CAPI3REF: Data Change Notification Callbacks
4987 **
4988 ** ^The sqlite3_update_hook() interface registers a callback function
4989 ** with the [database connection] identified by the first argument
4990 ** to be invoked whenever a row is updated, inserted or deleted in
4991 ** a rowid table.
4992 ** ^Any callback set by a previous call to this function
4993 ** for the same database connection is overridden.
4994 **
4995 ** ^The second argument is a pointer to the function to invoke when a
4996 ** row is updated, inserted or deleted in a rowid table.
4997 ** ^The first argument to the callback is a copy of the third argument
4998 ** to sqlite3_update_hook().
4999 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
5000 ** or [SQLITE_UPDATE], depending on the operation that caused the callback
5001 ** to be invoked.
5002 ** ^The third and fourth arguments to the callback contain pointers to the
5003 ** database and table name containing the affected row.
5004 ** ^The final callback parameter is the [rowid] of the row.
5005 ** ^In the case of an update, this is the [rowid] after the update takes place.
5006 **
5007 ** ^(The update hook is not invoked when internal system tables are
5008 ** modified (i.e. sqlite_master and sqlite_sequence).)^
5009 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
5010 **
5011 ** ^In the current implementation, the update hook
5012 ** is not invoked when duplication rows are deleted because of an
5013 ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
5014 ** invoked when rows are deleted using the [truncate optimization].
5015 ** The exceptions defined in this paragraph might change in a future
5016 ** release of SQLite.
5017 **
5018 ** The update hook implementation must not do anything that will modify
5019 ** the database connection that invoked the update hook.  Any actions
5020 ** to modify the database connection must be deferred until after the
5021 ** completion of the [sqlite3_step()] call that triggered the update hook.
5022 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
5023 ** database connections for the meaning of "modify" in this paragraph.
5024 **
5025 ** ^The sqlite3_update_hook(D,C,P) function
5026 ** returns the P argument from the previous call
5027 ** on the same [database connection] D, or NULL for
5028 ** the first call on D.
5029 **
5030 ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
5031 ** interfaces.
5032 */
5033 SQLITE_API void *sqlite3_update_hook(
5034   sqlite3*,
5035   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
5036   void*
5037 );
5038 
5039 /*
5040 ** CAPI3REF: Enable Or Disable Shared Pager Cache
5041 **
5042 ** ^(This routine enables or disables the sharing of the database cache
5043 ** and schema data structures between [database connection | connections]
5044 ** to the same database. Sharing is enabled if the argument is true
5045 ** and disabled if the argument is false.)^
5046 **
5047 ** ^Cache sharing is enabled and disabled for an entire process.
5048 ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
5049 ** sharing was enabled or disabled for each thread separately.
5050 **
5051 ** ^(The cache sharing mode set by this interface effects all subsequent
5052 ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
5053 ** Existing database connections continue use the sharing mode
5054 ** that was in effect at the time they were opened.)^
5055 **
5056 ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
5057 ** successfully.  An [error code] is returned otherwise.)^
5058 **
5059 ** ^Shared cache is disabled by default. But this might change in
5060 ** future releases of SQLite.  Applications that care about shared
5061 ** cache setting should set it explicitly.
5062 **
5063 ** This interface is threadsafe on processors where writing a
5064 ** 32-bit integer is atomic.
5065 **
5066 ** See Also:  [SQLite Shared-Cache Mode]
5067 */
5068 SQLITE_API int sqlite3_enable_shared_cache(int);
5069 
5070 /*
5071 ** CAPI3REF: Attempt To Free Heap Memory
5072 **
5073 ** ^The sqlite3_release_memory() interface attempts to free N bytes
5074 ** of heap memory by deallocating non-essential memory allocations
5075 ** held by the database library.   Memory used to cache database
5076 ** pages to improve performance is an example of non-essential memory.
5077 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
5078 ** which might be more or less than the amount requested.
5079 ** ^The sqlite3_release_memory() routine is a no-op returning zero
5080 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5081 **
5082 ** See also: [sqlite3_db_release_memory()]
5083 */
5084 SQLITE_API int sqlite3_release_memory(int);
5085 
5086 /*
5087 ** CAPI3REF: Free Memory Used By A Database Connection
5088 **
5089 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
5090 ** memory as possible from database connection D. Unlike the
5091 ** [sqlite3_release_memory()] interface, this interface is in effect even
5092 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
5093 ** omitted.
5094 **
5095 ** See also: [sqlite3_release_memory()]
5096 */
5097 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
5098 
5099 /*
5100 ** CAPI3REF: Impose A Limit On Heap Size
5101 **
5102 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
5103 ** soft limit on the amount of heap memory that may be allocated by SQLite.
5104 ** ^SQLite strives to keep heap memory utilization below the soft heap
5105 ** limit by reducing the number of pages held in the page cache
5106 ** as heap memory usages approaches the limit.
5107 ** ^The soft heap limit is "soft" because even though SQLite strives to stay
5108 ** below the limit, it will exceed the limit rather than generate
5109 ** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
5110 ** is advisory only.
5111 **
5112 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
5113 ** the soft heap limit prior to the call, or negative in the case of an
5114 ** error.  ^If the argument N is negative
5115 ** then no change is made to the soft heap limit.  Hence, the current
5116 ** size of the soft heap limit can be determined by invoking
5117 ** sqlite3_soft_heap_limit64() with a negative argument.
5118 **
5119 ** ^If the argument N is zero then the soft heap limit is disabled.
5120 **
5121 ** ^(The soft heap limit is not enforced in the current implementation
5122 ** if one or more of following conditions are true:
5123 **
5124 ** <ul>
5125 ** <li> The soft heap limit is set to zero.
5126 ** <li> Memory accounting is disabled using a combination of the
5127 **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
5128 **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
5129 ** <li> An alternative page cache implementation is specified using
5130 **      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
5131 ** <li> The page cache allocates from its own memory pool supplied
5132 **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
5133 **      from the heap.
5134 ** </ul>)^
5135 **
5136 ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
5137 ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
5138 ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
5139 ** the soft heap limit is enforced on every memory allocation.  Without
5140 ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
5141 ** when memory is allocated by the page cache.  Testing suggests that because
5142 ** the page cache is the predominate memory user in SQLite, most
5143 ** applications will achieve adequate soft heap limit enforcement without
5144 ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5145 **
5146 ** The circumstances under which SQLite will enforce the soft heap limit may
5147 ** changes in future releases of SQLite.
5148 */
5149 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
5150 
5151 /*
5152 ** CAPI3REF: Deprecated Soft Heap Limit Interface
5153 ** DEPRECATED
5154 **
5155 ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
5156 ** interface.  This routine is provided for historical compatibility
5157 ** only.  All new applications should use the
5158 ** [sqlite3_soft_heap_limit64()] interface rather than this one.
5159 */
5160 SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
5161 
5162 
5163 /*
5164 ** CAPI3REF: Extract Metadata About A Column Of A Table
5165 **
5166 ** ^This routine returns metadata about a specific column of a specific
5167 ** database table accessible using the [database connection] handle
5168 ** passed as the first function argument.
5169 **
5170 ** ^The column is identified by the second, third and fourth parameters to
5171 ** this function. ^The second parameter is either the name of the database
5172 ** (i.e. "main", "temp", or an attached database) containing the specified
5173 ** table or NULL. ^If it is NULL, then all attached databases are searched
5174 ** for the table using the same algorithm used by the database engine to
5175 ** resolve unqualified table references.
5176 **
5177 ** ^The third and fourth parameters to this function are the table and column
5178 ** name of the desired column, respectively. Neither of these parameters
5179 ** may be NULL.
5180 **
5181 ** ^Metadata is returned by writing to the memory locations passed as the 5th
5182 ** and subsequent parameters to this function. ^Any of these arguments may be
5183 ** NULL, in which case the corresponding element of metadata is omitted.
5184 **
5185 ** ^(<blockquote>
5186 ** <table border="1">
5187 ** <tr><th> Parameter <th> Output<br>Type <th>  Description
5188 **
5189 ** <tr><td> 5th <td> const char* <td> Data type
5190 ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
5191 ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
5192 ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
5193 ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
5194 ** </table>
5195 ** </blockquote>)^
5196 **
5197 ** ^The memory pointed to by the character pointers returned for the
5198 ** declaration type and collation sequence is valid only until the next
5199 ** call to any SQLite API function.
5200 **
5201 ** ^If the specified table is actually a view, an [error code] is returned.
5202 **
5203 ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
5204 ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
5205 ** parameters are set for the explicitly declared column. ^(If there is no
5206 ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
5207 ** parameters are set as follows:
5208 **
5209 ** <pre>
5210 **     data type: "INTEGER"
5211 **     collation sequence: "BINARY"
5212 **     not null: 0
5213 **     primary key: 1
5214 **     auto increment: 0
5215 ** </pre>)^
5216 **
5217 ** ^(This function may load one or more schemas from database files. If an
5218 ** error occurs during this process, or if the requested table or column
5219 ** cannot be found, an [error code] is returned and an error message left
5220 ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
5221 **
5222 ** ^This API is only available if the library was compiled with the
5223 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
5224 */
5225 SQLITE_API int sqlite3_table_column_metadata(
5226   sqlite3 *db,                /* Connection handle */
5227   const char *zDbName,        /* Database name or NULL */
5228   const char *zTableName,     /* Table name */
5229   const char *zColumnName,    /* Column name */
5230   char const **pzDataType,    /* OUTPUT: Declared data type */
5231   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
5232   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
5233   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
5234   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
5235 );
5236 
5237 /*
5238 ** CAPI3REF: Load An Extension
5239 **
5240 ** ^This interface loads an SQLite extension library from the named file.
5241 **
5242 ** ^The sqlite3_load_extension() interface attempts to load an
5243 ** [SQLite extension] library contained in the file zFile.  If
5244 ** the file cannot be loaded directly, attempts are made to load
5245 ** with various operating-system specific extensions added.
5246 ** So for example, if "samplelib" cannot be loaded, then names like
5247 ** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
5248 ** be tried also.
5249 **
5250 ** ^The entry point is zProc.
5251 ** ^(zProc may be 0, in which case SQLite will try to come up with an
5252 ** entry point name on its own.  It first tries "sqlite3_extension_init".
5253 ** If that does not work, it constructs a name "sqlite3_X_init" where the
5254 ** X is consists of the lower-case equivalent of all ASCII alphabetic
5255 ** characters in the filename from the last "/" to the first following
5256 ** "." and omitting any initial "lib".)^
5257 ** ^The sqlite3_load_extension() interface returns
5258 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5259 ** ^If an error occurs and pzErrMsg is not 0, then the
5260 ** [sqlite3_load_extension()] interface shall attempt to
5261 ** fill *pzErrMsg with error message text stored in memory
5262 ** obtained from [sqlite3_malloc()]. The calling function
5263 ** should free this memory by calling [sqlite3_free()].
5264 **
5265 ** ^Extension loading must be enabled using
5266 ** [sqlite3_enable_load_extension()] prior to calling this API,
5267 ** otherwise an error will be returned.
5268 **
5269 ** See also the [load_extension() SQL function].
5270 */
5271 SQLITE_API int sqlite3_load_extension(
5272   sqlite3 *db,          /* Load the extension into this database connection */
5273   const char *zFile,    /* Name of the shared library containing extension */
5274   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
5275   char **pzErrMsg       /* Put error message here if not 0 */
5276 );
5277 
5278 /*
5279 ** CAPI3REF: Enable Or Disable Extension Loading
5280 **
5281 ** ^So as not to open security holes in older applications that are
5282 ** unprepared to deal with [extension loading], and as a means of disabling
5283 ** [extension loading] while evaluating user-entered SQL, the following API
5284 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5285 **
5286 ** ^Extension loading is off by default.
5287 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5288 ** to turn extension loading on and call it with onoff==0 to turn
5289 ** it back off again.
5290 */
5291 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
5292 
5293 /*
5294 ** CAPI3REF: Automatically Load Statically Linked Extensions
5295 **
5296 ** ^This interface causes the xEntryPoint() function to be invoked for
5297 ** each new [database connection] that is created.  The idea here is that
5298 ** xEntryPoint() is the entry point for a statically linked [SQLite extension]
5299 ** that is to be automatically loaded into all new database connections.
5300 **
5301 ** ^(Even though the function prototype shows that xEntryPoint() takes
5302 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
5303 ** arguments and expects and integer result as if the signature of the
5304 ** entry point where as follows:
5305 **
5306 ** <blockquote><pre>
5307 ** &nbsp;  int xEntryPoint(
5308 ** &nbsp;    sqlite3 *db,
5309 ** &nbsp;    const char **pzErrMsg,
5310 ** &nbsp;    const struct sqlite3_api_routines *pThunk
5311 ** &nbsp;  );
5312 ** </pre></blockquote>)^
5313 **
5314 ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
5315 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
5316 ** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
5317 ** is NULL before calling the xEntryPoint().  ^SQLite will invoke
5318 ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
5319 ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
5320 ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
5321 **
5322 ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
5323 ** on the list of automatic extensions is a harmless no-op. ^No entry point
5324 ** will be called more than once for each database connection that is opened.
5325 **
5326 ** See also: [sqlite3_reset_auto_extension()]
5327 ** and [sqlite3_cancel_auto_extension()]
5328 */
5329 SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
5330 
5331 /*
5332 ** CAPI3REF: Cancel Automatic Extension Loading
5333 **
5334 ** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
5335 ** initialization routine X that was registered using a prior call to
5336 ** [sqlite3_auto_extension(X)].  ^The [sqlite3_cancel_auto_extension(X)]
5337 ** routine returns 1 if initialization routine X was successfully
5338 ** unregistered and it returns 0 if X was not on the list of initialization
5339 ** routines.
5340 */
5341 SQLITE_API int sqlite3_cancel_auto_extension(void (*xEntryPoint)(void));
5342 
5343 /*
5344 ** CAPI3REF: Reset Automatic Extension Loading
5345 **
5346 ** ^This interface disables all automatic extensions previously
5347 ** registered using [sqlite3_auto_extension()].
5348 */
5349 SQLITE_API void sqlite3_reset_auto_extension(void);
5350 
5351 /*
5352 ** The interface to the virtual-table mechanism is currently considered
5353 ** to be experimental.  The interface might change in incompatible ways.
5354 ** If this is a problem for you, do not use the interface at this time.
5355 **
5356 ** When the virtual-table mechanism stabilizes, we will declare the
5357 ** interface fixed, support it indefinitely, and remove this comment.
5358 */
5359 
5360 /*
5361 ** Structures used by the virtual table interface
5362 */
5363 typedef struct sqlite3_vtab sqlite3_vtab;
5364 typedef struct sqlite3_index_info sqlite3_index_info;
5365 typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
5366 typedef struct sqlite3_module sqlite3_module;
5367 
5368 /*
5369 ** CAPI3REF: Virtual Table Object
5370 ** KEYWORDS: sqlite3_module {virtual table module}
5371 **
5372 ** This structure, sometimes called a "virtual table module",
5373 ** defines the implementation of a [virtual tables].
5374 ** This structure consists mostly of methods for the module.
5375 **
5376 ** ^A virtual table module is created by filling in a persistent
5377 ** instance of this structure and passing a pointer to that instance
5378 ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
5379 ** ^The registration remains valid until it is replaced by a different
5380 ** module or until the [database connection] closes.  The content
5381 ** of this structure must not change while it is registered with
5382 ** any database connection.
5383 */
5384 struct sqlite3_module {
5385   int iVersion;
5386   int (*xCreate)(sqlite3*, void *pAux,
5387                int argc, const char *const*argv,
5388                sqlite3_vtab **ppVTab, char**);
5389   int (*xConnect)(sqlite3*, void *pAux,
5390                int argc, const char *const*argv,
5391                sqlite3_vtab **ppVTab, char**);
5392   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
5393   int (*xDisconnect)(sqlite3_vtab *pVTab);
5394   int (*xDestroy)(sqlite3_vtab *pVTab);
5395   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
5396   int (*xClose)(sqlite3_vtab_cursor*);
5397   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
5398                 int argc, sqlite3_value **argv);
5399   int (*xNext)(sqlite3_vtab_cursor*);
5400   int (*xEof)(sqlite3_vtab_cursor*);
5401   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
5402   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
5403   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
5404   int (*xBegin)(sqlite3_vtab *pVTab);
5405   int (*xSync)(sqlite3_vtab *pVTab);
5406   int (*xCommit)(sqlite3_vtab *pVTab);
5407   int (*xRollback)(sqlite3_vtab *pVTab);
5408   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
5409                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
5410                        void **ppArg);
5411   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
5412   /* The methods above are in version 1 of the sqlite_module object. Those
5413   ** below are for version 2 and greater. */
5414   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
5415   int (*xRelease)(sqlite3_vtab *pVTab, int);
5416   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
5417 };
5418 
5419 /*
5420 ** CAPI3REF: Virtual Table Indexing Information
5421 ** KEYWORDS: sqlite3_index_info
5422 **
5423 ** The sqlite3_index_info structure and its substructures is used as part
5424 ** of the [virtual table] interface to
5425 ** pass information into and receive the reply from the [xBestIndex]
5426 ** method of a [virtual table module].  The fields under **Inputs** are the
5427 ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
5428 ** results into the **Outputs** fields.
5429 **
5430 ** ^(The aConstraint[] array records WHERE clause constraints of the form:
5431 **
5432 ** <blockquote>column OP expr</blockquote>
5433 **
5434 ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
5435 ** stored in aConstraint[].op using one of the
5436 ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
5437 ** ^(The index of the column is stored in
5438 ** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
5439 ** expr on the right-hand side can be evaluated (and thus the constraint
5440 ** is usable) and false if it cannot.)^
5441 **
5442 ** ^The optimizer automatically inverts terms of the form "expr OP column"
5443 ** and makes other simplifications to the WHERE clause in an attempt to
5444 ** get as many WHERE clause terms into the form shown above as possible.
5445 ** ^The aConstraint[] array only reports WHERE clause terms that are
5446 ** relevant to the particular virtual table being queried.
5447 **
5448 ** ^Information about the ORDER BY clause is stored in aOrderBy[].
5449 ** ^Each term of aOrderBy records a column of the ORDER BY clause.
5450 **
5451 ** The [xBestIndex] method must fill aConstraintUsage[] with information
5452 ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
5453 ** the right-hand side of the corresponding aConstraint[] is evaluated
5454 ** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
5455 ** is true, then the constraint is assumed to be fully handled by the
5456 ** virtual table and is not checked again by SQLite.)^
5457 **
5458 ** ^The idxNum and idxPtr values are recorded and passed into the
5459 ** [xFilter] method.
5460 ** ^[sqlite3_free()] is used to free idxPtr if and only if
5461 ** needToFreeIdxPtr is true.
5462 **
5463 ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
5464 ** the correct order to satisfy the ORDER BY clause so that no separate
5465 ** sorting step is required.
5466 **
5467 ** ^The estimatedCost value is an estimate of the cost of a particular
5468 ** strategy. A cost of N indicates that the cost of the strategy is similar
5469 ** to a linear scan of an SQLite table with N rows. A cost of log(N)
5470 ** indicates that the expense of the operation is similar to that of a
5471 ** binary search on a unique indexed field of an SQLite table with N rows.
5472 **
5473 ** ^The estimatedRows value is an estimate of the number of rows that
5474 ** will be returned by the strategy.
5475 **
5476 ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
5477 ** structure for SQLite version 3.8.2. If a virtual table extension is
5478 ** used with an SQLite version earlier than 3.8.2, the results of attempting
5479 ** to read or write the estimatedRows field are undefined (but are likely
5480 ** to included crashing the application). The estimatedRows field should
5481 ** therefore only be used if [sqlite3_libversion_number()] returns a
5482 ** value greater than or equal to 3008002.
5483 */
5484 struct sqlite3_index_info {
5485   /* Inputs */
5486   int nConstraint;           /* Number of entries in aConstraint */
5487   struct sqlite3_index_constraint {
5488      int iColumn;              /* Column on left-hand side of constraint */
5489      unsigned char op;         /* Constraint operator */
5490      unsigned char usable;     /* True if this constraint is usable */
5491      int iTermOffset;          /* Used internally - xBestIndex should ignore */
5492   } *aConstraint;            /* Table of WHERE clause constraints */
5493   int nOrderBy;              /* Number of terms in the ORDER BY clause */
5494   struct sqlite3_index_orderby {
5495      int iColumn;              /* Column number */
5496      unsigned char desc;       /* True for DESC.  False for ASC. */
5497   } *aOrderBy;               /* The ORDER BY clause */
5498   /* Outputs */
5499   struct sqlite3_index_constraint_usage {
5500     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
5501     unsigned char omit;      /* Do not code a test for this constraint */
5502   } *aConstraintUsage;
5503   int idxNum;                /* Number used to identify the index */
5504   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
5505   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
5506   int orderByConsumed;       /* True if output is already ordered */
5507   double estimatedCost;           /* Estimated cost of using this index */
5508   /* Fields below are only available in SQLite 3.8.2 and later */
5509   sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
5510 };
5511 
5512 /*
5513 ** CAPI3REF: Virtual Table Constraint Operator Codes
5514 **
5515 ** These macros defined the allowed values for the
5516 ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
5517 ** an operator that is part of a constraint term in the wHERE clause of
5518 ** a query that uses a [virtual table].
5519 */
5520 #define SQLITE_INDEX_CONSTRAINT_EQ    2
5521 #define SQLITE_INDEX_CONSTRAINT_GT    4
5522 #define SQLITE_INDEX_CONSTRAINT_LE    8
5523 #define SQLITE_INDEX_CONSTRAINT_LT    16
5524 #define SQLITE_INDEX_CONSTRAINT_GE    32
5525 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
5526 
5527 /*
5528 ** CAPI3REF: Register A Virtual Table Implementation
5529 **
5530 ** ^These routines are used to register a new [virtual table module] name.
5531 ** ^Module names must be registered before
5532 ** creating a new [virtual table] using the module and before using a
5533 ** preexisting [virtual table] for the module.
5534 **
5535 ** ^The module name is registered on the [database connection] specified
5536 ** by the first parameter.  ^The name of the module is given by the
5537 ** second parameter.  ^The third parameter is a pointer to
5538 ** the implementation of the [virtual table module].   ^The fourth
5539 ** parameter is an arbitrary client data pointer that is passed through
5540 ** into the [xCreate] and [xConnect] methods of the virtual table module
5541 ** when a new virtual table is be being created or reinitialized.
5542 **
5543 ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
5544 ** is a pointer to a destructor for the pClientData.  ^SQLite will
5545 ** invoke the destructor function (if it is not NULL) when SQLite
5546 ** no longer needs the pClientData pointer.  ^The destructor will also
5547 ** be invoked if the call to sqlite3_create_module_v2() fails.
5548 ** ^The sqlite3_create_module()
5549 ** interface is equivalent to sqlite3_create_module_v2() with a NULL
5550 ** destructor.
5551 */
5552 SQLITE_API int sqlite3_create_module(
5553   sqlite3 *db,               /* SQLite connection to register module with */
5554   const char *zName,         /* Name of the module */
5555   const sqlite3_module *p,   /* Methods for the module */
5556   void *pClientData          /* Client data for xCreate/xConnect */
5557 );
5558 SQLITE_API int sqlite3_create_module_v2(
5559   sqlite3 *db,               /* SQLite connection to register module with */
5560   const char *zName,         /* Name of the module */
5561   const sqlite3_module *p,   /* Methods for the module */
5562   void *pClientData,         /* Client data for xCreate/xConnect */
5563   void(*xDestroy)(void*)     /* Module destructor function */
5564 );
5565 
5566 /*
5567 ** CAPI3REF: Virtual Table Instance Object
5568 ** KEYWORDS: sqlite3_vtab
5569 **
5570 ** Every [virtual table module] implementation uses a subclass
5571 ** of this object to describe a particular instance
5572 ** of the [virtual table].  Each subclass will
5573 ** be tailored to the specific needs of the module implementation.
5574 ** The purpose of this superclass is to define certain fields that are
5575 ** common to all module implementations.
5576 **
5577 ** ^Virtual tables methods can set an error message by assigning a
5578 ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
5579 ** take care that any prior string is freed by a call to [sqlite3_free()]
5580 ** prior to assigning a new string to zErrMsg.  ^After the error message
5581 ** is delivered up to the client application, the string will be automatically
5582 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5583 */
5584 struct sqlite3_vtab {
5585   const sqlite3_module *pModule;  /* The module for this virtual table */
5586   int nRef;                       /* NO LONGER USED */
5587   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
5588   /* Virtual table implementations will typically add additional fields */
5589 };
5590 
5591 /*
5592 ** CAPI3REF: Virtual Table Cursor Object
5593 ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
5594 **
5595 ** Every [virtual table module] implementation uses a subclass of the
5596 ** following structure to describe cursors that point into the
5597 ** [virtual table] and are used
5598 ** to loop through the virtual table.  Cursors are created using the
5599 ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
5600 ** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
5601 ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
5602 ** of the module.  Each module implementation will define
5603 ** the content of a cursor structure to suit its own needs.
5604 **
5605 ** This superclass exists in order to define fields of the cursor that
5606 ** are common to all implementations.
5607 */
5608 struct sqlite3_vtab_cursor {
5609   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
5610   /* Virtual table implementations will typically add additional fields */
5611 };
5612 
5613 /*
5614 ** CAPI3REF: Declare The Schema Of A Virtual Table
5615 **
5616 ** ^The [xCreate] and [xConnect] methods of a
5617 ** [virtual table module] call this interface
5618 ** to declare the format (the names and datatypes of the columns) of
5619 ** the virtual tables they implement.
5620 */
5621 SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
5622 
5623 /*
5624 ** CAPI3REF: Overload A Function For A Virtual Table
5625 **
5626 ** ^(Virtual tables can provide alternative implementations of functions
5627 ** using the [xFindFunction] method of the [virtual table module].
5628 ** But global versions of those functions
5629 ** must exist in order to be overloaded.)^
5630 **
5631 ** ^(This API makes sure a global version of a function with a particular
5632 ** name and number of parameters exists.  If no such function exists
5633 ** before this API is called, a new function is created.)^  ^The implementation
5634 ** of the new function always causes an exception to be thrown.  So
5635 ** the new function is not good for anything by itself.  Its only
5636 ** purpose is to be a placeholder function that can be overloaded
5637 ** by a [virtual table].
5638 */
5639 SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
5640 
5641 /*
5642 ** The interface to the virtual-table mechanism defined above (back up
5643 ** to a comment remarkably similar to this one) is currently considered
5644 ** to be experimental.  The interface might change in incompatible ways.
5645 ** If this is a problem for you, do not use the interface at this time.
5646 **
5647 ** When the virtual-table mechanism stabilizes, we will declare the
5648 ** interface fixed, support it indefinitely, and remove this comment.
5649 */
5650 
5651 /*
5652 ** CAPI3REF: A Handle To An Open BLOB
5653 ** KEYWORDS: {BLOB handle} {BLOB handles}
5654 **
5655 ** An instance of this object represents an open BLOB on which
5656 ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
5657 ** ^Objects of this type are created by [sqlite3_blob_open()]
5658 ** and destroyed by [sqlite3_blob_close()].
5659 ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
5660 ** can be used to read or write small subsections of the BLOB.
5661 ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
5662 */
5663 typedef struct sqlite3_blob sqlite3_blob;
5664 
5665 /*
5666 ** CAPI3REF: Open A BLOB For Incremental I/O
5667 **
5668 ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
5669 ** in row iRow, column zColumn, table zTable in database zDb;
5670 ** in other words, the same BLOB that would be selected by:
5671 **
5672 ** <pre>
5673 **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5674 ** </pre>)^
5675 **
5676 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
5677 ** and write access. ^If it is zero, the BLOB is opened for read access.
5678 ** ^It is not possible to open a column that is part of an index or primary
5679 ** key for writing. ^If [foreign key constraints] are enabled, it is
5680 ** not possible to open a column that is part of a [child key] for writing.
5681 **
5682 ** ^Note that the database name is not the filename that contains
5683 ** the database but rather the symbolic name of the database that
5684 ** appears after the AS keyword when the database is connected using [ATTACH].
5685 ** ^For the main database file, the database name is "main".
5686 ** ^For TEMP tables, the database name is "temp".
5687 **
5688 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5689 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5690 ** to be a null pointer.)^
5691 ** ^This function sets the [database connection] error code and message
5692 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5693 ** functions. ^Note that the *ppBlob variable is always initialized in a
5694 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5695 ** regardless of the success or failure of this routine.
5696 **
5697 ** ^(If the row that a BLOB handle points to is modified by an
5698 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5699 ** then the BLOB handle is marked as "expired".
5700 ** This is true if any column of the row is changed, even a column
5701 ** other than the one the BLOB handle is open on.)^
5702 ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
5703 ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
5704 ** ^(Changes written into a BLOB prior to the BLOB expiring are not
5705 ** rolled back by the expiration of the BLOB.  Such changes will eventually
5706 ** commit if the transaction continues to completion.)^
5707 **
5708 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5709 ** the opened blob.  ^The size of a blob may not be changed by this
5710 ** interface.  Use the [UPDATE] SQL command to change the size of a
5711 ** blob.
5712 **
5713 ** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
5714 ** table.  Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
5715 **
5716 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5717 ** and the built-in [zeroblob] SQL function can be used, if desired,
5718 ** to create an empty, zero-filled blob in which to read or write using
5719 ** this interface.
5720 **
5721 ** To avoid a resource leak, every open [BLOB handle] should eventually
5722 ** be released by a call to [sqlite3_blob_close()].
5723 */
5724 SQLITE_API int sqlite3_blob_open(
5725   sqlite3*,
5726   const char *zDb,
5727   const char *zTable,
5728   const char *zColumn,
5729   sqlite3_int64 iRow,
5730   int flags,
5731   sqlite3_blob **ppBlob
5732 );
5733 
5734 /*
5735 ** CAPI3REF: Move a BLOB Handle to a New Row
5736 **
5737 ** ^This function is used to move an existing blob handle so that it points
5738 ** to a different row of the same database table. ^The new row is identified
5739 ** by the rowid value passed as the second argument. Only the row can be
5740 ** changed. ^The database, table and column on which the blob handle is open
5741 ** remain the same. Moving an existing blob handle to a new row can be
5742 ** faster than closing the existing handle and opening a new one.
5743 **
5744 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
5745 ** it must exist and there must be either a blob or text value stored in
5746 ** the nominated column.)^ ^If the new row is not present in the table, or if
5747 ** it does not contain a blob or text value, or if another error occurs, an
5748 ** SQLite error code is returned and the blob handle is considered aborted.
5749 ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
5750 ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
5751 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
5752 ** always returns zero.
5753 **
5754 ** ^This function sets the database handle error code and message.
5755 */
5756 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5757 
5758 /*
5759 ** CAPI3REF: Close A BLOB Handle
5760 **
5761 ** ^Closes an open [BLOB handle].
5762 **
5763 ** ^Closing a BLOB shall cause the current transaction to commit
5764 ** if there are no other BLOBs, no pending prepared statements, and the
5765 ** database connection is in [autocommit mode].
5766 ** ^If any writes were made to the BLOB, they might be held in cache
5767 ** until the close operation if they will fit.
5768 **
5769 ** ^(Closing the BLOB often forces the changes
5770 ** out to disk and so if any I/O errors occur, they will likely occur
5771 ** at the time when the BLOB is closed.  Any errors that occur during
5772 ** closing are reported as a non-zero return value.)^
5773 **
5774 ** ^(The BLOB is closed unconditionally.  Even if this routine returns
5775 ** an error code, the BLOB is still closed.)^
5776 **
5777 ** ^Calling this routine with a null pointer (such as would be returned
5778 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5779 */
5780 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5781 
5782 /*
5783 ** CAPI3REF: Return The Size Of An Open BLOB
5784 **
5785 ** ^Returns the size in bytes of the BLOB accessible via the
5786 ** successfully opened [BLOB handle] in its only argument.  ^The
5787 ** incremental blob I/O routines can only read or overwriting existing
5788 ** blob content; they cannot change the size of a blob.
5789 **
5790 ** This routine only works on a [BLOB handle] which has been created
5791 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5792 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5793 ** to this routine results in undefined and probably undesirable behavior.
5794 */
5795 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
5796 
5797 /*
5798 ** CAPI3REF: Read Data From A BLOB Incrementally
5799 **
5800 ** ^(This function is used to read data from an open [BLOB handle] into a
5801 ** caller-supplied buffer. N bytes of data are copied into buffer Z
5802 ** from the open BLOB, starting at offset iOffset.)^
5803 **
5804 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5805 ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
5806 ** less than zero, [SQLITE_ERROR] is returned and no data is read.
5807 ** ^The size of the blob (and hence the maximum value of N+iOffset)
5808 ** can be determined using the [sqlite3_blob_bytes()] interface.
5809 **
5810 ** ^An attempt to read from an expired [BLOB handle] fails with an
5811 ** error code of [SQLITE_ABORT].
5812 **
5813 ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
5814 ** Otherwise, an [error code] or an [extended error code] is returned.)^
5815 **
5816 ** This routine only works on a [BLOB handle] which has been created
5817 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5818 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5819 ** to this routine results in undefined and probably undesirable behavior.
5820 **
5821 ** See also: [sqlite3_blob_write()].
5822 */
5823 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5824 
5825 /*
5826 ** CAPI3REF: Write Data Into A BLOB Incrementally
5827 **
5828 ** ^This function is used to write data into an open [BLOB handle] from a
5829 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5830 ** into the open BLOB, starting at offset iOffset.
5831 **
5832 ** ^If the [BLOB handle] passed as the first argument was not opened for
5833 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5834 ** this function returns [SQLITE_READONLY].
5835 **
5836 ** ^This function may only modify the contents of the BLOB; it is
5837 ** not possible to increase the size of a BLOB using this API.
5838 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5839 ** [SQLITE_ERROR] is returned and no data is written.  ^If N is
5840 ** less than zero [SQLITE_ERROR] is returned and no data is written.
5841 ** The size of the BLOB (and hence the maximum value of N+iOffset)
5842 ** can be determined using the [sqlite3_blob_bytes()] interface.
5843 **
5844 ** ^An attempt to write to an expired [BLOB handle] fails with an
5845 ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
5846 ** before the [BLOB handle] expired are not rolled back by the
5847 ** expiration of the handle, though of course those changes might
5848 ** have been overwritten by the statement that expired the BLOB handle
5849 ** or by other independent statements.
5850 **
5851 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5852 ** Otherwise, an  [error code] or an [extended error code] is returned.)^
5853 **
5854 ** This routine only works on a [BLOB handle] which has been created
5855 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5856 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5857 ** to this routine results in undefined and probably undesirable behavior.
5858 **
5859 ** See also: [sqlite3_blob_read()].
5860 */
5861 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
5862 
5863 /*
5864 ** CAPI3REF: Virtual File System Objects
5865 **
5866 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
5867 ** that SQLite uses to interact
5868 ** with the underlying operating system.  Most SQLite builds come with a
5869 ** single default VFS that is appropriate for the host computer.
5870 ** New VFSes can be registered and existing VFSes can be unregistered.
5871 ** The following interfaces are provided.
5872 **
5873 ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
5874 ** ^Names are case sensitive.
5875 ** ^Names are zero-terminated UTF-8 strings.
5876 ** ^If there is no match, a NULL pointer is returned.
5877 ** ^If zVfsName is NULL then the default VFS is returned.
5878 **
5879 ** ^New VFSes are registered with sqlite3_vfs_register().
5880 ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
5881 ** ^The same VFS can be registered multiple times without injury.
5882 ** ^To make an existing VFS into the default VFS, register it again
5883 ** with the makeDflt flag set.  If two different VFSes with the
5884 ** same name are registered, the behavior is undefined.  If a
5885 ** VFS is registered with a name that is NULL or an empty string,
5886 ** then the behavior is undefined.
5887 **
5888 ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
5889 ** ^(If the default VFS is unregistered, another VFS is chosen as
5890 ** the default.  The choice for the new VFS is arbitrary.)^
5891 */
5892 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
5893 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
5894 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
5895 
5896 /*
5897 ** CAPI3REF: Mutexes
5898 **
5899 ** The SQLite core uses these routines for thread
5900 ** synchronization. Though they are intended for internal
5901 ** use by SQLite, code that links against SQLite is
5902 ** permitted to use any of these routines.
5903 **
5904 ** The SQLite source code contains multiple implementations
5905 ** of these mutex routines.  An appropriate implementation
5906 ** is selected automatically at compile-time.  ^(The following
5907 ** implementations are available in the SQLite core:
5908 **
5909 ** <ul>
5910 ** <li>   SQLITE_MUTEX_PTHREADS
5911 ** <li>   SQLITE_MUTEX_W32
5912 ** <li>   SQLITE_MUTEX_NOOP
5913 ** </ul>)^
5914 **
5915 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5916 ** that does no real locking and is appropriate for use in
5917 ** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
5918 ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
5919 ** and Windows.
5920 **
5921 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5922 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5923 ** implementation is included with the library. In this case the
5924 ** application must supply a custom mutex implementation using the
5925 ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
5926 ** before calling sqlite3_initialize() or any other public sqlite3_
5927 ** function that calls sqlite3_initialize().)^
5928 **
5929 ** ^The sqlite3_mutex_alloc() routine allocates a new
5930 ** mutex and returns a pointer to it. ^If it returns NULL
5931 ** that means that a mutex could not be allocated.  ^SQLite
5932 ** will unwind its stack and return an error.  ^(The argument
5933 ** to sqlite3_mutex_alloc() is one of these integer constants:
5934 **
5935 ** <ul>
5936 ** <li>  SQLITE_MUTEX_FAST
5937 ** <li>  SQLITE_MUTEX_RECURSIVE
5938 ** <li>  SQLITE_MUTEX_STATIC_MASTER
5939 ** <li>  SQLITE_MUTEX_STATIC_MEM
5940 ** <li>  SQLITE_MUTEX_STATIC_MEM2
5941 ** <li>  SQLITE_MUTEX_STATIC_PRNG
5942 ** <li>  SQLITE_MUTEX_STATIC_LRU
5943 ** <li>  SQLITE_MUTEX_STATIC_LRU2
5944 ** </ul>)^
5945 **
5946 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5947 ** cause sqlite3_mutex_alloc() to create
5948 ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
5949 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
5950 ** The mutex implementation does not need to make a distinction
5951 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
5952 ** not want to.  ^SQLite will only request a recursive mutex in
5953 ** cases where it really needs one.  ^If a faster non-recursive mutex
5954 ** implementation is available on the host platform, the mutex subsystem
5955 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
5956 **
5957 ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
5958 ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
5959 ** a pointer to a static preexisting mutex.  ^Six static mutexes are
5960 ** used by the current version of SQLite.  Future versions of SQLite
5961 ** may add additional static mutexes.  Static mutexes are for internal
5962 ** use by SQLite only.  Applications that use SQLite mutexes should
5963 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
5964 ** SQLITE_MUTEX_RECURSIVE.
5965 **
5966 ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
5967 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
5968 ** returns a different mutex on every call.  ^But for the static
5969 ** mutex types, the same mutex is returned on every call that has
5970 ** the same type number.
5971 **
5972 ** ^The sqlite3_mutex_free() routine deallocates a previously
5973 ** allocated dynamic mutex.  ^SQLite is careful to deallocate every
5974 ** dynamic mutex that it allocates.  The dynamic mutexes must not be in
5975 ** use when they are deallocated.  Attempting to deallocate a static
5976 ** mutex results in undefined behavior.  ^SQLite never deallocates
5977 ** a static mutex.
5978 **
5979 ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
5980 ** to enter a mutex.  ^If another thread is already within the mutex,
5981 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
5982 ** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
5983 ** upon successful entry.  ^(Mutexes created using
5984 ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
5985 ** In such cases the,
5986 ** mutex must be exited an equal number of times before another thread
5987 ** can enter.)^  ^(If the same thread tries to enter any other
5988 ** kind of mutex more than once, the behavior is undefined.
5989 ** SQLite will never exhibit
5990 ** such behavior in its own use of mutexes.)^
5991 **
5992 ** ^(Some systems (for example, Windows 95) do not support the operation
5993 ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
5994 ** will always return SQLITE_BUSY.  The SQLite core only ever uses
5995 ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
5996 **
5997 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
5998 ** previously entered by the same thread.   ^(The behavior
5999 ** is undefined if the mutex is not currently entered by the
6000 ** calling thread or is not currently allocated.  SQLite will
6001 ** never do either.)^
6002 **
6003 ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
6004 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
6005 ** behave as no-ops.
6006 **
6007 ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
6008 */
6009 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
6010 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
6011 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
6012 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
6013 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
6014 
6015 /*
6016 ** CAPI3REF: Mutex Methods Object
6017 **
6018 ** An instance of this structure defines the low-level routines
6019 ** used to allocate and use mutexes.
6020 **
6021 ** Usually, the default mutex implementations provided by SQLite are
6022 ** sufficient, however the user has the option of substituting a custom
6023 ** implementation for specialized deployments or systems for which SQLite
6024 ** does not provide a suitable implementation. In this case, the user
6025 ** creates and populates an instance of this structure to pass
6026 ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
6027 ** Additionally, an instance of this structure can be used as an
6028 ** output variable when querying the system for the current mutex
6029 ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
6030 **
6031 ** ^The xMutexInit method defined by this structure is invoked as
6032 ** part of system initialization by the sqlite3_initialize() function.
6033 ** ^The xMutexInit routine is called by SQLite exactly once for each
6034 ** effective call to [sqlite3_initialize()].
6035 **
6036 ** ^The xMutexEnd method defined by this structure is invoked as
6037 ** part of system shutdown by the sqlite3_shutdown() function. The
6038 ** implementation of this method is expected to release all outstanding
6039 ** resources obtained by the mutex methods implementation, especially
6040 ** those obtained by the xMutexInit method.  ^The xMutexEnd()
6041 ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
6042 **
6043 ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
6044 ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
6045 ** xMutexNotheld) implement the following interfaces (respectively):
6046 **
6047 ** <ul>
6048 **   <li>  [sqlite3_mutex_alloc()] </li>
6049 **   <li>  [sqlite3_mutex_free()] </li>
6050 **   <li>  [sqlite3_mutex_enter()] </li>
6051 **   <li>  [sqlite3_mutex_try()] </li>
6052 **   <li>  [sqlite3_mutex_leave()] </li>
6053 **   <li>  [sqlite3_mutex_held()] </li>
6054 **   <li>  [sqlite3_mutex_notheld()] </li>
6055 ** </ul>)^
6056 **
6057 ** The only difference is that the public sqlite3_XXX functions enumerated
6058 ** above silently ignore any invocations that pass a NULL pointer instead
6059 ** of a valid mutex handle. The implementations of the methods defined
6060 ** by this structure are not required to handle this case, the results
6061 ** of passing a NULL pointer instead of a valid mutex handle are undefined
6062 ** (i.e. it is acceptable to provide an implementation that segfaults if
6063 ** it is passed a NULL pointer).
6064 **
6065 ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
6066 ** invoke xMutexInit() multiple times within the same process and without
6067 ** intervening calls to xMutexEnd().  Second and subsequent calls to
6068 ** xMutexInit() must be no-ops.
6069 **
6070 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
6071 ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
6072 ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
6073 ** memory allocation for a fast or recursive mutex.
6074 **
6075 ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
6076 ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
6077 ** If xMutexInit fails in any way, it is expected to clean up after itself
6078 ** prior to returning.
6079 */
6080 typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
6081 struct sqlite3_mutex_methods {
6082   int (*xMutexInit)(void);
6083   int (*xMutexEnd)(void);
6084   sqlite3_mutex *(*xMutexAlloc)(int);
6085   void (*xMutexFree)(sqlite3_mutex *);
6086   void (*xMutexEnter)(sqlite3_mutex *);
6087   int (*xMutexTry)(sqlite3_mutex *);
6088   void (*xMutexLeave)(sqlite3_mutex *);
6089   int (*xMutexHeld)(sqlite3_mutex *);
6090   int (*xMutexNotheld)(sqlite3_mutex *);
6091 };
6092 
6093 /*
6094 ** CAPI3REF: Mutex Verification Routines
6095 **
6096 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
6097 ** are intended for use inside assert() statements.  ^The SQLite core
6098 ** never uses these routines except inside an assert() and applications
6099 ** are advised to follow the lead of the core.  ^The SQLite core only
6100 ** provides implementations for these routines when it is compiled
6101 ** with the SQLITE_DEBUG flag.  ^External mutex implementations
6102 ** are only required to provide these routines if SQLITE_DEBUG is
6103 ** defined and if NDEBUG is not defined.
6104 **
6105 ** ^These routines should return true if the mutex in their argument
6106 ** is held or not held, respectively, by the calling thread.
6107 **
6108 ** ^The implementation is not required to provide versions of these
6109 ** routines that actually work. If the implementation does not provide working
6110 ** versions of these routines, it should at least provide stubs that always
6111 ** return true so that one does not get spurious assertion failures.
6112 **
6113 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
6114 ** the routine should return 1.   This seems counter-intuitive since
6115 ** clearly the mutex cannot be held if it does not exist.  But
6116 ** the reason the mutex does not exist is because the build is not
6117 ** using mutexes.  And we do not want the assert() containing the
6118 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
6119 ** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
6120 ** interface should also return 1 when given a NULL pointer.
6121 */
6122 #ifndef NDEBUG
6123 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
6124 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
6125 #endif
6126 
6127 /*
6128 ** CAPI3REF: Mutex Types
6129 **
6130 ** The [sqlite3_mutex_alloc()] interface takes a single argument
6131 ** which is one of these integer constants.
6132 **
6133 ** The set of static mutexes may change from one SQLite release to the
6134 ** next.  Applications that override the built-in mutex logic must be
6135 ** prepared to accommodate additional static mutexes.
6136 */
6137 #define SQLITE_MUTEX_FAST             0
6138 #define SQLITE_MUTEX_RECURSIVE        1
6139 #define SQLITE_MUTEX_STATIC_MASTER    2
6140 #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
6141 #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
6142 #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
6143 #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
6144 #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
6145 #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
6146 #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
6147 
6148 /*
6149 ** CAPI3REF: Retrieve the mutex for a database connection
6150 **
6151 ** ^This interface returns a pointer the [sqlite3_mutex] object that
6152 ** serializes access to the [database connection] given in the argument
6153 ** when the [threading mode] is Serialized.
6154 ** ^If the [threading mode] is Single-thread or Multi-thread then this
6155 ** routine returns a NULL pointer.
6156 */
6157 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
6158 
6159 /*
6160 ** CAPI3REF: Low-Level Control Of Database Files
6161 **
6162 ** ^The [sqlite3_file_control()] interface makes a direct call to the
6163 ** xFileControl method for the [sqlite3_io_methods] object associated
6164 ** with a particular database identified by the second argument. ^The
6165 ** name of the database is "main" for the main database or "temp" for the
6166 ** TEMP database, or the name that appears after the AS keyword for
6167 ** databases that are added using the [ATTACH] SQL command.
6168 ** ^A NULL pointer can be used in place of "main" to refer to the
6169 ** main database file.
6170 ** ^The third and fourth parameters to this routine
6171 ** are passed directly through to the second and third parameters of
6172 ** the xFileControl method.  ^The return value of the xFileControl
6173 ** method becomes the return value of this routine.
6174 **
6175 ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
6176 ** a pointer to the underlying [sqlite3_file] object to be written into
6177 ** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
6178 ** case is a short-circuit path which does not actually invoke the
6179 ** underlying sqlite3_io_methods.xFileControl method.
6180 **
6181 ** ^If the second parameter (zDbName) does not match the name of any
6182 ** open database file, then SQLITE_ERROR is returned.  ^This error
6183 ** code is not remembered and will not be recalled by [sqlite3_errcode()]
6184 ** or [sqlite3_errmsg()].  The underlying xFileControl method might
6185 ** also return SQLITE_ERROR.  There is no way to distinguish between
6186 ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
6187 ** xFileControl method.
6188 **
6189 ** See also: [SQLITE_FCNTL_LOCKSTATE]
6190 */
6191 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
6192 
6193 /*
6194 ** CAPI3REF: Testing Interface
6195 **
6196 ** ^The sqlite3_test_control() interface is used to read out internal
6197 ** state of SQLite and to inject faults into SQLite for testing
6198 ** purposes.  ^The first parameter is an operation code that determines
6199 ** the number, meaning, and operation of all subsequent parameters.
6200 **
6201 ** This interface is not for use by applications.  It exists solely
6202 ** for verifying the correct operation of the SQLite library.  Depending
6203 ** on how the SQLite library is compiled, this interface might not exist.
6204 **
6205 ** The details of the operation codes, their meanings, the parameters
6206 ** they take, and what they do are all subject to change without notice.
6207 ** Unlike most of the SQLite API, this function is not guaranteed to
6208 ** operate consistently from one release to the next.
6209 */
6210 SQLITE_API int sqlite3_test_control(int op, ...);
6211 
6212 /*
6213 ** CAPI3REF: Testing Interface Operation Codes
6214 **
6215 ** These constants are the valid operation code parameters used
6216 ** as the first argument to [sqlite3_test_control()].
6217 **
6218 ** These parameters and their meanings are subject to change
6219 ** without notice.  These values are for testing purposes only.
6220 ** Applications should not use any of these parameters or the
6221 ** [sqlite3_test_control()] interface.
6222 */
6223 #define SQLITE_TESTCTRL_FIRST                    5
6224 #define SQLITE_TESTCTRL_PRNG_SAVE                5
6225 #define SQLITE_TESTCTRL_PRNG_RESTORE             6
6226 #define SQLITE_TESTCTRL_PRNG_RESET               7
6227 #define SQLITE_TESTCTRL_BITVEC_TEST              8
6228 #define SQLITE_TESTCTRL_FAULT_INSTALL            9
6229 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
6230 #define SQLITE_TESTCTRL_PENDING_BYTE            11
6231 #define SQLITE_TESTCTRL_ASSERT                  12
6232 #define SQLITE_TESTCTRL_ALWAYS                  13
6233 #define SQLITE_TESTCTRL_RESERVE                 14
6234 #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
6235 #define SQLITE_TESTCTRL_ISKEYWORD               16
6236 #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
6237 #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
6238 #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
6239 #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
6240 #define SQLITE_TESTCTRL_VDBE_COVERAGE           21
6241 #define SQLITE_TESTCTRL_LAST                    21
6242 
6243 /*
6244 ** CAPI3REF: SQLite Runtime Status
6245 **
6246 ** ^This interface is used to retrieve runtime status information
6247 ** about the performance of SQLite, and optionally to reset various
6248 ** highwater marks.  ^The first argument is an integer code for
6249 ** the specific parameter to measure.  ^(Recognized integer codes
6250 ** are of the form [status parameters | SQLITE_STATUS_...].)^
6251 ** ^The current value of the parameter is returned into *pCurrent.
6252 ** ^The highest recorded value is returned in *pHighwater.  ^If the
6253 ** resetFlag is true, then the highest record value is reset after
6254 ** *pHighwater is written.  ^(Some parameters do not record the highest
6255 ** value.  For those parameters
6256 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
6257 ** ^(Other parameters record only the highwater mark and not the current
6258 ** value.  For these latter parameters nothing is written into *pCurrent.)^
6259 **
6260 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6261 ** non-zero [error code] on failure.
6262 **
6263 ** This routine is threadsafe but is not atomic.  This routine can be
6264 ** called while other threads are running the same or different SQLite
6265 ** interfaces.  However the values returned in *pCurrent and
6266 ** *pHighwater reflect the status of SQLite at different points in time
6267 ** and it is possible that another thread might change the parameter
6268 ** in between the times when *pCurrent and *pHighwater are written.
6269 **
6270 ** See also: [sqlite3_db_status()]
6271 */
6272 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6273 
6274 
6275 /*
6276 ** CAPI3REF: Status Parameters
6277 ** KEYWORDS: {status parameters}
6278 **
6279 ** These integer constants designate various run-time status parameters
6280 ** that can be returned by [sqlite3_status()].
6281 **
6282 ** <dl>
6283 ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
6284 ** <dd>This parameter is the current amount of memory checked out
6285 ** using [sqlite3_malloc()], either directly or indirectly.  The
6286 ** figure includes calls made to [sqlite3_malloc()] by the application
6287 ** and internal memory usage by the SQLite library.  Scratch memory
6288 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
6289 ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
6290 ** this parameter.  The amount returned is the sum of the allocation
6291 ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
6292 **
6293 ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
6294 ** <dd>This parameter records the largest memory allocation request
6295 ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
6296 ** internal equivalents).  Only the value returned in the
6297 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6298 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6299 **
6300 ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
6301 ** <dd>This parameter records the number of separate memory allocations
6302 ** currently checked out.</dd>)^
6303 **
6304 ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
6305 ** <dd>This parameter returns the number of pages used out of the
6306 ** [pagecache memory allocator] that was configured using
6307 ** [SQLITE_CONFIG_PAGECACHE].  The
6308 ** value returned is in pages, not in bytes.</dd>)^
6309 **
6310 ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
6311 ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
6312 ** <dd>This parameter returns the number of bytes of page cache
6313 ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
6314 ** buffer and where forced to overflow to [sqlite3_malloc()].  The
6315 ** returned value includes allocations that overflowed because they
6316 ** where too large (they were larger than the "sz" parameter to
6317 ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
6318 ** no space was left in the page cache.</dd>)^
6319 **
6320 ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
6321 ** <dd>This parameter records the largest memory allocation request
6322 ** handed to [pagecache memory allocator].  Only the value returned in the
6323 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6324 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6325 **
6326 ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
6327 ** <dd>This parameter returns the number of allocations used out of the
6328 ** [scratch memory allocator] configured using
6329 ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
6330 ** in bytes.  Since a single thread may only have one scratch allocation
6331 ** outstanding at time, this parameter also reports the number of threads
6332 ** using scratch memory at the same time.</dd>)^
6333 **
6334 ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
6335 ** <dd>This parameter returns the number of bytes of scratch memory
6336 ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
6337 ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
6338 ** returned include overflows because the requested allocation was too
6339 ** larger (that is, because the requested allocation was larger than the
6340 ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
6341 ** slots were available.
6342 ** </dd>)^
6343 **
6344 ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
6345 ** <dd>This parameter records the largest memory allocation request
6346 ** handed to [scratch memory allocator].  Only the value returned in the
6347 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6348 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6349 **
6350 ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
6351 ** <dd>This parameter records the deepest parser stack.  It is only
6352 ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
6353 ** </dl>
6354 **
6355 ** New status parameters may be added from time to time.
6356 */
6357 #define SQLITE_STATUS_MEMORY_USED          0
6358 #define SQLITE_STATUS_PAGECACHE_USED       1
6359 #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
6360 #define SQLITE_STATUS_SCRATCH_USED         3
6361 #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
6362 #define SQLITE_STATUS_MALLOC_SIZE          5
6363 #define SQLITE_STATUS_PARSER_STACK         6
6364 #define SQLITE_STATUS_PAGECACHE_SIZE       7
6365 #define SQLITE_STATUS_SCRATCH_SIZE         8
6366 #define SQLITE_STATUS_MALLOC_COUNT         9
6367 
6368 /*
6369 ** CAPI3REF: Database Connection Status
6370 **
6371 ** ^This interface is used to retrieve runtime status information
6372 ** about a single [database connection].  ^The first argument is the
6373 ** database connection object to be interrogated.  ^The second argument
6374 ** is an integer constant, taken from the set of
6375 ** [SQLITE_DBSTATUS options], that
6376 ** determines the parameter to interrogate.  The set of
6377 ** [SQLITE_DBSTATUS options] is likely
6378 ** to grow in future releases of SQLite.
6379 **
6380 ** ^The current value of the requested parameter is written into *pCur
6381 ** and the highest instantaneous value is written into *pHiwtr.  ^If
6382 ** the resetFlg is true, then the highest instantaneous value is
6383 ** reset back down to the current value.
6384 **
6385 ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
6386 ** non-zero [error code] on failure.
6387 **
6388 ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
6389 */
6390 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
6391 
6392 /*
6393 ** CAPI3REF: Status Parameters for database connections
6394 ** KEYWORDS: {SQLITE_DBSTATUS options}
6395 **
6396 ** These constants are the available integer "verbs" that can be passed as
6397 ** the second argument to the [sqlite3_db_status()] interface.
6398 **
6399 ** New verbs may be added in future releases of SQLite. Existing verbs
6400 ** might be discontinued. Applications should check the return code from
6401 ** [sqlite3_db_status()] to make sure that the call worked.
6402 ** The [sqlite3_db_status()] interface will return a non-zero error code
6403 ** if a discontinued or unsupported verb is invoked.
6404 **
6405 ** <dl>
6406 ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
6407 ** <dd>This parameter returns the number of lookaside memory slots currently
6408 ** checked out.</dd>)^
6409 **
6410 ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6411 ** <dd>This parameter returns the number malloc attempts that were
6412 ** satisfied using lookaside memory. Only the high-water value is meaningful;
6413 ** the current value is always zero.)^
6414 **
6415 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
6416 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6417 ** <dd>This parameter returns the number malloc attempts that might have
6418 ** been satisfied using lookaside memory but failed due to the amount of
6419 ** memory requested being larger than the lookaside slot size.
6420 ** Only the high-water value is meaningful;
6421 ** the current value is always zero.)^
6422 **
6423 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
6424 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6425 ** <dd>This parameter returns the number malloc attempts that might have
6426 ** been satisfied using lookaside memory but failed due to all lookaside
6427 ** memory already being in use.
6428 ** Only the high-water value is meaningful;
6429 ** the current value is always zero.)^
6430 **
6431 ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6432 ** <dd>This parameter returns the approximate number of of bytes of heap
6433 ** memory used by all pager caches associated with the database connection.)^
6434 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
6435 **
6436 ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
6437 ** <dd>This parameter returns the approximate number of of bytes of heap
6438 ** memory used to store the schema for all databases associated
6439 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
6440 ** ^The full amount of memory used by the schemas is reported, even if the
6441 ** schema memory is shared with other database connections due to
6442 ** [shared cache mode] being enabled.
6443 ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
6444 **
6445 ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
6446 ** <dd>This parameter returns the approximate number of of bytes of heap
6447 ** and lookaside memory used by all prepared statements associated with
6448 ** the database connection.)^
6449 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6450 ** </dd>
6451 **
6452 ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
6453 ** <dd>This parameter returns the number of pager cache hits that have
6454 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
6455 ** is always 0.
6456 ** </dd>
6457 **
6458 ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
6459 ** <dd>This parameter returns the number of pager cache misses that have
6460 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
6461 ** is always 0.
6462 ** </dd>
6463 **
6464 ** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
6465 ** <dd>This parameter returns the number of dirty cache entries that have
6466 ** been written to disk. Specifically, the number of pages written to the
6467 ** wal file in wal mode databases, or the number of pages written to the
6468 ** database file in rollback mode databases. Any pages written as part of
6469 ** transaction rollback or database recovery operations are not included.
6470 ** If an IO or other error occurs while writing a page to disk, the effect
6471 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
6472 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
6473 ** </dd>
6474 **
6475 ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
6476 ** <dd>This parameter returns zero for the current value if and only if
6477 ** all foreign key constraints (deferred or immediate) have been
6478 ** resolved.)^  ^The highwater mark is always 0.
6479 ** </dd>
6480 ** </dl>
6481 */
6482 #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
6483 #define SQLITE_DBSTATUS_CACHE_USED           1
6484 #define SQLITE_DBSTATUS_SCHEMA_USED          2
6485 #define SQLITE_DBSTATUS_STMT_USED            3
6486 #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
6487 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
6488 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
6489 #define SQLITE_DBSTATUS_CACHE_HIT            7
6490 #define SQLITE_DBSTATUS_CACHE_MISS           8
6491 #define SQLITE_DBSTATUS_CACHE_WRITE          9
6492 #define SQLITE_DBSTATUS_DEFERRED_FKS        10
6493 #define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
6494 
6495 
6496 /*
6497 ** CAPI3REF: Prepared Statement Status
6498 **
6499 ** ^(Each prepared statement maintains various
6500 ** [SQLITE_STMTSTATUS counters] that measure the number
6501 ** of times it has performed specific operations.)^  These counters can
6502 ** be used to monitor the performance characteristics of the prepared
6503 ** statements.  For example, if the number of table steps greatly exceeds
6504 ** the number of table searches or result rows, that would tend to indicate
6505 ** that the prepared statement is using a full table scan rather than
6506 ** an index.
6507 **
6508 ** ^(This interface is used to retrieve and reset counter values from
6509 ** a [prepared statement].  The first argument is the prepared statement
6510 ** object to be interrogated.  The second argument
6511 ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
6512 ** to be interrogated.)^
6513 ** ^The current value of the requested counter is returned.
6514 ** ^If the resetFlg is true, then the counter is reset to zero after this
6515 ** interface call returns.
6516 **
6517 ** See also: [sqlite3_status()] and [sqlite3_db_status()].
6518 */
6519 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
6520 
6521 /*
6522 ** CAPI3REF: Status Parameters for prepared statements
6523 ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
6524 **
6525 ** These preprocessor macros define integer codes that name counter
6526 ** values associated with the [sqlite3_stmt_status()] interface.
6527 ** The meanings of the various counters are as follows:
6528 **
6529 ** <dl>
6530 ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
6531 ** <dd>^This is the number of times that SQLite has stepped forward in
6532 ** a table as part of a full table scan.  Large numbers for this counter
6533 ** may indicate opportunities for performance improvement through
6534 ** careful use of indices.</dd>
6535 **
6536 ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
6537 ** <dd>^This is the number of sort operations that have occurred.
6538 ** A non-zero value in this counter may indicate an opportunity to
6539 ** improvement performance through careful use of indices.</dd>
6540 **
6541 ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
6542 ** <dd>^This is the number of rows inserted into transient indices that
6543 ** were created automatically in order to help joins run faster.
6544 ** A non-zero value in this counter may indicate an opportunity to
6545 ** improvement performance by adding permanent indices that do not
6546 ** need to be reinitialized each time the statement is run.</dd>
6547 **
6548 ** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
6549 ** <dd>^This is the number of virtual machine operations executed
6550 ** by the prepared statement if that number is less than or equal
6551 ** to 2147483647.  The number of virtual machine operations can be
6552 ** used as a proxy for the total work done by the prepared statement.
6553 ** If the number of virtual machine operations exceeds 2147483647
6554 ** then the value returned by this statement status code is undefined.
6555 ** </dd>
6556 ** </dl>
6557 */
6558 #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
6559 #define SQLITE_STMTSTATUS_SORT              2
6560 #define SQLITE_STMTSTATUS_AUTOINDEX         3
6561 #define SQLITE_STMTSTATUS_VM_STEP           4
6562 
6563 /*
6564 ** CAPI3REF: Custom Page Cache Object
6565 **
6566 ** The sqlite3_pcache type is opaque.  It is implemented by
6567 ** the pluggable module.  The SQLite core has no knowledge of
6568 ** its size or internal structure and never deals with the
6569 ** sqlite3_pcache object except by holding and passing pointers
6570 ** to the object.
6571 **
6572 ** See [sqlite3_pcache_methods2] for additional information.
6573 */
6574 typedef struct sqlite3_pcache sqlite3_pcache;
6575 
6576 /*
6577 ** CAPI3REF: Custom Page Cache Object
6578 **
6579 ** The sqlite3_pcache_page object represents a single page in the
6580 ** page cache.  The page cache will allocate instances of this
6581 ** object.  Various methods of the page cache use pointers to instances
6582 ** of this object as parameters or as their return value.
6583 **
6584 ** See [sqlite3_pcache_methods2] for additional information.
6585 */
6586 typedef struct sqlite3_pcache_page sqlite3_pcache_page;
6587 struct sqlite3_pcache_page {
6588   void *pBuf;        /* The content of the page */
6589   void *pExtra;      /* Extra information associated with the page */
6590 };
6591 
6592 /*
6593 ** CAPI3REF: Application Defined Page Cache.
6594 ** KEYWORDS: {page cache}
6595 **
6596 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
6597 ** register an alternative page cache implementation by passing in an
6598 ** instance of the sqlite3_pcache_methods2 structure.)^
6599 ** In many applications, most of the heap memory allocated by
6600 ** SQLite is used for the page cache.
6601 ** By implementing a
6602 ** custom page cache using this API, an application can better control
6603 ** the amount of memory consumed by SQLite, the way in which
6604 ** that memory is allocated and released, and the policies used to
6605 ** determine exactly which parts of a database file are cached and for
6606 ** how long.
6607 **
6608 ** The alternative page cache mechanism is an
6609 ** extreme measure that is only needed by the most demanding applications.
6610 ** The built-in page cache is recommended for most uses.
6611 **
6612 ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
6613 ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
6614 ** the application may discard the parameter after the call to
6615 ** [sqlite3_config()] returns.)^
6616 **
6617 ** [[the xInit() page cache method]]
6618 ** ^(The xInit() method is called once for each effective
6619 ** call to [sqlite3_initialize()])^
6620 ** (usually only once during the lifetime of the process). ^(The xInit()
6621 ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
6622 ** The intent of the xInit() method is to set up global data structures
6623 ** required by the custom page cache implementation.
6624 ** ^(If the xInit() method is NULL, then the
6625 ** built-in default page cache is used instead of the application defined
6626 ** page cache.)^
6627 **
6628 ** [[the xShutdown() page cache method]]
6629 ** ^The xShutdown() method is called by [sqlite3_shutdown()].
6630 ** It can be used to clean up
6631 ** any outstanding resources before process shutdown, if required.
6632 ** ^The xShutdown() method may be NULL.
6633 **
6634 ** ^SQLite automatically serializes calls to the xInit method,
6635 ** so the xInit method need not be threadsafe.  ^The
6636 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
6637 ** not need to be threadsafe either.  All other methods must be threadsafe
6638 ** in multithreaded applications.
6639 **
6640 ** ^SQLite will never invoke xInit() more than once without an intervening
6641 ** call to xShutdown().
6642 **
6643 ** [[the xCreate() page cache methods]]
6644 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
6645 ** SQLite will typically create one cache instance for each open database file,
6646 ** though this is not guaranteed. ^The
6647 ** first parameter, szPage, is the size in bytes of the pages that must
6648 ** be allocated by the cache.  ^szPage will always a power of two.  ^The
6649 ** second parameter szExtra is a number of bytes of extra storage
6650 ** associated with each page cache entry.  ^The szExtra parameter will
6651 ** a number less than 250.  SQLite will use the
6652 ** extra szExtra bytes on each page to store metadata about the underlying
6653 ** database page on disk.  The value passed into szExtra depends
6654 ** on the SQLite version, the target platform, and how SQLite was compiled.
6655 ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
6656 ** created will be used to cache database pages of a file stored on disk, or
6657 ** false if it is used for an in-memory database. The cache implementation
6658 ** does not have to do anything special based with the value of bPurgeable;
6659 ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
6660 ** never invoke xUnpin() except to deliberately delete a page.
6661 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
6662 ** false will always have the "discard" flag set to true.
6663 ** ^Hence, a cache created with bPurgeable false will
6664 ** never contain any unpinned pages.
6665 **
6666 ** [[the xCachesize() page cache method]]
6667 ** ^(The xCachesize() method may be called at any time by SQLite to set the
6668 ** suggested maximum cache-size (number of pages stored by) the cache
6669 ** instance passed as the first argument. This is the value configured using
6670 ** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
6671 ** parameter, the implementation is not required to do anything with this
6672 ** value; it is advisory only.
6673 **
6674 ** [[the xPagecount() page cache methods]]
6675 ** The xPagecount() method must return the number of pages currently
6676 ** stored in the cache, both pinned and unpinned.
6677 **
6678 ** [[the xFetch() page cache methods]]
6679 ** The xFetch() method locates a page in the cache and returns a pointer to
6680 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
6681 ** The pBuf element of the returned sqlite3_pcache_page object will be a
6682 ** pointer to a buffer of szPage bytes used to store the content of a
6683 ** single database page.  The pExtra element of sqlite3_pcache_page will be
6684 ** a pointer to the szExtra bytes of extra storage that SQLite has requested
6685 ** for each entry in the page cache.
6686 **
6687 ** The page to be fetched is determined by the key. ^The minimum key value
6688 ** is 1.  After it has been retrieved using xFetch, the page is considered
6689 ** to be "pinned".
6690 **
6691 ** If the requested page is already in the page cache, then the page cache
6692 ** implementation must return a pointer to the page buffer with its content
6693 ** intact.  If the requested page is not already in the cache, then the
6694 ** cache implementation should use the value of the createFlag
6695 ** parameter to help it determined what action to take:
6696 **
6697 ** <table border=1 width=85% align=center>
6698 ** <tr><th> createFlag <th> Behavior when page is not already in cache
6699 ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
6700 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
6701 **                 Otherwise return NULL.
6702 ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
6703 **                 NULL if allocating a new page is effectively impossible.
6704 ** </table>
6705 **
6706 ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
6707 ** will only use a createFlag of 2 after a prior call with a createFlag of 1
6708 ** failed.)^  In between the to xFetch() calls, SQLite may
6709 ** attempt to unpin one or more cache pages by spilling the content of
6710 ** pinned pages to disk and synching the operating system disk cache.
6711 **
6712 ** [[the xUnpin() page cache method]]
6713 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
6714 ** as its second argument.  If the third parameter, discard, is non-zero,
6715 ** then the page must be evicted from the cache.
6716 ** ^If the discard parameter is
6717 ** zero, then the page may be discarded or retained at the discretion of
6718 ** page cache implementation. ^The page cache implementation
6719 ** may choose to evict unpinned pages at any time.
6720 **
6721 ** The cache must not perform any reference counting. A single
6722 ** call to xUnpin() unpins the page regardless of the number of prior calls
6723 ** to xFetch().
6724 **
6725 ** [[the xRekey() page cache methods]]
6726 ** The xRekey() method is used to change the key value associated with the
6727 ** page passed as the second argument. If the cache
6728 ** previously contains an entry associated with newKey, it must be
6729 ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
6730 ** to be pinned.
6731 **
6732 ** When SQLite calls the xTruncate() method, the cache must discard all
6733 ** existing cache entries with page numbers (keys) greater than or equal
6734 ** to the value of the iLimit parameter passed to xTruncate(). If any
6735 ** of these pages are pinned, they are implicitly unpinned, meaning that
6736 ** they can be safely discarded.
6737 **
6738 ** [[the xDestroy() page cache method]]
6739 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6740 ** All resources associated with the specified cache should be freed. ^After
6741 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6742 ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
6743 ** functions.
6744 **
6745 ** [[the xShrink() page cache method]]
6746 ** ^SQLite invokes the xShrink() method when it wants the page cache to
6747 ** free up as much of heap memory as possible.  The page cache implementation
6748 ** is not obligated to free any memory, but well-behaved implementations should
6749 ** do their best.
6750 */
6751 typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
6752 struct sqlite3_pcache_methods2 {
6753   int iVersion;
6754   void *pArg;
6755   int (*xInit)(void*);
6756   void (*xShutdown)(void*);
6757   sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
6758   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6759   int (*xPagecount)(sqlite3_pcache*);
6760   sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6761   void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
6762   void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
6763       unsigned oldKey, unsigned newKey);
6764   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6765   void (*xDestroy)(sqlite3_pcache*);
6766   void (*xShrink)(sqlite3_pcache*);
6767 };
6768 
6769 /*
6770 ** This is the obsolete pcache_methods object that has now been replaced
6771 ** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
6772 ** retained in the header file for backwards compatibility only.
6773 */
6774 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6775 struct sqlite3_pcache_methods {
6776   void *pArg;
6777   int (*xInit)(void*);
6778   void (*xShutdown)(void*);
6779   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
6780   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6781   int (*xPagecount)(sqlite3_pcache*);
6782   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6783   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6784   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6785   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6786   void (*xDestroy)(sqlite3_pcache*);
6787 };
6788 
6789 
6790 /*
6791 ** CAPI3REF: Online Backup Object
6792 **
6793 ** The sqlite3_backup object records state information about an ongoing
6794 ** online backup operation.  ^The sqlite3_backup object is created by
6795 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
6796 ** [sqlite3_backup_finish()].
6797 **
6798 ** See Also: [Using the SQLite Online Backup API]
6799 */
6800 typedef struct sqlite3_backup sqlite3_backup;
6801 
6802 /*
6803 ** CAPI3REF: Online Backup API.
6804 **
6805 ** The backup API copies the content of one database into another.
6806 ** It is useful either for creating backups of databases or
6807 ** for copying in-memory databases to or from persistent files.
6808 **
6809 ** See Also: [Using the SQLite Online Backup API]
6810 **
6811 ** ^SQLite holds a write transaction open on the destination database file
6812 ** for the duration of the backup operation.
6813 ** ^The source database is read-locked only while it is being read;
6814 ** it is not locked continuously for the entire backup operation.
6815 ** ^Thus, the backup may be performed on a live source database without
6816 ** preventing other database connections from
6817 ** reading or writing to the source database while the backup is underway.
6818 **
6819 ** ^(To perform a backup operation:
6820 **   <ol>
6821 **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
6822 **         backup,
6823 **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
6824 **         the data between the two databases, and finally
6825 **     <li><b>sqlite3_backup_finish()</b> is called to release all resources
6826 **         associated with the backup operation.
6827 **   </ol>)^
6828 ** There should be exactly one call to sqlite3_backup_finish() for each
6829 ** successful call to sqlite3_backup_init().
6830 **
6831 ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
6832 **
6833 ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
6834 ** [database connection] associated with the destination database
6835 ** and the database name, respectively.
6836 ** ^The database name is "main" for the main database, "temp" for the
6837 ** temporary database, or the name specified after the AS keyword in
6838 ** an [ATTACH] statement for an attached database.
6839 ** ^The S and M arguments passed to
6840 ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
6841 ** and database name of the source database, respectively.
6842 ** ^The source and destination [database connections] (parameters S and D)
6843 ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
6844 ** an error.
6845 **
6846 ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
6847 ** returned and an error code and error message are stored in the
6848 ** destination [database connection] D.
6849 ** ^The error code and message for the failed call to sqlite3_backup_init()
6850 ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
6851 ** [sqlite3_errmsg16()] functions.
6852 ** ^A successful call to sqlite3_backup_init() returns a pointer to an
6853 ** [sqlite3_backup] object.
6854 ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
6855 ** sqlite3_backup_finish() functions to perform the specified backup
6856 ** operation.
6857 **
6858 ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
6859 **
6860 ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
6861 ** the source and destination databases specified by [sqlite3_backup] object B.
6862 ** ^If N is negative, all remaining source pages are copied.
6863 ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
6864 ** are still more pages to be copied, then the function returns [SQLITE_OK].
6865 ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
6866 ** from source to destination, then it returns [SQLITE_DONE].
6867 ** ^If an error occurs while running sqlite3_backup_step(B,N),
6868 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
6869 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
6870 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
6871 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
6872 **
6873 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
6874 ** <ol>
6875 ** <li> the destination database was opened read-only, or
6876 ** <li> the destination database is using write-ahead-log journaling
6877 ** and the destination and source page sizes differ, or
6878 ** <li> the destination database is an in-memory database and the
6879 ** destination and source page sizes differ.
6880 ** </ol>)^
6881 **
6882 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
6883 ** the [sqlite3_busy_handler | busy-handler function]
6884 ** is invoked (if one is specified). ^If the
6885 ** busy-handler returns non-zero before the lock is available, then
6886 ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
6887 ** sqlite3_backup_step() can be retried later. ^If the source
6888 ** [database connection]
6889 ** is being used to write to the source database when sqlite3_backup_step()
6890 ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
6891 ** case the call to sqlite3_backup_step() can be retried later on. ^(If
6892 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
6893 ** [SQLITE_READONLY] is returned, then
6894 ** there is no point in retrying the call to sqlite3_backup_step(). These
6895 ** errors are considered fatal.)^  The application must accept
6896 ** that the backup operation has failed and pass the backup operation handle
6897 ** to the sqlite3_backup_finish() to release associated resources.
6898 **
6899 ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
6900 ** on the destination file. ^The exclusive lock is not released until either
6901 ** sqlite3_backup_finish() is called or the backup operation is complete
6902 ** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
6903 ** sqlite3_backup_step() obtains a [shared lock] on the source database that
6904 ** lasts for the duration of the sqlite3_backup_step() call.
6905 ** ^Because the source database is not locked between calls to
6906 ** sqlite3_backup_step(), the source database may be modified mid-way
6907 ** through the backup process.  ^If the source database is modified by an
6908 ** external process or via a database connection other than the one being
6909 ** used by the backup operation, then the backup will be automatically
6910 ** restarted by the next call to sqlite3_backup_step(). ^If the source
6911 ** database is modified by the using the same database connection as is used
6912 ** by the backup operation, then the backup database is automatically
6913 ** updated at the same time.
6914 **
6915 ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
6916 **
6917 ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
6918 ** application wishes to abandon the backup operation, the application
6919 ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
6920 ** ^The sqlite3_backup_finish() interfaces releases all
6921 ** resources associated with the [sqlite3_backup] object.
6922 ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
6923 ** active write-transaction on the destination database is rolled back.
6924 ** The [sqlite3_backup] object is invalid
6925 ** and may not be used following a call to sqlite3_backup_finish().
6926 **
6927 ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
6928 ** sqlite3_backup_step() errors occurred, regardless or whether or not
6929 ** sqlite3_backup_step() completed.
6930 ** ^If an out-of-memory condition or IO error occurred during any prior
6931 ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
6932 ** sqlite3_backup_finish() returns the corresponding [error code].
6933 **
6934 ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
6935 ** is not a permanent error and does not affect the return value of
6936 ** sqlite3_backup_finish().
6937 **
6938 ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
6939 ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
6940 **
6941 ** ^Each call to sqlite3_backup_step() sets two values inside
6942 ** the [sqlite3_backup] object: the number of pages still to be backed
6943 ** up and the total number of pages in the source database file.
6944 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
6945 ** retrieve these two values, respectively.
6946 **
6947 ** ^The values returned by these functions are only updated by
6948 ** sqlite3_backup_step(). ^If the source database is modified during a backup
6949 ** operation, then the values are not updated to account for any extra
6950 ** pages that need to be updated or the size of the source database file
6951 ** changing.
6952 **
6953 ** <b>Concurrent Usage of Database Handles</b>
6954 **
6955 ** ^The source [database connection] may be used by the application for other
6956 ** purposes while a backup operation is underway or being initialized.
6957 ** ^If SQLite is compiled and configured to support threadsafe database
6958 ** connections, then the source database connection may be used concurrently
6959 ** from within other threads.
6960 **
6961 ** However, the application must guarantee that the destination
6962 ** [database connection] is not passed to any other API (by any thread) after
6963 ** sqlite3_backup_init() is called and before the corresponding call to
6964 ** sqlite3_backup_finish().  SQLite does not currently check to see
6965 ** if the application incorrectly accesses the destination [database connection]
6966 ** and so no error code is reported, but the operations may malfunction
6967 ** nevertheless.  Use of the destination database connection while a
6968 ** backup is in progress might also also cause a mutex deadlock.
6969 **
6970 ** If running in [shared cache mode], the application must
6971 ** guarantee that the shared cache used by the destination database
6972 ** is not accessed while the backup is running. In practice this means
6973 ** that the application must guarantee that the disk file being
6974 ** backed up to is not accessed by any connection within the process,
6975 ** not just the specific connection that was passed to sqlite3_backup_init().
6976 **
6977 ** The [sqlite3_backup] object itself is partially threadsafe. Multiple
6978 ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
6979 ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
6980 ** APIs are not strictly speaking threadsafe. If they are invoked at the
6981 ** same time as another thread is invoking sqlite3_backup_step() it is
6982 ** possible that they return invalid values.
6983 */
6984 SQLITE_API sqlite3_backup *sqlite3_backup_init(
6985   sqlite3 *pDest,                        /* Destination database handle */
6986   const char *zDestName,                 /* Destination database name */
6987   sqlite3 *pSource,                      /* Source database handle */
6988   const char *zSourceName                /* Source database name */
6989 );
6990 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
6991 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
6992 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
6993 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
6994 
6995 /*
6996 ** CAPI3REF: Unlock Notification
6997 **
6998 ** ^When running in shared-cache mode, a database operation may fail with
6999 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
7000 ** individual tables within the shared-cache cannot be obtained. See
7001 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
7002 ** ^This API may be used to register a callback that SQLite will invoke
7003 ** when the connection currently holding the required lock relinquishes it.
7004 ** ^This API is only available if the library was compiled with the
7005 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
7006 **
7007 ** See Also: [Using the SQLite Unlock Notification Feature].
7008 **
7009 ** ^Shared-cache locks are released when a database connection concludes
7010 ** its current transaction, either by committing it or rolling it back.
7011 **
7012 ** ^When a connection (known as the blocked connection) fails to obtain a
7013 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
7014 ** identity of the database connection (the blocking connection) that
7015 ** has locked the required resource is stored internally. ^After an
7016 ** application receives an SQLITE_LOCKED error, it may call the
7017 ** sqlite3_unlock_notify() method with the blocked connection handle as
7018 ** the first argument to register for a callback that will be invoked
7019 ** when the blocking connections current transaction is concluded. ^The
7020 ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
7021 ** call that concludes the blocking connections transaction.
7022 **
7023 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
7024 ** there is a chance that the blocking connection will have already
7025 ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
7026 ** If this happens, then the specified callback is invoked immediately,
7027 ** from within the call to sqlite3_unlock_notify().)^
7028 **
7029 ** ^If the blocked connection is attempting to obtain a write-lock on a
7030 ** shared-cache table, and more than one other connection currently holds
7031 ** a read-lock on the same table, then SQLite arbitrarily selects one of
7032 ** the other connections to use as the blocking connection.
7033 **
7034 ** ^(There may be at most one unlock-notify callback registered by a
7035 ** blocked connection. If sqlite3_unlock_notify() is called when the
7036 ** blocked connection already has a registered unlock-notify callback,
7037 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
7038 ** called with a NULL pointer as its second argument, then any existing
7039 ** unlock-notify callback is canceled. ^The blocked connections
7040 ** unlock-notify callback may also be canceled by closing the blocked
7041 ** connection using [sqlite3_close()].
7042 **
7043 ** The unlock-notify callback is not reentrant. If an application invokes
7044 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
7045 ** crash or deadlock may be the result.
7046 **
7047 ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
7048 ** returns SQLITE_OK.
7049 **
7050 ** <b>Callback Invocation Details</b>
7051 **
7052 ** When an unlock-notify callback is registered, the application provides a
7053 ** single void* pointer that is passed to the callback when it is invoked.
7054 ** However, the signature of the callback function allows SQLite to pass
7055 ** it an array of void* context pointers. The first argument passed to
7056 ** an unlock-notify callback is a pointer to an array of void* pointers,
7057 ** and the second is the number of entries in the array.
7058 **
7059 ** When a blocking connections transaction is concluded, there may be
7060 ** more than one blocked connection that has registered for an unlock-notify
7061 ** callback. ^If two or more such blocked connections have specified the
7062 ** same callback function, then instead of invoking the callback function
7063 ** multiple times, it is invoked once with the set of void* context pointers
7064 ** specified by the blocked connections bundled together into an array.
7065 ** This gives the application an opportunity to prioritize any actions
7066 ** related to the set of unblocked database connections.
7067 **
7068 ** <b>Deadlock Detection</b>
7069 **
7070 ** Assuming that after registering for an unlock-notify callback a
7071 ** database waits for the callback to be issued before taking any further
7072 ** action (a reasonable assumption), then using this API may cause the
7073 ** application to deadlock. For example, if connection X is waiting for
7074 ** connection Y's transaction to be concluded, and similarly connection
7075 ** Y is waiting on connection X's transaction, then neither connection
7076 ** will proceed and the system may remain deadlocked indefinitely.
7077 **
7078 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
7079 ** detection. ^If a given call to sqlite3_unlock_notify() would put the
7080 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
7081 ** unlock-notify callback is registered. The system is said to be in
7082 ** a deadlocked state if connection A has registered for an unlock-notify
7083 ** callback on the conclusion of connection B's transaction, and connection
7084 ** B has itself registered for an unlock-notify callback when connection
7085 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
7086 ** the system is also considered to be deadlocked if connection B has
7087 ** registered for an unlock-notify callback on the conclusion of connection
7088 ** C's transaction, where connection C is waiting on connection A. ^Any
7089 ** number of levels of indirection are allowed.
7090 **
7091 ** <b>The "DROP TABLE" Exception</b>
7092 **
7093 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
7094 ** always appropriate to call sqlite3_unlock_notify(). There is however,
7095 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
7096 ** SQLite checks if there are any currently executing SELECT statements
7097 ** that belong to the same connection. If there are, SQLITE_LOCKED is
7098 ** returned. In this case there is no "blocking connection", so invoking
7099 ** sqlite3_unlock_notify() results in the unlock-notify callback being
7100 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
7101 ** or "DROP INDEX" query, an infinite loop might be the result.
7102 **
7103 ** One way around this problem is to check the extended error code returned
7104 ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
7105 ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
7106 ** the special "DROP TABLE/INDEX" case, the extended error code is just
7107 ** SQLITE_LOCKED.)^
7108 */
7109 SQLITE_API int sqlite3_unlock_notify(
7110   sqlite3 *pBlocked,                          /* Waiting connection */
7111   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
7112   void *pNotifyArg                            /* Argument to pass to xNotify */
7113 );
7114 
7115 
7116 /*
7117 ** CAPI3REF: String Comparison
7118 **
7119 ** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
7120 ** and extensions to compare the contents of two buffers containing UTF-8
7121 ** strings in a case-independent fashion, using the same definition of "case
7122 ** independence" that SQLite uses internally when comparing identifiers.
7123 */
7124 SQLITE_API int sqlite3_stricmp(const char *, const char *);
7125 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
7126 
7127 /*
7128 ** CAPI3REF: String Globbing
7129 *
7130 ** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
7131 ** the glob pattern P, and it returns non-zero if string X does not match
7132 ** the glob pattern P.  ^The definition of glob pattern matching used in
7133 ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
7134 ** SQL dialect used by SQLite.  ^The sqlite3_strglob(P,X) function is case
7135 ** sensitive.
7136 **
7137 ** Note that this routine returns zero on a match and non-zero if the strings
7138 ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
7139 */
7140 SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
7141 
7142 /*
7143 ** CAPI3REF: Error Logging Interface
7144 **
7145 ** ^The [sqlite3_log()] interface writes a message into the [error log]
7146 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
7147 ** ^If logging is enabled, the zFormat string and subsequent arguments are
7148 ** used with [sqlite3_snprintf()] to generate the final output string.
7149 **
7150 ** The sqlite3_log() interface is intended for use by extensions such as
7151 ** virtual tables, collating functions, and SQL functions.  While there is
7152 ** nothing to prevent an application from calling sqlite3_log(), doing so
7153 ** is considered bad form.
7154 **
7155 ** The zFormat string must not be NULL.
7156 **
7157 ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
7158 ** will not use dynamically allocated memory.  The log message is stored in
7159 ** a fixed-length buffer on the stack.  If the log message is longer than
7160 ** a few hundred characters, it will be truncated to the length of the
7161 ** buffer.
7162 */
7163 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
7164 
7165 /*
7166 ** CAPI3REF: Write-Ahead Log Commit Hook
7167 **
7168 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
7169 ** will be invoked each time a database connection commits data to a
7170 ** [write-ahead log] (i.e. whenever a transaction is committed in
7171 ** [journal_mode | journal_mode=WAL mode]).
7172 **
7173 ** ^The callback is invoked by SQLite after the commit has taken place and
7174 ** the associated write-lock on the database released, so the implementation
7175 ** may read, write or [checkpoint] the database as required.
7176 **
7177 ** ^The first parameter passed to the callback function when it is invoked
7178 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
7179 ** registering the callback. ^The second is a copy of the database handle.
7180 ** ^The third parameter is the name of the database that was written to -
7181 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
7182 ** is the number of pages currently in the write-ahead log file,
7183 ** including those that were just committed.
7184 **
7185 ** The callback function should normally return [SQLITE_OK].  ^If an error
7186 ** code is returned, that error will propagate back up through the
7187 ** SQLite code base to cause the statement that provoked the callback
7188 ** to report an error, though the commit will have still occurred. If the
7189 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
7190 ** that does not correspond to any valid SQLite error code, the results
7191 ** are undefined.
7192 **
7193 ** A single database handle may have at most a single write-ahead log callback
7194 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
7195 ** previously registered write-ahead log callback. ^Note that the
7196 ** [sqlite3_wal_autocheckpoint()] interface and the
7197 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
7198 ** those overwrite any prior [sqlite3_wal_hook()] settings.
7199 */
7200 SQLITE_API void *sqlite3_wal_hook(
7201   sqlite3*,
7202   int(*)(void *,sqlite3*,const char*,int),
7203   void*
7204 );
7205 
7206 /*
7207 ** CAPI3REF: Configure an auto-checkpoint
7208 **
7209 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
7210 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
7211 ** to automatically [checkpoint]
7212 ** after committing a transaction if there are N or
7213 ** more frames in the [write-ahead log] file.  ^Passing zero or
7214 ** a negative value as the nFrame parameter disables automatic
7215 ** checkpoints entirely.
7216 **
7217 ** ^The callback registered by this function replaces any existing callback
7218 ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
7219 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7220 ** configured by this function.
7221 **
7222 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7223 ** from SQL.
7224 **
7225 ** ^Every new [database connection] defaults to having the auto-checkpoint
7226 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7227 ** pages.  The use of this interface
7228 ** is only necessary if the default setting is found to be suboptimal
7229 ** for a particular application.
7230 */
7231 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
7232 
7233 /*
7234 ** CAPI3REF: Checkpoint a database
7235 **
7236 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7237 ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
7238 ** empty string, then a checkpoint is run on all databases of
7239 ** connection D.  ^If the database connection D is not in
7240 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7241 **
7242 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
7243 ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
7244 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
7245 ** run whenever the WAL reaches a certain size threshold.
7246 **
7247 ** See also: [sqlite3_wal_checkpoint_v2()]
7248 */
7249 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
7250 
7251 /*
7252 ** CAPI3REF: Checkpoint a database
7253 **
7254 ** Run a checkpoint operation on WAL database zDb attached to database
7255 ** handle db. The specific operation is determined by the value of the
7256 ** eMode parameter:
7257 **
7258 ** <dl>
7259 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7260 **   Checkpoint as many frames as possible without waiting for any database
7261 **   readers or writers to finish. Sync the db file if all frames in the log
7262 **   are checkpointed. This mode is the same as calling
7263 **   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
7264 **
7265 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
7266 **   This mode blocks (calls the busy-handler callback) until there is no
7267 **   database writer and all readers are reading from the most recent database
7268 **   snapshot. It then checkpoints all frames in the log file and syncs the
7269 **   database file. This call blocks database writers while it is running,
7270 **   but not database readers.
7271 **
7272 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7273 **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7274 **   checkpointing the log file it blocks (calls the busy-handler callback)
7275 **   until all readers are reading from the database file only. This ensures
7276 **   that the next client to write to the database file restarts the log file
7277 **   from the beginning. This call blocks database writers while it is running,
7278 **   but not database readers.
7279 ** </dl>
7280 **
7281 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
7282 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
7283 ** the total number of checkpointed frames (including any that were already
7284 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
7285 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
7286 ** If no values are available because of an error, they are both set to -1
7287 ** before returning to communicate this to the caller.
7288 **
7289 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
7290 ** any other process is running a checkpoint operation at the same time, the
7291 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
7292 ** busy-handler configured, it will not be invoked in this case.
7293 **
7294 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
7295 ** "writer" lock on the database file. If the writer lock cannot be obtained
7296 ** immediately, and a busy-handler is configured, it is invoked and the writer
7297 ** lock retried until either the busy-handler returns 0 or the lock is
7298 ** successfully obtained. The busy-handler is also invoked while waiting for
7299 ** database readers as described above. If the busy-handler returns 0 before
7300 ** the writer lock is obtained or while waiting for database readers, the
7301 ** checkpoint operation proceeds from that point in the same way as
7302 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
7303 ** without blocking any further. SQLITE_BUSY is returned in this case.
7304 **
7305 ** If parameter zDb is NULL or points to a zero length string, then the
7306 ** specified operation is attempted on all WAL databases. In this case the
7307 ** values written to output parameters *pnLog and *pnCkpt are undefined. If
7308 ** an SQLITE_BUSY error is encountered when processing one or more of the
7309 ** attached WAL databases, the operation is still attempted on any remaining
7310 ** attached databases and SQLITE_BUSY is returned to the caller. If any other
7311 ** error occurs while processing an attached database, processing is abandoned
7312 ** and the error code returned to the caller immediately. If no error
7313 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
7314 ** databases, SQLITE_OK is returned.
7315 **
7316 ** If database zDb is the name of an attached database that is not in WAL
7317 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
7318 ** zDb is not NULL (or a zero length string) and is not the name of any
7319 ** attached database, SQLITE_ERROR is returned to the caller.
7320 */
7321 SQLITE_API int sqlite3_wal_checkpoint_v2(
7322   sqlite3 *db,                    /* Database handle */
7323   const char *zDb,                /* Name of attached database (or NULL) */
7324   int eMode,                      /* SQLITE_CHECKPOINT_* value */
7325   int *pnLog,                     /* OUT: Size of WAL log in frames */
7326   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
7327 );
7328 
7329 /*
7330 ** CAPI3REF: Checkpoint operation parameters
7331 **
7332 ** These constants can be used as the 3rd parameter to
7333 ** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
7334 ** documentation for additional information about the meaning and use of
7335 ** each of these values.
7336 */
7337 #define SQLITE_CHECKPOINT_PASSIVE 0
7338 #define SQLITE_CHECKPOINT_FULL    1
7339 #define SQLITE_CHECKPOINT_RESTART 2
7340 
7341 /*
7342 ** CAPI3REF: Virtual Table Interface Configuration
7343 **
7344 ** This function may be called by either the [xConnect] or [xCreate] method
7345 ** of a [virtual table] implementation to configure
7346 ** various facets of the virtual table interface.
7347 **
7348 ** If this interface is invoked outside the context of an xConnect or
7349 ** xCreate virtual table method then the behavior is undefined.
7350 **
7351 ** At present, there is only one option that may be configured using
7352 ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
7353 ** may be added in the future.
7354 */
7355 SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
7356 
7357 /*
7358 ** CAPI3REF: Virtual Table Configuration Options
7359 **
7360 ** These macros define the various options to the
7361 ** [sqlite3_vtab_config()] interface that [virtual table] implementations
7362 ** can use to customize and optimize their behavior.
7363 **
7364 ** <dl>
7365 ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
7366 ** <dd>Calls of the form
7367 ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
7368 ** where X is an integer.  If X is zero, then the [virtual table] whose
7369 ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
7370 ** support constraints.  In this configuration (which is the default) if
7371 ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
7372 ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
7373 ** specified as part of the users SQL statement, regardless of the actual
7374 ** ON CONFLICT mode specified.
7375 **
7376 ** If X is non-zero, then the virtual table implementation guarantees
7377 ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
7378 ** any modifications to internal or persistent data structures have been made.
7379 ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
7380 ** is able to roll back a statement or database transaction, and abandon
7381 ** or continue processing the current SQL statement as appropriate.
7382 ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
7383 ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
7384 ** had been ABORT.
7385 **
7386 ** Virtual table implementations that are required to handle OR REPLACE
7387 ** must do so within the [xUpdate] method. If a call to the
7388 ** [sqlite3_vtab_on_conflict()] function indicates that the current ON
7389 ** CONFLICT policy is REPLACE, the virtual table implementation should
7390 ** silently replace the appropriate rows within the xUpdate callback and
7391 ** return SQLITE_OK. Or, if this is not possible, it may return
7392 ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
7393 ** constraint handling.
7394 ** </dl>
7395 */
7396 #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
7397 
7398 /*
7399 ** CAPI3REF: Determine The Virtual Table Conflict Policy
7400 **
7401 ** This function may only be called from within a call to the [xUpdate] method
7402 ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
7403 ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
7404 ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
7405 ** of the SQL statement that triggered the call to the [xUpdate] method of the
7406 ** [virtual table].
7407 */
7408 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7409 
7410 /*
7411 ** CAPI3REF: Conflict resolution modes
7412 **
7413 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
7414 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
7415 ** is for the SQL statement being evaluated.
7416 **
7417 ** Note that the [SQLITE_IGNORE] constant is also used as a potential
7418 ** return value from the [sqlite3_set_authorizer()] callback and that
7419 ** [SQLITE_ABORT] is also a [result code].
7420 */
7421 #define SQLITE_ROLLBACK 1
7422 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7423 #define SQLITE_FAIL     3
7424 /* #define SQLITE_ABORT 4  // Also an error code */
7425 #define SQLITE_REPLACE  5
7426 
7427 
7428 
7429 /*
7430 ** Undo the hack that converts floating point types to integer for
7431 ** builds on processors without floating point support.
7432 */
7433 #ifdef SQLITE_OMIT_FLOATING_POINT
7434 # undef double
7435 #endif
7436 
7437 #if 0
7438 }  /* End of the 'extern "C"' block */
7439 #endif
7440 #endif /* _SQLITE3_H_ */
7441 
7442 /*
7443 ** 2010 August 30
7444 **
7445 ** The author disclaims copyright to this source code.  In place of
7446 ** a legal notice, here is a blessing:
7447 **
7448 **    May you do good and not evil.
7449 **    May you find forgiveness for yourself and forgive others.
7450 **    May you share freely, never taking more than you give.
7451 **
7452 *************************************************************************
7453 */
7454 
7455 #ifndef _SQLITE3RTREE_H_
7456 #define _SQLITE3RTREE_H_
7457 
7458 
7459 #if 0
7460 extern "C" {
7461 #endif
7462 
7463 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7464 
7465 /*
7466 ** Register a geometry callback named zGeom that can be used as part of an
7467 ** R-Tree geometry query as follows:
7468 **
7469 **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7470 */
7471 SQLITE_API int sqlite3_rtree_geometry_callback(
7472   sqlite3 *db,
7473   const char *zGeom,
7474 #ifdef SQLITE_RTREE_INT_ONLY
7475   int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7476 #else
7477   int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7478 #endif
7479   void *pContext
7480 );
7481 
7482 
7483 /*
7484 ** A pointer to a structure of the following type is passed as the first
7485 ** argument to callbacks registered using rtree_geometry_callback().
7486 */
7487 struct sqlite3_rtree_geometry {
7488   void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
7489   int nParam;                     /* Size of array aParam[] */
7490   double *aParam;                 /* Parameters passed to SQL geom function */
7491   void *pUser;                    /* Callback implementation user data */
7492   void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
7493 };
7494 
7495 
7496 #if 0
7497 }  /* end of the 'extern "C"' block */
7498 #endif
7499 
7500 #endif  /* ifndef _SQLITE3RTREE_H_ */
7501 
7502 
7503 /************** End of sqlite3.h *********************************************/
7504 /************** Continuing where we left off in sqliteInt.h ******************/
7505 
7506 /*
7507 ** Include the configuration header output by 'configure' if we're using the
7508 ** autoconf-based build
7509 */
7510 #ifdef _HAVE_SQLITE_CONFIG_H
7511 #include "config.h"
7512 #endif
7513 
7514 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
7515 /************** Begin file sqliteLimit.h *************************************/
7516 /*
7517 ** 2007 May 7
7518 **
7519 ** The author disclaims copyright to this source code.  In place of
7520 ** a legal notice, here is a blessing:
7521 **
7522 **    May you do good and not evil.
7523 **    May you find forgiveness for yourself and forgive others.
7524 **    May you share freely, never taking more than you give.
7525 **
7526 *************************************************************************
7527 **
7528 ** This file defines various limits of what SQLite can process.
7529 */
7530 
7531 /*
7532 ** The maximum length of a TEXT or BLOB in bytes.   This also
7533 ** limits the size of a row in a table or index.
7534 **
7535 ** The hard limit is the ability of a 32-bit signed integer
7536 ** to count the size: 2^31-1 or 2147483647.
7537 */
7538 #ifndef SQLITE_MAX_LENGTH
7539 # define SQLITE_MAX_LENGTH 1000000000
7540 #endif
7541 
7542 /*
7543 ** This is the maximum number of
7544 **
7545 **    * Columns in a table
7546 **    * Columns in an index
7547 **    * Columns in a view
7548 **    * Terms in the SET clause of an UPDATE statement
7549 **    * Terms in the result set of a SELECT statement
7550 **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
7551 **    * Terms in the VALUES clause of an INSERT statement
7552 **
7553 ** The hard upper limit here is 32676.  Most database people will
7554 ** tell you that in a well-normalized database, you usually should
7555 ** not have more than a dozen or so columns in any table.  And if
7556 ** that is the case, there is no point in having more than a few
7557 ** dozen values in any of the other situations described above.
7558 */
7559 #ifndef SQLITE_MAX_COLUMN
7560 # define SQLITE_MAX_COLUMN 2000
7561 #endif
7562 
7563 /*
7564 ** The maximum length of a single SQL statement in bytes.
7565 **
7566 ** It used to be the case that setting this value to zero would
7567 ** turn the limit off.  That is no longer true.  It is not possible
7568 ** to turn this limit off.
7569 */
7570 #ifndef SQLITE_MAX_SQL_LENGTH
7571 # define SQLITE_MAX_SQL_LENGTH 1000000000
7572 #endif
7573 
7574 /*
7575 ** The maximum depth of an expression tree. This is limited to
7576 ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
7577 ** want to place more severe limits on the complexity of an
7578 ** expression.
7579 **
7580 ** A value of 0 used to mean that the limit was not enforced.
7581 ** But that is no longer true.  The limit is now strictly enforced
7582 ** at all times.
7583 */
7584 #ifndef SQLITE_MAX_EXPR_DEPTH
7585 # define SQLITE_MAX_EXPR_DEPTH 1000
7586 #endif
7587 
7588 /*
7589 ** The maximum number of terms in a compound SELECT statement.
7590 ** The code generator for compound SELECT statements does one
7591 ** level of recursion for each term.  A stack overflow can result
7592 ** if the number of terms is too large.  In practice, most SQL
7593 ** never has more than 3 or 4 terms.  Use a value of 0 to disable
7594 ** any limit on the number of terms in a compount SELECT.
7595 */
7596 #ifndef SQLITE_MAX_COMPOUND_SELECT
7597 # define SQLITE_MAX_COMPOUND_SELECT 500
7598 #endif
7599 
7600 /*
7601 ** The maximum number of opcodes in a VDBE program.
7602 ** Not currently enforced.
7603 */
7604 #ifndef SQLITE_MAX_VDBE_OP
7605 # define SQLITE_MAX_VDBE_OP 25000
7606 #endif
7607 
7608 /*
7609 ** The maximum number of arguments to an SQL function.
7610 */
7611 #ifndef SQLITE_MAX_FUNCTION_ARG
7612 # define SQLITE_MAX_FUNCTION_ARG 127
7613 #endif
7614 
7615 /*
7616 ** The maximum number of in-memory pages to use for the main database
7617 ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
7618 */
7619 #ifndef SQLITE_DEFAULT_CACHE_SIZE
7620 # define SQLITE_DEFAULT_CACHE_SIZE  2000
7621 #endif
7622 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
7623 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
7624 #endif
7625 
7626 /*
7627 ** The default number of frames to accumulate in the log file before
7628 ** checkpointing the database in WAL mode.
7629 */
7630 #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
7631 # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
7632 #endif
7633 
7634 /*
7635 ** The maximum number of attached databases.  This must be between 0
7636 ** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
7637 ** is used internally to track attached databases.
7638 */
7639 #ifndef SQLITE_MAX_ATTACHED
7640 # define SQLITE_MAX_ATTACHED 10
7641 #endif
7642 
7643 
7644 /*
7645 ** The maximum value of a ?nnn wildcard that the parser will accept.
7646 */
7647 #ifndef SQLITE_MAX_VARIABLE_NUMBER
7648 # define SQLITE_MAX_VARIABLE_NUMBER 999
7649 #endif
7650 
7651 /* Maximum page size.  The upper bound on this value is 65536.  This a limit
7652 ** imposed by the use of 16-bit offsets within each page.
7653 **
7654 ** Earlier versions of SQLite allowed the user to change this value at
7655 ** compile time. This is no longer permitted, on the grounds that it creates
7656 ** a library that is technically incompatible with an SQLite library
7657 ** compiled with a different limit. If a process operating on a database
7658 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
7659 ** compiled with the default page-size limit will not be able to rollback
7660 ** the aborted transaction. This could lead to database corruption.
7661 */
7662 #ifdef SQLITE_MAX_PAGE_SIZE
7663 # undef SQLITE_MAX_PAGE_SIZE
7664 #endif
7665 #define SQLITE_MAX_PAGE_SIZE 65536
7666 
7667 
7668 /*
7669 ** The default size of a database page.
7670 */
7671 #ifndef SQLITE_DEFAULT_PAGE_SIZE
7672 # define SQLITE_DEFAULT_PAGE_SIZE 1024
7673 #endif
7674 #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
7675 # undef SQLITE_DEFAULT_PAGE_SIZE
7676 # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
7677 #endif
7678 
7679 /*
7680 ** Ordinarily, if no value is explicitly provided, SQLite creates databases
7681 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
7682 ** device characteristics (sector-size and atomic write() support),
7683 ** SQLite may choose a larger value. This constant is the maximum value
7684 ** SQLite will choose on its own.
7685 */
7686 #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
7687 # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
7688 #endif
7689 #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
7690 # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
7691 # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
7692 #endif
7693 
7694 
7695 /*
7696 ** Maximum number of pages in one database file.
7697 **
7698 ** This is really just the default value for the max_page_count pragma.
7699 ** This value can be lowered (or raised) at run-time using that the
7700 ** max_page_count macro.
7701 */
7702 #ifndef SQLITE_MAX_PAGE_COUNT
7703 # define SQLITE_MAX_PAGE_COUNT 1073741823
7704 #endif
7705 
7706 /*
7707 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
7708 ** operator.
7709 */
7710 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
7711 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
7712 #endif
7713 
7714 /*
7715 ** Maximum depth of recursion for triggers.
7716 **
7717 ** A value of 1 means that a trigger program will not be able to itself
7718 ** fire any triggers. A value of 0 means that no trigger programs at all
7719 ** may be executed.
7720 */
7721 #ifndef SQLITE_MAX_TRIGGER_DEPTH
7722 # define SQLITE_MAX_TRIGGER_DEPTH 1000
7723 #endif
7724 
7725 /************** End of sqliteLimit.h *****************************************/
7726 /************** Continuing where we left off in sqliteInt.h ******************/
7727 
7728 /* Disable nuisance warnings on Borland compilers */
7729 #if defined(__BORLANDC__)
7730 #pragma warn -rch /* unreachable code */
7731 #pragma warn -ccc /* Condition is always true or false */
7732 #pragma warn -aus /* Assigned value is never used */
7733 #pragma warn -csu /* Comparing signed and unsigned */
7734 #pragma warn -spa /* Suspicious pointer arithmetic */
7735 #endif
7736 
7737 /* Needed for various definitions... */
7738 #ifndef _GNU_SOURCE
7739 # define _GNU_SOURCE
7740 #endif
7741 
7742 #if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
7743 # define _BSD_SOURCE
7744 #endif
7745 
7746 /*
7747 ** Include standard header files as necessary
7748 */
7749 #ifdef HAVE_STDINT_H
7750 #include <stdint.h>
7751 #endif
7752 #ifdef HAVE_INTTYPES_H
7753 #include <inttypes.h>
7754 #endif
7755 
7756 /*
7757 ** The following macros are used to cast pointers to integers and
7758 ** integers to pointers.  The way you do this varies from one compiler
7759 ** to the next, so we have developed the following set of #if statements
7760 ** to generate appropriate macros for a wide range of compilers.
7761 **
7762 ** The correct "ANSI" way to do this is to use the intptr_t type.
7763 ** Unfortunately, that typedef is not available on all compilers, or
7764 ** if it is available, it requires an #include of specific headers
7765 ** that vary from one machine to the next.
7766 **
7767 ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
7768 ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
7769 ** So we have to define the macros in different ways depending on the
7770 ** compiler.
7771 */
7772 #if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
7773 # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
7774 # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
7775 #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
7776 # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
7777 # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
7778 #elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
7779 # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
7780 # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
7781 #else                          /* Generates a warning - but it always works */
7782 # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
7783 # define SQLITE_PTR_TO_INT(X)  ((int)(X))
7784 #endif
7785 
7786 /*
7787 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
7788 ** 0 means mutexes are permanently disable and the library is never
7789 ** threadsafe.  1 means the library is serialized which is the highest
7790 ** level of threadsafety.  2 means the library is multithreaded - multiple
7791 ** threads can use SQLite as long as no two threads try to use the same
7792 ** database connection at the same time.
7793 **
7794 ** Older versions of SQLite used an optional THREADSAFE macro.
7795 ** We support that for legacy.
7796 */
7797 #if !defined(SQLITE_THREADSAFE)
7798 # if defined(THREADSAFE)
7799 #   define SQLITE_THREADSAFE THREADSAFE
7800 # else
7801 #   define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
7802 # endif
7803 #endif
7804 
7805 /*
7806 ** Powersafe overwrite is on by default.  But can be turned off using
7807 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
7808 */
7809 #ifndef SQLITE_POWERSAFE_OVERWRITE
7810 # define SQLITE_POWERSAFE_OVERWRITE 1
7811 #endif
7812 
7813 /*
7814 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
7815 ** It determines whether or not the features related to
7816 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
7817 ** be overridden at runtime using the sqlite3_config() API.
7818 */
7819 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
7820 # define SQLITE_DEFAULT_MEMSTATUS 1
7821 #endif
7822 
7823 /*
7824 ** Exactly one of the following macros must be defined in order to
7825 ** specify which memory allocation subsystem to use.
7826 **
7827 **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
7828 **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
7829 **     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
7830 **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
7831 **
7832 ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
7833 ** assert() macro is enabled, each call into the Win32 native heap subsystem
7834 ** will cause HeapValidate to be called.  If heap validation should fail, an
7835 ** assertion will be triggered.
7836 **
7837 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
7838 ** the default.
7839 */
7840 #if defined(SQLITE_SYSTEM_MALLOC) \
7841   + defined(SQLITE_WIN32_MALLOC) \
7842   + defined(SQLITE_ZERO_MALLOC) \
7843   + defined(SQLITE_MEMDEBUG)>1
7844 # error "Two or more of the following compile-time configuration options\
7845  are defined but at most one is allowed:\
7846  SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
7847  SQLITE_ZERO_MALLOC"
7848 #endif
7849 #if defined(SQLITE_SYSTEM_MALLOC) \
7850   + defined(SQLITE_WIN32_MALLOC) \
7851   + defined(SQLITE_ZERO_MALLOC) \
7852   + defined(SQLITE_MEMDEBUG)==0
7853 # define SQLITE_SYSTEM_MALLOC 1
7854 #endif
7855 
7856 /*
7857 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
7858 ** sizes of memory allocations below this value where possible.
7859 */
7860 #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
7861 # define SQLITE_MALLOC_SOFT_LIMIT 1024
7862 #endif
7863 
7864 /*
7865 ** We need to define _XOPEN_SOURCE as follows in order to enable
7866 ** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
7867 ** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
7868 ** it.
7869 */
7870 #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
7871 #  define _XOPEN_SOURCE 600
7872 #endif
7873 
7874 /*
7875 ** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
7876 ** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
7877 ** make it true by defining or undefining NDEBUG.
7878 **
7879 ** Setting NDEBUG makes the code smaller and faster by disabling the
7880 ** assert() statements in the code.  So we want the default action
7881 ** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
7882 ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
7883 ** feature.
7884 */
7885 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
7886 # define NDEBUG 1
7887 #endif
7888 #if defined(NDEBUG) && defined(SQLITE_DEBUG)
7889 # undef NDEBUG
7890 #endif
7891 
7892 /*
7893 ** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
7894 */
7895 #if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
7896 # define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
7897 #endif
7898 
7899 /*
7900 ** The testcase() macro is used to aid in coverage testing.  When
7901 ** doing coverage testing, the condition inside the argument to
7902 ** testcase() must be evaluated both true and false in order to
7903 ** get full branch coverage.  The testcase() macro is inserted
7904 ** to help ensure adequate test coverage in places where simple
7905 ** condition/decision coverage is inadequate.  For example, testcase()
7906 ** can be used to make sure boundary values are tested.  For
7907 ** bitmask tests, testcase() can be used to make sure each bit
7908 ** is significant and used at least once.  On switch statements
7909 ** where multiple cases go to the same block of code, testcase()
7910 ** can insure that all cases are evaluated.
7911 **
7912 */
7913 #ifdef SQLITE_COVERAGE_TEST
7914 SQLITE_PRIVATE   void sqlite3Coverage(int);
7915 # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
7916 #else
7917 # define testcase(X)
7918 #endif
7919 
7920 /*
7921 ** The TESTONLY macro is used to enclose variable declarations or
7922 ** other bits of code that are needed to support the arguments
7923 ** within testcase() and assert() macros.
7924 */
7925 #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
7926 # define TESTONLY(X)  X
7927 #else
7928 # define TESTONLY(X)
7929 #endif
7930 
7931 /*
7932 ** Sometimes we need a small amount of code such as a variable initialization
7933 ** to setup for a later assert() statement.  We do not want this code to
7934 ** appear when assert() is disabled.  The following macro is therefore
7935 ** used to contain that setup code.  The "VVA" acronym stands for
7936 ** "Verification, Validation, and Accreditation".  In other words, the
7937 ** code within VVA_ONLY() will only run during verification processes.
7938 */
7939 #ifndef NDEBUG
7940 # define VVA_ONLY(X)  X
7941 #else
7942 # define VVA_ONLY(X)
7943 #endif
7944 
7945 /*
7946 ** The ALWAYS and NEVER macros surround boolean expressions which
7947 ** are intended to always be true or false, respectively.  Such
7948 ** expressions could be omitted from the code completely.  But they
7949 ** are included in a few cases in order to enhance the resilience
7950 ** of SQLite to unexpected behavior - to make the code "self-healing"
7951 ** or "ductile" rather than being "brittle" and crashing at the first
7952 ** hint of unplanned behavior.
7953 **
7954 ** In other words, ALWAYS and NEVER are added for defensive code.
7955 **
7956 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
7957 ** be true and false so that the unreachable code they specify will
7958 ** not be counted as untested code.
7959 */
7960 #if defined(SQLITE_COVERAGE_TEST)
7961 # define ALWAYS(X)      (1)
7962 # define NEVER(X)       (0)
7963 #elif !defined(NDEBUG)
7964 # define ALWAYS(X)      ((X)?1:(assert(0),0))
7965 # define NEVER(X)       ((X)?(assert(0),1):0)
7966 #else
7967 # define ALWAYS(X)      (X)
7968 # define NEVER(X)       (X)
7969 #endif
7970 
7971 /*
7972 ** Return true (non-zero) if the input is a integer that is too large
7973 ** to fit in 32-bits.  This macro is used inside of various testcase()
7974 ** macros to verify that we have tested SQLite for large-file support.
7975 */
7976 #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
7977 
7978 /*
7979 ** The macro unlikely() is a hint that surrounds a boolean
7980 ** expression that is usually false.  Macro likely() surrounds
7981 ** a boolean expression that is usually true.  These hints could,
7982 ** in theory, be used by the compiler to generate better code, but
7983 ** currently they are just comments for human readers.
7984 */
7985 #define likely(X)    (X)
7986 #define unlikely(X)  (X)
7987 
7988 /************** Include hash.h in the middle of sqliteInt.h ******************/
7989 /************** Begin file hash.h ********************************************/
7990 /*
7991 ** 2001 September 22
7992 **
7993 ** The author disclaims copyright to this source code.  In place of
7994 ** a legal notice, here is a blessing:
7995 **
7996 **    May you do good and not evil.
7997 **    May you find forgiveness for yourself and forgive others.
7998 **    May you share freely, never taking more than you give.
7999 **
8000 *************************************************************************
8001 ** This is the header file for the generic hash-table implementation
8002 ** used in SQLite.
8003 */
8004 #ifndef _SQLITE_HASH_H_
8005 #define _SQLITE_HASH_H_
8006 
8007 /* Forward declarations of structures. */
8008 typedef struct Hash Hash;
8009 typedef struct HashElem HashElem;
8010 
8011 /* A complete hash table is an instance of the following structure.
8012 ** The internals of this structure are intended to be opaque -- client
8013 ** code should not attempt to access or modify the fields of this structure
8014 ** directly.  Change this structure only by using the routines below.
8015 ** However, some of the "procedures" and "functions" for modifying and
8016 ** accessing this structure are really macros, so we can't really make
8017 ** this structure opaque.
8018 **
8019 ** All elements of the hash table are on a single doubly-linked list.
8020 ** Hash.first points to the head of this list.
8021 **
8022 ** There are Hash.htsize buckets.  Each bucket points to a spot in
8023 ** the global doubly-linked list.  The contents of the bucket are the
8024 ** element pointed to plus the next _ht.count-1 elements in the list.
8025 **
8026 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
8027 ** by a linear search of the global list.  For small tables, the
8028 ** Hash.ht table is never allocated because if there are few elements
8029 ** in the table, it is faster to do a linear search than to manage
8030 ** the hash table.
8031 */
8032 struct Hash {
8033   unsigned int htsize;      /* Number of buckets in the hash table */
8034   unsigned int count;       /* Number of entries in this table */
8035   HashElem *first;          /* The first element of the array */
8036   struct _ht {              /* the hash table */
8037     int count;                 /* Number of entries with this hash */
8038     HashElem *chain;           /* Pointer to first entry with this hash */
8039   } *ht;
8040 };
8041 
8042 /* Each element in the hash table is an instance of the following
8043 ** structure.  All elements are stored on a single doubly-linked list.
8044 **
8045 ** Again, this structure is intended to be opaque, but it can't really
8046 ** be opaque because it is used by macros.
8047 */
8048 struct HashElem {
8049   HashElem *next, *prev;       /* Next and previous elements in the table */
8050   void *data;                  /* Data associated with this element */
8051   const char *pKey; int nKey;  /* Key associated with this element */
8052 };
8053 
8054 /*
8055 ** Access routines.  To delete, insert a NULL pointer.
8056 */
8057 SQLITE_PRIVATE void sqlite3HashInit(Hash*);
8058 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
8059 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
8060 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
8061 
8062 /*
8063 ** Macros for looping over all elements of a hash table.  The idiom is
8064 ** like this:
8065 **
8066 **   Hash h;
8067 **   HashElem *p;
8068 **   ...
8069 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
8070 **     SomeStructure *pData = sqliteHashData(p);
8071 **     // do something with pData
8072 **   }
8073 */
8074 #define sqliteHashFirst(H)  ((H)->first)
8075 #define sqliteHashNext(E)   ((E)->next)
8076 #define sqliteHashData(E)   ((E)->data)
8077 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
8078 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
8079 
8080 /*
8081 ** Number of entries in a hash table
8082 */
8083 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
8084 
8085 #endif /* _SQLITE_HASH_H_ */
8086 
8087 /************** End of hash.h ************************************************/
8088 /************** Continuing where we left off in sqliteInt.h ******************/
8089 /************** Include parse.h in the middle of sqliteInt.h *****************/
8090 /************** Begin file parse.h *******************************************/
8091 #define TK_SEMI                             1
8092 #define TK_EXPLAIN                          2
8093 #define TK_QUERY                            3
8094 #define TK_PLAN                             4
8095 #define TK_BEGIN                            5
8096 #define TK_TRANSACTION                      6
8097 #define TK_DEFERRED                         7
8098 #define TK_IMMEDIATE                        8
8099 #define TK_EXCLUSIVE                        9
8100 #define TK_COMMIT                          10
8101 #define TK_END                             11
8102 #define TK_ROLLBACK                        12
8103 #define TK_SAVEPOINT                       13
8104 #define TK_RELEASE                         14
8105 #define TK_TO                              15
8106 #define TK_TABLE                           16
8107 #define TK_CREATE                          17
8108 #define TK_IF                              18
8109 #define TK_NOT                             19
8110 #define TK_EXISTS                          20
8111 #define TK_TEMP                            21
8112 #define TK_LP                              22
8113 #define TK_RP                              23
8114 #define TK_AS                              24
8115 #define TK_WITHOUT                         25
8116 #define TK_COMMA                           26
8117 #define TK_ID                              27
8118 #define TK_INDEXED                         28
8119 #define TK_ABORT                           29
8120 #define TK_ACTION                          30
8121 #define TK_AFTER                           31
8122 #define TK_ANALYZE                         32
8123 #define TK_ASC                             33
8124 #define TK_ATTACH                          34
8125 #define TK_BEFORE                          35
8126 #define TK_BY                              36
8127 #define TK_CASCADE                         37
8128 #define TK_CAST                            38
8129 #define TK_COLUMNKW                        39
8130 #define TK_CONFLICT                        40
8131 #define TK_DATABASE                        41
8132 #define TK_DESC                            42
8133 #define TK_DETACH                          43
8134 #define TK_EACH                            44
8135 #define TK_FAIL                            45
8136 #define TK_FOR                             46
8137 #define TK_IGNORE                          47
8138 #define TK_INITIALLY                       48
8139 #define TK_INSTEAD                         49
8140 #define TK_LIKE_KW                         50
8141 #define TK_MATCH                           51
8142 #define TK_NO                              52
8143 #define TK_KEY                             53
8144 #define TK_OF                              54
8145 #define TK_OFFSET                          55
8146 #define TK_PRAGMA                          56
8147 #define TK_RAISE                           57
8148 #define TK_RECURSIVE                       58
8149 #define TK_REPLACE                         59
8150 #define TK_RESTRICT                        60
8151 #define TK_ROW                             61
8152 #define TK_TRIGGER                         62
8153 #define TK_VACUUM                          63
8154 #define TK_VIEW                            64
8155 #define TK_VIRTUAL                         65
8156 #define TK_WITH                            66
8157 #define TK_REINDEX                         67
8158 #define TK_RENAME                          68
8159 #define TK_CTIME_KW                        69
8160 #define TK_ANY                             70
8161 #define TK_OR                              71
8162 #define TK_AND                             72
8163 #define TK_IS                              73
8164 #define TK_BETWEEN                         74
8165 #define TK_IN                              75
8166 #define TK_ISNULL                          76
8167 #define TK_NOTNULL                         77
8168 #define TK_NE                              78
8169 #define TK_EQ                              79
8170 #define TK_GT                              80
8171 #define TK_LE                              81
8172 #define TK_LT                              82
8173 #define TK_GE                              83
8174 #define TK_ESCAPE                          84
8175 #define TK_BITAND                          85
8176 #define TK_BITOR                           86
8177 #define TK_LSHIFT                          87
8178 #define TK_RSHIFT                          88
8179 #define TK_PLUS                            89
8180 #define TK_MINUS                           90
8181 #define TK_STAR                            91
8182 #define TK_SLASH                           92
8183 #define TK_REM                             93
8184 #define TK_CONCAT                          94
8185 #define TK_COLLATE                         95
8186 #define TK_BITNOT                          96
8187 #define TK_STRING                          97
8188 #define TK_JOIN_KW                         98
8189 #define TK_CONSTRAINT                      99
8190 #define TK_DEFAULT                        100
8191 #define TK_NULL                           101
8192 #define TK_PRIMARY                        102
8193 #define TK_UNIQUE                         103
8194 #define TK_CHECK                          104
8195 #define TK_REFERENCES                     105
8196 #define TK_AUTOINCR                       106
8197 #define TK_ON                             107
8198 #define TK_INSERT                         108
8199 #define TK_DELETE                         109
8200 #define TK_UPDATE                         110
8201 #define TK_SET                            111
8202 #define TK_DEFERRABLE                     112
8203 #define TK_FOREIGN                        113
8204 #define TK_DROP                           114
8205 #define TK_UNION                          115
8206 #define TK_ALL                            116
8207 #define TK_EXCEPT                         117
8208 #define TK_INTERSECT                      118
8209 #define TK_SELECT                         119
8210 #define TK_VALUES                         120
8211 #define TK_DISTINCT                       121
8212 #define TK_DOT                            122
8213 #define TK_FROM                           123
8214 #define TK_JOIN                           124
8215 #define TK_USING                          125
8216 #define TK_ORDER                          126
8217 #define TK_GROUP                          127
8218 #define TK_HAVING                         128
8219 #define TK_LIMIT                          129
8220 #define TK_WHERE                          130
8221 #define TK_INTO                           131
8222 #define TK_INTEGER                        132
8223 #define TK_FLOAT                          133
8224 #define TK_BLOB                           134
8225 #define TK_VARIABLE                       135
8226 #define TK_CASE                           136
8227 #define TK_WHEN                           137
8228 #define TK_THEN                           138
8229 #define TK_ELSE                           139
8230 #define TK_INDEX                          140
8231 #define TK_ALTER                          141
8232 #define TK_ADD                            142
8233 #define TK_TO_TEXT                        143
8234 #define TK_TO_BLOB                        144
8235 #define TK_TO_NUMERIC                     145
8236 #define TK_TO_INT                         146
8237 #define TK_TO_REAL                        147
8238 #define TK_ISNOT                          148
8239 #define TK_END_OF_FILE                    149
8240 #define TK_ILLEGAL                        150
8241 #define TK_SPACE                          151
8242 #define TK_UNCLOSED_STRING                152
8243 #define TK_FUNCTION                       153
8244 #define TK_COLUMN                         154
8245 #define TK_AGG_FUNCTION                   155
8246 #define TK_AGG_COLUMN                     156
8247 #define TK_UMINUS                         157
8248 #define TK_UPLUS                          158
8249 #define TK_REGISTER                       159
8250 
8251 /************** End of parse.h ***********************************************/
8252 /************** Continuing where we left off in sqliteInt.h ******************/
8253 #include <stdio.h>
8254 #include <stdlib.h>
8255 #include <string.h>
8256 #include <assert.h>
8257 #include <stddef.h>
8258 
8259 /*
8260 ** If compiling for a processor that lacks floating point support,
8261 ** substitute integer for floating-point
8262 */
8263 #ifdef SQLITE_OMIT_FLOATING_POINT
8264 # define double sqlite_int64
8265 # define float sqlite_int64
8266 # define LONGDOUBLE_TYPE sqlite_int64
8267 # ifndef SQLITE_BIG_DBL
8268 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
8269 # endif
8270 # define SQLITE_OMIT_DATETIME_FUNCS 1
8271 # define SQLITE_OMIT_TRACE 1
8272 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
8273 # undef SQLITE_HAVE_ISNAN
8274 #endif
8275 #ifndef SQLITE_BIG_DBL
8276 # define SQLITE_BIG_DBL (1e99)
8277 #endif
8278 
8279 /*
8280 ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
8281 ** afterward. Having this macro allows us to cause the C compiler
8282 ** to omit code used by TEMP tables without messy #ifndef statements.
8283 */
8284 #ifdef SQLITE_OMIT_TEMPDB
8285 #define OMIT_TEMPDB 1
8286 #else
8287 #define OMIT_TEMPDB 0
8288 #endif
8289 
8290 /*
8291 ** The "file format" number is an integer that is incremented whenever
8292 ** the VDBE-level file format changes.  The following macros define the
8293 ** the default file format for new databases and the maximum file format
8294 ** that the library can read.
8295 */
8296 #define SQLITE_MAX_FILE_FORMAT 4
8297 #ifndef SQLITE_DEFAULT_FILE_FORMAT
8298 # define SQLITE_DEFAULT_FILE_FORMAT 4
8299 #endif
8300 
8301 /*
8302 ** Determine whether triggers are recursive by default.  This can be
8303 ** changed at run-time using a pragma.
8304 */
8305 #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
8306 # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
8307 #endif
8308 
8309 /*
8310 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
8311 ** on the command-line
8312 */
8313 #ifndef SQLITE_TEMP_STORE
8314 # define SQLITE_TEMP_STORE 1
8315 # define SQLITE_TEMP_STORE_xc 1  /* Exclude from ctime.c */
8316 #endif
8317 
8318 /*
8319 ** GCC does not define the offsetof() macro so we'll have to do it
8320 ** ourselves.
8321 */
8322 #ifndef offsetof
8323 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
8324 #endif
8325 
8326 /*
8327 ** Macros to compute minimum and maximum of two numbers.
8328 */
8329 #define MIN(A,B) ((A)<(B)?(A):(B))
8330 #define MAX(A,B) ((A)>(B)?(A):(B))
8331 
8332 /*
8333 ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
8334 ** not, there are still machines out there that use EBCDIC.)
8335 */
8336 #if 'A' == '\301'
8337 # define SQLITE_EBCDIC 1
8338 #else
8339 # define SQLITE_ASCII 1
8340 #endif
8341 
8342 /*
8343 ** Integers of known sizes.  These typedefs might change for architectures
8344 ** where the sizes very.  Preprocessor macros are available so that the
8345 ** types can be conveniently redefined at compile-type.  Like this:
8346 **
8347 **         cc '-DUINTPTR_TYPE=long long int' ...
8348 */
8349 #ifndef UINT32_TYPE
8350 # ifdef HAVE_UINT32_T
8351 #  define UINT32_TYPE uint32_t
8352 # else
8353 #  define UINT32_TYPE unsigned int
8354 # endif
8355 #endif
8356 #ifndef UINT16_TYPE
8357 # ifdef HAVE_UINT16_T
8358 #  define UINT16_TYPE uint16_t
8359 # else
8360 #  define UINT16_TYPE unsigned short int
8361 # endif
8362 #endif
8363 #ifndef INT16_TYPE
8364 # ifdef HAVE_INT16_T
8365 #  define INT16_TYPE int16_t
8366 # else
8367 #  define INT16_TYPE short int
8368 # endif
8369 #endif
8370 #ifndef UINT8_TYPE
8371 # ifdef HAVE_UINT8_T
8372 #  define UINT8_TYPE uint8_t
8373 # else
8374 #  define UINT8_TYPE unsigned char
8375 # endif
8376 #endif
8377 #ifndef INT8_TYPE
8378 # ifdef HAVE_INT8_T
8379 #  define INT8_TYPE int8_t
8380 # else
8381 #  define INT8_TYPE signed char
8382 # endif
8383 #endif
8384 #ifndef LONGDOUBLE_TYPE
8385 # define LONGDOUBLE_TYPE long double
8386 #endif
8387 typedef sqlite_int64 i64;          /* 8-byte signed integer */
8388 typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
8389 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
8390 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
8391 typedef INT16_TYPE i16;            /* 2-byte signed integer */
8392 typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
8393 typedef INT8_TYPE i8;              /* 1-byte signed integer */
8394 
8395 /*
8396 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
8397 ** that can be stored in a u32 without loss of data.  The value
8398 ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
8399 ** have to specify the value in the less intuitive manner shown:
8400 */
8401 #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
8402 
8403 /*
8404 ** The datatype used to store estimates of the number of rows in a
8405 ** table or index.  This is an unsigned integer type.  For 99.9% of
8406 ** the world, a 32-bit integer is sufficient.  But a 64-bit integer
8407 ** can be used at compile-time if desired.
8408 */
8409 #ifdef SQLITE_64BIT_STATS
8410  typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
8411 #else
8412  typedef u32 tRowcnt;    /* 32-bit is the default */
8413 #endif
8414 
8415 /*
8416 ** Estimated quantities used for query planning are stored as 16-bit
8417 ** logarithms.  For quantity X, the value stored is 10*log2(X).  This
8418 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
8419 ** But the allowed values are "grainy".  Not every value is representable.
8420 ** For example, quantities 16 and 17 are both represented by a LogEst
8421 ** of 40.  However, since LogEst quantatites are suppose to be estimates,
8422 ** not exact values, this imprecision is not a problem.
8423 **
8424 ** "LogEst" is short for "Logarithimic Estimate".
8425 **
8426 ** Examples:
8427 **      1 -> 0              20 -> 43          10000 -> 132
8428 **      2 -> 10             25 -> 46          25000 -> 146
8429 **      3 -> 16            100 -> 66        1000000 -> 199
8430 **      4 -> 20           1000 -> 99        1048576 -> 200
8431 **     10 -> 33           1024 -> 100    4294967296 -> 320
8432 **
8433 ** The LogEst can be negative to indicate fractional values.
8434 ** Examples:
8435 **
8436 **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
8437 */
8438 typedef INT16_TYPE LogEst;
8439 
8440 /*
8441 ** Macros to determine whether the machine is big or little endian,
8442 ** evaluated at runtime.
8443 */
8444 #ifdef SQLITE_AMALGAMATION
8445 SQLITE_PRIVATE const int sqlite3one = 1;
8446 #else
8447 SQLITE_PRIVATE const int sqlite3one;
8448 #endif
8449 #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
8450                              || defined(__x86_64) || defined(__x86_64__)
8451 # define SQLITE_BIGENDIAN    0
8452 # define SQLITE_LITTLEENDIAN 1
8453 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
8454 #else
8455 # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
8456 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
8457 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
8458 #endif
8459 
8460 /*
8461 ** Constants for the largest and smallest possible 64-bit signed integers.
8462 ** These macros are designed to work correctly on both 32-bit and 64-bit
8463 ** compilers.
8464 */
8465 #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
8466 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
8467 
8468 /*
8469 ** Round up a number to the next larger multiple of 8.  This is used
8470 ** to force 8-byte alignment on 64-bit architectures.
8471 */
8472 #define ROUND8(x)     (((x)+7)&~7)
8473 
8474 /*
8475 ** Round down to the nearest multiple of 8
8476 */
8477 #define ROUNDDOWN8(x) ((x)&~7)
8478 
8479 /*
8480 ** Assert that the pointer X is aligned to an 8-byte boundary.  This
8481 ** macro is used only within assert() to verify that the code gets
8482 ** all alignment restrictions correct.
8483 **
8484 ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
8485 ** underlying malloc() implemention might return us 4-byte aligned
8486 ** pointers.  In that case, only verify 4-byte alignment.
8487 */
8488 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
8489 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
8490 #else
8491 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
8492 #endif
8493 
8494 /*
8495 ** Disable MMAP on platforms where it is known to not work
8496 */
8497 #if defined(__OpenBSD__) || defined(__QNXNTO__)
8498 # undef SQLITE_MAX_MMAP_SIZE
8499 # define SQLITE_MAX_MMAP_SIZE 0
8500 #endif
8501 
8502 /*
8503 ** Default maximum size of memory used by memory-mapped I/O in the VFS
8504 */
8505 #ifdef __APPLE__
8506 # include <TargetConditionals.h>
8507 # if TARGET_OS_IPHONE
8508 #   undef SQLITE_MAX_MMAP_SIZE
8509 #   define SQLITE_MAX_MMAP_SIZE 0
8510 # endif
8511 #endif
8512 #ifndef SQLITE_MAX_MMAP_SIZE
8513 # if defined(__linux__) \
8514   || defined(_WIN32) \
8515   || (defined(__APPLE__) && defined(__MACH__)) \
8516   || defined(__sun)
8517 #   define SQLITE_MAX_MMAP_SIZE 0x7fff0000  /* 2147418112 */
8518 # else
8519 #   define SQLITE_MAX_MMAP_SIZE 0
8520 # endif
8521 # define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
8522 #endif
8523 
8524 /*
8525 ** The default MMAP_SIZE is zero on all platforms.  Or, even if a larger
8526 ** default MMAP_SIZE is specified at compile-time, make sure that it does
8527 ** not exceed the maximum mmap size.
8528 */
8529 #ifndef SQLITE_DEFAULT_MMAP_SIZE
8530 # define SQLITE_DEFAULT_MMAP_SIZE 0
8531 # define SQLITE_DEFAULT_MMAP_SIZE_xc 1  /* Exclude from ctime.c */
8532 #endif
8533 #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
8534 # undef SQLITE_DEFAULT_MMAP_SIZE
8535 # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
8536 #endif
8537 
8538 /*
8539 ** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
8540 ** Priority is given to SQLITE_ENABLE_STAT4.  If either are defined, also
8541 ** define SQLITE_ENABLE_STAT3_OR_STAT4
8542 */
8543 #ifdef SQLITE_ENABLE_STAT4
8544 # undef SQLITE_ENABLE_STAT3
8545 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8546 #elif SQLITE_ENABLE_STAT3
8547 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8548 #elif SQLITE_ENABLE_STAT3_OR_STAT4
8549 # undef SQLITE_ENABLE_STAT3_OR_STAT4
8550 #endif
8551 
8552 /*
8553 ** An instance of the following structure is used to store the busy-handler
8554 ** callback for a given sqlite handle.
8555 **
8556 ** The sqlite.busyHandler member of the sqlite struct contains the busy
8557 ** callback for the database handle. Each pager opened via the sqlite
8558 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
8559 ** callback is currently invoked only from within pager.c.
8560 */
8561 typedef struct BusyHandler BusyHandler;
8562 struct BusyHandler {
8563   int (*xFunc)(void *,int);  /* The busy callback */
8564   void *pArg;                /* First arg to busy callback */
8565   int nBusy;                 /* Incremented with each busy call */
8566 };
8567 
8568 /*
8569 ** Name of the master database table.  The master database table
8570 ** is a special table that holds the names and attributes of all
8571 ** user tables and indices.
8572 */
8573 #define MASTER_NAME       "sqlite_master"
8574 #define TEMP_MASTER_NAME  "sqlite_temp_master"
8575 
8576 /*
8577 ** The root-page of the master database table.
8578 */
8579 #define MASTER_ROOT       1
8580 
8581 /*
8582 ** The name of the schema table.
8583 */
8584 #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
8585 
8586 /*
8587 ** A convenience macro that returns the number of elements in
8588 ** an array.
8589 */
8590 #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
8591 
8592 /*
8593 ** Determine if the argument is a power of two
8594 */
8595 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
8596 
8597 /*
8598 ** The following value as a destructor means to use sqlite3DbFree().
8599 ** The sqlite3DbFree() routine requires two parameters instead of the
8600 ** one parameter that destructors normally want.  So we have to introduce
8601 ** this magic value that the code knows to handle differently.  Any
8602 ** pointer will work here as long as it is distinct from SQLITE_STATIC
8603 ** and SQLITE_TRANSIENT.
8604 */
8605 #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3MallocSize)
8606 
8607 /*
8608 ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
8609 ** not support Writable Static Data (WSD) such as global and static variables.
8610 ** All variables must either be on the stack or dynamically allocated from
8611 ** the heap.  When WSD is unsupported, the variable declarations scattered
8612 ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
8613 ** macro is used for this purpose.  And instead of referencing the variable
8614 ** directly, we use its constant as a key to lookup the run-time allocated
8615 ** buffer that holds real variable.  The constant is also the initializer
8616 ** for the run-time allocated buffer.
8617 **
8618 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
8619 ** macros become no-ops and have zero performance impact.
8620 */
8621 #ifdef SQLITE_OMIT_WSD
8622   #define SQLITE_WSD const
8623   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
8624   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
8625 SQLITE_API   int sqlite3_wsd_init(int N, int J);
8626 SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
8627 #else
8628   #define SQLITE_WSD
8629   #define GLOBAL(t,v) v
8630   #define sqlite3GlobalConfig sqlite3Config
8631 #endif
8632 
8633 /*
8634 ** The following macros are used to suppress compiler warnings and to
8635 ** make it clear to human readers when a function parameter is deliberately
8636 ** left unused within the body of a function. This usually happens when
8637 ** a function is called via a function pointer. For example the
8638 ** implementation of an SQL aggregate step callback may not use the
8639 ** parameter indicating the number of arguments passed to the aggregate,
8640 ** if it knows that this is enforced elsewhere.
8641 **
8642 ** When a function parameter is not used at all within the body of a function,
8643 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
8644 ** However, these macros may also be used to suppress warnings related to
8645 ** parameters that may or may not be used depending on compilation options.
8646 ** For example those parameters only used in assert() statements. In these
8647 ** cases the parameters are named as per the usual conventions.
8648 */
8649 #define UNUSED_PARAMETER(x) (void)(x)
8650 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
8651 
8652 /*
8653 ** Forward references to structures
8654 */
8655 typedef struct AggInfo AggInfo;
8656 typedef struct AuthContext AuthContext;
8657 typedef struct AutoincInfo AutoincInfo;
8658 typedef struct Bitvec Bitvec;
8659 typedef struct CollSeq CollSeq;
8660 typedef struct Column Column;
8661 typedef struct Db Db;
8662 typedef struct Schema Schema;
8663 typedef struct Expr Expr;
8664 typedef struct ExprList ExprList;
8665 typedef struct ExprSpan ExprSpan;
8666 typedef struct FKey FKey;
8667 typedef struct FuncDestructor FuncDestructor;
8668 typedef struct FuncDef FuncDef;
8669 typedef struct FuncDefHash FuncDefHash;
8670 typedef struct IdList IdList;
8671 typedef struct Index Index;
8672 typedef struct IndexSample IndexSample;
8673 typedef struct KeyClass KeyClass;
8674 typedef struct KeyInfo KeyInfo;
8675 typedef struct Lookaside Lookaside;
8676 typedef struct LookasideSlot LookasideSlot;
8677 typedef struct Module Module;
8678 typedef struct NameContext NameContext;
8679 typedef struct Parse Parse;
8680 typedef struct PrintfArguments PrintfArguments;
8681 typedef struct RowSet RowSet;
8682 typedef struct Savepoint Savepoint;
8683 typedef struct Select Select;
8684 typedef struct SelectDest SelectDest;
8685 typedef struct SrcList SrcList;
8686 typedef struct StrAccum StrAccum;
8687 typedef struct Table Table;
8688 typedef struct TableLock TableLock;
8689 typedef struct Token Token;
8690 typedef struct Trigger Trigger;
8691 typedef struct TriggerPrg TriggerPrg;
8692 typedef struct TriggerStep TriggerStep;
8693 typedef struct UnpackedRecord UnpackedRecord;
8694 typedef struct VTable VTable;
8695 typedef struct VtabCtx VtabCtx;
8696 typedef struct Walker Walker;
8697 typedef struct WhereInfo WhereInfo;
8698 typedef struct With With;
8699 
8700 /*
8701 ** Defer sourcing vdbe.h and btree.h until after the "u8" and
8702 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
8703 ** pointer types (i.e. FuncDef) defined above.
8704 */
8705 /************** Include btree.h in the middle of sqliteInt.h *****************/
8706 /************** Begin file btree.h *******************************************/
8707 /*
8708 ** 2001 September 15
8709 **
8710 ** The author disclaims copyright to this source code.  In place of
8711 ** a legal notice, here is a blessing:
8712 **
8713 **    May you do good and not evil.
8714 **    May you find forgiveness for yourself and forgive others.
8715 **    May you share freely, never taking more than you give.
8716 **
8717 *************************************************************************
8718 ** This header file defines the interface that the sqlite B-Tree file
8719 ** subsystem.  See comments in the source code for a detailed description
8720 ** of what each interface routine does.
8721 */
8722 #ifndef _BTREE_H_
8723 #define _BTREE_H_
8724 
8725 /* TODO: This definition is just included so other modules compile. It
8726 ** needs to be revisited.
8727 */
8728 #define SQLITE_N_BTREE_META 10
8729 
8730 /*
8731 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
8732 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
8733 */
8734 #ifndef SQLITE_DEFAULT_AUTOVACUUM
8735   #define SQLITE_DEFAULT_AUTOVACUUM 0
8736 #endif
8737 
8738 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
8739 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
8740 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
8741 
8742 /*
8743 ** Forward declarations of structure
8744 */
8745 typedef struct Btree Btree;
8746 typedef struct BtCursor BtCursor;
8747 typedef struct BtShared BtShared;
8748 
8749 
8750 SQLITE_PRIVATE int sqlite3BtreeOpen(
8751   sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
8752   const char *zFilename,   /* Name of database file to open */
8753   sqlite3 *db,             /* Associated database connection */
8754   Btree **ppBtree,         /* Return open Btree* here */
8755   int flags,               /* Flags */
8756   int vfsFlags             /* Flags passed through to VFS open */
8757 );
8758 
8759 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
8760 ** following values.
8761 **
8762 ** NOTE:  These values must match the corresponding PAGER_ values in
8763 ** pager.h.
8764 */
8765 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
8766 #define BTREE_MEMORY        2  /* This is an in-memory DB */
8767 #define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
8768 #define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
8769 
8770 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
8771 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
8772 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
8773 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
8774 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
8775 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
8776 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8777 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
8778 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
8779 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
8780 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
8781 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
8782 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
8783 #endif
8784 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
8785 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
8786 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
8787 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
8788 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
8789 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
8790 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int);
8791 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
8792 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
8793 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
8794 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
8795 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
8796 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
8797 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
8798 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
8799 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
8800 
8801 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
8802 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
8803 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
8804 
8805 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
8806 
8807 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
8808 ** of the flags shown below.
8809 **
8810 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
8811 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
8812 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
8813 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
8814 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
8815 ** indices.)
8816 */
8817 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
8818 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
8819 
8820 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
8821 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
8822 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
8823 
8824 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
8825 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
8826 
8827 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
8828 
8829 /*
8830 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
8831 ** should be one of the following values. The integer values are assigned
8832 ** to constants so that the offset of the corresponding field in an
8833 ** SQLite database header may be found using the following formula:
8834 **
8835 **   offset = 36 + (idx * 4)
8836 **
8837 ** For example, the free-page-count field is located at byte offset 36 of
8838 ** the database file header. The incr-vacuum-flag field is located at
8839 ** byte offset 64 (== 36+4*7).
8840 */
8841 #define BTREE_FREE_PAGE_COUNT     0
8842 #define BTREE_SCHEMA_VERSION      1
8843 #define BTREE_FILE_FORMAT         2
8844 #define BTREE_DEFAULT_CACHE_SIZE  3
8845 #define BTREE_LARGEST_ROOT_PAGE   4
8846 #define BTREE_TEXT_ENCODING       5
8847 #define BTREE_USER_VERSION        6
8848 #define BTREE_INCR_VACUUM         7
8849 #define BTREE_APPLICATION_ID      8
8850 
8851 /*
8852 ** Values that may be OR'd together to form the second argument of an
8853 ** sqlite3BtreeCursorHints() call.
8854 */
8855 #define BTREE_BULKLOAD 0x00000001
8856 
8857 SQLITE_PRIVATE int sqlite3BtreeCursor(
8858   Btree*,                              /* BTree containing table to open */
8859   int iTable,                          /* Index of root page */
8860   int wrFlag,                          /* 1 for writing.  0 for read-only */
8861   struct KeyInfo*,                     /* First argument to compare function */
8862   BtCursor *pCursor                    /* Space to write cursor structure */
8863 );
8864 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
8865 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
8866 
8867 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
8868 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
8869   BtCursor*,
8870   UnpackedRecord *pUnKey,
8871   i64 intKey,
8872   int bias,
8873   int *pRes
8874 );
8875 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
8876 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
8877 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
8878                                   const void *pData, int nData,
8879                                   int nZero, int bias, int seekResult);
8880 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
8881 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
8882 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
8883 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
8884 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
8885 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
8886 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
8887 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
8888 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
8889 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
8890 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
8891 
8892 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
8893 SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
8894 
8895 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
8896 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
8897 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
8898 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
8899 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
8900 
8901 #ifndef NDEBUG
8902 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
8903 #endif
8904 
8905 #ifndef SQLITE_OMIT_BTREECOUNT
8906 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
8907 #endif
8908 
8909 #ifdef SQLITE_TEST
8910 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
8911 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
8912 #endif
8913 
8914 #ifndef SQLITE_OMIT_WAL
8915 SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
8916 #endif
8917 
8918 /*
8919 ** If we are not using shared cache, then there is no need to
8920 ** use mutexes to access the BtShared structures.  So make the
8921 ** Enter and Leave procedures no-ops.
8922 */
8923 #ifndef SQLITE_OMIT_SHARED_CACHE
8924 SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
8925 SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
8926 #else
8927 # define sqlite3BtreeEnter(X)
8928 # define sqlite3BtreeEnterAll(X)
8929 #endif
8930 
8931 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
8932 SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
8933 SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
8934 SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
8935 SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
8936 SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
8937 #ifndef NDEBUG
8938   /* These routines are used inside assert() statements only. */
8939 SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
8940 SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
8941 SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
8942 #endif
8943 #else
8944 
8945 # define sqlite3BtreeSharable(X) 0
8946 # define sqlite3BtreeLeave(X)
8947 # define sqlite3BtreeEnterCursor(X)
8948 # define sqlite3BtreeLeaveCursor(X)
8949 # define sqlite3BtreeLeaveAll(X)
8950 
8951 # define sqlite3BtreeHoldsMutex(X) 1
8952 # define sqlite3BtreeHoldsAllMutexes(X) 1
8953 # define sqlite3SchemaMutexHeld(X,Y,Z) 1
8954 #endif
8955 
8956 
8957 #endif /* _BTREE_H_ */
8958 
8959 /************** End of btree.h ***********************************************/
8960 /************** Continuing where we left off in sqliteInt.h ******************/
8961 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
8962 /************** Begin file vdbe.h ********************************************/
8963 /*
8964 ** 2001 September 15
8965 **
8966 ** The author disclaims copyright to this source code.  In place of
8967 ** a legal notice, here is a blessing:
8968 **
8969 **    May you do good and not evil.
8970 **    May you find forgiveness for yourself and forgive others.
8971 **    May you share freely, never taking more than you give.
8972 **
8973 *************************************************************************
8974 ** Header file for the Virtual DataBase Engine (VDBE)
8975 **
8976 ** This header defines the interface to the virtual database engine
8977 ** or VDBE.  The VDBE implements an abstract machine that runs a
8978 ** simple program to access and modify the underlying database.
8979 */
8980 #ifndef _SQLITE_VDBE_H_
8981 #define _SQLITE_VDBE_H_
8982 /* #include <stdio.h> */
8983 
8984 /*
8985 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
8986 ** in the source file sqliteVdbe.c are allowed to see the insides
8987 ** of this structure.
8988 */
8989 typedef struct Vdbe Vdbe;
8990 
8991 /*
8992 ** The names of the following types declared in vdbeInt.h are required
8993 ** for the VdbeOp definition.
8994 */
8995 typedef struct Mem Mem;
8996 typedef struct SubProgram SubProgram;
8997 
8998 /*
8999 ** A single instruction of the virtual machine has an opcode
9000 ** and as many as three operands.  The instruction is recorded
9001 ** as an instance of the following structure:
9002 */
9003 struct VdbeOp {
9004   u8 opcode;          /* What operation to perform */
9005   signed char p4type; /* One of the P4_xxx constants for p4 */
9006   u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
9007   u8 p5;              /* Fifth parameter is an unsigned character */
9008   int p1;             /* First operand */
9009   int p2;             /* Second parameter (often the jump destination) */
9010   int p3;             /* The third parameter */
9011   union {             /* fourth parameter */
9012     int i;                 /* Integer value if p4type==P4_INT32 */
9013     void *p;               /* Generic pointer */
9014     char *z;               /* Pointer to data for string (char array) types */
9015     i64 *pI64;             /* Used when p4type is P4_INT64 */
9016     double *pReal;         /* Used when p4type is P4_REAL */
9017     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
9018     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
9019     Mem *pMem;             /* Used when p4type is P4_MEM */
9020     VTable *pVtab;         /* Used when p4type is P4_VTAB */
9021     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
9022     int *ai;               /* Used when p4type is P4_INTARRAY */
9023     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
9024     int (*xAdvance)(BtCursor *, int *);
9025   } p4;
9026 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
9027   char *zComment;          /* Comment to improve readability */
9028 #endif
9029 #ifdef VDBE_PROFILE
9030   u32 cnt;                 /* Number of times this instruction was executed */
9031   u64 cycles;              /* Total time spent executing this instruction */
9032 #endif
9033 #ifdef SQLITE_VDBE_COVERAGE
9034   int iSrcLine;            /* Source-code line that generated this opcode */
9035 #endif
9036 };
9037 typedef struct VdbeOp VdbeOp;
9038 
9039 
9040 /*
9041 ** A sub-routine used to implement a trigger program.
9042 */
9043 struct SubProgram {
9044   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
9045   int nOp;                      /* Elements in aOp[] */
9046   int nMem;                     /* Number of memory cells required */
9047   int nCsr;                     /* Number of cursors required */
9048   int nOnce;                    /* Number of OP_Once instructions */
9049   void *token;                  /* id that may be used to recursive triggers */
9050   SubProgram *pNext;            /* Next sub-program already visited */
9051 };
9052 
9053 /*
9054 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
9055 ** it takes up less space.
9056 */
9057 struct VdbeOpList {
9058   u8 opcode;          /* What operation to perform */
9059   signed char p1;     /* First operand */
9060   signed char p2;     /* Second parameter (often the jump destination) */
9061   signed char p3;     /* Third parameter */
9062 };
9063 typedef struct VdbeOpList VdbeOpList;
9064 
9065 /*
9066 ** Allowed values of VdbeOp.p4type
9067 */
9068 #define P4_NOTUSED    0   /* The P4 parameter is not used */
9069 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
9070 #define P4_STATIC   (-2)  /* Pointer to a static string */
9071 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
9072 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
9073 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
9074 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
9075 #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
9076 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
9077 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
9078 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
9079 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
9080 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
9081 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
9082 #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
9083 #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
9084 
9085 /* Error message codes for OP_Halt */
9086 #define P5_ConstraintNotNull 1
9087 #define P5_ConstraintUnique  2
9088 #define P5_ConstraintCheck   3
9089 #define P5_ConstraintFK      4
9090 
9091 /*
9092 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
9093 ** number of columns of data returned by the statement.
9094 */
9095 #define COLNAME_NAME     0
9096 #define COLNAME_DECLTYPE 1
9097 #define COLNAME_DATABASE 2
9098 #define COLNAME_TABLE    3
9099 #define COLNAME_COLUMN   4
9100 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9101 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
9102 #else
9103 # ifdef SQLITE_OMIT_DECLTYPE
9104 #   define COLNAME_N      1      /* Store only the name */
9105 # else
9106 #   define COLNAME_N      2      /* Store the name and decltype */
9107 # endif
9108 #endif
9109 
9110 /*
9111 ** The following macro converts a relative address in the p2 field
9112 ** of a VdbeOp structure into a negative number so that
9113 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
9114 ** the macro again restores the address.
9115 */
9116 #define ADDR(X)  (-1-(X))
9117 
9118 /*
9119 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
9120 ** header file that defines a number for each opcode used by the VDBE.
9121 */
9122 /************** Include opcodes.h in the middle of vdbe.h ********************/
9123 /************** Begin file opcodes.h *****************************************/
9124 /* Automatically generated.  Do not edit */
9125 /* See the mkopcodeh.awk script for details */
9126 #define OP_Function        1 /* synopsis: r[P3]=func(r[P2@P5])             */
9127 #define OP_Savepoint       2
9128 #define OP_AutoCommit      3
9129 #define OP_Transaction     4
9130 #define OP_SorterNext      5
9131 #define OP_PrevIfOpen      6
9132 #define OP_NextIfOpen      7
9133 #define OP_Prev            8
9134 #define OP_Next            9
9135 #define OP_AggStep        10 /* synopsis: accum=r[P3] step(r[P2@P5])       */
9136 #define OP_Checkpoint     11
9137 #define OP_JournalMode    12
9138 #define OP_Vacuum         13
9139 #define OP_VFilter        14 /* synopsis: iPlan=r[P3] zPlan='P4'           */
9140 #define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
9141 #define OP_Goto           16
9142 #define OP_Gosub          17
9143 #define OP_Return         18
9144 #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
9145 #define OP_InitCoroutine  20
9146 #define OP_EndCoroutine   21
9147 #define OP_Yield          22
9148 #define OP_HaltIfNull     23 /* synopsis: if r[P3]=null halt               */
9149 #define OP_Halt           24
9150 #define OP_Integer        25 /* synopsis: r[P2]=P1                         */
9151 #define OP_Int64          26 /* synopsis: r[P2]=P4                         */
9152 #define OP_String         27 /* synopsis: r[P2]='P4' (len=P1)              */
9153 #define OP_Null           28 /* synopsis: r[P2..P3]=NULL                   */
9154 #define OP_SoftNull       29 /* synopsis: r[P1]=NULL                       */
9155 #define OP_Blob           30 /* synopsis: r[P2]=P4 (len=P1)                */
9156 #define OP_Variable       31 /* synopsis: r[P2]=parameter(P1,P4)           */
9157 #define OP_Move           32 /* synopsis: r[P2@P3]=r[P1@P3]                */
9158 #define OP_Copy           33 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
9159 #define OP_SCopy          34 /* synopsis: r[P2]=r[P1]                      */
9160 #define OP_ResultRow      35 /* synopsis: output=r[P1@P2]                  */
9161 #define OP_CollSeq        36
9162 #define OP_AddImm         37 /* synopsis: r[P1]=r[P1]+P2                   */
9163 #define OP_MustBeInt      38
9164 #define OP_RealAffinity   39
9165 #define OP_Permutation    40
9166 #define OP_Compare        41
9167 #define OP_Jump           42
9168 #define OP_Once           43
9169 #define OP_If             44
9170 #define OP_IfNot          45
9171 #define OP_Column         46 /* synopsis: r[P3]=PX                         */
9172 #define OP_Affinity       47 /* synopsis: affinity(r[P1@P2])               */
9173 #define OP_MakeRecord     48 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
9174 #define OP_Count          49 /* synopsis: r[P2]=count()                    */
9175 #define OP_ReadCookie     50
9176 #define OP_SetCookie      51
9177 #define OP_OpenRead       52 /* synopsis: root=P2 iDb=P3                   */
9178 #define OP_OpenWrite      53 /* synopsis: root=P2 iDb=P3                   */
9179 #define OP_OpenAutoindex  54 /* synopsis: nColumn=P2                       */
9180 #define OP_OpenEphemeral  55 /* synopsis: nColumn=P2                       */
9181 #define OP_SorterOpen     56
9182 #define OP_OpenPseudo     57 /* synopsis: P3 columns in r[P2]              */
9183 #define OP_Close          58
9184 #define OP_SeekLT         59
9185 #define OP_SeekLE         60
9186 #define OP_SeekGE         61
9187 #define OP_SeekGT         62
9188 #define OP_Seek           63 /* synopsis: intkey=r[P2]                     */
9189 #define OP_NoConflict     64 /* synopsis: key=r[P3@P4]                     */
9190 #define OP_NotFound       65 /* synopsis: key=r[P3@P4]                     */
9191 #define OP_Found          66 /* synopsis: key=r[P3@P4]                     */
9192 #define OP_NotExists      67 /* synopsis: intkey=r[P3]                     */
9193 #define OP_Sequence       68 /* synopsis: r[P2]=rowid                      */
9194 #define OP_NewRowid       69 /* synopsis: r[P2]=rowid                      */
9195 #define OP_Insert         70 /* synopsis: intkey=r[P3] data=r[P2]          */
9196 #define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
9197 #define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9198 #define OP_InsertInt      73 /* synopsis: intkey=P3 data=r[P2]             */
9199 #define OP_Delete         74
9200 #define OP_ResetCount     75
9201 #define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
9202 #define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9203 #define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9204 #define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9205 #define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9206 #define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9207 #define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9208 #define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9209 #define OP_SorterCompare  84 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
9210 #define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9211 #define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9212 #define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9213 #define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9214 #define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
9215 #define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9216 #define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9217 #define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9218 #define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9219 #define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9220 #define OP_SorterData     95 /* synopsis: r[P2]=data                       */
9221 #define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9222 #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
9223 #define OP_RowKey         98 /* synopsis: r[P2]=key                        */
9224 #define OP_RowData        99 /* synopsis: r[P2]=data                       */
9225 #define OP_Rowid         100 /* synopsis: r[P2]=rowid                      */
9226 #define OP_NullRow       101
9227 #define OP_Last          102
9228 #define OP_SorterSort    103
9229 #define OP_Sort          104
9230 #define OP_Rewind        105
9231 #define OP_SorterInsert  106
9232 #define OP_IdxInsert     107 /* synopsis: key=r[P2]                        */
9233 #define OP_IdxDelete     108 /* synopsis: key=r[P2@P3]                     */
9234 #define OP_IdxRowid      109 /* synopsis: r[P2]=rowid                      */
9235 #define OP_IdxLE         110 /* synopsis: key=r[P3@P4]                     */
9236 #define OP_IdxGT         111 /* synopsis: key=r[P3@P4]                     */
9237 #define OP_IdxLT         112 /* synopsis: key=r[P3@P4]                     */
9238 #define OP_IdxGE         113 /* synopsis: key=r[P3@P4]                     */
9239 #define OP_Destroy       114
9240 #define OP_Clear         115
9241 #define OP_CreateIndex   116 /* synopsis: r[P2]=root iDb=P1                */
9242 #define OP_CreateTable   117 /* synopsis: r[P2]=root iDb=P1                */
9243 #define OP_ParseSchema   118
9244 #define OP_LoadAnalysis  119
9245 #define OP_DropTable     120
9246 #define OP_DropIndex     121
9247 #define OP_DropTrigger   122
9248 #define OP_IntegrityCk   123
9249 #define OP_RowSetAdd     124 /* synopsis: rowset(P1)=r[P2]                 */
9250 #define OP_RowSetRead    125 /* synopsis: r[P3]=rowset(P1)                 */
9251 #define OP_RowSetTest    126 /* synopsis: if r[P3] in rowset(P1) goto P2   */
9252 #define OP_Program       127
9253 #define OP_Param         128
9254 #define OP_FkCounter     129 /* synopsis: fkctr[P1]+=P2                    */
9255 #define OP_FkIfZero      130 /* synopsis: if fkctr[P1]==0 goto P2          */
9256 #define OP_MemMax        131 /* synopsis: r[P1]=max(r[P1],r[P2])           */
9257 #define OP_IfPos         132 /* synopsis: if r[P1]>0 goto P2               */
9258 #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
9259 #define OP_IfNeg         134 /* synopsis: if r[P1]<0 goto P2               */
9260 #define OP_IfZero        135 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
9261 #define OP_AggFinal      136 /* synopsis: accum=r[P1] N=P2                 */
9262 #define OP_IncrVacuum    137
9263 #define OP_Expire        138
9264 #define OP_TableLock     139 /* synopsis: iDb=P1 root=P2 write=P3          */
9265 #define OP_VBegin        140
9266 #define OP_VCreate       141
9267 #define OP_VDestroy      142
9268 #define OP_ToText        143 /* same as TK_TO_TEXT                         */
9269 #define OP_ToBlob        144 /* same as TK_TO_BLOB                         */
9270 #define OP_ToNumeric     145 /* same as TK_TO_NUMERIC                      */
9271 #define OP_ToInt         146 /* same as TK_TO_INT                          */
9272 #define OP_ToReal        147 /* same as TK_TO_REAL                         */
9273 #define OP_VOpen         148
9274 #define OP_VColumn       149 /* synopsis: r[P3]=vcolumn(P2)                */
9275 #define OP_VNext         150
9276 #define OP_VRename       151
9277 #define OP_Pagecount     152
9278 #define OP_MaxPgcnt      153
9279 #define OP_Init          154 /* synopsis: Start at P2                      */
9280 #define OP_Noop          155
9281 #define OP_Explain       156
9282 
9283 
9284 /* Properties such as "out2" or "jump" that are specified in
9285 ** comments following the "case" for each opcode in the vdbe.c
9286 ** are encoded into bitvectors as follows:
9287 */
9288 #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
9289 #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
9290 #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
9291 #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
9292 #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
9293 #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
9294 #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
9295 #define OPFLG_INITIALIZER {\
9296 /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
9297 /*   8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\
9298 /*  16 */ 0x01, 0x01, 0x04, 0x24, 0x01, 0x04, 0x05, 0x10,\
9299 /*  24 */ 0x00, 0x02, 0x02, 0x02, 0x02, 0x00, 0x02, 0x02,\
9300 /*  32 */ 0x00, 0x00, 0x20, 0x00, 0x00, 0x04, 0x05, 0x04,\
9301 /*  40 */ 0x00, 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00,\
9302 /*  48 */ 0x00, 0x02, 0x02, 0x10, 0x00, 0x00, 0x00, 0x00,\
9303 /*  56 */ 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x08,\
9304 /*  64 */ 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00, 0x4c,\
9305 /*  72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
9306 /*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
9307 /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
9308 /*  96 */ 0x24, 0x02, 0x00, 0x00, 0x02, 0x00, 0x01, 0x01,\
9309 /* 104 */ 0x01, 0x01, 0x08, 0x08, 0x00, 0x02, 0x01, 0x01,\
9310 /* 112 */ 0x01, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00,\
9311 /* 120 */ 0x00, 0x00, 0x00, 0x00, 0x0c, 0x45, 0x15, 0x01,\
9312 /* 128 */ 0x02, 0x00, 0x01, 0x08, 0x05, 0x02, 0x05, 0x05,\
9313 /* 136 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,\
9314 /* 144 */ 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x01, 0x00,\
9315 /* 152 */ 0x02, 0x02, 0x01, 0x00, 0x00,}
9316 
9317 /************** End of opcodes.h *********************************************/
9318 /************** Continuing where we left off in vdbe.h ***********************/
9319 
9320 /*
9321 ** Prototypes for the VDBE interface.  See comments on the implementation
9322 ** for a description of what each of these routines does.
9323 */
9324 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
9325 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
9326 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
9327 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
9328 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
9329 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
9330 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
9331 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp, int iLineno);
9332 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
9333 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
9334 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
9335 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
9336 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
9337 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
9338 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
9339 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
9340 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
9341 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
9342 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
9343 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
9344 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
9345 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
9346 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
9347 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
9348 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
9349 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
9350 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
9351 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
9352 #ifdef SQLITE_DEBUG
9353 SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
9354 #endif
9355 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
9356 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
9357 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
9358 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
9359 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
9360 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
9361 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
9362 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
9363 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
9364 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
9365 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
9366 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
9367 #ifndef SQLITE_OMIT_TRACE
9368 SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9369 #endif
9370 
9371 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9372 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,const UnpackedRecord*,int);
9373 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
9374 
9375 typedef int (*RecordCompare)(int,const void*,const UnpackedRecord*,int);
9376 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord*);
9377 
9378 #ifndef SQLITE_OMIT_TRIGGER
9379 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
9380 #endif
9381 
9382 /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
9383 ** each VDBE opcode.
9384 **
9385 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
9386 ** comments in VDBE programs that show key decision points in the code
9387 ** generator.
9388 */
9389 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
9390 SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
9391 # define VdbeComment(X)  sqlite3VdbeComment X
9392 SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
9393 # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
9394 # ifdef SQLITE_ENABLE_MODULE_COMMENTS
9395 #   define VdbeModuleComment(X)  sqlite3VdbeNoopComment X
9396 # else
9397 #   define VdbeModuleComment(X)
9398 # endif
9399 #else
9400 # define VdbeComment(X)
9401 # define VdbeNoopComment(X)
9402 # define VdbeModuleComment(X)
9403 #endif
9404 
9405 /*
9406 ** The VdbeCoverage macros are used to set a coverage testing point
9407 ** for VDBE branch instructions.  The coverage testing points are line
9408 ** numbers in the sqlite3.c source file.  VDBE branch coverage testing
9409 ** only works with an amalagmation build.  That's ok since a VDBE branch
9410 ** coverage build designed for testing the test suite only.  No application
9411 ** should ever ship with VDBE branch coverage measuring turned on.
9412 **
9413 **    VdbeCoverage(v)                  // Mark the previously coded instruction
9414 **                                     // as a branch
9415 **
9416 **    VdbeCoverageIf(v, conditional)   // Mark previous if conditional true
9417 **
9418 **    VdbeCoverageAlwaysTaken(v)       // Previous branch is always taken
9419 **
9420 **    VdbeCoverageNeverTaken(v)        // Previous branch is never taken
9421 **
9422 ** Every VDBE branch operation must be tagged with one of the macros above.
9423 ** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
9424 ** -DSQLITE_DEBUG then an ALWAYS() will fail in the vdbeTakeBranch()
9425 ** routine in vdbe.c, alerting the developer to the missed tag.
9426 */
9427 #ifdef SQLITE_VDBE_COVERAGE
9428 SQLITE_PRIVATE   void sqlite3VdbeSetLineNumber(Vdbe*,int);
9429 # define VdbeCoverage(v) sqlite3VdbeSetLineNumber(v,__LINE__)
9430 # define VdbeCoverageIf(v,x) if(x)sqlite3VdbeSetLineNumber(v,__LINE__)
9431 # define VdbeCoverageAlwaysTaken(v) sqlite3VdbeSetLineNumber(v,2);
9432 # define VdbeCoverageNeverTaken(v) sqlite3VdbeSetLineNumber(v,1);
9433 # define VDBE_OFFSET_LINENO(x) (__LINE__+x)
9434 #else
9435 # define VdbeCoverage(v)
9436 # define VdbeCoverageIf(v,x)
9437 # define VdbeCoverageAlwaysTaken(v)
9438 # define VdbeCoverageNeverTaken(v)
9439 # define VDBE_OFFSET_LINENO(x) 0
9440 #endif
9441 
9442 #endif
9443 
9444 /************** End of vdbe.h ************************************************/
9445 /************** Continuing where we left off in sqliteInt.h ******************/
9446 /************** Include pager.h in the middle of sqliteInt.h *****************/
9447 /************** Begin file pager.h *******************************************/
9448 /*
9449 ** 2001 September 15
9450 **
9451 ** The author disclaims copyright to this source code.  In place of
9452 ** a legal notice, here is a blessing:
9453 **
9454 **    May you do good and not evil.
9455 **    May you find forgiveness for yourself and forgive others.
9456 **    May you share freely, never taking more than you give.
9457 **
9458 *************************************************************************
9459 ** This header file defines the interface that the sqlite page cache
9460 ** subsystem.  The page cache subsystem reads and writes a file a page
9461 ** at a time and provides a journal for rollback.
9462 */
9463 
9464 #ifndef _PAGER_H_
9465 #define _PAGER_H_
9466 
9467 /*
9468 ** Default maximum size for persistent journal files. A negative
9469 ** value means no limit. This value may be overridden using the
9470 ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
9471 */
9472 #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
9473   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
9474 #endif
9475 
9476 /*
9477 ** The type used to represent a page number.  The first page in a file
9478 ** is called page 1.  0 is used to represent "not a page".
9479 */
9480 typedef u32 Pgno;
9481 
9482 /*
9483 ** Each open file is managed by a separate instance of the "Pager" structure.
9484 */
9485 typedef struct Pager Pager;
9486 
9487 /*
9488 ** Handle type for pages.
9489 */
9490 typedef struct PgHdr DbPage;
9491 
9492 /*
9493 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
9494 ** reserved for working around a windows/posix incompatibility). It is
9495 ** used in the journal to signify that the remainder of the journal file
9496 ** is devoted to storing a master journal name - there are no more pages to
9497 ** roll back. See comments for function writeMasterJournal() in pager.c
9498 ** for details.
9499 */
9500 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
9501 
9502 /*
9503 ** Allowed values for the flags parameter to sqlite3PagerOpen().
9504 **
9505 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
9506 */
9507 #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
9508 #define PAGER_MEMORY        0x0002    /* In-memory database */
9509 
9510 /*
9511 ** Valid values for the second argument to sqlite3PagerLockingMode().
9512 */
9513 #define PAGER_LOCKINGMODE_QUERY      -1
9514 #define PAGER_LOCKINGMODE_NORMAL      0
9515 #define PAGER_LOCKINGMODE_EXCLUSIVE   1
9516 
9517 /*
9518 ** Numeric constants that encode the journalmode.
9519 */
9520 #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
9521 #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
9522 #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
9523 #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
9524 #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
9525 #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
9526 #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
9527 
9528 /*
9529 ** Flags that make up the mask passed to sqlite3PagerAcquire().
9530 */
9531 #define PAGER_GET_NOCONTENT     0x01  /* Do not load data from disk */
9532 #define PAGER_GET_READONLY      0x02  /* Read-only page is acceptable */
9533 
9534 /*
9535 ** Flags for sqlite3PagerSetFlags()
9536 */
9537 #define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
9538 #define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
9539 #define PAGER_SYNCHRONOUS_FULL      0x03  /* PRAGMA synchronous=FULL */
9540 #define PAGER_SYNCHRONOUS_MASK      0x03  /* Mask for three values above */
9541 #define PAGER_FULLFSYNC             0x04  /* PRAGMA fullfsync=ON */
9542 #define PAGER_CKPT_FULLFSYNC        0x08  /* PRAGMA checkpoint_fullfsync=ON */
9543 #define PAGER_CACHESPILL            0x10  /* PRAGMA cache_spill=ON */
9544 #define PAGER_FLAGS_MASK            0x1c  /* All above except SYNCHRONOUS */
9545 
9546 /*
9547 ** The remainder of this file contains the declarations of the functions
9548 ** that make up the Pager sub-system API. See source code comments for
9549 ** a detailed description of each routine.
9550 */
9551 
9552 /* Open and close a Pager connection. */
9553 SQLITE_PRIVATE int sqlite3PagerOpen(
9554   sqlite3_vfs*,
9555   Pager **ppPager,
9556   const char*,
9557   int,
9558   int,
9559   int,
9560   void(*)(DbPage*)
9561 );
9562 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
9563 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
9564 
9565 /* Functions used to configure a Pager object. */
9566 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
9567 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
9568 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
9569 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
9570 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
9571 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
9572 SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
9573 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
9574 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
9575 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
9576 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
9577 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
9578 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
9579 
9580 /* Functions used to obtain and release page references. */
9581 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
9582 #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
9583 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
9584 SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
9585 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
9586 SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
9587 
9588 /* Operations on page references. */
9589 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
9590 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
9591 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
9592 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
9593 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
9594 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
9595 
9596 /* Functions used to manage pager transactions and savepoints. */
9597 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
9598 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
9599 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
9600 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
9601 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
9602 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
9603 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
9604 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
9605 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
9606 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
9607 
9608 #ifndef SQLITE_OMIT_WAL
9609 SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
9610 SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
9611 SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
9612 SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
9613 SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
9614 #endif
9615 
9616 #ifdef SQLITE_ENABLE_ZIPVFS
9617 SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
9618 #endif
9619 
9620 /* Functions used to query pager state and configuration. */
9621 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
9622 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
9623 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
9624 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
9625 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
9626 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
9627 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
9628 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
9629 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
9630 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
9631 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
9632 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
9633 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
9634 
9635 /* Functions used to truncate the database file. */
9636 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
9637 
9638 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
9639 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
9640 #endif
9641 
9642 /* Functions to support testing and debugging. */
9643 #if !defined(NDEBUG) || defined(SQLITE_TEST)
9644 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
9645 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
9646 #endif
9647 #ifdef SQLITE_TEST
9648 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
9649 SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
9650   void disable_simulated_io_errors(void);
9651   void enable_simulated_io_errors(void);
9652 #else
9653 # define disable_simulated_io_errors()
9654 # define enable_simulated_io_errors()
9655 #endif
9656 
9657 #endif /* _PAGER_H_ */
9658 
9659 /************** End of pager.h ***********************************************/
9660 /************** Continuing where we left off in sqliteInt.h ******************/
9661 /************** Include pcache.h in the middle of sqliteInt.h ****************/
9662 /************** Begin file pcache.h ******************************************/
9663 /*
9664 ** 2008 August 05
9665 **
9666 ** The author disclaims copyright to this source code.  In place of
9667 ** a legal notice, here is a blessing:
9668 **
9669 **    May you do good and not evil.
9670 **    May you find forgiveness for yourself and forgive others.
9671 **    May you share freely, never taking more than you give.
9672 **
9673 *************************************************************************
9674 ** This header file defines the interface that the sqlite page cache
9675 ** subsystem.
9676 */
9677 
9678 #ifndef _PCACHE_H_
9679 
9680 typedef struct PgHdr PgHdr;
9681 typedef struct PCache PCache;
9682 
9683 /*
9684 ** Every page in the cache is controlled by an instance of the following
9685 ** structure.
9686 */
9687 struct PgHdr {
9688   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
9689   void *pData;                   /* Page data */
9690   void *pExtra;                  /* Extra content */
9691   PgHdr *pDirty;                 /* Transient list of dirty pages */
9692   Pager *pPager;                 /* The pager this page is part of */
9693   Pgno pgno;                     /* Page number for this page */
9694 #ifdef SQLITE_CHECK_PAGES
9695   u32 pageHash;                  /* Hash of page content */
9696 #endif
9697   u16 flags;                     /* PGHDR flags defined below */
9698 
9699   /**********************************************************************
9700   ** Elements above are public.  All that follows is private to pcache.c
9701   ** and should not be accessed by other modules.
9702   */
9703   i16 nRef;                      /* Number of users of this page */
9704   PCache *pCache;                /* Cache that owns this page */
9705 
9706   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
9707   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
9708 };
9709 
9710 /* Bit values for PgHdr.flags */
9711 #define PGHDR_DIRTY             0x002  /* Page has changed */
9712 #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
9713                                        ** writing this page to the database */
9714 #define PGHDR_NEED_READ         0x008  /* Content is unread */
9715 #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
9716 #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
9717 
9718 #define PGHDR_MMAP              0x040  /* This is an mmap page object */
9719 
9720 /* Initialize and shutdown the page cache subsystem */
9721 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
9722 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
9723 
9724 /* Page cache buffer management:
9725 ** These routines implement SQLITE_CONFIG_PAGECACHE.
9726 */
9727 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
9728 
9729 /* Create a new pager cache.
9730 ** Under memory stress, invoke xStress to try to make pages clean.
9731 ** Only clean and unpinned pages can be reclaimed.
9732 */
9733 SQLITE_PRIVATE void sqlite3PcacheOpen(
9734   int szPage,                    /* Size of every page */
9735   int szExtra,                   /* Extra space associated with each page */
9736   int bPurgeable,                /* True if pages are on backing store */
9737   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
9738   void *pStress,                 /* Argument to xStress */
9739   PCache *pToInit                /* Preallocated space for the PCache */
9740 );
9741 
9742 /* Modify the page-size after the cache has been created. */
9743 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
9744 
9745 /* Return the size in bytes of a PCache object.  Used to preallocate
9746 ** storage space.
9747 */
9748 SQLITE_PRIVATE int sqlite3PcacheSize(void);
9749 
9750 /* One release per successful fetch.  Page is pinned until released.
9751 ** Reference counted.
9752 */
9753 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
9754 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
9755 
9756 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
9757 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
9758 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
9759 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
9760 
9761 /* Change a page number.  Used by incr-vacuum. */
9762 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
9763 
9764 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
9765 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
9766 
9767 /* Get a list of all dirty pages in the cache, sorted by page number */
9768 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
9769 
9770 /* Reset and close the cache object */
9771 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
9772 
9773 /* Clear flags from pages of the page cache */
9774 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
9775 
9776 /* Discard the contents of the cache */
9777 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
9778 
9779 /* Return the total number of outstanding page references */
9780 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
9781 
9782 /* Increment the reference count of an existing page */
9783 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
9784 
9785 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
9786 
9787 /* Return the total number of pages stored in the cache */
9788 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
9789 
9790 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
9791 /* Iterate through all dirty pages currently stored in the cache. This
9792 ** interface is only available if SQLITE_CHECK_PAGES is defined when the
9793 ** library is built.
9794 */
9795 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
9796 #endif
9797 
9798 /* Set and get the suggested cache-size for the specified pager-cache.
9799 **
9800 ** If no global maximum is configured, then the system attempts to limit
9801 ** the total number of pages cached by purgeable pager-caches to the sum
9802 ** of the suggested cache-sizes.
9803 */
9804 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
9805 #ifdef SQLITE_TEST
9806 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
9807 #endif
9808 
9809 /* Free up as much memory as possible from the page cache */
9810 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
9811 
9812 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
9813 /* Try to return memory used by the pcache module to the main memory heap */
9814 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
9815 #endif
9816 
9817 #ifdef SQLITE_TEST
9818 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
9819 #endif
9820 
9821 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
9822 
9823 #endif /* _PCACHE_H_ */
9824 
9825 /************** End of pcache.h **********************************************/
9826 /************** Continuing where we left off in sqliteInt.h ******************/
9827 
9828 /************** Include os.h in the middle of sqliteInt.h ********************/
9829 /************** Begin file os.h **********************************************/
9830 /*
9831 ** 2001 September 16
9832 **
9833 ** The author disclaims copyright to this source code.  In place of
9834 ** a legal notice, here is a blessing:
9835 **
9836 **    May you do good and not evil.
9837 **    May you find forgiveness for yourself and forgive others.
9838 **    May you share freely, never taking more than you give.
9839 **
9840 ******************************************************************************
9841 **
9842 ** This header file (together with is companion C source-code file
9843 ** "os.c") attempt to abstract the underlying operating system so that
9844 ** the SQLite library will work on both POSIX and windows systems.
9845 **
9846 ** This header file is #include-ed by sqliteInt.h and thus ends up
9847 ** being included by every source file.
9848 */
9849 #ifndef _SQLITE_OS_H_
9850 #define _SQLITE_OS_H_
9851 
9852 /*
9853 ** Figure out if we are dealing with Unix, Windows, or some other
9854 ** operating system.  After the following block of preprocess macros,
9855 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
9856 ** will defined to either 1 or 0.  One of the four will be 1.  The other
9857 ** three will be 0.
9858 */
9859 #if defined(SQLITE_OS_OTHER)
9860 # if SQLITE_OS_OTHER==1
9861 #   undef SQLITE_OS_UNIX
9862 #   define SQLITE_OS_UNIX 0
9863 #   undef SQLITE_OS_WIN
9864 #   define SQLITE_OS_WIN 0
9865 # else
9866 #   undef SQLITE_OS_OTHER
9867 # endif
9868 #endif
9869 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
9870 # define SQLITE_OS_OTHER 0
9871 # ifndef SQLITE_OS_WIN
9872 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
9873 #     define SQLITE_OS_WIN 1
9874 #     define SQLITE_OS_UNIX 0
9875 #   else
9876 #     define SQLITE_OS_WIN 0
9877 #     define SQLITE_OS_UNIX 1
9878 #  endif
9879 # else
9880 #  define SQLITE_OS_UNIX 0
9881 # endif
9882 #else
9883 # ifndef SQLITE_OS_WIN
9884 #  define SQLITE_OS_WIN 0
9885 # endif
9886 #endif
9887 
9888 #if SQLITE_OS_WIN
9889 # include <windows.h>
9890 #endif
9891 
9892 /*
9893 ** Determine if we are dealing with Windows NT.
9894 **
9895 ** We ought to be able to determine if we are compiling for win98 or winNT
9896 ** using the _WIN32_WINNT macro as follows:
9897 **
9898 ** #if defined(_WIN32_WINNT)
9899 ** # define SQLITE_OS_WINNT 1
9900 ** #else
9901 ** # define SQLITE_OS_WINNT 0
9902 ** #endif
9903 **
9904 ** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
9905 ** so the above test does not work.  We'll just assume that everything is
9906 ** winNT unless the programmer explicitly says otherwise by setting
9907 ** SQLITE_OS_WINNT to 0.
9908 */
9909 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
9910 # define SQLITE_OS_WINNT 1
9911 #endif
9912 
9913 /*
9914 ** Determine if we are dealing with WindowsCE - which has a much
9915 ** reduced API.
9916 */
9917 #if defined(_WIN32_WCE)
9918 # define SQLITE_OS_WINCE 1
9919 #else
9920 # define SQLITE_OS_WINCE 0
9921 #endif
9922 
9923 /*
9924 ** Determine if we are dealing with WinRT, which provides only a subset of
9925 ** the full Win32 API.
9926 */
9927 #if !defined(SQLITE_OS_WINRT)
9928 # define SQLITE_OS_WINRT 0
9929 #endif
9930 
9931 /* If the SET_FULLSYNC macro is not defined above, then make it
9932 ** a no-op
9933 */
9934 #ifndef SET_FULLSYNC
9935 # define SET_FULLSYNC(x,y)
9936 #endif
9937 
9938 /*
9939 ** The default size of a disk sector
9940 */
9941 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
9942 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
9943 #endif
9944 
9945 /*
9946 ** Temporary files are named starting with this prefix followed by 16 random
9947 ** alphanumeric characters, and no file extension. They are stored in the
9948 ** OS's standard temporary file directory, and are deleted prior to exit.
9949 ** If sqlite is being embedded in another program, you may wish to change the
9950 ** prefix to reflect your program's name, so that if your program exits
9951 ** prematurely, old temporary files can be easily identified. This can be done
9952 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
9953 **
9954 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
9955 ** Mcafee started using SQLite in their anti-virus product and it
9956 ** started putting files with the "sqlite" name in the c:/temp folder.
9957 ** This annoyed many windows users.  Those users would then do a
9958 ** Google search for "sqlite", find the telephone numbers of the
9959 ** developers and call to wake them up at night and complain.
9960 ** For this reason, the default name prefix is changed to be "sqlite"
9961 ** spelled backwards.  So the temp files are still identified, but
9962 ** anybody smart enough to figure out the code is also likely smart
9963 ** enough to know that calling the developer will not help get rid
9964 ** of the file.
9965 */
9966 #ifndef SQLITE_TEMP_FILE_PREFIX
9967 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
9968 #endif
9969 
9970 /*
9971 ** The following values may be passed as the second argument to
9972 ** sqlite3OsLock(). The various locks exhibit the following semantics:
9973 **
9974 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
9975 ** RESERVED:  A single process may hold a RESERVED lock on a file at
9976 **            any time. Other processes may hold and obtain new SHARED locks.
9977 ** PENDING:   A single process may hold a PENDING lock on a file at
9978 **            any one time. Existing SHARED locks may persist, but no new
9979 **            SHARED locks may be obtained by other processes.
9980 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
9981 **
9982 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
9983 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
9984 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
9985 ** sqlite3OsLock().
9986 */
9987 #define NO_LOCK         0
9988 #define SHARED_LOCK     1
9989 #define RESERVED_LOCK   2
9990 #define PENDING_LOCK    3
9991 #define EXCLUSIVE_LOCK  4
9992 
9993 /*
9994 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
9995 **
9996 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
9997 ** those functions are not available.  So we use only LockFile() and
9998 ** UnlockFile().
9999 **
10000 ** LockFile() prevents not just writing but also reading by other processes.
10001 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
10002 ** byte out of a specific range of bytes. The lock byte is obtained at
10003 ** random so two separate readers can probably access the file at the
10004 ** same time, unless they are unlucky and choose the same lock byte.
10005 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
10006 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
10007 ** a single byte of the file that is designated as the reserved lock byte.
10008 ** A PENDING_LOCK is obtained by locking a designated byte different from
10009 ** the RESERVED_LOCK byte.
10010 **
10011 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
10012 ** which means we can use reader/writer locks.  When reader/writer locks
10013 ** are used, the lock is placed on the same range of bytes that is used
10014 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
10015 ** will support two or more Win95 readers or two or more WinNT readers.
10016 ** But a single Win95 reader will lock out all WinNT readers and a single
10017 ** WinNT reader will lock out all other Win95 readers.
10018 **
10019 ** The following #defines specify the range of bytes used for locking.
10020 ** SHARED_SIZE is the number of bytes available in the pool from which
10021 ** a random byte is selected for a shared lock.  The pool of bytes for
10022 ** shared locks begins at SHARED_FIRST.
10023 **
10024 ** The same locking strategy and
10025 ** byte ranges are used for Unix.  This leaves open the possiblity of having
10026 ** clients on win95, winNT, and unix all talking to the same shared file
10027 ** and all locking correctly.  To do so would require that samba (or whatever
10028 ** tool is being used for file sharing) implements locks correctly between
10029 ** windows and unix.  I'm guessing that isn't likely to happen, but by
10030 ** using the same locking range we are at least open to the possibility.
10031 **
10032 ** Locking in windows is manditory.  For this reason, we cannot store
10033 ** actual data in the bytes used for locking.  The pager never allocates
10034 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
10035 ** that all locks will fit on a single page even at the minimum page size.
10036 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
10037 ** is set high so that we don't have to allocate an unused page except
10038 ** for very large databases.  But one should test the page skipping logic
10039 ** by setting PENDING_BYTE low and running the entire regression suite.
10040 **
10041 ** Changing the value of PENDING_BYTE results in a subtly incompatible
10042 ** file format.  Depending on how it is changed, you might not notice
10043 ** the incompatibility right away, even running a full regression test.
10044 ** The default location of PENDING_BYTE is the first byte past the
10045 ** 1GB boundary.
10046 **
10047 */
10048 #ifdef SQLITE_OMIT_WSD
10049 # define PENDING_BYTE     (0x40000000)
10050 #else
10051 # define PENDING_BYTE      sqlite3PendingByte
10052 #endif
10053 #define RESERVED_BYTE     (PENDING_BYTE+1)
10054 #define SHARED_FIRST      (PENDING_BYTE+2)
10055 #define SHARED_SIZE       510
10056 
10057 /*
10058 ** Wrapper around OS specific sqlite3_os_init() function.
10059 */
10060 SQLITE_PRIVATE int sqlite3OsInit(void);
10061 
10062 /*
10063 ** Functions for accessing sqlite3_file methods
10064 */
10065 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
10066 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
10067 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
10068 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
10069 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
10070 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
10071 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
10072 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
10073 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
10074 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
10075 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
10076 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
10077 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
10078 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
10079 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
10080 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
10081 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
10082 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
10083 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
10084 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
10085 
10086 
10087 /*
10088 ** Functions for accessing sqlite3_vfs methods
10089 */
10090 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
10091 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
10092 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
10093 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
10094 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10095 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
10096 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
10097 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
10098 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
10099 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
10100 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
10101 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
10102 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
10103 
10104 /*
10105 ** Convenience functions for opening and closing files using
10106 ** sqlite3_malloc() to obtain space for the file-handle structure.
10107 */
10108 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
10109 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
10110 
10111 #endif /* _SQLITE_OS_H_ */
10112 
10113 /************** End of os.h **************************************************/
10114 /************** Continuing where we left off in sqliteInt.h ******************/
10115 /************** Include mutex.h in the middle of sqliteInt.h *****************/
10116 /************** Begin file mutex.h *******************************************/
10117 /*
10118 ** 2007 August 28
10119 **
10120 ** The author disclaims copyright to this source code.  In place of
10121 ** a legal notice, here is a blessing:
10122 **
10123 **    May you do good and not evil.
10124 **    May you find forgiveness for yourself and forgive others.
10125 **    May you share freely, never taking more than you give.
10126 **
10127 *************************************************************************
10128 **
10129 ** This file contains the common header for all mutex implementations.
10130 ** The sqliteInt.h header #includes this file so that it is available
10131 ** to all source files.  We break it out in an effort to keep the code
10132 ** better organized.
10133 **
10134 ** NOTE:  source files should *not* #include this header file directly.
10135 ** Source files should #include the sqliteInt.h file and let that file
10136 ** include this one indirectly.
10137 */
10138 
10139 
10140 /*
10141 ** Figure out what version of the code to use.  The choices are
10142 **
10143 **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
10144 **                             mutexes implemention cannot be overridden
10145 **                             at start-time.
10146 **
10147 **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
10148 **                             mutual exclusion is provided.  But this
10149 **                             implementation can be overridden at
10150 **                             start-time.
10151 **
10152 **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
10153 **
10154 **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
10155 */
10156 #if !SQLITE_THREADSAFE
10157 # define SQLITE_MUTEX_OMIT
10158 #endif
10159 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
10160 #  if SQLITE_OS_UNIX
10161 #    define SQLITE_MUTEX_PTHREADS
10162 #  elif SQLITE_OS_WIN
10163 #    define SQLITE_MUTEX_W32
10164 #  else
10165 #    define SQLITE_MUTEX_NOOP
10166 #  endif
10167 #endif
10168 
10169 #ifdef SQLITE_MUTEX_OMIT
10170 /*
10171 ** If this is a no-op implementation, implement everything as macros.
10172 */
10173 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
10174 #define sqlite3_mutex_free(X)
10175 #define sqlite3_mutex_enter(X)
10176 #define sqlite3_mutex_try(X)      SQLITE_OK
10177 #define sqlite3_mutex_leave(X)
10178 #define sqlite3_mutex_held(X)     ((void)(X),1)
10179 #define sqlite3_mutex_notheld(X)  ((void)(X),1)
10180 #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
10181 #define sqlite3MutexInit()        SQLITE_OK
10182 #define sqlite3MutexEnd()
10183 #define MUTEX_LOGIC(X)
10184 #else
10185 #define MUTEX_LOGIC(X)            X
10186 #endif /* defined(SQLITE_MUTEX_OMIT) */
10187 
10188 /************** End of mutex.h ***********************************************/
10189 /************** Continuing where we left off in sqliteInt.h ******************/
10190 
10191 
10192 /*
10193 ** Each database file to be accessed by the system is an instance
10194 ** of the following structure.  There are normally two of these structures
10195 ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
10196 ** aDb[1] is the database file used to hold temporary tables.  Additional
10197 ** databases may be attached.
10198 */
10199 struct Db {
10200   char *zName;         /* Name of this database */
10201   Btree *pBt;          /* The B*Tree structure for this database file */
10202   u8 safety_level;     /* How aggressive at syncing data to disk */
10203   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
10204 };
10205 
10206 /*
10207 ** An instance of the following structure stores a database schema.
10208 **
10209 ** Most Schema objects are associated with a Btree.  The exception is
10210 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
10211 ** In shared cache mode, a single Schema object can be shared by multiple
10212 ** Btrees that refer to the same underlying BtShared object.
10213 **
10214 ** Schema objects are automatically deallocated when the last Btree that
10215 ** references them is destroyed.   The TEMP Schema is manually freed by
10216 ** sqlite3_close().
10217 *
10218 ** A thread must be holding a mutex on the corresponding Btree in order
10219 ** to access Schema content.  This implies that the thread must also be
10220 ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
10221 ** For a TEMP Schema, only the connection mutex is required.
10222 */
10223 struct Schema {
10224   int schema_cookie;   /* Database schema version number for this file */
10225   int iGeneration;     /* Generation counter.  Incremented with each change */
10226   Hash tblHash;        /* All tables indexed by name */
10227   Hash idxHash;        /* All (named) indices indexed by name */
10228   Hash trigHash;       /* All triggers indexed by name */
10229   Hash fkeyHash;       /* All foreign keys by referenced table name */
10230   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
10231   u8 file_format;      /* Schema format version for this file */
10232   u8 enc;              /* Text encoding used by this database */
10233   u16 flags;           /* Flags associated with this schema */
10234   int cache_size;      /* Number of pages to use in the cache */
10235 };
10236 
10237 /*
10238 ** These macros can be used to test, set, or clear bits in the
10239 ** Db.pSchema->flags field.
10240 */
10241 #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
10242 #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
10243 #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
10244 #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
10245 
10246 /*
10247 ** Allowed values for the DB.pSchema->flags field.
10248 **
10249 ** The DB_SchemaLoaded flag is set after the database schema has been
10250 ** read into internal hash tables.
10251 **
10252 ** DB_UnresetViews means that one or more views have column names that
10253 ** have been filled out.  If the schema changes, these column names might
10254 ** changes and so the view will need to be reset.
10255 */
10256 #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
10257 #define DB_UnresetViews    0x0002  /* Some views have defined column names */
10258 #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
10259 
10260 /*
10261 ** The number of different kinds of things that can be limited
10262 ** using the sqlite3_limit() interface.
10263 */
10264 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
10265 
10266 /*
10267 ** Lookaside malloc is a set of fixed-size buffers that can be used
10268 ** to satisfy small transient memory allocation requests for objects
10269 ** associated with a particular database connection.  The use of
10270 ** lookaside malloc provides a significant performance enhancement
10271 ** (approx 10%) by avoiding numerous malloc/free requests while parsing
10272 ** SQL statements.
10273 **
10274 ** The Lookaside structure holds configuration information about the
10275 ** lookaside malloc subsystem.  Each available memory allocation in
10276 ** the lookaside subsystem is stored on a linked list of LookasideSlot
10277 ** objects.
10278 **
10279 ** Lookaside allocations are only allowed for objects that are associated
10280 ** with a particular database connection.  Hence, schema information cannot
10281 ** be stored in lookaside because in shared cache mode the schema information
10282 ** is shared by multiple database connections.  Therefore, while parsing
10283 ** schema information, the Lookaside.bEnabled flag is cleared so that
10284 ** lookaside allocations are not used to construct the schema objects.
10285 */
10286 struct Lookaside {
10287   u16 sz;                 /* Size of each buffer in bytes */
10288   u8 bEnabled;            /* False to disable new lookaside allocations */
10289   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
10290   int nOut;               /* Number of buffers currently checked out */
10291   int mxOut;              /* Highwater mark for nOut */
10292   int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
10293   LookasideSlot *pFree;   /* List of available buffers */
10294   void *pStart;           /* First byte of available memory space */
10295   void *pEnd;             /* First byte past end of available space */
10296 };
10297 struct LookasideSlot {
10298   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
10299 };
10300 
10301 /*
10302 ** A hash table for function definitions.
10303 **
10304 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
10305 ** Collisions are on the FuncDef.pHash chain.
10306 */
10307 struct FuncDefHash {
10308   FuncDef *a[23];       /* Hash table for functions */
10309 };
10310 
10311 /*
10312 ** Each database connection is an instance of the following structure.
10313 */
10314 struct sqlite3 {
10315   sqlite3_vfs *pVfs;            /* OS Interface */
10316   struct Vdbe *pVdbe;           /* List of active virtual machines */
10317   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
10318   sqlite3_mutex *mutex;         /* Connection mutex */
10319   Db *aDb;                      /* All backends */
10320   int nDb;                      /* Number of backends currently in use */
10321   int flags;                    /* Miscellaneous flags. See below */
10322   i64 lastRowid;                /* ROWID of most recent insert (see above) */
10323   i64 szMmap;                   /* Default mmap_size setting */
10324   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
10325   int errCode;                  /* Most recent error code (SQLITE_*) */
10326   int errMask;                  /* & result codes with this before returning */
10327   u16 dbOptFlags;               /* Flags to enable/disable optimizations */
10328   u8 autoCommit;                /* The auto-commit flag. */
10329   u8 temp_store;                /* 1: file 2: memory 0: default */
10330   u8 mallocFailed;              /* True if we have seen a malloc failure */
10331   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
10332   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
10333   u8 suppressErr;               /* Do not issue error messages if true */
10334   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
10335   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
10336   int nextPagesize;             /* Pagesize after VACUUM if >0 */
10337   u32 magic;                    /* Magic number for detect library misuse */
10338   int nChange;                  /* Value returned by sqlite3_changes() */
10339   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
10340   int aLimit[SQLITE_N_LIMIT];   /* Limits */
10341   struct sqlite3InitInfo {      /* Information used during initialization */
10342     int newTnum;                /* Rootpage of table being initialized */
10343     u8 iDb;                     /* Which db file is being initialized */
10344     u8 busy;                    /* TRUE if currently initializing */
10345     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
10346   } init;
10347   int nVdbeActive;              /* Number of VDBEs currently running */
10348   int nVdbeRead;                /* Number of active VDBEs that read or write */
10349   int nVdbeWrite;               /* Number of active VDBEs that read and write */
10350   int nVdbeExec;                /* Number of nested calls to VdbeExec() */
10351   int nExtension;               /* Number of loaded extensions */
10352   void **aExtension;            /* Array of shared library handles */
10353   void (*xTrace)(void*,const char*);        /* Trace function */
10354   void *pTraceArg;                          /* Argument to the trace function */
10355   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
10356   void *pProfileArg;                        /* Argument to profile function */
10357   void *pCommitArg;                 /* Argument to xCommitCallback() */
10358   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
10359   void *pRollbackArg;               /* Argument to xRollbackCallback() */
10360   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
10361   void *pUpdateArg;
10362   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
10363 #ifndef SQLITE_OMIT_WAL
10364   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
10365   void *pWalArg;
10366 #endif
10367   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
10368   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
10369   void *pCollNeededArg;
10370   sqlite3_value *pErr;          /* Most recent error message */
10371   union {
10372     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
10373     double notUsed1;            /* Spacer */
10374   } u1;
10375   Lookaside lookaside;          /* Lookaside malloc configuration */
10376 #ifndef SQLITE_OMIT_AUTHORIZATION
10377   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
10378                                 /* Access authorization function */
10379   void *pAuthArg;               /* 1st argument to the access auth function */
10380 #endif
10381 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10382   int (*xProgress)(void *);     /* The progress callback */
10383   void *pProgressArg;           /* Argument to the progress callback */
10384   unsigned nProgressOps;        /* Number of opcodes for progress callback */
10385 #endif
10386 #ifndef SQLITE_OMIT_VIRTUALTABLE
10387   int nVTrans;                  /* Allocated size of aVTrans */
10388   Hash aModule;                 /* populated by sqlite3_create_module() */
10389   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
10390   VTable **aVTrans;             /* Virtual tables with open transactions */
10391   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
10392 #endif
10393   FuncDefHash aFunc;            /* Hash table of connection functions */
10394   Hash aCollSeq;                /* All collating sequences */
10395   BusyHandler busyHandler;      /* Busy callback */
10396   Db aDbStatic[2];              /* Static space for the 2 default backends */
10397   Savepoint *pSavepoint;        /* List of active savepoints */
10398   int busyTimeout;              /* Busy handler timeout, in msec */
10399   int nSavepoint;               /* Number of non-transaction savepoints */
10400   int nStatement;               /* Number of nested statement-transactions  */
10401   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
10402   i64 nDeferredImmCons;         /* Net deferred immediate constraints */
10403   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
10404 
10405 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
10406   /* The following variables are all protected by the STATIC_MASTER
10407   ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
10408   **
10409   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
10410   ** unlock so that it can proceed.
10411   **
10412   ** When X.pBlockingConnection==Y, that means that something that X tried
10413   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
10414   ** held by Y.
10415   */
10416   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
10417   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
10418   void *pUnlockArg;                     /* Argument to xUnlockNotify */
10419   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
10420   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
10421 #endif
10422 };
10423 
10424 /*
10425 ** A macro to discover the encoding of a database.
10426 */
10427 #define ENC(db) ((db)->aDb[0].pSchema->enc)
10428 
10429 /*
10430 ** Possible values for the sqlite3.flags.
10431 */
10432 #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
10433 #define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
10434 #define SQLITE_FullFSync      0x00000004  /* Use full fsync on the backend */
10435 #define SQLITE_CkptFullFSync  0x00000008  /* Use full fsync for checkpoint */
10436 #define SQLITE_CacheSpill     0x00000010  /* OK to spill pager cache */
10437 #define SQLITE_FullColNames   0x00000020  /* Show full column names on SELECT */
10438 #define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
10439 #define SQLITE_CountRows      0x00000080  /* Count rows changed by INSERT, */
10440                                           /*   DELETE, or UPDATE and return */
10441                                           /*   the count using a callback. */
10442 #define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
10443                                           /*   result set is empty */
10444 #define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
10445 #define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
10446 #define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
10447 #define SQLITE_VdbeAddopTrace 0x00001000  /* Trace sqlite3VdbeAddOp() calls */
10448 #define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
10449 #define SQLITE_ReadUncommitted 0x0004000  /* For shared-cache mode */
10450 #define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
10451 #define SQLITE_RecoveryMode   0x00010000  /* Ignore schema errors */
10452 #define SQLITE_ReverseOrder   0x00020000  /* Reverse unordered SELECTs */
10453 #define SQLITE_RecTriggers    0x00040000  /* Enable recursive triggers */
10454 #define SQLITE_ForeignKeys    0x00080000  /* Enforce foreign key constraints  */
10455 #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
10456 #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
10457 #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
10458 #define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
10459 #define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
10460 #define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
10461 #define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
10462 
10463 
10464 /*
10465 ** Bits of the sqlite3.dbOptFlags field that are used by the
10466 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
10467 ** selectively disable various optimizations.
10468 */
10469 #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
10470 #define SQLITE_ColumnCache    0x0002   /* Column cache */
10471 #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
10472 #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
10473 /*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
10474 #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
10475 #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
10476 #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
10477 #define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
10478 #define SQLITE_Transitive     0x0200   /* Transitive constraints */
10479 #define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
10480 #define SQLITE_Stat3          0x0800   /* Use the SQLITE_STAT3 table */
10481 #define SQLITE_AdjustOutEst   0x1000   /* Adjust output estimates using WHERE */
10482 #define SQLITE_AllOpts        0xffff   /* All optimizations */
10483 
10484 /*
10485 ** Macros for testing whether or not optimizations are enabled or disabled.
10486 */
10487 #ifndef SQLITE_OMIT_BUILTIN_TEST
10488 #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
10489 #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
10490 #else
10491 #define OptimizationDisabled(db, mask)  0
10492 #define OptimizationEnabled(db, mask)   1
10493 #endif
10494 
10495 /*
10496 ** Return true if it OK to factor constant expressions into the initialization
10497 ** code. The argument is a Parse object for the code generator.
10498 */
10499 #define ConstFactorOk(P) ((P)->okConstFactor)
10500 
10501 /*
10502 ** Possible values for the sqlite.magic field.
10503 ** The numbers are obtained at random and have no special meaning, other
10504 ** than being distinct from one another.
10505 */
10506 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
10507 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
10508 #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
10509 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
10510 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
10511 #define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
10512 
10513 /*
10514 ** Each SQL function is defined by an instance of the following
10515 ** structure.  A pointer to this structure is stored in the sqlite.aFunc
10516 ** hash table.  When multiple functions have the same name, the hash table
10517 ** points to a linked list of these structures.
10518 */
10519 struct FuncDef {
10520   i16 nArg;            /* Number of arguments.  -1 means unlimited */
10521   u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
10522   void *pUserData;     /* User data parameter */
10523   FuncDef *pNext;      /* Next function with same name */
10524   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
10525   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
10526   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
10527   char *zName;         /* SQL name of the function. */
10528   FuncDef *pHash;      /* Next with a different name but the same hash */
10529   FuncDestructor *pDestructor;   /* Reference counted destructor function */
10530 };
10531 
10532 /*
10533 ** This structure encapsulates a user-function destructor callback (as
10534 ** configured using create_function_v2()) and a reference counter. When
10535 ** create_function_v2() is called to create a function with a destructor,
10536 ** a single object of this type is allocated. FuncDestructor.nRef is set to
10537 ** the number of FuncDef objects created (either 1 or 3, depending on whether
10538 ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
10539 ** member of each of the new FuncDef objects is set to point to the allocated
10540 ** FuncDestructor.
10541 **
10542 ** Thereafter, when one of the FuncDef objects is deleted, the reference
10543 ** count on this object is decremented. When it reaches 0, the destructor
10544 ** is invoked and the FuncDestructor structure freed.
10545 */
10546 struct FuncDestructor {
10547   int nRef;
10548   void (*xDestroy)(void *);
10549   void *pUserData;
10550 };
10551 
10552 /*
10553 ** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
10554 ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
10555 ** are assert() statements in the code to verify this.
10556 */
10557 #define SQLITE_FUNC_ENCMASK  0x003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
10558 #define SQLITE_FUNC_LIKE     0x004 /* Candidate for the LIKE optimization */
10559 #define SQLITE_FUNC_CASE     0x008 /* Case-sensitive LIKE-type function */
10560 #define SQLITE_FUNC_EPHEM    0x010 /* Ephemeral.  Delete with VDBE */
10561 #define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */
10562 #define SQLITE_FUNC_LENGTH   0x040 /* Built-in length() function */
10563 #define SQLITE_FUNC_TYPEOF   0x080 /* Built-in typeof() function */
10564 #define SQLITE_FUNC_COUNT    0x100 /* Built-in count(*) aggregate */
10565 #define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */
10566 #define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */
10567 #define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */
10568 
10569 /*
10570 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
10571 ** used to create the initializers for the FuncDef structures.
10572 **
10573 **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
10574 **     Used to create a scalar function definition of a function zName
10575 **     implemented by C function xFunc that accepts nArg arguments. The
10576 **     value passed as iArg is cast to a (void*) and made available
10577 **     as the user-data (sqlite3_user_data()) for the function. If
10578 **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
10579 **
10580 **   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
10581 **     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
10582 **
10583 **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
10584 **     Used to create an aggregate function definition implemented by
10585 **     the C functions xStep and xFinal. The first four parameters
10586 **     are interpreted in the same way as the first 4 parameters to
10587 **     FUNCTION().
10588 **
10589 **   LIKEFUNC(zName, nArg, pArg, flags)
10590 **     Used to create a scalar function definition of a function zName
10591 **     that accepts nArg arguments and is implemented by a call to C
10592 **     function likeFunc. Argument pArg is cast to a (void *) and made
10593 **     available as the function user-data (sqlite3_user_data()). The
10594 **     FuncDef.flags variable is set to the value passed as the flags
10595 **     parameter.
10596 */
10597 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
10598   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
10599    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
10600 #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
10601   {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
10602    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
10603 #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
10604   {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
10605    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
10606 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
10607   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
10608    pArg, 0, xFunc, 0, 0, #zName, 0, 0}
10609 #define LIKEFUNC(zName, nArg, arg, flags) \
10610   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
10611    (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
10612 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
10613   {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
10614    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
10615 
10616 /*
10617 ** All current savepoints are stored in a linked list starting at
10618 ** sqlite3.pSavepoint. The first element in the list is the most recently
10619 ** opened savepoint. Savepoints are added to the list by the vdbe
10620 ** OP_Savepoint instruction.
10621 */
10622 struct Savepoint {
10623   char *zName;                        /* Savepoint name (nul-terminated) */
10624   i64 nDeferredCons;                  /* Number of deferred fk violations */
10625   i64 nDeferredImmCons;               /* Number of deferred imm fk. */
10626   Savepoint *pNext;                   /* Parent savepoint (if any) */
10627 };
10628 
10629 /*
10630 ** The following are used as the second parameter to sqlite3Savepoint(),
10631 ** and as the P1 argument to the OP_Savepoint instruction.
10632 */
10633 #define SAVEPOINT_BEGIN      0
10634 #define SAVEPOINT_RELEASE    1
10635 #define SAVEPOINT_ROLLBACK   2
10636 
10637 
10638 /*
10639 ** Each SQLite module (virtual table definition) is defined by an
10640 ** instance of the following structure, stored in the sqlite3.aModule
10641 ** hash table.
10642 */
10643 struct Module {
10644   const sqlite3_module *pModule;       /* Callback pointers */
10645   const char *zName;                   /* Name passed to create_module() */
10646   void *pAux;                          /* pAux passed to create_module() */
10647   void (*xDestroy)(void *);            /* Module destructor function */
10648 };
10649 
10650 /*
10651 ** information about each column of an SQL table is held in an instance
10652 ** of this structure.
10653 */
10654 struct Column {
10655   char *zName;     /* Name of this column */
10656   Expr *pDflt;     /* Default value of this column */
10657   char *zDflt;     /* Original text of the default value */
10658   char *zType;     /* Data type for this column */
10659   char *zColl;     /* Collating sequence.  If NULL, use the default */
10660   u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
10661   char affinity;   /* One of the SQLITE_AFF_... values */
10662   u8 szEst;        /* Estimated size of this column.  INT==1 */
10663   u8 colFlags;     /* Boolean properties.  See COLFLAG_ defines below */
10664 };
10665 
10666 /* Allowed values for Column.colFlags:
10667 */
10668 #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
10669 #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
10670 
10671 /*
10672 ** A "Collating Sequence" is defined by an instance of the following
10673 ** structure. Conceptually, a collating sequence consists of a name and
10674 ** a comparison routine that defines the order of that sequence.
10675 **
10676 ** If CollSeq.xCmp is NULL, it means that the
10677 ** collating sequence is undefined.  Indices built on an undefined
10678 ** collating sequence may not be read or written.
10679 */
10680 struct CollSeq {
10681   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
10682   u8 enc;               /* Text encoding handled by xCmp() */
10683   void *pUser;          /* First argument to xCmp() */
10684   int (*xCmp)(void*,int, const void*, int, const void*);
10685   void (*xDel)(void*);  /* Destructor for pUser */
10686 };
10687 
10688 /*
10689 ** A sort order can be either ASC or DESC.
10690 */
10691 #define SQLITE_SO_ASC       0  /* Sort in ascending order */
10692 #define SQLITE_SO_DESC      1  /* Sort in ascending order */
10693 
10694 /*
10695 ** Column affinity types.
10696 **
10697 ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
10698 ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
10699 ** the speed a little by numbering the values consecutively.
10700 **
10701 ** But rather than start with 0 or 1, we begin with 'a'.  That way,
10702 ** when multiple affinity types are concatenated into a string and
10703 ** used as the P4 operand, they will be more readable.
10704 **
10705 ** Note also that the numeric types are grouped together so that testing
10706 ** for a numeric type is a single comparison.
10707 */
10708 #define SQLITE_AFF_TEXT     'a'
10709 #define SQLITE_AFF_NONE     'b'
10710 #define SQLITE_AFF_NUMERIC  'c'
10711 #define SQLITE_AFF_INTEGER  'd'
10712 #define SQLITE_AFF_REAL     'e'
10713 
10714 #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
10715 
10716 /*
10717 ** The SQLITE_AFF_MASK values masks off the significant bits of an
10718 ** affinity value.
10719 */
10720 #define SQLITE_AFF_MASK     0x67
10721 
10722 /*
10723 ** Additional bit values that can be ORed with an affinity without
10724 ** changing the affinity.
10725 **
10726 ** The SQLITE_NOTNULL flag is a combination of NULLEQ and JUMPIFNULL.
10727 ** It causes an assert() to fire if either operand to a comparison
10728 ** operator is NULL.  It is added to certain comparison operators to
10729 ** prove that the operands are always NOT NULL.
10730 */
10731 #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
10732 #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
10733 #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
10734 #define SQLITE_NOTNULL      0x88  /* Assert that operands are never NULL */
10735 
10736 /*
10737 ** An object of this type is created for each virtual table present in
10738 ** the database schema.
10739 **
10740 ** If the database schema is shared, then there is one instance of this
10741 ** structure for each database connection (sqlite3*) that uses the shared
10742 ** schema. This is because each database connection requires its own unique
10743 ** instance of the sqlite3_vtab* handle used to access the virtual table
10744 ** implementation. sqlite3_vtab* handles can not be shared between
10745 ** database connections, even when the rest of the in-memory database
10746 ** schema is shared, as the implementation often stores the database
10747 ** connection handle passed to it via the xConnect() or xCreate() method
10748 ** during initialization internally. This database connection handle may
10749 ** then be used by the virtual table implementation to access real tables
10750 ** within the database. So that they appear as part of the callers
10751 ** transaction, these accesses need to be made via the same database
10752 ** connection as that used to execute SQL operations on the virtual table.
10753 **
10754 ** All VTable objects that correspond to a single table in a shared
10755 ** database schema are initially stored in a linked-list pointed to by
10756 ** the Table.pVTable member variable of the corresponding Table object.
10757 ** When an sqlite3_prepare() operation is required to access the virtual
10758 ** table, it searches the list for the VTable that corresponds to the
10759 ** database connection doing the preparing so as to use the correct
10760 ** sqlite3_vtab* handle in the compiled query.
10761 **
10762 ** When an in-memory Table object is deleted (for example when the
10763 ** schema is being reloaded for some reason), the VTable objects are not
10764 ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
10765 ** immediately. Instead, they are moved from the Table.pVTable list to
10766 ** another linked list headed by the sqlite3.pDisconnect member of the
10767 ** corresponding sqlite3 structure. They are then deleted/xDisconnected
10768 ** next time a statement is prepared using said sqlite3*. This is done
10769 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
10770 ** Refer to comments above function sqlite3VtabUnlockList() for an
10771 ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
10772 ** list without holding the corresponding sqlite3.mutex mutex.
10773 **
10774 ** The memory for objects of this type is always allocated by
10775 ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
10776 ** the first argument.
10777 */
10778 struct VTable {
10779   sqlite3 *db;              /* Database connection associated with this table */
10780   Module *pMod;             /* Pointer to module implementation */
10781   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
10782   int nRef;                 /* Number of pointers to this structure */
10783   u8 bConstraint;           /* True if constraints are supported */
10784   int iSavepoint;           /* Depth of the SAVEPOINT stack */
10785   VTable *pNext;            /* Next in linked list (see above) */
10786 };
10787 
10788 /*
10789 ** Each SQL table is represented in memory by an instance of the
10790 ** following structure.
10791 **
10792 ** Table.zName is the name of the table.  The case of the original
10793 ** CREATE TABLE statement is stored, but case is not significant for
10794 ** comparisons.
10795 **
10796 ** Table.nCol is the number of columns in this table.  Table.aCol is a
10797 ** pointer to an array of Column structures, one for each column.
10798 **
10799 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
10800 ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
10801 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
10802 ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
10803 ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
10804 ** is generated for each row of the table.  TF_HasPrimaryKey is set if
10805 ** the table has any PRIMARY KEY, INTEGER or otherwise.
10806 **
10807 ** Table.tnum is the page number for the root BTree page of the table in the
10808 ** database file.  If Table.iDb is the index of the database table backend
10809 ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
10810 ** holds temporary tables and indices.  If TF_Ephemeral is set
10811 ** then the table is stored in a file that is automatically deleted
10812 ** when the VDBE cursor to the table is closed.  In this case Table.tnum
10813 ** refers VDBE cursor number that holds the table open, not to the root
10814 ** page number.  Transient tables are used to hold the results of a
10815 ** sub-query that appears instead of a real table name in the FROM clause
10816 ** of a SELECT statement.
10817 */
10818 struct Table {
10819   char *zName;         /* Name of the table or view */
10820   Column *aCol;        /* Information about each column */
10821   Index *pIndex;       /* List of SQL indexes on this table. */
10822   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
10823   FKey *pFKey;         /* Linked list of all foreign keys in this table */
10824   char *zColAff;       /* String defining the affinity of each column */
10825 #ifndef SQLITE_OMIT_CHECK
10826   ExprList *pCheck;    /* All CHECK constraints */
10827 #endif
10828   tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
10829   int tnum;            /* Root BTree node for this table (see note above) */
10830   i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
10831   i16 nCol;            /* Number of columns in this table */
10832   u16 nRef;            /* Number of pointers to this Table */
10833   LogEst szTabRow;     /* Estimated size of each table row in bytes */
10834   u8 tabFlags;         /* Mask of TF_* values */
10835   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
10836 #ifndef SQLITE_OMIT_ALTERTABLE
10837   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
10838 #endif
10839 #ifndef SQLITE_OMIT_VIRTUALTABLE
10840   int nModuleArg;      /* Number of arguments to the module */
10841   char **azModuleArg;  /* Text of all module args. [0] is module name */
10842   VTable *pVTable;     /* List of VTable objects. */
10843 #endif
10844   Trigger *pTrigger;   /* List of triggers stored in pSchema */
10845   Schema *pSchema;     /* Schema that contains this table */
10846   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
10847 };
10848 
10849 /*
10850 ** Allowed values for Table.tabFlags.
10851 */
10852 #define TF_Readonly        0x01    /* Read-only system table */
10853 #define TF_Ephemeral       0x02    /* An ephemeral table */
10854 #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
10855 #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
10856 #define TF_Virtual         0x10    /* Is a virtual table */
10857 #define TF_WithoutRowid    0x20    /* No rowid used. PRIMARY KEY is the key */
10858 
10859 
10860 /*
10861 ** Test to see whether or not a table is a virtual table.  This is
10862 ** done as a macro so that it will be optimized out when virtual
10863 ** table support is omitted from the build.
10864 */
10865 #ifndef SQLITE_OMIT_VIRTUALTABLE
10866 #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
10867 #  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
10868 #else
10869 #  define IsVirtual(X)      0
10870 #  define IsHiddenColumn(X) 0
10871 #endif
10872 
10873 /* Does the table have a rowid */
10874 #define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
10875 
10876 /*
10877 ** Each foreign key constraint is an instance of the following structure.
10878 **
10879 ** A foreign key is associated with two tables.  The "from" table is
10880 ** the table that contains the REFERENCES clause that creates the foreign
10881 ** key.  The "to" table is the table that is named in the REFERENCES clause.
10882 ** Consider this example:
10883 **
10884 **     CREATE TABLE ex1(
10885 **       a INTEGER PRIMARY KEY,
10886 **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
10887 **     );
10888 **
10889 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
10890 ** Equivalent names:
10891 **
10892 **     from-table == child-table
10893 **       to-table == parent-table
10894 **
10895 ** Each REFERENCES clause generates an instance of the following structure
10896 ** which is attached to the from-table.  The to-table need not exist when
10897 ** the from-table is created.  The existence of the to-table is not checked.
10898 **
10899 ** The list of all parents for child Table X is held at X.pFKey.
10900 **
10901 ** A list of all children for a table named Z (which might not even exist)
10902 ** is held in Schema.fkeyHash with a hash key of Z.
10903 */
10904 struct FKey {
10905   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
10906   FKey *pNextFrom;  /* Next FKey with the same in pFrom. Next parent of pFrom */
10907   char *zTo;        /* Name of table that the key points to (aka: Parent) */
10908   FKey *pNextTo;    /* Next with the same zTo. Next child of zTo. */
10909   FKey *pPrevTo;    /* Previous with the same zTo */
10910   int nCol;         /* Number of columns in this key */
10911   /* EV: R-30323-21917 */
10912   u8 isDeferred;       /* True if constraint checking is deferred till COMMIT */
10913   u8 aAction[2];        /* ON DELETE and ON UPDATE actions, respectively */
10914   Trigger *apTrigger[2];/* Triggers for aAction[] actions */
10915   struct sColMap {      /* Mapping of columns in pFrom to columns in zTo */
10916     int iFrom;            /* Index of column in pFrom */
10917     char *zCol;           /* Name of column in zTo.  If NULL use PRIMARY KEY */
10918   } aCol[1];            /* One entry for each of nCol columns */
10919 };
10920 
10921 /*
10922 ** SQLite supports many different ways to resolve a constraint
10923 ** error.  ROLLBACK processing means that a constraint violation
10924 ** causes the operation in process to fail and for the current transaction
10925 ** to be rolled back.  ABORT processing means the operation in process
10926 ** fails and any prior changes from that one operation are backed out,
10927 ** but the transaction is not rolled back.  FAIL processing means that
10928 ** the operation in progress stops and returns an error code.  But prior
10929 ** changes due to the same operation are not backed out and no rollback
10930 ** occurs.  IGNORE means that the particular row that caused the constraint
10931 ** error is not inserted or updated.  Processing continues and no error
10932 ** is returned.  REPLACE means that preexisting database rows that caused
10933 ** a UNIQUE constraint violation are removed so that the new insert or
10934 ** update can proceed.  Processing continues and no error is reported.
10935 **
10936 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
10937 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
10938 ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
10939 ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
10940 ** referenced table row is propagated into the row that holds the
10941 ** foreign key.
10942 **
10943 ** The following symbolic values are used to record which type
10944 ** of action to take.
10945 */
10946 #define OE_None     0   /* There is no constraint to check */
10947 #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
10948 #define OE_Abort    2   /* Back out changes but do no rollback transaction */
10949 #define OE_Fail     3   /* Stop the operation but leave all prior changes */
10950 #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
10951 #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
10952 
10953 #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
10954 #define OE_SetNull  7   /* Set the foreign key value to NULL */
10955 #define OE_SetDflt  8   /* Set the foreign key value to its default */
10956 #define OE_Cascade  9   /* Cascade the changes */
10957 
10958 #define OE_Default  10  /* Do whatever the default action is */
10959 
10960 
10961 /*
10962 ** An instance of the following structure is passed as the first
10963 ** argument to sqlite3VdbeKeyCompare and is used to control the
10964 ** comparison of the two index keys.
10965 **
10966 ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
10967 ** are nField slots for the columns of an index then one extra slot
10968 ** for the rowid at the end.
10969 */
10970 struct KeyInfo {
10971   u32 nRef;           /* Number of references to this KeyInfo object */
10972   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
10973   u16 nField;         /* Number of key columns in the index */
10974   u16 nXField;        /* Number of columns beyond the key columns */
10975   sqlite3 *db;        /* The database connection */
10976   u8 *aSortOrder;     /* Sort order for each column. */
10977   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
10978 };
10979 
10980 /*
10981 ** An instance of the following structure holds information about a
10982 ** single index record that has already been parsed out into individual
10983 ** values.
10984 **
10985 ** A record is an object that contains one or more fields of data.
10986 ** Records are used to store the content of a table row and to store
10987 ** the key of an index.  A blob encoding of a record is created by
10988 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10989 ** OP_Column opcode.
10990 **
10991 ** This structure holds a record that has already been disassembled
10992 ** into its constituent fields.
10993 **
10994 ** The r1 and r2 member variables are only used by the optimized comparison
10995 ** functions vdbeRecordCompareInt() and vdbeRecordCompareString().
10996 */
10997 struct UnpackedRecord {
10998   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
10999   u16 nField;         /* Number of entries in apMem[] */
11000   i8 default_rc;      /* Comparison result if keys are equal */
11001   Mem *aMem;          /* Values */
11002   int r1;             /* Value to return if (lhs > rhs) */
11003   int r2;             /* Value to return if (rhs < lhs) */
11004 };
11005 
11006 
11007 /*
11008 ** Each SQL index is represented in memory by an
11009 ** instance of the following structure.
11010 **
11011 ** The columns of the table that are to be indexed are described
11012 ** by the aiColumn[] field of this structure.  For example, suppose
11013 ** we have the following table and index:
11014 **
11015 **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
11016 **     CREATE INDEX Ex2 ON Ex1(c3,c1);
11017 **
11018 ** In the Table structure describing Ex1, nCol==3 because there are
11019 ** three columns in the table.  In the Index structure describing
11020 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
11021 ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
11022 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
11023 ** The second column to be indexed (c1) has an index of 0 in
11024 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
11025 **
11026 ** The Index.onError field determines whether or not the indexed columns
11027 ** must be unique and what to do if they are not.  When Index.onError=OE_None,
11028 ** it means this is not a unique index.  Otherwise it is a unique index
11029 ** and the value of Index.onError indicate the which conflict resolution
11030 ** algorithm to employ whenever an attempt is made to insert a non-unique
11031 ** element.
11032 */
11033 struct Index {
11034   char *zName;             /* Name of this index */
11035   i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
11036   tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
11037   Table *pTable;           /* The SQL table being indexed */
11038   char *zColAff;           /* String defining the affinity of each column */
11039   Index *pNext;            /* The next index associated with the same table */
11040   Schema *pSchema;         /* Schema containing this index */
11041   u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
11042   char **azColl;           /* Array of collation sequence names for index */
11043   Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
11044   KeyInfo *pKeyInfo;       /* A KeyInfo object suitable for this index */
11045   int tnum;                /* DB Page containing root of this index */
11046   LogEst szIdxRow;         /* Estimated average row size in bytes */
11047   u16 nKeyCol;             /* Number of columns forming the key */
11048   u16 nColumn;             /* Number of columns stored in the index */
11049   u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
11050   unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
11051   unsigned bUnordered:1;   /* Use this index for == or IN queries only */
11052   unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
11053   unsigned isResized:1;    /* True if resizeIndexObject() has been called */
11054   unsigned isCovering:1;   /* True if this is a covering index */
11055 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
11056   int nSample;             /* Number of elements in aSample[] */
11057   int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
11058   tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
11059   IndexSample *aSample;    /* Samples of the left-most key */
11060 #endif
11061 };
11062 
11063 /*
11064 ** Each sample stored in the sqlite_stat3 table is represented in memory
11065 ** using a structure of this type.  See documentation at the top of the
11066 ** analyze.c source file for additional information.
11067 */
11068 struct IndexSample {
11069   void *p;          /* Pointer to sampled record */
11070   int n;            /* Size of record in bytes */
11071   tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
11072   tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
11073   tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
11074 };
11075 
11076 /*
11077 ** Each token coming out of the lexer is an instance of
11078 ** this structure.  Tokens are also used as part of an expression.
11079 **
11080 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
11081 ** may contain random values.  Do not make any assumptions about Token.dyn
11082 ** and Token.n when Token.z==0.
11083 */
11084 struct Token {
11085   const char *z;     /* Text of the token.  Not NULL-terminated! */
11086   unsigned int n;    /* Number of characters in this token */
11087 };
11088 
11089 /*
11090 ** An instance of this structure contains information needed to generate
11091 ** code for a SELECT that contains aggregate functions.
11092 **
11093 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
11094 ** pointer to this structure.  The Expr.iColumn field is the index in
11095 ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
11096 ** code for that node.
11097 **
11098 ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
11099 ** original Select structure that describes the SELECT statement.  These
11100 ** fields do not need to be freed when deallocating the AggInfo structure.
11101 */
11102 struct AggInfo {
11103   u8 directMode;          /* Direct rendering mode means take data directly
11104                           ** from source tables rather than from accumulators */
11105   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
11106                           ** than the source table */
11107   int sortingIdx;         /* Cursor number of the sorting index */
11108   int sortingIdxPTab;     /* Cursor number of pseudo-table */
11109   int nSortingColumn;     /* Number of columns in the sorting index */
11110   int mnReg, mxReg;       /* Range of registers allocated for aCol and aFunc */
11111   ExprList *pGroupBy;     /* The group by clause */
11112   struct AggInfo_col {    /* For each column used in source tables */
11113     Table *pTab;             /* Source table */
11114     int iTable;              /* Cursor number of the source table */
11115     int iColumn;             /* Column number within the source table */
11116     int iSorterColumn;       /* Column number in the sorting index */
11117     int iMem;                /* Memory location that acts as accumulator */
11118     Expr *pExpr;             /* The original expression */
11119   } *aCol;
11120   int nColumn;            /* Number of used entries in aCol[] */
11121   int nAccumulator;       /* Number of columns that show through to the output.
11122                           ** Additional columns are used only as parameters to
11123                           ** aggregate functions */
11124   struct AggInfo_func {   /* For each aggregate function */
11125     Expr *pExpr;             /* Expression encoding the function */
11126     FuncDef *pFunc;          /* The aggregate function implementation */
11127     int iMem;                /* Memory location that acts as accumulator */
11128     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
11129   } *aFunc;
11130   int nFunc;              /* Number of entries in aFunc[] */
11131 };
11132 
11133 /*
11134 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
11135 ** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
11136 ** than 32767 we have to make it 32-bit.  16-bit is preferred because
11137 ** it uses less memory in the Expr object, which is a big memory user
11138 ** in systems with lots of prepared statements.  And few applications
11139 ** need more than about 10 or 20 variables.  But some extreme users want
11140 ** to have prepared statements with over 32767 variables, and for them
11141 ** the option is available (at compile-time).
11142 */
11143 #if SQLITE_MAX_VARIABLE_NUMBER<=32767
11144 typedef i16 ynVar;
11145 #else
11146 typedef int ynVar;
11147 #endif
11148 
11149 /*
11150 ** Each node of an expression in the parse tree is an instance
11151 ** of this structure.
11152 **
11153 ** Expr.op is the opcode. The integer parser token codes are reused
11154 ** as opcodes here. For example, the parser defines TK_GE to be an integer
11155 ** code representing the ">=" operator. This same integer code is reused
11156 ** to represent the greater-than-or-equal-to operator in the expression
11157 ** tree.
11158 **
11159 ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
11160 ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
11161 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
11162 ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
11163 ** then Expr.token contains the name of the function.
11164 **
11165 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
11166 ** binary operator. Either or both may be NULL.
11167 **
11168 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
11169 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
11170 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
11171 ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
11172 ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
11173 ** valid.
11174 **
11175 ** An expression of the form ID or ID.ID refers to a column in a table.
11176 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
11177 ** the integer cursor number of a VDBE cursor pointing to that table and
11178 ** Expr.iColumn is the column number for the specific column.  If the
11179 ** expression is used as a result in an aggregate SELECT, then the
11180 ** value is also stored in the Expr.iAgg column in the aggregate so that
11181 ** it can be accessed after all aggregates are computed.
11182 **
11183 ** If the expression is an unbound variable marker (a question mark
11184 ** character '?' in the original SQL) then the Expr.iTable holds the index
11185 ** number for that variable.
11186 **
11187 ** If the expression is a subquery then Expr.iColumn holds an integer
11188 ** register number containing the result of the subquery.  If the
11189 ** subquery gives a constant result, then iTable is -1.  If the subquery
11190 ** gives a different answer at different times during statement processing
11191 ** then iTable is the address of a subroutine that computes the subquery.
11192 **
11193 ** If the Expr is of type OP_Column, and the table it is selecting from
11194 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
11195 ** corresponding table definition.
11196 **
11197 ** ALLOCATION NOTES:
11198 **
11199 ** Expr objects can use a lot of memory space in database schema.  To
11200 ** help reduce memory requirements, sometimes an Expr object will be
11201 ** truncated.  And to reduce the number of memory allocations, sometimes
11202 ** two or more Expr objects will be stored in a single memory allocation,
11203 ** together with Expr.zToken strings.
11204 **
11205 ** If the EP_Reduced and EP_TokenOnly flags are set when
11206 ** an Expr object is truncated.  When EP_Reduced is set, then all
11207 ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
11208 ** are contained within the same memory allocation.  Note, however, that
11209 ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
11210 ** allocated, regardless of whether or not EP_Reduced is set.
11211 */
11212 struct Expr {
11213   u8 op;                 /* Operation performed by this node */
11214   char affinity;         /* The affinity of the column or 0 if not a column */
11215   u32 flags;             /* Various flags.  EP_* See below */
11216   union {
11217     char *zToken;          /* Token value. Zero terminated and dequoted */
11218     int iValue;            /* Non-negative integer value if EP_IntValue */
11219   } u;
11220 
11221   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
11222   ** space is allocated for the fields below this point. An attempt to
11223   ** access them will result in a segfault or malfunction.
11224   *********************************************************************/
11225 
11226   Expr *pLeft;           /* Left subnode */
11227   Expr *pRight;          /* Right subnode */
11228   union {
11229     ExprList *pList;     /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
11230     Select *pSelect;     /* EP_xIsSelect and op = IN, EXISTS, SELECT */
11231   } x;
11232 
11233   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
11234   ** space is allocated for the fields below this point. An attempt to
11235   ** access them will result in a segfault or malfunction.
11236   *********************************************************************/
11237 
11238 #if SQLITE_MAX_EXPR_DEPTH>0
11239   int nHeight;           /* Height of the tree headed by this node */
11240 #endif
11241   int iTable;            /* TK_COLUMN: cursor number of table holding column
11242                          ** TK_REGISTER: register number
11243                          ** TK_TRIGGER: 1 -> new, 0 -> old
11244                          ** EP_Unlikely:  1000 times likelihood */
11245   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
11246                          ** TK_VARIABLE: variable number (always >= 1). */
11247   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
11248   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
11249   u8 op2;                /* TK_REGISTER: original value of Expr.op
11250                          ** TK_COLUMN: the value of p5 for OP_Column
11251                          ** TK_AGG_FUNCTION: nesting depth */
11252   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
11253   Table *pTab;           /* Table for TK_COLUMN expressions. */
11254 };
11255 
11256 /*
11257 ** The following are the meanings of bits in the Expr.flags field.
11258 */
11259 #define EP_FromJoin  0x000001 /* Originated in ON or USING clause of a join */
11260 #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
11261 #define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
11262 #define EP_Error     0x000008 /* Expression contains one or more errors */
11263 #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
11264 #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
11265 #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
11266 #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
11267 #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
11268       /* unused      0x000200 */
11269 #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
11270 #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
11271 #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
11272 #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
11273 #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
11274 #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
11275 #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
11276 #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
11277 #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
11278 #define EP_Constant  0x080000 /* Node is a constant */
11279 
11280 /*
11281 ** These macros can be used to test, set, or clear bits in the
11282 ** Expr.flags field.
11283 */
11284 #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
11285 #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
11286 #define ExprSetProperty(E,P)     (E)->flags|=(P)
11287 #define ExprClearProperty(E,P)   (E)->flags&=~(P)
11288 
11289 /* The ExprSetVVAProperty() macro is used for Verification, Validation,
11290 ** and Accreditation only.  It works like ExprSetProperty() during VVA
11291 ** processes but is a no-op for delivery.
11292 */
11293 #ifdef SQLITE_DEBUG
11294 # define ExprSetVVAProperty(E,P)  (E)->flags|=(P)
11295 #else
11296 # define ExprSetVVAProperty(E,P)
11297 #endif
11298 
11299 /*
11300 ** Macros to determine the number of bytes required by a normal Expr
11301 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
11302 ** and an Expr struct with the EP_TokenOnly flag set.
11303 */
11304 #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
11305 #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
11306 #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
11307 
11308 /*
11309 ** Flags passed to the sqlite3ExprDup() function. See the header comment
11310 ** above sqlite3ExprDup() for details.
11311 */
11312 #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
11313 
11314 /*
11315 ** A list of expressions.  Each expression may optionally have a
11316 ** name.  An expr/name combination can be used in several ways, such
11317 ** as the list of "expr AS ID" fields following a "SELECT" or in the
11318 ** list of "ID = expr" items in an UPDATE.  A list of expressions can
11319 ** also be used as the argument to a function, in which case the a.zName
11320 ** field is not used.
11321 **
11322 ** By default the Expr.zSpan field holds a human-readable description of
11323 ** the expression that is used in the generation of error messages and
11324 ** column labels.  In this case, Expr.zSpan is typically the text of a
11325 ** column expression as it exists in a SELECT statement.  However, if
11326 ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
11327 ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
11328 ** form is used for name resolution with nested FROM clauses.
11329 */
11330 struct ExprList {
11331   int nExpr;             /* Number of expressions on the list */
11332   int iECursor;          /* VDBE Cursor associated with this ExprList */
11333   struct ExprList_item { /* For each expression in the list */
11334     Expr *pExpr;            /* The list of expressions */
11335     char *zName;            /* Token associated with this expression */
11336     char *zSpan;            /* Original text of the expression */
11337     u8 sortOrder;           /* 1 for DESC or 0 for ASC */
11338     unsigned done :1;       /* A flag to indicate when processing is finished */
11339     unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
11340     unsigned reusable :1;   /* Constant expression is reusable */
11341     union {
11342       struct {
11343         u16 iOrderByCol;      /* For ORDER BY, column number in result set */
11344         u16 iAlias;           /* Index into Parse.aAlias[] for zName */
11345       } x;
11346       int iConstExprReg;      /* Register in which Expr value is cached */
11347     } u;
11348   } *a;                  /* Alloc a power of two greater or equal to nExpr */
11349 };
11350 
11351 /*
11352 ** An instance of this structure is used by the parser to record both
11353 ** the parse tree for an expression and the span of input text for an
11354 ** expression.
11355 */
11356 struct ExprSpan {
11357   Expr *pExpr;          /* The expression parse tree */
11358   const char *zStart;   /* First character of input text */
11359   const char *zEnd;     /* One character past the end of input text */
11360 };
11361 
11362 /*
11363 ** An instance of this structure can hold a simple list of identifiers,
11364 ** such as the list "a,b,c" in the following statements:
11365 **
11366 **      INSERT INTO t(a,b,c) VALUES ...;
11367 **      CREATE INDEX idx ON t(a,b,c);
11368 **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
11369 **
11370 ** The IdList.a.idx field is used when the IdList represents the list of
11371 ** column names after a table name in an INSERT statement.  In the statement
11372 **
11373 **     INSERT INTO t(a,b,c) ...
11374 **
11375 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
11376 */
11377 struct IdList {
11378   struct IdList_item {
11379     char *zName;      /* Name of the identifier */
11380     int idx;          /* Index in some Table.aCol[] of a column named zName */
11381   } *a;
11382   int nId;         /* Number of identifiers on the list */
11383 };
11384 
11385 /*
11386 ** The bitmask datatype defined below is used for various optimizations.
11387 **
11388 ** Changing this from a 64-bit to a 32-bit type limits the number of
11389 ** tables in a join to 32 instead of 64.  But it also reduces the size
11390 ** of the library by 738 bytes on ix86.
11391 */
11392 typedef u64 Bitmask;
11393 
11394 /*
11395 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
11396 */
11397 #define BMS  ((int)(sizeof(Bitmask)*8))
11398 
11399 /*
11400 ** A bit in a Bitmask
11401 */
11402 #define MASKBIT(n)   (((Bitmask)1)<<(n))
11403 #define MASKBIT32(n) (((unsigned int)1)<<(n))
11404 
11405 /*
11406 ** The following structure describes the FROM clause of a SELECT statement.
11407 ** Each table or subquery in the FROM clause is a separate element of
11408 ** the SrcList.a[] array.
11409 **
11410 ** With the addition of multiple database support, the following structure
11411 ** can also be used to describe a particular table such as the table that
11412 ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
11413 ** such a table must be a simple name: ID.  But in SQLite, the table can
11414 ** now be identified by a database name, a dot, then the table name: ID.ID.
11415 **
11416 ** The jointype starts out showing the join type between the current table
11417 ** and the next table on the list.  The parser builds the list this way.
11418 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
11419 ** jointype expresses the join between the table and the previous table.
11420 **
11421 ** In the colUsed field, the high-order bit (bit 63) is set if the table
11422 ** contains more than 63 columns and the 64-th or later column is used.
11423 */
11424 struct SrcList {
11425   int nSrc;        /* Number of tables or subqueries in the FROM clause */
11426   u32 nAlloc;      /* Number of entries allocated in a[] below */
11427   struct SrcList_item {
11428     Schema *pSchema;  /* Schema to which this item is fixed */
11429     char *zDatabase;  /* Name of database holding this table */
11430     char *zName;      /* Name of the table */
11431     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
11432     Table *pTab;      /* An SQL table corresponding to zName */
11433     Select *pSelect;  /* A SELECT statement used in place of a table name */
11434     int addrFillSub;  /* Address of subroutine to manifest a subquery */
11435     int regReturn;    /* Register holding return address of addrFillSub */
11436     int regResult;    /* Registers holding results of a co-routine */
11437     u8 jointype;      /* Type of join between this able and the previous */
11438     unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
11439     unsigned isCorrelated :1;  /* True if sub-query is correlated */
11440     unsigned viaCoroutine :1;  /* Implemented as a co-routine */
11441     unsigned isRecursive :1;   /* True for recursive reference in WITH */
11442 #ifndef SQLITE_OMIT_EXPLAIN
11443     u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
11444 #endif
11445     int iCursor;      /* The VDBE cursor number used to access this table */
11446     Expr *pOn;        /* The ON clause of a join */
11447     IdList *pUsing;   /* The USING clause of a join */
11448     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
11449     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
11450     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
11451   } a[1];             /* One entry for each identifier on the list */
11452 };
11453 
11454 /*
11455 ** Permitted values of the SrcList.a.jointype field
11456 */
11457 #define JT_INNER     0x0001    /* Any kind of inner or cross join */
11458 #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
11459 #define JT_NATURAL   0x0004    /* True for a "natural" join */
11460 #define JT_LEFT      0x0008    /* Left outer join */
11461 #define JT_RIGHT     0x0010    /* Right outer join */
11462 #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
11463 #define JT_ERROR     0x0040    /* unknown or unsupported join type */
11464 
11465 
11466 /*
11467 ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
11468 ** and the WhereInfo.wctrlFlags member.
11469 */
11470 #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
11471 #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
11472 #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
11473 #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
11474 #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
11475 #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
11476 #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
11477 #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
11478 #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
11479 #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
11480 #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
11481 #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
11482 
11483 /* Allowed return values from sqlite3WhereIsDistinct()
11484 */
11485 #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
11486 #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
11487 #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
11488 #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
11489 
11490 /*
11491 ** A NameContext defines a context in which to resolve table and column
11492 ** names.  The context consists of a list of tables (the pSrcList) field and
11493 ** a list of named expression (pEList).  The named expression list may
11494 ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
11495 ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
11496 ** pEList corresponds to the result set of a SELECT and is NULL for
11497 ** other statements.
11498 **
11499 ** NameContexts can be nested.  When resolving names, the inner-most
11500 ** context is searched first.  If no match is found, the next outer
11501 ** context is checked.  If there is still no match, the next context
11502 ** is checked.  This process continues until either a match is found
11503 ** or all contexts are check.  When a match is found, the nRef member of
11504 ** the context containing the match is incremented.
11505 **
11506 ** Each subquery gets a new NameContext.  The pNext field points to the
11507 ** NameContext in the parent query.  Thus the process of scanning the
11508 ** NameContext list corresponds to searching through successively outer
11509 ** subqueries looking for a match.
11510 */
11511 struct NameContext {
11512   Parse *pParse;       /* The parser */
11513   SrcList *pSrcList;   /* One or more tables used to resolve names */
11514   ExprList *pEList;    /* Optional list of result-set columns */
11515   AggInfo *pAggInfo;   /* Information about aggregates at this level */
11516   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
11517   int nRef;            /* Number of names resolved by this context */
11518   int nErr;            /* Number of errors encountered while resolving names */
11519   u8 ncFlags;          /* Zero or more NC_* flags defined below */
11520 };
11521 
11522 /*
11523 ** Allowed values for the NameContext, ncFlags field.
11524 */
11525 #define NC_AllowAgg  0x01    /* Aggregate functions are allowed here */
11526 #define NC_HasAgg    0x02    /* One or more aggregate functions seen */
11527 #define NC_IsCheck   0x04    /* True if resolving names in a CHECK constraint */
11528 #define NC_InAggFunc 0x08    /* True if analyzing arguments to an agg func */
11529 #define NC_PartIdx   0x10    /* True if resolving a partial index WHERE */
11530 
11531 /*
11532 ** An instance of the following structure contains all information
11533 ** needed to generate code for a single SELECT statement.
11534 **
11535 ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
11536 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
11537 ** limit and nOffset to the value of the offset (or 0 if there is not
11538 ** offset).  But later on, nLimit and nOffset become the memory locations
11539 ** in the VDBE that record the limit and offset counters.
11540 **
11541 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
11542 ** These addresses must be stored so that we can go back and fill in
11543 ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
11544 ** the number of columns in P2 can be computed at the same time
11545 ** as the OP_OpenEphm instruction is coded because not
11546 ** enough information about the compound query is known at that point.
11547 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
11548 ** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
11549 ** sequences for the ORDER BY clause.
11550 */
11551 struct Select {
11552   ExprList *pEList;      /* The fields of the result */
11553   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
11554   u16 selFlags;          /* Various SF_* values */
11555   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
11556   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
11557   u64 nSelectRow;        /* Estimated number of result rows */
11558   SrcList *pSrc;         /* The FROM clause */
11559   Expr *pWhere;          /* The WHERE clause */
11560   ExprList *pGroupBy;    /* The GROUP BY clause */
11561   Expr *pHaving;         /* The HAVING clause */
11562   ExprList *pOrderBy;    /* The ORDER BY clause */
11563   Select *pPrior;        /* Prior select in a compound select statement */
11564   Select *pNext;         /* Next select to the left in a compound */
11565   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
11566   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
11567   With *pWith;           /* WITH clause attached to this select. Or NULL. */
11568 };
11569 
11570 /*
11571 ** Allowed values for Select.selFlags.  The "SF" prefix stands for
11572 ** "Select Flag".
11573 */
11574 #define SF_Distinct        0x0001  /* Output should be DISTINCT */
11575 #define SF_Resolved        0x0002  /* Identifiers have been resolved */
11576 #define SF_Aggregate       0x0004  /* Contains aggregate functions */
11577 #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
11578 #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
11579 #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
11580 #define SF_UseSorter       0x0040  /* Sort using a sorter */
11581 #define SF_Values          0x0080  /* Synthesized from VALUES clause */
11582 #define SF_Materialize     0x0100  /* NOT USED */
11583 #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
11584 #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
11585 #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
11586 #define SF_Compound        0x1000  /* Part of a compound query */
11587 
11588 
11589 /*
11590 ** The results of a SELECT can be distributed in several ways, as defined
11591 ** by one of the following macros.  The "SRT" prefix means "SELECT Result
11592 ** Type".
11593 **
11594 **     SRT_Union       Store results as a key in a temporary index
11595 **                     identified by pDest->iSDParm.
11596 **
11597 **     SRT_Except      Remove results from the temporary index pDest->iSDParm.
11598 **
11599 **     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
11600 **                     set is not empty.
11601 **
11602 **     SRT_Discard     Throw the results away.  This is used by SELECT
11603 **                     statements within triggers whose only purpose is
11604 **                     the side-effects of functions.
11605 **
11606 ** All of the above are free to ignore their ORDER BY clause. Those that
11607 ** follow must honor the ORDER BY clause.
11608 **
11609 **     SRT_Output      Generate a row of output (using the OP_ResultRow
11610 **                     opcode) for each row in the result set.
11611 **
11612 **     SRT_Mem         Only valid if the result is a single column.
11613 **                     Store the first column of the first result row
11614 **                     in register pDest->iSDParm then abandon the rest
11615 **                     of the query.  This destination implies "LIMIT 1".
11616 **
11617 **     SRT_Set         The result must be a single column.  Store each
11618 **                     row of result as the key in table pDest->iSDParm.
11619 **                     Apply the affinity pDest->affSdst before storing
11620 **                     results.  Used to implement "IN (SELECT ...)".
11621 **
11622 **     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
11623 **                     the result there. The cursor is left open after
11624 **                     returning.  This is like SRT_Table except that
11625 **                     this destination uses OP_OpenEphemeral to create
11626 **                     the table first.
11627 **
11628 **     SRT_Coroutine   Generate a co-routine that returns a new row of
11629 **                     results each time it is invoked.  The entry point
11630 **                     of the co-routine is stored in register pDest->iSDParm
11631 **                     and the result row is stored in pDest->nDest registers
11632 **                     starting with pDest->iSdst.
11633 **
11634 **     SRT_Table       Store results in temporary table pDest->iSDParm.
11635 **                     This is like SRT_EphemTab except that the table
11636 **                     is assumed to already be open.
11637 **
11638 **     SRT_DistTable   Store results in a temporary table pDest->iSDParm.
11639 **                     But also use temporary table pDest->iSDParm+1 as
11640 **                     a record of all prior results and ignore any duplicate
11641 **                     rows.  Name means:  "Distinct Table".
11642 **
11643 **     SRT_Queue       Store results in priority queue pDest->iSDParm (really
11644 **                     an index).  Append a sequence number so that all entries
11645 **                     are distinct.
11646 **
11647 **     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
11648 **                     the same record has never been stored before.  The
11649 **                     index at pDest->iSDParm+1 hold all prior stores.
11650 */
11651 #define SRT_Union        1  /* Store result as keys in an index */
11652 #define SRT_Except       2  /* Remove result from a UNION index */
11653 #define SRT_Exists       3  /* Store 1 if the result is not empty */
11654 #define SRT_Discard      4  /* Do not save the results anywhere */
11655 
11656 /* The ORDER BY clause is ignored for all of the above */
11657 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
11658 
11659 #define SRT_Output       5  /* Output each row of result */
11660 #define SRT_Mem          6  /* Store result in a memory cell */
11661 #define SRT_Set          7  /* Store results as keys in an index */
11662 #define SRT_EphemTab     8  /* Create transient tab and store like SRT_Table */
11663 #define SRT_Coroutine    9  /* Generate a single row of result */
11664 #define SRT_Table       10  /* Store result as data with an automatic rowid */
11665 #define SRT_DistTable   11  /* Like SRT_Table, but unique results only */
11666 #define SRT_Queue       12  /* Store result in an queue */
11667 #define SRT_DistQueue   13  /* Like SRT_Queue, but unique results only */
11668 
11669 /*
11670 ** An instance of this object describes where to put of the results of
11671 ** a SELECT statement.
11672 */
11673 struct SelectDest {
11674   u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
11675   char affSdst;        /* Affinity used when eDest==SRT_Set */
11676   int iSDParm;         /* A parameter used by the eDest disposal method */
11677   int iSdst;           /* Base register where results are written */
11678   int nSdst;           /* Number of registers allocated */
11679   ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
11680 };
11681 
11682 /*
11683 ** During code generation of statements that do inserts into AUTOINCREMENT
11684 ** tables, the following information is attached to the Table.u.autoInc.p
11685 ** pointer of each autoincrement table to record some side information that
11686 ** the code generator needs.  We have to keep per-table autoincrement
11687 ** information in case inserts are down within triggers.  Triggers do not
11688 ** normally coordinate their activities, but we do need to coordinate the
11689 ** loading and saving of autoincrement information.
11690 */
11691 struct AutoincInfo {
11692   AutoincInfo *pNext;   /* Next info block in a list of them all */
11693   Table *pTab;          /* Table this info block refers to */
11694   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
11695   int regCtr;           /* Memory register holding the rowid counter */
11696 };
11697 
11698 /*
11699 ** Size of the column cache
11700 */
11701 #ifndef SQLITE_N_COLCACHE
11702 # define SQLITE_N_COLCACHE 10
11703 #endif
11704 
11705 /*
11706 ** At least one instance of the following structure is created for each
11707 ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
11708 ** statement. All such objects are stored in the linked list headed at
11709 ** Parse.pTriggerPrg and deleted once statement compilation has been
11710 ** completed.
11711 **
11712 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
11713 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
11714 ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
11715 ** The Parse.pTriggerPrg list never contains two entries with the same
11716 ** values for both pTrigger and orconf.
11717 **
11718 ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
11719 ** accessed (or set to 0 for triggers fired as a result of INSERT
11720 ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
11721 ** a mask of new.* columns used by the program.
11722 */
11723 struct TriggerPrg {
11724   Trigger *pTrigger;      /* Trigger this program was coded from */
11725   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
11726   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
11727   int orconf;             /* Default ON CONFLICT policy */
11728   u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
11729 };
11730 
11731 /*
11732 ** The yDbMask datatype for the bitmask of all attached databases.
11733 */
11734 #if SQLITE_MAX_ATTACHED>30
11735   typedef sqlite3_uint64 yDbMask;
11736 #else
11737   typedef unsigned int yDbMask;
11738 #endif
11739 
11740 /*
11741 ** An SQL parser context.  A copy of this structure is passed through
11742 ** the parser and down into all the parser action routine in order to
11743 ** carry around information that is global to the entire parse.
11744 **
11745 ** The structure is divided into two parts.  When the parser and code
11746 ** generate call themselves recursively, the first part of the structure
11747 ** is constant but the second part is reset at the beginning and end of
11748 ** each recursion.
11749 **
11750 ** The nTableLock and aTableLock variables are only used if the shared-cache
11751 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
11752 ** used to store the set of table-locks required by the statement being
11753 ** compiled. Function sqlite3TableLock() is used to add entries to the
11754 ** list.
11755 */
11756 struct Parse {
11757   sqlite3 *db;         /* The main database structure */
11758   char *zErrMsg;       /* An error message */
11759   Vdbe *pVdbe;         /* An engine for executing database bytecode */
11760   int rc;              /* Return code from execution */
11761   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
11762   u8 checkSchema;      /* Causes schema cookie check after an error */
11763   u8 nested;           /* Number of nested calls to the parser/code generator */
11764   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
11765   u8 nColCache;        /* Number of entries in aColCache[] */
11766   u8 iColCache;        /* Next entry in aColCache[] to replace */
11767   u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
11768   u8 mayAbort;         /* True if statement may throw an ABORT exception */
11769   u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
11770   u8 okConstFactor;    /* OK to factor out constants */
11771   int aTempReg[8];     /* Holding area for temporary registers */
11772   int nRangeReg;       /* Size of the temporary register block */
11773   int iRangeReg;       /* First register in temporary register block */
11774   int nErr;            /* Number of errors seen */
11775   int nTab;            /* Number of previously allocated VDBE cursors */
11776   int nMem;            /* Number of memory cells used so far */
11777   int nSet;            /* Number of sets used so far */
11778   int nOnce;           /* Number of OP_Once instructions so far */
11779   int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
11780   int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
11781   int ckBase;          /* Base register of data during check constraints */
11782   int iPartIdxTab;     /* Table corresponding to a partial index */
11783   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
11784   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
11785   int nLabel;          /* Number of labels used */
11786   int *aLabel;         /* Space to hold the labels */
11787   struct yColCache {
11788     int iTable;           /* Table cursor number */
11789     i16 iColumn;          /* Table column number */
11790     u8 tempReg;           /* iReg is a temp register that needs to be freed */
11791     int iLevel;           /* Nesting level */
11792     int iReg;             /* Reg with value of this column. 0 means none. */
11793     int lru;              /* Least recently used entry has the smallest value */
11794   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
11795   ExprList *pConstExpr;/* Constant expressions */
11796   Token constraintName;/* Name of the constraint currently being parsed */
11797   yDbMask writeMask;   /* Start a write transaction on these databases */
11798   yDbMask cookieMask;  /* Bitmask of schema verified databases */
11799   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
11800   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
11801   int regRoot;         /* Register holding root page number for new objects */
11802   int nMaxArg;         /* Max args passed to user function by sub-program */
11803 #ifndef SQLITE_OMIT_SHARED_CACHE
11804   int nTableLock;        /* Number of locks in aTableLock */
11805   TableLock *aTableLock; /* Required table locks for shared-cache mode */
11806 #endif
11807   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
11808 
11809   /* Information used while coding trigger programs. */
11810   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
11811   Table *pTriggerTab;  /* Table triggers are being coded for */
11812   int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
11813   int addrSkipPK;      /* Address of instruction to skip PRIMARY KEY index */
11814   u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
11815   u32 oldmask;         /* Mask of old.* columns referenced */
11816   u32 newmask;         /* Mask of new.* columns referenced */
11817   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
11818   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
11819   u8 disableTriggers;  /* True to disable triggers */
11820 
11821   /************************************************************************
11822   ** Above is constant between recursions.  Below is reset before and after
11823   ** each recursion.  The boundary between these two regions is determined
11824   ** using offsetof(Parse,nVar) so the nVar field must be the first field
11825   ** in the recursive region.
11826   ************************************************************************/
11827 
11828   int nVar;                 /* Number of '?' variables seen in the SQL so far */
11829   int nzVar;                /* Number of available slots in azVar[] */
11830   u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
11831   u8 bFreeWith;             /* True if pWith should be freed with parser */
11832   u8 explain;               /* True if the EXPLAIN flag is found on the query */
11833 #ifndef SQLITE_OMIT_VIRTUALTABLE
11834   u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
11835   int nVtabLock;            /* Number of virtual tables to lock */
11836 #endif
11837   int nAlias;               /* Number of aliased result set columns */
11838   int nHeight;              /* Expression tree height of current sub-select */
11839 #ifndef SQLITE_OMIT_EXPLAIN
11840   int iSelectId;            /* ID of current select for EXPLAIN output */
11841   int iNextSelectId;        /* Next available select ID for EXPLAIN output */
11842 #endif
11843   char **azVar;             /* Pointers to names of parameters */
11844   Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
11845   const char *zTail;        /* All SQL text past the last semicolon parsed */
11846   Table *pNewTable;         /* A table being constructed by CREATE TABLE */
11847   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
11848   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
11849   Token sNameToken;         /* Token with unqualified schema object name */
11850   Token sLastToken;         /* The last token parsed */
11851 #ifndef SQLITE_OMIT_VIRTUALTABLE
11852   Token sArg;               /* Complete text of a module argument */
11853   Table **apVtabLock;       /* Pointer to virtual tables needing locking */
11854 #endif
11855   Table *pZombieTab;        /* List of Table objects to delete after code gen */
11856   TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
11857   With *pWith;              /* Current WITH clause, or NULL */
11858 };
11859 
11860 /*
11861 ** Return true if currently inside an sqlite3_declare_vtab() call.
11862 */
11863 #ifdef SQLITE_OMIT_VIRTUALTABLE
11864   #define IN_DECLARE_VTAB 0
11865 #else
11866   #define IN_DECLARE_VTAB (pParse->declareVtab)
11867 #endif
11868 
11869 /*
11870 ** An instance of the following structure can be declared on a stack and used
11871 ** to save the Parse.zAuthContext value so that it can be restored later.
11872 */
11873 struct AuthContext {
11874   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
11875   Parse *pParse;              /* The Parse structure */
11876 };
11877 
11878 /*
11879 ** Bitfield flags for P5 value in various opcodes.
11880 */
11881 #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
11882 #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
11883 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
11884 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
11885 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
11886 #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
11887 #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
11888 #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
11889 #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
11890 #define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
11891 #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
11892 
11893 /*
11894  * Each trigger present in the database schema is stored as an instance of
11895  * struct Trigger.
11896  *
11897  * Pointers to instances of struct Trigger are stored in two ways.
11898  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
11899  *    database). This allows Trigger structures to be retrieved by name.
11900  * 2. All triggers associated with a single table form a linked list, using the
11901  *    pNext member of struct Trigger. A pointer to the first element of the
11902  *    linked list is stored as the "pTrigger" member of the associated
11903  *    struct Table.
11904  *
11905  * The "step_list" member points to the first element of a linked list
11906  * containing the SQL statements specified as the trigger program.
11907  */
11908 struct Trigger {
11909   char *zName;            /* The name of the trigger                        */
11910   char *table;            /* The table or view to which the trigger applies */
11911   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
11912   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
11913   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
11914   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
11915                              the <column-list> is stored here */
11916   Schema *pSchema;        /* Schema containing the trigger */
11917   Schema *pTabSchema;     /* Schema containing the table */
11918   TriggerStep *step_list; /* Link list of trigger program steps             */
11919   Trigger *pNext;         /* Next trigger associated with the table */
11920 };
11921 
11922 /*
11923 ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
11924 ** determine which.
11925 **
11926 ** If there are multiple triggers, you might of some BEFORE and some AFTER.
11927 ** In that cases, the constants below can be ORed together.
11928 */
11929 #define TRIGGER_BEFORE  1
11930 #define TRIGGER_AFTER   2
11931 
11932 /*
11933  * An instance of struct TriggerStep is used to store a single SQL statement
11934  * that is a part of a trigger-program.
11935  *
11936  * Instances of struct TriggerStep are stored in a singly linked list (linked
11937  * using the "pNext" member) referenced by the "step_list" member of the
11938  * associated struct Trigger instance. The first element of the linked list is
11939  * the first step of the trigger-program.
11940  *
11941  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
11942  * "SELECT" statement. The meanings of the other members is determined by the
11943  * value of "op" as follows:
11944  *
11945  * (op == TK_INSERT)
11946  * orconf    -> stores the ON CONFLICT algorithm
11947  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
11948  *              this stores a pointer to the SELECT statement. Otherwise NULL.
11949  * target    -> A token holding the quoted name of the table to insert into.
11950  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
11951  *              this stores values to be inserted. Otherwise NULL.
11952  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
11953  *              statement, then this stores the column-names to be
11954  *              inserted into.
11955  *
11956  * (op == TK_DELETE)
11957  * target    -> A token holding the quoted name of the table to delete from.
11958  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
11959  *              Otherwise NULL.
11960  *
11961  * (op == TK_UPDATE)
11962  * target    -> A token holding the quoted name of the table to update rows of.
11963  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
11964  *              Otherwise NULL.
11965  * pExprList -> A list of the columns to update and the expressions to update
11966  *              them to. See sqlite3Update() documentation of "pChanges"
11967  *              argument.
11968  *
11969  */
11970 struct TriggerStep {
11971   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
11972   u8 orconf;           /* OE_Rollback etc. */
11973   Trigger *pTrig;      /* The trigger that this step is a part of */
11974   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
11975   Token target;        /* Target table for DELETE, UPDATE, INSERT */
11976   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
11977   ExprList *pExprList; /* SET clause for UPDATE. */
11978   IdList *pIdList;     /* Column names for INSERT */
11979   TriggerStep *pNext;  /* Next in the link-list */
11980   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
11981 };
11982 
11983 /*
11984 ** The following structure contains information used by the sqliteFix...
11985 ** routines as they walk the parse tree to make database references
11986 ** explicit.
11987 */
11988 typedef struct DbFixer DbFixer;
11989 struct DbFixer {
11990   Parse *pParse;      /* The parsing context.  Error messages written here */
11991   Schema *pSchema;    /* Fix items to this schema */
11992   int bVarOnly;       /* Check for variable references only */
11993   const char *zDb;    /* Make sure all objects are contained in this database */
11994   const char *zType;  /* Type of the container - used for error messages */
11995   const Token *pName; /* Name of the container - used for error messages */
11996 };
11997 
11998 /*
11999 ** An objected used to accumulate the text of a string where we
12000 ** do not necessarily know how big the string will be in the end.
12001 */
12002 struct StrAccum {
12003   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
12004   char *zBase;         /* A base allocation.  Not from malloc. */
12005   char *zText;         /* The string collected so far */
12006   int  nChar;          /* Length of the string so far */
12007   int  nAlloc;         /* Amount of space allocated in zText */
12008   int  mxAlloc;        /* Maximum allowed string length */
12009   u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
12010   u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
12011 };
12012 #define STRACCUM_NOMEM   1
12013 #define STRACCUM_TOOBIG  2
12014 
12015 /*
12016 ** A pointer to this structure is used to communicate information
12017 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
12018 */
12019 typedef struct {
12020   sqlite3 *db;        /* The database being initialized */
12021   char **pzErrMsg;    /* Error message stored here */
12022   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
12023   int rc;             /* Result code stored here */
12024 } InitData;
12025 
12026 /*
12027 ** Structure containing global configuration data for the SQLite library.
12028 **
12029 ** This structure also contains some state information.
12030 */
12031 struct Sqlite3Config {
12032   int bMemstat;                     /* True to enable memory status */
12033   int bCoreMutex;                   /* True to enable core mutexing */
12034   int bFullMutex;                   /* True to enable full mutexing */
12035   int bOpenUri;                     /* True to interpret filenames as URIs */
12036   int bUseCis;                      /* Use covering indices for full-scans */
12037   int mxStrlen;                     /* Maximum string length */
12038   int neverCorrupt;                 /* Database is always well-formed */
12039   int szLookaside;                  /* Default lookaside buffer size */
12040   int nLookaside;                   /* Default lookaside buffer count */
12041   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
12042   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
12043   sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
12044   void *pHeap;                      /* Heap storage space */
12045   int nHeap;                        /* Size of pHeap[] */
12046   int mnReq, mxReq;                 /* Min and max heap requests sizes */
12047   sqlite3_int64 szMmap;             /* mmap() space per open file */
12048   sqlite3_int64 mxMmap;             /* Maximum value for szMmap */
12049   void *pScratch;                   /* Scratch memory */
12050   int szScratch;                    /* Size of each scratch buffer */
12051   int nScratch;                     /* Number of scratch buffers */
12052   void *pPage;                      /* Page cache memory */
12053   int szPage;                       /* Size of each page in pPage[] */
12054   int nPage;                        /* Number of pages in pPage[] */
12055   int mxParserStack;                /* maximum depth of the parser stack */
12056   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
12057   /* The above might be initialized to non-zero.  The following need to always
12058   ** initially be zero, however. */
12059   int isInit;                       /* True after initialization has finished */
12060   int inProgress;                   /* True while initialization in progress */
12061   int isMutexInit;                  /* True after mutexes are initialized */
12062   int isMallocInit;                 /* True after malloc is initialized */
12063   int isPCacheInit;                 /* True after malloc is initialized */
12064   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
12065   int nRefInitMutex;                /* Number of users of pInitMutex */
12066   void (*xLog)(void*,int,const char*); /* Function for logging */
12067   void *pLogArg;                       /* First argument to xLog() */
12068   int bLocaltimeFault;              /* True to fail localtime() calls */
12069 #ifdef SQLITE_ENABLE_SQLLOG
12070   void(*xSqllog)(void*,sqlite3*,const char*, int);
12071   void *pSqllogArg;
12072 #endif
12073 #ifdef SQLITE_VDBE_COVERAGE
12074   /* The following callback (if not NULL) is invoked on every VDBE branch
12075   ** operation.  Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
12076   */
12077   void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx);  /* Callback */
12078   void *pVdbeBranchArg;                                     /* 1st argument */
12079 #endif
12080 };
12081 
12082 /*
12083 ** This macro is used inside of assert() statements to indicate that
12084 ** the assert is only valid on a well-formed database.  Instead of:
12085 **
12086 **     assert( X );
12087 **
12088 ** One writes:
12089 **
12090 **     assert( X || CORRUPT_DB );
12091 **
12092 ** CORRUPT_DB is true during normal operation.  CORRUPT_DB does not indicate
12093 ** that the database is definitely corrupt, only that it might be corrupt.
12094 ** For most test cases, CORRUPT_DB is set to false using a special
12095 ** sqlite3_test_control().  This enables assert() statements to prove
12096 ** things that are always true for well-formed databases.
12097 */
12098 #define CORRUPT_DB  (sqlite3Config.neverCorrupt==0)
12099 
12100 /*
12101 ** Context pointer passed down through the tree-walk.
12102 */
12103 struct Walker {
12104   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
12105   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
12106   void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
12107   Parse *pParse;                            /* Parser context.  */
12108   int walkerDepth;                          /* Number of subqueries */
12109   union {                                   /* Extra data for callback */
12110     NameContext *pNC;                          /* Naming context */
12111     int i;                                     /* Integer value */
12112     SrcList *pSrcList;                         /* FROM clause */
12113     struct SrcCount *pSrcCount;                /* Counting column references */
12114   } u;
12115 };
12116 
12117 /* Forward declarations */
12118 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
12119 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
12120 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
12121 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
12122 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
12123 
12124 /*
12125 ** Return code from the parse-tree walking primitives and their
12126 ** callbacks.
12127 */
12128 #define WRC_Continue    0   /* Continue down into children */
12129 #define WRC_Prune       1   /* Omit children but continue walking siblings */
12130 #define WRC_Abort       2   /* Abandon the tree walk */
12131 
12132 /*
12133 ** An instance of this structure represents a set of one or more CTEs
12134 ** (common table expressions) created by a single WITH clause.
12135 */
12136 struct With {
12137   int nCte;                       /* Number of CTEs in the WITH clause */
12138   With *pOuter;                   /* Containing WITH clause, or NULL */
12139   struct Cte {                    /* For each CTE in the WITH clause.... */
12140     char *zName;                    /* Name of this CTE */
12141     ExprList *pCols;                /* List of explicit column names, or NULL */
12142     Select *pSelect;                /* The definition of this CTE */
12143     const char *zErr;               /* Error message for circular references */
12144   } a[1];
12145 };
12146 
12147 /*
12148 ** Assuming zIn points to the first byte of a UTF-8 character,
12149 ** advance zIn to point to the first byte of the next UTF-8 character.
12150 */
12151 #define SQLITE_SKIP_UTF8(zIn) {                        \
12152   if( (*(zIn++))>=0xc0 ){                              \
12153     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
12154   }                                                    \
12155 }
12156 
12157 /*
12158 ** The SQLITE_*_BKPT macros are substitutes for the error codes with
12159 ** the same name but without the _BKPT suffix.  These macros invoke
12160 ** routines that report the line-number on which the error originated
12161 ** using sqlite3_log().  The routines also provide a convenient place
12162 ** to set a debugger breakpoint.
12163 */
12164 SQLITE_PRIVATE int sqlite3CorruptError(int);
12165 SQLITE_PRIVATE int sqlite3MisuseError(int);
12166 SQLITE_PRIVATE int sqlite3CantopenError(int);
12167 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
12168 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
12169 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
12170 
12171 
12172 /*
12173 ** FTS4 is really an extension for FTS3.  It is enabled using the
12174 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
12175 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
12176 */
12177 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
12178 # define SQLITE_ENABLE_FTS3
12179 #endif
12180 
12181 /*
12182 ** The ctype.h header is needed for non-ASCII systems.  It is also
12183 ** needed by FTS3 when FTS3 is included in the amalgamation.
12184 */
12185 #if !defined(SQLITE_ASCII) || \
12186     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
12187 # include <ctype.h>
12188 #endif
12189 
12190 /*
12191 ** The following macros mimic the standard library functions toupper(),
12192 ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
12193 ** sqlite versions only work for ASCII characters, regardless of locale.
12194 */
12195 #ifdef SQLITE_ASCII
12196 # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
12197 # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
12198 # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
12199 # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
12200 # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
12201 # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
12202 # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
12203 #else
12204 # define sqlite3Toupper(x)   toupper((unsigned char)(x))
12205 # define sqlite3Isspace(x)   isspace((unsigned char)(x))
12206 # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
12207 # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
12208 # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
12209 # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
12210 # define sqlite3Tolower(x)   tolower((unsigned char)(x))
12211 #endif
12212 
12213 /*
12214 ** Internal function prototypes
12215 */
12216 #define sqlite3StrICmp sqlite3_stricmp
12217 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
12218 #define sqlite3StrNICmp sqlite3_strnicmp
12219 
12220 SQLITE_PRIVATE int sqlite3MallocInit(void);
12221 SQLITE_PRIVATE void sqlite3MallocEnd(void);
12222 SQLITE_PRIVATE void *sqlite3Malloc(int);
12223 SQLITE_PRIVATE void *sqlite3MallocZero(int);
12224 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
12225 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
12226 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
12227 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
12228 SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
12229 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
12230 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
12231 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
12232 SQLITE_PRIVATE int sqlite3MallocSize(void*);
12233 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
12234 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
12235 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
12236 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
12237 SQLITE_PRIVATE void sqlite3PageFree(void*);
12238 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
12239 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
12240 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
12241 
12242 /*
12243 ** On systems with ample stack space and that support alloca(), make
12244 ** use of alloca() to obtain space for large automatic objects.  By default,
12245 ** obtain space from malloc().
12246 **
12247 ** The alloca() routine never returns NULL.  This will cause code paths
12248 ** that deal with sqlite3StackAlloc() failures to be unreachable.
12249 */
12250 #ifdef SQLITE_USE_ALLOCA
12251 # define sqlite3StackAllocRaw(D,N)   alloca(N)
12252 # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
12253 # define sqlite3StackFree(D,P)
12254 #else
12255 # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
12256 # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
12257 # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
12258 #endif
12259 
12260 #ifdef SQLITE_ENABLE_MEMSYS3
12261 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
12262 #endif
12263 #ifdef SQLITE_ENABLE_MEMSYS5
12264 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
12265 #endif
12266 
12267 
12268 #ifndef SQLITE_MUTEX_OMIT
12269 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
12270 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
12271 SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
12272 SQLITE_PRIVATE   int sqlite3MutexInit(void);
12273 SQLITE_PRIVATE   int sqlite3MutexEnd(void);
12274 #endif
12275 
12276 SQLITE_PRIVATE int sqlite3StatusValue(int);
12277 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
12278 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
12279 
12280 #ifndef SQLITE_OMIT_FLOATING_POINT
12281 SQLITE_PRIVATE   int sqlite3IsNaN(double);
12282 #else
12283 # define sqlite3IsNaN(X)  0
12284 #endif
12285 
12286 /*
12287 ** An instance of the following structure holds information about SQL
12288 ** functions arguments that are the parameters to the printf() function.
12289 */
12290 struct PrintfArguments {
12291   int nArg;                /* Total number of arguments */
12292   int nUsed;               /* Number of arguments used so far */
12293   sqlite3_value **apArg;   /* The argument values */
12294 };
12295 
12296 #define SQLITE_PRINTF_INTERNAL 0x01
12297 #define SQLITE_PRINTF_SQLFUNC  0x02
12298 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
12299 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
12300 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
12301 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
12302 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
12303 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
12304 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
12305 #endif
12306 #if defined(SQLITE_TEST)
12307 SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
12308 #endif
12309 
12310 /* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
12311 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
12312 SQLITE_PRIVATE   void sqlite3ExplainBegin(Vdbe*);
12313 SQLITE_PRIVATE   void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
12314 SQLITE_PRIVATE   void sqlite3ExplainNL(Vdbe*);
12315 SQLITE_PRIVATE   void sqlite3ExplainPush(Vdbe*);
12316 SQLITE_PRIVATE   void sqlite3ExplainPop(Vdbe*);
12317 SQLITE_PRIVATE   void sqlite3ExplainFinish(Vdbe*);
12318 SQLITE_PRIVATE   void sqlite3ExplainSelect(Vdbe*, Select*);
12319 SQLITE_PRIVATE   void sqlite3ExplainExpr(Vdbe*, Expr*);
12320 SQLITE_PRIVATE   void sqlite3ExplainExprList(Vdbe*, ExprList*);
12321 SQLITE_PRIVATE   const char *sqlite3VdbeExplanation(Vdbe*);
12322 #else
12323 # define sqlite3ExplainBegin(X)
12324 # define sqlite3ExplainSelect(A,B)
12325 # define sqlite3ExplainExpr(A,B)
12326 # define sqlite3ExplainExprList(A,B)
12327 # define sqlite3ExplainFinish(X)
12328 # define sqlite3VdbeExplanation(X) 0
12329 #endif
12330 
12331 
12332 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
12333 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
12334 SQLITE_PRIVATE int sqlite3Dequote(char*);
12335 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
12336 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
12337 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
12338 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
12339 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
12340 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
12341 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
12342 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
12343 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
12344 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
12345 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
12346 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
12347 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
12348 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
12349 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
12350 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
12351 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
12352 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
12353 SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
12354 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
12355 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
12356 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
12357 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
12358 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
12359 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
12360 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
12361 SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
12362 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
12363 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
12364 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
12365 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
12366 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16);
12367 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
12368 SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
12369 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
12370 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
12371 SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
12372 SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
12373 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
12374 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
12375 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
12376 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
12377                     sqlite3_vfs**,char**,char **);
12378 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
12379 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
12380 
12381 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
12382 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
12383 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
12384 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
12385 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
12386 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
12387 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
12388 
12389 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
12390 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
12391 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
12392 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
12393 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
12394 
12395 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
12396 
12397 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
12398 SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
12399 #else
12400 # define sqlite3ViewGetColumnNames(A,B) 0
12401 #endif
12402 
12403 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
12404 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
12405 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
12406 #ifndef SQLITE_OMIT_AUTOINCREMENT
12407 SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
12408 SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
12409 #else
12410 # define sqlite3AutoincrementBegin(X)
12411 # define sqlite3AutoincrementEnd(X)
12412 #endif
12413 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
12414 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
12415 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
12416 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
12417 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
12418 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
12419 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
12420                                       Token*, Select*, Expr*, IdList*);
12421 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
12422 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
12423 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
12424 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
12425 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
12426 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
12427 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
12428 SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
12429                           Expr*, int, int);
12430 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
12431 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
12432 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
12433                          Expr*,ExprList*,u16,Expr*,Expr*);
12434 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
12435 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
12436 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
12437 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
12438 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
12439 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
12440 #endif
12441 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
12442 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
12443 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
12444 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
12445 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
12446 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
12447 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
12448 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
12449 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
12450 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
12451 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
12452 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
12453 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
12454 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
12455 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
12456 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
12457 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
12458 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
12459 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
12460 SQLITE_PRIVATE void sqlite3ExprCode(Parse*, Expr*, int);
12461 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse*, Expr*, int);
12462 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
12463 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
12464 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
12465 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse*, Expr*, int);
12466 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
12467 #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
12468 #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
12469 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
12470 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
12471 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
12472 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
12473 SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
12474 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
12475 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
12476 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
12477 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
12478 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
12479 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
12480 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
12481 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
12482 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
12483 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
12484 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
12485 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
12486 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
12487 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
12488 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
12489 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
12490 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
12491 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
12492 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
12493 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
12494 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
12495 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
12496 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
12497 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
12498 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
12499 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
12500 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
12501 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
12502 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
12503 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
12504 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
12505 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
12506 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
12507 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
12508 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
12509                                      u8,u8,int,int*);
12510 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
12511 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
12512 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
12513 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
12514 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
12515 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
12516 SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
12517 SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
12518 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
12519 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
12520 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
12521 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
12522 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
12523 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
12524 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
12525 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
12526 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
12527 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
12528 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
12529 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
12530 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
12531 
12532 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
12533 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
12534 #endif
12535 
12536 #ifndef SQLITE_OMIT_TRIGGER
12537 SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
12538                            Expr*,int, int);
12539 SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
12540 SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
12541 SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
12542 SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
12543 SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
12544 SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
12545                             int, int, int);
12546 SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
12547   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
12548 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
12549 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
12550 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
12551                                         Select*,u8);
12552 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
12553 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
12554 SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
12555 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
12556 SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
12557 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
12558 #else
12559 # define sqlite3TriggersExist(B,C,D,E,F) 0
12560 # define sqlite3DeleteTrigger(A,B)
12561 # define sqlite3DropTriggerPtr(A,B)
12562 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
12563 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
12564 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
12565 # define sqlite3TriggerList(X, Y) 0
12566 # define sqlite3ParseToplevel(p) p
12567 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
12568 #endif
12569 
12570 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
12571 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
12572 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
12573 #ifndef SQLITE_OMIT_AUTHORIZATION
12574 SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
12575 SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
12576 SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
12577 SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
12578 SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
12579 #else
12580 # define sqlite3AuthRead(a,b,c,d)
12581 # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
12582 # define sqlite3AuthContextPush(a,b,c)
12583 # define sqlite3AuthContextPop(a)  ((void)(a))
12584 #endif
12585 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
12586 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
12587 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
12588 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
12589 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
12590 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
12591 SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
12592 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
12593 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
12594 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
12595 SQLITE_PRIVATE int sqlite3Atoi(const char*);
12596 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
12597 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
12598 SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
12599 SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
12600 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
12601 #ifndef SQLITE_OMIT_VIRTUALTABLE
12602 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
12603 #endif
12604 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
12605 
12606 /*
12607 ** Routines to read and write variable-length integers.  These used to
12608 ** be defined locally, but now we use the varint routines in the util.c
12609 ** file.  Code should use the MACRO forms below, as the Varint32 versions
12610 ** are coded to assume the single byte case is already handled (which
12611 ** the MACRO form does).
12612 */
12613 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
12614 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
12615 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
12616 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
12617 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
12618 
12619 /*
12620 ** The header of a record consists of a sequence variable-length integers.
12621 ** These integers are almost always small and are encoded as a single byte.
12622 ** The following macros take advantage this fact to provide a fast encode
12623 ** and decode of the integers in a record header.  It is faster for the common
12624 ** case where the integer is a single byte.  It is a little slower when the
12625 ** integer is two or more bytes.  But overall it is faster.
12626 **
12627 ** The following expressions are equivalent:
12628 **
12629 **     x = sqlite3GetVarint32( A, &B );
12630 **     x = sqlite3PutVarint32( A, B );
12631 **
12632 **     x = getVarint32( A, B );
12633 **     x = putVarint32( A, B );
12634 **
12635 */
12636 #define getVarint32(A,B)  \
12637   (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
12638 #define putVarint32(A,B)  \
12639   (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
12640   sqlite3PutVarint32((A),(B)))
12641 #define getVarint    sqlite3GetVarint
12642 #define putVarint    sqlite3PutVarint
12643 
12644 
12645 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
12646 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe*, Table*, int);
12647 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
12648 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
12649 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
12650 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
12651 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
12652 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
12653 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
12654 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
12655 
12656 #if defined(SQLITE_TEST)
12657 SQLITE_PRIVATE const char *sqlite3ErrName(int);
12658 #endif
12659 
12660 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
12661 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
12662 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
12663 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
12664 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
12665 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
12666 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
12667 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
12668 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
12669 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
12670 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
12671 SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
12672 SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
12673 SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
12674 SQLITE_PRIVATE int sqlite3AbsInt32(int);
12675 #ifdef SQLITE_ENABLE_8_3_NAMES
12676 SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
12677 #else
12678 # define sqlite3FileSuffix3(X,Y)
12679 #endif
12680 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
12681 
12682 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
12683 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
12684 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12685                         void(*)(void*));
12686 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
12687 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
12688 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
12689 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
12690 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
12691 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
12692 #ifndef SQLITE_AMALGAMATION
12693 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
12694 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
12695 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
12696 SQLITE_PRIVATE const Token sqlite3IntTokens[];
12697 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
12698 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
12699 #ifndef SQLITE_OMIT_WSD
12700 SQLITE_PRIVATE int sqlite3PendingByte;
12701 #endif
12702 #endif
12703 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
12704 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
12705 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
12706 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
12707 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
12708 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
12709 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
12710 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
12711 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
12712 SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
12713 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
12714 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
12715 SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
12716 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
12717 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
12718 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
12719 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
12720 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
12721 SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
12722 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
12723 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
12724 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
12725 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
12726 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
12727 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
12728 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
12729 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
12730 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
12731 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
12732 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
12733 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
12734 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
12735 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
12736 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
12737 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
12738 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
12739 #ifdef SQLITE_DEBUG
12740 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
12741 #endif
12742 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
12743   void (*)(sqlite3_context*,int,sqlite3_value **),
12744   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
12745   FuncDestructor *pDestructor
12746 );
12747 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
12748 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
12749 
12750 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
12751 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
12752 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
12753 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
12754 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
12755 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
12756 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
12757 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
12758 
12759 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
12760 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12761 
12762 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12763 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12764 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12765 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12766 #endif
12767 
12768 /*
12769 ** The interface to the LEMON-generated parser
12770 */
12771 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
12772 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
12773 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
12774 #ifdef YYTRACKMAXSTACKDEPTH
12775 SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
12776 #endif
12777 
12778 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
12779 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12780 SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
12781 #else
12782 # define sqlite3CloseExtensions(X)
12783 #endif
12784 
12785 #ifndef SQLITE_OMIT_SHARED_CACHE
12786 SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
12787 #else
12788   #define sqlite3TableLock(v,w,x,y,z)
12789 #endif
12790 
12791 #ifdef SQLITE_TEST
12792 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
12793 #endif
12794 
12795 #ifdef SQLITE_OMIT_VIRTUALTABLE
12796 #  define sqlite3VtabClear(Y)
12797 #  define sqlite3VtabSync(X,Y) SQLITE_OK
12798 #  define sqlite3VtabRollback(X)
12799 #  define sqlite3VtabCommit(X)
12800 #  define sqlite3VtabInSync(db) 0
12801 #  define sqlite3VtabLock(X)
12802 #  define sqlite3VtabUnlock(X)
12803 #  define sqlite3VtabUnlockList(X)
12804 #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
12805 #  define sqlite3GetVTable(X,Y)  ((VTable*)0)
12806 #else
12807 SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
12808 SQLITE_PRIVATE    void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
12809 SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, Vdbe*);
12810 SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
12811 SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
12812 SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
12813 SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
12814 SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
12815 SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
12816 SQLITE_PRIVATE    void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
12817 SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
12818 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
12819 #endif
12820 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
12821 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
12822 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
12823 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
12824 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
12825 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
12826 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
12827 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
12828 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
12829 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
12830 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
12831 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
12832 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
12833 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
12834 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
12835 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
12836 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
12837 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
12838 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
12839 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
12840 #ifndef SQLITE_OMIT_WAL
12841 SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
12842 SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
12843 #endif
12844 #ifndef SQLITE_OMIT_CTE
12845 SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
12846 SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
12847 SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
12848 #else
12849 #define sqlite3WithPush(x,y,z)
12850 #define sqlite3WithDelete(x,y)
12851 #endif
12852 
12853 /* Declarations for functions in fkey.c. All of these are replaced by
12854 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
12855 ** key functionality is available. If OMIT_TRIGGER is defined but
12856 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
12857 ** this case foreign keys are parsed, but no other functionality is
12858 ** provided (enforcement of FK constraints requires the triggers sub-system).
12859 */
12860 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
12861 SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
12862 SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
12863 SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
12864 SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
12865 SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
12866 SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
12867 #else
12868   #define sqlite3FkActions(a,b,c,d,e,f)
12869   #define sqlite3FkCheck(a,b,c,d,e,f)
12870   #define sqlite3FkDropTable(a,b,c)
12871   #define sqlite3FkOldmask(a,b)         0
12872   #define sqlite3FkRequired(a,b,c,d)    0
12873 #endif
12874 #ifndef SQLITE_OMIT_FOREIGN_KEY
12875 SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
12876 SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
12877 #else
12878   #define sqlite3FkDelete(a,b)
12879   #define sqlite3FkLocateIndex(a,b,c,d,e)
12880 #endif
12881 
12882 
12883 /*
12884 ** Available fault injectors.  Should be numbered beginning with 0.
12885 */
12886 #define SQLITE_FAULTINJECTOR_MALLOC     0
12887 #define SQLITE_FAULTINJECTOR_COUNT      1
12888 
12889 /*
12890 ** The interface to the code in fault.c used for identifying "benign"
12891 ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
12892 ** is not defined.
12893 */
12894 #ifndef SQLITE_OMIT_BUILTIN_TEST
12895 SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
12896 SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
12897 #else
12898   #define sqlite3BeginBenignMalloc()
12899   #define sqlite3EndBenignMalloc()
12900 #endif
12901 
12902 #define IN_INDEX_ROWID           1
12903 #define IN_INDEX_EPH             2
12904 #define IN_INDEX_INDEX_ASC       3
12905 #define IN_INDEX_INDEX_DESC      4
12906 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
12907 
12908 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
12909 SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
12910 SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
12911 SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
12912 SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
12913 #else
12914   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
12915   #define sqlite3JournalExists(p) 1
12916 #endif
12917 
12918 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
12919 SQLITE_PRIVATE int sqlite3MemJournalSize(void);
12920 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
12921 
12922 #if SQLITE_MAX_EXPR_DEPTH>0
12923 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
12924 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
12925 SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
12926 #else
12927   #define sqlite3ExprSetHeight(x,y)
12928   #define sqlite3SelectExprHeight(x) 0
12929   #define sqlite3ExprCheckHeight(x,y)
12930 #endif
12931 
12932 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
12933 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
12934 
12935 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12936 SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
12937 SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
12938 SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
12939 #else
12940   #define sqlite3ConnectionBlocked(x,y)
12941   #define sqlite3ConnectionUnlocked(x)
12942   #define sqlite3ConnectionClosed(x)
12943 #endif
12944 
12945 #ifdef SQLITE_DEBUG
12946 SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
12947 #endif
12948 
12949 /*
12950 ** If the SQLITE_ENABLE IOTRACE exists then the global variable
12951 ** sqlite3IoTrace is a pointer to a printf-like routine used to
12952 ** print I/O tracing messages.
12953 */
12954 #ifdef SQLITE_ENABLE_IOTRACE
12955 # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
12956 SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
12957 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
12958 #else
12959 # define IOTRACE(A)
12960 # define sqlite3VdbeIOTraceSql(X)
12961 #endif
12962 
12963 /*
12964 ** These routines are available for the mem2.c debugging memory allocator
12965 ** only.  They are used to verify that different "types" of memory
12966 ** allocations are properly tracked by the system.
12967 **
12968 ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
12969 ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
12970 ** a single bit set.
12971 **
12972 ** sqlite3MemdebugHasType() returns true if any of the bits in its second
12973 ** argument match the type set by the previous sqlite3MemdebugSetType().
12974 ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
12975 **
12976 ** sqlite3MemdebugNoType() returns true if none of the bits in its second
12977 ** argument match the type set by the previous sqlite3MemdebugSetType().
12978 **
12979 ** Perhaps the most important point is the difference between MEMTYPE_HEAP
12980 ** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
12981 ** it might have been allocated by lookaside, except the allocation was
12982 ** too large or lookaside was already full.  It is important to verify
12983 ** that allocations that might have been satisfied by lookaside are not
12984 ** passed back to non-lookaside free() routines.  Asserts such as the
12985 ** example above are placed on the non-lookaside free() routines to verify
12986 ** this constraint.
12987 **
12988 ** All of this is no-op for a production build.  It only comes into
12989 ** play when the SQLITE_MEMDEBUG compile-time option is used.
12990 */
12991 #ifdef SQLITE_MEMDEBUG
12992 SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
12993 SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
12994 SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
12995 #else
12996 # define sqlite3MemdebugSetType(X,Y)  /* no-op */
12997 # define sqlite3MemdebugHasType(X,Y)  1
12998 # define sqlite3MemdebugNoType(X,Y)   1
12999 #endif
13000 #define MEMTYPE_HEAP       0x01  /* General heap allocations */
13001 #define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
13002 #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
13003 #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
13004 #define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
13005 
13006 #endif /* _SQLITEINT_H_ */
13007 
13008 /************** End of sqliteInt.h *******************************************/
13009 /************** Begin file global.c ******************************************/
13010 /*
13011 ** 2008 June 13
13012 **
13013 ** The author disclaims copyright to this source code.  In place of
13014 ** a legal notice, here is a blessing:
13015 **
13016 **    May you do good and not evil.
13017 **    May you find forgiveness for yourself and forgive others.
13018 **    May you share freely, never taking more than you give.
13019 **
13020 *************************************************************************
13021 **
13022 ** This file contains definitions of global variables and contants.
13023 */
13024 
13025 /* An array to map all upper-case characters into their corresponding
13026 ** lower-case character.
13027 **
13028 ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
13029 ** handle case conversions for the UTF character set since the tables
13030 ** involved are nearly as big or bigger than SQLite itself.
13031 */
13032 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
13033 #ifdef SQLITE_ASCII
13034       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
13035      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
13036      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
13037      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
13038     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
13039     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
13040     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
13041     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
13042     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
13043     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
13044     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
13045     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
13046     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
13047     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
13048     252,253,254,255
13049 #endif
13050 #ifdef SQLITE_EBCDIC
13051       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
13052      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
13053      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
13054      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
13055      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
13056      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
13057      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
13058     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
13059     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
13060     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
13061     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
13062     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
13063     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
13064     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
13065     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
13066     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
13067 #endif
13068 };
13069 
13070 /*
13071 ** The following 256 byte lookup table is used to support SQLites built-in
13072 ** equivalents to the following standard library functions:
13073 **
13074 **   isspace()                        0x01
13075 **   isalpha()                        0x02
13076 **   isdigit()                        0x04
13077 **   isalnum()                        0x06
13078 **   isxdigit()                       0x08
13079 **   toupper()                        0x20
13080 **   SQLite identifier character      0x40
13081 **
13082 ** Bit 0x20 is set if the mapped character requires translation to upper
13083 ** case. i.e. if the character is a lower-case ASCII character.
13084 ** If x is a lower-case ASCII character, then its upper-case equivalent
13085 ** is (x - 0x20). Therefore toupper() can be implemented as:
13086 **
13087 **   (x & ~(map[x]&0x20))
13088 **
13089 ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
13090 ** array. tolower() is used more often than toupper() by SQLite.
13091 **
13092 ** Bit 0x40 is set if the character non-alphanumeric and can be used in an
13093 ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
13094 ** non-ASCII UTF character. Hence the test for whether or not a character is
13095 ** part of an identifier is 0x46.
13096 **
13097 ** SQLite's versions are identical to the standard versions assuming a
13098 ** locale of "C". They are implemented as macros in sqliteInt.h.
13099 */
13100 #ifdef SQLITE_ASCII
13101 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
13102   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
13103   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
13104   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
13105   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
13106   0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
13107   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
13108   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
13109   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
13110 
13111   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
13112   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
13113   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
13114   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
13115   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
13116   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
13117   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
13118   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
13119 
13120   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
13121   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
13122   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
13123   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
13124   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
13125   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
13126   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
13127   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
13128 
13129   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
13130   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
13131   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
13132   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
13133   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
13134   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
13135   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
13136   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
13137 };
13138 #endif
13139 
13140 #ifndef SQLITE_USE_URI
13141 # define  SQLITE_USE_URI 0
13142 #endif
13143 
13144 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
13145 # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
13146 #endif
13147 
13148 /*
13149 ** The following singleton contains the global configuration for
13150 ** the SQLite library.
13151 */
13152 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
13153    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
13154    1,                         /* bCoreMutex */
13155    SQLITE_THREADSAFE==1,      /* bFullMutex */
13156    SQLITE_USE_URI,            /* bOpenUri */
13157    SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
13158    0x7ffffffe,                /* mxStrlen */
13159    0,                         /* neverCorrupt */
13160    128,                       /* szLookaside */
13161    500,                       /* nLookaside */
13162    {0,0,0,0,0,0,0,0},         /* m */
13163    {0,0,0,0,0,0,0,0,0},       /* mutex */
13164    {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
13165    (void*)0,                  /* pHeap */
13166    0,                         /* nHeap */
13167    0, 0,                      /* mnHeap, mxHeap */
13168    SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
13169    SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
13170    (void*)0,                  /* pScratch */
13171    0,                         /* szScratch */
13172    0,                         /* nScratch */
13173    (void*)0,                  /* pPage */
13174    0,                         /* szPage */
13175    0,                         /* nPage */
13176    0,                         /* mxParserStack */
13177    0,                         /* sharedCacheEnabled */
13178    /* All the rest should always be initialized to zero */
13179    0,                         /* isInit */
13180    0,                         /* inProgress */
13181    0,                         /* isMutexInit */
13182    0,                         /* isMallocInit */
13183    0,                         /* isPCacheInit */
13184    0,                         /* pInitMutex */
13185    0,                         /* nRefInitMutex */
13186    0,                         /* xLog */
13187    0,                         /* pLogArg */
13188    0,                         /* bLocaltimeFault */
13189 #ifdef SQLITE_ENABLE_SQLLOG
13190    0,                         /* xSqllog */
13191    0                          /* pSqllogArg */
13192 #endif
13193 };
13194 
13195 /*
13196 ** Hash table for global functions - functions common to all
13197 ** database connections.  After initialization, this table is
13198 ** read-only.
13199 */
13200 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
13201 
13202 /*
13203 ** Constant tokens for values 0 and 1.
13204 */
13205 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
13206    { "0", 1 },
13207    { "1", 1 }
13208 };
13209 
13210 
13211 /*
13212 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
13213 ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
13214 ** the database page that contains the pending byte.  It never attempts
13215 ** to read or write that page.  The pending byte page is set assign
13216 ** for use by the VFS layers as space for managing file locks.
13217 **
13218 ** During testing, it is often desirable to move the pending byte to
13219 ** a different position in the file.  This allows code that has to
13220 ** deal with the pending byte to run on files that are much smaller
13221 ** than 1 GiB.  The sqlite3_test_control() interface can be used to
13222 ** move the pending byte.
13223 **
13224 ** IMPORTANT:  Changing the pending byte to any value other than
13225 ** 0x40000000 results in an incompatible database file format!
13226 ** Changing the pending byte during operating results in undefined
13227 ** and dileterious behavior.
13228 */
13229 #ifndef SQLITE_OMIT_WSD
13230 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
13231 #endif
13232 
13233 /*
13234 ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
13235 ** created by mkopcodeh.awk during compilation.  Data is obtained
13236 ** from the comments following the "case OP_xxxx:" statements in
13237 ** the vdbe.c file.
13238 */
13239 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
13240 
13241 /************** End of global.c **********************************************/
13242 /************** Begin file ctime.c *******************************************/
13243 /*
13244 ** 2010 February 23
13245 **
13246 ** The author disclaims copyright to this source code.  In place of
13247 ** a legal notice, here is a blessing:
13248 **
13249 **    May you do good and not evil.
13250 **    May you find forgiveness for yourself and forgive others.
13251 **    May you share freely, never taking more than you give.
13252 **
13253 *************************************************************************
13254 **
13255 ** This file implements routines used to report what compile-time options
13256 ** SQLite was built with.
13257 */
13258 
13259 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
13260 
13261 
13262 /*
13263 ** An array of names of all compile-time options.  This array should
13264 ** be sorted A-Z.
13265 **
13266 ** This array looks large, but in a typical installation actually uses
13267 ** only a handful of compile-time options, so most times this array is usually
13268 ** rather short and uses little memory space.
13269 */
13270 static const char * const azCompileOpt[] = {
13271 
13272 /* These macros are provided to "stringify" the value of the define
13273 ** for those options in which the value is meaningful. */
13274 #define CTIMEOPT_VAL_(opt) #opt
13275 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
13276 
13277 #ifdef SQLITE_32BIT_ROWID
13278   "32BIT_ROWID",
13279 #endif
13280 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
13281   "4_BYTE_ALIGNED_MALLOC",
13282 #endif
13283 #ifdef SQLITE_CASE_SENSITIVE_LIKE
13284   "CASE_SENSITIVE_LIKE",
13285 #endif
13286 #ifdef SQLITE_CHECK_PAGES
13287   "CHECK_PAGES",
13288 #endif
13289 #ifdef SQLITE_COVERAGE_TEST
13290   "COVERAGE_TEST",
13291 #endif
13292 #ifdef SQLITE_DEBUG
13293   "DEBUG",
13294 #endif
13295 #ifdef SQLITE_DEFAULT_LOCKING_MODE
13296   "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
13297 #endif
13298 #if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
13299   "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
13300 #endif
13301 #ifdef SQLITE_DISABLE_DIRSYNC
13302   "DISABLE_DIRSYNC",
13303 #endif
13304 #ifdef SQLITE_DISABLE_LFS
13305   "DISABLE_LFS",
13306 #endif
13307 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
13308   "ENABLE_ATOMIC_WRITE",
13309 #endif
13310 #ifdef SQLITE_ENABLE_CEROD
13311   "ENABLE_CEROD",
13312 #endif
13313 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13314   "ENABLE_COLUMN_METADATA",
13315 #endif
13316 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
13317   "ENABLE_EXPENSIVE_ASSERT",
13318 #endif
13319 #ifdef SQLITE_ENABLE_FTS1
13320   "ENABLE_FTS1",
13321 #endif
13322 #ifdef SQLITE_ENABLE_FTS2
13323   "ENABLE_FTS2",
13324 #endif
13325 #ifdef SQLITE_ENABLE_FTS3
13326   "ENABLE_FTS3",
13327 #endif
13328 #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
13329   "ENABLE_FTS3_PARENTHESIS",
13330 #endif
13331 #ifdef SQLITE_ENABLE_FTS4
13332   "ENABLE_FTS4",
13333 #endif
13334 #ifdef SQLITE_ENABLE_ICU
13335   "ENABLE_ICU",
13336 #endif
13337 #ifdef SQLITE_ENABLE_IOTRACE
13338   "ENABLE_IOTRACE",
13339 #endif
13340 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
13341   "ENABLE_LOAD_EXTENSION",
13342 #endif
13343 #ifdef SQLITE_ENABLE_LOCKING_STYLE
13344   "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
13345 #endif
13346 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
13347   "ENABLE_MEMORY_MANAGEMENT",
13348 #endif
13349 #ifdef SQLITE_ENABLE_MEMSYS3
13350   "ENABLE_MEMSYS3",
13351 #endif
13352 #ifdef SQLITE_ENABLE_MEMSYS5
13353   "ENABLE_MEMSYS5",
13354 #endif
13355 #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
13356   "ENABLE_OVERSIZE_CELL_CHECK",
13357 #endif
13358 #ifdef SQLITE_ENABLE_RTREE
13359   "ENABLE_RTREE",
13360 #endif
13361 #if defined(SQLITE_ENABLE_STAT4)
13362   "ENABLE_STAT4",
13363 #elif defined(SQLITE_ENABLE_STAT3)
13364   "ENABLE_STAT3",
13365 #endif
13366 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
13367   "ENABLE_UNLOCK_NOTIFY",
13368 #endif
13369 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
13370   "ENABLE_UPDATE_DELETE_LIMIT",
13371 #endif
13372 #ifdef SQLITE_HAS_CODEC
13373   "HAS_CODEC",
13374 #endif
13375 #ifdef SQLITE_HAVE_ISNAN
13376   "HAVE_ISNAN",
13377 #endif
13378 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
13379   "HOMEGROWN_RECURSIVE_MUTEX",
13380 #endif
13381 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
13382   "IGNORE_AFP_LOCK_ERRORS",
13383 #endif
13384 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13385   "IGNORE_FLOCK_LOCK_ERRORS",
13386 #endif
13387 #ifdef SQLITE_INT64_TYPE
13388   "INT64_TYPE",
13389 #endif
13390 #ifdef SQLITE_LOCK_TRACE
13391   "LOCK_TRACE",
13392 #endif
13393 #if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
13394   "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
13395 #endif
13396 #ifdef SQLITE_MAX_SCHEMA_RETRY
13397   "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
13398 #endif
13399 #ifdef SQLITE_MEMDEBUG
13400   "MEMDEBUG",
13401 #endif
13402 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
13403   "MIXED_ENDIAN_64BIT_FLOAT",
13404 #endif
13405 #ifdef SQLITE_NO_SYNC
13406   "NO_SYNC",
13407 #endif
13408 #ifdef SQLITE_OMIT_ALTERTABLE
13409   "OMIT_ALTERTABLE",
13410 #endif
13411 #ifdef SQLITE_OMIT_ANALYZE
13412   "OMIT_ANALYZE",
13413 #endif
13414 #ifdef SQLITE_OMIT_ATTACH
13415   "OMIT_ATTACH",
13416 #endif
13417 #ifdef SQLITE_OMIT_AUTHORIZATION
13418   "OMIT_AUTHORIZATION",
13419 #endif
13420 #ifdef SQLITE_OMIT_AUTOINCREMENT
13421   "OMIT_AUTOINCREMENT",
13422 #endif
13423 #ifdef SQLITE_OMIT_AUTOINIT
13424   "OMIT_AUTOINIT",
13425 #endif
13426 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
13427   "OMIT_AUTOMATIC_INDEX",
13428 #endif
13429 #ifdef SQLITE_OMIT_AUTORESET
13430   "OMIT_AUTORESET",
13431 #endif
13432 #ifdef SQLITE_OMIT_AUTOVACUUM
13433   "OMIT_AUTOVACUUM",
13434 #endif
13435 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
13436   "OMIT_BETWEEN_OPTIMIZATION",
13437 #endif
13438 #ifdef SQLITE_OMIT_BLOB_LITERAL
13439   "OMIT_BLOB_LITERAL",
13440 #endif
13441 #ifdef SQLITE_OMIT_BTREECOUNT
13442   "OMIT_BTREECOUNT",
13443 #endif
13444 #ifdef SQLITE_OMIT_BUILTIN_TEST
13445   "OMIT_BUILTIN_TEST",
13446 #endif
13447 #ifdef SQLITE_OMIT_CAST
13448   "OMIT_CAST",
13449 #endif
13450 #ifdef SQLITE_OMIT_CHECK
13451   "OMIT_CHECK",
13452 #endif
13453 #ifdef SQLITE_OMIT_COMPLETE
13454   "OMIT_COMPLETE",
13455 #endif
13456 #ifdef SQLITE_OMIT_COMPOUND_SELECT
13457   "OMIT_COMPOUND_SELECT",
13458 #endif
13459 #ifdef SQLITE_OMIT_CTE
13460   "OMIT_CTE",
13461 #endif
13462 #ifdef SQLITE_OMIT_DATETIME_FUNCS
13463   "OMIT_DATETIME_FUNCS",
13464 #endif
13465 #ifdef SQLITE_OMIT_DECLTYPE
13466   "OMIT_DECLTYPE",
13467 #endif
13468 #ifdef SQLITE_OMIT_DEPRECATED
13469   "OMIT_DEPRECATED",
13470 #endif
13471 #ifdef SQLITE_OMIT_DISKIO
13472   "OMIT_DISKIO",
13473 #endif
13474 #ifdef SQLITE_OMIT_EXPLAIN
13475   "OMIT_EXPLAIN",
13476 #endif
13477 #ifdef SQLITE_OMIT_FLAG_PRAGMAS
13478   "OMIT_FLAG_PRAGMAS",
13479 #endif
13480 #ifdef SQLITE_OMIT_FLOATING_POINT
13481   "OMIT_FLOATING_POINT",
13482 #endif
13483 #ifdef SQLITE_OMIT_FOREIGN_KEY
13484   "OMIT_FOREIGN_KEY",
13485 #endif
13486 #ifdef SQLITE_OMIT_GET_TABLE
13487   "OMIT_GET_TABLE",
13488 #endif
13489 #ifdef SQLITE_OMIT_INCRBLOB
13490   "OMIT_INCRBLOB",
13491 #endif
13492 #ifdef SQLITE_OMIT_INTEGRITY_CHECK
13493   "OMIT_INTEGRITY_CHECK",
13494 #endif
13495 #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
13496   "OMIT_LIKE_OPTIMIZATION",
13497 #endif
13498 #ifdef SQLITE_OMIT_LOAD_EXTENSION
13499   "OMIT_LOAD_EXTENSION",
13500 #endif
13501 #ifdef SQLITE_OMIT_LOCALTIME
13502   "OMIT_LOCALTIME",
13503 #endif
13504 #ifdef SQLITE_OMIT_LOOKASIDE
13505   "OMIT_LOOKASIDE",
13506 #endif
13507 #ifdef SQLITE_OMIT_MEMORYDB
13508   "OMIT_MEMORYDB",
13509 #endif
13510 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
13511   "OMIT_OR_OPTIMIZATION",
13512 #endif
13513 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
13514   "OMIT_PAGER_PRAGMAS",
13515 #endif
13516 #ifdef SQLITE_OMIT_PRAGMA
13517   "OMIT_PRAGMA",
13518 #endif
13519 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
13520   "OMIT_PROGRESS_CALLBACK",
13521 #endif
13522 #ifdef SQLITE_OMIT_QUICKBALANCE
13523   "OMIT_QUICKBALANCE",
13524 #endif
13525 #ifdef SQLITE_OMIT_REINDEX
13526   "OMIT_REINDEX",
13527 #endif
13528 #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
13529   "OMIT_SCHEMA_PRAGMAS",
13530 #endif
13531 #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
13532   "OMIT_SCHEMA_VERSION_PRAGMAS",
13533 #endif
13534 #ifdef SQLITE_OMIT_SHARED_CACHE
13535   "OMIT_SHARED_CACHE",
13536 #endif
13537 #ifdef SQLITE_OMIT_SUBQUERY
13538   "OMIT_SUBQUERY",
13539 #endif
13540 #ifdef SQLITE_OMIT_TCL_VARIABLE
13541   "OMIT_TCL_VARIABLE",
13542 #endif
13543 #ifdef SQLITE_OMIT_TEMPDB
13544   "OMIT_TEMPDB",
13545 #endif
13546 #ifdef SQLITE_OMIT_TRACE
13547   "OMIT_TRACE",
13548 #endif
13549 #ifdef SQLITE_OMIT_TRIGGER
13550   "OMIT_TRIGGER",
13551 #endif
13552 #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
13553   "OMIT_TRUNCATE_OPTIMIZATION",
13554 #endif
13555 #ifdef SQLITE_OMIT_UTF16
13556   "OMIT_UTF16",
13557 #endif
13558 #ifdef SQLITE_OMIT_VACUUM
13559   "OMIT_VACUUM",
13560 #endif
13561 #ifdef SQLITE_OMIT_VIEW
13562   "OMIT_VIEW",
13563 #endif
13564 #ifdef SQLITE_OMIT_VIRTUALTABLE
13565   "OMIT_VIRTUALTABLE",
13566 #endif
13567 #ifdef SQLITE_OMIT_WAL
13568   "OMIT_WAL",
13569 #endif
13570 #ifdef SQLITE_OMIT_WSD
13571   "OMIT_WSD",
13572 #endif
13573 #ifdef SQLITE_OMIT_XFER_OPT
13574   "OMIT_XFER_OPT",
13575 #endif
13576 #ifdef SQLITE_PERFORMANCE_TRACE
13577   "PERFORMANCE_TRACE",
13578 #endif
13579 #ifdef SQLITE_PROXY_DEBUG
13580   "PROXY_DEBUG",
13581 #endif
13582 #ifdef SQLITE_RTREE_INT_ONLY
13583   "RTREE_INT_ONLY",
13584 #endif
13585 #ifdef SQLITE_SECURE_DELETE
13586   "SECURE_DELETE",
13587 #endif
13588 #ifdef SQLITE_SMALL_STACK
13589   "SMALL_STACK",
13590 #endif
13591 #ifdef SQLITE_SOUNDEX
13592   "SOUNDEX",
13593 #endif
13594 #ifdef SQLITE_SYSTEM_MALLOC
13595   "SYSTEM_MALLOC",
13596 #endif
13597 #ifdef SQLITE_TCL
13598   "TCL",
13599 #endif
13600 #if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
13601   "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
13602 #endif
13603 #ifdef SQLITE_TEST
13604   "TEST",
13605 #endif
13606 #if defined(SQLITE_THREADSAFE)
13607   "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
13608 #endif
13609 #ifdef SQLITE_USE_ALLOCA
13610   "USE_ALLOCA",
13611 #endif
13612 #ifdef SQLITE_WIN32_MALLOC
13613   "WIN32_MALLOC",
13614 #endif
13615 #ifdef SQLITE_ZERO_MALLOC
13616   "ZERO_MALLOC"
13617 #endif
13618 };
13619 
13620 /*
13621 ** Given the name of a compile-time option, return true if that option
13622 ** was used and false if not.
13623 **
13624 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
13625 ** is not required for a match.
13626 */
sqlite3_compileoption_used(const char * zOptName)13627 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
13628   int i, n;
13629   if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
13630   n = sqlite3Strlen30(zOptName);
13631 
13632   /* Since ArraySize(azCompileOpt) is normally in single digits, a
13633   ** linear search is adequate.  No need for a binary search. */
13634   for(i=0; i<ArraySize(azCompileOpt); i++){
13635     if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
13636      && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
13637     ){
13638       return 1;
13639     }
13640   }
13641   return 0;
13642 }
13643 
13644 /*
13645 ** Return the N-th compile-time option string.  If N is out of range,
13646 ** return a NULL pointer.
13647 */
sqlite3_compileoption_get(int N)13648 SQLITE_API const char *sqlite3_compileoption_get(int N){
13649   if( N>=0 && N<ArraySize(azCompileOpt) ){
13650     return azCompileOpt[N];
13651   }
13652   return 0;
13653 }
13654 
13655 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
13656 
13657 /************** End of ctime.c ***********************************************/
13658 /************** Begin file status.c ******************************************/
13659 /*
13660 ** 2008 June 18
13661 **
13662 ** The author disclaims copyright to this source code.  In place of
13663 ** a legal notice, here is a blessing:
13664 **
13665 **    May you do good and not evil.
13666 **    May you find forgiveness for yourself and forgive others.
13667 **    May you share freely, never taking more than you give.
13668 **
13669 *************************************************************************
13670 **
13671 ** This module implements the sqlite3_status() interface and related
13672 ** functionality.
13673 */
13674 /************** Include vdbeInt.h in the middle of status.c ******************/
13675 /************** Begin file vdbeInt.h *****************************************/
13676 /*
13677 ** 2003 September 6
13678 **
13679 ** The author disclaims copyright to this source code.  In place of
13680 ** a legal notice, here is a blessing:
13681 **
13682 **    May you do good and not evil.
13683 **    May you find forgiveness for yourself and forgive others.
13684 **    May you share freely, never taking more than you give.
13685 **
13686 *************************************************************************
13687 ** This is the header file for information that is private to the
13688 ** VDBE.  This information used to all be at the top of the single
13689 ** source code file "vdbe.c".  When that file became too big (over
13690 ** 6000 lines long) it was split up into several smaller files and
13691 ** this header information was factored out.
13692 */
13693 #ifndef _VDBEINT_H_
13694 #define _VDBEINT_H_
13695 
13696 /*
13697 ** The maximum number of times that a statement will try to reparse
13698 ** itself before giving up and returning SQLITE_SCHEMA.
13699 */
13700 #ifndef SQLITE_MAX_SCHEMA_RETRY
13701 # define SQLITE_MAX_SCHEMA_RETRY 50
13702 #endif
13703 
13704 /*
13705 ** SQL is translated into a sequence of instructions to be
13706 ** executed by a virtual machine.  Each instruction is an instance
13707 ** of the following structure.
13708 */
13709 typedef struct VdbeOp Op;
13710 
13711 /*
13712 ** Boolean values
13713 */
13714 typedef unsigned Bool;
13715 
13716 /* Opaque type used by code in vdbesort.c */
13717 typedef struct VdbeSorter VdbeSorter;
13718 
13719 /* Opaque type used by the explainer */
13720 typedef struct Explain Explain;
13721 
13722 /* Elements of the linked list at Vdbe.pAuxData */
13723 typedef struct AuxData AuxData;
13724 
13725 /*
13726 ** A cursor is a pointer into a single BTree within a database file.
13727 ** The cursor can seek to a BTree entry with a particular key, or
13728 ** loop over all entries of the Btree.  You can also insert new BTree
13729 ** entries or retrieve the key or data from the entry that the cursor
13730 ** is currently pointing to.
13731 **
13732 ** Cursors can also point to virtual tables, sorters, or "pseudo-tables".
13733 ** A pseudo-table is a single-row table implemented by registers.
13734 **
13735 ** Every cursor that the virtual machine has open is represented by an
13736 ** instance of the following structure.
13737 */
13738 struct VdbeCursor {
13739   BtCursor *pCursor;    /* The cursor structure of the backend */
13740   Btree *pBt;           /* Separate file holding temporary table */
13741   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
13742   int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
13743   int pseudoTableReg;   /* Register holding pseudotable content. */
13744   i16 nField;           /* Number of fields in the header */
13745   u16 nHdrParsed;       /* Number of header fields parsed so far */
13746   i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
13747   u8 nullRow;           /* True if pointing to a row with no data */
13748   u8 rowidIsValid;      /* True if lastRowid is valid */
13749   u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
13750   Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
13751   Bool isTable:1;       /* True if a table requiring integer keys */
13752   Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
13753   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
13754   i64 seqCount;         /* Sequence counter */
13755   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
13756   i64 lastRowid;        /* Rowid being deleted by OP_Delete */
13757   VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
13758 
13759   /* Cached information about the header for the data record that the
13760   ** cursor is currently pointing to.  Only valid if cacheStatus matches
13761   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
13762   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
13763   ** the cache is out of date.
13764   **
13765   ** aRow might point to (ephemeral) data for the current row, or it might
13766   ** be NULL.
13767   */
13768   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
13769   u32 payloadSize;      /* Total number of bytes in the record */
13770   u32 szRow;            /* Byte available in aRow */
13771   u32 iHdrOffset;       /* Offset to next unparsed byte of the header */
13772   const u8 *aRow;       /* Data for the current row, if all on one page */
13773   u32 aType[1];         /* Type values for all entries in the record */
13774   /* 2*nField extra array elements allocated for aType[], beyond the one
13775   ** static element declared in the structure.  nField total array slots for
13776   ** aType[] and nField+1 array slots for aOffset[] */
13777 };
13778 typedef struct VdbeCursor VdbeCursor;
13779 
13780 /*
13781 ** When a sub-program is executed (OP_Program), a structure of this type
13782 ** is allocated to store the current value of the program counter, as
13783 ** well as the current memory cell array and various other frame specific
13784 ** values stored in the Vdbe struct. When the sub-program is finished,
13785 ** these values are copied back to the Vdbe from the VdbeFrame structure,
13786 ** restoring the state of the VM to as it was before the sub-program
13787 ** began executing.
13788 **
13789 ** The memory for a VdbeFrame object is allocated and managed by a memory
13790 ** cell in the parent (calling) frame. When the memory cell is deleted or
13791 ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
13792 ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
13793 ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
13794 ** this instead of deleting the VdbeFrame immediately is to avoid recursive
13795 ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
13796 ** child frame are released.
13797 **
13798 ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
13799 ** set to NULL if the currently executing frame is the main program.
13800 */
13801 typedef struct VdbeFrame VdbeFrame;
13802 struct VdbeFrame {
13803   Vdbe *v;                /* VM this frame belongs to */
13804   VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
13805   Op *aOp;                /* Program instructions for parent frame */
13806   Mem *aMem;              /* Array of memory cells for parent frame */
13807   u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
13808   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
13809   void *token;            /* Copy of SubProgram.token */
13810   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
13811   int nCursor;            /* Number of entries in apCsr */
13812   int pc;                 /* Program Counter in parent (calling) frame */
13813   int nOp;                /* Size of aOp array */
13814   int nMem;               /* Number of entries in aMem */
13815   int nOnceFlag;          /* Number of entries in aOnceFlag */
13816   int nChildMem;          /* Number of memory cells for child frame */
13817   int nChildCsr;          /* Number of cursors for child frame */
13818   int nChange;            /* Statement changes (Vdbe.nChanges)     */
13819 };
13820 
13821 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
13822 
13823 /*
13824 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
13825 */
13826 #define CACHE_STALE 0
13827 
13828 /*
13829 ** Internally, the vdbe manipulates nearly all SQL values as Mem
13830 ** structures. Each Mem struct may cache multiple representations (string,
13831 ** integer etc.) of the same value.
13832 */
13833 struct Mem {
13834   sqlite3 *db;        /* The associated database connection */
13835   char *z;            /* String or BLOB value */
13836   double r;           /* Real value */
13837   union {
13838     i64 i;              /* Integer value used when MEM_Int is set in flags */
13839     int nZero;          /* Used when bit MEM_Zero is set in flags */
13840     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
13841     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
13842     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
13843   } u;
13844   int n;              /* Number of characters in string value, excluding '\0' */
13845   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
13846   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
13847 #ifdef SQLITE_DEBUG
13848   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
13849   void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
13850 #endif
13851   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
13852   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
13853 };
13854 
13855 /* One or more of the following flags are set to indicate the validOK
13856 ** representations of the value stored in the Mem struct.
13857 **
13858 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
13859 ** No other flags may be set in this case.
13860 **
13861 ** If the MEM_Str flag is set then Mem.z points at a string representation.
13862 ** Usually this is encoded in the same unicode encoding as the main
13863 ** database (see below for exceptions). If the MEM_Term flag is also
13864 ** set, then the string is nul terminated. The MEM_Int and MEM_Real
13865 ** flags may coexist with the MEM_Str flag.
13866 */
13867 #define MEM_Null      0x0001   /* Value is NULL */
13868 #define MEM_Str       0x0002   /* Value is a string */
13869 #define MEM_Int       0x0004   /* Value is an integer */
13870 #define MEM_Real      0x0008   /* Value is a real number */
13871 #define MEM_Blob      0x0010   /* Value is a BLOB */
13872 #define MEM_AffMask   0x001f   /* Mask of affinity bits */
13873 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
13874 #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
13875 #define MEM_Undefined 0x0080   /* Value is undefined */
13876 #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
13877 #define MEM_TypeMask  0x01ff   /* Mask of type bits */
13878 
13879 
13880 /* Whenever Mem contains a valid string or blob representation, one of
13881 ** the following flags must be set to determine the memory management
13882 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
13883 ** string is \000 or \u0000 terminated
13884 */
13885 #define MEM_Term      0x0200   /* String rep is nul terminated */
13886 #define MEM_Dyn       0x0400   /* Need to call Mem.xDel() on Mem.z */
13887 #define MEM_Static    0x0800   /* Mem.z points to a static string */
13888 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
13889 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
13890 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
13891 #ifdef SQLITE_OMIT_INCRBLOB
13892   #undef MEM_Zero
13893   #define MEM_Zero 0x0000
13894 #endif
13895 
13896 /*
13897 ** Clear any existing type flags from a Mem and replace them with f
13898 */
13899 #define MemSetTypeFlag(p, f) \
13900    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
13901 
13902 /*
13903 ** Return true if a memory cell is not marked as invalid.  This macro
13904 ** is for use inside assert() statements only.
13905 */
13906 #ifdef SQLITE_DEBUG
13907 #define memIsValid(M)  ((M)->flags & MEM_Undefined)==0
13908 #endif
13909 
13910 /*
13911 ** Each auxilliary data pointer stored by a user defined function
13912 ** implementation calling sqlite3_set_auxdata() is stored in an instance
13913 ** of this structure. All such structures associated with a single VM
13914 ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
13915 ** when the VM is halted (if not before).
13916 */
13917 struct AuxData {
13918   int iOp;                        /* Instruction number of OP_Function opcode */
13919   int iArg;                       /* Index of function argument. */
13920   void *pAux;                     /* Aux data pointer */
13921   void (*xDelete)(void *);        /* Destructor for the aux data */
13922   AuxData *pNext;                 /* Next element in list */
13923 };
13924 
13925 /*
13926 ** The "context" argument for a installable function.  A pointer to an
13927 ** instance of this structure is the first argument to the routines used
13928 ** implement the SQL functions.
13929 **
13930 ** There is a typedef for this structure in sqlite.h.  So all routines,
13931 ** even the public interface to SQLite, can use a pointer to this structure.
13932 ** But this file is the only place where the internal details of this
13933 ** structure are known.
13934 **
13935 ** This structure is defined inside of vdbeInt.h because it uses substructures
13936 ** (Mem) which are only defined there.
13937 */
13938 struct sqlite3_context {
13939   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
13940   Mem s;                /* The return value is stored here */
13941   Mem *pMem;            /* Memory cell used to store aggregate context */
13942   CollSeq *pColl;       /* Collating sequence */
13943   Vdbe *pVdbe;          /* The VM that owns this context */
13944   int iOp;              /* Instruction number of OP_Function */
13945   int isError;          /* Error code returned by the function. */
13946   u8 skipFlag;          /* Skip skip accumulator loading if true */
13947   u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
13948 };
13949 
13950 /*
13951 ** An Explain object accumulates indented output which is helpful
13952 ** in describing recursive data structures.
13953 */
13954 struct Explain {
13955   Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
13956   StrAccum str;      /* The string being accumulated */
13957   int nIndent;       /* Number of elements in aIndent */
13958   u16 aIndent[100];  /* Levels of indentation */
13959   char zBase[100];   /* Initial space */
13960 };
13961 
13962 /* A bitfield type for use inside of structures.  Always follow with :N where
13963 ** N is the number of bits.
13964 */
13965 typedef unsigned bft;  /* Bit Field Type */
13966 
13967 /*
13968 ** An instance of the virtual machine.  This structure contains the complete
13969 ** state of the virtual machine.
13970 **
13971 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
13972 ** is really a pointer to an instance of this structure.
13973 **
13974 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
13975 ** any virtual table method invocations made by the vdbe program. It is
13976 ** set to 2 for xDestroy method calls and 1 for all other methods. This
13977 ** variable is used for two purposes: to allow xDestroy methods to execute
13978 ** "DROP TABLE" statements and to prevent some nasty side effects of
13979 ** malloc failure when SQLite is invoked recursively by a virtual table
13980 ** method function.
13981 */
13982 struct Vdbe {
13983   sqlite3 *db;            /* The database connection that owns this statement */
13984   Op *aOp;                /* Space to hold the virtual machine's program */
13985   Mem *aMem;              /* The memory locations */
13986   Mem **apArg;            /* Arguments to currently executing user function */
13987   Mem *aColName;          /* Column names to return */
13988   Mem *pResultSet;        /* Pointer to an array of results */
13989   Parse *pParse;          /* Parsing context used to create this Vdbe */
13990   int nMem;               /* Number of memory locations currently allocated */
13991   int nOp;                /* Number of instructions in the program */
13992   int nCursor;            /* Number of slots in apCsr[] */
13993   u32 magic;              /* Magic number for sanity checking */
13994   char *zErrMsg;          /* Error message written here */
13995   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
13996   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
13997   Mem *aVar;              /* Values for the OP_Variable opcode. */
13998   char **azVar;           /* Name of variables */
13999   ynVar nVar;             /* Number of entries in aVar[] */
14000   ynVar nzVar;            /* Number of entries in azVar[] */
14001   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
14002   int pc;                 /* The program counter */
14003   int rc;                 /* Value to return */
14004   u16 nResColumn;         /* Number of columns in one row of the result set */
14005   u8 errorAction;         /* Recovery action to do in case of an error */
14006   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
14007   bft explain:2;          /* True if EXPLAIN present on SQL command */
14008   bft inVtabMethod:2;     /* See comments above */
14009   bft changeCntOn:1;      /* True to update the change-counter */
14010   bft expired:1;          /* True if the VM needs to be recompiled */
14011   bft runOnlyOnce:1;      /* Automatically expire on reset */
14012   bft usesStmtJournal:1;  /* True if uses a statement journal */
14013   bft readOnly:1;         /* True for statements that do not write */
14014   bft bIsReader:1;        /* True for statements that read */
14015   bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
14016   bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
14017   int nChange;            /* Number of db changes made since last reset */
14018   yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
14019   yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
14020   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
14021   u32 aCounter[5];        /* Counters used by sqlite3_stmt_status() */
14022 #ifndef SQLITE_OMIT_TRACE
14023   i64 startTime;          /* Time when query started - used for profiling */
14024 #endif
14025   i64 iCurrentTime;       /* Value of julianday('now') for this statement */
14026   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
14027   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
14028   i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
14029   char *zSql;             /* Text of the SQL statement that generated this */
14030   void *pFree;            /* Free this when deleting the vdbe */
14031 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
14032   Explain *pExplain;      /* The explainer */
14033   char *zExplain;         /* Explanation of data structures */
14034 #endif
14035   VdbeFrame *pFrame;      /* Parent frame */
14036   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
14037   int nFrame;             /* Number of frames in pFrame list */
14038   u32 expmask;            /* Binding to these vars invalidates VM */
14039   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
14040   int nOnceFlag;          /* Size of array aOnceFlag[] */
14041   u8 *aOnceFlag;          /* Flags for OP_Once */
14042   AuxData *pAuxData;      /* Linked list of auxdata allocations */
14043 };
14044 
14045 /*
14046 ** The following are allowed values for Vdbe.magic
14047 */
14048 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
14049 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
14050 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
14051 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
14052 
14053 /*
14054 ** Function prototypes
14055 */
14056 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
14057 void sqliteVdbePopStack(Vdbe*,int);
14058 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
14059 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
14060 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
14061 #endif
14062 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
14063 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
14064 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
14065 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
14066 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
14067 
14068 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
14069 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,const UnpackedRecord*,int*);
14070 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
14071 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
14072 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
14073 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
14074 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
14075 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
14076 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
14077 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
14078 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
14079 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
14080 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
14081 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
14082 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
14083 #ifdef SQLITE_OMIT_FLOATING_POINT
14084 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
14085 #else
14086 SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
14087 #endif
14088 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
14089 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
14090 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
14091 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
14092 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
14093 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
14094 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
14095 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
14096 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
14097 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
14098 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
14099 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
14100 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
14101 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
14102 #define VdbeMemDynamic(X)  \
14103   (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
14104 #define VdbeMemRelease(X)  \
14105   if( VdbeMemDynamic(X) ) sqlite3VdbeMemReleaseExternal(X);
14106 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
14107 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
14108 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
14109 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
14110 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
14111 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
14112 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
14113 
14114 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
14115 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
14116 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
14117 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
14118 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
14119 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
14120 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
14121 
14122 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
14123 SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
14124 SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
14125 #else
14126 # define sqlite3VdbeEnter(X)
14127 # define sqlite3VdbeLeave(X)
14128 #endif
14129 
14130 #ifdef SQLITE_DEBUG
14131 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
14132 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem*);
14133 #endif
14134 
14135 #ifndef SQLITE_OMIT_FOREIGN_KEY
14136 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
14137 #else
14138 # define sqlite3VdbeCheckFk(p,i) 0
14139 #endif
14140 
14141 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
14142 #ifdef SQLITE_DEBUG
14143 SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
14144 SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
14145 #endif
14146 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
14147 
14148 #ifndef SQLITE_OMIT_INCRBLOB
14149 SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
14150   #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
14151 #else
14152   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
14153   #define ExpandBlob(P) SQLITE_OK
14154 #endif
14155 
14156 #endif /* !defined(_VDBEINT_H_) */
14157 
14158 /************** End of vdbeInt.h *********************************************/
14159 /************** Continuing where we left off in status.c *********************/
14160 
14161 /*
14162 ** Variables in which to record status information.
14163 */
14164 typedef struct sqlite3StatType sqlite3StatType;
14165 static SQLITE_WSD struct sqlite3StatType {
14166   int nowValue[10];         /* Current value */
14167   int mxValue[10];          /* Maximum value */
14168 } sqlite3Stat = { {0,}, {0,} };
14169 
14170 
14171 /* The "wsdStat" macro will resolve to the status information
14172 ** state vector.  If writable static data is unsupported on the target,
14173 ** we have to locate the state vector at run-time.  In the more common
14174 ** case where writable static data is supported, wsdStat can refer directly
14175 ** to the "sqlite3Stat" state vector declared above.
14176 */
14177 #ifdef SQLITE_OMIT_WSD
14178 # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
14179 # define wsdStat x[0]
14180 #else
14181 # define wsdStatInit
14182 # define wsdStat sqlite3Stat
14183 #endif
14184 
14185 /*
14186 ** Return the current value of a status parameter.
14187 */
sqlite3StatusValue(int op)14188 SQLITE_PRIVATE int sqlite3StatusValue(int op){
14189   wsdStatInit;
14190   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14191   return wsdStat.nowValue[op];
14192 }
14193 
14194 /*
14195 ** Add N to the value of a status record.  It is assumed that the
14196 ** caller holds appropriate locks.
14197 */
sqlite3StatusAdd(int op,int N)14198 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
14199   wsdStatInit;
14200   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14201   wsdStat.nowValue[op] += N;
14202   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
14203     wsdStat.mxValue[op] = wsdStat.nowValue[op];
14204   }
14205 }
14206 
14207 /*
14208 ** Set the value of a status to X.
14209 */
sqlite3StatusSet(int op,int X)14210 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
14211   wsdStatInit;
14212   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14213   wsdStat.nowValue[op] = X;
14214   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
14215     wsdStat.mxValue[op] = wsdStat.nowValue[op];
14216   }
14217 }
14218 
14219 /*
14220 ** Query status information.
14221 **
14222 ** This implementation assumes that reading or writing an aligned
14223 ** 32-bit integer is an atomic operation.  If that assumption is not true,
14224 ** then this routine is not threadsafe.
14225 */
sqlite3_status(int op,int * pCurrent,int * pHighwater,int resetFlag)14226 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
14227   wsdStatInit;
14228   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
14229     return SQLITE_MISUSE_BKPT;
14230   }
14231   *pCurrent = wsdStat.nowValue[op];
14232   *pHighwater = wsdStat.mxValue[op];
14233   if( resetFlag ){
14234     wsdStat.mxValue[op] = wsdStat.nowValue[op];
14235   }
14236   return SQLITE_OK;
14237 }
14238 
14239 /*
14240 ** Query status information for a single database connection
14241 */
sqlite3_db_status(sqlite3 * db,int op,int * pCurrent,int * pHighwater,int resetFlag)14242 SQLITE_API int sqlite3_db_status(
14243   sqlite3 *db,          /* The database connection whose status is desired */
14244   int op,               /* Status verb */
14245   int *pCurrent,        /* Write current value here */
14246   int *pHighwater,      /* Write high-water mark here */
14247   int resetFlag         /* Reset high-water mark if true */
14248 ){
14249   int rc = SQLITE_OK;   /* Return code */
14250   sqlite3_mutex_enter(db->mutex);
14251   switch( op ){
14252     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
14253       *pCurrent = db->lookaside.nOut;
14254       *pHighwater = db->lookaside.mxOut;
14255       if( resetFlag ){
14256         db->lookaside.mxOut = db->lookaside.nOut;
14257       }
14258       break;
14259     }
14260 
14261     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
14262     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
14263     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
14264       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
14265       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
14266       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
14267       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
14268       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
14269       *pCurrent = 0;
14270       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
14271       if( resetFlag ){
14272         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
14273       }
14274       break;
14275     }
14276 
14277     /*
14278     ** Return an approximation for the amount of memory currently used
14279     ** by all pagers associated with the given database connection.  The
14280     ** highwater mark is meaningless and is returned as zero.
14281     */
14282     case SQLITE_DBSTATUS_CACHE_USED: {
14283       int totalUsed = 0;
14284       int i;
14285       sqlite3BtreeEnterAll(db);
14286       for(i=0; i<db->nDb; i++){
14287         Btree *pBt = db->aDb[i].pBt;
14288         if( pBt ){
14289           Pager *pPager = sqlite3BtreePager(pBt);
14290           totalUsed += sqlite3PagerMemUsed(pPager);
14291         }
14292       }
14293       sqlite3BtreeLeaveAll(db);
14294       *pCurrent = totalUsed;
14295       *pHighwater = 0;
14296       break;
14297     }
14298 
14299     /*
14300     ** *pCurrent gets an accurate estimate of the amount of memory used
14301     ** to store the schema for all databases (main, temp, and any ATTACHed
14302     ** databases.  *pHighwater is set to zero.
14303     */
14304     case SQLITE_DBSTATUS_SCHEMA_USED: {
14305       int i;                      /* Used to iterate through schemas */
14306       int nByte = 0;              /* Used to accumulate return value */
14307 
14308       sqlite3BtreeEnterAll(db);
14309       db->pnBytesFreed = &nByte;
14310       for(i=0; i<db->nDb; i++){
14311         Schema *pSchema = db->aDb[i].pSchema;
14312         if( ALWAYS(pSchema!=0) ){
14313           HashElem *p;
14314 
14315           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
14316               pSchema->tblHash.count
14317             + pSchema->trigHash.count
14318             + pSchema->idxHash.count
14319             + pSchema->fkeyHash.count
14320           );
14321           nByte += sqlite3MallocSize(pSchema->tblHash.ht);
14322           nByte += sqlite3MallocSize(pSchema->trigHash.ht);
14323           nByte += sqlite3MallocSize(pSchema->idxHash.ht);
14324           nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
14325 
14326           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
14327             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
14328           }
14329           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
14330             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
14331           }
14332         }
14333       }
14334       db->pnBytesFreed = 0;
14335       sqlite3BtreeLeaveAll(db);
14336 
14337       *pHighwater = 0;
14338       *pCurrent = nByte;
14339       break;
14340     }
14341 
14342     /*
14343     ** *pCurrent gets an accurate estimate of the amount of memory used
14344     ** to store all prepared statements.
14345     ** *pHighwater is set to zero.
14346     */
14347     case SQLITE_DBSTATUS_STMT_USED: {
14348       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
14349       int nByte = 0;              /* Used to accumulate return value */
14350 
14351       db->pnBytesFreed = &nByte;
14352       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
14353         sqlite3VdbeClearObject(db, pVdbe);
14354         sqlite3DbFree(db, pVdbe);
14355       }
14356       db->pnBytesFreed = 0;
14357 
14358       *pHighwater = 0;
14359       *pCurrent = nByte;
14360 
14361       break;
14362     }
14363 
14364     /*
14365     ** Set *pCurrent to the total cache hits or misses encountered by all
14366     ** pagers the database handle is connected to. *pHighwater is always set
14367     ** to zero.
14368     */
14369     case SQLITE_DBSTATUS_CACHE_HIT:
14370     case SQLITE_DBSTATUS_CACHE_MISS:
14371     case SQLITE_DBSTATUS_CACHE_WRITE:{
14372       int i;
14373       int nRet = 0;
14374       assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
14375       assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
14376 
14377       for(i=0; i<db->nDb; i++){
14378         if( db->aDb[i].pBt ){
14379           Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
14380           sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
14381         }
14382       }
14383       *pHighwater = 0;
14384       *pCurrent = nRet;
14385       break;
14386     }
14387 
14388     /* Set *pCurrent to non-zero if there are unresolved deferred foreign
14389     ** key constraints.  Set *pCurrent to zero if all foreign key constraints
14390     ** have been satisfied.  The *pHighwater is always set to zero.
14391     */
14392     case SQLITE_DBSTATUS_DEFERRED_FKS: {
14393       *pHighwater = 0;
14394       *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
14395       break;
14396     }
14397 
14398     default: {
14399       rc = SQLITE_ERROR;
14400     }
14401   }
14402   sqlite3_mutex_leave(db->mutex);
14403   return rc;
14404 }
14405 
14406 /************** End of status.c **********************************************/
14407 /************** Begin file date.c ********************************************/
14408 /*
14409 ** 2003 October 31
14410 **
14411 ** The author disclaims copyright to this source code.  In place of
14412 ** a legal notice, here is a blessing:
14413 **
14414 **    May you do good and not evil.
14415 **    May you find forgiveness for yourself and forgive others.
14416 **    May you share freely, never taking more than you give.
14417 **
14418 *************************************************************************
14419 ** This file contains the C functions that implement date and time
14420 ** functions for SQLite.
14421 **
14422 ** There is only one exported symbol in this file - the function
14423 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
14424 ** All other code has file scope.
14425 **
14426 ** SQLite processes all times and dates as Julian Day numbers.  The
14427 ** dates and times are stored as the number of days since noon
14428 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
14429 ** calendar system.
14430 **
14431 ** 1970-01-01 00:00:00 is JD 2440587.5
14432 ** 2000-01-01 00:00:00 is JD 2451544.5
14433 **
14434 ** This implemention requires years to be expressed as a 4-digit number
14435 ** which means that only dates between 0000-01-01 and 9999-12-31 can
14436 ** be represented, even though julian day numbers allow a much wider
14437 ** range of dates.
14438 **
14439 ** The Gregorian calendar system is used for all dates and times,
14440 ** even those that predate the Gregorian calendar.  Historians usually
14441 ** use the Julian calendar for dates prior to 1582-10-15 and for some
14442 ** dates afterwards, depending on locale.  Beware of this difference.
14443 **
14444 ** The conversion algorithms are implemented based on descriptions
14445 ** in the following text:
14446 **
14447 **      Jean Meeus
14448 **      Astronomical Algorithms, 2nd Edition, 1998
14449 **      ISBM 0-943396-61-1
14450 **      Willmann-Bell, Inc
14451 **      Richmond, Virginia (USA)
14452 */
14453 /* #include <stdlib.h> */
14454 /* #include <assert.h> */
14455 #include <time.h>
14456 
14457 #ifndef SQLITE_OMIT_DATETIME_FUNCS
14458 
14459 
14460 /*
14461 ** A structure for holding a single date and time.
14462 */
14463 typedef struct DateTime DateTime;
14464 struct DateTime {
14465   sqlite3_int64 iJD; /* The julian day number times 86400000 */
14466   int Y, M, D;       /* Year, month, and day */
14467   int h, m;          /* Hour and minutes */
14468   int tz;            /* Timezone offset in minutes */
14469   double s;          /* Seconds */
14470   char validYMD;     /* True (1) if Y,M,D are valid */
14471   char validHMS;     /* True (1) if h,m,s are valid */
14472   char validJD;      /* True (1) if iJD is valid */
14473   char validTZ;      /* True (1) if tz is valid */
14474 };
14475 
14476 
14477 /*
14478 ** Convert zDate into one or more integers.  Additional arguments
14479 ** come in groups of 5 as follows:
14480 **
14481 **       N       number of digits in the integer
14482 **       min     minimum allowed value of the integer
14483 **       max     maximum allowed value of the integer
14484 **       nextC   first character after the integer
14485 **       pVal    where to write the integers value.
14486 **
14487 ** Conversions continue until one with nextC==0 is encountered.
14488 ** The function returns the number of successful conversions.
14489 */
getDigits(const char * zDate,...)14490 static int getDigits(const char *zDate, ...){
14491   va_list ap;
14492   int val;
14493   int N;
14494   int min;
14495   int max;
14496   int nextC;
14497   int *pVal;
14498   int cnt = 0;
14499   va_start(ap, zDate);
14500   do{
14501     N = va_arg(ap, int);
14502     min = va_arg(ap, int);
14503     max = va_arg(ap, int);
14504     nextC = va_arg(ap, int);
14505     pVal = va_arg(ap, int*);
14506     val = 0;
14507     while( N-- ){
14508       if( !sqlite3Isdigit(*zDate) ){
14509         goto end_getDigits;
14510       }
14511       val = val*10 + *zDate - '0';
14512       zDate++;
14513     }
14514     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
14515       goto end_getDigits;
14516     }
14517     *pVal = val;
14518     zDate++;
14519     cnt++;
14520   }while( nextC );
14521 end_getDigits:
14522   va_end(ap);
14523   return cnt;
14524 }
14525 
14526 /*
14527 ** Parse a timezone extension on the end of a date-time.
14528 ** The extension is of the form:
14529 **
14530 **        (+/-)HH:MM
14531 **
14532 ** Or the "zulu" notation:
14533 **
14534 **        Z
14535 **
14536 ** If the parse is successful, write the number of minutes
14537 ** of change in p->tz and return 0.  If a parser error occurs,
14538 ** return non-zero.
14539 **
14540 ** A missing specifier is not considered an error.
14541 */
parseTimezone(const char * zDate,DateTime * p)14542 static int parseTimezone(const char *zDate, DateTime *p){
14543   int sgn = 0;
14544   int nHr, nMn;
14545   int c;
14546   while( sqlite3Isspace(*zDate) ){ zDate++; }
14547   p->tz = 0;
14548   c = *zDate;
14549   if( c=='-' ){
14550     sgn = -1;
14551   }else if( c=='+' ){
14552     sgn = +1;
14553   }else if( c=='Z' || c=='z' ){
14554     zDate++;
14555     goto zulu_time;
14556   }else{
14557     return c!=0;
14558   }
14559   zDate++;
14560   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
14561     return 1;
14562   }
14563   zDate += 5;
14564   p->tz = sgn*(nMn + nHr*60);
14565 zulu_time:
14566   while( sqlite3Isspace(*zDate) ){ zDate++; }
14567   return *zDate!=0;
14568 }
14569 
14570 /*
14571 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
14572 ** The HH, MM, and SS must each be exactly 2 digits.  The
14573 ** fractional seconds FFFF can be one or more digits.
14574 **
14575 ** Return 1 if there is a parsing error and 0 on success.
14576 */
parseHhMmSs(const char * zDate,DateTime * p)14577 static int parseHhMmSs(const char *zDate, DateTime *p){
14578   int h, m, s;
14579   double ms = 0.0;
14580   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
14581     return 1;
14582   }
14583   zDate += 5;
14584   if( *zDate==':' ){
14585     zDate++;
14586     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
14587       return 1;
14588     }
14589     zDate += 2;
14590     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
14591       double rScale = 1.0;
14592       zDate++;
14593       while( sqlite3Isdigit(*zDate) ){
14594         ms = ms*10.0 + *zDate - '0';
14595         rScale *= 10.0;
14596         zDate++;
14597       }
14598       ms /= rScale;
14599     }
14600   }else{
14601     s = 0;
14602   }
14603   p->validJD = 0;
14604   p->validHMS = 1;
14605   p->h = h;
14606   p->m = m;
14607   p->s = s + ms;
14608   if( parseTimezone(zDate, p) ) return 1;
14609   p->validTZ = (p->tz!=0)?1:0;
14610   return 0;
14611 }
14612 
14613 /*
14614 ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
14615 ** that the YYYY-MM-DD is according to the Gregorian calendar.
14616 **
14617 ** Reference:  Meeus page 61
14618 */
computeJD(DateTime * p)14619 static void computeJD(DateTime *p){
14620   int Y, M, D, A, B, X1, X2;
14621 
14622   if( p->validJD ) return;
14623   if( p->validYMD ){
14624     Y = p->Y;
14625     M = p->M;
14626     D = p->D;
14627   }else{
14628     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
14629     M = 1;
14630     D = 1;
14631   }
14632   if( M<=2 ){
14633     Y--;
14634     M += 12;
14635   }
14636   A = Y/100;
14637   B = 2 - A + (A/4);
14638   X1 = 36525*(Y+4716)/100;
14639   X2 = 306001*(M+1)/10000;
14640   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
14641   p->validJD = 1;
14642   if( p->validHMS ){
14643     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
14644     if( p->validTZ ){
14645       p->iJD -= p->tz*60000;
14646       p->validYMD = 0;
14647       p->validHMS = 0;
14648       p->validTZ = 0;
14649     }
14650   }
14651 }
14652 
14653 /*
14654 ** Parse dates of the form
14655 **
14656 **     YYYY-MM-DD HH:MM:SS.FFF
14657 **     YYYY-MM-DD HH:MM:SS
14658 **     YYYY-MM-DD HH:MM
14659 **     YYYY-MM-DD
14660 **
14661 ** Write the result into the DateTime structure and return 0
14662 ** on success and 1 if the input string is not a well-formed
14663 ** date.
14664 */
parseYyyyMmDd(const char * zDate,DateTime * p)14665 static int parseYyyyMmDd(const char *zDate, DateTime *p){
14666   int Y, M, D, neg;
14667 
14668   if( zDate[0]=='-' ){
14669     zDate++;
14670     neg = 1;
14671   }else{
14672     neg = 0;
14673   }
14674   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
14675     return 1;
14676   }
14677   zDate += 10;
14678   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
14679   if( parseHhMmSs(zDate, p)==0 ){
14680     /* We got the time */
14681   }else if( *zDate==0 ){
14682     p->validHMS = 0;
14683   }else{
14684     return 1;
14685   }
14686   p->validJD = 0;
14687   p->validYMD = 1;
14688   p->Y = neg ? -Y : Y;
14689   p->M = M;
14690   p->D = D;
14691   if( p->validTZ ){
14692     computeJD(p);
14693   }
14694   return 0;
14695 }
14696 
14697 /*
14698 ** Set the time to the current time reported by the VFS.
14699 **
14700 ** Return the number of errors.
14701 */
setDateTimeToCurrent(sqlite3_context * context,DateTime * p)14702 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
14703   p->iJD = sqlite3StmtCurrentTime(context);
14704   if( p->iJD>0 ){
14705     p->validJD = 1;
14706     return 0;
14707   }else{
14708     return 1;
14709   }
14710 }
14711 
14712 /*
14713 ** Attempt to parse the given string into a Julian Day Number.  Return
14714 ** the number of errors.
14715 **
14716 ** The following are acceptable forms for the input string:
14717 **
14718 **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
14719 **      DDDD.DD
14720 **      now
14721 **
14722 ** In the first form, the +/-HH:MM is always optional.  The fractional
14723 ** seconds extension (the ".FFF") is optional.  The seconds portion
14724 ** (":SS.FFF") is option.  The year and date can be omitted as long
14725 ** as there is a time string.  The time string can be omitted as long
14726 ** as there is a year and date.
14727 */
parseDateOrTime(sqlite3_context * context,const char * zDate,DateTime * p)14728 static int parseDateOrTime(
14729   sqlite3_context *context,
14730   const char *zDate,
14731   DateTime *p
14732 ){
14733   double r;
14734   if( parseYyyyMmDd(zDate,p)==0 ){
14735     return 0;
14736   }else if( parseHhMmSs(zDate, p)==0 ){
14737     return 0;
14738   }else if( sqlite3StrICmp(zDate,"now")==0){
14739     return setDateTimeToCurrent(context, p);
14740   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
14741     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
14742     p->validJD = 1;
14743     return 0;
14744   }
14745   return 1;
14746 }
14747 
14748 /*
14749 ** Compute the Year, Month, and Day from the julian day number.
14750 */
computeYMD(DateTime * p)14751 static void computeYMD(DateTime *p){
14752   int Z, A, B, C, D, E, X1;
14753   if( p->validYMD ) return;
14754   if( !p->validJD ){
14755     p->Y = 2000;
14756     p->M = 1;
14757     p->D = 1;
14758   }else{
14759     Z = (int)((p->iJD + 43200000)/86400000);
14760     A = (int)((Z - 1867216.25)/36524.25);
14761     A = Z + 1 + A - (A/4);
14762     B = A + 1524;
14763     C = (int)((B - 122.1)/365.25);
14764     D = (36525*C)/100;
14765     E = (int)((B-D)/30.6001);
14766     X1 = (int)(30.6001*E);
14767     p->D = B - D - X1;
14768     p->M = E<14 ? E-1 : E-13;
14769     p->Y = p->M>2 ? C - 4716 : C - 4715;
14770   }
14771   p->validYMD = 1;
14772 }
14773 
14774 /*
14775 ** Compute the Hour, Minute, and Seconds from the julian day number.
14776 */
computeHMS(DateTime * p)14777 static void computeHMS(DateTime *p){
14778   int s;
14779   if( p->validHMS ) return;
14780   computeJD(p);
14781   s = (int)((p->iJD + 43200000) % 86400000);
14782   p->s = s/1000.0;
14783   s = (int)p->s;
14784   p->s -= s;
14785   p->h = s/3600;
14786   s -= p->h*3600;
14787   p->m = s/60;
14788   p->s += s - p->m*60;
14789   p->validHMS = 1;
14790 }
14791 
14792 /*
14793 ** Compute both YMD and HMS
14794 */
computeYMD_HMS(DateTime * p)14795 static void computeYMD_HMS(DateTime *p){
14796   computeYMD(p);
14797   computeHMS(p);
14798 }
14799 
14800 /*
14801 ** Clear the YMD and HMS and the TZ
14802 */
clearYMD_HMS_TZ(DateTime * p)14803 static void clearYMD_HMS_TZ(DateTime *p){
14804   p->validYMD = 0;
14805   p->validHMS = 0;
14806   p->validTZ = 0;
14807 }
14808 
14809 /*
14810 ** On recent Windows platforms, the localtime_s() function is available
14811 ** as part of the "Secure CRT". It is essentially equivalent to
14812 ** localtime_r() available under most POSIX platforms, except that the
14813 ** order of the parameters is reversed.
14814 **
14815 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
14816 **
14817 ** If the user has not indicated to use localtime_r() or localtime_s()
14818 ** already, check for an MSVC build environment that provides
14819 ** localtime_s().
14820 */
14821 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
14822      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
14823 #define HAVE_LOCALTIME_S 1
14824 #endif
14825 
14826 #ifndef SQLITE_OMIT_LOCALTIME
14827 /*
14828 ** The following routine implements the rough equivalent of localtime_r()
14829 ** using whatever operating-system specific localtime facility that
14830 ** is available.  This routine returns 0 on success and
14831 ** non-zero on any kind of error.
14832 **
14833 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
14834 ** routine will always fail.
14835 **
14836 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
14837 ** library function localtime_r() is used to assist in the calculation of
14838 ** local time.
14839 */
osLocaltime(time_t * t,struct tm * pTm)14840 static int osLocaltime(time_t *t, struct tm *pTm){
14841   int rc;
14842 #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
14843       && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
14844   struct tm *pX;
14845 #if SQLITE_THREADSAFE>0
14846   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14847 #endif
14848   sqlite3_mutex_enter(mutex);
14849   pX = localtime(t);
14850 #ifndef SQLITE_OMIT_BUILTIN_TEST
14851   if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
14852 #endif
14853   if( pX ) *pTm = *pX;
14854   sqlite3_mutex_leave(mutex);
14855   rc = pX==0;
14856 #else
14857 #ifndef SQLITE_OMIT_BUILTIN_TEST
14858   if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
14859 #endif
14860 #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
14861   rc = localtime_r(t, pTm)==0;
14862 #else
14863   rc = localtime_s(pTm, t);
14864 #endif /* HAVE_LOCALTIME_R */
14865 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
14866   return rc;
14867 }
14868 #endif /* SQLITE_OMIT_LOCALTIME */
14869 
14870 
14871 #ifndef SQLITE_OMIT_LOCALTIME
14872 /*
14873 ** Compute the difference (in milliseconds) between localtime and UTC
14874 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
14875 ** return this value and set *pRc to SQLITE_OK.
14876 **
14877 ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
14878 ** is undefined in this case.
14879 */
localtimeOffset(DateTime * p,sqlite3_context * pCtx,int * pRc)14880 static sqlite3_int64 localtimeOffset(
14881   DateTime *p,                    /* Date at which to calculate offset */
14882   sqlite3_context *pCtx,          /* Write error here if one occurs */
14883   int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
14884 ){
14885   DateTime x, y;
14886   time_t t;
14887   struct tm sLocal;
14888 
14889   /* Initialize the contents of sLocal to avoid a compiler warning. */
14890   memset(&sLocal, 0, sizeof(sLocal));
14891 
14892   x = *p;
14893   computeYMD_HMS(&x);
14894   if( x.Y<1971 || x.Y>=2038 ){
14895     /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
14896     ** works for years between 1970 and 2037. For dates outside this range,
14897     ** SQLite attempts to map the year into an equivalent year within this
14898     ** range, do the calculation, then map the year back.
14899     */
14900     x.Y = 2000;
14901     x.M = 1;
14902     x.D = 1;
14903     x.h = 0;
14904     x.m = 0;
14905     x.s = 0.0;
14906   } else {
14907     int s = (int)(x.s + 0.5);
14908     x.s = s;
14909   }
14910   x.tz = 0;
14911   x.validJD = 0;
14912   computeJD(&x);
14913   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
14914   if( osLocaltime(&t, &sLocal) ){
14915     sqlite3_result_error(pCtx, "local time unavailable", -1);
14916     *pRc = SQLITE_ERROR;
14917     return 0;
14918   }
14919   y.Y = sLocal.tm_year + 1900;
14920   y.M = sLocal.tm_mon + 1;
14921   y.D = sLocal.tm_mday;
14922   y.h = sLocal.tm_hour;
14923   y.m = sLocal.tm_min;
14924   y.s = sLocal.tm_sec;
14925   y.validYMD = 1;
14926   y.validHMS = 1;
14927   y.validJD = 0;
14928   y.validTZ = 0;
14929   computeJD(&y);
14930   *pRc = SQLITE_OK;
14931   return y.iJD - x.iJD;
14932 }
14933 #endif /* SQLITE_OMIT_LOCALTIME */
14934 
14935 /*
14936 ** Process a modifier to a date-time stamp.  The modifiers are
14937 ** as follows:
14938 **
14939 **     NNN days
14940 **     NNN hours
14941 **     NNN minutes
14942 **     NNN.NNNN seconds
14943 **     NNN months
14944 **     NNN years
14945 **     start of month
14946 **     start of year
14947 **     start of week
14948 **     start of day
14949 **     weekday N
14950 **     unixepoch
14951 **     localtime
14952 **     utc
14953 **
14954 ** Return 0 on success and 1 if there is any kind of error. If the error
14955 ** is in a system call (i.e. localtime()), then an error message is written
14956 ** to context pCtx. If the error is an unrecognized modifier, no error is
14957 ** written to pCtx.
14958 */
parseModifier(sqlite3_context * pCtx,const char * zMod,DateTime * p)14959 static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
14960   int rc = 1;
14961   int n;
14962   double r;
14963   char *z, zBuf[30];
14964   z = zBuf;
14965   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
14966     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
14967   }
14968   z[n] = 0;
14969   switch( z[0] ){
14970 #ifndef SQLITE_OMIT_LOCALTIME
14971     case 'l': {
14972       /*    localtime
14973       **
14974       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
14975       ** show local time.
14976       */
14977       if( strcmp(z, "localtime")==0 ){
14978         computeJD(p);
14979         p->iJD += localtimeOffset(p, pCtx, &rc);
14980         clearYMD_HMS_TZ(p);
14981       }
14982       break;
14983     }
14984 #endif
14985     case 'u': {
14986       /*
14987       **    unixepoch
14988       **
14989       ** Treat the current value of p->iJD as the number of
14990       ** seconds since 1970.  Convert to a real julian day number.
14991       */
14992       if( strcmp(z, "unixepoch")==0 && p->validJD ){
14993         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
14994         clearYMD_HMS_TZ(p);
14995         rc = 0;
14996       }
14997 #ifndef SQLITE_OMIT_LOCALTIME
14998       else if( strcmp(z, "utc")==0 ){
14999         sqlite3_int64 c1;
15000         computeJD(p);
15001         c1 = localtimeOffset(p, pCtx, &rc);
15002         if( rc==SQLITE_OK ){
15003           p->iJD -= c1;
15004           clearYMD_HMS_TZ(p);
15005           p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
15006         }
15007       }
15008 #endif
15009       break;
15010     }
15011     case 'w': {
15012       /*
15013       **    weekday N
15014       **
15015       ** Move the date to the same time on the next occurrence of
15016       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
15017       ** date is already on the appropriate weekday, this is a no-op.
15018       */
15019       if( strncmp(z, "weekday ", 8)==0
15020                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
15021                && (n=(int)r)==r && n>=0 && r<7 ){
15022         sqlite3_int64 Z;
15023         computeYMD_HMS(p);
15024         p->validTZ = 0;
15025         p->validJD = 0;
15026         computeJD(p);
15027         Z = ((p->iJD + 129600000)/86400000) % 7;
15028         if( Z>n ) Z -= 7;
15029         p->iJD += (n - Z)*86400000;
15030         clearYMD_HMS_TZ(p);
15031         rc = 0;
15032       }
15033       break;
15034     }
15035     case 's': {
15036       /*
15037       **    start of TTTTT
15038       **
15039       ** Move the date backwards to the beginning of the current day,
15040       ** or month or year.
15041       */
15042       if( strncmp(z, "start of ", 9)!=0 ) break;
15043       z += 9;
15044       computeYMD(p);
15045       p->validHMS = 1;
15046       p->h = p->m = 0;
15047       p->s = 0.0;
15048       p->validTZ = 0;
15049       p->validJD = 0;
15050       if( strcmp(z,"month")==0 ){
15051         p->D = 1;
15052         rc = 0;
15053       }else if( strcmp(z,"year")==0 ){
15054         computeYMD(p);
15055         p->M = 1;
15056         p->D = 1;
15057         rc = 0;
15058       }else if( strcmp(z,"day")==0 ){
15059         rc = 0;
15060       }
15061       break;
15062     }
15063     case '+':
15064     case '-':
15065     case '0':
15066     case '1':
15067     case '2':
15068     case '3':
15069     case '4':
15070     case '5':
15071     case '6':
15072     case '7':
15073     case '8':
15074     case '9': {
15075       double rRounder;
15076       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
15077       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
15078         rc = 1;
15079         break;
15080       }
15081       if( z[n]==':' ){
15082         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
15083         ** specified number of hours, minutes, seconds, and fractional seconds
15084         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
15085         ** omitted.
15086         */
15087         const char *z2 = z;
15088         DateTime tx;
15089         sqlite3_int64 day;
15090         if( !sqlite3Isdigit(*z2) ) z2++;
15091         memset(&tx, 0, sizeof(tx));
15092         if( parseHhMmSs(z2, &tx) ) break;
15093         computeJD(&tx);
15094         tx.iJD -= 43200000;
15095         day = tx.iJD/86400000;
15096         tx.iJD -= day*86400000;
15097         if( z[0]=='-' ) tx.iJD = -tx.iJD;
15098         computeJD(p);
15099         clearYMD_HMS_TZ(p);
15100         p->iJD += tx.iJD;
15101         rc = 0;
15102         break;
15103       }
15104       z += n;
15105       while( sqlite3Isspace(*z) ) z++;
15106       n = sqlite3Strlen30(z);
15107       if( n>10 || n<3 ) break;
15108       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
15109       computeJD(p);
15110       rc = 0;
15111       rRounder = r<0 ? -0.5 : +0.5;
15112       if( n==3 && strcmp(z,"day")==0 ){
15113         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
15114       }else if( n==4 && strcmp(z,"hour")==0 ){
15115         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
15116       }else if( n==6 && strcmp(z,"minute")==0 ){
15117         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
15118       }else if( n==6 && strcmp(z,"second")==0 ){
15119         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
15120       }else if( n==5 && strcmp(z,"month")==0 ){
15121         int x, y;
15122         computeYMD_HMS(p);
15123         p->M += (int)r;
15124         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
15125         p->Y += x;
15126         p->M -= x*12;
15127         p->validJD = 0;
15128         computeJD(p);
15129         y = (int)r;
15130         if( y!=r ){
15131           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
15132         }
15133       }else if( n==4 && strcmp(z,"year")==0 ){
15134         int y = (int)r;
15135         computeYMD_HMS(p);
15136         p->Y += y;
15137         p->validJD = 0;
15138         computeJD(p);
15139         if( y!=r ){
15140           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
15141         }
15142       }else{
15143         rc = 1;
15144       }
15145       clearYMD_HMS_TZ(p);
15146       break;
15147     }
15148     default: {
15149       break;
15150     }
15151   }
15152   return rc;
15153 }
15154 
15155 /*
15156 ** Process time function arguments.  argv[0] is a date-time stamp.
15157 ** argv[1] and following are modifiers.  Parse them all and write
15158 ** the resulting time into the DateTime structure p.  Return 0
15159 ** on success and 1 if there are any errors.
15160 **
15161 ** If there are zero parameters (if even argv[0] is undefined)
15162 ** then assume a default value of "now" for argv[0].
15163 */
isDate(sqlite3_context * context,int argc,sqlite3_value ** argv,DateTime * p)15164 static int isDate(
15165   sqlite3_context *context,
15166   int argc,
15167   sqlite3_value **argv,
15168   DateTime *p
15169 ){
15170   int i;
15171   const unsigned char *z;
15172   int eType;
15173   memset(p, 0, sizeof(*p));
15174   if( argc==0 ){
15175     return setDateTimeToCurrent(context, p);
15176   }
15177   if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
15178                    || eType==SQLITE_INTEGER ){
15179     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
15180     p->validJD = 1;
15181   }else{
15182     z = sqlite3_value_text(argv[0]);
15183     if( !z || parseDateOrTime(context, (char*)z, p) ){
15184       return 1;
15185     }
15186   }
15187   for(i=1; i<argc; i++){
15188     z = sqlite3_value_text(argv[i]);
15189     if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
15190   }
15191   return 0;
15192 }
15193 
15194 
15195 /*
15196 ** The following routines implement the various date and time functions
15197 ** of SQLite.
15198 */
15199 
15200 /*
15201 **    julianday( TIMESTRING, MOD, MOD, ...)
15202 **
15203 ** Return the julian day number of the date specified in the arguments
15204 */
juliandayFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)15205 static void juliandayFunc(
15206   sqlite3_context *context,
15207   int argc,
15208   sqlite3_value **argv
15209 ){
15210   DateTime x;
15211   if( isDate(context, argc, argv, &x)==0 ){
15212     computeJD(&x);
15213     sqlite3_result_double(context, x.iJD/86400000.0);
15214   }
15215 }
15216 
15217 /*
15218 **    datetime( TIMESTRING, MOD, MOD, ...)
15219 **
15220 ** Return YYYY-MM-DD HH:MM:SS
15221 */
datetimeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)15222 static void datetimeFunc(
15223   sqlite3_context *context,
15224   int argc,
15225   sqlite3_value **argv
15226 ){
15227   DateTime x;
15228   if( isDate(context, argc, argv, &x)==0 ){
15229     char zBuf[100];
15230     computeYMD_HMS(&x);
15231     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
15232                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
15233     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15234   }
15235 }
15236 
15237 /*
15238 **    time( TIMESTRING, MOD, MOD, ...)
15239 **
15240 ** Return HH:MM:SS
15241 */
timeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)15242 static void timeFunc(
15243   sqlite3_context *context,
15244   int argc,
15245   sqlite3_value **argv
15246 ){
15247   DateTime x;
15248   if( isDate(context, argc, argv, &x)==0 ){
15249     char zBuf[100];
15250     computeHMS(&x);
15251     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
15252     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15253   }
15254 }
15255 
15256 /*
15257 **    date( TIMESTRING, MOD, MOD, ...)
15258 **
15259 ** Return YYYY-MM-DD
15260 */
dateFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)15261 static void dateFunc(
15262   sqlite3_context *context,
15263   int argc,
15264   sqlite3_value **argv
15265 ){
15266   DateTime x;
15267   if( isDate(context, argc, argv, &x)==0 ){
15268     char zBuf[100];
15269     computeYMD(&x);
15270     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
15271     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15272   }
15273 }
15274 
15275 /*
15276 **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
15277 **
15278 ** Return a string described by FORMAT.  Conversions as follows:
15279 **
15280 **   %d  day of month
15281 **   %f  ** fractional seconds  SS.SSS
15282 **   %H  hour 00-24
15283 **   %j  day of year 000-366
15284 **   %J  ** Julian day number
15285 **   %m  month 01-12
15286 **   %M  minute 00-59
15287 **   %s  seconds since 1970-01-01
15288 **   %S  seconds 00-59
15289 **   %w  day of week 0-6  sunday==0
15290 **   %W  week of year 00-53
15291 **   %Y  year 0000-9999
15292 **   %%  %
15293 */
strftimeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)15294 static void strftimeFunc(
15295   sqlite3_context *context,
15296   int argc,
15297   sqlite3_value **argv
15298 ){
15299   DateTime x;
15300   u64 n;
15301   size_t i,j;
15302   char *z;
15303   sqlite3 *db;
15304   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
15305   char zBuf[100];
15306   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
15307   db = sqlite3_context_db_handle(context);
15308   for(i=0, n=1; zFmt[i]; i++, n++){
15309     if( zFmt[i]=='%' ){
15310       switch( zFmt[i+1] ){
15311         case 'd':
15312         case 'H':
15313         case 'm':
15314         case 'M':
15315         case 'S':
15316         case 'W':
15317           n++;
15318           /* fall thru */
15319         case 'w':
15320         case '%':
15321           break;
15322         case 'f':
15323           n += 8;
15324           break;
15325         case 'j':
15326           n += 3;
15327           break;
15328         case 'Y':
15329           n += 8;
15330           break;
15331         case 's':
15332         case 'J':
15333           n += 50;
15334           break;
15335         default:
15336           return;  /* ERROR.  return a NULL */
15337       }
15338       i++;
15339     }
15340   }
15341   testcase( n==sizeof(zBuf)-1 );
15342   testcase( n==sizeof(zBuf) );
15343   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
15344   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
15345   if( n<sizeof(zBuf) ){
15346     z = zBuf;
15347   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
15348     sqlite3_result_error_toobig(context);
15349     return;
15350   }else{
15351     z = sqlite3DbMallocRaw(db, (int)n);
15352     if( z==0 ){
15353       sqlite3_result_error_nomem(context);
15354       return;
15355     }
15356   }
15357   computeJD(&x);
15358   computeYMD_HMS(&x);
15359   for(i=j=0; zFmt[i]; i++){
15360     if( zFmt[i]!='%' ){
15361       z[j++] = zFmt[i];
15362     }else{
15363       i++;
15364       switch( zFmt[i] ){
15365         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
15366         case 'f': {
15367           double s = x.s;
15368           if( s>59.999 ) s = 59.999;
15369           sqlite3_snprintf(7, &z[j],"%06.3f", s);
15370           j += sqlite3Strlen30(&z[j]);
15371           break;
15372         }
15373         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
15374         case 'W': /* Fall thru */
15375         case 'j': {
15376           int nDay;             /* Number of days since 1st day of year */
15377           DateTime y = x;
15378           y.validJD = 0;
15379           y.M = 1;
15380           y.D = 1;
15381           computeJD(&y);
15382           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
15383           if( zFmt[i]=='W' ){
15384             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
15385             wd = (int)(((x.iJD+43200000)/86400000)%7);
15386             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
15387             j += 2;
15388           }else{
15389             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
15390             j += 3;
15391           }
15392           break;
15393         }
15394         case 'J': {
15395           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
15396           j+=sqlite3Strlen30(&z[j]);
15397           break;
15398         }
15399         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
15400         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
15401         case 's': {
15402           sqlite3_snprintf(30,&z[j],"%lld",
15403                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
15404           j += sqlite3Strlen30(&z[j]);
15405           break;
15406         }
15407         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
15408         case 'w': {
15409           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
15410           break;
15411         }
15412         case 'Y': {
15413           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
15414           break;
15415         }
15416         default:   z[j++] = '%'; break;
15417       }
15418     }
15419   }
15420   z[j] = 0;
15421   sqlite3_result_text(context, z, -1,
15422                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
15423 }
15424 
15425 /*
15426 ** current_time()
15427 **
15428 ** This function returns the same value as time('now').
15429 */
ctimeFunc(sqlite3_context * context,int NotUsed,sqlite3_value ** NotUsed2)15430 static void ctimeFunc(
15431   sqlite3_context *context,
15432   int NotUsed,
15433   sqlite3_value **NotUsed2
15434 ){
15435   UNUSED_PARAMETER2(NotUsed, NotUsed2);
15436   timeFunc(context, 0, 0);
15437 }
15438 
15439 /*
15440 ** current_date()
15441 **
15442 ** This function returns the same value as date('now').
15443 */
cdateFunc(sqlite3_context * context,int NotUsed,sqlite3_value ** NotUsed2)15444 static void cdateFunc(
15445   sqlite3_context *context,
15446   int NotUsed,
15447   sqlite3_value **NotUsed2
15448 ){
15449   UNUSED_PARAMETER2(NotUsed, NotUsed2);
15450   dateFunc(context, 0, 0);
15451 }
15452 
15453 /*
15454 ** current_timestamp()
15455 **
15456 ** This function returns the same value as datetime('now').
15457 */
ctimestampFunc(sqlite3_context * context,int NotUsed,sqlite3_value ** NotUsed2)15458 static void ctimestampFunc(
15459   sqlite3_context *context,
15460   int NotUsed,
15461   sqlite3_value **NotUsed2
15462 ){
15463   UNUSED_PARAMETER2(NotUsed, NotUsed2);
15464   datetimeFunc(context, 0, 0);
15465 }
15466 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
15467 
15468 #ifdef SQLITE_OMIT_DATETIME_FUNCS
15469 /*
15470 ** If the library is compiled to omit the full-scale date and time
15471 ** handling (to get a smaller binary), the following minimal version
15472 ** of the functions current_time(), current_date() and current_timestamp()
15473 ** are included instead. This is to support column declarations that
15474 ** include "DEFAULT CURRENT_TIME" etc.
15475 **
15476 ** This function uses the C-library functions time(), gmtime()
15477 ** and strftime(). The format string to pass to strftime() is supplied
15478 ** as the user-data for the function.
15479 */
currentTimeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)15480 static void currentTimeFunc(
15481   sqlite3_context *context,
15482   int argc,
15483   sqlite3_value **argv
15484 ){
15485   time_t t;
15486   char *zFormat = (char *)sqlite3_user_data(context);
15487   sqlite3 *db;
15488   sqlite3_int64 iT;
15489   struct tm *pTm;
15490   struct tm sNow;
15491   char zBuf[20];
15492 
15493   UNUSED_PARAMETER(argc);
15494   UNUSED_PARAMETER(argv);
15495 
15496   iT = sqlite3StmtCurrentTime(context);
15497   if( iT<=0 ) return;
15498   t = iT/1000 - 10000*(sqlite3_int64)21086676;
15499 #ifdef HAVE_GMTIME_R
15500   pTm = gmtime_r(&t, &sNow);
15501 #else
15502   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
15503   pTm = gmtime(&t);
15504   if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
15505   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
15506 #endif
15507   if( pTm ){
15508     strftime(zBuf, 20, zFormat, &sNow);
15509     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15510   }
15511 }
15512 #endif
15513 
15514 /*
15515 ** This function registered all of the above C functions as SQL
15516 ** functions.  This should be the only routine in this file with
15517 ** external linkage.
15518 */
sqlite3RegisterDateTimeFunctions(void)15519 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
15520   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
15521 #ifndef SQLITE_OMIT_DATETIME_FUNCS
15522     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
15523     FUNCTION(date,             -1, 0, 0, dateFunc      ),
15524     FUNCTION(time,             -1, 0, 0, timeFunc      ),
15525     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
15526     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
15527     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
15528     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
15529     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
15530 #else
15531     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
15532     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
15533     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
15534 #endif
15535   };
15536   int i;
15537   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
15538   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
15539 
15540   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
15541     sqlite3FuncDefInsert(pHash, &aFunc[i]);
15542   }
15543 }
15544 
15545 /************** End of date.c ************************************************/
15546 /************** Begin file os.c **********************************************/
15547 /*
15548 ** 2005 November 29
15549 **
15550 ** The author disclaims copyright to this source code.  In place of
15551 ** a legal notice, here is a blessing:
15552 **
15553 **    May you do good and not evil.
15554 **    May you find forgiveness for yourself and forgive others.
15555 **    May you share freely, never taking more than you give.
15556 **
15557 ******************************************************************************
15558 **
15559 ** This file contains OS interface code that is common to all
15560 ** architectures.
15561 */
15562 #define _SQLITE_OS_C_ 1
15563 #undef _SQLITE_OS_C_
15564 
15565 /*
15566 ** The default SQLite sqlite3_vfs implementations do not allocate
15567 ** memory (actually, os_unix.c allocates a small amount of memory
15568 ** from within OsOpen()), but some third-party implementations may.
15569 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
15570 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
15571 **
15572 ** The following functions are instrumented for malloc() failure
15573 ** testing:
15574 **
15575 **     sqlite3OsRead()
15576 **     sqlite3OsWrite()
15577 **     sqlite3OsSync()
15578 **     sqlite3OsFileSize()
15579 **     sqlite3OsLock()
15580 **     sqlite3OsCheckReservedLock()
15581 **     sqlite3OsFileControl()
15582 **     sqlite3OsShmMap()
15583 **     sqlite3OsOpen()
15584 **     sqlite3OsDelete()
15585 **     sqlite3OsAccess()
15586 **     sqlite3OsFullPathname()
15587 **
15588 */
15589 #if defined(SQLITE_TEST)
15590 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
15591   #define DO_OS_MALLOC_TEST(x)                                       \
15592   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
15593     void *pTstAlloc = sqlite3Malloc(10);                             \
15594     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
15595     sqlite3_free(pTstAlloc);                                         \
15596   }
15597 #else
15598   #define DO_OS_MALLOC_TEST(x)
15599 #endif
15600 
15601 /*
15602 ** The following routines are convenience wrappers around methods
15603 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
15604 ** of this would be completely automatic if SQLite were coded using
15605 ** C++ instead of plain old C.
15606 */
sqlite3OsClose(sqlite3_file * pId)15607 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
15608   int rc = SQLITE_OK;
15609   if( pId->pMethods ){
15610     rc = pId->pMethods->xClose(pId);
15611     pId->pMethods = 0;
15612   }
15613   return rc;
15614 }
sqlite3OsRead(sqlite3_file * id,void * pBuf,int amt,i64 offset)15615 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
15616   DO_OS_MALLOC_TEST(id);
15617   return id->pMethods->xRead(id, pBuf, amt, offset);
15618 }
sqlite3OsWrite(sqlite3_file * id,const void * pBuf,int amt,i64 offset)15619 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
15620   DO_OS_MALLOC_TEST(id);
15621   return id->pMethods->xWrite(id, pBuf, amt, offset);
15622 }
sqlite3OsTruncate(sqlite3_file * id,i64 size)15623 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
15624   return id->pMethods->xTruncate(id, size);
15625 }
sqlite3OsSync(sqlite3_file * id,int flags)15626 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
15627   DO_OS_MALLOC_TEST(id);
15628   return id->pMethods->xSync(id, flags);
15629 }
sqlite3OsFileSize(sqlite3_file * id,i64 * pSize)15630 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
15631   DO_OS_MALLOC_TEST(id);
15632   return id->pMethods->xFileSize(id, pSize);
15633 }
sqlite3OsLock(sqlite3_file * id,int lockType)15634 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
15635   DO_OS_MALLOC_TEST(id);
15636   return id->pMethods->xLock(id, lockType);
15637 }
sqlite3OsUnlock(sqlite3_file * id,int lockType)15638 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
15639   return id->pMethods->xUnlock(id, lockType);
15640 }
sqlite3OsCheckReservedLock(sqlite3_file * id,int * pResOut)15641 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
15642   DO_OS_MALLOC_TEST(id);
15643   return id->pMethods->xCheckReservedLock(id, pResOut);
15644 }
15645 
15646 /*
15647 ** Use sqlite3OsFileControl() when we are doing something that might fail
15648 ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
15649 ** when simply tossing information over the wall to the VFS and we do not
15650 ** really care if the VFS receives and understands the information since it
15651 ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
15652 ** routine has no return value since the return value would be meaningless.
15653 */
sqlite3OsFileControl(sqlite3_file * id,int op,void * pArg)15654 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
15655 #ifdef SQLITE_TEST
15656   if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
15657     /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
15658     ** is using a regular VFS, it is called after the corresponding
15659     ** transaction has been committed. Injecting a fault at this point
15660     ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
15661     ** but the transaction is committed anyway.
15662     **
15663     ** The core must call OsFileControl() though, not OsFileControlHint(),
15664     ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
15665     ** means the commit really has failed and an error should be returned
15666     ** to the user.  */
15667     DO_OS_MALLOC_TEST(id);
15668   }
15669 #endif
15670   return id->pMethods->xFileControl(id, op, pArg);
15671 }
sqlite3OsFileControlHint(sqlite3_file * id,int op,void * pArg)15672 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
15673   (void)id->pMethods->xFileControl(id, op, pArg);
15674 }
15675 
sqlite3OsSectorSize(sqlite3_file * id)15676 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
15677   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
15678   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
15679 }
sqlite3OsDeviceCharacteristics(sqlite3_file * id)15680 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
15681   return id->pMethods->xDeviceCharacteristics(id);
15682 }
sqlite3OsShmLock(sqlite3_file * id,int offset,int n,int flags)15683 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
15684   return id->pMethods->xShmLock(id, offset, n, flags);
15685 }
sqlite3OsShmBarrier(sqlite3_file * id)15686 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
15687   id->pMethods->xShmBarrier(id);
15688 }
sqlite3OsShmUnmap(sqlite3_file * id,int deleteFlag)15689 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
15690   return id->pMethods->xShmUnmap(id, deleteFlag);
15691 }
sqlite3OsShmMap(sqlite3_file * id,int iPage,int pgsz,int bExtend,void volatile ** pp)15692 SQLITE_PRIVATE int sqlite3OsShmMap(
15693   sqlite3_file *id,               /* Database file handle */
15694   int iPage,
15695   int pgsz,
15696   int bExtend,                    /* True to extend file if necessary */
15697   void volatile **pp              /* OUT: Pointer to mapping */
15698 ){
15699   DO_OS_MALLOC_TEST(id);
15700   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
15701 }
15702 
15703 #if SQLITE_MAX_MMAP_SIZE>0
15704 /* The real implementation of xFetch and xUnfetch */
sqlite3OsFetch(sqlite3_file * id,i64 iOff,int iAmt,void ** pp)15705 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15706   DO_OS_MALLOC_TEST(id);
15707   return id->pMethods->xFetch(id, iOff, iAmt, pp);
15708 }
sqlite3OsUnfetch(sqlite3_file * id,i64 iOff,void * p)15709 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15710   return id->pMethods->xUnfetch(id, iOff, p);
15711 }
15712 #else
15713 /* No-op stubs to use when memory-mapped I/O is disabled */
sqlite3OsFetch(sqlite3_file * id,i64 iOff,int iAmt,void ** pp)15714 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15715   *pp = 0;
15716   return SQLITE_OK;
15717 }
sqlite3OsUnfetch(sqlite3_file * id,i64 iOff,void * p)15718 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15719   return SQLITE_OK;
15720 }
15721 #endif
15722 
15723 /*
15724 ** The next group of routines are convenience wrappers around the
15725 ** VFS methods.
15726 */
sqlite3OsOpen(sqlite3_vfs * pVfs,const char * zPath,sqlite3_file * pFile,int flags,int * pFlagsOut)15727 SQLITE_PRIVATE int sqlite3OsOpen(
15728   sqlite3_vfs *pVfs,
15729   const char *zPath,
15730   sqlite3_file *pFile,
15731   int flags,
15732   int *pFlagsOut
15733 ){
15734   int rc;
15735   DO_OS_MALLOC_TEST(0);
15736   /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
15737   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
15738   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
15739   ** reaching the VFS. */
15740   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
15741   assert( rc==SQLITE_OK || pFile->pMethods==0 );
15742   return rc;
15743 }
sqlite3OsDelete(sqlite3_vfs * pVfs,const char * zPath,int dirSync)15744 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
15745   DO_OS_MALLOC_TEST(0);
15746   assert( dirSync==0 || dirSync==1 );
15747   return pVfs->xDelete(pVfs, zPath, dirSync);
15748 }
sqlite3OsAccess(sqlite3_vfs * pVfs,const char * zPath,int flags,int * pResOut)15749 SQLITE_PRIVATE int sqlite3OsAccess(
15750   sqlite3_vfs *pVfs,
15751   const char *zPath,
15752   int flags,
15753   int *pResOut
15754 ){
15755   DO_OS_MALLOC_TEST(0);
15756   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
15757 }
sqlite3OsFullPathname(sqlite3_vfs * pVfs,const char * zPath,int nPathOut,char * zPathOut)15758 SQLITE_PRIVATE int sqlite3OsFullPathname(
15759   sqlite3_vfs *pVfs,
15760   const char *zPath,
15761   int nPathOut,
15762   char *zPathOut
15763 ){
15764   DO_OS_MALLOC_TEST(0);
15765   zPathOut[0] = 0;
15766   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
15767 }
15768 #ifndef SQLITE_OMIT_LOAD_EXTENSION
sqlite3OsDlOpen(sqlite3_vfs * pVfs,const char * zPath)15769 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
15770   return pVfs->xDlOpen(pVfs, zPath);
15771 }
sqlite3OsDlError(sqlite3_vfs * pVfs,int nByte,char * zBufOut)15772 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
15773   pVfs->xDlError(pVfs, nByte, zBufOut);
15774 }
sqlite3OsDlSym(sqlite3_vfs * pVfs,void * pHdle,const char * zSym)15775 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
15776   return pVfs->xDlSym(pVfs, pHdle, zSym);
15777 }
sqlite3OsDlClose(sqlite3_vfs * pVfs,void * pHandle)15778 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
15779   pVfs->xDlClose(pVfs, pHandle);
15780 }
15781 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
sqlite3OsRandomness(sqlite3_vfs * pVfs,int nByte,char * zBufOut)15782 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
15783   return pVfs->xRandomness(pVfs, nByte, zBufOut);
15784 }
sqlite3OsSleep(sqlite3_vfs * pVfs,int nMicro)15785 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
15786   return pVfs->xSleep(pVfs, nMicro);
15787 }
sqlite3OsCurrentTimeInt64(sqlite3_vfs * pVfs,sqlite3_int64 * pTimeOut)15788 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
15789   int rc;
15790   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
15791   ** method to get the current date and time if that method is available
15792   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
15793   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
15794   ** unavailable.
15795   */
15796   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
15797     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
15798   }else{
15799     double r;
15800     rc = pVfs->xCurrentTime(pVfs, &r);
15801     *pTimeOut = (sqlite3_int64)(r*86400000.0);
15802   }
15803   return rc;
15804 }
15805 
sqlite3OsOpenMalloc(sqlite3_vfs * pVfs,const char * zFile,sqlite3_file ** ppFile,int flags,int * pOutFlags)15806 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
15807   sqlite3_vfs *pVfs,
15808   const char *zFile,
15809   sqlite3_file **ppFile,
15810   int flags,
15811   int *pOutFlags
15812 ){
15813   int rc = SQLITE_NOMEM;
15814   sqlite3_file *pFile;
15815   pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
15816   if( pFile ){
15817     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
15818     if( rc!=SQLITE_OK ){
15819       sqlite3_free(pFile);
15820     }else{
15821       *ppFile = pFile;
15822     }
15823   }
15824   return rc;
15825 }
sqlite3OsCloseFree(sqlite3_file * pFile)15826 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
15827   int rc = SQLITE_OK;
15828   assert( pFile );
15829   rc = sqlite3OsClose(pFile);
15830   sqlite3_free(pFile);
15831   return rc;
15832 }
15833 
15834 /*
15835 ** This function is a wrapper around the OS specific implementation of
15836 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
15837 ** ability to simulate a malloc failure, so that the handling of an
15838 ** error in sqlite3_os_init() by the upper layers can be tested.
15839 */
sqlite3OsInit(void)15840 SQLITE_PRIVATE int sqlite3OsInit(void){
15841   void *p = sqlite3_malloc(10);
15842   if( p==0 ) return SQLITE_NOMEM;
15843   sqlite3_free(p);
15844   return sqlite3_os_init();
15845 }
15846 
15847 /*
15848 ** The list of all registered VFS implementations.
15849 */
15850 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
15851 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
15852 
15853 /*
15854 ** Locate a VFS by name.  If no name is given, simply return the
15855 ** first VFS on the list.
15856 */
sqlite3_vfs_find(const char * zVfs)15857 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
15858   sqlite3_vfs *pVfs = 0;
15859 #if SQLITE_THREADSAFE
15860   sqlite3_mutex *mutex;
15861 #endif
15862 #ifndef SQLITE_OMIT_AUTOINIT
15863   int rc = sqlite3_initialize();
15864   if( rc ) return 0;
15865 #endif
15866 #if SQLITE_THREADSAFE
15867   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15868 #endif
15869   sqlite3_mutex_enter(mutex);
15870   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
15871     if( zVfs==0 ) break;
15872     if( strcmp(zVfs, pVfs->zName)==0 ) break;
15873   }
15874   sqlite3_mutex_leave(mutex);
15875   return pVfs;
15876 }
15877 
15878 /*
15879 ** Unlink a VFS from the linked list
15880 */
vfsUnlink(sqlite3_vfs * pVfs)15881 static void vfsUnlink(sqlite3_vfs *pVfs){
15882   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
15883   if( pVfs==0 ){
15884     /* No-op */
15885   }else if( vfsList==pVfs ){
15886     vfsList = pVfs->pNext;
15887   }else if( vfsList ){
15888     sqlite3_vfs *p = vfsList;
15889     while( p->pNext && p->pNext!=pVfs ){
15890       p = p->pNext;
15891     }
15892     if( p->pNext==pVfs ){
15893       p->pNext = pVfs->pNext;
15894     }
15895   }
15896 }
15897 
15898 /*
15899 ** Register a VFS with the system.  It is harmless to register the same
15900 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
15901 ** true.
15902 */
sqlite3_vfs_register(sqlite3_vfs * pVfs,int makeDflt)15903 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
15904   MUTEX_LOGIC(sqlite3_mutex *mutex;)
15905 #ifndef SQLITE_OMIT_AUTOINIT
15906   int rc = sqlite3_initialize();
15907   if( rc ) return rc;
15908 #endif
15909   MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
15910   sqlite3_mutex_enter(mutex);
15911   vfsUnlink(pVfs);
15912   if( makeDflt || vfsList==0 ){
15913     pVfs->pNext = vfsList;
15914     vfsList = pVfs;
15915   }else{
15916     pVfs->pNext = vfsList->pNext;
15917     vfsList->pNext = pVfs;
15918   }
15919   assert(vfsList);
15920   sqlite3_mutex_leave(mutex);
15921   return SQLITE_OK;
15922 }
15923 
15924 /*
15925 ** Unregister a VFS so that it is no longer accessible.
15926 */
sqlite3_vfs_unregister(sqlite3_vfs * pVfs)15927 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
15928 #if SQLITE_THREADSAFE
15929   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15930 #endif
15931   sqlite3_mutex_enter(mutex);
15932   vfsUnlink(pVfs);
15933   sqlite3_mutex_leave(mutex);
15934   return SQLITE_OK;
15935 }
15936 
15937 /************** End of os.c **************************************************/
15938 /************** Begin file fault.c *******************************************/
15939 /*
15940 ** 2008 Jan 22
15941 **
15942 ** The author disclaims copyright to this source code.  In place of
15943 ** a legal notice, here is a blessing:
15944 **
15945 **    May you do good and not evil.
15946 **    May you find forgiveness for yourself and forgive others.
15947 **    May you share freely, never taking more than you give.
15948 **
15949 *************************************************************************
15950 **
15951 ** This file contains code to support the concept of "benign"
15952 ** malloc failures (when the xMalloc() or xRealloc() method of the
15953 ** sqlite3_mem_methods structure fails to allocate a block of memory
15954 ** and returns 0).
15955 **
15956 ** Most malloc failures are non-benign. After they occur, SQLite
15957 ** abandons the current operation and returns an error code (usually
15958 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
15959 ** fatal. For example, if a malloc fails while resizing a hash table, this
15960 ** is completely recoverable simply by not carrying out the resize. The
15961 ** hash table will continue to function normally.  So a malloc failure
15962 ** during a hash table resize is a benign fault.
15963 */
15964 
15965 
15966 #ifndef SQLITE_OMIT_BUILTIN_TEST
15967 
15968 /*
15969 ** Global variables.
15970 */
15971 typedef struct BenignMallocHooks BenignMallocHooks;
15972 static SQLITE_WSD struct BenignMallocHooks {
15973   void (*xBenignBegin)(void);
15974   void (*xBenignEnd)(void);
15975 } sqlite3Hooks = { 0, 0 };
15976 
15977 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
15978 ** structure.  If writable static data is unsupported on the target,
15979 ** we have to locate the state vector at run-time.  In the more common
15980 ** case where writable static data is supported, wsdHooks can refer directly
15981 ** to the "sqlite3Hooks" state vector declared above.
15982 */
15983 #ifdef SQLITE_OMIT_WSD
15984 # define wsdHooksInit \
15985   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
15986 # define wsdHooks x[0]
15987 #else
15988 # define wsdHooksInit
15989 # define wsdHooks sqlite3Hooks
15990 #endif
15991 
15992 
15993 /*
15994 ** Register hooks to call when sqlite3BeginBenignMalloc() and
15995 ** sqlite3EndBenignMalloc() are called, respectively.
15996 */
sqlite3BenignMallocHooks(void (* xBenignBegin)(void),void (* xBenignEnd)(void))15997 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
15998   void (*xBenignBegin)(void),
15999   void (*xBenignEnd)(void)
16000 ){
16001   wsdHooksInit;
16002   wsdHooks.xBenignBegin = xBenignBegin;
16003   wsdHooks.xBenignEnd = xBenignEnd;
16004 }
16005 
16006 /*
16007 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
16008 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
16009 ** indicates that subsequent malloc failures are non-benign.
16010 */
sqlite3BeginBenignMalloc(void)16011 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
16012   wsdHooksInit;
16013   if( wsdHooks.xBenignBegin ){
16014     wsdHooks.xBenignBegin();
16015   }
16016 }
sqlite3EndBenignMalloc(void)16017 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
16018   wsdHooksInit;
16019   if( wsdHooks.xBenignEnd ){
16020     wsdHooks.xBenignEnd();
16021   }
16022 }
16023 
16024 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
16025 
16026 /************** End of fault.c ***********************************************/
16027 /************** Begin file mem0.c ********************************************/
16028 /*
16029 ** 2008 October 28
16030 **
16031 ** The author disclaims copyright to this source code.  In place of
16032 ** a legal notice, here is a blessing:
16033 **
16034 **    May you do good and not evil.
16035 **    May you find forgiveness for yourself and forgive others.
16036 **    May you share freely, never taking more than you give.
16037 **
16038 *************************************************************************
16039 **
16040 ** This file contains a no-op memory allocation drivers for use when
16041 ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
16042 ** here always fail.  SQLite will not operate with these drivers.  These
16043 ** are merely placeholders.  Real drivers must be substituted using
16044 ** sqlite3_config() before SQLite will operate.
16045 */
16046 
16047 /*
16048 ** This version of the memory allocator is the default.  It is
16049 ** used when no other memory allocator is specified using compile-time
16050 ** macros.
16051 */
16052 #ifdef SQLITE_ZERO_MALLOC
16053 
16054 /*
16055 ** No-op versions of all memory allocation routines
16056 */
sqlite3MemMalloc(int nByte)16057 static void *sqlite3MemMalloc(int nByte){ return 0; }
sqlite3MemFree(void * pPrior)16058 static void sqlite3MemFree(void *pPrior){ return; }
sqlite3MemRealloc(void * pPrior,int nByte)16059 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
sqlite3MemSize(void * pPrior)16060 static int sqlite3MemSize(void *pPrior){ return 0; }
sqlite3MemRoundup(int n)16061 static int sqlite3MemRoundup(int n){ return n; }
sqlite3MemInit(void * NotUsed)16062 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
sqlite3MemShutdown(void * NotUsed)16063 static void sqlite3MemShutdown(void *NotUsed){ return; }
16064 
16065 /*
16066 ** This routine is the only routine in this file with external linkage.
16067 **
16068 ** Populate the low-level memory allocation function pointers in
16069 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
16070 */
sqlite3MemSetDefault(void)16071 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
16072   static const sqlite3_mem_methods defaultMethods = {
16073      sqlite3MemMalloc,
16074      sqlite3MemFree,
16075      sqlite3MemRealloc,
16076      sqlite3MemSize,
16077      sqlite3MemRoundup,
16078      sqlite3MemInit,
16079      sqlite3MemShutdown,
16080      0
16081   };
16082   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
16083 }
16084 
16085 #endif /* SQLITE_ZERO_MALLOC */
16086 
16087 /************** End of mem0.c ************************************************/
16088 /************** Begin file mem1.c ********************************************/
16089 /*
16090 ** 2007 August 14
16091 **
16092 ** The author disclaims copyright to this source code.  In place of
16093 ** a legal notice, here is a blessing:
16094 **
16095 **    May you do good and not evil.
16096 **    May you find forgiveness for yourself and forgive others.
16097 **    May you share freely, never taking more than you give.
16098 **
16099 *************************************************************************
16100 **
16101 ** This file contains low-level memory allocation drivers for when
16102 ** SQLite will use the standard C-library malloc/realloc/free interface
16103 ** to obtain the memory it needs.
16104 **
16105 ** This file contains implementations of the low-level memory allocation
16106 ** routines specified in the sqlite3_mem_methods object.  The content of
16107 ** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
16108 ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
16109 ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
16110 ** default configuration is to use memory allocation routines in this
16111 ** file.
16112 **
16113 ** C-preprocessor macro summary:
16114 **
16115 **    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
16116 **                                the malloc_usable_size() interface exists
16117 **                                on the target platform.  Or, this symbol
16118 **                                can be set manually, if desired.
16119 **                                If an equivalent interface exists by
16120 **                                a different name, using a separate -D
16121 **                                option to rename it.
16122 **
16123 **    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
16124 **                                memory allocator.  Set this symbol to enable
16125 **                                building on older macs.
16126 **
16127 **    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
16128 **                                _msize() on windows systems.  This might
16129 **                                be necessary when compiling for Delphi,
16130 **                                for example.
16131 */
16132 
16133 /*
16134 ** This version of the memory allocator is the default.  It is
16135 ** used when no other memory allocator is specified using compile-time
16136 ** macros.
16137 */
16138 #ifdef SQLITE_SYSTEM_MALLOC
16139 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
16140 
16141 /*
16142 ** Use the zone allocator available on apple products unless the
16143 ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
16144 */
16145 #include <sys/sysctl.h>
16146 #include <malloc/malloc.h>
16147 #include <libkern/OSAtomic.h>
16148 static malloc_zone_t* _sqliteZone_;
16149 #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
16150 #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
16151 #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
16152 #define SQLITE_MALLOCSIZE(x) \
16153         (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
16154 
16155 #else /* if not __APPLE__ */
16156 
16157 /*
16158 ** Use standard C library malloc and free on non-Apple systems.
16159 ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
16160 */
16161 #define SQLITE_MALLOC(x)             malloc(x)
16162 #define SQLITE_FREE(x)               free(x)
16163 #define SQLITE_REALLOC(x,y)          realloc((x),(y))
16164 
16165 /*
16166 ** The malloc.h header file is needed for malloc_usable_size() function
16167 ** on some systems (e.g. Linux).
16168 */
16169 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE)
16170 #  define SQLITE_USE_MALLOC_H
16171 #  define SQLITE_USE_MALLOC_USABLE_SIZE
16172 /*
16173 ** The MSVCRT has malloc_usable_size(), but it is called _msize().  The
16174 ** use of _msize() is automatic, but can be disabled by compiling with
16175 ** -DSQLITE_WITHOUT_MSIZE.  Using the _msize() function also requires
16176 ** the malloc.h header file.
16177 */
16178 #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
16179 #  define SQLITE_USE_MALLOC_H
16180 #  define SQLITE_USE_MSIZE
16181 #endif
16182 
16183 /*
16184 ** Include the malloc.h header file, if necessary.  Also set define macro
16185 ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
16186 ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
16187 ** The memory size function can always be overridden manually by defining
16188 ** the macro SQLITE_MALLOCSIZE to the desired function name.
16189 */
16190 #if defined(SQLITE_USE_MALLOC_H)
16191 #  include <malloc.h>
16192 #  if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
16193 #    if !defined(SQLITE_MALLOCSIZE)
16194 #      define SQLITE_MALLOCSIZE(x)   malloc_usable_size(x)
16195 #    endif
16196 #  elif defined(SQLITE_USE_MSIZE)
16197 #    if !defined(SQLITE_MALLOCSIZE)
16198 #      define SQLITE_MALLOCSIZE      _msize
16199 #    endif
16200 #  endif
16201 #endif /* defined(SQLITE_USE_MALLOC_H) */
16202 
16203 #endif /* __APPLE__ or not __APPLE__ */
16204 
16205 /*
16206 ** Like malloc(), but remember the size of the allocation
16207 ** so that we can find it later using sqlite3MemSize().
16208 **
16209 ** For this low-level routine, we are guaranteed that nByte>0 because
16210 ** cases of nByte<=0 will be intercepted and dealt with by higher level
16211 ** routines.
16212 */
sqlite3MemMalloc(int nByte)16213 static void *sqlite3MemMalloc(int nByte){
16214 #ifdef SQLITE_MALLOCSIZE
16215   void *p = SQLITE_MALLOC( nByte );
16216   if( p==0 ){
16217     testcase( sqlite3GlobalConfig.xLog!=0 );
16218     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
16219   }
16220   return p;
16221 #else
16222   sqlite3_int64 *p;
16223   assert( nByte>0 );
16224   nByte = ROUND8(nByte);
16225   p = SQLITE_MALLOC( nByte+8 );
16226   if( p ){
16227     p[0] = nByte;
16228     p++;
16229   }else{
16230     testcase( sqlite3GlobalConfig.xLog!=0 );
16231     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
16232   }
16233   return (void *)p;
16234 #endif
16235 }
16236 
16237 /*
16238 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
16239 ** or sqlite3MemRealloc().
16240 **
16241 ** For this low-level routine, we already know that pPrior!=0 since
16242 ** cases where pPrior==0 will have been intecepted and dealt with
16243 ** by higher-level routines.
16244 */
sqlite3MemFree(void * pPrior)16245 static void sqlite3MemFree(void *pPrior){
16246 #ifdef SQLITE_MALLOCSIZE
16247   SQLITE_FREE(pPrior);
16248 #else
16249   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
16250   assert( pPrior!=0 );
16251   p--;
16252   SQLITE_FREE(p);
16253 #endif
16254 }
16255 
16256 /*
16257 ** Report the allocated size of a prior return from xMalloc()
16258 ** or xRealloc().
16259 */
sqlite3MemSize(void * pPrior)16260 static int sqlite3MemSize(void *pPrior){
16261 #ifdef SQLITE_MALLOCSIZE
16262   return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
16263 #else
16264   sqlite3_int64 *p;
16265   if( pPrior==0 ) return 0;
16266   p = (sqlite3_int64*)pPrior;
16267   p--;
16268   return (int)p[0];
16269 #endif
16270 }
16271 
16272 /*
16273 ** Like realloc().  Resize an allocation previously obtained from
16274 ** sqlite3MemMalloc().
16275 **
16276 ** For this low-level interface, we know that pPrior!=0.  Cases where
16277 ** pPrior==0 while have been intercepted by higher-level routine and
16278 ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
16279 ** cases where nByte<=0 will have been intercepted by higher-level
16280 ** routines and redirected to xFree.
16281 */
sqlite3MemRealloc(void * pPrior,int nByte)16282 static void *sqlite3MemRealloc(void *pPrior, int nByte){
16283 #ifdef SQLITE_MALLOCSIZE
16284   void *p = SQLITE_REALLOC(pPrior, nByte);
16285   if( p==0 ){
16286     testcase( sqlite3GlobalConfig.xLog!=0 );
16287     sqlite3_log(SQLITE_NOMEM,
16288       "failed memory resize %u to %u bytes",
16289       SQLITE_MALLOCSIZE(pPrior), nByte);
16290   }
16291   return p;
16292 #else
16293   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
16294   assert( pPrior!=0 && nByte>0 );
16295   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
16296   p--;
16297   p = SQLITE_REALLOC(p, nByte+8 );
16298   if( p ){
16299     p[0] = nByte;
16300     p++;
16301   }else{
16302     testcase( sqlite3GlobalConfig.xLog!=0 );
16303     sqlite3_log(SQLITE_NOMEM,
16304       "failed memory resize %u to %u bytes",
16305       sqlite3MemSize(pPrior), nByte);
16306   }
16307   return (void*)p;
16308 #endif
16309 }
16310 
16311 /*
16312 ** Round up a request size to the next valid allocation size.
16313 */
sqlite3MemRoundup(int n)16314 static int sqlite3MemRoundup(int n){
16315   return ROUND8(n);
16316 }
16317 
16318 /*
16319 ** Initialize this module.
16320 */
sqlite3MemInit(void * NotUsed)16321 static int sqlite3MemInit(void *NotUsed){
16322 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
16323   int cpuCount;
16324   size_t len;
16325   if( _sqliteZone_ ){
16326     return SQLITE_OK;
16327   }
16328   len = sizeof(cpuCount);
16329   /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
16330   sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
16331   if( cpuCount>1 ){
16332     /* defer MT decisions to system malloc */
16333     _sqliteZone_ = malloc_default_zone();
16334   }else{
16335     /* only 1 core, use our own zone to contention over global locks,
16336     ** e.g. we have our own dedicated locks */
16337     bool success;
16338     malloc_zone_t* newzone = malloc_create_zone(4096, 0);
16339     malloc_set_zone_name(newzone, "Sqlite_Heap");
16340     do{
16341       success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
16342                                  (void * volatile *)&_sqliteZone_);
16343     }while(!_sqliteZone_);
16344     if( !success ){
16345       /* somebody registered a zone first */
16346       malloc_destroy_zone(newzone);
16347     }
16348   }
16349 #endif
16350   UNUSED_PARAMETER(NotUsed);
16351   return SQLITE_OK;
16352 }
16353 
16354 /*
16355 ** Deinitialize this module.
16356 */
sqlite3MemShutdown(void * NotUsed)16357 static void sqlite3MemShutdown(void *NotUsed){
16358   UNUSED_PARAMETER(NotUsed);
16359   return;
16360 }
16361 
16362 /*
16363 ** This routine is the only routine in this file with external linkage.
16364 **
16365 ** Populate the low-level memory allocation function pointers in
16366 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
16367 */
sqlite3MemSetDefault(void)16368 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
16369   static const sqlite3_mem_methods defaultMethods = {
16370      sqlite3MemMalloc,
16371      sqlite3MemFree,
16372      sqlite3MemRealloc,
16373      sqlite3MemSize,
16374      sqlite3MemRoundup,
16375      sqlite3MemInit,
16376      sqlite3MemShutdown,
16377      0
16378   };
16379   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
16380 }
16381 
16382 #endif /* SQLITE_SYSTEM_MALLOC */
16383 
16384 /************** End of mem1.c ************************************************/
16385 /************** Begin file mem2.c ********************************************/
16386 /*
16387 ** 2007 August 15
16388 **
16389 ** The author disclaims copyright to this source code.  In place of
16390 ** a legal notice, here is a blessing:
16391 **
16392 **    May you do good and not evil.
16393 **    May you find forgiveness for yourself and forgive others.
16394 **    May you share freely, never taking more than you give.
16395 **
16396 *************************************************************************
16397 **
16398 ** This file contains low-level memory allocation drivers for when
16399 ** SQLite will use the standard C-library malloc/realloc/free interface
16400 ** to obtain the memory it needs while adding lots of additional debugging
16401 ** information to each allocation in order to help detect and fix memory
16402 ** leaks and memory usage errors.
16403 **
16404 ** This file contains implementations of the low-level memory allocation
16405 ** routines specified in the sqlite3_mem_methods object.
16406 */
16407 
16408 /*
16409 ** This version of the memory allocator is used only if the
16410 ** SQLITE_MEMDEBUG macro is defined
16411 */
16412 #ifdef SQLITE_MEMDEBUG
16413 
16414 /*
16415 ** The backtrace functionality is only available with GLIBC
16416 */
16417 #ifdef __GLIBC__
16418   extern int backtrace(void**,int);
16419   extern void backtrace_symbols_fd(void*const*,int,int);
16420 #else
16421 # define backtrace(A,B) 1
16422 # define backtrace_symbols_fd(A,B,C)
16423 #endif
16424 /* #include <stdio.h> */
16425 
16426 /*
16427 ** Each memory allocation looks like this:
16428 **
16429 **  ------------------------------------------------------------------------
16430 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
16431 **  ------------------------------------------------------------------------
16432 **
16433 ** The application code sees only a pointer to the allocation.  We have
16434 ** to back up from the allocation pointer to find the MemBlockHdr.  The
16435 ** MemBlockHdr tells us the size of the allocation and the number of
16436 ** backtrace pointers.  There is also a guard word at the end of the
16437 ** MemBlockHdr.
16438 */
16439 struct MemBlockHdr {
16440   i64 iSize;                          /* Size of this allocation */
16441   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
16442   char nBacktrace;                    /* Number of backtraces on this alloc */
16443   char nBacktraceSlots;               /* Available backtrace slots */
16444   u8 nTitle;                          /* Bytes of title; includes '\0' */
16445   u8 eType;                           /* Allocation type code */
16446   int iForeGuard;                     /* Guard word for sanity */
16447 };
16448 
16449 /*
16450 ** Guard words
16451 */
16452 #define FOREGUARD 0x80F5E153
16453 #define REARGUARD 0xE4676B53
16454 
16455 /*
16456 ** Number of malloc size increments to track.
16457 */
16458 #define NCSIZE  1000
16459 
16460 /*
16461 ** All of the static variables used by this module are collected
16462 ** into a single structure named "mem".  This is to keep the
16463 ** static variables organized and to reduce namespace pollution
16464 ** when this module is combined with other in the amalgamation.
16465 */
16466 static struct {
16467 
16468   /*
16469   ** Mutex to control access to the memory allocation subsystem.
16470   */
16471   sqlite3_mutex *mutex;
16472 
16473   /*
16474   ** Head and tail of a linked list of all outstanding allocations
16475   */
16476   struct MemBlockHdr *pFirst;
16477   struct MemBlockHdr *pLast;
16478 
16479   /*
16480   ** The number of levels of backtrace to save in new allocations.
16481   */
16482   int nBacktrace;
16483   void (*xBacktrace)(int, int, void **);
16484 
16485   /*
16486   ** Title text to insert in front of each block
16487   */
16488   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
16489   char zTitle[100];  /* The title text */
16490 
16491   /*
16492   ** sqlite3MallocDisallow() increments the following counter.
16493   ** sqlite3MallocAllow() decrements it.
16494   */
16495   int disallow; /* Do not allow memory allocation */
16496 
16497   /*
16498   ** Gather statistics on the sizes of memory allocations.
16499   ** nAlloc[i] is the number of allocation attempts of i*8
16500   ** bytes.  i==NCSIZE is the number of allocation attempts for
16501   ** sizes more than NCSIZE*8 bytes.
16502   */
16503   int nAlloc[NCSIZE];      /* Total number of allocations */
16504   int nCurrent[NCSIZE];    /* Current number of allocations */
16505   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
16506 
16507 } mem;
16508 
16509 
16510 /*
16511 ** Adjust memory usage statistics
16512 */
adjustStats(int iSize,int increment)16513 static void adjustStats(int iSize, int increment){
16514   int i = ROUND8(iSize)/8;
16515   if( i>NCSIZE-1 ){
16516     i = NCSIZE - 1;
16517   }
16518   if( increment>0 ){
16519     mem.nAlloc[i]++;
16520     mem.nCurrent[i]++;
16521     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
16522       mem.mxCurrent[i] = mem.nCurrent[i];
16523     }
16524   }else{
16525     mem.nCurrent[i]--;
16526     assert( mem.nCurrent[i]>=0 );
16527   }
16528 }
16529 
16530 /*
16531 ** Given an allocation, find the MemBlockHdr for that allocation.
16532 **
16533 ** This routine checks the guards at either end of the allocation and
16534 ** if they are incorrect it asserts.
16535 */
sqlite3MemsysGetHeader(void * pAllocation)16536 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
16537   struct MemBlockHdr *p;
16538   int *pInt;
16539   u8 *pU8;
16540   int nReserve;
16541 
16542   p = (struct MemBlockHdr*)pAllocation;
16543   p--;
16544   assert( p->iForeGuard==(int)FOREGUARD );
16545   nReserve = ROUND8(p->iSize);
16546   pInt = (int*)pAllocation;
16547   pU8 = (u8*)pAllocation;
16548   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
16549   /* This checks any of the "extra" bytes allocated due
16550   ** to rounding up to an 8 byte boundary to ensure
16551   ** they haven't been overwritten.
16552   */
16553   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
16554   return p;
16555 }
16556 
16557 /*
16558 ** Return the number of bytes currently allocated at address p.
16559 */
sqlite3MemSize(void * p)16560 static int sqlite3MemSize(void *p){
16561   struct MemBlockHdr *pHdr;
16562   if( !p ){
16563     return 0;
16564   }
16565   pHdr = sqlite3MemsysGetHeader(p);
16566   return (int)pHdr->iSize;
16567 }
16568 
16569 /*
16570 ** Initialize the memory allocation subsystem.
16571 */
sqlite3MemInit(void * NotUsed)16572 static int sqlite3MemInit(void *NotUsed){
16573   UNUSED_PARAMETER(NotUsed);
16574   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
16575   if( !sqlite3GlobalConfig.bMemstat ){
16576     /* If memory status is enabled, then the malloc.c wrapper will already
16577     ** hold the STATIC_MEM mutex when the routines here are invoked. */
16578     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
16579   }
16580   return SQLITE_OK;
16581 }
16582 
16583 /*
16584 ** Deinitialize the memory allocation subsystem.
16585 */
sqlite3MemShutdown(void * NotUsed)16586 static void sqlite3MemShutdown(void *NotUsed){
16587   UNUSED_PARAMETER(NotUsed);
16588   mem.mutex = 0;
16589 }
16590 
16591 /*
16592 ** Round up a request size to the next valid allocation size.
16593 */
sqlite3MemRoundup(int n)16594 static int sqlite3MemRoundup(int n){
16595   return ROUND8(n);
16596 }
16597 
16598 /*
16599 ** Fill a buffer with pseudo-random bytes.  This is used to preset
16600 ** the content of a new memory allocation to unpredictable values and
16601 ** to clear the content of a freed allocation to unpredictable values.
16602 */
randomFill(char * pBuf,int nByte)16603 static void randomFill(char *pBuf, int nByte){
16604   unsigned int x, y, r;
16605   x = SQLITE_PTR_TO_INT(pBuf);
16606   y = nByte | 1;
16607   while( nByte >= 4 ){
16608     x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16609     y = y*1103515245 + 12345;
16610     r = x ^ y;
16611     *(int*)pBuf = r;
16612     pBuf += 4;
16613     nByte -= 4;
16614   }
16615   while( nByte-- > 0 ){
16616     x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16617     y = y*1103515245 + 12345;
16618     r = x ^ y;
16619     *(pBuf++) = r & 0xff;
16620   }
16621 }
16622 
16623 /*
16624 ** Allocate nByte bytes of memory.
16625 */
sqlite3MemMalloc(int nByte)16626 static void *sqlite3MemMalloc(int nByte){
16627   struct MemBlockHdr *pHdr;
16628   void **pBt;
16629   char *z;
16630   int *pInt;
16631   void *p = 0;
16632   int totalSize;
16633   int nReserve;
16634   sqlite3_mutex_enter(mem.mutex);
16635   assert( mem.disallow==0 );
16636   nReserve = ROUND8(nByte);
16637   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
16638                mem.nBacktrace*sizeof(void*) + mem.nTitle;
16639   p = malloc(totalSize);
16640   if( p ){
16641     z = p;
16642     pBt = (void**)&z[mem.nTitle];
16643     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
16644     pHdr->pNext = 0;
16645     pHdr->pPrev = mem.pLast;
16646     if( mem.pLast ){
16647       mem.pLast->pNext = pHdr;
16648     }else{
16649       mem.pFirst = pHdr;
16650     }
16651     mem.pLast = pHdr;
16652     pHdr->iForeGuard = FOREGUARD;
16653     pHdr->eType = MEMTYPE_HEAP;
16654     pHdr->nBacktraceSlots = mem.nBacktrace;
16655     pHdr->nTitle = mem.nTitle;
16656     if( mem.nBacktrace ){
16657       void *aAddr[40];
16658       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
16659       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
16660       assert(pBt[0]);
16661       if( mem.xBacktrace ){
16662         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
16663       }
16664     }else{
16665       pHdr->nBacktrace = 0;
16666     }
16667     if( mem.nTitle ){
16668       memcpy(z, mem.zTitle, mem.nTitle);
16669     }
16670     pHdr->iSize = nByte;
16671     adjustStats(nByte, +1);
16672     pInt = (int*)&pHdr[1];
16673     pInt[nReserve/sizeof(int)] = REARGUARD;
16674     randomFill((char*)pInt, nByte);
16675     memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
16676     p = (void*)pInt;
16677   }
16678   sqlite3_mutex_leave(mem.mutex);
16679   return p;
16680 }
16681 
16682 /*
16683 ** Free memory.
16684 */
sqlite3MemFree(void * pPrior)16685 static void sqlite3MemFree(void *pPrior){
16686   struct MemBlockHdr *pHdr;
16687   void **pBt;
16688   char *z;
16689   assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
16690        || mem.mutex!=0 );
16691   pHdr = sqlite3MemsysGetHeader(pPrior);
16692   pBt = (void**)pHdr;
16693   pBt -= pHdr->nBacktraceSlots;
16694   sqlite3_mutex_enter(mem.mutex);
16695   if( pHdr->pPrev ){
16696     assert( pHdr->pPrev->pNext==pHdr );
16697     pHdr->pPrev->pNext = pHdr->pNext;
16698   }else{
16699     assert( mem.pFirst==pHdr );
16700     mem.pFirst = pHdr->pNext;
16701   }
16702   if( pHdr->pNext ){
16703     assert( pHdr->pNext->pPrev==pHdr );
16704     pHdr->pNext->pPrev = pHdr->pPrev;
16705   }else{
16706     assert( mem.pLast==pHdr );
16707     mem.pLast = pHdr->pPrev;
16708   }
16709   z = (char*)pBt;
16710   z -= pHdr->nTitle;
16711   adjustStats((int)pHdr->iSize, -1);
16712   randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16713                 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
16714   free(z);
16715   sqlite3_mutex_leave(mem.mutex);
16716 }
16717 
16718 /*
16719 ** Change the size of an existing memory allocation.
16720 **
16721 ** For this debugging implementation, we *always* make a copy of the
16722 ** allocation into a new place in memory.  In this way, if the
16723 ** higher level code is using pointer to the old allocation, it is
16724 ** much more likely to break and we are much more liking to find
16725 ** the error.
16726 */
sqlite3MemRealloc(void * pPrior,int nByte)16727 static void *sqlite3MemRealloc(void *pPrior, int nByte){
16728   struct MemBlockHdr *pOldHdr;
16729   void *pNew;
16730   assert( mem.disallow==0 );
16731   assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
16732   pOldHdr = sqlite3MemsysGetHeader(pPrior);
16733   pNew = sqlite3MemMalloc(nByte);
16734   if( pNew ){
16735     memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
16736     if( nByte>pOldHdr->iSize ){
16737       randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
16738     }
16739     sqlite3MemFree(pPrior);
16740   }
16741   return pNew;
16742 }
16743 
16744 /*
16745 ** Populate the low-level memory allocation function pointers in
16746 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
16747 */
sqlite3MemSetDefault(void)16748 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
16749   static const sqlite3_mem_methods defaultMethods = {
16750      sqlite3MemMalloc,
16751      sqlite3MemFree,
16752      sqlite3MemRealloc,
16753      sqlite3MemSize,
16754      sqlite3MemRoundup,
16755      sqlite3MemInit,
16756      sqlite3MemShutdown,
16757      0
16758   };
16759   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
16760 }
16761 
16762 /*
16763 ** Set the "type" of an allocation.
16764 */
sqlite3MemdebugSetType(void * p,u8 eType)16765 SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
16766   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
16767     struct MemBlockHdr *pHdr;
16768     pHdr = sqlite3MemsysGetHeader(p);
16769     assert( pHdr->iForeGuard==FOREGUARD );
16770     pHdr->eType = eType;
16771   }
16772 }
16773 
16774 /*
16775 ** Return TRUE if the mask of type in eType matches the type of the
16776 ** allocation p.  Also return true if p==NULL.
16777 **
16778 ** This routine is designed for use within an assert() statement, to
16779 ** verify the type of an allocation.  For example:
16780 **
16781 **     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
16782 */
sqlite3MemdebugHasType(void * p,u8 eType)16783 SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
16784   int rc = 1;
16785   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
16786     struct MemBlockHdr *pHdr;
16787     pHdr = sqlite3MemsysGetHeader(p);
16788     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
16789     if( (pHdr->eType&eType)==0 ){
16790       rc = 0;
16791     }
16792   }
16793   return rc;
16794 }
16795 
16796 /*
16797 ** Return TRUE if the mask of type in eType matches no bits of the type of the
16798 ** allocation p.  Also return true if p==NULL.
16799 **
16800 ** This routine is designed for use within an assert() statement, to
16801 ** verify the type of an allocation.  For example:
16802 **
16803 **     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
16804 */
sqlite3MemdebugNoType(void * p,u8 eType)16805 SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
16806   int rc = 1;
16807   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
16808     struct MemBlockHdr *pHdr;
16809     pHdr = sqlite3MemsysGetHeader(p);
16810     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
16811     if( (pHdr->eType&eType)!=0 ){
16812       rc = 0;
16813     }
16814   }
16815   return rc;
16816 }
16817 
16818 /*
16819 ** Set the number of backtrace levels kept for each allocation.
16820 ** A value of zero turns off backtracing.  The number is always rounded
16821 ** up to a multiple of 2.
16822 */
sqlite3MemdebugBacktrace(int depth)16823 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
16824   if( depth<0 ){ depth = 0; }
16825   if( depth>20 ){ depth = 20; }
16826   depth = (depth+1)&0xfe;
16827   mem.nBacktrace = depth;
16828 }
16829 
sqlite3MemdebugBacktraceCallback(void (* xBacktrace)(int,int,void **))16830 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
16831   mem.xBacktrace = xBacktrace;
16832 }
16833 
16834 /*
16835 ** Set the title string for subsequent allocations.
16836 */
sqlite3MemdebugSettitle(const char * zTitle)16837 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
16838   unsigned int n = sqlite3Strlen30(zTitle) + 1;
16839   sqlite3_mutex_enter(mem.mutex);
16840   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
16841   memcpy(mem.zTitle, zTitle, n);
16842   mem.zTitle[n] = 0;
16843   mem.nTitle = ROUND8(n);
16844   sqlite3_mutex_leave(mem.mutex);
16845 }
16846 
sqlite3MemdebugSync()16847 SQLITE_PRIVATE void sqlite3MemdebugSync(){
16848   struct MemBlockHdr *pHdr;
16849   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16850     void **pBt = (void**)pHdr;
16851     pBt -= pHdr->nBacktraceSlots;
16852     mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16853   }
16854 }
16855 
16856 /*
16857 ** Open the file indicated and write a log of all unfreed memory
16858 ** allocations into that log.
16859 */
sqlite3MemdebugDump(const char * zFilename)16860 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
16861   FILE *out;
16862   struct MemBlockHdr *pHdr;
16863   void **pBt;
16864   int i;
16865   out = fopen(zFilename, "w");
16866   if( out==0 ){
16867     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
16868                     zFilename);
16869     return;
16870   }
16871   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16872     char *z = (char*)pHdr;
16873     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
16874     fprintf(out, "**** %lld bytes at %p from %s ****\n",
16875             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
16876     if( pHdr->nBacktrace ){
16877       fflush(out);
16878       pBt = (void**)pHdr;
16879       pBt -= pHdr->nBacktraceSlots;
16880       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
16881       fprintf(out, "\n");
16882     }
16883   }
16884   fprintf(out, "COUNTS:\n");
16885   for(i=0; i<NCSIZE-1; i++){
16886     if( mem.nAlloc[i] ){
16887       fprintf(out, "   %5d: %10d %10d %10d\n",
16888             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
16889     }
16890   }
16891   if( mem.nAlloc[NCSIZE-1] ){
16892     fprintf(out, "   %5d: %10d %10d %10d\n",
16893              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
16894              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
16895   }
16896   fclose(out);
16897 }
16898 
16899 /*
16900 ** Return the number of times sqlite3MemMalloc() has been called.
16901 */
sqlite3MemdebugMallocCount()16902 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
16903   int i;
16904   int nTotal = 0;
16905   for(i=0; i<NCSIZE; i++){
16906     nTotal += mem.nAlloc[i];
16907   }
16908   return nTotal;
16909 }
16910 
16911 
16912 #endif /* SQLITE_MEMDEBUG */
16913 
16914 /************** End of mem2.c ************************************************/
16915 /************** Begin file mem3.c ********************************************/
16916 /*
16917 ** 2007 October 14
16918 **
16919 ** The author disclaims copyright to this source code.  In place of
16920 ** a legal notice, here is a blessing:
16921 **
16922 **    May you do good and not evil.
16923 **    May you find forgiveness for yourself and forgive others.
16924 **    May you share freely, never taking more than you give.
16925 **
16926 *************************************************************************
16927 ** This file contains the C functions that implement a memory
16928 ** allocation subsystem for use by SQLite.
16929 **
16930 ** This version of the memory allocation subsystem omits all
16931 ** use of malloc(). The SQLite user supplies a block of memory
16932 ** before calling sqlite3_initialize() from which allocations
16933 ** are made and returned by the xMalloc() and xRealloc()
16934 ** implementations. Once sqlite3_initialize() has been called,
16935 ** the amount of memory available to SQLite is fixed and cannot
16936 ** be changed.
16937 **
16938 ** This version of the memory allocation subsystem is included
16939 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
16940 */
16941 
16942 /*
16943 ** This version of the memory allocator is only built into the library
16944 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
16945 ** mean that the library will use a memory-pool by default, just that
16946 ** it is available. The mempool allocator is activated by calling
16947 ** sqlite3_config().
16948 */
16949 #ifdef SQLITE_ENABLE_MEMSYS3
16950 
16951 /*
16952 ** Maximum size (in Mem3Blocks) of a "small" chunk.
16953 */
16954 #define MX_SMALL 10
16955 
16956 
16957 /*
16958 ** Number of freelist hash slots
16959 */
16960 #define N_HASH  61
16961 
16962 /*
16963 ** A memory allocation (also called a "chunk") consists of two or
16964 ** more blocks where each block is 8 bytes.  The first 8 bytes are
16965 ** a header that is not returned to the user.
16966 **
16967 ** A chunk is two or more blocks that is either checked out or
16968 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
16969 ** size of the allocation in blocks if the allocation is free.
16970 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
16971 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
16972 ** is true if the previous chunk is checked out and false if the
16973 ** previous chunk is free.  The u.hdr.prevSize field is the size of
16974 ** the previous chunk in blocks if the previous chunk is on the
16975 ** freelist. If the previous chunk is checked out, then
16976 ** u.hdr.prevSize can be part of the data for that chunk and should
16977 ** not be read or written.
16978 **
16979 ** We often identify a chunk by its index in mem3.aPool[].  When
16980 ** this is done, the chunk index refers to the second block of
16981 ** the chunk.  In this way, the first chunk has an index of 1.
16982 ** A chunk index of 0 means "no such chunk" and is the equivalent
16983 ** of a NULL pointer.
16984 **
16985 ** The second block of free chunks is of the form u.list.  The
16986 ** two fields form a double-linked list of chunks of related sizes.
16987 ** Pointers to the head of the list are stored in mem3.aiSmall[]
16988 ** for smaller chunks and mem3.aiHash[] for larger chunks.
16989 **
16990 ** The second block of a chunk is user data if the chunk is checked
16991 ** out.  If a chunk is checked out, the user data may extend into
16992 ** the u.hdr.prevSize value of the following chunk.
16993 */
16994 typedef struct Mem3Block Mem3Block;
16995 struct Mem3Block {
16996   union {
16997     struct {
16998       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
16999       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
17000     } hdr;
17001     struct {
17002       u32 next;       /* Index in mem3.aPool[] of next free chunk */
17003       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
17004     } list;
17005   } u;
17006 };
17007 
17008 /*
17009 ** All of the static variables used by this module are collected
17010 ** into a single structure named "mem3".  This is to keep the
17011 ** static variables organized and to reduce namespace pollution
17012 ** when this module is combined with other in the amalgamation.
17013 */
17014 static SQLITE_WSD struct Mem3Global {
17015   /*
17016   ** Memory available for allocation. nPool is the size of the array
17017   ** (in Mem3Blocks) pointed to by aPool less 2.
17018   */
17019   u32 nPool;
17020   Mem3Block *aPool;
17021 
17022   /*
17023   ** True if we are evaluating an out-of-memory callback.
17024   */
17025   int alarmBusy;
17026 
17027   /*
17028   ** Mutex to control access to the memory allocation subsystem.
17029   */
17030   sqlite3_mutex *mutex;
17031 
17032   /*
17033   ** The minimum amount of free space that we have seen.
17034   */
17035   u32 mnMaster;
17036 
17037   /*
17038   ** iMaster is the index of the master chunk.  Most new allocations
17039   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
17040   ** of the current master.  iMaster is 0 if there is not master chunk.
17041   ** The master chunk is not in either the aiHash[] or aiSmall[].
17042   */
17043   u32 iMaster;
17044   u32 szMaster;
17045 
17046   /*
17047   ** Array of lists of free blocks according to the block size
17048   ** for smaller chunks, or a hash on the block size for larger
17049   ** chunks.
17050   */
17051   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
17052   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
17053 } mem3 = { 97535575 };
17054 
17055 #define mem3 GLOBAL(struct Mem3Global, mem3)
17056 
17057 /*
17058 ** Unlink the chunk at mem3.aPool[i] from list it is currently
17059 ** on.  *pRoot is the list that i is a member of.
17060 */
memsys3UnlinkFromList(u32 i,u32 * pRoot)17061 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
17062   u32 next = mem3.aPool[i].u.list.next;
17063   u32 prev = mem3.aPool[i].u.list.prev;
17064   assert( sqlite3_mutex_held(mem3.mutex) );
17065   if( prev==0 ){
17066     *pRoot = next;
17067   }else{
17068     mem3.aPool[prev].u.list.next = next;
17069   }
17070   if( next ){
17071     mem3.aPool[next].u.list.prev = prev;
17072   }
17073   mem3.aPool[i].u.list.next = 0;
17074   mem3.aPool[i].u.list.prev = 0;
17075 }
17076 
17077 /*
17078 ** Unlink the chunk at index i from
17079 ** whatever list is currently a member of.
17080 */
memsys3Unlink(u32 i)17081 static void memsys3Unlink(u32 i){
17082   u32 size, hash;
17083   assert( sqlite3_mutex_held(mem3.mutex) );
17084   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
17085   assert( i>=1 );
17086   size = mem3.aPool[i-1].u.hdr.size4x/4;
17087   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
17088   assert( size>=2 );
17089   if( size <= MX_SMALL ){
17090     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
17091   }else{
17092     hash = size % N_HASH;
17093     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
17094   }
17095 }
17096 
17097 /*
17098 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
17099 ** at *pRoot.
17100 */
memsys3LinkIntoList(u32 i,u32 * pRoot)17101 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
17102   assert( sqlite3_mutex_held(mem3.mutex) );
17103   mem3.aPool[i].u.list.next = *pRoot;
17104   mem3.aPool[i].u.list.prev = 0;
17105   if( *pRoot ){
17106     mem3.aPool[*pRoot].u.list.prev = i;
17107   }
17108   *pRoot = i;
17109 }
17110 
17111 /*
17112 ** Link the chunk at index i into either the appropriate
17113 ** small chunk list, or into the large chunk hash table.
17114 */
memsys3Link(u32 i)17115 static void memsys3Link(u32 i){
17116   u32 size, hash;
17117   assert( sqlite3_mutex_held(mem3.mutex) );
17118   assert( i>=1 );
17119   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
17120   size = mem3.aPool[i-1].u.hdr.size4x/4;
17121   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
17122   assert( size>=2 );
17123   if( size <= MX_SMALL ){
17124     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
17125   }else{
17126     hash = size % N_HASH;
17127     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
17128   }
17129 }
17130 
17131 /*
17132 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
17133 ** will already be held (obtained by code in malloc.c) if
17134 ** sqlite3GlobalConfig.bMemStat is true.
17135 */
memsys3Enter(void)17136 static void memsys3Enter(void){
17137   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
17138     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
17139   }
17140   sqlite3_mutex_enter(mem3.mutex);
17141 }
memsys3Leave(void)17142 static void memsys3Leave(void){
17143   sqlite3_mutex_leave(mem3.mutex);
17144 }
17145 
17146 /*
17147 ** Called when we are unable to satisfy an allocation of nBytes.
17148 */
memsys3OutOfMemory(int nByte)17149 static void memsys3OutOfMemory(int nByte){
17150   if( !mem3.alarmBusy ){
17151     mem3.alarmBusy = 1;
17152     assert( sqlite3_mutex_held(mem3.mutex) );
17153     sqlite3_mutex_leave(mem3.mutex);
17154     sqlite3_release_memory(nByte);
17155     sqlite3_mutex_enter(mem3.mutex);
17156     mem3.alarmBusy = 0;
17157   }
17158 }
17159 
17160 
17161 /*
17162 ** Chunk i is a free chunk that has been unlinked.  Adjust its
17163 ** size parameters for check-out and return a pointer to the
17164 ** user portion of the chunk.
17165 */
memsys3Checkout(u32 i,u32 nBlock)17166 static void *memsys3Checkout(u32 i, u32 nBlock){
17167   u32 x;
17168   assert( sqlite3_mutex_held(mem3.mutex) );
17169   assert( i>=1 );
17170   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
17171   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
17172   x = mem3.aPool[i-1].u.hdr.size4x;
17173   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
17174   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
17175   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
17176   return &mem3.aPool[i];
17177 }
17178 
17179 /*
17180 ** Carve a piece off of the end of the mem3.iMaster free chunk.
17181 ** Return a pointer to the new allocation.  Or, if the master chunk
17182 ** is not large enough, return 0.
17183 */
memsys3FromMaster(u32 nBlock)17184 static void *memsys3FromMaster(u32 nBlock){
17185   assert( sqlite3_mutex_held(mem3.mutex) );
17186   assert( mem3.szMaster>=nBlock );
17187   if( nBlock>=mem3.szMaster-1 ){
17188     /* Use the entire master */
17189     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
17190     mem3.iMaster = 0;
17191     mem3.szMaster = 0;
17192     mem3.mnMaster = 0;
17193     return p;
17194   }else{
17195     /* Split the master block.  Return the tail. */
17196     u32 newi, x;
17197     newi = mem3.iMaster + mem3.szMaster - nBlock;
17198     assert( newi > mem3.iMaster+1 );
17199     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
17200     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
17201     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
17202     mem3.szMaster -= nBlock;
17203     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
17204     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
17205     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
17206     if( mem3.szMaster < mem3.mnMaster ){
17207       mem3.mnMaster = mem3.szMaster;
17208     }
17209     return (void*)&mem3.aPool[newi];
17210   }
17211 }
17212 
17213 /*
17214 ** *pRoot is the head of a list of free chunks of the same size
17215 ** or same size hash.  In other words, *pRoot is an entry in either
17216 ** mem3.aiSmall[] or mem3.aiHash[].
17217 **
17218 ** This routine examines all entries on the given list and tries
17219 ** to coalesce each entries with adjacent free chunks.
17220 **
17221 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
17222 ** the current mem3.iMaster with the new larger chunk.  In order for
17223 ** this mem3.iMaster replacement to work, the master chunk must be
17224 ** linked into the hash tables.  That is not the normal state of
17225 ** affairs, of course.  The calling routine must link the master
17226 ** chunk before invoking this routine, then must unlink the (possibly
17227 ** changed) master chunk once this routine has finished.
17228 */
memsys3Merge(u32 * pRoot)17229 static void memsys3Merge(u32 *pRoot){
17230   u32 iNext, prev, size, i, x;
17231 
17232   assert( sqlite3_mutex_held(mem3.mutex) );
17233   for(i=*pRoot; i>0; i=iNext){
17234     iNext = mem3.aPool[i].u.list.next;
17235     size = mem3.aPool[i-1].u.hdr.size4x;
17236     assert( (size&1)==0 );
17237     if( (size&2)==0 ){
17238       memsys3UnlinkFromList(i, pRoot);
17239       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
17240       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
17241       if( prev==iNext ){
17242         iNext = mem3.aPool[prev].u.list.next;
17243       }
17244       memsys3Unlink(prev);
17245       size = i + size/4 - prev;
17246       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
17247       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
17248       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
17249       memsys3Link(prev);
17250       i = prev;
17251     }else{
17252       size /= 4;
17253     }
17254     if( size>mem3.szMaster ){
17255       mem3.iMaster = i;
17256       mem3.szMaster = size;
17257     }
17258   }
17259 }
17260 
17261 /*
17262 ** Return a block of memory of at least nBytes in size.
17263 ** Return NULL if unable.
17264 **
17265 ** This function assumes that the necessary mutexes, if any, are
17266 ** already held by the caller. Hence "Unsafe".
17267 */
memsys3MallocUnsafe(int nByte)17268 static void *memsys3MallocUnsafe(int nByte){
17269   u32 i;
17270   u32 nBlock;
17271   u32 toFree;
17272 
17273   assert( sqlite3_mutex_held(mem3.mutex) );
17274   assert( sizeof(Mem3Block)==8 );
17275   if( nByte<=12 ){
17276     nBlock = 2;
17277   }else{
17278     nBlock = (nByte + 11)/8;
17279   }
17280   assert( nBlock>=2 );
17281 
17282   /* STEP 1:
17283   ** Look for an entry of the correct size in either the small
17284   ** chunk table or in the large chunk hash table.  This is
17285   ** successful most of the time (about 9 times out of 10).
17286   */
17287   if( nBlock <= MX_SMALL ){
17288     i = mem3.aiSmall[nBlock-2];
17289     if( i>0 ){
17290       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
17291       return memsys3Checkout(i, nBlock);
17292     }
17293   }else{
17294     int hash = nBlock % N_HASH;
17295     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
17296       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
17297         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
17298         return memsys3Checkout(i, nBlock);
17299       }
17300     }
17301   }
17302 
17303   /* STEP 2:
17304   ** Try to satisfy the allocation by carving a piece off of the end
17305   ** of the master chunk.  This step usually works if step 1 fails.
17306   */
17307   if( mem3.szMaster>=nBlock ){
17308     return memsys3FromMaster(nBlock);
17309   }
17310 
17311 
17312   /* STEP 3:
17313   ** Loop through the entire memory pool.  Coalesce adjacent free
17314   ** chunks.  Recompute the master chunk as the largest free chunk.
17315   ** Then try again to satisfy the allocation by carving a piece off
17316   ** of the end of the master chunk.  This step happens very
17317   ** rarely (we hope!)
17318   */
17319   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
17320     memsys3OutOfMemory(toFree);
17321     if( mem3.iMaster ){
17322       memsys3Link(mem3.iMaster);
17323       mem3.iMaster = 0;
17324       mem3.szMaster = 0;
17325     }
17326     for(i=0; i<N_HASH; i++){
17327       memsys3Merge(&mem3.aiHash[i]);
17328     }
17329     for(i=0; i<MX_SMALL-1; i++){
17330       memsys3Merge(&mem3.aiSmall[i]);
17331     }
17332     if( mem3.szMaster ){
17333       memsys3Unlink(mem3.iMaster);
17334       if( mem3.szMaster>=nBlock ){
17335         return memsys3FromMaster(nBlock);
17336       }
17337     }
17338   }
17339 
17340   /* If none of the above worked, then we fail. */
17341   return 0;
17342 }
17343 
17344 /*
17345 ** Free an outstanding memory allocation.
17346 **
17347 ** This function assumes that the necessary mutexes, if any, are
17348 ** already held by the caller. Hence "Unsafe".
17349 */
memsys3FreeUnsafe(void * pOld)17350 static void memsys3FreeUnsafe(void *pOld){
17351   Mem3Block *p = (Mem3Block*)pOld;
17352   int i;
17353   u32 size, x;
17354   assert( sqlite3_mutex_held(mem3.mutex) );
17355   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
17356   i = p - mem3.aPool;
17357   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
17358   size = mem3.aPool[i-1].u.hdr.size4x/4;
17359   assert( i+size<=mem3.nPool+1 );
17360   mem3.aPool[i-1].u.hdr.size4x &= ~1;
17361   mem3.aPool[i+size-1].u.hdr.prevSize = size;
17362   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
17363   memsys3Link(i);
17364 
17365   /* Try to expand the master using the newly freed chunk */
17366   if( mem3.iMaster ){
17367     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
17368       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
17369       mem3.iMaster -= size;
17370       mem3.szMaster += size;
17371       memsys3Unlink(mem3.iMaster);
17372       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
17373       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
17374       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
17375     }
17376     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
17377     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
17378       memsys3Unlink(mem3.iMaster+mem3.szMaster);
17379       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
17380       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
17381       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
17382     }
17383   }
17384 }
17385 
17386 /*
17387 ** Return the size of an outstanding allocation, in bytes.  The
17388 ** size returned omits the 8-byte header overhead.  This only
17389 ** works for chunks that are currently checked out.
17390 */
memsys3Size(void * p)17391 static int memsys3Size(void *p){
17392   Mem3Block *pBlock;
17393   if( p==0 ) return 0;
17394   pBlock = (Mem3Block*)p;
17395   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
17396   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
17397 }
17398 
17399 /*
17400 ** Round up a request size to the next valid allocation size.
17401 */
memsys3Roundup(int n)17402 static int memsys3Roundup(int n){
17403   if( n<=12 ){
17404     return 12;
17405   }else{
17406     return ((n+11)&~7) - 4;
17407   }
17408 }
17409 
17410 /*
17411 ** Allocate nBytes of memory.
17412 */
memsys3Malloc(int nBytes)17413 static void *memsys3Malloc(int nBytes){
17414   sqlite3_int64 *p;
17415   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
17416   memsys3Enter();
17417   p = memsys3MallocUnsafe(nBytes);
17418   memsys3Leave();
17419   return (void*)p;
17420 }
17421 
17422 /*
17423 ** Free memory.
17424 */
memsys3Free(void * pPrior)17425 static void memsys3Free(void *pPrior){
17426   assert( pPrior );
17427   memsys3Enter();
17428   memsys3FreeUnsafe(pPrior);
17429   memsys3Leave();
17430 }
17431 
17432 /*
17433 ** Change the size of an existing memory allocation
17434 */
memsys3Realloc(void * pPrior,int nBytes)17435 static void *memsys3Realloc(void *pPrior, int nBytes){
17436   int nOld;
17437   void *p;
17438   if( pPrior==0 ){
17439     return sqlite3_malloc(nBytes);
17440   }
17441   if( nBytes<=0 ){
17442     sqlite3_free(pPrior);
17443     return 0;
17444   }
17445   nOld = memsys3Size(pPrior);
17446   if( nBytes<=nOld && nBytes>=nOld-128 ){
17447     return pPrior;
17448   }
17449   memsys3Enter();
17450   p = memsys3MallocUnsafe(nBytes);
17451   if( p ){
17452     if( nOld<nBytes ){
17453       memcpy(p, pPrior, nOld);
17454     }else{
17455       memcpy(p, pPrior, nBytes);
17456     }
17457     memsys3FreeUnsafe(pPrior);
17458   }
17459   memsys3Leave();
17460   return p;
17461 }
17462 
17463 /*
17464 ** Initialize this module.
17465 */
memsys3Init(void * NotUsed)17466 static int memsys3Init(void *NotUsed){
17467   UNUSED_PARAMETER(NotUsed);
17468   if( !sqlite3GlobalConfig.pHeap ){
17469     return SQLITE_ERROR;
17470   }
17471 
17472   /* Store a pointer to the memory block in global structure mem3. */
17473   assert( sizeof(Mem3Block)==8 );
17474   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
17475   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
17476 
17477   /* Initialize the master block. */
17478   mem3.szMaster = mem3.nPool;
17479   mem3.mnMaster = mem3.szMaster;
17480   mem3.iMaster = 1;
17481   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
17482   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
17483   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
17484 
17485   return SQLITE_OK;
17486 }
17487 
17488 /*
17489 ** Deinitialize this module.
17490 */
memsys3Shutdown(void * NotUsed)17491 static void memsys3Shutdown(void *NotUsed){
17492   UNUSED_PARAMETER(NotUsed);
17493   mem3.mutex = 0;
17494   return;
17495 }
17496 
17497 
17498 
17499 /*
17500 ** Open the file indicated and write a log of all unfreed memory
17501 ** allocations into that log.
17502 */
sqlite3Memsys3Dump(const char * zFilename)17503 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
17504 #ifdef SQLITE_DEBUG
17505   FILE *out;
17506   u32 i, j;
17507   u32 size;
17508   if( zFilename==0 || zFilename[0]==0 ){
17509     out = stdout;
17510   }else{
17511     out = fopen(zFilename, "w");
17512     if( out==0 ){
17513       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
17514                       zFilename);
17515       return;
17516     }
17517   }
17518   memsys3Enter();
17519   fprintf(out, "CHUNKS:\n");
17520   for(i=1; i<=mem3.nPool; i+=size/4){
17521     size = mem3.aPool[i-1].u.hdr.size4x;
17522     if( size/4<=1 ){
17523       fprintf(out, "%p size error\n", &mem3.aPool[i]);
17524       assert( 0 );
17525       break;
17526     }
17527     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
17528       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
17529       assert( 0 );
17530       break;
17531     }
17532     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
17533       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
17534       assert( 0 );
17535       break;
17536     }
17537     if( size&1 ){
17538       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
17539     }else{
17540       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
17541                   i==mem3.iMaster ? " **master**" : "");
17542     }
17543   }
17544   for(i=0; i<MX_SMALL-1; i++){
17545     if( mem3.aiSmall[i]==0 ) continue;
17546     fprintf(out, "small(%2d):", i);
17547     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
17548       fprintf(out, " %p(%d)", &mem3.aPool[j],
17549               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
17550     }
17551     fprintf(out, "\n");
17552   }
17553   for(i=0; i<N_HASH; i++){
17554     if( mem3.aiHash[i]==0 ) continue;
17555     fprintf(out, "hash(%2d):", i);
17556     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
17557       fprintf(out, " %p(%d)", &mem3.aPool[j],
17558               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
17559     }
17560     fprintf(out, "\n");
17561   }
17562   fprintf(out, "master=%d\n", mem3.iMaster);
17563   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
17564   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
17565   sqlite3_mutex_leave(mem3.mutex);
17566   if( out==stdout ){
17567     fflush(stdout);
17568   }else{
17569     fclose(out);
17570   }
17571 #else
17572   UNUSED_PARAMETER(zFilename);
17573 #endif
17574 }
17575 
17576 /*
17577 ** This routine is the only routine in this file with external
17578 ** linkage.
17579 **
17580 ** Populate the low-level memory allocation function pointers in
17581 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
17582 ** arguments specify the block of memory to manage.
17583 **
17584 ** This routine is only called by sqlite3_config(), and therefore
17585 ** is not required to be threadsafe (it is not).
17586 */
sqlite3MemGetMemsys3(void)17587 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
17588   static const sqlite3_mem_methods mempoolMethods = {
17589      memsys3Malloc,
17590      memsys3Free,
17591      memsys3Realloc,
17592      memsys3Size,
17593      memsys3Roundup,
17594      memsys3Init,
17595      memsys3Shutdown,
17596      0
17597   };
17598   return &mempoolMethods;
17599 }
17600 
17601 #endif /* SQLITE_ENABLE_MEMSYS3 */
17602 
17603 /************** End of mem3.c ************************************************/
17604 /************** Begin file mem5.c ********************************************/
17605 /*
17606 ** 2007 October 14
17607 **
17608 ** The author disclaims copyright to this source code.  In place of
17609 ** a legal notice, here is a blessing:
17610 **
17611 **    May you do good and not evil.
17612 **    May you find forgiveness for yourself and forgive others.
17613 **    May you share freely, never taking more than you give.
17614 **
17615 *************************************************************************
17616 ** This file contains the C functions that implement a memory
17617 ** allocation subsystem for use by SQLite.
17618 **
17619 ** This version of the memory allocation subsystem omits all
17620 ** use of malloc(). The application gives SQLite a block of memory
17621 ** before calling sqlite3_initialize() from which allocations
17622 ** are made and returned by the xMalloc() and xRealloc()
17623 ** implementations. Once sqlite3_initialize() has been called,
17624 ** the amount of memory available to SQLite is fixed and cannot
17625 ** be changed.
17626 **
17627 ** This version of the memory allocation subsystem is included
17628 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
17629 **
17630 ** This memory allocator uses the following algorithm:
17631 **
17632 **   1.  All memory allocations sizes are rounded up to a power of 2.
17633 **
17634 **   2.  If two adjacent free blocks are the halves of a larger block,
17635 **       then the two blocks are coalesed into the single larger block.
17636 **
17637 **   3.  New memory is allocated from the first available free block.
17638 **
17639 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
17640 ** Concerning Dynamic Storage Allocation". Journal of the Association for
17641 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
17642 **
17643 ** Let n be the size of the largest allocation divided by the minimum
17644 ** allocation size (after rounding all sizes up to a power of 2.)  Let M
17645 ** be the maximum amount of memory ever outstanding at one time.  Let
17646 ** N be the total amount of memory available for allocation.  Robson
17647 ** proved that this memory allocator will never breakdown due to
17648 ** fragmentation as long as the following constraint holds:
17649 **
17650 **      N >=  M*(1 + log2(n)/2) - n + 1
17651 **
17652 ** The sqlite3_status() logic tracks the maximum values of n and M so
17653 ** that an application can, at any time, verify this constraint.
17654 */
17655 
17656 /*
17657 ** This version of the memory allocator is used only when
17658 ** SQLITE_ENABLE_MEMSYS5 is defined.
17659 */
17660 #ifdef SQLITE_ENABLE_MEMSYS5
17661 
17662 /*
17663 ** A minimum allocation is an instance of the following structure.
17664 ** Larger allocations are an array of these structures where the
17665 ** size of the array is a power of 2.
17666 **
17667 ** The size of this object must be a power of two.  That fact is
17668 ** verified in memsys5Init().
17669 */
17670 typedef struct Mem5Link Mem5Link;
17671 struct Mem5Link {
17672   int next;       /* Index of next free chunk */
17673   int prev;       /* Index of previous free chunk */
17674 };
17675 
17676 /*
17677 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
17678 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
17679 ** it is not actually possible to reach this limit.
17680 */
17681 #define LOGMAX 30
17682 
17683 /*
17684 ** Masks used for mem5.aCtrl[] elements.
17685 */
17686 #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
17687 #define CTRL_FREE     0x20    /* True if not checked out */
17688 
17689 /*
17690 ** All of the static variables used by this module are collected
17691 ** into a single structure named "mem5".  This is to keep the
17692 ** static variables organized and to reduce namespace pollution
17693 ** when this module is combined with other in the amalgamation.
17694 */
17695 static SQLITE_WSD struct Mem5Global {
17696   /*
17697   ** Memory available for allocation
17698   */
17699   int szAtom;      /* Smallest possible allocation in bytes */
17700   int nBlock;      /* Number of szAtom sized blocks in zPool */
17701   u8 *zPool;       /* Memory available to be allocated */
17702 
17703   /*
17704   ** Mutex to control access to the memory allocation subsystem.
17705   */
17706   sqlite3_mutex *mutex;
17707 
17708   /*
17709   ** Performance statistics
17710   */
17711   u64 nAlloc;         /* Total number of calls to malloc */
17712   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
17713   u64 totalExcess;    /* Total internal fragmentation */
17714   u32 currentOut;     /* Current checkout, including internal fragmentation */
17715   u32 currentCount;   /* Current number of distinct checkouts */
17716   u32 maxOut;         /* Maximum instantaneous currentOut */
17717   u32 maxCount;       /* Maximum instantaneous currentCount */
17718   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
17719 
17720   /*
17721   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
17722   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
17723   ** and so forth.
17724   */
17725   int aiFreelist[LOGMAX+1];
17726 
17727   /*
17728   ** Space for tracking which blocks are checked out and the size
17729   ** of each block.  One byte per block.
17730   */
17731   u8 *aCtrl;
17732 
17733 } mem5;
17734 
17735 /*
17736 ** Access the static variable through a macro for SQLITE_OMIT_WSD.
17737 */
17738 #define mem5 GLOBAL(struct Mem5Global, mem5)
17739 
17740 /*
17741 ** Assuming mem5.zPool is divided up into an array of Mem5Link
17742 ** structures, return a pointer to the idx-th such link.
17743 */
17744 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
17745 
17746 /*
17747 ** Unlink the chunk at mem5.aPool[i] from list it is currently
17748 ** on.  It should be found on mem5.aiFreelist[iLogsize].
17749 */
memsys5Unlink(int i,int iLogsize)17750 static void memsys5Unlink(int i, int iLogsize){
17751   int next, prev;
17752   assert( i>=0 && i<mem5.nBlock );
17753   assert( iLogsize>=0 && iLogsize<=LOGMAX );
17754   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
17755 
17756   next = MEM5LINK(i)->next;
17757   prev = MEM5LINK(i)->prev;
17758   if( prev<0 ){
17759     mem5.aiFreelist[iLogsize] = next;
17760   }else{
17761     MEM5LINK(prev)->next = next;
17762   }
17763   if( next>=0 ){
17764     MEM5LINK(next)->prev = prev;
17765   }
17766 }
17767 
17768 /*
17769 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
17770 ** free list.
17771 */
memsys5Link(int i,int iLogsize)17772 static void memsys5Link(int i, int iLogsize){
17773   int x;
17774   assert( sqlite3_mutex_held(mem5.mutex) );
17775   assert( i>=0 && i<mem5.nBlock );
17776   assert( iLogsize>=0 && iLogsize<=LOGMAX );
17777   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
17778 
17779   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
17780   MEM5LINK(i)->prev = -1;
17781   if( x>=0 ){
17782     assert( x<mem5.nBlock );
17783     MEM5LINK(x)->prev = i;
17784   }
17785   mem5.aiFreelist[iLogsize] = i;
17786 }
17787 
17788 /*
17789 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
17790 ** will already be held (obtained by code in malloc.c) if
17791 ** sqlite3GlobalConfig.bMemStat is true.
17792 */
memsys5Enter(void)17793 static void memsys5Enter(void){
17794   sqlite3_mutex_enter(mem5.mutex);
17795 }
memsys5Leave(void)17796 static void memsys5Leave(void){
17797   sqlite3_mutex_leave(mem5.mutex);
17798 }
17799 
17800 /*
17801 ** Return the size of an outstanding allocation, in bytes.  The
17802 ** size returned omits the 8-byte header overhead.  This only
17803 ** works for chunks that are currently checked out.
17804 */
memsys5Size(void * p)17805 static int memsys5Size(void *p){
17806   int iSize = 0;
17807   if( p ){
17808     int i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
17809     assert( i>=0 && i<mem5.nBlock );
17810     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
17811   }
17812   return iSize;
17813 }
17814 
17815 /*
17816 ** Return a block of memory of at least nBytes in size.
17817 ** Return NULL if unable.  Return NULL if nBytes==0.
17818 **
17819 ** The caller guarantees that nByte is positive.
17820 **
17821 ** The caller has obtained a mutex prior to invoking this
17822 ** routine so there is never any chance that two or more
17823 ** threads can be in this routine at the same time.
17824 */
memsys5MallocUnsafe(int nByte)17825 static void *memsys5MallocUnsafe(int nByte){
17826   int i;           /* Index of a mem5.aPool[] slot */
17827   int iBin;        /* Index into mem5.aiFreelist[] */
17828   int iFullSz;     /* Size of allocation rounded up to power of 2 */
17829   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
17830 
17831   /* nByte must be a positive */
17832   assert( nByte>0 );
17833 
17834   /* Keep track of the maximum allocation request.  Even unfulfilled
17835   ** requests are counted */
17836   if( (u32)nByte>mem5.maxRequest ){
17837     mem5.maxRequest = nByte;
17838   }
17839 
17840   /* Abort if the requested allocation size is larger than the largest
17841   ** power of two that we can represent using 32-bit signed integers.
17842   */
17843   if( nByte > 0x40000000 ){
17844     return 0;
17845   }
17846 
17847   /* Round nByte up to the next valid power of two */
17848   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
17849 
17850   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
17851   ** block.  If not, then split a block of the next larger power of
17852   ** two in order to create a new free block of size iLogsize.
17853   */
17854   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
17855   if( iBin>LOGMAX ){
17856     testcase( sqlite3GlobalConfig.xLog!=0 );
17857     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
17858     return 0;
17859   }
17860   i = mem5.aiFreelist[iBin];
17861   memsys5Unlink(i, iBin);
17862   while( iBin>iLogsize ){
17863     int newSize;
17864 
17865     iBin--;
17866     newSize = 1 << iBin;
17867     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
17868     memsys5Link(i+newSize, iBin);
17869   }
17870   mem5.aCtrl[i] = iLogsize;
17871 
17872   /* Update allocator performance statistics. */
17873   mem5.nAlloc++;
17874   mem5.totalAlloc += iFullSz;
17875   mem5.totalExcess += iFullSz - nByte;
17876   mem5.currentCount++;
17877   mem5.currentOut += iFullSz;
17878   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
17879   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
17880 
17881 #ifdef SQLITE_DEBUG
17882   /* Make sure the allocated memory does not assume that it is set to zero
17883   ** or retains a value from a previous allocation */
17884   memset(&mem5.zPool[i*mem5.szAtom], 0xAA, iFullSz);
17885 #endif
17886 
17887   /* Return a pointer to the allocated memory. */
17888   return (void*)&mem5.zPool[i*mem5.szAtom];
17889 }
17890 
17891 /*
17892 ** Free an outstanding memory allocation.
17893 */
memsys5FreeUnsafe(void * pOld)17894 static void memsys5FreeUnsafe(void *pOld){
17895   u32 size, iLogsize;
17896   int iBlock;
17897 
17898   /* Set iBlock to the index of the block pointed to by pOld in
17899   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
17900   */
17901   iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
17902 
17903   /* Check that the pointer pOld points to a valid, non-free block. */
17904   assert( iBlock>=0 && iBlock<mem5.nBlock );
17905   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
17906   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
17907 
17908   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
17909   size = 1<<iLogsize;
17910   assert( iBlock+size-1<(u32)mem5.nBlock );
17911 
17912   mem5.aCtrl[iBlock] |= CTRL_FREE;
17913   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
17914   assert( mem5.currentCount>0 );
17915   assert( mem5.currentOut>=(size*mem5.szAtom) );
17916   mem5.currentCount--;
17917   mem5.currentOut -= size*mem5.szAtom;
17918   assert( mem5.currentOut>0 || mem5.currentCount==0 );
17919   assert( mem5.currentCount>0 || mem5.currentOut==0 );
17920 
17921   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
17922   while( ALWAYS(iLogsize<LOGMAX) ){
17923     int iBuddy;
17924     if( (iBlock>>iLogsize) & 1 ){
17925       iBuddy = iBlock - size;
17926     }else{
17927       iBuddy = iBlock + size;
17928     }
17929     assert( iBuddy>=0 );
17930     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
17931     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
17932     memsys5Unlink(iBuddy, iLogsize);
17933     iLogsize++;
17934     if( iBuddy<iBlock ){
17935       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
17936       mem5.aCtrl[iBlock] = 0;
17937       iBlock = iBuddy;
17938     }else{
17939       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
17940       mem5.aCtrl[iBuddy] = 0;
17941     }
17942     size *= 2;
17943   }
17944 
17945 #ifdef SQLITE_DEBUG
17946   /* Overwrite freed memory with the 0x55 bit pattern to verify that it is
17947   ** not used after being freed */
17948   memset(&mem5.zPool[iBlock*mem5.szAtom], 0x55, size);
17949 #endif
17950 
17951   memsys5Link(iBlock, iLogsize);
17952 }
17953 
17954 /*
17955 ** Allocate nBytes of memory.
17956 */
memsys5Malloc(int nBytes)17957 static void *memsys5Malloc(int nBytes){
17958   sqlite3_int64 *p = 0;
17959   if( nBytes>0 ){
17960     memsys5Enter();
17961     p = memsys5MallocUnsafe(nBytes);
17962     memsys5Leave();
17963   }
17964   return (void*)p;
17965 }
17966 
17967 /*
17968 ** Free memory.
17969 **
17970 ** The outer layer memory allocator prevents this routine from
17971 ** being called with pPrior==0.
17972 */
memsys5Free(void * pPrior)17973 static void memsys5Free(void *pPrior){
17974   assert( pPrior!=0 );
17975   memsys5Enter();
17976   memsys5FreeUnsafe(pPrior);
17977   memsys5Leave();
17978 }
17979 
17980 /*
17981 ** Change the size of an existing memory allocation.
17982 **
17983 ** The outer layer memory allocator prevents this routine from
17984 ** being called with pPrior==0.
17985 **
17986 ** nBytes is always a value obtained from a prior call to
17987 ** memsys5Round().  Hence nBytes is always a non-negative power
17988 ** of two.  If nBytes==0 that means that an oversize allocation
17989 ** (an allocation larger than 0x40000000) was requested and this
17990 ** routine should return 0 without freeing pPrior.
17991 */
memsys5Realloc(void * pPrior,int nBytes)17992 static void *memsys5Realloc(void *pPrior, int nBytes){
17993   int nOld;
17994   void *p;
17995   assert( pPrior!=0 );
17996   assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
17997   assert( nBytes>=0 );
17998   if( nBytes==0 ){
17999     return 0;
18000   }
18001   nOld = memsys5Size(pPrior);
18002   if( nBytes<=nOld ){
18003     return pPrior;
18004   }
18005   memsys5Enter();
18006   p = memsys5MallocUnsafe(nBytes);
18007   if( p ){
18008     memcpy(p, pPrior, nOld);
18009     memsys5FreeUnsafe(pPrior);
18010   }
18011   memsys5Leave();
18012   return p;
18013 }
18014 
18015 /*
18016 ** Round up a request size to the next valid allocation size.  If
18017 ** the allocation is too large to be handled by this allocation system,
18018 ** return 0.
18019 **
18020 ** All allocations must be a power of two and must be expressed by a
18021 ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
18022 ** or 1073741824 bytes.
18023 */
memsys5Roundup(int n)18024 static int memsys5Roundup(int n){
18025   int iFullSz;
18026   if( n > 0x40000000 ) return 0;
18027   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
18028   return iFullSz;
18029 }
18030 
18031 /*
18032 ** Return the ceiling of the logarithm base 2 of iValue.
18033 **
18034 ** Examples:   memsys5Log(1) -> 0
18035 **             memsys5Log(2) -> 1
18036 **             memsys5Log(4) -> 2
18037 **             memsys5Log(5) -> 3
18038 **             memsys5Log(8) -> 3
18039 **             memsys5Log(9) -> 4
18040 */
memsys5Log(int iValue)18041 static int memsys5Log(int iValue){
18042   int iLog;
18043   for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
18044   return iLog;
18045 }
18046 
18047 /*
18048 ** Initialize the memory allocator.
18049 **
18050 ** This routine is not threadsafe.  The caller must be holding a mutex
18051 ** to prevent multiple threads from entering at the same time.
18052 */
memsys5Init(void * NotUsed)18053 static int memsys5Init(void *NotUsed){
18054   int ii;            /* Loop counter */
18055   int nByte;         /* Number of bytes of memory available to this allocator */
18056   u8 *zByte;         /* Memory usable by this allocator */
18057   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
18058   int iOffset;       /* An offset into mem5.aCtrl[] */
18059 
18060   UNUSED_PARAMETER(NotUsed);
18061 
18062   /* For the purposes of this routine, disable the mutex */
18063   mem5.mutex = 0;
18064 
18065   /* The size of a Mem5Link object must be a power of two.  Verify that
18066   ** this is case.
18067   */
18068   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
18069 
18070   nByte = sqlite3GlobalConfig.nHeap;
18071   zByte = (u8*)sqlite3GlobalConfig.pHeap;
18072   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
18073 
18074   /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
18075   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
18076   mem5.szAtom = (1<<nMinLog);
18077   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
18078     mem5.szAtom = mem5.szAtom << 1;
18079   }
18080 
18081   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
18082   mem5.zPool = zByte;
18083   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
18084 
18085   for(ii=0; ii<=LOGMAX; ii++){
18086     mem5.aiFreelist[ii] = -1;
18087   }
18088 
18089   iOffset = 0;
18090   for(ii=LOGMAX; ii>=0; ii--){
18091     int nAlloc = (1<<ii);
18092     if( (iOffset+nAlloc)<=mem5.nBlock ){
18093       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
18094       memsys5Link(iOffset, ii);
18095       iOffset += nAlloc;
18096     }
18097     assert((iOffset+nAlloc)>mem5.nBlock);
18098   }
18099 
18100   /* If a mutex is required for normal operation, allocate one */
18101   if( sqlite3GlobalConfig.bMemstat==0 ){
18102     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
18103   }
18104 
18105   return SQLITE_OK;
18106 }
18107 
18108 /*
18109 ** Deinitialize this module.
18110 */
memsys5Shutdown(void * NotUsed)18111 static void memsys5Shutdown(void *NotUsed){
18112   UNUSED_PARAMETER(NotUsed);
18113   mem5.mutex = 0;
18114   return;
18115 }
18116 
18117 #ifdef SQLITE_TEST
18118 /*
18119 ** Open the file indicated and write a log of all unfreed memory
18120 ** allocations into that log.
18121 */
sqlite3Memsys5Dump(const char * zFilename)18122 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
18123   FILE *out;
18124   int i, j, n;
18125   int nMinLog;
18126 
18127   if( zFilename==0 || zFilename[0]==0 ){
18128     out = stdout;
18129   }else{
18130     out = fopen(zFilename, "w");
18131     if( out==0 ){
18132       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
18133                       zFilename);
18134       return;
18135     }
18136   }
18137   memsys5Enter();
18138   nMinLog = memsys5Log(mem5.szAtom);
18139   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
18140     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
18141     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
18142   }
18143   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
18144   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
18145   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
18146   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
18147   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
18148   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
18149   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
18150   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
18151   memsys5Leave();
18152   if( out==stdout ){
18153     fflush(stdout);
18154   }else{
18155     fclose(out);
18156   }
18157 }
18158 #endif
18159 
18160 /*
18161 ** This routine is the only routine in this file with external
18162 ** linkage. It returns a pointer to a static sqlite3_mem_methods
18163 ** struct populated with the memsys5 methods.
18164 */
sqlite3MemGetMemsys5(void)18165 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
18166   static const sqlite3_mem_methods memsys5Methods = {
18167      memsys5Malloc,
18168      memsys5Free,
18169      memsys5Realloc,
18170      memsys5Size,
18171      memsys5Roundup,
18172      memsys5Init,
18173      memsys5Shutdown,
18174      0
18175   };
18176   return &memsys5Methods;
18177 }
18178 
18179 #endif /* SQLITE_ENABLE_MEMSYS5 */
18180 
18181 /************** End of mem5.c ************************************************/
18182 /************** Begin file mutex.c *******************************************/
18183 /*
18184 ** 2007 August 14
18185 **
18186 ** The author disclaims copyright to this source code.  In place of
18187 ** a legal notice, here is a blessing:
18188 **
18189 **    May you do good and not evil.
18190 **    May you find forgiveness for yourself and forgive others.
18191 **    May you share freely, never taking more than you give.
18192 **
18193 *************************************************************************
18194 ** This file contains the C functions that implement mutexes.
18195 **
18196 ** This file contains code that is common across all mutex implementations.
18197 */
18198 
18199 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
18200 /*
18201 ** For debugging purposes, record when the mutex subsystem is initialized
18202 ** and uninitialized so that we can assert() if there is an attempt to
18203 ** allocate a mutex while the system is uninitialized.
18204 */
18205 static SQLITE_WSD int mutexIsInit = 0;
18206 #endif /* SQLITE_DEBUG */
18207 
18208 
18209 #ifndef SQLITE_MUTEX_OMIT
18210 /*
18211 ** Initialize the mutex system.
18212 */
sqlite3MutexInit(void)18213 SQLITE_PRIVATE int sqlite3MutexInit(void){
18214   int rc = SQLITE_OK;
18215   if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
18216     /* If the xMutexAlloc method has not been set, then the user did not
18217     ** install a mutex implementation via sqlite3_config() prior to
18218     ** sqlite3_initialize() being called. This block copies pointers to
18219     ** the default implementation into the sqlite3GlobalConfig structure.
18220     */
18221     sqlite3_mutex_methods const *pFrom;
18222     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
18223 
18224     if( sqlite3GlobalConfig.bCoreMutex ){
18225       pFrom = sqlite3DefaultMutex();
18226     }else{
18227       pFrom = sqlite3NoopMutex();
18228     }
18229     memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
18230     memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
18231            sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
18232     pTo->xMutexAlloc = pFrom->xMutexAlloc;
18233   }
18234   rc = sqlite3GlobalConfig.mutex.xMutexInit();
18235 
18236 #ifdef SQLITE_DEBUG
18237   GLOBAL(int, mutexIsInit) = 1;
18238 #endif
18239 
18240   return rc;
18241 }
18242 
18243 /*
18244 ** Shutdown the mutex system. This call frees resources allocated by
18245 ** sqlite3MutexInit().
18246 */
sqlite3MutexEnd(void)18247 SQLITE_PRIVATE int sqlite3MutexEnd(void){
18248   int rc = SQLITE_OK;
18249   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
18250     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
18251   }
18252 
18253 #ifdef SQLITE_DEBUG
18254   GLOBAL(int, mutexIsInit) = 0;
18255 #endif
18256 
18257   return rc;
18258 }
18259 
18260 /*
18261 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
18262 */
sqlite3_mutex_alloc(int id)18263 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
18264 #ifndef SQLITE_OMIT_AUTOINIT
18265   if( sqlite3_initialize() ) return 0;
18266 #endif
18267   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18268 }
18269 
sqlite3MutexAlloc(int id)18270 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
18271   if( !sqlite3GlobalConfig.bCoreMutex ){
18272     return 0;
18273   }
18274   assert( GLOBAL(int, mutexIsInit) );
18275   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18276 }
18277 
18278 /*
18279 ** Free a dynamic mutex.
18280 */
sqlite3_mutex_free(sqlite3_mutex * p)18281 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
18282   if( p ){
18283     sqlite3GlobalConfig.mutex.xMutexFree(p);
18284   }
18285 }
18286 
18287 /*
18288 ** Obtain the mutex p. If some other thread already has the mutex, block
18289 ** until it can be obtained.
18290 */
sqlite3_mutex_enter(sqlite3_mutex * p)18291 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
18292   if( p ){
18293     sqlite3GlobalConfig.mutex.xMutexEnter(p);
18294   }
18295 }
18296 
18297 /*
18298 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
18299 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
18300 */
sqlite3_mutex_try(sqlite3_mutex * p)18301 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
18302   int rc = SQLITE_OK;
18303   if( p ){
18304     return sqlite3GlobalConfig.mutex.xMutexTry(p);
18305   }
18306   return rc;
18307 }
18308 
18309 /*
18310 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
18311 ** entered by the same thread.  The behavior is undefined if the mutex
18312 ** is not currently entered. If a NULL pointer is passed as an argument
18313 ** this function is a no-op.
18314 */
sqlite3_mutex_leave(sqlite3_mutex * p)18315 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
18316   if( p ){
18317     sqlite3GlobalConfig.mutex.xMutexLeave(p);
18318   }
18319 }
18320 
18321 #ifndef NDEBUG
18322 /*
18323 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18324 ** intended for use inside assert() statements.
18325 */
sqlite3_mutex_held(sqlite3_mutex * p)18326 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
18327   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
18328 }
sqlite3_mutex_notheld(sqlite3_mutex * p)18329 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
18330   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
18331 }
18332 #endif
18333 
18334 #endif /* !defined(SQLITE_MUTEX_OMIT) */
18335 
18336 /************** End of mutex.c ***********************************************/
18337 /************** Begin file mutex_noop.c **************************************/
18338 /*
18339 ** 2008 October 07
18340 **
18341 ** The author disclaims copyright to this source code.  In place of
18342 ** a legal notice, here is a blessing:
18343 **
18344 **    May you do good and not evil.
18345 **    May you find forgiveness for yourself and forgive others.
18346 **    May you share freely, never taking more than you give.
18347 **
18348 *************************************************************************
18349 ** This file contains the C functions that implement mutexes.
18350 **
18351 ** This implementation in this file does not provide any mutual
18352 ** exclusion and is thus suitable for use only in applications
18353 ** that use SQLite in a single thread.  The routines defined
18354 ** here are place-holders.  Applications can substitute working
18355 ** mutex routines at start-time using the
18356 **
18357 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
18358 **
18359 ** interface.
18360 **
18361 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
18362 ** that does error checking on mutexes to make sure they are being
18363 ** called correctly.
18364 */
18365 
18366 #ifndef SQLITE_MUTEX_OMIT
18367 
18368 #ifndef SQLITE_DEBUG
18369 /*
18370 ** Stub routines for all mutex methods.
18371 **
18372 ** This routines provide no mutual exclusion or error checking.
18373 */
noopMutexInit(void)18374 static int noopMutexInit(void){ return SQLITE_OK; }
noopMutexEnd(void)18375 static int noopMutexEnd(void){ return SQLITE_OK; }
noopMutexAlloc(int id)18376 static sqlite3_mutex *noopMutexAlloc(int id){
18377   UNUSED_PARAMETER(id);
18378   return (sqlite3_mutex*)8;
18379 }
noopMutexFree(sqlite3_mutex * p)18380 static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
noopMutexEnter(sqlite3_mutex * p)18381 static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
noopMutexTry(sqlite3_mutex * p)18382 static int noopMutexTry(sqlite3_mutex *p){
18383   UNUSED_PARAMETER(p);
18384   return SQLITE_OK;
18385 }
noopMutexLeave(sqlite3_mutex * p)18386 static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
18387 
sqlite3NoopMutex(void)18388 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
18389   static const sqlite3_mutex_methods sMutex = {
18390     noopMutexInit,
18391     noopMutexEnd,
18392     noopMutexAlloc,
18393     noopMutexFree,
18394     noopMutexEnter,
18395     noopMutexTry,
18396     noopMutexLeave,
18397 
18398     0,
18399     0,
18400   };
18401 
18402   return &sMutex;
18403 }
18404 #endif /* !SQLITE_DEBUG */
18405 
18406 #ifdef SQLITE_DEBUG
18407 /*
18408 ** In this implementation, error checking is provided for testing
18409 ** and debugging purposes.  The mutexes still do not provide any
18410 ** mutual exclusion.
18411 */
18412 
18413 /*
18414 ** The mutex object
18415 */
18416 typedef struct sqlite3_debug_mutex {
18417   int id;     /* The mutex type */
18418   int cnt;    /* Number of entries without a matching leave */
18419 } sqlite3_debug_mutex;
18420 
18421 /*
18422 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18423 ** intended for use inside assert() statements.
18424 */
debugMutexHeld(sqlite3_mutex * pX)18425 static int debugMutexHeld(sqlite3_mutex *pX){
18426   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18427   return p==0 || p->cnt>0;
18428 }
debugMutexNotheld(sqlite3_mutex * pX)18429 static int debugMutexNotheld(sqlite3_mutex *pX){
18430   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18431   return p==0 || p->cnt==0;
18432 }
18433 
18434 /*
18435 ** Initialize and deinitialize the mutex subsystem.
18436 */
debugMutexInit(void)18437 static int debugMutexInit(void){ return SQLITE_OK; }
debugMutexEnd(void)18438 static int debugMutexEnd(void){ return SQLITE_OK; }
18439 
18440 /*
18441 ** The sqlite3_mutex_alloc() routine allocates a new
18442 ** mutex and returns a pointer to it.  If it returns NULL
18443 ** that means that a mutex could not be allocated.
18444 */
debugMutexAlloc(int id)18445 static sqlite3_mutex *debugMutexAlloc(int id){
18446   static sqlite3_debug_mutex aStatic[6];
18447   sqlite3_debug_mutex *pNew = 0;
18448   switch( id ){
18449     case SQLITE_MUTEX_FAST:
18450     case SQLITE_MUTEX_RECURSIVE: {
18451       pNew = sqlite3Malloc(sizeof(*pNew));
18452       if( pNew ){
18453         pNew->id = id;
18454         pNew->cnt = 0;
18455       }
18456       break;
18457     }
18458     default: {
18459       assert( id-2 >= 0 );
18460       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
18461       pNew = &aStatic[id-2];
18462       pNew->id = id;
18463       break;
18464     }
18465   }
18466   return (sqlite3_mutex*)pNew;
18467 }
18468 
18469 /*
18470 ** This routine deallocates a previously allocated mutex.
18471 */
debugMutexFree(sqlite3_mutex * pX)18472 static void debugMutexFree(sqlite3_mutex *pX){
18473   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18474   assert( p->cnt==0 );
18475   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
18476   sqlite3_free(p);
18477 }
18478 
18479 /*
18480 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
18481 ** to enter a mutex.  If another thread is already within the mutex,
18482 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
18483 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
18484 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
18485 ** be entered multiple times by the same thread.  In such cases the,
18486 ** mutex must be exited an equal number of times before another thread
18487 ** can enter.  If the same thread tries to enter any other kind of mutex
18488 ** more than once, the behavior is undefined.
18489 */
debugMutexEnter(sqlite3_mutex * pX)18490 static void debugMutexEnter(sqlite3_mutex *pX){
18491   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18492   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
18493   p->cnt++;
18494 }
debugMutexTry(sqlite3_mutex * pX)18495 static int debugMutexTry(sqlite3_mutex *pX){
18496   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18497   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
18498   p->cnt++;
18499   return SQLITE_OK;
18500 }
18501 
18502 /*
18503 ** The sqlite3_mutex_leave() routine exits a mutex that was
18504 ** previously entered by the same thread.  The behavior
18505 ** is undefined if the mutex is not currently entered or
18506 ** is not currently allocated.  SQLite will never do either.
18507 */
debugMutexLeave(sqlite3_mutex * pX)18508 static void debugMutexLeave(sqlite3_mutex *pX){
18509   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18510   assert( debugMutexHeld(pX) );
18511   p->cnt--;
18512   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
18513 }
18514 
sqlite3NoopMutex(void)18515 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
18516   static const sqlite3_mutex_methods sMutex = {
18517     debugMutexInit,
18518     debugMutexEnd,
18519     debugMutexAlloc,
18520     debugMutexFree,
18521     debugMutexEnter,
18522     debugMutexTry,
18523     debugMutexLeave,
18524 
18525     debugMutexHeld,
18526     debugMutexNotheld
18527   };
18528 
18529   return &sMutex;
18530 }
18531 #endif /* SQLITE_DEBUG */
18532 
18533 /*
18534 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
18535 ** is used regardless of the run-time threadsafety setting.
18536 */
18537 #ifdef SQLITE_MUTEX_NOOP
sqlite3DefaultMutex(void)18538 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
18539   return sqlite3NoopMutex();
18540 }
18541 #endif /* defined(SQLITE_MUTEX_NOOP) */
18542 #endif /* !defined(SQLITE_MUTEX_OMIT) */
18543 
18544 /************** End of mutex_noop.c ******************************************/
18545 /************** Begin file mutex_unix.c **************************************/
18546 /*
18547 ** 2007 August 28
18548 **
18549 ** The author disclaims copyright to this source code.  In place of
18550 ** a legal notice, here is a blessing:
18551 **
18552 **    May you do good and not evil.
18553 **    May you find forgiveness for yourself and forgive others.
18554 **    May you share freely, never taking more than you give.
18555 **
18556 *************************************************************************
18557 ** This file contains the C functions that implement mutexes for pthreads
18558 */
18559 
18560 /*
18561 ** The code in this file is only used if we are compiling threadsafe
18562 ** under unix with pthreads.
18563 **
18564 ** Note that this implementation requires a version of pthreads that
18565 ** supports recursive mutexes.
18566 */
18567 #ifdef SQLITE_MUTEX_PTHREADS
18568 
18569 #include <pthread.h>
18570 
18571 /*
18572 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
18573 ** are necessary under two condidtions:  (1) Debug builds and (2) using
18574 ** home-grown mutexes.  Encapsulate these conditions into a single #define.
18575 */
18576 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
18577 # define SQLITE_MUTEX_NREF 1
18578 #else
18579 # define SQLITE_MUTEX_NREF 0
18580 #endif
18581 
18582 /*
18583 ** Each recursive mutex is an instance of the following structure.
18584 */
18585 struct sqlite3_mutex {
18586   pthread_mutex_t mutex;     /* Mutex controlling the lock */
18587 #if SQLITE_MUTEX_NREF
18588   int id;                    /* Mutex type */
18589   volatile int nRef;         /* Number of entrances */
18590   volatile pthread_t owner;  /* Thread that is within this mutex */
18591   int trace;                 /* True to trace changes */
18592 #endif
18593 };
18594 #if SQLITE_MUTEX_NREF
18595 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
18596 #else
18597 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
18598 #endif
18599 
18600 /*
18601 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18602 ** intended for use only inside assert() statements.  On some platforms,
18603 ** there might be race conditions that can cause these routines to
18604 ** deliver incorrect results.  In particular, if pthread_equal() is
18605 ** not an atomic operation, then these routines might delivery
18606 ** incorrect results.  On most platforms, pthread_equal() is a
18607 ** comparison of two integers and is therefore atomic.  But we are
18608 ** told that HPUX is not such a platform.  If so, then these routines
18609 ** will not always work correctly on HPUX.
18610 **
18611 ** On those platforms where pthread_equal() is not atomic, SQLite
18612 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
18613 ** make sure no assert() statements are evaluated and hence these
18614 ** routines are never called.
18615 */
18616 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
pthreadMutexHeld(sqlite3_mutex * p)18617 static int pthreadMutexHeld(sqlite3_mutex *p){
18618   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
18619 }
pthreadMutexNotheld(sqlite3_mutex * p)18620 static int pthreadMutexNotheld(sqlite3_mutex *p){
18621   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
18622 }
18623 #endif
18624 
18625 /*
18626 ** Initialize and deinitialize the mutex subsystem.
18627 */
pthreadMutexInit(void)18628 static int pthreadMutexInit(void){ return SQLITE_OK; }
pthreadMutexEnd(void)18629 static int pthreadMutexEnd(void){ return SQLITE_OK; }
18630 
18631 /*
18632 ** The sqlite3_mutex_alloc() routine allocates a new
18633 ** mutex and returns a pointer to it.  If it returns NULL
18634 ** that means that a mutex could not be allocated.  SQLite
18635 ** will unwind its stack and return an error.  The argument
18636 ** to sqlite3_mutex_alloc() is one of these integer constants:
18637 **
18638 ** <ul>
18639 ** <li>  SQLITE_MUTEX_FAST
18640 ** <li>  SQLITE_MUTEX_RECURSIVE
18641 ** <li>  SQLITE_MUTEX_STATIC_MASTER
18642 ** <li>  SQLITE_MUTEX_STATIC_MEM
18643 ** <li>  SQLITE_MUTEX_STATIC_MEM2
18644 ** <li>  SQLITE_MUTEX_STATIC_PRNG
18645 ** <li>  SQLITE_MUTEX_STATIC_LRU
18646 ** <li>  SQLITE_MUTEX_STATIC_PMEM
18647 ** </ul>
18648 **
18649 ** The first two constants cause sqlite3_mutex_alloc() to create
18650 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
18651 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
18652 ** The mutex implementation does not need to make a distinction
18653 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
18654 ** not want to.  But SQLite will only request a recursive mutex in
18655 ** cases where it really needs one.  If a faster non-recursive mutex
18656 ** implementation is available on the host platform, the mutex subsystem
18657 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
18658 **
18659 ** The other allowed parameters to sqlite3_mutex_alloc() each return
18660 ** a pointer to a static preexisting mutex.  Six static mutexes are
18661 ** used by the current version of SQLite.  Future versions of SQLite
18662 ** may add additional static mutexes.  Static mutexes are for internal
18663 ** use by SQLite only.  Applications that use SQLite mutexes should
18664 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
18665 ** SQLITE_MUTEX_RECURSIVE.
18666 **
18667 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
18668 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
18669 ** returns a different mutex on every call.  But for the static
18670 ** mutex types, the same mutex is returned on every call that has
18671 ** the same type number.
18672 */
pthreadMutexAlloc(int iType)18673 static sqlite3_mutex *pthreadMutexAlloc(int iType){
18674   static sqlite3_mutex staticMutexes[] = {
18675     SQLITE3_MUTEX_INITIALIZER,
18676     SQLITE3_MUTEX_INITIALIZER,
18677     SQLITE3_MUTEX_INITIALIZER,
18678     SQLITE3_MUTEX_INITIALIZER,
18679     SQLITE3_MUTEX_INITIALIZER,
18680     SQLITE3_MUTEX_INITIALIZER
18681   };
18682   sqlite3_mutex *p;
18683   switch( iType ){
18684     case SQLITE_MUTEX_RECURSIVE: {
18685       p = sqlite3MallocZero( sizeof(*p) );
18686       if( p ){
18687 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18688         /* If recursive mutexes are not available, we will have to
18689         ** build our own.  See below. */
18690         pthread_mutex_init(&p->mutex, 0);
18691 #else
18692         /* Use a recursive mutex if it is available */
18693         pthread_mutexattr_t recursiveAttr;
18694         pthread_mutexattr_init(&recursiveAttr);
18695         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
18696         pthread_mutex_init(&p->mutex, &recursiveAttr);
18697         pthread_mutexattr_destroy(&recursiveAttr);
18698 #endif
18699 #if SQLITE_MUTEX_NREF
18700         p->id = iType;
18701 #endif
18702       }
18703       break;
18704     }
18705     case SQLITE_MUTEX_FAST: {
18706       p = sqlite3MallocZero( sizeof(*p) );
18707       if( p ){
18708 #if SQLITE_MUTEX_NREF
18709         p->id = iType;
18710 #endif
18711         pthread_mutex_init(&p->mutex, 0);
18712       }
18713       break;
18714     }
18715     default: {
18716       assert( iType-2 >= 0 );
18717       assert( iType-2 < ArraySize(staticMutexes) );
18718       p = &staticMutexes[iType-2];
18719 #if SQLITE_MUTEX_NREF
18720       p->id = iType;
18721 #endif
18722       break;
18723     }
18724   }
18725   return p;
18726 }
18727 
18728 
18729 /*
18730 ** This routine deallocates a previously
18731 ** allocated mutex.  SQLite is careful to deallocate every
18732 ** mutex that it allocates.
18733 */
pthreadMutexFree(sqlite3_mutex * p)18734 static void pthreadMutexFree(sqlite3_mutex *p){
18735   assert( p->nRef==0 );
18736   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
18737   pthread_mutex_destroy(&p->mutex);
18738   sqlite3_free(p);
18739 }
18740 
18741 /*
18742 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
18743 ** to enter a mutex.  If another thread is already within the mutex,
18744 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
18745 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
18746 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
18747 ** be entered multiple times by the same thread.  In such cases the,
18748 ** mutex must be exited an equal number of times before another thread
18749 ** can enter.  If the same thread tries to enter any other kind of mutex
18750 ** more than once, the behavior is undefined.
18751 */
pthreadMutexEnter(sqlite3_mutex * p)18752 static void pthreadMutexEnter(sqlite3_mutex *p){
18753   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
18754 
18755 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18756   /* If recursive mutexes are not available, then we have to grow
18757   ** our own.  This implementation assumes that pthread_equal()
18758   ** is atomic - that it cannot be deceived into thinking self
18759   ** and p->owner are equal if p->owner changes between two values
18760   ** that are not equal to self while the comparison is taking place.
18761   ** This implementation also assumes a coherent cache - that
18762   ** separate processes cannot read different values from the same
18763   ** address at the same time.  If either of these two conditions
18764   ** are not met, then the mutexes will fail and problems will result.
18765   */
18766   {
18767     pthread_t self = pthread_self();
18768     if( p->nRef>0 && pthread_equal(p->owner, self) ){
18769       p->nRef++;
18770     }else{
18771       pthread_mutex_lock(&p->mutex);
18772       assert( p->nRef==0 );
18773       p->owner = self;
18774       p->nRef = 1;
18775     }
18776   }
18777 #else
18778   /* Use the built-in recursive mutexes if they are available.
18779   */
18780   pthread_mutex_lock(&p->mutex);
18781 #if SQLITE_MUTEX_NREF
18782   assert( p->nRef>0 || p->owner==0 );
18783   p->owner = pthread_self();
18784   p->nRef++;
18785 #endif
18786 #endif
18787 
18788 #ifdef SQLITE_DEBUG
18789   if( p->trace ){
18790     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18791   }
18792 #endif
18793 }
pthreadMutexTry(sqlite3_mutex * p)18794 static int pthreadMutexTry(sqlite3_mutex *p){
18795   int rc;
18796   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
18797 
18798 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18799   /* If recursive mutexes are not available, then we have to grow
18800   ** our own.  This implementation assumes that pthread_equal()
18801   ** is atomic - that it cannot be deceived into thinking self
18802   ** and p->owner are equal if p->owner changes between two values
18803   ** that are not equal to self while the comparison is taking place.
18804   ** This implementation also assumes a coherent cache - that
18805   ** separate processes cannot read different values from the same
18806   ** address at the same time.  If either of these two conditions
18807   ** are not met, then the mutexes will fail and problems will result.
18808   */
18809   {
18810     pthread_t self = pthread_self();
18811     if( p->nRef>0 && pthread_equal(p->owner, self) ){
18812       p->nRef++;
18813       rc = SQLITE_OK;
18814     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
18815       assert( p->nRef==0 );
18816       p->owner = self;
18817       p->nRef = 1;
18818       rc = SQLITE_OK;
18819     }else{
18820       rc = SQLITE_BUSY;
18821     }
18822   }
18823 #else
18824   /* Use the built-in recursive mutexes if they are available.
18825   */
18826   if( pthread_mutex_trylock(&p->mutex)==0 ){
18827 #if SQLITE_MUTEX_NREF
18828     p->owner = pthread_self();
18829     p->nRef++;
18830 #endif
18831     rc = SQLITE_OK;
18832   }else{
18833     rc = SQLITE_BUSY;
18834   }
18835 #endif
18836 
18837 #ifdef SQLITE_DEBUG
18838   if( rc==SQLITE_OK && p->trace ){
18839     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18840   }
18841 #endif
18842   return rc;
18843 }
18844 
18845 /*
18846 ** The sqlite3_mutex_leave() routine exits a mutex that was
18847 ** previously entered by the same thread.  The behavior
18848 ** is undefined if the mutex is not currently entered or
18849 ** is not currently allocated.  SQLite will never do either.
18850 */
pthreadMutexLeave(sqlite3_mutex * p)18851 static void pthreadMutexLeave(sqlite3_mutex *p){
18852   assert( pthreadMutexHeld(p) );
18853 #if SQLITE_MUTEX_NREF
18854   p->nRef--;
18855   if( p->nRef==0 ) p->owner = 0;
18856 #endif
18857   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
18858 
18859 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18860   if( p->nRef==0 ){
18861     pthread_mutex_unlock(&p->mutex);
18862   }
18863 #else
18864   pthread_mutex_unlock(&p->mutex);
18865 #endif
18866 
18867 #ifdef SQLITE_DEBUG
18868   if( p->trace ){
18869     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18870   }
18871 #endif
18872 }
18873 
sqlite3DefaultMutex(void)18874 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
18875   static const sqlite3_mutex_methods sMutex = {
18876     pthreadMutexInit,
18877     pthreadMutexEnd,
18878     pthreadMutexAlloc,
18879     pthreadMutexFree,
18880     pthreadMutexEnter,
18881     pthreadMutexTry,
18882     pthreadMutexLeave,
18883 #ifdef SQLITE_DEBUG
18884     pthreadMutexHeld,
18885     pthreadMutexNotheld
18886 #else
18887     0,
18888     0
18889 #endif
18890   };
18891 
18892   return &sMutex;
18893 }
18894 
18895 #endif /* SQLITE_MUTEX_PTHREADS */
18896 
18897 /************** End of mutex_unix.c ******************************************/
18898 /************** Begin file mutex_w32.c ***************************************/
18899 /*
18900 ** 2007 August 14
18901 **
18902 ** The author disclaims copyright to this source code.  In place of
18903 ** a legal notice, here is a blessing:
18904 **
18905 **    May you do good and not evil.
18906 **    May you find forgiveness for yourself and forgive others.
18907 **    May you share freely, never taking more than you give.
18908 **
18909 *************************************************************************
18910 ** This file contains the C functions that implement mutexes for win32
18911 */
18912 
18913 /*
18914 ** The code in this file is only used if we are compiling multithreaded
18915 ** on a win32 system.
18916 */
18917 #ifdef SQLITE_MUTEX_W32
18918 
18919 /*
18920 ** Each recursive mutex is an instance of the following structure.
18921 */
18922 struct sqlite3_mutex {
18923   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
18924   int id;                    /* Mutex type */
18925 #ifdef SQLITE_DEBUG
18926   volatile int nRef;         /* Number of enterances */
18927   volatile DWORD owner;      /* Thread holding this mutex */
18928   int trace;                 /* True to trace changes */
18929 #endif
18930 };
18931 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
18932 #ifdef SQLITE_DEBUG
18933 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
18934 #else
18935 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
18936 #endif
18937 
18938 /*
18939 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
18940 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
18941 **
18942 ** Here is an interesting observation:  Win95, Win98, and WinME lack
18943 ** the LockFileEx() API.  But we can still statically link against that
18944 ** API as long as we don't call it win running Win95/98/ME.  A call to
18945 ** this routine is used to determine if the host is Win95/98/ME or
18946 ** WinNT/2K/XP so that we will know whether or not we can safely call
18947 ** the LockFileEx() API.
18948 **
18949 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
18950 ** which is only available if your application was compiled with
18951 ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
18952 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
18953 ** this out as well.
18954 */
18955 #if 0
18956 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
18957 # define mutexIsNT()  (1)
18958 #else
mutexIsNT(void)18959   static int mutexIsNT(void){
18960     static int osType = 0;
18961     if( osType==0 ){
18962       OSVERSIONINFO sInfo;
18963       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
18964       GetVersionEx(&sInfo);
18965       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
18966     }
18967     return osType==2;
18968   }
18969 #endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
18970 #endif
18971 
18972 #ifdef SQLITE_DEBUG
18973 /*
18974 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18975 ** intended for use only inside assert() statements.
18976 */
winMutexHeld(sqlite3_mutex * p)18977 static int winMutexHeld(sqlite3_mutex *p){
18978   return p->nRef!=0 && p->owner==GetCurrentThreadId();
18979 }
winMutexNotheld2(sqlite3_mutex * p,DWORD tid)18980 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
18981   return p->nRef==0 || p->owner!=tid;
18982 }
winMutexNotheld(sqlite3_mutex * p)18983 static int winMutexNotheld(sqlite3_mutex *p){
18984   DWORD tid = GetCurrentThreadId();
18985   return winMutexNotheld2(p, tid);
18986 }
18987 #endif
18988 
18989 
18990 /*
18991 ** Initialize and deinitialize the mutex subsystem.
18992 */
18993 static sqlite3_mutex winMutex_staticMutexes[6] = {
18994   SQLITE3_MUTEX_INITIALIZER,
18995   SQLITE3_MUTEX_INITIALIZER,
18996   SQLITE3_MUTEX_INITIALIZER,
18997   SQLITE3_MUTEX_INITIALIZER,
18998   SQLITE3_MUTEX_INITIALIZER,
18999   SQLITE3_MUTEX_INITIALIZER
19000 };
19001 static int winMutex_isInit = 0;
19002 /* As winMutexInit() and winMutexEnd() are called as part
19003 ** of the sqlite3_initialize and sqlite3_shutdown()
19004 ** processing, the "interlocked" magic is probably not
19005 ** strictly necessary.
19006 */
19007 static LONG winMutex_lock = 0;
19008 
19009 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
19010 
winMutexInit(void)19011 static int winMutexInit(void){
19012   /* The first to increment to 1 does actual initialization */
19013   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
19014     int i;
19015     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
19016 #if SQLITE_OS_WINRT
19017       InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
19018 #else
19019       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
19020 #endif
19021     }
19022     winMutex_isInit = 1;
19023   }else{
19024     /* Someone else is in the process of initing the static mutexes */
19025     while( !winMutex_isInit ){
19026       sqlite3_win32_sleep(1);
19027     }
19028   }
19029   return SQLITE_OK;
19030 }
19031 
winMutexEnd(void)19032 static int winMutexEnd(void){
19033   /* The first to decrement to 0 does actual shutdown
19034   ** (which should be the last to shutdown.) */
19035   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
19036     if( winMutex_isInit==1 ){
19037       int i;
19038       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
19039         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
19040       }
19041       winMutex_isInit = 0;
19042     }
19043   }
19044   return SQLITE_OK;
19045 }
19046 
19047 /*
19048 ** The sqlite3_mutex_alloc() routine allocates a new
19049 ** mutex and returns a pointer to it.  If it returns NULL
19050 ** that means that a mutex could not be allocated.  SQLite
19051 ** will unwind its stack and return an error.  The argument
19052 ** to sqlite3_mutex_alloc() is one of these integer constants:
19053 **
19054 ** <ul>
19055 ** <li>  SQLITE_MUTEX_FAST
19056 ** <li>  SQLITE_MUTEX_RECURSIVE
19057 ** <li>  SQLITE_MUTEX_STATIC_MASTER
19058 ** <li>  SQLITE_MUTEX_STATIC_MEM
19059 ** <li>  SQLITE_MUTEX_STATIC_MEM2
19060 ** <li>  SQLITE_MUTEX_STATIC_PRNG
19061 ** <li>  SQLITE_MUTEX_STATIC_LRU
19062 ** <li>  SQLITE_MUTEX_STATIC_PMEM
19063 ** </ul>
19064 **
19065 ** The first two constants cause sqlite3_mutex_alloc() to create
19066 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
19067 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
19068 ** The mutex implementation does not need to make a distinction
19069 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
19070 ** not want to.  But SQLite will only request a recursive mutex in
19071 ** cases where it really needs one.  If a faster non-recursive mutex
19072 ** implementation is available on the host platform, the mutex subsystem
19073 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
19074 **
19075 ** The other allowed parameters to sqlite3_mutex_alloc() each return
19076 ** a pointer to a static preexisting mutex.  Six static mutexes are
19077 ** used by the current version of SQLite.  Future versions of SQLite
19078 ** may add additional static mutexes.  Static mutexes are for internal
19079 ** use by SQLite only.  Applications that use SQLite mutexes should
19080 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
19081 ** SQLITE_MUTEX_RECURSIVE.
19082 **
19083 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
19084 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
19085 ** returns a different mutex on every call.  But for the static
19086 ** mutex types, the same mutex is returned on every call that has
19087 ** the same type number.
19088 */
winMutexAlloc(int iType)19089 static sqlite3_mutex *winMutexAlloc(int iType){
19090   sqlite3_mutex *p;
19091 
19092   switch( iType ){
19093     case SQLITE_MUTEX_FAST:
19094     case SQLITE_MUTEX_RECURSIVE: {
19095       p = sqlite3MallocZero( sizeof(*p) );
19096       if( p ){
19097 #ifdef SQLITE_DEBUG
19098         p->id = iType;
19099 #endif
19100 #if SQLITE_OS_WINRT
19101         InitializeCriticalSectionEx(&p->mutex, 0, 0);
19102 #else
19103         InitializeCriticalSection(&p->mutex);
19104 #endif
19105       }
19106       break;
19107     }
19108     default: {
19109       assert( winMutex_isInit==1 );
19110       assert( iType-2 >= 0 );
19111       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
19112       p = &winMutex_staticMutexes[iType-2];
19113 #ifdef SQLITE_DEBUG
19114       p->id = iType;
19115 #endif
19116       break;
19117     }
19118   }
19119   return p;
19120 }
19121 
19122 
19123 /*
19124 ** This routine deallocates a previously
19125 ** allocated mutex.  SQLite is careful to deallocate every
19126 ** mutex that it allocates.
19127 */
winMutexFree(sqlite3_mutex * p)19128 static void winMutexFree(sqlite3_mutex *p){
19129   assert( p );
19130   assert( p->nRef==0 && p->owner==0 );
19131   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
19132   DeleteCriticalSection(&p->mutex);
19133   sqlite3_free(p);
19134 }
19135 
19136 /*
19137 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
19138 ** to enter a mutex.  If another thread is already within the mutex,
19139 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
19140 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
19141 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
19142 ** be entered multiple times by the same thread.  In such cases the,
19143 ** mutex must be exited an equal number of times before another thread
19144 ** can enter.  If the same thread tries to enter any other kind of mutex
19145 ** more than once, the behavior is undefined.
19146 */
winMutexEnter(sqlite3_mutex * p)19147 static void winMutexEnter(sqlite3_mutex *p){
19148 #ifdef SQLITE_DEBUG
19149   DWORD tid = GetCurrentThreadId();
19150   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19151 #endif
19152   EnterCriticalSection(&p->mutex);
19153 #ifdef SQLITE_DEBUG
19154   assert( p->nRef>0 || p->owner==0 );
19155   p->owner = tid;
19156   p->nRef++;
19157   if( p->trace ){
19158     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19159   }
19160 #endif
19161 }
winMutexTry(sqlite3_mutex * p)19162 static int winMutexTry(sqlite3_mutex *p){
19163 #ifndef NDEBUG
19164   DWORD tid = GetCurrentThreadId();
19165 #endif
19166   int rc = SQLITE_BUSY;
19167   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19168   /*
19169   ** The sqlite3_mutex_try() routine is very rarely used, and when it
19170   ** is used it is merely an optimization.  So it is OK for it to always
19171   ** fail.
19172   **
19173   ** The TryEnterCriticalSection() interface is only available on WinNT.
19174   ** And some windows compilers complain if you try to use it without
19175   ** first doing some #defines that prevent SQLite from building on Win98.
19176   ** For that reason, we will omit this optimization for now.  See
19177   ** ticket #2685.
19178   */
19179 #if 0
19180   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
19181     p->owner = tid;
19182     p->nRef++;
19183     rc = SQLITE_OK;
19184   }
19185 #else
19186   UNUSED_PARAMETER(p);
19187 #endif
19188 #ifdef SQLITE_DEBUG
19189   if( rc==SQLITE_OK && p->trace ){
19190     printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19191   }
19192 #endif
19193   return rc;
19194 }
19195 
19196 /*
19197 ** The sqlite3_mutex_leave() routine exits a mutex that was
19198 ** previously entered by the same thread.  The behavior
19199 ** is undefined if the mutex is not currently entered or
19200 ** is not currently allocated.  SQLite will never do either.
19201 */
winMutexLeave(sqlite3_mutex * p)19202 static void winMutexLeave(sqlite3_mutex *p){
19203 #ifndef NDEBUG
19204   DWORD tid = GetCurrentThreadId();
19205   assert( p->nRef>0 );
19206   assert( p->owner==tid );
19207   p->nRef--;
19208   if( p->nRef==0 ) p->owner = 0;
19209   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
19210 #endif
19211   LeaveCriticalSection(&p->mutex);
19212 #ifdef SQLITE_DEBUG
19213   if( p->trace ){
19214     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19215   }
19216 #endif
19217 }
19218 
sqlite3DefaultMutex(void)19219 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
19220   static const sqlite3_mutex_methods sMutex = {
19221     winMutexInit,
19222     winMutexEnd,
19223     winMutexAlloc,
19224     winMutexFree,
19225     winMutexEnter,
19226     winMutexTry,
19227     winMutexLeave,
19228 #ifdef SQLITE_DEBUG
19229     winMutexHeld,
19230     winMutexNotheld
19231 #else
19232     0,
19233     0
19234 #endif
19235   };
19236 
19237   return &sMutex;
19238 }
19239 #endif /* SQLITE_MUTEX_W32 */
19240 
19241 /************** End of mutex_w32.c *******************************************/
19242 /************** Begin file malloc.c ******************************************/
19243 /*
19244 ** 2001 September 15
19245 **
19246 ** The author disclaims copyright to this source code.  In place of
19247 ** a legal notice, here is a blessing:
19248 **
19249 **    May you do good and not evil.
19250 **    May you find forgiveness for yourself and forgive others.
19251 **    May you share freely, never taking more than you give.
19252 **
19253 *************************************************************************
19254 **
19255 ** Memory allocation functions used throughout sqlite.
19256 */
19257 /* #include <stdarg.h> */
19258 
19259 /*
19260 ** Attempt to release up to n bytes of non-essential memory currently
19261 ** held by SQLite. An example of non-essential memory is memory used to
19262 ** cache database pages that are not currently in use.
19263 */
sqlite3_release_memory(int n)19264 SQLITE_API int sqlite3_release_memory(int n){
19265 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
19266   return sqlite3PcacheReleaseMemory(n);
19267 #else
19268   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
19269   ** is a no-op returning zero if SQLite is not compiled with
19270   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
19271   UNUSED_PARAMETER(n);
19272   return 0;
19273 #endif
19274 }
19275 
19276 /*
19277 ** An instance of the following object records the location of
19278 ** each unused scratch buffer.
19279 */
19280 typedef struct ScratchFreeslot {
19281   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
19282 } ScratchFreeslot;
19283 
19284 /*
19285 ** State information local to the memory allocation subsystem.
19286 */
19287 static SQLITE_WSD struct Mem0Global {
19288   sqlite3_mutex *mutex;         /* Mutex to serialize access */
19289 
19290   /*
19291   ** The alarm callback and its arguments.  The mem0.mutex lock will
19292   ** be held while the callback is running.  Recursive calls into
19293   ** the memory subsystem are allowed, but no new callbacks will be
19294   ** issued.
19295   */
19296   sqlite3_int64 alarmThreshold;
19297   void (*alarmCallback)(void*, sqlite3_int64,int);
19298   void *alarmArg;
19299 
19300   /*
19301   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
19302   ** (so that a range test can be used to determine if an allocation
19303   ** being freed came from pScratch) and a pointer to the list of
19304   ** unused scratch allocations.
19305   */
19306   void *pScratchEnd;
19307   ScratchFreeslot *pScratchFree;
19308   u32 nScratchFree;
19309 
19310   /*
19311   ** True if heap is nearly "full" where "full" is defined by the
19312   ** sqlite3_soft_heap_limit() setting.
19313   */
19314   int nearlyFull;
19315 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
19316 
19317 #define mem0 GLOBAL(struct Mem0Global, mem0)
19318 
19319 /*
19320 ** This routine runs when the memory allocator sees that the
19321 ** total memory allocation is about to exceed the soft heap
19322 ** limit.
19323 */
softHeapLimitEnforcer(void * NotUsed,sqlite3_int64 NotUsed2,int allocSize)19324 static void softHeapLimitEnforcer(
19325   void *NotUsed,
19326   sqlite3_int64 NotUsed2,
19327   int allocSize
19328 ){
19329   UNUSED_PARAMETER2(NotUsed, NotUsed2);
19330   sqlite3_release_memory(allocSize);
19331 }
19332 
19333 /*
19334 ** Change the alarm callback
19335 */
sqlite3MemoryAlarm(void (* xCallback)(void * pArg,sqlite3_int64 used,int N),void * pArg,sqlite3_int64 iThreshold)19336 static int sqlite3MemoryAlarm(
19337   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
19338   void *pArg,
19339   sqlite3_int64 iThreshold
19340 ){
19341   int nUsed;
19342   sqlite3_mutex_enter(mem0.mutex);
19343   mem0.alarmCallback = xCallback;
19344   mem0.alarmArg = pArg;
19345   mem0.alarmThreshold = iThreshold;
19346   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
19347   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
19348   sqlite3_mutex_leave(mem0.mutex);
19349   return SQLITE_OK;
19350 }
19351 
19352 #ifndef SQLITE_OMIT_DEPRECATED
19353 /*
19354 ** Deprecated external interface.  Internal/core SQLite code
19355 ** should call sqlite3MemoryAlarm.
19356 */
sqlite3_memory_alarm(void (* xCallback)(void * pArg,sqlite3_int64 used,int N),void * pArg,sqlite3_int64 iThreshold)19357 SQLITE_API int sqlite3_memory_alarm(
19358   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
19359   void *pArg,
19360   sqlite3_int64 iThreshold
19361 ){
19362   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
19363 }
19364 #endif
19365 
19366 /*
19367 ** Set the soft heap-size limit for the library. Passing a zero or
19368 ** negative value indicates no limit.
19369 */
sqlite3_soft_heap_limit64(sqlite3_int64 n)19370 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
19371   sqlite3_int64 priorLimit;
19372   sqlite3_int64 excess;
19373 #ifndef SQLITE_OMIT_AUTOINIT
19374   int rc = sqlite3_initialize();
19375   if( rc ) return -1;
19376 #endif
19377   sqlite3_mutex_enter(mem0.mutex);
19378   priorLimit = mem0.alarmThreshold;
19379   sqlite3_mutex_leave(mem0.mutex);
19380   if( n<0 ) return priorLimit;
19381   if( n>0 ){
19382     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
19383   }else{
19384     sqlite3MemoryAlarm(0, 0, 0);
19385   }
19386   excess = sqlite3_memory_used() - n;
19387   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
19388   return priorLimit;
19389 }
sqlite3_soft_heap_limit(int n)19390 SQLITE_API void sqlite3_soft_heap_limit(int n){
19391   if( n<0 ) n = 0;
19392   sqlite3_soft_heap_limit64(n);
19393 }
19394 
19395 /*
19396 ** Initialize the memory allocation subsystem.
19397 */
sqlite3MallocInit(void)19398 SQLITE_PRIVATE int sqlite3MallocInit(void){
19399   if( sqlite3GlobalConfig.m.xMalloc==0 ){
19400     sqlite3MemSetDefault();
19401   }
19402   memset(&mem0, 0, sizeof(mem0));
19403   if( sqlite3GlobalConfig.bCoreMutex ){
19404     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
19405   }
19406   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
19407       && sqlite3GlobalConfig.nScratch>0 ){
19408     int i, n, sz;
19409     ScratchFreeslot *pSlot;
19410     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
19411     sqlite3GlobalConfig.szScratch = sz;
19412     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
19413     n = sqlite3GlobalConfig.nScratch;
19414     mem0.pScratchFree = pSlot;
19415     mem0.nScratchFree = n;
19416     for(i=0; i<n-1; i++){
19417       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
19418       pSlot = pSlot->pNext;
19419     }
19420     pSlot->pNext = 0;
19421     mem0.pScratchEnd = (void*)&pSlot[1];
19422   }else{
19423     mem0.pScratchEnd = 0;
19424     sqlite3GlobalConfig.pScratch = 0;
19425     sqlite3GlobalConfig.szScratch = 0;
19426     sqlite3GlobalConfig.nScratch = 0;
19427   }
19428   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
19429       || sqlite3GlobalConfig.nPage<1 ){
19430     sqlite3GlobalConfig.pPage = 0;
19431     sqlite3GlobalConfig.szPage = 0;
19432     sqlite3GlobalConfig.nPage = 0;
19433   }
19434   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
19435 }
19436 
19437 /*
19438 ** Return true if the heap is currently under memory pressure - in other
19439 ** words if the amount of heap used is close to the limit set by
19440 ** sqlite3_soft_heap_limit().
19441 */
sqlite3HeapNearlyFull(void)19442 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
19443   return mem0.nearlyFull;
19444 }
19445 
19446 /*
19447 ** Deinitialize the memory allocation subsystem.
19448 */
sqlite3MallocEnd(void)19449 SQLITE_PRIVATE void sqlite3MallocEnd(void){
19450   if( sqlite3GlobalConfig.m.xShutdown ){
19451     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
19452   }
19453   memset(&mem0, 0, sizeof(mem0));
19454 }
19455 
19456 /*
19457 ** Return the amount of memory currently checked out.
19458 */
sqlite3_memory_used(void)19459 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
19460   int n, mx;
19461   sqlite3_int64 res;
19462   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
19463   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
19464   return res;
19465 }
19466 
19467 /*
19468 ** Return the maximum amount of memory that has ever been
19469 ** checked out since either the beginning of this process
19470 ** or since the most recent reset.
19471 */
sqlite3_memory_highwater(int resetFlag)19472 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
19473   int n, mx;
19474   sqlite3_int64 res;
19475   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
19476   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
19477   return res;
19478 }
19479 
19480 /*
19481 ** Trigger the alarm
19482 */
sqlite3MallocAlarm(int nByte)19483 static void sqlite3MallocAlarm(int nByte){
19484   void (*xCallback)(void*,sqlite3_int64,int);
19485   sqlite3_int64 nowUsed;
19486   void *pArg;
19487   if( mem0.alarmCallback==0 ) return;
19488   xCallback = mem0.alarmCallback;
19489   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
19490   pArg = mem0.alarmArg;
19491   mem0.alarmCallback = 0;
19492   sqlite3_mutex_leave(mem0.mutex);
19493   xCallback(pArg, nowUsed, nByte);
19494   sqlite3_mutex_enter(mem0.mutex);
19495   mem0.alarmCallback = xCallback;
19496   mem0.alarmArg = pArg;
19497 }
19498 
19499 /*
19500 ** Do a memory allocation with statistics and alarms.  Assume the
19501 ** lock is already held.
19502 */
mallocWithAlarm(int n,void ** pp)19503 static int mallocWithAlarm(int n, void **pp){
19504   int nFull;
19505   void *p;
19506   assert( sqlite3_mutex_held(mem0.mutex) );
19507   nFull = sqlite3GlobalConfig.m.xRoundup(n);
19508   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
19509   if( mem0.alarmCallback!=0 ){
19510     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
19511     if( nUsed >= mem0.alarmThreshold - nFull ){
19512       mem0.nearlyFull = 1;
19513       sqlite3MallocAlarm(nFull);
19514     }else{
19515       mem0.nearlyFull = 0;
19516     }
19517   }
19518   p = sqlite3GlobalConfig.m.xMalloc(nFull);
19519 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
19520   if( p==0 && mem0.alarmCallback ){
19521     sqlite3MallocAlarm(nFull);
19522     p = sqlite3GlobalConfig.m.xMalloc(nFull);
19523   }
19524 #endif
19525   if( p ){
19526     nFull = sqlite3MallocSize(p);
19527     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
19528     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
19529   }
19530   *pp = p;
19531   return nFull;
19532 }
19533 
19534 /*
19535 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
19536 ** assumes the memory subsystem has already been initialized.
19537 */
sqlite3Malloc(int n)19538 SQLITE_PRIVATE void *sqlite3Malloc(int n){
19539   void *p;
19540   if( n<=0               /* IMP: R-65312-04917 */
19541    || n>=0x7fffff00
19542   ){
19543     /* A memory allocation of a number of bytes which is near the maximum
19544     ** signed integer value might cause an integer overflow inside of the
19545     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
19546     ** 255 bytes of overhead.  SQLite itself will never use anything near
19547     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
19548     p = 0;
19549   }else if( sqlite3GlobalConfig.bMemstat ){
19550     sqlite3_mutex_enter(mem0.mutex);
19551     mallocWithAlarm(n, &p);
19552     sqlite3_mutex_leave(mem0.mutex);
19553   }else{
19554     p = sqlite3GlobalConfig.m.xMalloc(n);
19555   }
19556   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
19557   return p;
19558 }
19559 
19560 /*
19561 ** This version of the memory allocation is for use by the application.
19562 ** First make sure the memory subsystem is initialized, then do the
19563 ** allocation.
19564 */
sqlite3_malloc(int n)19565 SQLITE_API void *sqlite3_malloc(int n){
19566 #ifndef SQLITE_OMIT_AUTOINIT
19567   if( sqlite3_initialize() ) return 0;
19568 #endif
19569   return sqlite3Malloc(n);
19570 }
19571 
19572 /*
19573 ** Each thread may only have a single outstanding allocation from
19574 ** xScratchMalloc().  We verify this constraint in the single-threaded
19575 ** case by setting scratchAllocOut to 1 when an allocation
19576 ** is outstanding clearing it when the allocation is freed.
19577 */
19578 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
19579 static int scratchAllocOut = 0;
19580 #endif
19581 
19582 
19583 /*
19584 ** Allocate memory that is to be used and released right away.
19585 ** This routine is similar to alloca() in that it is not intended
19586 ** for situations where the memory might be held long-term.  This
19587 ** routine is intended to get memory to old large transient data
19588 ** structures that would not normally fit on the stack of an
19589 ** embedded processor.
19590 */
sqlite3ScratchMalloc(int n)19591 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
19592   void *p;
19593   assert( n>0 );
19594 
19595   sqlite3_mutex_enter(mem0.mutex);
19596   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
19597     p = mem0.pScratchFree;
19598     mem0.pScratchFree = mem0.pScratchFree->pNext;
19599     mem0.nScratchFree--;
19600     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
19601     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
19602     sqlite3_mutex_leave(mem0.mutex);
19603   }else{
19604     if( sqlite3GlobalConfig.bMemstat ){
19605       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
19606       n = mallocWithAlarm(n, &p);
19607       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
19608       sqlite3_mutex_leave(mem0.mutex);
19609     }else{
19610       sqlite3_mutex_leave(mem0.mutex);
19611       p = sqlite3GlobalConfig.m.xMalloc(n);
19612     }
19613     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
19614   }
19615   assert( sqlite3_mutex_notheld(mem0.mutex) );
19616 
19617 
19618 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
19619   /* Verify that no more than two scratch allocations per thread
19620   ** are outstanding at one time.  (This is only checked in the
19621   ** single-threaded case since checking in the multi-threaded case
19622   ** would be much more complicated.) */
19623   assert( scratchAllocOut<=1 );
19624   if( p ) scratchAllocOut++;
19625 #endif
19626 
19627   return p;
19628 }
sqlite3ScratchFree(void * p)19629 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
19630   if( p ){
19631 
19632 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
19633     /* Verify that no more than two scratch allocation per thread
19634     ** is outstanding at one time.  (This is only checked in the
19635     ** single-threaded case since checking in the multi-threaded case
19636     ** would be much more complicated.) */
19637     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
19638     scratchAllocOut--;
19639 #endif
19640 
19641     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
19642       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
19643       ScratchFreeslot *pSlot;
19644       pSlot = (ScratchFreeslot*)p;
19645       sqlite3_mutex_enter(mem0.mutex);
19646       pSlot->pNext = mem0.pScratchFree;
19647       mem0.pScratchFree = pSlot;
19648       mem0.nScratchFree++;
19649       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
19650       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
19651       sqlite3_mutex_leave(mem0.mutex);
19652     }else{
19653       /* Release memory back to the heap */
19654       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
19655       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
19656       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
19657       if( sqlite3GlobalConfig.bMemstat ){
19658         int iSize = sqlite3MallocSize(p);
19659         sqlite3_mutex_enter(mem0.mutex);
19660         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
19661         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
19662         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
19663         sqlite3GlobalConfig.m.xFree(p);
19664         sqlite3_mutex_leave(mem0.mutex);
19665       }else{
19666         sqlite3GlobalConfig.m.xFree(p);
19667       }
19668     }
19669   }
19670 }
19671 
19672 /*
19673 ** TRUE if p is a lookaside memory allocation from db
19674 */
19675 #ifndef SQLITE_OMIT_LOOKASIDE
isLookaside(sqlite3 * db,void * p)19676 static int isLookaside(sqlite3 *db, void *p){
19677   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
19678 }
19679 #else
19680 #define isLookaside(A,B) 0
19681 #endif
19682 
19683 /*
19684 ** Return the size of a memory allocation previously obtained from
19685 ** sqlite3Malloc() or sqlite3_malloc().
19686 */
sqlite3MallocSize(void * p)19687 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
19688   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
19689   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
19690   return sqlite3GlobalConfig.m.xSize(p);
19691 }
sqlite3DbMallocSize(sqlite3 * db,void * p)19692 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
19693   assert( db!=0 );
19694   assert( sqlite3_mutex_held(db->mutex) );
19695   if( isLookaside(db, p) ){
19696     return db->lookaside.sz;
19697   }else{
19698     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
19699     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
19700     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
19701     return sqlite3GlobalConfig.m.xSize(p);
19702   }
19703 }
19704 
19705 /*
19706 ** Free memory previously obtained from sqlite3Malloc().
19707 */
sqlite3_free(void * p)19708 SQLITE_API void sqlite3_free(void *p){
19709   if( p==0 ) return;  /* IMP: R-49053-54554 */
19710   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
19711   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
19712   if( sqlite3GlobalConfig.bMemstat ){
19713     sqlite3_mutex_enter(mem0.mutex);
19714     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
19715     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
19716     sqlite3GlobalConfig.m.xFree(p);
19717     sqlite3_mutex_leave(mem0.mutex);
19718   }else{
19719     sqlite3GlobalConfig.m.xFree(p);
19720   }
19721 }
19722 
19723 /*
19724 ** Free memory that might be associated with a particular database
19725 ** connection.
19726 */
sqlite3DbFree(sqlite3 * db,void * p)19727 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
19728   assert( db==0 || sqlite3_mutex_held(db->mutex) );
19729   if( p==0 ) return;
19730   if( db ){
19731     if( db->pnBytesFreed ){
19732       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
19733       return;
19734     }
19735     if( isLookaside(db, p) ){
19736       LookasideSlot *pBuf = (LookasideSlot*)p;
19737 #if SQLITE_DEBUG
19738       /* Trash all content in the buffer being freed */
19739       memset(p, 0xaa, db->lookaside.sz);
19740 #endif
19741       pBuf->pNext = db->lookaside.pFree;
19742       db->lookaside.pFree = pBuf;
19743       db->lookaside.nOut--;
19744       return;
19745     }
19746   }
19747   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
19748   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
19749   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
19750   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
19751   sqlite3_free(p);
19752 }
19753 
19754 /*
19755 ** Change the size of an existing memory allocation
19756 */
sqlite3Realloc(void * pOld,int nBytes)19757 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
19758   int nOld, nNew, nDiff;
19759   void *pNew;
19760   if( pOld==0 ){
19761     return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
19762   }
19763   if( nBytes<=0 ){
19764     sqlite3_free(pOld); /* IMP: R-31593-10574 */
19765     return 0;
19766   }
19767   if( nBytes>=0x7fffff00 ){
19768     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
19769     return 0;
19770   }
19771   nOld = sqlite3MallocSize(pOld);
19772   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
19773   ** argument to xRealloc is always a value returned by a prior call to
19774   ** xRoundup. */
19775   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
19776   if( nOld==nNew ){
19777     pNew = pOld;
19778   }else if( sqlite3GlobalConfig.bMemstat ){
19779     sqlite3_mutex_enter(mem0.mutex);
19780     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
19781     nDiff = nNew - nOld;
19782     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
19783           mem0.alarmThreshold-nDiff ){
19784       sqlite3MallocAlarm(nDiff);
19785     }
19786     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
19787     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
19788     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
19789     if( pNew==0 && mem0.alarmCallback ){
19790       sqlite3MallocAlarm(nBytes);
19791       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
19792     }
19793     if( pNew ){
19794       nNew = sqlite3MallocSize(pNew);
19795       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
19796     }
19797     sqlite3_mutex_leave(mem0.mutex);
19798   }else{
19799     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
19800   }
19801   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
19802   return pNew;
19803 }
19804 
19805 /*
19806 ** The public interface to sqlite3Realloc.  Make sure that the memory
19807 ** subsystem is initialized prior to invoking sqliteRealloc.
19808 */
sqlite3_realloc(void * pOld,int n)19809 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
19810 #ifndef SQLITE_OMIT_AUTOINIT
19811   if( sqlite3_initialize() ) return 0;
19812 #endif
19813   return sqlite3Realloc(pOld, n);
19814 }
19815 
19816 
19817 /*
19818 ** Allocate and zero memory.
19819 */
sqlite3MallocZero(int n)19820 SQLITE_PRIVATE void *sqlite3MallocZero(int n){
19821   void *p = sqlite3Malloc(n);
19822   if( p ){
19823     memset(p, 0, n);
19824   }
19825   return p;
19826 }
19827 
19828 /*
19829 ** Allocate and zero memory.  If the allocation fails, make
19830 ** the mallocFailed flag in the connection pointer.
19831 */
sqlite3DbMallocZero(sqlite3 * db,int n)19832 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
19833   void *p = sqlite3DbMallocRaw(db, n);
19834   if( p ){
19835     memset(p, 0, n);
19836   }
19837   return p;
19838 }
19839 
19840 /*
19841 ** Allocate and zero memory.  If the allocation fails, make
19842 ** the mallocFailed flag in the connection pointer.
19843 **
19844 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
19845 ** failure on the same database connection) then always return 0.
19846 ** Hence for a particular database connection, once malloc starts
19847 ** failing, it fails consistently until mallocFailed is reset.
19848 ** This is an important assumption.  There are many places in the
19849 ** code that do things like this:
19850 **
19851 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
19852 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
19853 **         if( b ) a[10] = 9;
19854 **
19855 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
19856 ** that all prior mallocs (ex: "a") worked too.
19857 */
sqlite3DbMallocRaw(sqlite3 * db,int n)19858 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
19859   void *p;
19860   assert( db==0 || sqlite3_mutex_held(db->mutex) );
19861   assert( db==0 || db->pnBytesFreed==0 );
19862 #ifndef SQLITE_OMIT_LOOKASIDE
19863   if( db ){
19864     LookasideSlot *pBuf;
19865     if( db->mallocFailed ){
19866       return 0;
19867     }
19868     if( db->lookaside.bEnabled ){
19869       if( n>db->lookaside.sz ){
19870         db->lookaside.anStat[1]++;
19871       }else if( (pBuf = db->lookaside.pFree)==0 ){
19872         db->lookaside.anStat[2]++;
19873       }else{
19874         db->lookaside.pFree = pBuf->pNext;
19875         db->lookaside.nOut++;
19876         db->lookaside.anStat[0]++;
19877         if( db->lookaside.nOut>db->lookaside.mxOut ){
19878           db->lookaside.mxOut = db->lookaside.nOut;
19879         }
19880         return (void*)pBuf;
19881       }
19882     }
19883   }
19884 #else
19885   if( db && db->mallocFailed ){
19886     return 0;
19887   }
19888 #endif
19889   p = sqlite3Malloc(n);
19890   if( !p && db ){
19891     db->mallocFailed = 1;
19892   }
19893   sqlite3MemdebugSetType(p, MEMTYPE_DB |
19894          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
19895   return p;
19896 }
19897 
19898 /*
19899 ** Resize the block of memory pointed to by p to n bytes. If the
19900 ** resize fails, set the mallocFailed flag in the connection object.
19901 */
sqlite3DbRealloc(sqlite3 * db,void * p,int n)19902 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
19903   void *pNew = 0;
19904   assert( db!=0 );
19905   assert( sqlite3_mutex_held(db->mutex) );
19906   if( db->mallocFailed==0 ){
19907     if( p==0 ){
19908       return sqlite3DbMallocRaw(db, n);
19909     }
19910     if( isLookaside(db, p) ){
19911       if( n<=db->lookaside.sz ){
19912         return p;
19913       }
19914       pNew = sqlite3DbMallocRaw(db, n);
19915       if( pNew ){
19916         memcpy(pNew, p, db->lookaside.sz);
19917         sqlite3DbFree(db, p);
19918       }
19919     }else{
19920       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
19921       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
19922       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
19923       pNew = sqlite3_realloc(p, n);
19924       if( !pNew ){
19925         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
19926         db->mallocFailed = 1;
19927       }
19928       sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
19929             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
19930     }
19931   }
19932   return pNew;
19933 }
19934 
19935 /*
19936 ** Attempt to reallocate p.  If the reallocation fails, then free p
19937 ** and set the mallocFailed flag in the database connection.
19938 */
sqlite3DbReallocOrFree(sqlite3 * db,void * p,int n)19939 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
19940   void *pNew;
19941   pNew = sqlite3DbRealloc(db, p, n);
19942   if( !pNew ){
19943     sqlite3DbFree(db, p);
19944   }
19945   return pNew;
19946 }
19947 
19948 /*
19949 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
19950 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
19951 ** is because when memory debugging is turned on, these two functions are
19952 ** called via macros that record the current file and line number in the
19953 ** ThreadData structure.
19954 */
sqlite3DbStrDup(sqlite3 * db,const char * z)19955 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
19956   char *zNew;
19957   size_t n;
19958   if( z==0 ){
19959     return 0;
19960   }
19961   n = sqlite3Strlen30(z) + 1;
19962   assert( (n&0x7fffffff)==n );
19963   zNew = sqlite3DbMallocRaw(db, (int)n);
19964   if( zNew ){
19965     memcpy(zNew, z, n);
19966   }
19967   return zNew;
19968 }
sqlite3DbStrNDup(sqlite3 * db,const char * z,int n)19969 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
19970   char *zNew;
19971   if( z==0 ){
19972     return 0;
19973   }
19974   assert( (n&0x7fffffff)==n );
19975   zNew = sqlite3DbMallocRaw(db, n+1);
19976   if( zNew ){
19977     memcpy(zNew, z, n);
19978     zNew[n] = 0;
19979   }
19980   return zNew;
19981 }
19982 
19983 /*
19984 ** Create a string from the zFromat argument and the va_list that follows.
19985 ** Store the string in memory obtained from sqliteMalloc() and make *pz
19986 ** point to that string.
19987 */
sqlite3SetString(char ** pz,sqlite3 * db,const char * zFormat,...)19988 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
19989   va_list ap;
19990   char *z;
19991 
19992   va_start(ap, zFormat);
19993   z = sqlite3VMPrintf(db, zFormat, ap);
19994   va_end(ap);
19995   sqlite3DbFree(db, *pz);
19996   *pz = z;
19997 }
19998 
19999 
20000 /*
20001 ** This function must be called before exiting any API function (i.e.
20002 ** returning control to the user) that has called sqlite3_malloc or
20003 ** sqlite3_realloc.
20004 **
20005 ** The returned value is normally a copy of the second argument to this
20006 ** function. However, if a malloc() failure has occurred since the previous
20007 ** invocation SQLITE_NOMEM is returned instead.
20008 **
20009 ** If the first argument, db, is not NULL and a malloc() error has occurred,
20010 ** then the connection error-code (the value returned by sqlite3_errcode())
20011 ** is set to SQLITE_NOMEM.
20012 */
sqlite3ApiExit(sqlite3 * db,int rc)20013 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
20014   /* If the db handle is not NULL, then we must hold the connection handle
20015   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
20016   ** is unsafe, as is the call to sqlite3Error().
20017   */
20018   assert( !db || sqlite3_mutex_held(db->mutex) );
20019   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
20020     sqlite3Error(db, SQLITE_NOMEM, 0);
20021     db->mallocFailed = 0;
20022     rc = SQLITE_NOMEM;
20023   }
20024   return rc & (db ? db->errMask : 0xff);
20025 }
20026 
20027 /************** End of malloc.c **********************************************/
20028 /************** Begin file printf.c ******************************************/
20029 /*
20030 ** The "printf" code that follows dates from the 1980's.  It is in
20031 ** the public domain.  The original comments are included here for
20032 ** completeness.  They are very out-of-date but might be useful as
20033 ** an historical reference.  Most of the "enhancements" have been backed
20034 ** out so that the functionality is now the same as standard printf().
20035 **
20036 **************************************************************************
20037 **
20038 ** This file contains code for a set of "printf"-like routines.  These
20039 ** routines format strings much like the printf() from the standard C
20040 ** library, though the implementation here has enhancements to support
20041 ** SQLlite.
20042 */
20043 
20044 /*
20045 ** Conversion types fall into various categories as defined by the
20046 ** following enumeration.
20047 */
20048 #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
20049 #define etFLOAT       2 /* Floating point.  %f */
20050 #define etEXP         3 /* Exponentional notation. %e and %E */
20051 #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
20052 #define etSIZE        5 /* Return number of characters processed so far. %n */
20053 #define etSTRING      6 /* Strings. %s */
20054 #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
20055 #define etPERCENT     8 /* Percent symbol. %% */
20056 #define etCHARX       9 /* Characters. %c */
20057 /* The rest are extensions, not normally found in printf() */
20058 #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
20059 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
20060                           NULL pointers replaced by SQL NULL.  %Q */
20061 #define etTOKEN      12 /* a pointer to a Token structure */
20062 #define etSRCLIST    13 /* a pointer to a SrcList */
20063 #define etPOINTER    14 /* The %p conversion */
20064 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
20065 #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
20066 
20067 #define etINVALID     0 /* Any unrecognized conversion type */
20068 
20069 
20070 /*
20071 ** An "etByte" is an 8-bit unsigned value.
20072 */
20073 typedef unsigned char etByte;
20074 
20075 /*
20076 ** Each builtin conversion character (ex: the 'd' in "%d") is described
20077 ** by an instance of the following structure
20078 */
20079 typedef struct et_info {   /* Information about each format field */
20080   char fmttype;            /* The format field code letter */
20081   etByte base;             /* The base for radix conversion */
20082   etByte flags;            /* One or more of FLAG_ constants below */
20083   etByte type;             /* Conversion paradigm */
20084   etByte charset;          /* Offset into aDigits[] of the digits string */
20085   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
20086 } et_info;
20087 
20088 /*
20089 ** Allowed values for et_info.flags
20090 */
20091 #define FLAG_SIGNED  1     /* True if the value to convert is signed */
20092 #define FLAG_INTERN  2     /* True if for internal use only */
20093 #define FLAG_STRING  4     /* Allow infinity precision */
20094 
20095 
20096 /*
20097 ** The following table is searched linearly, so it is good to put the
20098 ** most frequently used conversion types first.
20099 */
20100 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
20101 static const char aPrefix[] = "-x0\000X0";
20102 static const et_info fmtinfo[] = {
20103   {  'd', 10, 1, etRADIX,      0,  0 },
20104   {  's',  0, 4, etSTRING,     0,  0 },
20105   {  'g',  0, 1, etGENERIC,    30, 0 },
20106   {  'z',  0, 4, etDYNSTRING,  0,  0 },
20107   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
20108   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
20109   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
20110   {  'c',  0, 0, etCHARX,      0,  0 },
20111   {  'o',  8, 0, etRADIX,      0,  2 },
20112   {  'u', 10, 0, etRADIX,      0,  0 },
20113   {  'x', 16, 0, etRADIX,      16, 1 },
20114   {  'X', 16, 0, etRADIX,      0,  4 },
20115 #ifndef SQLITE_OMIT_FLOATING_POINT
20116   {  'f',  0, 1, etFLOAT,      0,  0 },
20117   {  'e',  0, 1, etEXP,        30, 0 },
20118   {  'E',  0, 1, etEXP,        14, 0 },
20119   {  'G',  0, 1, etGENERIC,    14, 0 },
20120 #endif
20121   {  'i', 10, 1, etRADIX,      0,  0 },
20122   {  'n',  0, 0, etSIZE,       0,  0 },
20123   {  '%',  0, 0, etPERCENT,    0,  0 },
20124   {  'p', 16, 0, etPOINTER,    0,  1 },
20125 
20126 /* All the rest have the FLAG_INTERN bit set and are thus for internal
20127 ** use only */
20128   {  'T',  0, 2, etTOKEN,      0,  0 },
20129   {  'S',  0, 2, etSRCLIST,    0,  0 },
20130   {  'r', 10, 3, etORDINAL,    0,  0 },
20131 };
20132 
20133 /*
20134 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
20135 ** conversions will work.
20136 */
20137 #ifndef SQLITE_OMIT_FLOATING_POINT
20138 /*
20139 ** "*val" is a double such that 0.1 <= *val < 10.0
20140 ** Return the ascii code for the leading digit of *val, then
20141 ** multiply "*val" by 10.0 to renormalize.
20142 **
20143 ** Example:
20144 **     input:     *val = 3.14159
20145 **     output:    *val = 1.4159    function return = '3'
20146 **
20147 ** The counter *cnt is incremented each time.  After counter exceeds
20148 ** 16 (the number of significant digits in a 64-bit float) '0' is
20149 ** always returned.
20150 */
et_getdigit(LONGDOUBLE_TYPE * val,int * cnt)20151 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
20152   int digit;
20153   LONGDOUBLE_TYPE d;
20154   if( (*cnt)<=0 ) return '0';
20155   (*cnt)--;
20156   digit = (int)*val;
20157   d = digit;
20158   digit += '0';
20159   *val = (*val - d)*10.0;
20160   return (char)digit;
20161 }
20162 #endif /* SQLITE_OMIT_FLOATING_POINT */
20163 
20164 /*
20165 ** Append N space characters to the given string buffer.
20166 */
sqlite3AppendSpace(StrAccum * pAccum,int N)20167 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
20168   static const char zSpaces[] = "                             ";
20169   while( N>=(int)sizeof(zSpaces)-1 ){
20170     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
20171     N -= sizeof(zSpaces)-1;
20172   }
20173   if( N>0 ){
20174     sqlite3StrAccumAppend(pAccum, zSpaces, N);
20175   }
20176 }
20177 
20178 /*
20179 ** Set the StrAccum object to an error mode.
20180 */
setStrAccumError(StrAccum * p,u8 eError)20181 static void setStrAccumError(StrAccum *p, u8 eError){
20182   p->accError = eError;
20183   p->nAlloc = 0;
20184 }
20185 
20186 /*
20187 ** Extra argument values from a PrintfArguments object
20188 */
getIntArg(PrintfArguments * p)20189 static sqlite3_int64 getIntArg(PrintfArguments *p){
20190   if( p->nArg<=p->nUsed ) return 0;
20191   return sqlite3_value_int64(p->apArg[p->nUsed++]);
20192 }
getDoubleArg(PrintfArguments * p)20193 static double getDoubleArg(PrintfArguments *p){
20194   if( p->nArg<=p->nUsed ) return 0.0;
20195   return sqlite3_value_double(p->apArg[p->nUsed++]);
20196 }
getTextArg(PrintfArguments * p)20197 static char *getTextArg(PrintfArguments *p){
20198   if( p->nArg<=p->nUsed ) return 0;
20199   return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
20200 }
20201 
20202 
20203 /*
20204 ** On machines with a small stack size, you can redefine the
20205 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
20206 */
20207 #ifndef SQLITE_PRINT_BUF_SIZE
20208 # define SQLITE_PRINT_BUF_SIZE 70
20209 #endif
20210 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
20211 
20212 /*
20213 ** Render a string given by "fmt" into the StrAccum object.
20214 */
sqlite3VXPrintf(StrAccum * pAccum,u32 bFlags,const char * fmt,va_list ap)20215 SQLITE_PRIVATE void sqlite3VXPrintf(
20216   StrAccum *pAccum,          /* Accumulate results here */
20217   u32 bFlags,                /* SQLITE_PRINTF_* flags */
20218   const char *fmt,           /* Format string */
20219   va_list ap                 /* arguments */
20220 ){
20221   int c;                     /* Next character in the format string */
20222   char *bufpt;               /* Pointer to the conversion buffer */
20223   int precision;             /* Precision of the current field */
20224   int length;                /* Length of the field */
20225   int idx;                   /* A general purpose loop counter */
20226   int width;                 /* Width of the current field */
20227   etByte flag_leftjustify;   /* True if "-" flag is present */
20228   etByte flag_plussign;      /* True if "+" flag is present */
20229   etByte flag_blanksign;     /* True if " " flag is present */
20230   etByte flag_alternateform; /* True if "#" flag is present */
20231   etByte flag_altform2;      /* True if "!" flag is present */
20232   etByte flag_zeropad;       /* True if field width constant starts with zero */
20233   etByte flag_long;          /* True if "l" flag is present */
20234   etByte flag_longlong;      /* True if the "ll" flag is present */
20235   etByte done;               /* Loop termination flag */
20236   etByte xtype = 0;          /* Conversion paradigm */
20237   u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
20238   u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
20239   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
20240   sqlite_uint64 longvalue;   /* Value for integer types */
20241   LONGDOUBLE_TYPE realvalue; /* Value for real types */
20242   const et_info *infop;      /* Pointer to the appropriate info structure */
20243   char *zOut;                /* Rendering buffer */
20244   int nOut;                  /* Size of the rendering buffer */
20245   char *zExtra;              /* Malloced memory used by some conversion */
20246 #ifndef SQLITE_OMIT_FLOATING_POINT
20247   int  exp, e2;              /* exponent of real numbers */
20248   int nsd;                   /* Number of significant digits returned */
20249   double rounder;            /* Used for rounding floating point values */
20250   etByte flag_dp;            /* True if decimal point should be shown */
20251   etByte flag_rtz;           /* True if trailing zeros should be removed */
20252 #endif
20253   PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
20254   char buf[etBUFSIZE];       /* Conversion buffer */
20255 
20256   bufpt = 0;
20257   if( bFlags ){
20258     if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
20259       pArgList = va_arg(ap, PrintfArguments*);
20260     }
20261     useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
20262   }else{
20263     bArgList = useIntern = 0;
20264   }
20265   for(; (c=(*fmt))!=0; ++fmt){
20266     if( c!='%' ){
20267       int amt;
20268       bufpt = (char *)fmt;
20269       amt = 1;
20270       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
20271       sqlite3StrAccumAppend(pAccum, bufpt, amt);
20272       if( c==0 ) break;
20273     }
20274     if( (c=(*++fmt))==0 ){
20275       sqlite3StrAccumAppend(pAccum, "%", 1);
20276       break;
20277     }
20278     /* Find out what flags are present */
20279     flag_leftjustify = flag_plussign = flag_blanksign =
20280      flag_alternateform = flag_altform2 = flag_zeropad = 0;
20281     done = 0;
20282     do{
20283       switch( c ){
20284         case '-':   flag_leftjustify = 1;     break;
20285         case '+':   flag_plussign = 1;        break;
20286         case ' ':   flag_blanksign = 1;       break;
20287         case '#':   flag_alternateform = 1;   break;
20288         case '!':   flag_altform2 = 1;        break;
20289         case '0':   flag_zeropad = 1;         break;
20290         default:    done = 1;                 break;
20291       }
20292     }while( !done && (c=(*++fmt))!=0 );
20293     /* Get the field width */
20294     width = 0;
20295     if( c=='*' ){
20296       if( bArgList ){
20297         width = (int)getIntArg(pArgList);
20298       }else{
20299         width = va_arg(ap,int);
20300       }
20301       if( width<0 ){
20302         flag_leftjustify = 1;
20303         width = -width;
20304       }
20305       c = *++fmt;
20306     }else{
20307       while( c>='0' && c<='9' ){
20308         width = width*10 + c - '0';
20309         c = *++fmt;
20310       }
20311     }
20312     /* Get the precision */
20313     if( c=='.' ){
20314       precision = 0;
20315       c = *++fmt;
20316       if( c=='*' ){
20317         if( bArgList ){
20318           precision = (int)getIntArg(pArgList);
20319         }else{
20320           precision = va_arg(ap,int);
20321         }
20322         if( precision<0 ) precision = -precision;
20323         c = *++fmt;
20324       }else{
20325         while( c>='0' && c<='9' ){
20326           precision = precision*10 + c - '0';
20327           c = *++fmt;
20328         }
20329       }
20330     }else{
20331       precision = -1;
20332     }
20333     /* Get the conversion type modifier */
20334     if( c=='l' ){
20335       flag_long = 1;
20336       c = *++fmt;
20337       if( c=='l' ){
20338         flag_longlong = 1;
20339         c = *++fmt;
20340       }else{
20341         flag_longlong = 0;
20342       }
20343     }else{
20344       flag_long = flag_longlong = 0;
20345     }
20346     /* Fetch the info entry for the field */
20347     infop = &fmtinfo[0];
20348     xtype = etINVALID;
20349     for(idx=0; idx<ArraySize(fmtinfo); idx++){
20350       if( c==fmtinfo[idx].fmttype ){
20351         infop = &fmtinfo[idx];
20352         if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
20353           xtype = infop->type;
20354         }else{
20355           return;
20356         }
20357         break;
20358       }
20359     }
20360     zExtra = 0;
20361 
20362     /*
20363     ** At this point, variables are initialized as follows:
20364     **
20365     **   flag_alternateform          TRUE if a '#' is present.
20366     **   flag_altform2               TRUE if a '!' is present.
20367     **   flag_plussign               TRUE if a '+' is present.
20368     **   flag_leftjustify            TRUE if a '-' is present or if the
20369     **                               field width was negative.
20370     **   flag_zeropad                TRUE if the width began with 0.
20371     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
20372     **                               the conversion character.
20373     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
20374     **                               the conversion character.
20375     **   flag_blanksign              TRUE if a ' ' is present.
20376     **   width                       The specified field width.  This is
20377     **                               always non-negative.  Zero is the default.
20378     **   precision                   The specified precision.  The default
20379     **                               is -1.
20380     **   xtype                       The class of the conversion.
20381     **   infop                       Pointer to the appropriate info struct.
20382     */
20383     switch( xtype ){
20384       case etPOINTER:
20385         flag_longlong = sizeof(char*)==sizeof(i64);
20386         flag_long = sizeof(char*)==sizeof(long int);
20387         /* Fall through into the next case */
20388       case etORDINAL:
20389       case etRADIX:
20390         if( infop->flags & FLAG_SIGNED ){
20391           i64 v;
20392           if( bArgList ){
20393             v = getIntArg(pArgList);
20394           }else if( flag_longlong ){
20395             v = va_arg(ap,i64);
20396           }else if( flag_long ){
20397             v = va_arg(ap,long int);
20398           }else{
20399             v = va_arg(ap,int);
20400           }
20401           if( v<0 ){
20402             if( v==SMALLEST_INT64 ){
20403               longvalue = ((u64)1)<<63;
20404             }else{
20405               longvalue = -v;
20406             }
20407             prefix = '-';
20408           }else{
20409             longvalue = v;
20410             if( flag_plussign )        prefix = '+';
20411             else if( flag_blanksign )  prefix = ' ';
20412             else                       prefix = 0;
20413           }
20414         }else{
20415           if( bArgList ){
20416             longvalue = (u64)getIntArg(pArgList);
20417           }else if( flag_longlong ){
20418             longvalue = va_arg(ap,u64);
20419           }else if( flag_long ){
20420             longvalue = va_arg(ap,unsigned long int);
20421           }else{
20422             longvalue = va_arg(ap,unsigned int);
20423           }
20424           prefix = 0;
20425         }
20426         if( longvalue==0 ) flag_alternateform = 0;
20427         if( flag_zeropad && precision<width-(prefix!=0) ){
20428           precision = width-(prefix!=0);
20429         }
20430         if( precision<etBUFSIZE-10 ){
20431           nOut = etBUFSIZE;
20432           zOut = buf;
20433         }else{
20434           nOut = precision + 10;
20435           zOut = zExtra = sqlite3Malloc( nOut );
20436           if( zOut==0 ){
20437             setStrAccumError(pAccum, STRACCUM_NOMEM);
20438             return;
20439           }
20440         }
20441         bufpt = &zOut[nOut-1];
20442         if( xtype==etORDINAL ){
20443           static const char zOrd[] = "thstndrd";
20444           int x = (int)(longvalue % 10);
20445           if( x>=4 || (longvalue/10)%10==1 ){
20446             x = 0;
20447           }
20448           *(--bufpt) = zOrd[x*2+1];
20449           *(--bufpt) = zOrd[x*2];
20450         }
20451         {
20452           register const char *cset;      /* Use registers for speed */
20453           register int base;
20454           cset = &aDigits[infop->charset];
20455           base = infop->base;
20456           do{                                           /* Convert to ascii */
20457             *(--bufpt) = cset[longvalue%base];
20458             longvalue = longvalue/base;
20459           }while( longvalue>0 );
20460         }
20461         length = (int)(&zOut[nOut-1]-bufpt);
20462         for(idx=precision-length; idx>0; idx--){
20463           *(--bufpt) = '0';                             /* Zero pad */
20464         }
20465         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
20466         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
20467           const char *pre;
20468           char x;
20469           pre = &aPrefix[infop->prefix];
20470           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
20471         }
20472         length = (int)(&zOut[nOut-1]-bufpt);
20473         break;
20474       case etFLOAT:
20475       case etEXP:
20476       case etGENERIC:
20477         if( bArgList ){
20478           realvalue = getDoubleArg(pArgList);
20479         }else{
20480           realvalue = va_arg(ap,double);
20481         }
20482 #ifdef SQLITE_OMIT_FLOATING_POINT
20483         length = 0;
20484 #else
20485         if( precision<0 ) precision = 6;         /* Set default precision */
20486         if( realvalue<0.0 ){
20487           realvalue = -realvalue;
20488           prefix = '-';
20489         }else{
20490           if( flag_plussign )          prefix = '+';
20491           else if( flag_blanksign )    prefix = ' ';
20492           else                         prefix = 0;
20493         }
20494         if( xtype==etGENERIC && precision>0 ) precision--;
20495         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
20496         if( xtype==etFLOAT ) realvalue += rounder;
20497         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
20498         exp = 0;
20499         if( sqlite3IsNaN((double)realvalue) ){
20500           bufpt = "NaN";
20501           length = 3;
20502           break;
20503         }
20504         if( realvalue>0.0 ){
20505           LONGDOUBLE_TYPE scale = 1.0;
20506           while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
20507           while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
20508           while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
20509           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
20510           realvalue /= scale;
20511           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
20512           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
20513           if( exp>350 ){
20514             if( prefix=='-' ){
20515               bufpt = "-Inf";
20516             }else if( prefix=='+' ){
20517               bufpt = "+Inf";
20518             }else{
20519               bufpt = "Inf";
20520             }
20521             length = sqlite3Strlen30(bufpt);
20522             break;
20523           }
20524         }
20525         bufpt = buf;
20526         /*
20527         ** If the field type is etGENERIC, then convert to either etEXP
20528         ** or etFLOAT, as appropriate.
20529         */
20530         if( xtype!=etFLOAT ){
20531           realvalue += rounder;
20532           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
20533         }
20534         if( xtype==etGENERIC ){
20535           flag_rtz = !flag_alternateform;
20536           if( exp<-4 || exp>precision ){
20537             xtype = etEXP;
20538           }else{
20539             precision = precision - exp;
20540             xtype = etFLOAT;
20541           }
20542         }else{
20543           flag_rtz = flag_altform2;
20544         }
20545         if( xtype==etEXP ){
20546           e2 = 0;
20547         }else{
20548           e2 = exp;
20549         }
20550         if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
20551           bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
20552           if( bufpt==0 ){
20553             setStrAccumError(pAccum, STRACCUM_NOMEM);
20554             return;
20555           }
20556         }
20557         zOut = bufpt;
20558         nsd = 16 + flag_altform2*10;
20559         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
20560         /* The sign in front of the number */
20561         if( prefix ){
20562           *(bufpt++) = prefix;
20563         }
20564         /* Digits prior to the decimal point */
20565         if( e2<0 ){
20566           *(bufpt++) = '0';
20567         }else{
20568           for(; e2>=0; e2--){
20569             *(bufpt++) = et_getdigit(&realvalue,&nsd);
20570           }
20571         }
20572         /* The decimal point */
20573         if( flag_dp ){
20574           *(bufpt++) = '.';
20575         }
20576         /* "0" digits after the decimal point but before the first
20577         ** significant digit of the number */
20578         for(e2++; e2<0; precision--, e2++){
20579           assert( precision>0 );
20580           *(bufpt++) = '0';
20581         }
20582         /* Significant digits after the decimal point */
20583         while( (precision--)>0 ){
20584           *(bufpt++) = et_getdigit(&realvalue,&nsd);
20585         }
20586         /* Remove trailing zeros and the "." if no digits follow the "." */
20587         if( flag_rtz && flag_dp ){
20588           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
20589           assert( bufpt>zOut );
20590           if( bufpt[-1]=='.' ){
20591             if( flag_altform2 ){
20592               *(bufpt++) = '0';
20593             }else{
20594               *(--bufpt) = 0;
20595             }
20596           }
20597         }
20598         /* Add the "eNNN" suffix */
20599         if( xtype==etEXP ){
20600           *(bufpt++) = aDigits[infop->charset];
20601           if( exp<0 ){
20602             *(bufpt++) = '-'; exp = -exp;
20603           }else{
20604             *(bufpt++) = '+';
20605           }
20606           if( exp>=100 ){
20607             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
20608             exp %= 100;
20609           }
20610           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
20611           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
20612         }
20613         *bufpt = 0;
20614 
20615         /* The converted number is in buf[] and zero terminated. Output it.
20616         ** Note that the number is in the usual order, not reversed as with
20617         ** integer conversions. */
20618         length = (int)(bufpt-zOut);
20619         bufpt = zOut;
20620 
20621         /* Special case:  Add leading zeros if the flag_zeropad flag is
20622         ** set and we are not left justified */
20623         if( flag_zeropad && !flag_leftjustify && length < width){
20624           int i;
20625           int nPad = width - length;
20626           for(i=width; i>=nPad; i--){
20627             bufpt[i] = bufpt[i-nPad];
20628           }
20629           i = prefix!=0;
20630           while( nPad-- ) bufpt[i++] = '0';
20631           length = width;
20632         }
20633 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
20634         break;
20635       case etSIZE:
20636         if( !bArgList ){
20637           *(va_arg(ap,int*)) = pAccum->nChar;
20638         }
20639         length = width = 0;
20640         break;
20641       case etPERCENT:
20642         buf[0] = '%';
20643         bufpt = buf;
20644         length = 1;
20645         break;
20646       case etCHARX:
20647         if( bArgList ){
20648           bufpt = getTextArg(pArgList);
20649           c = bufpt ? bufpt[0] : 0;
20650         }else{
20651           c = va_arg(ap,int);
20652         }
20653         buf[0] = (char)c;
20654         if( precision>=0 ){
20655           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
20656           length = precision;
20657         }else{
20658           length =1;
20659         }
20660         bufpt = buf;
20661         break;
20662       case etSTRING:
20663       case etDYNSTRING:
20664         if( bArgList ){
20665           bufpt = getTextArg(pArgList);
20666         }else{
20667           bufpt = va_arg(ap,char*);
20668         }
20669         if( bufpt==0 ){
20670           bufpt = "";
20671         }else if( xtype==etDYNSTRING && !bArgList ){
20672           zExtra = bufpt;
20673         }
20674         if( precision>=0 ){
20675           for(length=0; length<precision && bufpt[length]; length++){}
20676         }else{
20677           length = sqlite3Strlen30(bufpt);
20678         }
20679         break;
20680       case etSQLESCAPE:
20681       case etSQLESCAPE2:
20682       case etSQLESCAPE3: {
20683         int i, j, k, n, isnull;
20684         int needQuote;
20685         char ch;
20686         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
20687         char *escarg;
20688 
20689         if( bArgList ){
20690           escarg = getTextArg(pArgList);
20691         }else{
20692           escarg = va_arg(ap,char*);
20693         }
20694         isnull = escarg==0;
20695         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
20696         k = precision;
20697         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
20698           if( ch==q )  n++;
20699         }
20700         needQuote = !isnull && xtype==etSQLESCAPE2;
20701         n += i + 1 + needQuote*2;
20702         if( n>etBUFSIZE ){
20703           bufpt = zExtra = sqlite3Malloc( n );
20704           if( bufpt==0 ){
20705             setStrAccumError(pAccum, STRACCUM_NOMEM);
20706             return;
20707           }
20708         }else{
20709           bufpt = buf;
20710         }
20711         j = 0;
20712         if( needQuote ) bufpt[j++] = q;
20713         k = i;
20714         for(i=0; i<k; i++){
20715           bufpt[j++] = ch = escarg[i];
20716           if( ch==q ) bufpt[j++] = ch;
20717         }
20718         if( needQuote ) bufpt[j++] = q;
20719         bufpt[j] = 0;
20720         length = j;
20721         /* The precision in %q and %Q means how many input characters to
20722         ** consume, not the length of the output...
20723         ** if( precision>=0 && precision<length ) length = precision; */
20724         break;
20725       }
20726       case etTOKEN: {
20727         Token *pToken = va_arg(ap, Token*);
20728         assert( bArgList==0 );
20729         if( pToken && pToken->n ){
20730           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
20731         }
20732         length = width = 0;
20733         break;
20734       }
20735       case etSRCLIST: {
20736         SrcList *pSrc = va_arg(ap, SrcList*);
20737         int k = va_arg(ap, int);
20738         struct SrcList_item *pItem = &pSrc->a[k];
20739         assert( bArgList==0 );
20740         assert( k>=0 && k<pSrc->nSrc );
20741         if( pItem->zDatabase ){
20742           sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
20743           sqlite3StrAccumAppend(pAccum, ".", 1);
20744         }
20745         sqlite3StrAccumAppendAll(pAccum, pItem->zName);
20746         length = width = 0;
20747         break;
20748       }
20749       default: {
20750         assert( xtype==etINVALID );
20751         return;
20752       }
20753     }/* End switch over the format type */
20754     /*
20755     ** The text of the conversion is pointed to by "bufpt" and is
20756     ** "length" characters long.  The field width is "width".  Do
20757     ** the output.
20758     */
20759     if( !flag_leftjustify ){
20760       register int nspace;
20761       nspace = width-length;
20762       if( nspace>0 ){
20763         sqlite3AppendSpace(pAccum, nspace);
20764       }
20765     }
20766     if( length>0 ){
20767       sqlite3StrAccumAppend(pAccum, bufpt, length);
20768     }
20769     if( flag_leftjustify ){
20770       register int nspace;
20771       nspace = width-length;
20772       if( nspace>0 ){
20773         sqlite3AppendSpace(pAccum, nspace);
20774       }
20775     }
20776     if( zExtra ) sqlite3_free(zExtra);
20777   }/* End for loop over the format string */
20778 } /* End of function */
20779 
20780 /*
20781 ** Append N bytes of text from z to the StrAccum object.
20782 */
sqlite3StrAccumAppend(StrAccum * p,const char * z,int N)20783 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
20784   assert( z!=0 );
20785   assert( p->zText!=0 || p->nChar==0 || p->accError );
20786   assert( N>=0 );
20787   assert( p->accError==0 || p->nAlloc==0 );
20788   if( p->nChar+N >= p->nAlloc ){
20789     char *zNew;
20790     if( p->accError ){
20791       testcase(p->accError==STRACCUM_TOOBIG);
20792       testcase(p->accError==STRACCUM_NOMEM);
20793       return;
20794     }
20795     if( !p->useMalloc ){
20796       N = p->nAlloc - p->nChar - 1;
20797       setStrAccumError(p, STRACCUM_TOOBIG);
20798       if( N<=0 ){
20799         return;
20800       }
20801     }else{
20802       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
20803       i64 szNew = p->nChar;
20804       szNew += N + 1;
20805       if( szNew > p->mxAlloc ){
20806         sqlite3StrAccumReset(p);
20807         setStrAccumError(p, STRACCUM_TOOBIG);
20808         return;
20809       }else{
20810         p->nAlloc = (int)szNew;
20811       }
20812       if( p->useMalloc==1 ){
20813         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
20814       }else{
20815         zNew = sqlite3_realloc(zOld, p->nAlloc);
20816       }
20817       if( zNew ){
20818         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
20819         p->zText = zNew;
20820       }else{
20821         sqlite3StrAccumReset(p);
20822         setStrAccumError(p, STRACCUM_NOMEM);
20823         return;
20824       }
20825     }
20826   }
20827   assert( p->zText );
20828   memcpy(&p->zText[p->nChar], z, N);
20829   p->nChar += N;
20830 }
20831 
20832 /*
20833 ** Append the complete text of zero-terminated string z[] to the p string.
20834 */
sqlite3StrAccumAppendAll(StrAccum * p,const char * z)20835 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
20836   sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
20837 }
20838 
20839 
20840 /*
20841 ** Finish off a string by making sure it is zero-terminated.
20842 ** Return a pointer to the resulting string.  Return a NULL
20843 ** pointer if any kind of error was encountered.
20844 */
sqlite3StrAccumFinish(StrAccum * p)20845 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
20846   if( p->zText ){
20847     p->zText[p->nChar] = 0;
20848     if( p->useMalloc && p->zText==p->zBase ){
20849       if( p->useMalloc==1 ){
20850         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
20851       }else{
20852         p->zText = sqlite3_malloc(p->nChar+1);
20853       }
20854       if( p->zText ){
20855         memcpy(p->zText, p->zBase, p->nChar+1);
20856       }else{
20857         setStrAccumError(p, STRACCUM_NOMEM);
20858       }
20859     }
20860   }
20861   return p->zText;
20862 }
20863 
20864 /*
20865 ** Reset an StrAccum string.  Reclaim all malloced memory.
20866 */
sqlite3StrAccumReset(StrAccum * p)20867 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
20868   if( p->zText!=p->zBase ){
20869     if( p->useMalloc==1 ){
20870       sqlite3DbFree(p->db, p->zText);
20871     }else{
20872       sqlite3_free(p->zText);
20873     }
20874   }
20875   p->zText = 0;
20876 }
20877 
20878 /*
20879 ** Initialize a string accumulator
20880 */
sqlite3StrAccumInit(StrAccum * p,char * zBase,int n,int mx)20881 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
20882   p->zText = p->zBase = zBase;
20883   p->db = 0;
20884   p->nChar = 0;
20885   p->nAlloc = n;
20886   p->mxAlloc = mx;
20887   p->useMalloc = 1;
20888   p->accError = 0;
20889 }
20890 
20891 /*
20892 ** Print into memory obtained from sqliteMalloc().  Use the internal
20893 ** %-conversion extensions.
20894 */
sqlite3VMPrintf(sqlite3 * db,const char * zFormat,va_list ap)20895 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
20896   char *z;
20897   char zBase[SQLITE_PRINT_BUF_SIZE];
20898   StrAccum acc;
20899   assert( db!=0 );
20900   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
20901                       db->aLimit[SQLITE_LIMIT_LENGTH]);
20902   acc.db = db;
20903   sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
20904   z = sqlite3StrAccumFinish(&acc);
20905   if( acc.accError==STRACCUM_NOMEM ){
20906     db->mallocFailed = 1;
20907   }
20908   return z;
20909 }
20910 
20911 /*
20912 ** Print into memory obtained from sqliteMalloc().  Use the internal
20913 ** %-conversion extensions.
20914 */
sqlite3MPrintf(sqlite3 * db,const char * zFormat,...)20915 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
20916   va_list ap;
20917   char *z;
20918   va_start(ap, zFormat);
20919   z = sqlite3VMPrintf(db, zFormat, ap);
20920   va_end(ap);
20921   return z;
20922 }
20923 
20924 /*
20925 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
20926 ** the string and before returnning.  This routine is intended to be used
20927 ** to modify an existing string.  For example:
20928 **
20929 **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
20930 **
20931 */
sqlite3MAppendf(sqlite3 * db,char * zStr,const char * zFormat,...)20932 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
20933   va_list ap;
20934   char *z;
20935   va_start(ap, zFormat);
20936   z = sqlite3VMPrintf(db, zFormat, ap);
20937   va_end(ap);
20938   sqlite3DbFree(db, zStr);
20939   return z;
20940 }
20941 
20942 /*
20943 ** Print into memory obtained from sqlite3_malloc().  Omit the internal
20944 ** %-conversion extensions.
20945 */
sqlite3_vmprintf(const char * zFormat,va_list ap)20946 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
20947   char *z;
20948   char zBase[SQLITE_PRINT_BUF_SIZE];
20949   StrAccum acc;
20950 #ifndef SQLITE_OMIT_AUTOINIT
20951   if( sqlite3_initialize() ) return 0;
20952 #endif
20953   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
20954   acc.useMalloc = 2;
20955   sqlite3VXPrintf(&acc, 0, zFormat, ap);
20956   z = sqlite3StrAccumFinish(&acc);
20957   return z;
20958 }
20959 
20960 /*
20961 ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
20962 ** %-conversion extensions.
20963 */
sqlite3_mprintf(const char * zFormat,...)20964 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
20965   va_list ap;
20966   char *z;
20967 #ifndef SQLITE_OMIT_AUTOINIT
20968   if( sqlite3_initialize() ) return 0;
20969 #endif
20970   va_start(ap, zFormat);
20971   z = sqlite3_vmprintf(zFormat, ap);
20972   va_end(ap);
20973   return z;
20974 }
20975 
20976 /*
20977 ** sqlite3_snprintf() works like snprintf() except that it ignores the
20978 ** current locale settings.  This is important for SQLite because we
20979 ** are not able to use a "," as the decimal point in place of "." as
20980 ** specified by some locales.
20981 **
20982 ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
20983 ** from the snprintf() standard.  Unfortunately, it is too late to change
20984 ** this without breaking compatibility, so we just have to live with the
20985 ** mistake.
20986 **
20987 ** sqlite3_vsnprintf() is the varargs version.
20988 */
sqlite3_vsnprintf(int n,char * zBuf,const char * zFormat,va_list ap)20989 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
20990   StrAccum acc;
20991   if( n<=0 ) return zBuf;
20992   sqlite3StrAccumInit(&acc, zBuf, n, 0);
20993   acc.useMalloc = 0;
20994   sqlite3VXPrintf(&acc, 0, zFormat, ap);
20995   return sqlite3StrAccumFinish(&acc);
20996 }
sqlite3_snprintf(int n,char * zBuf,const char * zFormat,...)20997 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
20998   char *z;
20999   va_list ap;
21000   va_start(ap,zFormat);
21001   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
21002   va_end(ap);
21003   return z;
21004 }
21005 
21006 /*
21007 ** This is the routine that actually formats the sqlite3_log() message.
21008 ** We house it in a separate routine from sqlite3_log() to avoid using
21009 ** stack space on small-stack systems when logging is disabled.
21010 **
21011 ** sqlite3_log() must render into a static buffer.  It cannot dynamically
21012 ** allocate memory because it might be called while the memory allocator
21013 ** mutex is held.
21014 */
renderLogMsg(int iErrCode,const char * zFormat,va_list ap)21015 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
21016   StrAccum acc;                          /* String accumulator */
21017   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
21018 
21019   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
21020   acc.useMalloc = 0;
21021   sqlite3VXPrintf(&acc, 0, zFormat, ap);
21022   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
21023                            sqlite3StrAccumFinish(&acc));
21024 }
21025 
21026 /*
21027 ** Format and write a message to the log if logging is enabled.
21028 */
sqlite3_log(int iErrCode,const char * zFormat,...)21029 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
21030   va_list ap;                             /* Vararg list */
21031   if( sqlite3GlobalConfig.xLog ){
21032     va_start(ap, zFormat);
21033     renderLogMsg(iErrCode, zFormat, ap);
21034     va_end(ap);
21035   }
21036 }
21037 
21038 #if defined(SQLITE_DEBUG)
21039 /*
21040 ** A version of printf() that understands %lld.  Used for debugging.
21041 ** The printf() built into some versions of windows does not understand %lld
21042 ** and segfaults if you give it a long long int.
21043 */
sqlite3DebugPrintf(const char * zFormat,...)21044 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
21045   va_list ap;
21046   StrAccum acc;
21047   char zBuf[500];
21048   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
21049   acc.useMalloc = 0;
21050   va_start(ap,zFormat);
21051   sqlite3VXPrintf(&acc, 0, zFormat, ap);
21052   va_end(ap);
21053   sqlite3StrAccumFinish(&acc);
21054   fprintf(stdout,"%s", zBuf);
21055   fflush(stdout);
21056 }
21057 #endif
21058 
21059 /*
21060 ** variable-argument wrapper around sqlite3VXPrintf().
21061 */
sqlite3XPrintf(StrAccum * p,u32 bFlags,const char * zFormat,...)21062 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
21063   va_list ap;
21064   va_start(ap,zFormat);
21065   sqlite3VXPrintf(p, bFlags, zFormat, ap);
21066   va_end(ap);
21067 }
21068 
21069 /************** End of printf.c **********************************************/
21070 /************** Begin file random.c ******************************************/
21071 /*
21072 ** 2001 September 15
21073 **
21074 ** The author disclaims copyright to this source code.  In place of
21075 ** a legal notice, here is a blessing:
21076 **
21077 **    May you do good and not evil.
21078 **    May you find forgiveness for yourself and forgive others.
21079 **    May you share freely, never taking more than you give.
21080 **
21081 *************************************************************************
21082 ** This file contains code to implement a pseudo-random number
21083 ** generator (PRNG) for SQLite.
21084 **
21085 ** Random numbers are used by some of the database backends in order
21086 ** to generate random integer keys for tables or random filenames.
21087 */
21088 
21089 
21090 /* All threads share a single random number generator.
21091 ** This structure is the current state of the generator.
21092 */
21093 static SQLITE_WSD struct sqlite3PrngType {
21094   unsigned char isInit;          /* True if initialized */
21095   unsigned char i, j;            /* State variables */
21096   unsigned char s[256];          /* State variables */
21097 } sqlite3Prng;
21098 
21099 /*
21100 ** Return N random bytes.
21101 */
sqlite3_randomness(int N,void * pBuf)21102 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
21103   unsigned char t;
21104   unsigned char *zBuf = pBuf;
21105 
21106   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
21107   ** state vector.  If writable static data is unsupported on the target,
21108   ** we have to locate the state vector at run-time.  In the more common
21109   ** case where writable static data is supported, wsdPrng can refer directly
21110   ** to the "sqlite3Prng" state vector declared above.
21111   */
21112 #ifdef SQLITE_OMIT_WSD
21113   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
21114 # define wsdPrng p[0]
21115 #else
21116 # define wsdPrng sqlite3Prng
21117 #endif
21118 
21119 #if SQLITE_THREADSAFE
21120   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
21121   sqlite3_mutex_enter(mutex);
21122 #endif
21123 
21124   if( N<=0 ){
21125     wsdPrng.isInit = 0;
21126     sqlite3_mutex_leave(mutex);
21127     return;
21128   }
21129 
21130   /* Initialize the state of the random number generator once,
21131   ** the first time this routine is called.  The seed value does
21132   ** not need to contain a lot of randomness since we are not
21133   ** trying to do secure encryption or anything like that...
21134   **
21135   ** Nothing in this file or anywhere else in SQLite does any kind of
21136   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
21137   ** number generator) not as an encryption device.
21138   */
21139   if( !wsdPrng.isInit ){
21140     int i;
21141     char k[256];
21142     wsdPrng.j = 0;
21143     wsdPrng.i = 0;
21144     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
21145     for(i=0; i<256; i++){
21146       wsdPrng.s[i] = (u8)i;
21147     }
21148     for(i=0; i<256; i++){
21149       wsdPrng.j += wsdPrng.s[i] + k[i];
21150       t = wsdPrng.s[wsdPrng.j];
21151       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
21152       wsdPrng.s[i] = t;
21153     }
21154     wsdPrng.isInit = 1;
21155   }
21156 
21157   assert( N>0 );
21158   do{
21159     wsdPrng.i++;
21160     t = wsdPrng.s[wsdPrng.i];
21161     wsdPrng.j += t;
21162     wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
21163     wsdPrng.s[wsdPrng.j] = t;
21164     t += wsdPrng.s[wsdPrng.i];
21165     *(zBuf++) = wsdPrng.s[t];
21166   }while( --N );
21167   sqlite3_mutex_leave(mutex);
21168 }
21169 
21170 #ifndef SQLITE_OMIT_BUILTIN_TEST
21171 /*
21172 ** For testing purposes, we sometimes want to preserve the state of
21173 ** PRNG and restore the PRNG to its saved state at a later time, or
21174 ** to reset the PRNG to its initial state.  These routines accomplish
21175 ** those tasks.
21176 **
21177 ** The sqlite3_test_control() interface calls these routines to
21178 ** control the PRNG.
21179 */
21180 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
sqlite3PrngSaveState(void)21181 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
21182   memcpy(
21183     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
21184     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
21185     sizeof(sqlite3Prng)
21186   );
21187 }
sqlite3PrngRestoreState(void)21188 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
21189   memcpy(
21190     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
21191     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
21192     sizeof(sqlite3Prng)
21193   );
21194 }
21195 #endif /* SQLITE_OMIT_BUILTIN_TEST */
21196 
21197 /************** End of random.c **********************************************/
21198 /************** Begin file utf.c *********************************************/
21199 /*
21200 ** 2004 April 13
21201 **
21202 ** The author disclaims copyright to this source code.  In place of
21203 ** a legal notice, here is a blessing:
21204 **
21205 **    May you do good and not evil.
21206 **    May you find forgiveness for yourself and forgive others.
21207 **    May you share freely, never taking more than you give.
21208 **
21209 *************************************************************************
21210 ** This file contains routines used to translate between UTF-8,
21211 ** UTF-16, UTF-16BE, and UTF-16LE.
21212 **
21213 ** Notes on UTF-8:
21214 **
21215 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
21216 **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
21217 **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
21218 **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
21219 **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
21220 **
21221 **
21222 ** Notes on UTF-16:  (with wwww+1==uuuuu)
21223 **
21224 **      Word-0               Word-1          Value
21225 **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
21226 **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
21227 **
21228 **
21229 ** BOM or Byte Order Mark:
21230 **     0xff 0xfe   little-endian utf-16 follows
21231 **     0xfe 0xff   big-endian utf-16 follows
21232 **
21233 */
21234 /* #include <assert.h> */
21235 
21236 #ifndef SQLITE_AMALGAMATION
21237 /*
21238 ** The following constant value is used by the SQLITE_BIGENDIAN and
21239 ** SQLITE_LITTLEENDIAN macros.
21240 */
21241 SQLITE_PRIVATE const int sqlite3one = 1;
21242 #endif /* SQLITE_AMALGAMATION */
21243 
21244 /*
21245 ** This lookup table is used to help decode the first byte of
21246 ** a multi-byte UTF8 character.
21247 */
21248 static const unsigned char sqlite3Utf8Trans1[] = {
21249   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21250   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
21251   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
21252   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
21253   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21254   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
21255   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21256   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
21257 };
21258 
21259 
21260 #define WRITE_UTF8(zOut, c) {                          \
21261   if( c<0x00080 ){                                     \
21262     *zOut++ = (u8)(c&0xFF);                            \
21263   }                                                    \
21264   else if( c<0x00800 ){                                \
21265     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
21266     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
21267   }                                                    \
21268   else if( c<0x10000 ){                                \
21269     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
21270     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
21271     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
21272   }else{                                               \
21273     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
21274     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
21275     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
21276     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
21277   }                                                    \
21278 }
21279 
21280 #define WRITE_UTF16LE(zOut, c) {                                    \
21281   if( c<=0xFFFF ){                                                  \
21282     *zOut++ = (u8)(c&0x00FF);                                       \
21283     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
21284   }else{                                                            \
21285     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
21286     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
21287     *zOut++ = (u8)(c&0x00FF);                                       \
21288     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
21289   }                                                                 \
21290 }
21291 
21292 #define WRITE_UTF16BE(zOut, c) {                                    \
21293   if( c<=0xFFFF ){                                                  \
21294     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
21295     *zOut++ = (u8)(c&0x00FF);                                       \
21296   }else{                                                            \
21297     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
21298     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
21299     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
21300     *zOut++ = (u8)(c&0x00FF);                                       \
21301   }                                                                 \
21302 }
21303 
21304 #define READ_UTF16LE(zIn, TERM, c){                                   \
21305   c = (*zIn++);                                                       \
21306   c += ((*zIn++)<<8);                                                 \
21307   if( c>=0xD800 && c<0xE000 && TERM ){                                \
21308     int c2 = (*zIn++);                                                \
21309     c2 += ((*zIn++)<<8);                                              \
21310     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
21311   }                                                                   \
21312 }
21313 
21314 #define READ_UTF16BE(zIn, TERM, c){                                   \
21315   c = ((*zIn++)<<8);                                                  \
21316   c += (*zIn++);                                                      \
21317   if( c>=0xD800 && c<0xE000 && TERM ){                                \
21318     int c2 = ((*zIn++)<<8);                                           \
21319     c2 += (*zIn++);                                                   \
21320     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
21321   }                                                                   \
21322 }
21323 
21324 /*
21325 ** Translate a single UTF-8 character.  Return the unicode value.
21326 **
21327 ** During translation, assume that the byte that zTerm points
21328 ** is a 0x00.
21329 **
21330 ** Write a pointer to the next unread byte back into *pzNext.
21331 **
21332 ** Notes On Invalid UTF-8:
21333 **
21334 **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
21335 **     be encoded as a multi-byte character.  Any multi-byte character that
21336 **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
21337 **
21338 **  *  This routine never allows a UTF16 surrogate value to be encoded.
21339 **     If a multi-byte character attempts to encode a value between
21340 **     0xd800 and 0xe000 then it is rendered as 0xfffd.
21341 **
21342 **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
21343 **     byte of a character are interpreted as single-byte characters
21344 **     and rendered as themselves even though they are technically
21345 **     invalid characters.
21346 **
21347 **  *  This routine accepts an infinite number of different UTF8 encodings
21348 **     for unicode values 0x80 and greater.  It do not change over-length
21349 **     encodings to 0xfffd as some systems recommend.
21350 */
21351 #define READ_UTF8(zIn, zTerm, c)                           \
21352   c = *(zIn++);                                            \
21353   if( c>=0xc0 ){                                           \
21354     c = sqlite3Utf8Trans1[c-0xc0];                         \
21355     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
21356       c = (c<<6) + (0x3f & *(zIn++));                      \
21357     }                                                      \
21358     if( c<0x80                                             \
21359         || (c&0xFFFFF800)==0xD800                          \
21360         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
21361   }
sqlite3Utf8Read(const unsigned char ** pz)21362 SQLITE_PRIVATE u32 sqlite3Utf8Read(
21363   const unsigned char **pz    /* Pointer to string from which to read char */
21364 ){
21365   unsigned int c;
21366 
21367   /* Same as READ_UTF8() above but without the zTerm parameter.
21368   ** For this routine, we assume the UTF8 string is always zero-terminated.
21369   */
21370   c = *((*pz)++);
21371   if( c>=0xc0 ){
21372     c = sqlite3Utf8Trans1[c-0xc0];
21373     while( (*(*pz) & 0xc0)==0x80 ){
21374       c = (c<<6) + (0x3f & *((*pz)++));
21375     }
21376     if( c<0x80
21377         || (c&0xFFFFF800)==0xD800
21378         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
21379   }
21380   return c;
21381 }
21382 
21383 
21384 
21385 
21386 /*
21387 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
21388 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
21389 */
21390 /* #define TRANSLATE_TRACE 1 */
21391 
21392 #ifndef SQLITE_OMIT_UTF16
21393 /*
21394 ** This routine transforms the internal text encoding used by pMem to
21395 ** desiredEnc. It is an error if the string is already of the desired
21396 ** encoding, or if *pMem does not contain a string value.
21397 */
sqlite3VdbeMemTranslate(Mem * pMem,u8 desiredEnc)21398 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
21399   int len;                    /* Maximum length of output string in bytes */
21400   unsigned char *zOut;                  /* Output buffer */
21401   unsigned char *zIn;                   /* Input iterator */
21402   unsigned char *zTerm;                 /* End of input */
21403   unsigned char *z;                     /* Output iterator */
21404   unsigned int c;
21405 
21406   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
21407   assert( pMem->flags&MEM_Str );
21408   assert( pMem->enc!=desiredEnc );
21409   assert( pMem->enc!=0 );
21410   assert( pMem->n>=0 );
21411 
21412 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
21413   {
21414     char zBuf[100];
21415     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
21416     fprintf(stderr, "INPUT:  %s\n", zBuf);
21417   }
21418 #endif
21419 
21420   /* If the translation is between UTF-16 little and big endian, then
21421   ** all that is required is to swap the byte order. This case is handled
21422   ** differently from the others.
21423   */
21424   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
21425     u8 temp;
21426     int rc;
21427     rc = sqlite3VdbeMemMakeWriteable(pMem);
21428     if( rc!=SQLITE_OK ){
21429       assert( rc==SQLITE_NOMEM );
21430       return SQLITE_NOMEM;
21431     }
21432     zIn = (u8*)pMem->z;
21433     zTerm = &zIn[pMem->n&~1];
21434     while( zIn<zTerm ){
21435       temp = *zIn;
21436       *zIn = *(zIn+1);
21437       zIn++;
21438       *zIn++ = temp;
21439     }
21440     pMem->enc = desiredEnc;
21441     goto translate_out;
21442   }
21443 
21444   /* Set len to the maximum number of bytes required in the output buffer. */
21445   if( desiredEnc==SQLITE_UTF8 ){
21446     /* When converting from UTF-16, the maximum growth results from
21447     ** translating a 2-byte character to a 4-byte UTF-8 character.
21448     ** A single byte is required for the output string
21449     ** nul-terminator.
21450     */
21451     pMem->n &= ~1;
21452     len = pMem->n * 2 + 1;
21453   }else{
21454     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
21455     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
21456     ** character. Two bytes are required in the output buffer for the
21457     ** nul-terminator.
21458     */
21459     len = pMem->n * 2 + 2;
21460   }
21461 
21462   /* Set zIn to point at the start of the input buffer and zTerm to point 1
21463   ** byte past the end.
21464   **
21465   ** Variable zOut is set to point at the output buffer, space obtained
21466   ** from sqlite3_malloc().
21467   */
21468   zIn = (u8*)pMem->z;
21469   zTerm = &zIn[pMem->n];
21470   zOut = sqlite3DbMallocRaw(pMem->db, len);
21471   if( !zOut ){
21472     return SQLITE_NOMEM;
21473   }
21474   z = zOut;
21475 
21476   if( pMem->enc==SQLITE_UTF8 ){
21477     if( desiredEnc==SQLITE_UTF16LE ){
21478       /* UTF-8 -> UTF-16 Little-endian */
21479       while( zIn<zTerm ){
21480         READ_UTF8(zIn, zTerm, c);
21481         WRITE_UTF16LE(z, c);
21482       }
21483     }else{
21484       assert( desiredEnc==SQLITE_UTF16BE );
21485       /* UTF-8 -> UTF-16 Big-endian */
21486       while( zIn<zTerm ){
21487         READ_UTF8(zIn, zTerm, c);
21488         WRITE_UTF16BE(z, c);
21489       }
21490     }
21491     pMem->n = (int)(z - zOut);
21492     *z++ = 0;
21493   }else{
21494     assert( desiredEnc==SQLITE_UTF8 );
21495     if( pMem->enc==SQLITE_UTF16LE ){
21496       /* UTF-16 Little-endian -> UTF-8 */
21497       while( zIn<zTerm ){
21498         READ_UTF16LE(zIn, zIn<zTerm, c);
21499         WRITE_UTF8(z, c);
21500       }
21501     }else{
21502       /* UTF-16 Big-endian -> UTF-8 */
21503       while( zIn<zTerm ){
21504         READ_UTF16BE(zIn, zIn<zTerm, c);
21505         WRITE_UTF8(z, c);
21506       }
21507     }
21508     pMem->n = (int)(z - zOut);
21509   }
21510   *z = 0;
21511   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
21512 
21513   sqlite3VdbeMemRelease(pMem);
21514   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
21515   pMem->enc = desiredEnc;
21516   pMem->flags |= (MEM_Term);
21517   pMem->z = (char*)zOut;
21518   pMem->zMalloc = pMem->z;
21519 
21520 translate_out:
21521 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
21522   {
21523     char zBuf[100];
21524     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
21525     fprintf(stderr, "OUTPUT: %s\n", zBuf);
21526   }
21527 #endif
21528   return SQLITE_OK;
21529 }
21530 
21531 /*
21532 ** This routine checks for a byte-order mark at the beginning of the
21533 ** UTF-16 string stored in *pMem. If one is present, it is removed and
21534 ** the encoding of the Mem adjusted. This routine does not do any
21535 ** byte-swapping, it just sets Mem.enc appropriately.
21536 **
21537 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
21538 ** changed by this function.
21539 */
sqlite3VdbeMemHandleBom(Mem * pMem)21540 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
21541   int rc = SQLITE_OK;
21542   u8 bom = 0;
21543 
21544   assert( pMem->n>=0 );
21545   if( pMem->n>1 ){
21546     u8 b1 = *(u8 *)pMem->z;
21547     u8 b2 = *(((u8 *)pMem->z) + 1);
21548     if( b1==0xFE && b2==0xFF ){
21549       bom = SQLITE_UTF16BE;
21550     }
21551     if( b1==0xFF && b2==0xFE ){
21552       bom = SQLITE_UTF16LE;
21553     }
21554   }
21555 
21556   if( bom ){
21557     rc = sqlite3VdbeMemMakeWriteable(pMem);
21558     if( rc==SQLITE_OK ){
21559       pMem->n -= 2;
21560       memmove(pMem->z, &pMem->z[2], pMem->n);
21561       pMem->z[pMem->n] = '\0';
21562       pMem->z[pMem->n+1] = '\0';
21563       pMem->flags |= MEM_Term;
21564       pMem->enc = bom;
21565     }
21566   }
21567   return rc;
21568 }
21569 #endif /* SQLITE_OMIT_UTF16 */
21570 
21571 /*
21572 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
21573 ** return the number of unicode characters in pZ up to (but not including)
21574 ** the first 0x00 byte. If nByte is not less than zero, return the
21575 ** number of unicode characters in the first nByte of pZ (or up to
21576 ** the first 0x00, whichever comes first).
21577 */
sqlite3Utf8CharLen(const char * zIn,int nByte)21578 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
21579   int r = 0;
21580   const u8 *z = (const u8*)zIn;
21581   const u8 *zTerm;
21582   if( nByte>=0 ){
21583     zTerm = &z[nByte];
21584   }else{
21585     zTerm = (const u8*)(-1);
21586   }
21587   assert( z<=zTerm );
21588   while( *z!=0 && z<zTerm ){
21589     SQLITE_SKIP_UTF8(z);
21590     r++;
21591   }
21592   return r;
21593 }
21594 
21595 /* This test function is not currently used by the automated test-suite.
21596 ** Hence it is only available in debug builds.
21597 */
21598 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
21599 /*
21600 ** Translate UTF-8 to UTF-8.
21601 **
21602 ** This has the effect of making sure that the string is well-formed
21603 ** UTF-8.  Miscoded characters are removed.
21604 **
21605 ** The translation is done in-place and aborted if the output
21606 ** overruns the input.
21607 */
sqlite3Utf8To8(unsigned char * zIn)21608 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
21609   unsigned char *zOut = zIn;
21610   unsigned char *zStart = zIn;
21611   u32 c;
21612 
21613   while( zIn[0] && zOut<=zIn ){
21614     c = sqlite3Utf8Read((const u8**)&zIn);
21615     if( c!=0xfffd ){
21616       WRITE_UTF8(zOut, c);
21617     }
21618   }
21619   *zOut = 0;
21620   return (int)(zOut - zStart);
21621 }
21622 #endif
21623 
21624 #ifndef SQLITE_OMIT_UTF16
21625 /*
21626 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
21627 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
21628 ** be freed by the calling function.
21629 **
21630 ** NULL is returned if there is an allocation error.
21631 */
sqlite3Utf16to8(sqlite3 * db,const void * z,int nByte,u8 enc)21632 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
21633   Mem m;
21634   memset(&m, 0, sizeof(m));
21635   m.db = db;
21636   sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
21637   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
21638   if( db->mallocFailed ){
21639     sqlite3VdbeMemRelease(&m);
21640     m.z = 0;
21641   }
21642   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
21643   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
21644   assert( m.z || db->mallocFailed );
21645   return m.z;
21646 }
21647 
21648 /*
21649 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
21650 ** Return the number of bytes in the first nChar unicode characters
21651 ** in pZ.  nChar must be non-negative.
21652 */
sqlite3Utf16ByteLen(const void * zIn,int nChar)21653 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
21654   int c;
21655   unsigned char const *z = zIn;
21656   int n = 0;
21657 
21658   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
21659     while( n<nChar ){
21660       READ_UTF16BE(z, 1, c);
21661       n++;
21662     }
21663   }else{
21664     while( n<nChar ){
21665       READ_UTF16LE(z, 1, c);
21666       n++;
21667     }
21668   }
21669   return (int)(z-(unsigned char const *)zIn);
21670 }
21671 
21672 #if defined(SQLITE_TEST)
21673 /*
21674 ** This routine is called from the TCL test function "translate_selftest".
21675 ** It checks that the primitives for serializing and deserializing
21676 ** characters in each encoding are inverses of each other.
21677 */
sqlite3UtfSelfTest(void)21678 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
21679   unsigned int i, t;
21680   unsigned char zBuf[20];
21681   unsigned char *z;
21682   int n;
21683   unsigned int c;
21684 
21685   for(i=0; i<0x00110000; i++){
21686     z = zBuf;
21687     WRITE_UTF8(z, i);
21688     n = (int)(z-zBuf);
21689     assert( n>0 && n<=4 );
21690     z[0] = 0;
21691     z = zBuf;
21692     c = sqlite3Utf8Read((const u8**)&z);
21693     t = i;
21694     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
21695     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
21696     assert( c==t );
21697     assert( (z-zBuf)==n );
21698   }
21699   for(i=0; i<0x00110000; i++){
21700     if( i>=0xD800 && i<0xE000 ) continue;
21701     z = zBuf;
21702     WRITE_UTF16LE(z, i);
21703     n = (int)(z-zBuf);
21704     assert( n>0 && n<=4 );
21705     z[0] = 0;
21706     z = zBuf;
21707     READ_UTF16LE(z, 1, c);
21708     assert( c==i );
21709     assert( (z-zBuf)==n );
21710   }
21711   for(i=0; i<0x00110000; i++){
21712     if( i>=0xD800 && i<0xE000 ) continue;
21713     z = zBuf;
21714     WRITE_UTF16BE(z, i);
21715     n = (int)(z-zBuf);
21716     assert( n>0 && n<=4 );
21717     z[0] = 0;
21718     z = zBuf;
21719     READ_UTF16BE(z, 1, c);
21720     assert( c==i );
21721     assert( (z-zBuf)==n );
21722   }
21723 }
21724 #endif /* SQLITE_TEST */
21725 #endif /* SQLITE_OMIT_UTF16 */
21726 
21727 /************** End of utf.c *************************************************/
21728 /************** Begin file util.c ********************************************/
21729 /*
21730 ** 2001 September 15
21731 **
21732 ** The author disclaims copyright to this source code.  In place of
21733 ** a legal notice, here is a blessing:
21734 **
21735 **    May you do good and not evil.
21736 **    May you find forgiveness for yourself and forgive others.
21737 **    May you share freely, never taking more than you give.
21738 **
21739 *************************************************************************
21740 ** Utility functions used throughout sqlite.
21741 **
21742 ** This file contains functions for allocating memory, comparing
21743 ** strings, and stuff like that.
21744 **
21745 */
21746 /* #include <stdarg.h> */
21747 #ifdef SQLITE_HAVE_ISNAN
21748 # include <math.h>
21749 #endif
21750 
21751 /*
21752 ** Routine needed to support the testcase() macro.
21753 */
21754 #ifdef SQLITE_COVERAGE_TEST
sqlite3Coverage(int x)21755 SQLITE_PRIVATE void sqlite3Coverage(int x){
21756   static unsigned dummy = 0;
21757   dummy += (unsigned)x;
21758 }
21759 #endif
21760 
21761 #ifndef SQLITE_OMIT_FLOATING_POINT
21762 /*
21763 ** Return true if the floating point value is Not a Number (NaN).
21764 **
21765 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
21766 ** Otherwise, we have our own implementation that works on most systems.
21767 */
sqlite3IsNaN(double x)21768 SQLITE_PRIVATE int sqlite3IsNaN(double x){
21769   int rc;   /* The value return */
21770 #if !defined(SQLITE_HAVE_ISNAN)
21771   /*
21772   ** Systems that support the isnan() library function should probably
21773   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
21774   ** found that many systems do not have a working isnan() function so
21775   ** this implementation is provided as an alternative.
21776   **
21777   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
21778   ** On the other hand, the use of -ffast-math comes with the following
21779   ** warning:
21780   **
21781   **      This option [-ffast-math] should never be turned on by any
21782   **      -O option since it can result in incorrect output for programs
21783   **      which depend on an exact implementation of IEEE or ISO
21784   **      rules/specifications for math functions.
21785   **
21786   ** Under MSVC, this NaN test may fail if compiled with a floating-
21787   ** point precision mode other than /fp:precise.  From the MSDN
21788   ** documentation:
21789   **
21790   **      The compiler [with /fp:precise] will properly handle comparisons
21791   **      involving NaN. For example, x != x evaluates to true if x is NaN
21792   **      ...
21793   */
21794 #ifdef __FAST_MATH__
21795 # error SQLite will not work correctly with the -ffast-math option of GCC.
21796 #endif
21797   volatile double y = x;
21798   volatile double z = y;
21799   rc = (y!=z);
21800 #else  /* if defined(SQLITE_HAVE_ISNAN) */
21801   rc = isnan(x);
21802 #endif /* SQLITE_HAVE_ISNAN */
21803   testcase( rc );
21804   return rc;
21805 }
21806 #endif /* SQLITE_OMIT_FLOATING_POINT */
21807 
21808 /*
21809 ** Compute a string length that is limited to what can be stored in
21810 ** lower 30 bits of a 32-bit signed integer.
21811 **
21812 ** The value returned will never be negative.  Nor will it ever be greater
21813 ** than the actual length of the string.  For very long strings (greater
21814 ** than 1GiB) the value returned might be less than the true string length.
21815 */
sqlite3Strlen30(const char * z)21816 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
21817   const char *z2 = z;
21818   if( z==0 ) return 0;
21819   while( *z2 ){ z2++; }
21820   return 0x3fffffff & (int)(z2 - z);
21821 }
21822 
21823 /*
21824 ** Set the most recent error code and error string for the sqlite
21825 ** handle "db". The error code is set to "err_code".
21826 **
21827 ** If it is not NULL, string zFormat specifies the format of the
21828 ** error string in the style of the printf functions: The following
21829 ** format characters are allowed:
21830 **
21831 **      %s      Insert a string
21832 **      %z      A string that should be freed after use
21833 **      %d      Insert an integer
21834 **      %T      Insert a token
21835 **      %S      Insert the first element of a SrcList
21836 **
21837 ** zFormat and any string tokens that follow it are assumed to be
21838 ** encoded in UTF-8.
21839 **
21840 ** To clear the most recent error for sqlite handle "db", sqlite3Error
21841 ** should be called with err_code set to SQLITE_OK and zFormat set
21842 ** to NULL.
21843 */
sqlite3Error(sqlite3 * db,int err_code,const char * zFormat,...)21844 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
21845   assert( db!=0 );
21846   db->errCode = err_code;
21847   if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
21848     char *z;
21849     va_list ap;
21850     va_start(ap, zFormat);
21851     z = sqlite3VMPrintf(db, zFormat, ap);
21852     va_end(ap);
21853     sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
21854   }else if( db->pErr ){
21855     sqlite3ValueSetNull(db->pErr);
21856   }
21857 }
21858 
21859 /*
21860 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
21861 ** The following formatting characters are allowed:
21862 **
21863 **      %s      Insert a string
21864 **      %z      A string that should be freed after use
21865 **      %d      Insert an integer
21866 **      %T      Insert a token
21867 **      %S      Insert the first element of a SrcList
21868 **
21869 ** This function should be used to report any error that occurs whilst
21870 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
21871 ** last thing the sqlite3_prepare() function does is copy the error
21872 ** stored by this function into the database handle using sqlite3Error().
21873 ** Function sqlite3Error() should be used during statement execution
21874 ** (sqlite3_step() etc.).
21875 */
sqlite3ErrorMsg(Parse * pParse,const char * zFormat,...)21876 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
21877   char *zMsg;
21878   va_list ap;
21879   sqlite3 *db = pParse->db;
21880   va_start(ap, zFormat);
21881   zMsg = sqlite3VMPrintf(db, zFormat, ap);
21882   va_end(ap);
21883   if( db->suppressErr ){
21884     sqlite3DbFree(db, zMsg);
21885   }else{
21886     pParse->nErr++;
21887     sqlite3DbFree(db, pParse->zErrMsg);
21888     pParse->zErrMsg = zMsg;
21889     pParse->rc = SQLITE_ERROR;
21890   }
21891 }
21892 
21893 /*
21894 ** Convert an SQL-style quoted string into a normal string by removing
21895 ** the quote characters.  The conversion is done in-place.  If the
21896 ** input does not begin with a quote character, then this routine
21897 ** is a no-op.
21898 **
21899 ** The input string must be zero-terminated.  A new zero-terminator
21900 ** is added to the dequoted string.
21901 **
21902 ** The return value is -1 if no dequoting occurs or the length of the
21903 ** dequoted string, exclusive of the zero terminator, if dequoting does
21904 ** occur.
21905 **
21906 ** 2002-Feb-14: This routine is extended to remove MS-Access style
21907 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
21908 ** "a-b-c".
21909 */
sqlite3Dequote(char * z)21910 SQLITE_PRIVATE int sqlite3Dequote(char *z){
21911   char quote;
21912   int i, j;
21913   if( z==0 ) return -1;
21914   quote = z[0];
21915   switch( quote ){
21916     case '\'':  break;
21917     case '"':   break;
21918     case '`':   break;                /* For MySQL compatibility */
21919     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
21920     default:    return -1;
21921   }
21922   for(i=1, j=0;; i++){
21923     assert( z[i] );
21924     if( z[i]==quote ){
21925       if( z[i+1]==quote ){
21926         z[j++] = quote;
21927         i++;
21928       }else{
21929         break;
21930       }
21931     }else{
21932       z[j++] = z[i];
21933     }
21934   }
21935   z[j] = 0;
21936   return j;
21937 }
21938 
21939 /* Convenient short-hand */
21940 #define UpperToLower sqlite3UpperToLower
21941 
21942 /*
21943 ** Some systems have stricmp().  Others have strcasecmp().  Because
21944 ** there is no consistency, we will define our own.
21945 **
21946 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
21947 ** sqlite3_strnicmp() APIs allow applications and extensions to compare
21948 ** the contents of two buffers containing UTF-8 strings in a
21949 ** case-independent fashion, using the same definition of "case
21950 ** independence" that SQLite uses internally when comparing identifiers.
21951 */
sqlite3_stricmp(const char * zLeft,const char * zRight)21952 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
21953   register unsigned char *a, *b;
21954   a = (unsigned char *)zLeft;
21955   b = (unsigned char *)zRight;
21956   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
21957   return UpperToLower[*a] - UpperToLower[*b];
21958 }
sqlite3_strnicmp(const char * zLeft,const char * zRight,int N)21959 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
21960   register unsigned char *a, *b;
21961   a = (unsigned char *)zLeft;
21962   b = (unsigned char *)zRight;
21963   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
21964   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
21965 }
21966 
21967 /*
21968 ** The string z[] is an text representation of a real number.
21969 ** Convert this string to a double and write it into *pResult.
21970 **
21971 ** The string z[] is length bytes in length (bytes, not characters) and
21972 ** uses the encoding enc.  The string is not necessarily zero-terminated.
21973 **
21974 ** Return TRUE if the result is a valid real number (or integer) and FALSE
21975 ** if the string is empty or contains extraneous text.  Valid numbers
21976 ** are in one of these formats:
21977 **
21978 **    [+-]digits[E[+-]digits]
21979 **    [+-]digits.[digits][E[+-]digits]
21980 **    [+-].digits[E[+-]digits]
21981 **
21982 ** Leading and trailing whitespace is ignored for the purpose of determining
21983 ** validity.
21984 **
21985 ** If some prefix of the input string is a valid number, this routine
21986 ** returns FALSE but it still converts the prefix and writes the result
21987 ** into *pResult.
21988 */
sqlite3AtoF(const char * z,double * pResult,int length,u8 enc)21989 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
21990 #ifndef SQLITE_OMIT_FLOATING_POINT
21991   int incr;
21992   const char *zEnd = z + length;
21993   /* sign * significand * (10 ^ (esign * exponent)) */
21994   int sign = 1;    /* sign of significand */
21995   i64 s = 0;       /* significand */
21996   int d = 0;       /* adjust exponent for shifting decimal point */
21997   int esign = 1;   /* sign of exponent */
21998   int e = 0;       /* exponent */
21999   int eValid = 1;  /* True exponent is either not used or is well-formed */
22000   double result;
22001   int nDigits = 0;
22002   int nonNum = 0;
22003 
22004   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
22005   *pResult = 0.0;   /* Default return value, in case of an error */
22006 
22007   if( enc==SQLITE_UTF8 ){
22008     incr = 1;
22009   }else{
22010     int i;
22011     incr = 2;
22012     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
22013     for(i=3-enc; i<length && z[i]==0; i+=2){}
22014     nonNum = i<length;
22015     zEnd = z+i+enc-3;
22016     z += (enc&1);
22017   }
22018 
22019   /* skip leading spaces */
22020   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
22021   if( z>=zEnd ) return 0;
22022 
22023   /* get sign of significand */
22024   if( *z=='-' ){
22025     sign = -1;
22026     z+=incr;
22027   }else if( *z=='+' ){
22028     z+=incr;
22029   }
22030 
22031   /* skip leading zeroes */
22032   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
22033 
22034   /* copy max significant digits to significand */
22035   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
22036     s = s*10 + (*z - '0');
22037     z+=incr, nDigits++;
22038   }
22039 
22040   /* skip non-significant significand digits
22041   ** (increase exponent by d to shift decimal left) */
22042   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
22043   if( z>=zEnd ) goto do_atof_calc;
22044 
22045   /* if decimal point is present */
22046   if( *z=='.' ){
22047     z+=incr;
22048     /* copy digits from after decimal to significand
22049     ** (decrease exponent by d to shift decimal right) */
22050     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
22051       s = s*10 + (*z - '0');
22052       z+=incr, nDigits++, d--;
22053     }
22054     /* skip non-significant digits */
22055     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
22056   }
22057   if( z>=zEnd ) goto do_atof_calc;
22058 
22059   /* if exponent is present */
22060   if( *z=='e' || *z=='E' ){
22061     z+=incr;
22062     eValid = 0;
22063     if( z>=zEnd ) goto do_atof_calc;
22064     /* get sign of exponent */
22065     if( *z=='-' ){
22066       esign = -1;
22067       z+=incr;
22068     }else if( *z=='+' ){
22069       z+=incr;
22070     }
22071     /* copy digits to exponent */
22072     while( z<zEnd && sqlite3Isdigit(*z) ){
22073       e = e<10000 ? (e*10 + (*z - '0')) : 10000;
22074       z+=incr;
22075       eValid = 1;
22076     }
22077   }
22078 
22079   /* skip trailing spaces */
22080   if( nDigits && eValid ){
22081     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
22082   }
22083 
22084 do_atof_calc:
22085   /* adjust exponent by d, and update sign */
22086   e = (e*esign) + d;
22087   if( e<0 ) {
22088     esign = -1;
22089     e *= -1;
22090   } else {
22091     esign = 1;
22092   }
22093 
22094   /* if 0 significand */
22095   if( !s ) {
22096     /* In the IEEE 754 standard, zero is signed.
22097     ** Add the sign if we've seen at least one digit */
22098     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
22099   } else {
22100     /* attempt to reduce exponent */
22101     if( esign>0 ){
22102       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
22103     }else{
22104       while( !(s%10) && e>0 ) e--,s/=10;
22105     }
22106 
22107     /* adjust the sign of significand */
22108     s = sign<0 ? -s : s;
22109 
22110     /* if exponent, scale significand as appropriate
22111     ** and store in result. */
22112     if( e ){
22113       LONGDOUBLE_TYPE scale = 1.0;
22114       /* attempt to handle extremely small/large numbers better */
22115       if( e>307 && e<342 ){
22116         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
22117         if( esign<0 ){
22118           result = s / scale;
22119           result /= 1.0e+308;
22120         }else{
22121           result = s * scale;
22122           result *= 1.0e+308;
22123         }
22124       }else if( e>=342 ){
22125         if( esign<0 ){
22126           result = 0.0*s;
22127         }else{
22128           result = 1e308*1e308*s;  /* Infinity */
22129         }
22130       }else{
22131         /* 1.0e+22 is the largest power of 10 than can be
22132         ** represented exactly. */
22133         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
22134         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
22135         if( esign<0 ){
22136           result = s / scale;
22137         }else{
22138           result = s * scale;
22139         }
22140       }
22141     } else {
22142       result = (double)s;
22143     }
22144   }
22145 
22146   /* store the result */
22147   *pResult = result;
22148 
22149   /* return true if number and no extra non-whitespace chracters after */
22150   return z>=zEnd && nDigits>0 && eValid && nonNum==0;
22151 #else
22152   return !sqlite3Atoi64(z, pResult, length, enc);
22153 #endif /* SQLITE_OMIT_FLOATING_POINT */
22154 }
22155 
22156 /*
22157 ** Compare the 19-character string zNum against the text representation
22158 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
22159 ** if zNum is less than, equal to, or greater than the string.
22160 ** Note that zNum must contain exactly 19 characters.
22161 **
22162 ** Unlike memcmp() this routine is guaranteed to return the difference
22163 ** in the values of the last digit if the only difference is in the
22164 ** last digit.  So, for example,
22165 **
22166 **      compare2pow63("9223372036854775800", 1)
22167 **
22168 ** will return -8.
22169 */
compare2pow63(const char * zNum,int incr)22170 static int compare2pow63(const char *zNum, int incr){
22171   int c = 0;
22172   int i;
22173                     /* 012345678901234567 */
22174   const char *pow63 = "922337203685477580";
22175   for(i=0; c==0 && i<18; i++){
22176     c = (zNum[i*incr]-pow63[i])*10;
22177   }
22178   if( c==0 ){
22179     c = zNum[18*incr] - '8';
22180     testcase( c==(-1) );
22181     testcase( c==0 );
22182     testcase( c==(+1) );
22183   }
22184   return c;
22185 }
22186 
22187 
22188 /*
22189 ** Convert zNum to a 64-bit signed integer.
22190 **
22191 ** If the zNum value is representable as a 64-bit twos-complement
22192 ** integer, then write that value into *pNum and return 0.
22193 **
22194 ** If zNum is exactly 9223372036854775808, return 2.  This special
22195 ** case is broken out because while 9223372036854775808 cannot be a
22196 ** signed 64-bit integer, its negative -9223372036854775808 can be.
22197 **
22198 ** If zNum is too big for a 64-bit integer and is not
22199 ** 9223372036854775808  or if zNum contains any non-numeric text,
22200 ** then return 1.
22201 **
22202 ** length is the number of bytes in the string (bytes, not characters).
22203 ** The string is not necessarily zero-terminated.  The encoding is
22204 ** given by enc.
22205 */
sqlite3Atoi64(const char * zNum,i64 * pNum,int length,u8 enc)22206 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
22207   int incr;
22208   u64 u = 0;
22209   int neg = 0; /* assume positive */
22210   int i;
22211   int c = 0;
22212   int nonNum = 0;
22213   const char *zStart;
22214   const char *zEnd = zNum + length;
22215   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
22216   if( enc==SQLITE_UTF8 ){
22217     incr = 1;
22218   }else{
22219     incr = 2;
22220     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
22221     for(i=3-enc; i<length && zNum[i]==0; i+=2){}
22222     nonNum = i<length;
22223     zEnd = zNum+i+enc-3;
22224     zNum += (enc&1);
22225   }
22226   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
22227   if( zNum<zEnd ){
22228     if( *zNum=='-' ){
22229       neg = 1;
22230       zNum+=incr;
22231     }else if( *zNum=='+' ){
22232       zNum+=incr;
22233     }
22234   }
22235   zStart = zNum;
22236   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
22237   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
22238     u = u*10 + c - '0';
22239   }
22240   if( u>LARGEST_INT64 ){
22241     *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
22242   }else if( neg ){
22243     *pNum = -(i64)u;
22244   }else{
22245     *pNum = (i64)u;
22246   }
22247   testcase( i==18 );
22248   testcase( i==19 );
22249   testcase( i==20 );
22250   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
22251     /* zNum is empty or contains non-numeric text or is longer
22252     ** than 19 digits (thus guaranteeing that it is too large) */
22253     return 1;
22254   }else if( i<19*incr ){
22255     /* Less than 19 digits, so we know that it fits in 64 bits */
22256     assert( u<=LARGEST_INT64 );
22257     return 0;
22258   }else{
22259     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
22260     c = compare2pow63(zNum, incr);
22261     if( c<0 ){
22262       /* zNum is less than 9223372036854775808 so it fits */
22263       assert( u<=LARGEST_INT64 );
22264       return 0;
22265     }else if( c>0 ){
22266       /* zNum is greater than 9223372036854775808 so it overflows */
22267       return 1;
22268     }else{
22269       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
22270       ** special case 2 overflow if positive */
22271       assert( u-1==LARGEST_INT64 );
22272       return neg ? 0 : 2;
22273     }
22274   }
22275 }
22276 
22277 /*
22278 ** If zNum represents an integer that will fit in 32-bits, then set
22279 ** *pValue to that integer and return true.  Otherwise return false.
22280 **
22281 ** Any non-numeric characters that following zNum are ignored.
22282 ** This is different from sqlite3Atoi64() which requires the
22283 ** input number to be zero-terminated.
22284 */
sqlite3GetInt32(const char * zNum,int * pValue)22285 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
22286   sqlite_int64 v = 0;
22287   int i, c;
22288   int neg = 0;
22289   if( zNum[0]=='-' ){
22290     neg = 1;
22291     zNum++;
22292   }else if( zNum[0]=='+' ){
22293     zNum++;
22294   }
22295   while( zNum[0]=='0' ) zNum++;
22296   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
22297     v = v*10 + c;
22298   }
22299 
22300   /* The longest decimal representation of a 32 bit integer is 10 digits:
22301   **
22302   **             1234567890
22303   **     2^31 -> 2147483648
22304   */
22305   testcase( i==10 );
22306   if( i>10 ){
22307     return 0;
22308   }
22309   testcase( v-neg==2147483647 );
22310   if( v-neg>2147483647 ){
22311     return 0;
22312   }
22313   if( neg ){
22314     v = -v;
22315   }
22316   *pValue = (int)v;
22317   return 1;
22318 }
22319 
22320 /*
22321 ** Return a 32-bit integer value extracted from a string.  If the
22322 ** string is not an integer, just return 0.
22323 */
sqlite3Atoi(const char * z)22324 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
22325   int x = 0;
22326   if( z ) sqlite3GetInt32(z, &x);
22327   return x;
22328 }
22329 
22330 /*
22331 ** The variable-length integer encoding is as follows:
22332 **
22333 ** KEY:
22334 **         A = 0xxxxxxx    7 bits of data and one flag bit
22335 **         B = 1xxxxxxx    7 bits of data and one flag bit
22336 **         C = xxxxxxxx    8 bits of data
22337 **
22338 **  7 bits - A
22339 ** 14 bits - BA
22340 ** 21 bits - BBA
22341 ** 28 bits - BBBA
22342 ** 35 bits - BBBBA
22343 ** 42 bits - BBBBBA
22344 ** 49 bits - BBBBBBA
22345 ** 56 bits - BBBBBBBA
22346 ** 64 bits - BBBBBBBBC
22347 */
22348 
22349 /*
22350 ** Write a 64-bit variable-length integer to memory starting at p[0].
22351 ** The length of data write will be between 1 and 9 bytes.  The number
22352 ** of bytes written is returned.
22353 **
22354 ** A variable-length integer consists of the lower 7 bits of each byte
22355 ** for all bytes that have the 8th bit set and one byte with the 8th
22356 ** bit clear.  Except, if we get to the 9th byte, it stores the full
22357 ** 8 bits and is the last byte.
22358 */
sqlite3PutVarint(unsigned char * p,u64 v)22359 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
22360   int i, j, n;
22361   u8 buf[10];
22362   if( v & (((u64)0xff000000)<<32) ){
22363     p[8] = (u8)v;
22364     v >>= 8;
22365     for(i=7; i>=0; i--){
22366       p[i] = (u8)((v & 0x7f) | 0x80);
22367       v >>= 7;
22368     }
22369     return 9;
22370   }
22371   n = 0;
22372   do{
22373     buf[n++] = (u8)((v & 0x7f) | 0x80);
22374     v >>= 7;
22375   }while( v!=0 );
22376   buf[0] &= 0x7f;
22377   assert( n<=9 );
22378   for(i=0, j=n-1; j>=0; j--, i++){
22379     p[i] = buf[j];
22380   }
22381   return n;
22382 }
22383 
22384 /*
22385 ** This routine is a faster version of sqlite3PutVarint() that only
22386 ** works for 32-bit positive integers and which is optimized for
22387 ** the common case of small integers.  A MACRO version, putVarint32,
22388 ** is provided which inlines the single-byte case.  All code should use
22389 ** the MACRO version as this function assumes the single-byte case has
22390 ** already been handled.
22391 */
sqlite3PutVarint32(unsigned char * p,u32 v)22392 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
22393 #ifndef putVarint32
22394   if( (v & ~0x7f)==0 ){
22395     p[0] = v;
22396     return 1;
22397   }
22398 #endif
22399   if( (v & ~0x3fff)==0 ){
22400     p[0] = (u8)((v>>7) | 0x80);
22401     p[1] = (u8)(v & 0x7f);
22402     return 2;
22403   }
22404   return sqlite3PutVarint(p, v);
22405 }
22406 
22407 /*
22408 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
22409 ** are defined here rather than simply putting the constant expressions
22410 ** inline in order to work around bugs in the RVT compiler.
22411 **
22412 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
22413 **
22414 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
22415 */
22416 #define SLOT_2_0     0x001fc07f
22417 #define SLOT_4_2_0   0xf01fc07f
22418 
22419 
22420 /*
22421 ** Read a 64-bit variable-length integer from memory starting at p[0].
22422 ** Return the number of bytes read.  The value is stored in *v.
22423 */
sqlite3GetVarint(const unsigned char * p,u64 * v)22424 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
22425   u32 a,b,s;
22426 
22427   a = *p;
22428   /* a: p0 (unmasked) */
22429   if (!(a&0x80))
22430   {
22431     *v = a;
22432     return 1;
22433   }
22434 
22435   p++;
22436   b = *p;
22437   /* b: p1 (unmasked) */
22438   if (!(b&0x80))
22439   {
22440     a &= 0x7f;
22441     a = a<<7;
22442     a |= b;
22443     *v = a;
22444     return 2;
22445   }
22446 
22447   /* Verify that constants are precomputed correctly */
22448   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
22449   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
22450 
22451   p++;
22452   a = a<<14;
22453   a |= *p;
22454   /* a: p0<<14 | p2 (unmasked) */
22455   if (!(a&0x80))
22456   {
22457     a &= SLOT_2_0;
22458     b &= 0x7f;
22459     b = b<<7;
22460     a |= b;
22461     *v = a;
22462     return 3;
22463   }
22464 
22465   /* CSE1 from below */
22466   a &= SLOT_2_0;
22467   p++;
22468   b = b<<14;
22469   b |= *p;
22470   /* b: p1<<14 | p3 (unmasked) */
22471   if (!(b&0x80))
22472   {
22473     b &= SLOT_2_0;
22474     /* moved CSE1 up */
22475     /* a &= (0x7f<<14)|(0x7f); */
22476     a = a<<7;
22477     a |= b;
22478     *v = a;
22479     return 4;
22480   }
22481 
22482   /* a: p0<<14 | p2 (masked) */
22483   /* b: p1<<14 | p3 (unmasked) */
22484   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
22485   /* moved CSE1 up */
22486   /* a &= (0x7f<<14)|(0x7f); */
22487   b &= SLOT_2_0;
22488   s = a;
22489   /* s: p0<<14 | p2 (masked) */
22490 
22491   p++;
22492   a = a<<14;
22493   a |= *p;
22494   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
22495   if (!(a&0x80))
22496   {
22497     /* we can skip these cause they were (effectively) done above in calc'ing s */
22498     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
22499     /* b &= (0x7f<<14)|(0x7f); */
22500     b = b<<7;
22501     a |= b;
22502     s = s>>18;
22503     *v = ((u64)s)<<32 | a;
22504     return 5;
22505   }
22506 
22507   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
22508   s = s<<7;
22509   s |= b;
22510   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
22511 
22512   p++;
22513   b = b<<14;
22514   b |= *p;
22515   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
22516   if (!(b&0x80))
22517   {
22518     /* we can skip this cause it was (effectively) done above in calc'ing s */
22519     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
22520     a &= SLOT_2_0;
22521     a = a<<7;
22522     a |= b;
22523     s = s>>18;
22524     *v = ((u64)s)<<32 | a;
22525     return 6;
22526   }
22527 
22528   p++;
22529   a = a<<14;
22530   a |= *p;
22531   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
22532   if (!(a&0x80))
22533   {
22534     a &= SLOT_4_2_0;
22535     b &= SLOT_2_0;
22536     b = b<<7;
22537     a |= b;
22538     s = s>>11;
22539     *v = ((u64)s)<<32 | a;
22540     return 7;
22541   }
22542 
22543   /* CSE2 from below */
22544   a &= SLOT_2_0;
22545   p++;
22546   b = b<<14;
22547   b |= *p;
22548   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
22549   if (!(b&0x80))
22550   {
22551     b &= SLOT_4_2_0;
22552     /* moved CSE2 up */
22553     /* a &= (0x7f<<14)|(0x7f); */
22554     a = a<<7;
22555     a |= b;
22556     s = s>>4;
22557     *v = ((u64)s)<<32 | a;
22558     return 8;
22559   }
22560 
22561   p++;
22562   a = a<<15;
22563   a |= *p;
22564   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
22565 
22566   /* moved CSE2 up */
22567   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
22568   b &= SLOT_2_0;
22569   b = b<<8;
22570   a |= b;
22571 
22572   s = s<<4;
22573   b = p[-4];
22574   b &= 0x7f;
22575   b = b>>3;
22576   s |= b;
22577 
22578   *v = ((u64)s)<<32 | a;
22579 
22580   return 9;
22581 }
22582 
22583 /*
22584 ** Read a 32-bit variable-length integer from memory starting at p[0].
22585 ** Return the number of bytes read.  The value is stored in *v.
22586 **
22587 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
22588 ** integer, then set *v to 0xffffffff.
22589 **
22590 ** A MACRO version, getVarint32, is provided which inlines the
22591 ** single-byte case.  All code should use the MACRO version as
22592 ** this function assumes the single-byte case has already been handled.
22593 */
sqlite3GetVarint32(const unsigned char * p,u32 * v)22594 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
22595   u32 a,b;
22596 
22597   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
22598   ** by the getVarin32() macro */
22599   a = *p;
22600   /* a: p0 (unmasked) */
22601 #ifndef getVarint32
22602   if (!(a&0x80))
22603   {
22604     /* Values between 0 and 127 */
22605     *v = a;
22606     return 1;
22607   }
22608 #endif
22609 
22610   /* The 2-byte case */
22611   p++;
22612   b = *p;
22613   /* b: p1 (unmasked) */
22614   if (!(b&0x80))
22615   {
22616     /* Values between 128 and 16383 */
22617     a &= 0x7f;
22618     a = a<<7;
22619     *v = a | b;
22620     return 2;
22621   }
22622 
22623   /* The 3-byte case */
22624   p++;
22625   a = a<<14;
22626   a |= *p;
22627   /* a: p0<<14 | p2 (unmasked) */
22628   if (!(a&0x80))
22629   {
22630     /* Values between 16384 and 2097151 */
22631     a &= (0x7f<<14)|(0x7f);
22632     b &= 0x7f;
22633     b = b<<7;
22634     *v = a | b;
22635     return 3;
22636   }
22637 
22638   /* A 32-bit varint is used to store size information in btrees.
22639   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
22640   ** A 3-byte varint is sufficient, for example, to record the size
22641   ** of a 1048569-byte BLOB or string.
22642   **
22643   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
22644   ** rare larger cases can be handled by the slower 64-bit varint
22645   ** routine.
22646   */
22647 #if 1
22648   {
22649     u64 v64;
22650     u8 n;
22651 
22652     p -= 2;
22653     n = sqlite3GetVarint(p, &v64);
22654     assert( n>3 && n<=9 );
22655     if( (v64 & SQLITE_MAX_U32)!=v64 ){
22656       *v = 0xffffffff;
22657     }else{
22658       *v = (u32)v64;
22659     }
22660     return n;
22661   }
22662 
22663 #else
22664   /* For following code (kept for historical record only) shows an
22665   ** unrolling for the 3- and 4-byte varint cases.  This code is
22666   ** slightly faster, but it is also larger and much harder to test.
22667   */
22668   p++;
22669   b = b<<14;
22670   b |= *p;
22671   /* b: p1<<14 | p3 (unmasked) */
22672   if (!(b&0x80))
22673   {
22674     /* Values between 2097152 and 268435455 */
22675     b &= (0x7f<<14)|(0x7f);
22676     a &= (0x7f<<14)|(0x7f);
22677     a = a<<7;
22678     *v = a | b;
22679     return 4;
22680   }
22681 
22682   p++;
22683   a = a<<14;
22684   a |= *p;
22685   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
22686   if (!(a&0x80))
22687   {
22688     /* Values  between 268435456 and 34359738367 */
22689     a &= SLOT_4_2_0;
22690     b &= SLOT_4_2_0;
22691     b = b<<7;
22692     *v = a | b;
22693     return 5;
22694   }
22695 
22696   /* We can only reach this point when reading a corrupt database
22697   ** file.  In that case we are not in any hurry.  Use the (relatively
22698   ** slow) general-purpose sqlite3GetVarint() routine to extract the
22699   ** value. */
22700   {
22701     u64 v64;
22702     u8 n;
22703 
22704     p -= 4;
22705     n = sqlite3GetVarint(p, &v64);
22706     assert( n>5 && n<=9 );
22707     *v = (u32)v64;
22708     return n;
22709   }
22710 #endif
22711 }
22712 
22713 /*
22714 ** Return the number of bytes that will be needed to store the given
22715 ** 64-bit integer.
22716 */
sqlite3VarintLen(u64 v)22717 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
22718   int i = 0;
22719   do{
22720     i++;
22721     v >>= 7;
22722   }while( v!=0 && ALWAYS(i<9) );
22723   return i;
22724 }
22725 
22726 
22727 /*
22728 ** Read or write a four-byte big-endian integer value.
22729 */
sqlite3Get4byte(const u8 * p)22730 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
22731   testcase( p[0]&0x80 );
22732   return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
22733 }
sqlite3Put4byte(unsigned char * p,u32 v)22734 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
22735   p[0] = (u8)(v>>24);
22736   p[1] = (u8)(v>>16);
22737   p[2] = (u8)(v>>8);
22738   p[3] = (u8)v;
22739 }
22740 
22741 
22742 
22743 /*
22744 ** Translate a single byte of Hex into an integer.
22745 ** This routine only works if h really is a valid hexadecimal
22746 ** character:  0..9a..fA..F
22747 */
sqlite3HexToInt(int h)22748 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
22749   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
22750 #ifdef SQLITE_ASCII
22751   h += 9*(1&(h>>6));
22752 #endif
22753 #ifdef SQLITE_EBCDIC
22754   h += 9*(1&~(h>>4));
22755 #endif
22756   return (u8)(h & 0xf);
22757 }
22758 
22759 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
22760 /*
22761 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
22762 ** value.  Return a pointer to its binary value.  Space to hold the
22763 ** binary value has been obtained from malloc and must be freed by
22764 ** the calling routine.
22765 */
sqlite3HexToBlob(sqlite3 * db,const char * z,int n)22766 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
22767   char *zBlob;
22768   int i;
22769 
22770   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
22771   n--;
22772   if( zBlob ){
22773     for(i=0; i<n; i+=2){
22774       zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
22775     }
22776     zBlob[i/2] = 0;
22777   }
22778   return zBlob;
22779 }
22780 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
22781 
22782 /*
22783 ** Log an error that is an API call on a connection pointer that should
22784 ** not have been used.  The "type" of connection pointer is given as the
22785 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
22786 */
logBadConnection(const char * zType)22787 static void logBadConnection(const char *zType){
22788   sqlite3_log(SQLITE_MISUSE,
22789      "API call with %s database connection pointer",
22790      zType
22791   );
22792 }
22793 
22794 /*
22795 ** Check to make sure we have a valid db pointer.  This test is not
22796 ** foolproof but it does provide some measure of protection against
22797 ** misuse of the interface such as passing in db pointers that are
22798 ** NULL or which have been previously closed.  If this routine returns
22799 ** 1 it means that the db pointer is valid and 0 if it should not be
22800 ** dereferenced for any reason.  The calling function should invoke
22801 ** SQLITE_MISUSE immediately.
22802 **
22803 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
22804 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
22805 ** open properly and is not fit for general use but which can be
22806 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
22807 */
sqlite3SafetyCheckOk(sqlite3 * db)22808 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
22809   u32 magic;
22810   if( db==0 ){
22811     logBadConnection("NULL");
22812     return 0;
22813   }
22814   magic = db->magic;
22815   if( magic!=SQLITE_MAGIC_OPEN ){
22816     if( sqlite3SafetyCheckSickOrOk(db) ){
22817       testcase( sqlite3GlobalConfig.xLog!=0 );
22818       logBadConnection("unopened");
22819     }
22820     return 0;
22821   }else{
22822     return 1;
22823   }
22824 }
sqlite3SafetyCheckSickOrOk(sqlite3 * db)22825 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
22826   u32 magic;
22827   magic = db->magic;
22828   if( magic!=SQLITE_MAGIC_SICK &&
22829       magic!=SQLITE_MAGIC_OPEN &&
22830       magic!=SQLITE_MAGIC_BUSY ){
22831     testcase( sqlite3GlobalConfig.xLog!=0 );
22832     logBadConnection("invalid");
22833     return 0;
22834   }else{
22835     return 1;
22836   }
22837 }
22838 
22839 /*
22840 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
22841 ** the other 64-bit signed integer at *pA and store the result in *pA.
22842 ** Return 0 on success.  Or if the operation would have resulted in an
22843 ** overflow, leave *pA unchanged and return 1.
22844 */
sqlite3AddInt64(i64 * pA,i64 iB)22845 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
22846   i64 iA = *pA;
22847   testcase( iA==0 ); testcase( iA==1 );
22848   testcase( iB==-1 ); testcase( iB==0 );
22849   if( iB>=0 ){
22850     testcase( iA>0 && LARGEST_INT64 - iA == iB );
22851     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
22852     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
22853   }else{
22854     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
22855     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
22856     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
22857   }
22858   *pA += iB;
22859   return 0;
22860 }
sqlite3SubInt64(i64 * pA,i64 iB)22861 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
22862   testcase( iB==SMALLEST_INT64+1 );
22863   if( iB==SMALLEST_INT64 ){
22864     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
22865     if( (*pA)>=0 ) return 1;
22866     *pA -= iB;
22867     return 0;
22868   }else{
22869     return sqlite3AddInt64(pA, -iB);
22870   }
22871 }
22872 #define TWOPOWER32 (((i64)1)<<32)
22873 #define TWOPOWER31 (((i64)1)<<31)
sqlite3MulInt64(i64 * pA,i64 iB)22874 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
22875   i64 iA = *pA;
22876   i64 iA1, iA0, iB1, iB0, r;
22877 
22878   iA1 = iA/TWOPOWER32;
22879   iA0 = iA % TWOPOWER32;
22880   iB1 = iB/TWOPOWER32;
22881   iB0 = iB % TWOPOWER32;
22882   if( iA1==0 ){
22883     if( iB1==0 ){
22884       *pA *= iB;
22885       return 0;
22886     }
22887     r = iA0*iB1;
22888   }else if( iB1==0 ){
22889     r = iA1*iB0;
22890   }else{
22891     /* If both iA1 and iB1 are non-zero, overflow will result */
22892     return 1;
22893   }
22894   testcase( r==(-TWOPOWER31)-1 );
22895   testcase( r==(-TWOPOWER31) );
22896   testcase( r==TWOPOWER31 );
22897   testcase( r==TWOPOWER31-1 );
22898   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
22899   r *= TWOPOWER32;
22900   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
22901   *pA = r;
22902   return 0;
22903 }
22904 
22905 /*
22906 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or
22907 ** if the integer has a value of -2147483648, return +2147483647
22908 */
sqlite3AbsInt32(int x)22909 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
22910   if( x>=0 ) return x;
22911   if( x==(int)0x80000000 ) return 0x7fffffff;
22912   return -x;
22913 }
22914 
22915 #ifdef SQLITE_ENABLE_8_3_NAMES
22916 /*
22917 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
22918 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
22919 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
22920 ** three characters, then shorten the suffix on z[] to be the last three
22921 ** characters of the original suffix.
22922 **
22923 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
22924 ** do the suffix shortening regardless of URI parameter.
22925 **
22926 ** Examples:
22927 **
22928 **     test.db-journal    =>   test.nal
22929 **     test.db-wal        =>   test.wal
22930 **     test.db-shm        =>   test.shm
22931 **     test.db-mj7f3319fa =>   test.9fa
22932 */
sqlite3FileSuffix3(const char * zBaseFilename,char * z)22933 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
22934 #if SQLITE_ENABLE_8_3_NAMES<2
22935   if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
22936 #endif
22937   {
22938     int i, sz;
22939     sz = sqlite3Strlen30(z);
22940     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
22941     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
22942   }
22943 }
22944 #endif
22945 
22946 /*
22947 ** Find (an approximate) sum of two LogEst values.  This computation is
22948 ** not a simple "+" operator because LogEst is stored as a logarithmic
22949 ** value.
22950 **
22951 */
sqlite3LogEstAdd(LogEst a,LogEst b)22952 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
22953   static const unsigned char x[] = {
22954      10, 10,                         /* 0,1 */
22955       9, 9,                          /* 2,3 */
22956       8, 8,                          /* 4,5 */
22957       7, 7, 7,                       /* 6,7,8 */
22958       6, 6, 6,                       /* 9,10,11 */
22959       5, 5, 5,                       /* 12-14 */
22960       4, 4, 4, 4,                    /* 15-18 */
22961       3, 3, 3, 3, 3, 3,              /* 19-24 */
22962       2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
22963   };
22964   if( a>=b ){
22965     if( a>b+49 ) return a;
22966     if( a>b+31 ) return a+1;
22967     return a+x[a-b];
22968   }else{
22969     if( b>a+49 ) return b;
22970     if( b>a+31 ) return b+1;
22971     return b+x[b-a];
22972   }
22973 }
22974 
22975 /*
22976 ** Convert an integer into a LogEst.  In other words, compute a
22977 ** good approximatation for 10*log2(x).
22978 */
sqlite3LogEst(u64 x)22979 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
22980   static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
22981   LogEst y = 40;
22982   if( x<8 ){
22983     if( x<2 ) return 0;
22984     while( x<8 ){  y -= 10; x <<= 1; }
22985   }else{
22986     while( x>255 ){ y += 40; x >>= 4; }
22987     while( x>15 ){  y += 10; x >>= 1; }
22988   }
22989   return a[x&7] + y - 10;
22990 }
22991 
22992 #ifndef SQLITE_OMIT_VIRTUALTABLE
22993 /*
22994 ** Convert a double into a LogEst
22995 ** In other words, compute an approximation for 10*log2(x).
22996 */
sqlite3LogEstFromDouble(double x)22997 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
22998   u64 a;
22999   LogEst e;
23000   assert( sizeof(x)==8 && sizeof(a)==8 );
23001   if( x<=1 ) return 0;
23002   if( x<=2000000000 ) return sqlite3LogEst((u64)x);
23003   memcpy(&a, &x, 8);
23004   e = (a>>52) - 1022;
23005   return e*10;
23006 }
23007 #endif /* SQLITE_OMIT_VIRTUALTABLE */
23008 
23009 /*
23010 ** Convert a LogEst into an integer.
23011 */
sqlite3LogEstToInt(LogEst x)23012 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
23013   u64 n;
23014   if( x<10 ) return 1;
23015   n = x%10;
23016   x /= 10;
23017   if( n>=5 ) n -= 2;
23018   else if( n>=1 ) n -= 1;
23019   if( x>=3 ){
23020     return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
23021   }
23022   return (n+8)>>(3-x);
23023 }
23024 
23025 /************** End of util.c ************************************************/
23026 /************** Begin file hash.c ********************************************/
23027 /*
23028 ** 2001 September 22
23029 **
23030 ** The author disclaims copyright to this source code.  In place of
23031 ** a legal notice, here is a blessing:
23032 **
23033 **    May you do good and not evil.
23034 **    May you find forgiveness for yourself and forgive others.
23035 **    May you share freely, never taking more than you give.
23036 **
23037 *************************************************************************
23038 ** This is the implementation of generic hash-tables
23039 ** used in SQLite.
23040 */
23041 /* #include <assert.h> */
23042 
23043 /* Turn bulk memory into a hash table object by initializing the
23044 ** fields of the Hash structure.
23045 **
23046 ** "pNew" is a pointer to the hash table that is to be initialized.
23047 */
sqlite3HashInit(Hash * pNew)23048 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
23049   assert( pNew!=0 );
23050   pNew->first = 0;
23051   pNew->count = 0;
23052   pNew->htsize = 0;
23053   pNew->ht = 0;
23054 }
23055 
23056 /* Remove all entries from a hash table.  Reclaim all memory.
23057 ** Call this routine to delete a hash table or to reset a hash table
23058 ** to the empty state.
23059 */
sqlite3HashClear(Hash * pH)23060 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
23061   HashElem *elem;         /* For looping over all elements of the table */
23062 
23063   assert( pH!=0 );
23064   elem = pH->first;
23065   pH->first = 0;
23066   sqlite3_free(pH->ht);
23067   pH->ht = 0;
23068   pH->htsize = 0;
23069   while( elem ){
23070     HashElem *next_elem = elem->next;
23071     sqlite3_free(elem);
23072     elem = next_elem;
23073   }
23074   pH->count = 0;
23075 }
23076 
23077 /*
23078 ** The hashing function.
23079 */
strHash(const char * z,int nKey)23080 static unsigned int strHash(const char *z, int nKey){
23081   unsigned int h = 0;
23082   assert( nKey>=0 );
23083   while( nKey > 0  ){
23084     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
23085     nKey--;
23086   }
23087   return h;
23088 }
23089 
23090 
23091 /* Link pNew element into the hash table pH.  If pEntry!=0 then also
23092 ** insert pNew into the pEntry hash bucket.
23093 */
insertElement(Hash * pH,struct _ht * pEntry,HashElem * pNew)23094 static void insertElement(
23095   Hash *pH,              /* The complete hash table */
23096   struct _ht *pEntry,    /* The entry into which pNew is inserted */
23097   HashElem *pNew         /* The element to be inserted */
23098 ){
23099   HashElem *pHead;       /* First element already in pEntry */
23100   if( pEntry ){
23101     pHead = pEntry->count ? pEntry->chain : 0;
23102     pEntry->count++;
23103     pEntry->chain = pNew;
23104   }else{
23105     pHead = 0;
23106   }
23107   if( pHead ){
23108     pNew->next = pHead;
23109     pNew->prev = pHead->prev;
23110     if( pHead->prev ){ pHead->prev->next = pNew; }
23111     else             { pH->first = pNew; }
23112     pHead->prev = pNew;
23113   }else{
23114     pNew->next = pH->first;
23115     if( pH->first ){ pH->first->prev = pNew; }
23116     pNew->prev = 0;
23117     pH->first = pNew;
23118   }
23119 }
23120 
23121 
23122 /* Resize the hash table so that it cantains "new_size" buckets.
23123 **
23124 ** The hash table might fail to resize if sqlite3_malloc() fails or
23125 ** if the new size is the same as the prior size.
23126 ** Return TRUE if the resize occurs and false if not.
23127 */
rehash(Hash * pH,unsigned int new_size)23128 static int rehash(Hash *pH, unsigned int new_size){
23129   struct _ht *new_ht;            /* The new hash table */
23130   HashElem *elem, *next_elem;    /* For looping over existing elements */
23131 
23132 #if SQLITE_MALLOC_SOFT_LIMIT>0
23133   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
23134     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
23135   }
23136   if( new_size==pH->htsize ) return 0;
23137 #endif
23138 
23139   /* The inability to allocates space for a larger hash table is
23140   ** a performance hit but it is not a fatal error.  So mark the
23141   ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
23142   ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
23143   ** only zeroes the requested number of bytes whereas this module will
23144   ** use the actual amount of space allocated for the hash table (which
23145   ** may be larger than the requested amount).
23146   */
23147   sqlite3BeginBenignMalloc();
23148   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
23149   sqlite3EndBenignMalloc();
23150 
23151   if( new_ht==0 ) return 0;
23152   sqlite3_free(pH->ht);
23153   pH->ht = new_ht;
23154   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
23155   memset(new_ht, 0, new_size*sizeof(struct _ht));
23156   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
23157     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
23158     next_elem = elem->next;
23159     insertElement(pH, &new_ht[h], elem);
23160   }
23161   return 1;
23162 }
23163 
23164 /* This function (for internal use only) locates an element in an
23165 ** hash table that matches the given key.  The hash for this key has
23166 ** already been computed and is passed as the 4th parameter.
23167 */
findElementGivenHash(const Hash * pH,const char * pKey,int nKey,unsigned int h)23168 static HashElem *findElementGivenHash(
23169   const Hash *pH,     /* The pH to be searched */
23170   const char *pKey,   /* The key we are searching for */
23171   int nKey,           /* Bytes in key (not counting zero terminator) */
23172   unsigned int h      /* The hash for this key. */
23173 ){
23174   HashElem *elem;                /* Used to loop thru the element list */
23175   int count;                     /* Number of elements left to test */
23176 
23177   if( pH->ht ){
23178     struct _ht *pEntry = &pH->ht[h];
23179     elem = pEntry->chain;
23180     count = pEntry->count;
23181   }else{
23182     elem = pH->first;
23183     count = pH->count;
23184   }
23185   while( count-- && ALWAYS(elem) ){
23186     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
23187       return elem;
23188     }
23189     elem = elem->next;
23190   }
23191   return 0;
23192 }
23193 
23194 /* Remove a single entry from the hash table given a pointer to that
23195 ** element and a hash on the element's key.
23196 */
removeElementGivenHash(Hash * pH,HashElem * elem,unsigned int h)23197 static void removeElementGivenHash(
23198   Hash *pH,         /* The pH containing "elem" */
23199   HashElem* elem,   /* The element to be removed from the pH */
23200   unsigned int h    /* Hash value for the element */
23201 ){
23202   struct _ht *pEntry;
23203   if( elem->prev ){
23204     elem->prev->next = elem->next;
23205   }else{
23206     pH->first = elem->next;
23207   }
23208   if( elem->next ){
23209     elem->next->prev = elem->prev;
23210   }
23211   if( pH->ht ){
23212     pEntry = &pH->ht[h];
23213     if( pEntry->chain==elem ){
23214       pEntry->chain = elem->next;
23215     }
23216     pEntry->count--;
23217     assert( pEntry->count>=0 );
23218   }
23219   sqlite3_free( elem );
23220   pH->count--;
23221   if( pH->count==0 ){
23222     assert( pH->first==0 );
23223     assert( pH->count==0 );
23224     sqlite3HashClear(pH);
23225   }
23226 }
23227 
23228 /* Attempt to locate an element of the hash table pH with a key
23229 ** that matches pKey,nKey.  Return the data for this element if it is
23230 ** found, or NULL if there is no match.
23231 */
sqlite3HashFind(const Hash * pH,const char * pKey,int nKey)23232 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
23233   HashElem *elem;    /* The element that matches key */
23234   unsigned int h;    /* A hash on key */
23235 
23236   assert( pH!=0 );
23237   assert( pKey!=0 );
23238   assert( nKey>=0 );
23239   if( pH->ht ){
23240     h = strHash(pKey, nKey) % pH->htsize;
23241   }else{
23242     h = 0;
23243   }
23244   elem = findElementGivenHash(pH, pKey, nKey, h);
23245   return elem ? elem->data : 0;
23246 }
23247 
23248 /* Insert an element into the hash table pH.  The key is pKey,nKey
23249 ** and the data is "data".
23250 **
23251 ** If no element exists with a matching key, then a new
23252 ** element is created and NULL is returned.
23253 **
23254 ** If another element already exists with the same key, then the
23255 ** new data replaces the old data and the old data is returned.
23256 ** The key is not copied in this instance.  If a malloc fails, then
23257 ** the new data is returned and the hash table is unchanged.
23258 **
23259 ** If the "data" parameter to this function is NULL, then the
23260 ** element corresponding to "key" is removed from the hash table.
23261 */
sqlite3HashInsert(Hash * pH,const char * pKey,int nKey,void * data)23262 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
23263   unsigned int h;       /* the hash of the key modulo hash table size */
23264   HashElem *elem;       /* Used to loop thru the element list */
23265   HashElem *new_elem;   /* New element added to the pH */
23266 
23267   assert( pH!=0 );
23268   assert( pKey!=0 );
23269   assert( nKey>=0 );
23270   if( pH->htsize ){
23271     h = strHash(pKey, nKey) % pH->htsize;
23272   }else{
23273     h = 0;
23274   }
23275   elem = findElementGivenHash(pH,pKey,nKey,h);
23276   if( elem ){
23277     void *old_data = elem->data;
23278     if( data==0 ){
23279       removeElementGivenHash(pH,elem,h);
23280     }else{
23281       elem->data = data;
23282       elem->pKey = pKey;
23283       assert(nKey==elem->nKey);
23284     }
23285     return old_data;
23286   }
23287   if( data==0 ) return 0;
23288   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
23289   if( new_elem==0 ) return data;
23290   new_elem->pKey = pKey;
23291   new_elem->nKey = nKey;
23292   new_elem->data = data;
23293   pH->count++;
23294   if( pH->count>=10 && pH->count > 2*pH->htsize ){
23295     if( rehash(pH, pH->count*2) ){
23296       assert( pH->htsize>0 );
23297       h = strHash(pKey, nKey) % pH->htsize;
23298     }
23299   }
23300   if( pH->ht ){
23301     insertElement(pH, &pH->ht[h], new_elem);
23302   }else{
23303     insertElement(pH, 0, new_elem);
23304   }
23305   return 0;
23306 }
23307 
23308 /************** End of hash.c ************************************************/
23309 /************** Begin file opcodes.c *****************************************/
23310 /* Automatically generated.  Do not edit */
23311 /* See the mkopcodec.awk script for details. */
23312 #if !defined(SQLITE_OMIT_EXPLAIN) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
23313 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
23314 # define OpHelp(X) "\0" X
23315 #else
23316 # define OpHelp(X)
23317 #endif
sqlite3OpcodeName(int i)23318 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
23319  static const char *const azName[] = { "?",
23320      /*   1 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
23321      /*   2 */ "Savepoint"        OpHelp(""),
23322      /*   3 */ "AutoCommit"       OpHelp(""),
23323      /*   4 */ "Transaction"      OpHelp(""),
23324      /*   5 */ "SorterNext"       OpHelp(""),
23325      /*   6 */ "PrevIfOpen"       OpHelp(""),
23326      /*   7 */ "NextIfOpen"       OpHelp(""),
23327      /*   8 */ "Prev"             OpHelp(""),
23328      /*   9 */ "Next"             OpHelp(""),
23329      /*  10 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
23330      /*  11 */ "Checkpoint"       OpHelp(""),
23331      /*  12 */ "JournalMode"      OpHelp(""),
23332      /*  13 */ "Vacuum"           OpHelp(""),
23333      /*  14 */ "VFilter"          OpHelp("iPlan=r[P3] zPlan='P4'"),
23334      /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
23335      /*  16 */ "Goto"             OpHelp(""),
23336      /*  17 */ "Gosub"            OpHelp(""),
23337      /*  18 */ "Return"           OpHelp(""),
23338      /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
23339      /*  20 */ "InitCoroutine"    OpHelp(""),
23340      /*  21 */ "EndCoroutine"     OpHelp(""),
23341      /*  22 */ "Yield"            OpHelp(""),
23342      /*  23 */ "HaltIfNull"       OpHelp("if r[P3]=null halt"),
23343      /*  24 */ "Halt"             OpHelp(""),
23344      /*  25 */ "Integer"          OpHelp("r[P2]=P1"),
23345      /*  26 */ "Int64"            OpHelp("r[P2]=P4"),
23346      /*  27 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
23347      /*  28 */ "Null"             OpHelp("r[P2..P3]=NULL"),
23348      /*  29 */ "SoftNull"         OpHelp("r[P1]=NULL"),
23349      /*  30 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
23350      /*  31 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
23351      /*  32 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
23352      /*  33 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
23353      /*  34 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
23354      /*  35 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
23355      /*  36 */ "CollSeq"          OpHelp(""),
23356      /*  37 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
23357      /*  38 */ "MustBeInt"        OpHelp(""),
23358      /*  39 */ "RealAffinity"     OpHelp(""),
23359      /*  40 */ "Permutation"      OpHelp(""),
23360      /*  41 */ "Compare"          OpHelp(""),
23361      /*  42 */ "Jump"             OpHelp(""),
23362      /*  43 */ "Once"             OpHelp(""),
23363      /*  44 */ "If"               OpHelp(""),
23364      /*  45 */ "IfNot"            OpHelp(""),
23365      /*  46 */ "Column"           OpHelp("r[P3]=PX"),
23366      /*  47 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
23367      /*  48 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
23368      /*  49 */ "Count"            OpHelp("r[P2]=count()"),
23369      /*  50 */ "ReadCookie"       OpHelp(""),
23370      /*  51 */ "SetCookie"        OpHelp(""),
23371      /*  52 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
23372      /*  53 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
23373      /*  54 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
23374      /*  55 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
23375      /*  56 */ "SorterOpen"       OpHelp(""),
23376      /*  57 */ "OpenPseudo"       OpHelp("P3 columns in r[P2]"),
23377      /*  58 */ "Close"            OpHelp(""),
23378      /*  59 */ "SeekLT"           OpHelp(""),
23379      /*  60 */ "SeekLE"           OpHelp(""),
23380      /*  61 */ "SeekGE"           OpHelp(""),
23381      /*  62 */ "SeekGT"           OpHelp(""),
23382      /*  63 */ "Seek"             OpHelp("intkey=r[P2]"),
23383      /*  64 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
23384      /*  65 */ "NotFound"         OpHelp("key=r[P3@P4]"),
23385      /*  66 */ "Found"            OpHelp("key=r[P3@P4]"),
23386      /*  67 */ "NotExists"        OpHelp("intkey=r[P3]"),
23387      /*  68 */ "Sequence"         OpHelp("r[P2]=rowid"),
23388      /*  69 */ "NewRowid"         OpHelp("r[P2]=rowid"),
23389      /*  70 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
23390      /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
23391      /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
23392      /*  73 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
23393      /*  74 */ "Delete"           OpHelp(""),
23394      /*  75 */ "ResetCount"       OpHelp(""),
23395      /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
23396      /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
23397      /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
23398      /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
23399      /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
23400      /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
23401      /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
23402      /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
23403      /*  84 */ "SorterCompare"    OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
23404      /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
23405      /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
23406      /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
23407      /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
23408      /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
23409      /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
23410      /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
23411      /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
23412      /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
23413      /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
23414      /*  95 */ "SorterData"       OpHelp("r[P2]=data"),
23415      /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
23416      /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
23417      /*  98 */ "RowKey"           OpHelp("r[P2]=key"),
23418      /*  99 */ "RowData"          OpHelp("r[P2]=data"),
23419      /* 100 */ "Rowid"            OpHelp("r[P2]=rowid"),
23420      /* 101 */ "NullRow"          OpHelp(""),
23421      /* 102 */ "Last"             OpHelp(""),
23422      /* 103 */ "SorterSort"       OpHelp(""),
23423      /* 104 */ "Sort"             OpHelp(""),
23424      /* 105 */ "Rewind"           OpHelp(""),
23425      /* 106 */ "SorterInsert"     OpHelp(""),
23426      /* 107 */ "IdxInsert"        OpHelp("key=r[P2]"),
23427      /* 108 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
23428      /* 109 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
23429      /* 110 */ "IdxLE"            OpHelp("key=r[P3@P4]"),
23430      /* 111 */ "IdxGT"            OpHelp("key=r[P3@P4]"),
23431      /* 112 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
23432      /* 113 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
23433      /* 114 */ "Destroy"          OpHelp(""),
23434      /* 115 */ "Clear"            OpHelp(""),
23435      /* 116 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
23436      /* 117 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
23437      /* 118 */ "ParseSchema"      OpHelp(""),
23438      /* 119 */ "LoadAnalysis"     OpHelp(""),
23439      /* 120 */ "DropTable"        OpHelp(""),
23440      /* 121 */ "DropIndex"        OpHelp(""),
23441      /* 122 */ "DropTrigger"      OpHelp(""),
23442      /* 123 */ "IntegrityCk"      OpHelp(""),
23443      /* 124 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
23444      /* 125 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
23445      /* 126 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
23446      /* 127 */ "Program"          OpHelp(""),
23447      /* 128 */ "Param"            OpHelp(""),
23448      /* 129 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
23449      /* 130 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
23450      /* 131 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
23451      /* 132 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
23452      /* 133 */ "Real"             OpHelp("r[P2]=P4"),
23453      /* 134 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
23454      /* 135 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
23455      /* 136 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
23456      /* 137 */ "IncrVacuum"       OpHelp(""),
23457      /* 138 */ "Expire"           OpHelp(""),
23458      /* 139 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
23459      /* 140 */ "VBegin"           OpHelp(""),
23460      /* 141 */ "VCreate"          OpHelp(""),
23461      /* 142 */ "VDestroy"         OpHelp(""),
23462      /* 143 */ "ToText"           OpHelp(""),
23463      /* 144 */ "ToBlob"           OpHelp(""),
23464      /* 145 */ "ToNumeric"        OpHelp(""),
23465      /* 146 */ "ToInt"            OpHelp(""),
23466      /* 147 */ "ToReal"           OpHelp(""),
23467      /* 148 */ "VOpen"            OpHelp(""),
23468      /* 149 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
23469      /* 150 */ "VNext"            OpHelp(""),
23470      /* 151 */ "VRename"          OpHelp(""),
23471      /* 152 */ "Pagecount"        OpHelp(""),
23472      /* 153 */ "MaxPgcnt"         OpHelp(""),
23473      /* 154 */ "Init"             OpHelp("Start at P2"),
23474      /* 155 */ "Noop"             OpHelp(""),
23475      /* 156 */ "Explain"          OpHelp(""),
23476   };
23477   return azName[i];
23478 }
23479 #endif
23480 
23481 /************** End of opcodes.c *********************************************/
23482 /************** Begin file os_unix.c *****************************************/
23483 /*
23484 ** 2004 May 22
23485 **
23486 ** The author disclaims copyright to this source code.  In place of
23487 ** a legal notice, here is a blessing:
23488 **
23489 **    May you do good and not evil.
23490 **    May you find forgiveness for yourself and forgive others.
23491 **    May you share freely, never taking more than you give.
23492 **
23493 ******************************************************************************
23494 **
23495 ** This file contains the VFS implementation for unix-like operating systems
23496 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
23497 **
23498 ** There are actually several different VFS implementations in this file.
23499 ** The differences are in the way that file locking is done.  The default
23500 ** implementation uses Posix Advisory Locks.  Alternative implementations
23501 ** use flock(), dot-files, various proprietary locking schemas, or simply
23502 ** skip locking all together.
23503 **
23504 ** This source file is organized into divisions where the logic for various
23505 ** subfunctions is contained within the appropriate division.  PLEASE
23506 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
23507 ** in the correct division and should be clearly labeled.
23508 **
23509 ** The layout of divisions is as follows:
23510 **
23511 **   *  General-purpose declarations and utility functions.
23512 **   *  Unique file ID logic used by VxWorks.
23513 **   *  Various locking primitive implementations (all except proxy locking):
23514 **      + for Posix Advisory Locks
23515 **      + for no-op locks
23516 **      + for dot-file locks
23517 **      + for flock() locking
23518 **      + for named semaphore locks (VxWorks only)
23519 **      + for AFP filesystem locks (MacOSX only)
23520 **   *  sqlite3_file methods not associated with locking.
23521 **   *  Definitions of sqlite3_io_methods objects for all locking
23522 **      methods plus "finder" functions for each locking method.
23523 **   *  sqlite3_vfs method implementations.
23524 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
23525 **   *  Definitions of sqlite3_vfs objects for all locking methods
23526 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
23527 */
23528 #if SQLITE_OS_UNIX              /* This file is used on unix only */
23529 
23530 /*
23531 ** There are various methods for file locking used for concurrency
23532 ** control:
23533 **
23534 **   1. POSIX locking (the default),
23535 **   2. No locking,
23536 **   3. Dot-file locking,
23537 **   4. flock() locking,
23538 **   5. AFP locking (OSX only),
23539 **   6. Named POSIX semaphores (VXWorks only),
23540 **   7. proxy locking. (OSX only)
23541 **
23542 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
23543 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
23544 ** selection of the appropriate locking style based on the filesystem
23545 ** where the database is located.
23546 */
23547 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
23548 #  if defined(__APPLE__)
23549 #    define SQLITE_ENABLE_LOCKING_STYLE 1
23550 #  else
23551 #    define SQLITE_ENABLE_LOCKING_STYLE 0
23552 #  endif
23553 #endif
23554 
23555 /*
23556 ** Define the OS_VXWORKS pre-processor macro to 1 if building on
23557 ** vxworks, or 0 otherwise.
23558 */
23559 #ifndef OS_VXWORKS
23560 #  if defined(__RTP__) || defined(_WRS_KERNEL)
23561 #    define OS_VXWORKS 1
23562 #  else
23563 #    define OS_VXWORKS 0
23564 #  endif
23565 #endif
23566 
23567 /*
23568 ** standard include files.
23569 */
23570 #include <sys/types.h>
23571 #include <sys/stat.h>
23572 #include <fcntl.h>
23573 #include <unistd.h>
23574 /* #include <time.h> */
23575 #include <sys/time.h>
23576 #include <errno.h>
23577 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
23578 #include <sys/mman.h>
23579 #endif
23580 
23581 
23582 #if SQLITE_ENABLE_LOCKING_STYLE
23583 # include <sys/ioctl.h>
23584 # if OS_VXWORKS
23585 #  include <semaphore.h>
23586 #  include <limits.h>
23587 # else
23588 #  include <sys/file.h>
23589 #  include <sys/param.h>
23590 # endif
23591 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
23592 
23593 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
23594 # include <sys/mount.h>
23595 #endif
23596 
23597 #ifdef HAVE_UTIME
23598 # include <utime.h>
23599 #endif
23600 
23601 /*
23602 ** Allowed values of unixFile.fsFlags
23603 */
23604 #define SQLITE_FSFLAGS_IS_MSDOS     0x1
23605 
23606 /*
23607 ** If we are to be thread-safe, include the pthreads header and define
23608 ** the SQLITE_UNIX_THREADS macro.
23609 */
23610 #if SQLITE_THREADSAFE
23611 /* # include <pthread.h> */
23612 # define SQLITE_UNIX_THREADS 1
23613 #endif
23614 
23615 /*
23616 ** Default permissions when creating a new file
23617 */
23618 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
23619 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
23620 #endif
23621 
23622 /*
23623 ** Default permissions when creating auto proxy dir
23624 */
23625 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
23626 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
23627 #endif
23628 
23629 /*
23630 ** Maximum supported path-length.
23631 */
23632 #define MAX_PATHNAME 512
23633 
23634 /*
23635 ** Only set the lastErrno if the error code is a real error and not
23636 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
23637 */
23638 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
23639 
23640 /* Forward references */
23641 typedef struct unixShm unixShm;               /* Connection shared memory */
23642 typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
23643 typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
23644 typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
23645 
23646 /*
23647 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
23648 ** cannot be closed immediately. In these cases, instances of the following
23649 ** structure are used to store the file descriptor while waiting for an
23650 ** opportunity to either close or reuse it.
23651 */
23652 struct UnixUnusedFd {
23653   int fd;                   /* File descriptor to close */
23654   int flags;                /* Flags this file descriptor was opened with */
23655   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
23656 };
23657 
23658 /*
23659 ** The unixFile structure is subclass of sqlite3_file specific to the unix
23660 ** VFS implementations.
23661 */
23662 typedef struct unixFile unixFile;
23663 struct unixFile {
23664   sqlite3_io_methods const *pMethod;  /* Always the first entry */
23665   sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
23666   unixInodeInfo *pInode;              /* Info about locks on this inode */
23667   int h;                              /* The file descriptor */
23668   unsigned char eFileLock;            /* The type of lock held on this fd */
23669   unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
23670   int lastErrno;                      /* The unix errno from last I/O error */
23671   void *lockingContext;               /* Locking style specific state */
23672   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
23673   const char *zPath;                  /* Name of the file */
23674   unixShm *pShm;                      /* Shared memory segment information */
23675   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
23676 #if SQLITE_MAX_MMAP_SIZE>0
23677   int nFetchOut;                      /* Number of outstanding xFetch refs */
23678   sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
23679   sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
23680   sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
23681   void *pMapRegion;                   /* Memory mapped region */
23682 #endif
23683 #ifdef __QNXNTO__
23684   int sectorSize;                     /* Device sector size */
23685   int deviceCharacteristics;          /* Precomputed device characteristics */
23686 #endif
23687 #if SQLITE_ENABLE_LOCKING_STYLE
23688   int openFlags;                      /* The flags specified at open() */
23689 #endif
23690 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
23691   unsigned fsFlags;                   /* cached details from statfs() */
23692 #endif
23693 #if OS_VXWORKS
23694   struct vxworksFileId *pId;          /* Unique file ID */
23695 #endif
23696 #ifdef SQLITE_DEBUG
23697   /* The next group of variables are used to track whether or not the
23698   ** transaction counter in bytes 24-27 of database files are updated
23699   ** whenever any part of the database changes.  An assertion fault will
23700   ** occur if a file is updated without also updating the transaction
23701   ** counter.  This test is made to avoid new problems similar to the
23702   ** one described by ticket #3584.
23703   */
23704   unsigned char transCntrChng;   /* True if the transaction counter changed */
23705   unsigned char dbUpdate;        /* True if any part of database file changed */
23706   unsigned char inNormalWrite;   /* True if in a normal write operation */
23707 
23708 #endif
23709 
23710 #ifdef SQLITE_TEST
23711   /* In test mode, increase the size of this structure a bit so that
23712   ** it is larger than the struct CrashFile defined in test6.c.
23713   */
23714   char aPadding[32];
23715 #endif
23716 };
23717 
23718 /* This variable holds the process id (pid) from when the xRandomness()
23719 ** method was called.  If xOpen() is called from a different process id,
23720 ** indicating that a fork() has occurred, the PRNG will be reset.
23721 */
23722 static int randomnessPid = 0;
23723 
23724 /*
23725 ** Allowed values for the unixFile.ctrlFlags bitmask:
23726 */
23727 #define UNIXFILE_EXCL        0x01     /* Connections from one process only */
23728 #define UNIXFILE_RDONLY      0x02     /* Connection is read only */
23729 #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
23730 #ifndef SQLITE_DISABLE_DIRSYNC
23731 # define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
23732 #else
23733 # define UNIXFILE_DIRSYNC    0x00
23734 #endif
23735 #define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
23736 #define UNIXFILE_DELETE      0x20     /* Delete on close */
23737 #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
23738 #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
23739 #define UNIXFILE_WARNED    0x0100     /* verifyDbFile() warnings have been issued */
23740 
23741 /*
23742 ** Include code that is common to all os_*.c files
23743 */
23744 /************** Include os_common.h in the middle of os_unix.c ***************/
23745 /************** Begin file os_common.h ***************************************/
23746 /*
23747 ** 2004 May 22
23748 **
23749 ** The author disclaims copyright to this source code.  In place of
23750 ** a legal notice, here is a blessing:
23751 **
23752 **    May you do good and not evil.
23753 **    May you find forgiveness for yourself and forgive others.
23754 **    May you share freely, never taking more than you give.
23755 **
23756 ******************************************************************************
23757 **
23758 ** This file contains macros and a little bit of code that is common to
23759 ** all of the platform-specific files (os_*.c) and is #included into those
23760 ** files.
23761 **
23762 ** This file should be #included by the os_*.c files only.  It is not a
23763 ** general purpose header file.
23764 */
23765 #ifndef _OS_COMMON_H_
23766 #define _OS_COMMON_H_
23767 
23768 /*
23769 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
23770 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
23771 ** switch.  The following code should catch this problem at compile-time.
23772 */
23773 #ifdef MEMORY_DEBUG
23774 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
23775 #endif
23776 
23777 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
23778 # ifndef SQLITE_DEBUG_OS_TRACE
23779 #   define SQLITE_DEBUG_OS_TRACE 0
23780 # endif
23781   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
23782 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
23783 #else
23784 # define OSTRACE(X)
23785 #endif
23786 
23787 /*
23788 ** Macros for performance tracing.  Normally turned off.  Only works
23789 ** on i486 hardware.
23790 */
23791 #ifdef SQLITE_PERFORMANCE_TRACE
23792 
23793 /*
23794 ** hwtime.h contains inline assembler code for implementing
23795 ** high-performance timing routines.
23796 */
23797 /************** Include hwtime.h in the middle of os_common.h ****************/
23798 /************** Begin file hwtime.h ******************************************/
23799 /*
23800 ** 2008 May 27
23801 **
23802 ** The author disclaims copyright to this source code.  In place of
23803 ** a legal notice, here is a blessing:
23804 **
23805 **    May you do good and not evil.
23806 **    May you find forgiveness for yourself and forgive others.
23807 **    May you share freely, never taking more than you give.
23808 **
23809 ******************************************************************************
23810 **
23811 ** This file contains inline asm code for retrieving "high-performance"
23812 ** counters for x86 class CPUs.
23813 */
23814 #ifndef _HWTIME_H_
23815 #define _HWTIME_H_
23816 
23817 /*
23818 ** The following routine only works on pentium-class (or newer) processors.
23819 ** It uses the RDTSC opcode to read the cycle count value out of the
23820 ** processor and returns that value.  This can be used for high-res
23821 ** profiling.
23822 */
23823 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
23824       (defined(i386) || defined(__i386__) || defined(_M_IX86))
23825 
23826   #if defined(__GNUC__)
23827 
sqlite3Hwtime(void)23828   __inline__ sqlite_uint64 sqlite3Hwtime(void){
23829      unsigned int lo, hi;
23830      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
23831      return (sqlite_uint64)hi << 32 | lo;
23832   }
23833 
23834   #elif defined(_MSC_VER)
23835 
sqlite3Hwtime(void)23836   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
23837      __asm {
23838         rdtsc
23839         ret       ; return value at EDX:EAX
23840      }
23841   }
23842 
23843   #endif
23844 
23845 #elif (defined(__GNUC__) && defined(__x86_64__))
23846 
23847   __inline__ sqlite_uint64 sqlite3Hwtime(void){
23848       unsigned long val;
23849       __asm__ __volatile__ ("rdtsc" : "=A" (val));
23850       return val;
23851   }
23852 
23853 #elif (defined(__GNUC__) && defined(__ppc__))
23854 
23855   __inline__ sqlite_uint64 sqlite3Hwtime(void){
23856       unsigned long long retval;
23857       unsigned long junk;
23858       __asm__ __volatile__ ("\n\
23859           1:      mftbu   %1\n\
23860                   mftb    %L0\n\
23861                   mftbu   %0\n\
23862                   cmpw    %0,%1\n\
23863                   bne     1b"
23864                   : "=r" (retval), "=r" (junk));
23865       return retval;
23866   }
23867 
23868 #else
23869 
23870   #error Need implementation of sqlite3Hwtime() for your platform.
23871 
23872   /*
23873   ** To compile without implementing sqlite3Hwtime() for your platform,
23874   ** you can remove the above #error and use the following
23875   ** stub function.  You will lose timing support for many
23876   ** of the debugging and testing utilities, but it should at
23877   ** least compile and run.
23878   */
23879 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
23880 
23881 #endif
23882 
23883 #endif /* !defined(_HWTIME_H_) */
23884 
23885 /************** End of hwtime.h **********************************************/
23886 /************** Continuing where we left off in os_common.h ******************/
23887 
23888 static sqlite_uint64 g_start;
23889 static sqlite_uint64 g_elapsed;
23890 #define TIMER_START       g_start=sqlite3Hwtime()
23891 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
23892 #define TIMER_ELAPSED     g_elapsed
23893 #else
23894 #define TIMER_START
23895 #define TIMER_END
23896 #define TIMER_ELAPSED     ((sqlite_uint64)0)
23897 #endif
23898 
23899 /*
23900 ** If we compile with the SQLITE_TEST macro set, then the following block
23901 ** of code will give us the ability to simulate a disk I/O error.  This
23902 ** is used for testing the I/O recovery logic.
23903 */
23904 #ifdef SQLITE_TEST
23905 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
23906 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
23907 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
23908 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
23909 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
23910 SQLITE_API int sqlite3_diskfull_pending = 0;
23911 SQLITE_API int sqlite3_diskfull = 0;
23912 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
23913 #define SimulateIOError(CODE)  \
23914   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
23915        || sqlite3_io_error_pending-- == 1 )  \
23916               { local_ioerr(); CODE; }
23917 static void local_ioerr(){
23918   IOTRACE(("IOERR\n"));
23919   sqlite3_io_error_hit++;
23920   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
23921 }
23922 #define SimulateDiskfullError(CODE) \
23923    if( sqlite3_diskfull_pending ){ \
23924      if( sqlite3_diskfull_pending == 1 ){ \
23925        local_ioerr(); \
23926        sqlite3_diskfull = 1; \
23927        sqlite3_io_error_hit = 1; \
23928        CODE; \
23929      }else{ \
23930        sqlite3_diskfull_pending--; \
23931      } \
23932    }
23933 #else
23934 #define SimulateIOErrorBenign(X)
23935 #define SimulateIOError(A)
23936 #define SimulateDiskfullError(A)
23937 #endif
23938 
23939 /*
23940 ** When testing, keep a count of the number of open files.
23941 */
23942 #ifdef SQLITE_TEST
23943 SQLITE_API int sqlite3_open_file_count = 0;
23944 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
23945 #else
23946 #define OpenCounter(X)
23947 #endif
23948 
23949 #endif /* !defined(_OS_COMMON_H_) */
23950 
23951 /************** End of os_common.h *******************************************/
23952 /************** Continuing where we left off in os_unix.c ********************/
23953 
23954 /*
23955 ** Define various macros that are missing from some systems.
23956 */
23957 #ifndef O_LARGEFILE
23958 # define O_LARGEFILE 0
23959 #endif
23960 #ifdef SQLITE_DISABLE_LFS
23961 # undef O_LARGEFILE
23962 # define O_LARGEFILE 0
23963 #endif
23964 #ifndef O_NOFOLLOW
23965 # define O_NOFOLLOW 0
23966 #endif
23967 #ifndef O_BINARY
23968 # define O_BINARY 0
23969 #endif
23970 
23971 /*
23972 ** The threadid macro resolves to the thread-id or to 0.  Used for
23973 ** testing and debugging only.
23974 */
23975 #if SQLITE_THREADSAFE
23976 #define threadid pthread_self()
23977 #else
23978 #define threadid 0
23979 #endif
23980 
23981 /*
23982 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
23983 */
23984 #if !defined(HAVE_MREMAP)
23985 # if defined(__linux__) && defined(_GNU_SOURCE)
23986 #  define HAVE_MREMAP 1
23987 # else
23988 #  define HAVE_MREMAP 0
23989 # endif
23990 #endif
23991 
23992 /*
23993 ** Different Unix systems declare open() in different ways.  Same use
23994 ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
23995 ** The difference is important when using a pointer to the function.
23996 **
23997 ** The safest way to deal with the problem is to always use this wrapper
23998 ** which always has the same well-defined interface.
23999 */
24000 static int posixOpen(const char *zFile, int flags, int mode){
24001   return open(zFile, flags, mode);
24002 }
24003 
24004 /*
24005 ** On some systems, calls to fchown() will trigger a message in a security
24006 ** log if they come from non-root processes.  So avoid calling fchown() if
24007 ** we are not running as root.
24008 */
24009 static int posixFchown(int fd, uid_t uid, gid_t gid){
24010   return geteuid() ? 0 : fchown(fd,uid,gid);
24011 }
24012 
24013 /* Forward reference */
24014 static int openDirectory(const char*, int*);
24015 
24016 /*
24017 ** Many system calls are accessed through pointer-to-functions so that
24018 ** they may be overridden at runtime to facilitate fault injection during
24019 ** testing and sandboxing.  The following array holds the names and pointers
24020 ** to all overrideable system calls.
24021 */
24022 static struct unix_syscall {
24023   const char *zName;            /* Name of the system call */
24024   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
24025   sqlite3_syscall_ptr pDefault; /* Default value */
24026 } aSyscall[] = {
24027   { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
24028 #define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
24029 
24030   { "close",        (sqlite3_syscall_ptr)close,      0  },
24031 #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
24032 
24033   { "access",       (sqlite3_syscall_ptr)access,     0  },
24034 #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
24035 
24036   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
24037 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
24038 
24039   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
24040 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
24041 
24042 /*
24043 ** The DJGPP compiler environment looks mostly like Unix, but it
24044 ** lacks the fcntl() system call.  So redefine fcntl() to be something
24045 ** that always succeeds.  This means that locking does not occur under
24046 ** DJGPP.  But it is DOS - what did you expect?
24047 */
24048 #ifdef __DJGPP__
24049   { "fstat",        0,                 0  },
24050 #define osFstat(a,b,c)    0
24051 #else
24052   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
24053 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
24054 #endif
24055 
24056   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
24057 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
24058 
24059   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
24060 #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
24061 
24062   { "read",         (sqlite3_syscall_ptr)read,       0  },
24063 #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
24064 
24065 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24066   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
24067 #else
24068   { "pread",        (sqlite3_syscall_ptr)0,          0  },
24069 #endif
24070 #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
24071 
24072 #if defined(USE_PREAD64)
24073   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
24074 #else
24075   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
24076 #endif
24077 #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
24078 
24079   { "write",        (sqlite3_syscall_ptr)write,      0  },
24080 #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
24081 
24082 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24083   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
24084 #else
24085   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
24086 #endif
24087 #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
24088                     aSyscall[12].pCurrent)
24089 
24090 #if defined(USE_PREAD64)
24091   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
24092 #else
24093   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
24094 #endif
24095 #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
24096                     aSyscall[13].pCurrent)
24097 
24098   { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
24099 #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
24100 
24101 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
24102   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
24103 #else
24104   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
24105 #endif
24106 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
24107 
24108   { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
24109 #define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
24110 
24111   { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
24112 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
24113 
24114   { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
24115 #define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
24116 
24117   { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
24118 #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
24119 
24120   { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
24121 #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
24122 
24123 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
24124   { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
24125 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
24126 
24127   { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
24128 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
24129 
24130 #if HAVE_MREMAP
24131   { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
24132 #else
24133   { "mremap",       (sqlite3_syscall_ptr)0,               0 },
24134 #endif
24135 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
24136 #endif
24137 
24138 }; /* End of the overrideable system calls */
24139 
24140 /*
24141 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
24142 ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
24143 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
24144 ** system call named zName.
24145 */
24146 static int unixSetSystemCall(
24147   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
24148   const char *zName,            /* Name of system call to override */
24149   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
24150 ){
24151   unsigned int i;
24152   int rc = SQLITE_NOTFOUND;
24153 
24154   UNUSED_PARAMETER(pNotUsed);
24155   if( zName==0 ){
24156     /* If no zName is given, restore all system calls to their default
24157     ** settings and return NULL
24158     */
24159     rc = SQLITE_OK;
24160     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24161       if( aSyscall[i].pDefault ){
24162         aSyscall[i].pCurrent = aSyscall[i].pDefault;
24163       }
24164     }
24165   }else{
24166     /* If zName is specified, operate on only the one system call
24167     ** specified.
24168     */
24169     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24170       if( strcmp(zName, aSyscall[i].zName)==0 ){
24171         if( aSyscall[i].pDefault==0 ){
24172           aSyscall[i].pDefault = aSyscall[i].pCurrent;
24173         }
24174         rc = SQLITE_OK;
24175         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
24176         aSyscall[i].pCurrent = pNewFunc;
24177         break;
24178       }
24179     }
24180   }
24181   return rc;
24182 }
24183 
24184 /*
24185 ** Return the value of a system call.  Return NULL if zName is not a
24186 ** recognized system call name.  NULL is also returned if the system call
24187 ** is currently undefined.
24188 */
24189 static sqlite3_syscall_ptr unixGetSystemCall(
24190   sqlite3_vfs *pNotUsed,
24191   const char *zName
24192 ){
24193   unsigned int i;
24194 
24195   UNUSED_PARAMETER(pNotUsed);
24196   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24197     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
24198   }
24199   return 0;
24200 }
24201 
24202 /*
24203 ** Return the name of the first system call after zName.  If zName==NULL
24204 ** then return the name of the first system call.  Return NULL if zName
24205 ** is the last system call or if zName is not the name of a valid
24206 ** system call.
24207 */
24208 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
24209   int i = -1;
24210 
24211   UNUSED_PARAMETER(p);
24212   if( zName ){
24213     for(i=0; i<ArraySize(aSyscall)-1; i++){
24214       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
24215     }
24216   }
24217   for(i++; i<ArraySize(aSyscall); i++){
24218     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
24219   }
24220   return 0;
24221 }
24222 
24223 /*
24224 ** Do not accept any file descriptor less than this value, in order to avoid
24225 ** opening database file using file descriptors that are commonly used for
24226 ** standard input, output, and error.
24227 */
24228 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
24229 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
24230 #endif
24231 
24232 /*
24233 ** Invoke open().  Do so multiple times, until it either succeeds or
24234 ** fails for some reason other than EINTR.
24235 **
24236 ** If the file creation mode "m" is 0 then set it to the default for
24237 ** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
24238 ** 0644) as modified by the system umask.  If m is not 0, then
24239 ** make the file creation mode be exactly m ignoring the umask.
24240 **
24241 ** The m parameter will be non-zero only when creating -wal, -journal,
24242 ** and -shm files.  We want those files to have *exactly* the same
24243 ** permissions as their original database, unadulterated by the umask.
24244 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
24245 ** transaction crashes and leaves behind hot journals, then any
24246 ** process that is able to write to the database will also be able to
24247 ** recover the hot journals.
24248 */
24249 static int robust_open(const char *z, int f, mode_t m){
24250   int fd;
24251   mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
24252   while(1){
24253 #if defined(O_CLOEXEC)
24254     fd = osOpen(z,f|O_CLOEXEC,m2);
24255 #else
24256     fd = osOpen(z,f,m2);
24257 #endif
24258     if( fd<0 ){
24259       if( errno==EINTR ) continue;
24260       break;
24261     }
24262     if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
24263     osClose(fd);
24264     sqlite3_log(SQLITE_WARNING,
24265                 "attempt to open \"%s\" as file descriptor %d", z, fd);
24266     fd = -1;
24267     if( osOpen("/dev/null", f, m)<0 ) break;
24268   }
24269   if( fd>=0 ){
24270     if( m!=0 ){
24271       struct stat statbuf;
24272       if( osFstat(fd, &statbuf)==0
24273        && statbuf.st_size==0
24274        && (statbuf.st_mode&0777)!=m
24275       ){
24276         osFchmod(fd, m);
24277       }
24278     }
24279 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
24280     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
24281 #endif
24282   }
24283   return fd;
24284 }
24285 
24286 /*
24287 ** Helper functions to obtain and relinquish the global mutex. The
24288 ** global mutex is used to protect the unixInodeInfo and
24289 ** vxworksFileId objects used by this file, all of which may be
24290 ** shared by multiple threads.
24291 **
24292 ** Function unixMutexHeld() is used to assert() that the global mutex
24293 ** is held when required. This function is only used as part of assert()
24294 ** statements. e.g.
24295 **
24296 **   unixEnterMutex()
24297 **     assert( unixMutexHeld() );
24298 **   unixEnterLeave()
24299 */
24300 static void unixEnterMutex(void){
24301   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24302 }
24303 static void unixLeaveMutex(void){
24304   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24305 }
24306 #ifdef SQLITE_DEBUG
24307 static int unixMutexHeld(void) {
24308   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24309 }
24310 #endif
24311 
24312 
24313 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
24314 /*
24315 ** Helper function for printing out trace information from debugging
24316 ** binaries. This returns the string represetation of the supplied
24317 ** integer lock-type.
24318 */
24319 static const char *azFileLock(int eFileLock){
24320   switch( eFileLock ){
24321     case NO_LOCK: return "NONE";
24322     case SHARED_LOCK: return "SHARED";
24323     case RESERVED_LOCK: return "RESERVED";
24324     case PENDING_LOCK: return "PENDING";
24325     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
24326   }
24327   return "ERROR";
24328 }
24329 #endif
24330 
24331 #ifdef SQLITE_LOCK_TRACE
24332 /*
24333 ** Print out information about all locking operations.
24334 **
24335 ** This routine is used for troubleshooting locks on multithreaded
24336 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
24337 ** command-line option on the compiler.  This code is normally
24338 ** turned off.
24339 */
24340 static int lockTrace(int fd, int op, struct flock *p){
24341   char *zOpName, *zType;
24342   int s;
24343   int savedErrno;
24344   if( op==F_GETLK ){
24345     zOpName = "GETLK";
24346   }else if( op==F_SETLK ){
24347     zOpName = "SETLK";
24348   }else{
24349     s = osFcntl(fd, op, p);
24350     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
24351     return s;
24352   }
24353   if( p->l_type==F_RDLCK ){
24354     zType = "RDLCK";
24355   }else if( p->l_type==F_WRLCK ){
24356     zType = "WRLCK";
24357   }else if( p->l_type==F_UNLCK ){
24358     zType = "UNLCK";
24359   }else{
24360     assert( 0 );
24361   }
24362   assert( p->l_whence==SEEK_SET );
24363   s = osFcntl(fd, op, p);
24364   savedErrno = errno;
24365   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
24366      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
24367      (int)p->l_pid, s);
24368   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
24369     struct flock l2;
24370     l2 = *p;
24371     osFcntl(fd, F_GETLK, &l2);
24372     if( l2.l_type==F_RDLCK ){
24373       zType = "RDLCK";
24374     }else if( l2.l_type==F_WRLCK ){
24375       zType = "WRLCK";
24376     }else if( l2.l_type==F_UNLCK ){
24377       zType = "UNLCK";
24378     }else{
24379       assert( 0 );
24380     }
24381     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
24382        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
24383   }
24384   errno = savedErrno;
24385   return s;
24386 }
24387 #undef osFcntl
24388 #define osFcntl lockTrace
24389 #endif /* SQLITE_LOCK_TRACE */
24390 
24391 /*
24392 ** Retry ftruncate() calls that fail due to EINTR
24393 */
24394 static int robust_ftruncate(int h, sqlite3_int64 sz){
24395   int rc;
24396   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
24397   return rc;
24398 }
24399 
24400 /*
24401 ** This routine translates a standard POSIX errno code into something
24402 ** useful to the clients of the sqlite3 functions.  Specifically, it is
24403 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
24404 ** and a variety of "please close the file descriptor NOW" errors into
24405 ** SQLITE_IOERR
24406 **
24407 ** Errors during initialization of locks, or file system support for locks,
24408 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
24409 */
24410 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
24411   switch (posixError) {
24412 #if 0
24413   /* At one point this code was not commented out. In theory, this branch
24414   ** should never be hit, as this function should only be called after
24415   ** a locking-related function (i.e. fcntl()) has returned non-zero with
24416   ** the value of errno as the first argument. Since a system call has failed,
24417   ** errno should be non-zero.
24418   **
24419   ** Despite this, if errno really is zero, we still don't want to return
24420   ** SQLITE_OK. The system call failed, and *some* SQLite error should be
24421   ** propagated back to the caller. Commenting this branch out means errno==0
24422   ** will be handled by the "default:" case below.
24423   */
24424   case 0:
24425     return SQLITE_OK;
24426 #endif
24427 
24428   case EAGAIN:
24429   case ETIMEDOUT:
24430   case EBUSY:
24431   case EINTR:
24432   case ENOLCK:
24433     /* random NFS retry error, unless during file system support
24434      * introspection, in which it actually means what it says */
24435     return SQLITE_BUSY;
24436 
24437   case EACCES:
24438     /* EACCES is like EAGAIN during locking operations, but not any other time*/
24439     if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
24440         (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
24441         (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
24442         (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
24443       return SQLITE_BUSY;
24444     }
24445     /* else fall through */
24446   case EPERM:
24447     return SQLITE_PERM;
24448 
24449   /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
24450   ** this module never makes such a call. And the code in SQLite itself
24451   ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
24452   ** this case is also commented out. If the system does set errno to EDEADLK,
24453   ** the default SQLITE_IOERR_XXX code will be returned. */
24454 #if 0
24455   case EDEADLK:
24456     return SQLITE_IOERR_BLOCKED;
24457 #endif
24458 
24459 #if EOPNOTSUPP!=ENOTSUP
24460   case EOPNOTSUPP:
24461     /* something went terribly awry, unless during file system support
24462      * introspection, in which it actually means what it says */
24463 #endif
24464 #ifdef ENOTSUP
24465   case ENOTSUP:
24466     /* invalid fd, unless during file system support introspection, in which
24467      * it actually means what it says */
24468 #endif
24469   case EIO:
24470   case EBADF:
24471   case EINVAL:
24472   case ENOTCONN:
24473   case ENODEV:
24474   case ENXIO:
24475   case ENOENT:
24476 #ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
24477   case ESTALE:
24478 #endif
24479   case ENOSYS:
24480     /* these should force the client to close the file and reconnect */
24481 
24482   default:
24483     return sqliteIOErr;
24484   }
24485 }
24486 
24487 
24488 /******************************************************************************
24489 ****************** Begin Unique File ID Utility Used By VxWorks ***************
24490 **
24491 ** On most versions of unix, we can get a unique ID for a file by concatenating
24492 ** the device number and the inode number.  But this does not work on VxWorks.
24493 ** On VxWorks, a unique file id must be based on the canonical filename.
24494 **
24495 ** A pointer to an instance of the following structure can be used as a
24496 ** unique file ID in VxWorks.  Each instance of this structure contains
24497 ** a copy of the canonical filename.  There is also a reference count.
24498 ** The structure is reclaimed when the number of pointers to it drops to
24499 ** zero.
24500 **
24501 ** There are never very many files open at one time and lookups are not
24502 ** a performance-critical path, so it is sufficient to put these
24503 ** structures on a linked list.
24504 */
24505 struct vxworksFileId {
24506   struct vxworksFileId *pNext;  /* Next in a list of them all */
24507   int nRef;                     /* Number of references to this one */
24508   int nName;                    /* Length of the zCanonicalName[] string */
24509   char *zCanonicalName;         /* Canonical filename */
24510 };
24511 
24512 #if OS_VXWORKS
24513 /*
24514 ** All unique filenames are held on a linked list headed by this
24515 ** variable:
24516 */
24517 static struct vxworksFileId *vxworksFileList = 0;
24518 
24519 /*
24520 ** Simplify a filename into its canonical form
24521 ** by making the following changes:
24522 **
24523 **  * removing any trailing and duplicate /
24524 **  * convert /./ into just /
24525 **  * convert /A/../ where A is any simple name into just /
24526 **
24527 ** Changes are made in-place.  Return the new name length.
24528 **
24529 ** The original filename is in z[0..n-1].  Return the number of
24530 ** characters in the simplified name.
24531 */
24532 static int vxworksSimplifyName(char *z, int n){
24533   int i, j;
24534   while( n>1 && z[n-1]=='/' ){ n--; }
24535   for(i=j=0; i<n; i++){
24536     if( z[i]=='/' ){
24537       if( z[i+1]=='/' ) continue;
24538       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
24539         i += 1;
24540         continue;
24541       }
24542       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
24543         while( j>0 && z[j-1]!='/' ){ j--; }
24544         if( j>0 ){ j--; }
24545         i += 2;
24546         continue;
24547       }
24548     }
24549     z[j++] = z[i];
24550   }
24551   z[j] = 0;
24552   return j;
24553 }
24554 
24555 /*
24556 ** Find a unique file ID for the given absolute pathname.  Return
24557 ** a pointer to the vxworksFileId object.  This pointer is the unique
24558 ** file ID.
24559 **
24560 ** The nRef field of the vxworksFileId object is incremented before
24561 ** the object is returned.  A new vxworksFileId object is created
24562 ** and added to the global list if necessary.
24563 **
24564 ** If a memory allocation error occurs, return NULL.
24565 */
24566 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
24567   struct vxworksFileId *pNew;         /* search key and new file ID */
24568   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
24569   int n;                              /* Length of zAbsoluteName string */
24570 
24571   assert( zAbsoluteName[0]=='/' );
24572   n = (int)strlen(zAbsoluteName);
24573   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
24574   if( pNew==0 ) return 0;
24575   pNew->zCanonicalName = (char*)&pNew[1];
24576   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
24577   n = vxworksSimplifyName(pNew->zCanonicalName, n);
24578 
24579   /* Search for an existing entry that matching the canonical name.
24580   ** If found, increment the reference count and return a pointer to
24581   ** the existing file ID.
24582   */
24583   unixEnterMutex();
24584   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
24585     if( pCandidate->nName==n
24586      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
24587     ){
24588        sqlite3_free(pNew);
24589        pCandidate->nRef++;
24590        unixLeaveMutex();
24591        return pCandidate;
24592     }
24593   }
24594 
24595   /* No match was found.  We will make a new file ID */
24596   pNew->nRef = 1;
24597   pNew->nName = n;
24598   pNew->pNext = vxworksFileList;
24599   vxworksFileList = pNew;
24600   unixLeaveMutex();
24601   return pNew;
24602 }
24603 
24604 /*
24605 ** Decrement the reference count on a vxworksFileId object.  Free
24606 ** the object when the reference count reaches zero.
24607 */
24608 static void vxworksReleaseFileId(struct vxworksFileId *pId){
24609   unixEnterMutex();
24610   assert( pId->nRef>0 );
24611   pId->nRef--;
24612   if( pId->nRef==0 ){
24613     struct vxworksFileId **pp;
24614     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
24615     assert( *pp==pId );
24616     *pp = pId->pNext;
24617     sqlite3_free(pId);
24618   }
24619   unixLeaveMutex();
24620 }
24621 #endif /* OS_VXWORKS */
24622 /*************** End of Unique File ID Utility Used By VxWorks ****************
24623 ******************************************************************************/
24624 
24625 
24626 /******************************************************************************
24627 *************************** Posix Advisory Locking ****************************
24628 **
24629 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
24630 ** section 6.5.2.2 lines 483 through 490 specify that when a process
24631 ** sets or clears a lock, that operation overrides any prior locks set
24632 ** by the same process.  It does not explicitly say so, but this implies
24633 ** that it overrides locks set by the same process using a different
24634 ** file descriptor.  Consider this test case:
24635 **
24636 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
24637 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
24638 **
24639 ** Suppose ./file1 and ./file2 are really the same file (because
24640 ** one is a hard or symbolic link to the other) then if you set
24641 ** an exclusive lock on fd1, then try to get an exclusive lock
24642 ** on fd2, it works.  I would have expected the second lock to
24643 ** fail since there was already a lock on the file due to fd1.
24644 ** But not so.  Since both locks came from the same process, the
24645 ** second overrides the first, even though they were on different
24646 ** file descriptors opened on different file names.
24647 **
24648 ** This means that we cannot use POSIX locks to synchronize file access
24649 ** among competing threads of the same process.  POSIX locks will work fine
24650 ** to synchronize access for threads in separate processes, but not
24651 ** threads within the same process.
24652 **
24653 ** To work around the problem, SQLite has to manage file locks internally
24654 ** on its own.  Whenever a new database is opened, we have to find the
24655 ** specific inode of the database file (the inode is determined by the
24656 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
24657 ** and check for locks already existing on that inode.  When locks are
24658 ** created or removed, we have to look at our own internal record of the
24659 ** locks to see if another thread has previously set a lock on that same
24660 ** inode.
24661 **
24662 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
24663 ** For VxWorks, we have to use the alternative unique ID system based on
24664 ** canonical filename and implemented in the previous division.)
24665 **
24666 ** The sqlite3_file structure for POSIX is no longer just an integer file
24667 ** descriptor.  It is now a structure that holds the integer file
24668 ** descriptor and a pointer to a structure that describes the internal
24669 ** locks on the corresponding inode.  There is one locking structure
24670 ** per inode, so if the same inode is opened twice, both unixFile structures
24671 ** point to the same locking structure.  The locking structure keeps
24672 ** a reference count (so we will know when to delete it) and a "cnt"
24673 ** field that tells us its internal lock status.  cnt==0 means the
24674 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
24675 ** cnt>0 means there are cnt shared locks on the file.
24676 **
24677 ** Any attempt to lock or unlock a file first checks the locking
24678 ** structure.  The fcntl() system call is only invoked to set a
24679 ** POSIX lock if the internal lock structure transitions between
24680 ** a locked and an unlocked state.
24681 **
24682 ** But wait:  there are yet more problems with POSIX advisory locks.
24683 **
24684 ** If you close a file descriptor that points to a file that has locks,
24685 ** all locks on that file that are owned by the current process are
24686 ** released.  To work around this problem, each unixInodeInfo object
24687 ** maintains a count of the number of pending locks on tha inode.
24688 ** When an attempt is made to close an unixFile, if there are
24689 ** other unixFile open on the same inode that are holding locks, the call
24690 ** to close() the file descriptor is deferred until all of the locks clear.
24691 ** The unixInodeInfo structure keeps a list of file descriptors that need to
24692 ** be closed and that list is walked (and cleared) when the last lock
24693 ** clears.
24694 **
24695 ** Yet another problem:  LinuxThreads do not play well with posix locks.
24696 **
24697 ** Many older versions of linux use the LinuxThreads library which is
24698 ** not posix compliant.  Under LinuxThreads, a lock created by thread
24699 ** A cannot be modified or overridden by a different thread B.
24700 ** Only thread A can modify the lock.  Locking behavior is correct
24701 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
24702 ** on linux - with NPTL a lock created by thread A can override locks
24703 ** in thread B.  But there is no way to know at compile-time which
24704 ** threading library is being used.  So there is no way to know at
24705 ** compile-time whether or not thread A can override locks on thread B.
24706 ** One has to do a run-time check to discover the behavior of the
24707 ** current process.
24708 **
24709 ** SQLite used to support LinuxThreads.  But support for LinuxThreads
24710 ** was dropped beginning with version 3.7.0.  SQLite will still work with
24711 ** LinuxThreads provided that (1) there is no more than one connection
24712 ** per database file in the same process and (2) database connections
24713 ** do not move across threads.
24714 */
24715 
24716 /*
24717 ** An instance of the following structure serves as the key used
24718 ** to locate a particular unixInodeInfo object.
24719 */
24720 struct unixFileId {
24721   dev_t dev;                  /* Device number */
24722 #if OS_VXWORKS
24723   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
24724 #else
24725   ino_t ino;                  /* Inode number */
24726 #endif
24727 };
24728 
24729 /*
24730 ** An instance of the following structure is allocated for each open
24731 ** inode.  Or, on LinuxThreads, there is one of these structures for
24732 ** each inode opened by each thread.
24733 **
24734 ** A single inode can have multiple file descriptors, so each unixFile
24735 ** structure contains a pointer to an instance of this object and this
24736 ** object keeps a count of the number of unixFile pointing to it.
24737 */
24738 struct unixInodeInfo {
24739   struct unixFileId fileId;       /* The lookup key */
24740   int nShared;                    /* Number of SHARED locks held */
24741   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
24742   unsigned char bProcessLock;     /* An exclusive process lock is held */
24743   int nRef;                       /* Number of pointers to this structure */
24744   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
24745   int nLock;                      /* Number of outstanding file locks */
24746   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
24747   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
24748   unixInodeInfo *pPrev;           /*    .... doubly linked */
24749 #if SQLITE_ENABLE_LOCKING_STYLE
24750   unsigned long long sharedByte;  /* for AFP simulated shared lock */
24751 #endif
24752 #if OS_VXWORKS
24753   sem_t *pSem;                    /* Named POSIX semaphore */
24754   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
24755 #endif
24756 };
24757 
24758 /*
24759 ** A lists of all unixInodeInfo objects.
24760 */
24761 static unixInodeInfo *inodeList = 0;
24762 
24763 /*
24764 **
24765 ** This function - unixLogError_x(), is only ever called via the macro
24766 ** unixLogError().
24767 **
24768 ** It is invoked after an error occurs in an OS function and errno has been
24769 ** set. It logs a message using sqlite3_log() containing the current value of
24770 ** errno and, if possible, the human-readable equivalent from strerror() or
24771 ** strerror_r().
24772 **
24773 ** The first argument passed to the macro should be the error code that
24774 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
24775 ** The two subsequent arguments should be the name of the OS function that
24776 ** failed (e.g. "unlink", "open") and the associated file-system path,
24777 ** if any.
24778 */
24779 #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
24780 static int unixLogErrorAtLine(
24781   int errcode,                    /* SQLite error code */
24782   const char *zFunc,              /* Name of OS function that failed */
24783   const char *zPath,              /* File path associated with error */
24784   int iLine                       /* Source line number where error occurred */
24785 ){
24786   char *zErr;                     /* Message from strerror() or equivalent */
24787   int iErrno = errno;             /* Saved syscall error number */
24788 
24789   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
24790   ** the strerror() function to obtain the human-readable error message
24791   ** equivalent to errno. Otherwise, use strerror_r().
24792   */
24793 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
24794   char aErr[80];
24795   memset(aErr, 0, sizeof(aErr));
24796   zErr = aErr;
24797 
24798   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
24799   ** assume that the system provides the GNU version of strerror_r() that
24800   ** returns a pointer to a buffer containing the error message. That pointer
24801   ** may point to aErr[], or it may point to some static storage somewhere.
24802   ** Otherwise, assume that the system provides the POSIX version of
24803   ** strerror_r(), which always writes an error message into aErr[].
24804   **
24805   ** If the code incorrectly assumes that it is the POSIX version that is
24806   ** available, the error message will often be an empty string. Not a
24807   ** huge problem. Incorrectly concluding that the GNU version is available
24808   ** could lead to a segfault though.
24809   */
24810 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
24811   zErr =
24812 # endif
24813   strerror_r(iErrno, aErr, sizeof(aErr)-1);
24814 
24815 #elif SQLITE_THREADSAFE
24816   /* This is a threadsafe build, but strerror_r() is not available. */
24817   zErr = "";
24818 #else
24819   /* Non-threadsafe build, use strerror(). */
24820   zErr = strerror(iErrno);
24821 #endif
24822 
24823   if( zPath==0 ) zPath = "";
24824   sqlite3_log(errcode,
24825       "os_unix.c:%d: (%d) %s(%s) - %s",
24826       iLine, iErrno, zFunc, zPath, zErr
24827   );
24828 
24829   return errcode;
24830 }
24831 
24832 /*
24833 ** Close a file descriptor.
24834 **
24835 ** We assume that close() almost always works, since it is only in a
24836 ** very sick application or on a very sick platform that it might fail.
24837 ** If it does fail, simply leak the file descriptor, but do log the
24838 ** error.
24839 **
24840 ** Note that it is not safe to retry close() after EINTR since the
24841 ** file descriptor might have already been reused by another thread.
24842 ** So we don't even try to recover from an EINTR.  Just log the error
24843 ** and move on.
24844 */
24845 static void robust_close(unixFile *pFile, int h, int lineno){
24846   if( osClose(h) ){
24847     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
24848                        pFile ? pFile->zPath : 0, lineno);
24849   }
24850 }
24851 
24852 /*
24853 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
24854 */
24855 static void closePendingFds(unixFile *pFile){
24856   unixInodeInfo *pInode = pFile->pInode;
24857   UnixUnusedFd *p;
24858   UnixUnusedFd *pNext;
24859   for(p=pInode->pUnused; p; p=pNext){
24860     pNext = p->pNext;
24861     robust_close(pFile, p->fd, __LINE__);
24862     sqlite3_free(p);
24863   }
24864   pInode->pUnused = 0;
24865 }
24866 
24867 /*
24868 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
24869 **
24870 ** The mutex entered using the unixEnterMutex() function must be held
24871 ** when this function is called.
24872 */
24873 static void releaseInodeInfo(unixFile *pFile){
24874   unixInodeInfo *pInode = pFile->pInode;
24875   assert( unixMutexHeld() );
24876   if( ALWAYS(pInode) ){
24877     pInode->nRef--;
24878     if( pInode->nRef==0 ){
24879       assert( pInode->pShmNode==0 );
24880       closePendingFds(pFile);
24881       if( pInode->pPrev ){
24882         assert( pInode->pPrev->pNext==pInode );
24883         pInode->pPrev->pNext = pInode->pNext;
24884       }else{
24885         assert( inodeList==pInode );
24886         inodeList = pInode->pNext;
24887       }
24888       if( pInode->pNext ){
24889         assert( pInode->pNext->pPrev==pInode );
24890         pInode->pNext->pPrev = pInode->pPrev;
24891       }
24892       sqlite3_free(pInode);
24893     }
24894   }
24895 }
24896 
24897 /*
24898 ** Given a file descriptor, locate the unixInodeInfo object that
24899 ** describes that file descriptor.  Create a new one if necessary.  The
24900 ** return value might be uninitialized if an error occurs.
24901 **
24902 ** The mutex entered using the unixEnterMutex() function must be held
24903 ** when this function is called.
24904 **
24905 ** Return an appropriate error code.
24906 */
24907 static int findInodeInfo(
24908   unixFile *pFile,               /* Unix file with file desc used in the key */
24909   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
24910 ){
24911   int rc;                        /* System call return code */
24912   int fd;                        /* The file descriptor for pFile */
24913   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
24914   struct stat statbuf;           /* Low-level file information */
24915   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
24916 
24917   assert( unixMutexHeld() );
24918 
24919   /* Get low-level information about the file that we can used to
24920   ** create a unique name for the file.
24921   */
24922   fd = pFile->h;
24923   rc = osFstat(fd, &statbuf);
24924   if( rc!=0 ){
24925     pFile->lastErrno = errno;
24926 #ifdef EOVERFLOW
24927     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
24928 #endif
24929     return SQLITE_IOERR;
24930   }
24931 
24932 #ifdef __APPLE__
24933   /* On OS X on an msdos filesystem, the inode number is reported
24934   ** incorrectly for zero-size files.  See ticket #3260.  To work
24935   ** around this problem (we consider it a bug in OS X, not SQLite)
24936   ** we always increase the file size to 1 by writing a single byte
24937   ** prior to accessing the inode number.  The one byte written is
24938   ** an ASCII 'S' character which also happens to be the first byte
24939   ** in the header of every SQLite database.  In this way, if there
24940   ** is a race condition such that another thread has already populated
24941   ** the first page of the database, no damage is done.
24942   */
24943   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
24944     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
24945     if( rc!=1 ){
24946       pFile->lastErrno = errno;
24947       return SQLITE_IOERR;
24948     }
24949     rc = osFstat(fd, &statbuf);
24950     if( rc!=0 ){
24951       pFile->lastErrno = errno;
24952       return SQLITE_IOERR;
24953     }
24954   }
24955 #endif
24956 
24957   memset(&fileId, 0, sizeof(fileId));
24958   fileId.dev = statbuf.st_dev;
24959 #if OS_VXWORKS
24960   fileId.pId = pFile->pId;
24961 #else
24962   fileId.ino = statbuf.st_ino;
24963 #endif
24964   pInode = inodeList;
24965   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
24966     pInode = pInode->pNext;
24967   }
24968   if( pInode==0 ){
24969     pInode = sqlite3_malloc( sizeof(*pInode) );
24970     if( pInode==0 ){
24971       return SQLITE_NOMEM;
24972     }
24973     memset(pInode, 0, sizeof(*pInode));
24974     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
24975     pInode->nRef = 1;
24976     pInode->pNext = inodeList;
24977     pInode->pPrev = 0;
24978     if( inodeList ) inodeList->pPrev = pInode;
24979     inodeList = pInode;
24980   }else{
24981     pInode->nRef++;
24982   }
24983   *ppInode = pInode;
24984   return SQLITE_OK;
24985 }
24986 
24987 /*
24988 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
24989 */
24990 static int fileHasMoved(unixFile *pFile){
24991   struct stat buf;
24992   return pFile->pInode!=0 &&
24993          (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
24994 }
24995 
24996 
24997 /*
24998 ** Check a unixFile that is a database.  Verify the following:
24999 **
25000 ** (1) There is exactly one hard link on the file
25001 ** (2) The file is not a symbolic link
25002 ** (3) The file has not been renamed or unlinked
25003 **
25004 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
25005 */
25006 static void verifyDbFile(unixFile *pFile){
25007   struct stat buf;
25008   int rc;
25009   if( pFile->ctrlFlags & UNIXFILE_WARNED ){
25010     /* One or more of the following warnings have already been issued.  Do not
25011     ** repeat them so as not to clutter the error log */
25012     return;
25013   }
25014   rc = osFstat(pFile->h, &buf);
25015   if( rc!=0 ){
25016     sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
25017     pFile->ctrlFlags |= UNIXFILE_WARNED;
25018     return;
25019   }
25020   if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
25021     sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
25022     pFile->ctrlFlags |= UNIXFILE_WARNED;
25023     return;
25024   }
25025   if( buf.st_nlink>1 ){
25026     sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
25027     pFile->ctrlFlags |= UNIXFILE_WARNED;
25028     return;
25029   }
25030   if( fileHasMoved(pFile) ){
25031     sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
25032     pFile->ctrlFlags |= UNIXFILE_WARNED;
25033     return;
25034   }
25035 }
25036 
25037 
25038 /*
25039 ** This routine checks if there is a RESERVED lock held on the specified
25040 ** file by this or any other process. If such a lock is held, set *pResOut
25041 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
25042 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25043 */
25044 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
25045   int rc = SQLITE_OK;
25046   int reserved = 0;
25047   unixFile *pFile = (unixFile*)id;
25048 
25049   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25050 
25051   assert( pFile );
25052   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
25053 
25054   /* Check if a thread in this process holds such a lock */
25055   if( pFile->pInode->eFileLock>SHARED_LOCK ){
25056     reserved = 1;
25057   }
25058 
25059   /* Otherwise see if some other process holds it.
25060   */
25061 #ifndef __DJGPP__
25062   if( !reserved && !pFile->pInode->bProcessLock ){
25063     struct flock lock;
25064     lock.l_whence = SEEK_SET;
25065     lock.l_start = RESERVED_BYTE;
25066     lock.l_len = 1;
25067     lock.l_type = F_WRLCK;
25068     if( osFcntl(pFile->h, F_GETLK, &lock) ){
25069       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
25070       pFile->lastErrno = errno;
25071     } else if( lock.l_type!=F_UNLCK ){
25072       reserved = 1;
25073     }
25074   }
25075 #endif
25076 
25077   unixLeaveMutex();
25078   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
25079 
25080   *pResOut = reserved;
25081   return rc;
25082 }
25083 
25084 /*
25085 ** Attempt to set a system-lock on the file pFile.  The lock is
25086 ** described by pLock.
25087 **
25088 ** If the pFile was opened read/write from unix-excl, then the only lock
25089 ** ever obtained is an exclusive lock, and it is obtained exactly once
25090 ** the first time any lock is attempted.  All subsequent system locking
25091 ** operations become no-ops.  Locking operations still happen internally,
25092 ** in order to coordinate access between separate database connections
25093 ** within this process, but all of that is handled in memory and the
25094 ** operating system does not participate.
25095 **
25096 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
25097 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
25098 ** and is read-only.
25099 **
25100 ** Zero is returned if the call completes successfully, or -1 if a call
25101 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
25102 */
25103 static int unixFileLock(unixFile *pFile, struct flock *pLock){
25104   int rc;
25105   unixInodeInfo *pInode = pFile->pInode;
25106   assert( unixMutexHeld() );
25107   assert( pInode!=0 );
25108   if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
25109    && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
25110   ){
25111     if( pInode->bProcessLock==0 ){
25112       struct flock lock;
25113       assert( pInode->nLock==0 );
25114       lock.l_whence = SEEK_SET;
25115       lock.l_start = SHARED_FIRST;
25116       lock.l_len = SHARED_SIZE;
25117       lock.l_type = F_WRLCK;
25118       rc = osFcntl(pFile->h, F_SETLK, &lock);
25119       if( rc<0 ) return rc;
25120       pInode->bProcessLock = 1;
25121       pInode->nLock++;
25122     }else{
25123       rc = 0;
25124     }
25125   }else{
25126     rc = osFcntl(pFile->h, F_SETLK, pLock);
25127   }
25128   return rc;
25129 }
25130 
25131 /*
25132 ** Lock the file with the lock specified by parameter eFileLock - one
25133 ** of the following:
25134 **
25135 **     (1) SHARED_LOCK
25136 **     (2) RESERVED_LOCK
25137 **     (3) PENDING_LOCK
25138 **     (4) EXCLUSIVE_LOCK
25139 **
25140 ** Sometimes when requesting one lock state, additional lock states
25141 ** are inserted in between.  The locking might fail on one of the later
25142 ** transitions leaving the lock state different from what it started but
25143 ** still short of its goal.  The following chart shows the allowed
25144 ** transitions and the inserted intermediate states:
25145 **
25146 **    UNLOCKED -> SHARED
25147 **    SHARED -> RESERVED
25148 **    SHARED -> (PENDING) -> EXCLUSIVE
25149 **    RESERVED -> (PENDING) -> EXCLUSIVE
25150 **    PENDING -> EXCLUSIVE
25151 **
25152 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25153 ** routine to lower a locking level.
25154 */
25155 static int unixLock(sqlite3_file *id, int eFileLock){
25156   /* The following describes the implementation of the various locks and
25157   ** lock transitions in terms of the POSIX advisory shared and exclusive
25158   ** lock primitives (called read-locks and write-locks below, to avoid
25159   ** confusion with SQLite lock names). The algorithms are complicated
25160   ** slightly in order to be compatible with windows systems simultaneously
25161   ** accessing the same database file, in case that is ever required.
25162   **
25163   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
25164   ** byte', each single bytes at well known offsets, and the 'shared byte
25165   ** range', a range of 510 bytes at a well known offset.
25166   **
25167   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
25168   ** byte'.  If this is successful, a random byte from the 'shared byte
25169   ** range' is read-locked and the lock on the 'pending byte' released.
25170   **
25171   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
25172   ** A RESERVED lock is implemented by grabbing a write-lock on the
25173   ** 'reserved byte'.
25174   **
25175   ** A process may only obtain a PENDING lock after it has obtained a
25176   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
25177   ** on the 'pending byte'. This ensures that no new SHARED locks can be
25178   ** obtained, but existing SHARED locks are allowed to persist. A process
25179   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
25180   ** This property is used by the algorithm for rolling back a journal file
25181   ** after a crash.
25182   **
25183   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
25184   ** implemented by obtaining a write-lock on the entire 'shared byte
25185   ** range'. Since all other locks require a read-lock on one of the bytes
25186   ** within this range, this ensures that no other locks are held on the
25187   ** database.
25188   **
25189   ** The reason a single byte cannot be used instead of the 'shared byte
25190   ** range' is that some versions of windows do not support read-locks. By
25191   ** locking a random byte from a range, concurrent SHARED locks may exist
25192   ** even if the locking primitive used is always a write-lock.
25193   */
25194   int rc = SQLITE_OK;
25195   unixFile *pFile = (unixFile*)id;
25196   unixInodeInfo *pInode;
25197   struct flock lock;
25198   int tErrno = 0;
25199 
25200   assert( pFile );
25201   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
25202       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25203       azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
25204 
25205   /* If there is already a lock of this type or more restrictive on the
25206   ** unixFile, do nothing. Don't use the end_lock: exit path, as
25207   ** unixEnterMutex() hasn't been called yet.
25208   */
25209   if( pFile->eFileLock>=eFileLock ){
25210     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
25211             azFileLock(eFileLock)));
25212     return SQLITE_OK;
25213   }
25214 
25215   /* Make sure the locking sequence is correct.
25216   **  (1) We never move from unlocked to anything higher than shared lock.
25217   **  (2) SQLite never explicitly requests a pendig lock.
25218   **  (3) A shared lock is always held when a reserve lock is requested.
25219   */
25220   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
25221   assert( eFileLock!=PENDING_LOCK );
25222   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
25223 
25224   /* This mutex is needed because pFile->pInode is shared across threads
25225   */
25226   unixEnterMutex();
25227   pInode = pFile->pInode;
25228 
25229   /* If some thread using this PID has a lock via a different unixFile*
25230   ** handle that precludes the requested lock, return BUSY.
25231   */
25232   if( (pFile->eFileLock!=pInode->eFileLock &&
25233           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
25234   ){
25235     rc = SQLITE_BUSY;
25236     goto end_lock;
25237   }
25238 
25239   /* If a SHARED lock is requested, and some thread using this PID already
25240   ** has a SHARED or RESERVED lock, then increment reference counts and
25241   ** return SQLITE_OK.
25242   */
25243   if( eFileLock==SHARED_LOCK &&
25244       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
25245     assert( eFileLock==SHARED_LOCK );
25246     assert( pFile->eFileLock==0 );
25247     assert( pInode->nShared>0 );
25248     pFile->eFileLock = SHARED_LOCK;
25249     pInode->nShared++;
25250     pInode->nLock++;
25251     goto end_lock;
25252   }
25253 
25254 
25255   /* A PENDING lock is needed before acquiring a SHARED lock and before
25256   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
25257   ** be released.
25258   */
25259   lock.l_len = 1L;
25260   lock.l_whence = SEEK_SET;
25261   if( eFileLock==SHARED_LOCK
25262       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
25263   ){
25264     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
25265     lock.l_start = PENDING_BYTE;
25266     if( unixFileLock(pFile, &lock) ){
25267       tErrno = errno;
25268       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25269       if( rc!=SQLITE_BUSY ){
25270         pFile->lastErrno = tErrno;
25271       }
25272       goto end_lock;
25273     }
25274   }
25275 
25276 
25277   /* If control gets to this point, then actually go ahead and make
25278   ** operating system calls for the specified lock.
25279   */
25280   if( eFileLock==SHARED_LOCK ){
25281     assert( pInode->nShared==0 );
25282     assert( pInode->eFileLock==0 );
25283     assert( rc==SQLITE_OK );
25284 
25285     /* Now get the read-lock */
25286     lock.l_start = SHARED_FIRST;
25287     lock.l_len = SHARED_SIZE;
25288     if( unixFileLock(pFile, &lock) ){
25289       tErrno = errno;
25290       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25291     }
25292 
25293     /* Drop the temporary PENDING lock */
25294     lock.l_start = PENDING_BYTE;
25295     lock.l_len = 1L;
25296     lock.l_type = F_UNLCK;
25297     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
25298       /* This could happen with a network mount */
25299       tErrno = errno;
25300       rc = SQLITE_IOERR_UNLOCK;
25301     }
25302 
25303     if( rc ){
25304       if( rc!=SQLITE_BUSY ){
25305         pFile->lastErrno = tErrno;
25306       }
25307       goto end_lock;
25308     }else{
25309       pFile->eFileLock = SHARED_LOCK;
25310       pInode->nLock++;
25311       pInode->nShared = 1;
25312     }
25313   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
25314     /* We are trying for an exclusive lock but another thread in this
25315     ** same process is still holding a shared lock. */
25316     rc = SQLITE_BUSY;
25317   }else{
25318     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
25319     ** assumed that there is a SHARED or greater lock on the file
25320     ** already.
25321     */
25322     assert( 0!=pFile->eFileLock );
25323     lock.l_type = F_WRLCK;
25324 
25325     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
25326     if( eFileLock==RESERVED_LOCK ){
25327       lock.l_start = RESERVED_BYTE;
25328       lock.l_len = 1L;
25329     }else{
25330       lock.l_start = SHARED_FIRST;
25331       lock.l_len = SHARED_SIZE;
25332     }
25333 
25334     if( unixFileLock(pFile, &lock) ){
25335       tErrno = errno;
25336       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25337       if( rc!=SQLITE_BUSY ){
25338         pFile->lastErrno = tErrno;
25339       }
25340     }
25341   }
25342 
25343 
25344 #ifdef SQLITE_DEBUG
25345   /* Set up the transaction-counter change checking flags when
25346   ** transitioning from a SHARED to a RESERVED lock.  The change
25347   ** from SHARED to RESERVED marks the beginning of a normal
25348   ** write operation (not a hot journal rollback).
25349   */
25350   if( rc==SQLITE_OK
25351    && pFile->eFileLock<=SHARED_LOCK
25352    && eFileLock==RESERVED_LOCK
25353   ){
25354     pFile->transCntrChng = 0;
25355     pFile->dbUpdate = 0;
25356     pFile->inNormalWrite = 1;
25357   }
25358 #endif
25359 
25360 
25361   if( rc==SQLITE_OK ){
25362     pFile->eFileLock = eFileLock;
25363     pInode->eFileLock = eFileLock;
25364   }else if( eFileLock==EXCLUSIVE_LOCK ){
25365     pFile->eFileLock = PENDING_LOCK;
25366     pInode->eFileLock = PENDING_LOCK;
25367   }
25368 
25369 end_lock:
25370   unixLeaveMutex();
25371   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
25372       rc==SQLITE_OK ? "ok" : "failed"));
25373   return rc;
25374 }
25375 
25376 /*
25377 ** Add the file descriptor used by file handle pFile to the corresponding
25378 ** pUnused list.
25379 */
25380 static void setPendingFd(unixFile *pFile){
25381   unixInodeInfo *pInode = pFile->pInode;
25382   UnixUnusedFd *p = pFile->pUnused;
25383   p->pNext = pInode->pUnused;
25384   pInode->pUnused = p;
25385   pFile->h = -1;
25386   pFile->pUnused = 0;
25387 }
25388 
25389 /*
25390 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25391 ** must be either NO_LOCK or SHARED_LOCK.
25392 **
25393 ** If the locking level of the file descriptor is already at or below
25394 ** the requested locking level, this routine is a no-op.
25395 **
25396 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
25397 ** the byte range is divided into 2 parts and the first part is unlocked then
25398 ** set to a read lock, then the other part is simply unlocked.  This works
25399 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
25400 ** remove the write lock on a region when a read lock is set.
25401 */
25402 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
25403   unixFile *pFile = (unixFile*)id;
25404   unixInodeInfo *pInode;
25405   struct flock lock;
25406   int rc = SQLITE_OK;
25407 
25408   assert( pFile );
25409   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
25410       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
25411       getpid()));
25412 
25413   assert( eFileLock<=SHARED_LOCK );
25414   if( pFile->eFileLock<=eFileLock ){
25415     return SQLITE_OK;
25416   }
25417   unixEnterMutex();
25418   pInode = pFile->pInode;
25419   assert( pInode->nShared!=0 );
25420   if( pFile->eFileLock>SHARED_LOCK ){
25421     assert( pInode->eFileLock==pFile->eFileLock );
25422 
25423 #ifdef SQLITE_DEBUG
25424     /* When reducing a lock such that other processes can start
25425     ** reading the database file again, make sure that the
25426     ** transaction counter was updated if any part of the database
25427     ** file changed.  If the transaction counter is not updated,
25428     ** other connections to the same file might not realize that
25429     ** the file has changed and hence might not know to flush their
25430     ** cache.  The use of a stale cache can lead to database corruption.
25431     */
25432     pFile->inNormalWrite = 0;
25433 #endif
25434 
25435     /* downgrading to a shared lock on NFS involves clearing the write lock
25436     ** before establishing the readlock - to avoid a race condition we downgrade
25437     ** the lock in 2 blocks, so that part of the range will be covered by a
25438     ** write lock until the rest is covered by a read lock:
25439     **  1:   [WWWWW]
25440     **  2:   [....W]
25441     **  3:   [RRRRW]
25442     **  4:   [RRRR.]
25443     */
25444     if( eFileLock==SHARED_LOCK ){
25445 
25446 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
25447       (void)handleNFSUnlock;
25448       assert( handleNFSUnlock==0 );
25449 #endif
25450 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
25451       if( handleNFSUnlock ){
25452         int tErrno;               /* Error code from system call errors */
25453         off_t divSize = SHARED_SIZE - 1;
25454 
25455         lock.l_type = F_UNLCK;
25456         lock.l_whence = SEEK_SET;
25457         lock.l_start = SHARED_FIRST;
25458         lock.l_len = divSize;
25459         if( unixFileLock(pFile, &lock)==(-1) ){
25460           tErrno = errno;
25461           rc = SQLITE_IOERR_UNLOCK;
25462           if( IS_LOCK_ERROR(rc) ){
25463             pFile->lastErrno = tErrno;
25464           }
25465           goto end_unlock;
25466         }
25467         lock.l_type = F_RDLCK;
25468         lock.l_whence = SEEK_SET;
25469         lock.l_start = SHARED_FIRST;
25470         lock.l_len = divSize;
25471         if( unixFileLock(pFile, &lock)==(-1) ){
25472           tErrno = errno;
25473           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
25474           if( IS_LOCK_ERROR(rc) ){
25475             pFile->lastErrno = tErrno;
25476           }
25477           goto end_unlock;
25478         }
25479         lock.l_type = F_UNLCK;
25480         lock.l_whence = SEEK_SET;
25481         lock.l_start = SHARED_FIRST+divSize;
25482         lock.l_len = SHARED_SIZE-divSize;
25483         if( unixFileLock(pFile, &lock)==(-1) ){
25484           tErrno = errno;
25485           rc = SQLITE_IOERR_UNLOCK;
25486           if( IS_LOCK_ERROR(rc) ){
25487             pFile->lastErrno = tErrno;
25488           }
25489           goto end_unlock;
25490         }
25491       }else
25492 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
25493       {
25494         lock.l_type = F_RDLCK;
25495         lock.l_whence = SEEK_SET;
25496         lock.l_start = SHARED_FIRST;
25497         lock.l_len = SHARED_SIZE;
25498         if( unixFileLock(pFile, &lock) ){
25499           /* In theory, the call to unixFileLock() cannot fail because another
25500           ** process is holding an incompatible lock. If it does, this
25501           ** indicates that the other process is not following the locking
25502           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
25503           ** SQLITE_BUSY would confuse the upper layer (in practice it causes
25504           ** an assert to fail). */
25505           rc = SQLITE_IOERR_RDLOCK;
25506           pFile->lastErrno = errno;
25507           goto end_unlock;
25508         }
25509       }
25510     }
25511     lock.l_type = F_UNLCK;
25512     lock.l_whence = SEEK_SET;
25513     lock.l_start = PENDING_BYTE;
25514     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
25515     if( unixFileLock(pFile, &lock)==0 ){
25516       pInode->eFileLock = SHARED_LOCK;
25517     }else{
25518       rc = SQLITE_IOERR_UNLOCK;
25519       pFile->lastErrno = errno;
25520       goto end_unlock;
25521     }
25522   }
25523   if( eFileLock==NO_LOCK ){
25524     /* Decrement the shared lock counter.  Release the lock using an
25525     ** OS call only when all threads in this same process have released
25526     ** the lock.
25527     */
25528     pInode->nShared--;
25529     if( pInode->nShared==0 ){
25530       lock.l_type = F_UNLCK;
25531       lock.l_whence = SEEK_SET;
25532       lock.l_start = lock.l_len = 0L;
25533       if( unixFileLock(pFile, &lock)==0 ){
25534         pInode->eFileLock = NO_LOCK;
25535       }else{
25536         rc = SQLITE_IOERR_UNLOCK;
25537         pFile->lastErrno = errno;
25538         pInode->eFileLock = NO_LOCK;
25539         pFile->eFileLock = NO_LOCK;
25540       }
25541     }
25542 
25543     /* Decrement the count of locks against this same file.  When the
25544     ** count reaches zero, close any other file descriptors whose close
25545     ** was deferred because of outstanding locks.
25546     */
25547     pInode->nLock--;
25548     assert( pInode->nLock>=0 );
25549     if( pInode->nLock==0 ){
25550       closePendingFds(pFile);
25551     }
25552   }
25553 
25554 end_unlock:
25555   unixLeaveMutex();
25556   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
25557   return rc;
25558 }
25559 
25560 /*
25561 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25562 ** must be either NO_LOCK or SHARED_LOCK.
25563 **
25564 ** If the locking level of the file descriptor is already at or below
25565 ** the requested locking level, this routine is a no-op.
25566 */
25567 static int unixUnlock(sqlite3_file *id, int eFileLock){
25568 #if SQLITE_MAX_MMAP_SIZE>0
25569   assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
25570 #endif
25571   return posixUnlock(id, eFileLock, 0);
25572 }
25573 
25574 #if SQLITE_MAX_MMAP_SIZE>0
25575 static int unixMapfile(unixFile *pFd, i64 nByte);
25576 static void unixUnmapfile(unixFile *pFd);
25577 #endif
25578 
25579 /*
25580 ** This function performs the parts of the "close file" operation
25581 ** common to all locking schemes. It closes the directory and file
25582 ** handles, if they are valid, and sets all fields of the unixFile
25583 ** structure to 0.
25584 **
25585 ** It is *not* necessary to hold the mutex when this routine is called,
25586 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
25587 ** vxworksReleaseFileId() routine.
25588 */
25589 static int closeUnixFile(sqlite3_file *id){
25590   unixFile *pFile = (unixFile*)id;
25591 #if SQLITE_MAX_MMAP_SIZE>0
25592   unixUnmapfile(pFile);
25593 #endif
25594   if( pFile->h>=0 ){
25595     robust_close(pFile, pFile->h, __LINE__);
25596     pFile->h = -1;
25597   }
25598 #if OS_VXWORKS
25599   if( pFile->pId ){
25600     if( pFile->ctrlFlags & UNIXFILE_DELETE ){
25601       osUnlink(pFile->pId->zCanonicalName);
25602     }
25603     vxworksReleaseFileId(pFile->pId);
25604     pFile->pId = 0;
25605   }
25606 #endif
25607   OSTRACE(("CLOSE   %-3d\n", pFile->h));
25608   OpenCounter(-1);
25609   sqlite3_free(pFile->pUnused);
25610   memset(pFile, 0, sizeof(unixFile));
25611   return SQLITE_OK;
25612 }
25613 
25614 /*
25615 ** Close a file.
25616 */
25617 static int unixClose(sqlite3_file *id){
25618   int rc = SQLITE_OK;
25619   unixFile *pFile = (unixFile *)id;
25620   verifyDbFile(pFile);
25621   unixUnlock(id, NO_LOCK);
25622   unixEnterMutex();
25623 
25624   /* unixFile.pInode is always valid here. Otherwise, a different close
25625   ** routine (e.g. nolockClose()) would be called instead.
25626   */
25627   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
25628   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
25629     /* If there are outstanding locks, do not actually close the file just
25630     ** yet because that would clear those locks.  Instead, add the file
25631     ** descriptor to pInode->pUnused list.  It will be automatically closed
25632     ** when the last lock is cleared.
25633     */
25634     setPendingFd(pFile);
25635   }
25636   releaseInodeInfo(pFile);
25637   rc = closeUnixFile(id);
25638   unixLeaveMutex();
25639   return rc;
25640 }
25641 
25642 /************** End of the posix advisory lock implementation *****************
25643 ******************************************************************************/
25644 
25645 /******************************************************************************
25646 ****************************** No-op Locking **********************************
25647 **
25648 ** Of the various locking implementations available, this is by far the
25649 ** simplest:  locking is ignored.  No attempt is made to lock the database
25650 ** file for reading or writing.
25651 **
25652 ** This locking mode is appropriate for use on read-only databases
25653 ** (ex: databases that are burned into CD-ROM, for example.)  It can
25654 ** also be used if the application employs some external mechanism to
25655 ** prevent simultaneous access of the same database by two or more
25656 ** database connections.  But there is a serious risk of database
25657 ** corruption if this locking mode is used in situations where multiple
25658 ** database connections are accessing the same database file at the same
25659 ** time and one or more of those connections are writing.
25660 */
25661 
25662 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
25663   UNUSED_PARAMETER(NotUsed);
25664   *pResOut = 0;
25665   return SQLITE_OK;
25666 }
25667 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
25668   UNUSED_PARAMETER2(NotUsed, NotUsed2);
25669   return SQLITE_OK;
25670 }
25671 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
25672   UNUSED_PARAMETER2(NotUsed, NotUsed2);
25673   return SQLITE_OK;
25674 }
25675 
25676 /*
25677 ** Close the file.
25678 */
25679 static int nolockClose(sqlite3_file *id) {
25680   return closeUnixFile(id);
25681 }
25682 
25683 /******************* End of the no-op lock implementation *********************
25684 ******************************************************************************/
25685 
25686 /******************************************************************************
25687 ************************* Begin dot-file Locking ******************************
25688 **
25689 ** The dotfile locking implementation uses the existence of separate lock
25690 ** files (really a directory) to control access to the database.  This works
25691 ** on just about every filesystem imaginable.  But there are serious downsides:
25692 **
25693 **    (1)  There is zero concurrency.  A single reader blocks all other
25694 **         connections from reading or writing the database.
25695 **
25696 **    (2)  An application crash or power loss can leave stale lock files
25697 **         sitting around that need to be cleared manually.
25698 **
25699 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
25700 ** other locking strategy is available.
25701 **
25702 ** Dotfile locking works by creating a subdirectory in the same directory as
25703 ** the database and with the same name but with a ".lock" extension added.
25704 ** The existence of a lock directory implies an EXCLUSIVE lock.  All other
25705 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
25706 */
25707 
25708 /*
25709 ** The file suffix added to the data base filename in order to create the
25710 ** lock directory.
25711 */
25712 #define DOTLOCK_SUFFIX ".lock"
25713 
25714 /*
25715 ** This routine checks if there is a RESERVED lock held on the specified
25716 ** file by this or any other process. If such a lock is held, set *pResOut
25717 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
25718 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25719 **
25720 ** In dotfile locking, either a lock exists or it does not.  So in this
25721 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
25722 ** is held on the file and false if the file is unlocked.
25723 */
25724 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
25725   int rc = SQLITE_OK;
25726   int reserved = 0;
25727   unixFile *pFile = (unixFile*)id;
25728 
25729   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25730 
25731   assert( pFile );
25732 
25733   /* Check if a thread in this process holds such a lock */
25734   if( pFile->eFileLock>SHARED_LOCK ){
25735     /* Either this connection or some other connection in the same process
25736     ** holds a lock on the file.  No need to check further. */
25737     reserved = 1;
25738   }else{
25739     /* The lock is held if and only if the lockfile exists */
25740     const char *zLockFile = (const char*)pFile->lockingContext;
25741     reserved = osAccess(zLockFile, 0)==0;
25742   }
25743   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
25744   *pResOut = reserved;
25745   return rc;
25746 }
25747 
25748 /*
25749 ** Lock the file with the lock specified by parameter eFileLock - one
25750 ** of the following:
25751 **
25752 **     (1) SHARED_LOCK
25753 **     (2) RESERVED_LOCK
25754 **     (3) PENDING_LOCK
25755 **     (4) EXCLUSIVE_LOCK
25756 **
25757 ** Sometimes when requesting one lock state, additional lock states
25758 ** are inserted in between.  The locking might fail on one of the later
25759 ** transitions leaving the lock state different from what it started but
25760 ** still short of its goal.  The following chart shows the allowed
25761 ** transitions and the inserted intermediate states:
25762 **
25763 **    UNLOCKED -> SHARED
25764 **    SHARED -> RESERVED
25765 **    SHARED -> (PENDING) -> EXCLUSIVE
25766 **    RESERVED -> (PENDING) -> EXCLUSIVE
25767 **    PENDING -> EXCLUSIVE
25768 **
25769 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25770 ** routine to lower a locking level.
25771 **
25772 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
25773 ** But we track the other locking levels internally.
25774 */
25775 static int dotlockLock(sqlite3_file *id, int eFileLock) {
25776   unixFile *pFile = (unixFile*)id;
25777   char *zLockFile = (char *)pFile->lockingContext;
25778   int rc = SQLITE_OK;
25779 
25780 
25781   /* If we have any lock, then the lock file already exists.  All we have
25782   ** to do is adjust our internal record of the lock level.
25783   */
25784   if( pFile->eFileLock > NO_LOCK ){
25785     pFile->eFileLock = eFileLock;
25786     /* Always update the timestamp on the old file */
25787 #ifdef HAVE_UTIME
25788     utime(zLockFile, NULL);
25789 #else
25790     utimes(zLockFile, NULL);
25791 #endif
25792     return SQLITE_OK;
25793   }
25794 
25795   /* grab an exclusive lock */
25796   rc = osMkdir(zLockFile, 0777);
25797   if( rc<0 ){
25798     /* failed to open/create the lock directory */
25799     int tErrno = errno;
25800     if( EEXIST == tErrno ){
25801       rc = SQLITE_BUSY;
25802     } else {
25803       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25804       if( IS_LOCK_ERROR(rc) ){
25805         pFile->lastErrno = tErrno;
25806       }
25807     }
25808     return rc;
25809   }
25810 
25811   /* got it, set the type and return ok */
25812   pFile->eFileLock = eFileLock;
25813   return rc;
25814 }
25815 
25816 /*
25817 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25818 ** must be either NO_LOCK or SHARED_LOCK.
25819 **
25820 ** If the locking level of the file descriptor is already at or below
25821 ** the requested locking level, this routine is a no-op.
25822 **
25823 ** When the locking level reaches NO_LOCK, delete the lock file.
25824 */
25825 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
25826   unixFile *pFile = (unixFile*)id;
25827   char *zLockFile = (char *)pFile->lockingContext;
25828   int rc;
25829 
25830   assert( pFile );
25831   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
25832            pFile->eFileLock, getpid()));
25833   assert( eFileLock<=SHARED_LOCK );
25834 
25835   /* no-op if possible */
25836   if( pFile->eFileLock==eFileLock ){
25837     return SQLITE_OK;
25838   }
25839 
25840   /* To downgrade to shared, simply update our internal notion of the
25841   ** lock state.  No need to mess with the file on disk.
25842   */
25843   if( eFileLock==SHARED_LOCK ){
25844     pFile->eFileLock = SHARED_LOCK;
25845     return SQLITE_OK;
25846   }
25847 
25848   /* To fully unlock the database, delete the lock file */
25849   assert( eFileLock==NO_LOCK );
25850   rc = osRmdir(zLockFile);
25851   if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
25852   if( rc<0 ){
25853     int tErrno = errno;
25854     rc = 0;
25855     if( ENOENT != tErrno ){
25856       rc = SQLITE_IOERR_UNLOCK;
25857     }
25858     if( IS_LOCK_ERROR(rc) ){
25859       pFile->lastErrno = tErrno;
25860     }
25861     return rc;
25862   }
25863   pFile->eFileLock = NO_LOCK;
25864   return SQLITE_OK;
25865 }
25866 
25867 /*
25868 ** Close a file.  Make sure the lock has been released before closing.
25869 */
25870 static int dotlockClose(sqlite3_file *id) {
25871   int rc = SQLITE_OK;
25872   if( id ){
25873     unixFile *pFile = (unixFile*)id;
25874     dotlockUnlock(id, NO_LOCK);
25875     sqlite3_free(pFile->lockingContext);
25876     rc = closeUnixFile(id);
25877   }
25878   return rc;
25879 }
25880 /****************** End of the dot-file lock implementation *******************
25881 ******************************************************************************/
25882 
25883 /******************************************************************************
25884 ************************** Begin flock Locking ********************************
25885 **
25886 ** Use the flock() system call to do file locking.
25887 **
25888 ** flock() locking is like dot-file locking in that the various
25889 ** fine-grain locking levels supported by SQLite are collapsed into
25890 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
25891 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
25892 ** still works when you do this, but concurrency is reduced since
25893 ** only a single process can be reading the database at a time.
25894 **
25895 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
25896 ** compiling for VXWORKS.
25897 */
25898 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
25899 
25900 /*
25901 ** Retry flock() calls that fail with EINTR
25902 */
25903 #ifdef EINTR
25904 static int robust_flock(int fd, int op){
25905   int rc;
25906   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
25907   return rc;
25908 }
25909 #else
25910 # define robust_flock(a,b) flock(a,b)
25911 #endif
25912 
25913 
25914 /*
25915 ** This routine checks if there is a RESERVED lock held on the specified
25916 ** file by this or any other process. If such a lock is held, set *pResOut
25917 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
25918 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25919 */
25920 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
25921   int rc = SQLITE_OK;
25922   int reserved = 0;
25923   unixFile *pFile = (unixFile*)id;
25924 
25925   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25926 
25927   assert( pFile );
25928 
25929   /* Check if a thread in this process holds such a lock */
25930   if( pFile->eFileLock>SHARED_LOCK ){
25931     reserved = 1;
25932   }
25933 
25934   /* Otherwise see if some other process holds it. */
25935   if( !reserved ){
25936     /* attempt to get the lock */
25937     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
25938     if( !lrc ){
25939       /* got the lock, unlock it */
25940       lrc = robust_flock(pFile->h, LOCK_UN);
25941       if ( lrc ) {
25942         int tErrno = errno;
25943         /* unlock failed with an error */
25944         lrc = SQLITE_IOERR_UNLOCK;
25945         if( IS_LOCK_ERROR(lrc) ){
25946           pFile->lastErrno = tErrno;
25947           rc = lrc;
25948         }
25949       }
25950     } else {
25951       int tErrno = errno;
25952       reserved = 1;
25953       /* someone else might have it reserved */
25954       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25955       if( IS_LOCK_ERROR(lrc) ){
25956         pFile->lastErrno = tErrno;
25957         rc = lrc;
25958       }
25959     }
25960   }
25961   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
25962 
25963 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
25964   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
25965     rc = SQLITE_OK;
25966     reserved=1;
25967   }
25968 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
25969   *pResOut = reserved;
25970   return rc;
25971 }
25972 
25973 /*
25974 ** Lock the file with the lock specified by parameter eFileLock - one
25975 ** of the following:
25976 **
25977 **     (1) SHARED_LOCK
25978 **     (2) RESERVED_LOCK
25979 **     (3) PENDING_LOCK
25980 **     (4) EXCLUSIVE_LOCK
25981 **
25982 ** Sometimes when requesting one lock state, additional lock states
25983 ** are inserted in between.  The locking might fail on one of the later
25984 ** transitions leaving the lock state different from what it started but
25985 ** still short of its goal.  The following chart shows the allowed
25986 ** transitions and the inserted intermediate states:
25987 **
25988 **    UNLOCKED -> SHARED
25989 **    SHARED -> RESERVED
25990 **    SHARED -> (PENDING) -> EXCLUSIVE
25991 **    RESERVED -> (PENDING) -> EXCLUSIVE
25992 **    PENDING -> EXCLUSIVE
25993 **
25994 ** flock() only really support EXCLUSIVE locks.  We track intermediate
25995 ** lock states in the sqlite3_file structure, but all locks SHARED or
25996 ** above are really EXCLUSIVE locks and exclude all other processes from
25997 ** access the file.
25998 **
25999 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26000 ** routine to lower a locking level.
26001 */
26002 static int flockLock(sqlite3_file *id, int eFileLock) {
26003   int rc = SQLITE_OK;
26004   unixFile *pFile = (unixFile*)id;
26005 
26006   assert( pFile );
26007 
26008   /* if we already have a lock, it is exclusive.
26009   ** Just adjust level and punt on outta here. */
26010   if (pFile->eFileLock > NO_LOCK) {
26011     pFile->eFileLock = eFileLock;
26012     return SQLITE_OK;
26013   }
26014 
26015   /* grab an exclusive lock */
26016 
26017   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
26018     int tErrno = errno;
26019     /* didn't get, must be busy */
26020     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
26021     if( IS_LOCK_ERROR(rc) ){
26022       pFile->lastErrno = tErrno;
26023     }
26024   } else {
26025     /* got it, set the type and return ok */
26026     pFile->eFileLock = eFileLock;
26027   }
26028   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
26029            rc==SQLITE_OK ? "ok" : "failed"));
26030 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26031   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
26032     rc = SQLITE_BUSY;
26033   }
26034 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26035   return rc;
26036 }
26037 
26038 
26039 /*
26040 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26041 ** must be either NO_LOCK or SHARED_LOCK.
26042 **
26043 ** If the locking level of the file descriptor is already at or below
26044 ** the requested locking level, this routine is a no-op.
26045 */
26046 static int flockUnlock(sqlite3_file *id, int eFileLock) {
26047   unixFile *pFile = (unixFile*)id;
26048 
26049   assert( pFile );
26050   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
26051            pFile->eFileLock, getpid()));
26052   assert( eFileLock<=SHARED_LOCK );
26053 
26054   /* no-op if possible */
26055   if( pFile->eFileLock==eFileLock ){
26056     return SQLITE_OK;
26057   }
26058 
26059   /* shared can just be set because we always have an exclusive */
26060   if (eFileLock==SHARED_LOCK) {
26061     pFile->eFileLock = eFileLock;
26062     return SQLITE_OK;
26063   }
26064 
26065   /* no, really, unlock. */
26066   if( robust_flock(pFile->h, LOCK_UN) ){
26067 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
26068     return SQLITE_OK;
26069 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
26070     return SQLITE_IOERR_UNLOCK;
26071   }else{
26072     pFile->eFileLock = NO_LOCK;
26073     return SQLITE_OK;
26074   }
26075 }
26076 
26077 /*
26078 ** Close a file.
26079 */
26080 static int flockClose(sqlite3_file *id) {
26081   int rc = SQLITE_OK;
26082   if( id ){
26083     flockUnlock(id, NO_LOCK);
26084     rc = closeUnixFile(id);
26085   }
26086   return rc;
26087 }
26088 
26089 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
26090 
26091 /******************* End of the flock lock implementation *********************
26092 ******************************************************************************/
26093 
26094 /******************************************************************************
26095 ************************ Begin Named Semaphore Locking ************************
26096 **
26097 ** Named semaphore locking is only supported on VxWorks.
26098 **
26099 ** Semaphore locking is like dot-lock and flock in that it really only
26100 ** supports EXCLUSIVE locking.  Only a single process can read or write
26101 ** the database file at a time.  This reduces potential concurrency, but
26102 ** makes the lock implementation much easier.
26103 */
26104 #if OS_VXWORKS
26105 
26106 /*
26107 ** This routine checks if there is a RESERVED lock held on the specified
26108 ** file by this or any other process. If such a lock is held, set *pResOut
26109 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26110 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26111 */
26112 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
26113   int rc = SQLITE_OK;
26114   int reserved = 0;
26115   unixFile *pFile = (unixFile*)id;
26116 
26117   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26118 
26119   assert( pFile );
26120 
26121   /* Check if a thread in this process holds such a lock */
26122   if( pFile->eFileLock>SHARED_LOCK ){
26123     reserved = 1;
26124   }
26125 
26126   /* Otherwise see if some other process holds it. */
26127   if( !reserved ){
26128     sem_t *pSem = pFile->pInode->pSem;
26129     struct stat statBuf;
26130 
26131     if( sem_trywait(pSem)==-1 ){
26132       int tErrno = errno;
26133       if( EAGAIN != tErrno ){
26134         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
26135         pFile->lastErrno = tErrno;
26136       } else {
26137         /* someone else has the lock when we are in NO_LOCK */
26138         reserved = (pFile->eFileLock < SHARED_LOCK);
26139       }
26140     }else{
26141       /* we could have it if we want it */
26142       sem_post(pSem);
26143     }
26144   }
26145   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
26146 
26147   *pResOut = reserved;
26148   return rc;
26149 }
26150 
26151 /*
26152 ** Lock the file with the lock specified by parameter eFileLock - one
26153 ** of the following:
26154 **
26155 **     (1) SHARED_LOCK
26156 **     (2) RESERVED_LOCK
26157 **     (3) PENDING_LOCK
26158 **     (4) EXCLUSIVE_LOCK
26159 **
26160 ** Sometimes when requesting one lock state, additional lock states
26161 ** are inserted in between.  The locking might fail on one of the later
26162 ** transitions leaving the lock state different from what it started but
26163 ** still short of its goal.  The following chart shows the allowed
26164 ** transitions and the inserted intermediate states:
26165 **
26166 **    UNLOCKED -> SHARED
26167 **    SHARED -> RESERVED
26168 **    SHARED -> (PENDING) -> EXCLUSIVE
26169 **    RESERVED -> (PENDING) -> EXCLUSIVE
26170 **    PENDING -> EXCLUSIVE
26171 **
26172 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
26173 ** lock states in the sqlite3_file structure, but all locks SHARED or
26174 ** above are really EXCLUSIVE locks and exclude all other processes from
26175 ** access the file.
26176 **
26177 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26178 ** routine to lower a locking level.
26179 */
26180 static int semLock(sqlite3_file *id, int eFileLock) {
26181   unixFile *pFile = (unixFile*)id;
26182   int fd;
26183   sem_t *pSem = pFile->pInode->pSem;
26184   int rc = SQLITE_OK;
26185 
26186   /* if we already have a lock, it is exclusive.
26187   ** Just adjust level and punt on outta here. */
26188   if (pFile->eFileLock > NO_LOCK) {
26189     pFile->eFileLock = eFileLock;
26190     rc = SQLITE_OK;
26191     goto sem_end_lock;
26192   }
26193 
26194   /* lock semaphore now but bail out when already locked. */
26195   if( sem_trywait(pSem)==-1 ){
26196     rc = SQLITE_BUSY;
26197     goto sem_end_lock;
26198   }
26199 
26200   /* got it, set the type and return ok */
26201   pFile->eFileLock = eFileLock;
26202 
26203  sem_end_lock:
26204   return rc;
26205 }
26206 
26207 /*
26208 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26209 ** must be either NO_LOCK or SHARED_LOCK.
26210 **
26211 ** If the locking level of the file descriptor is already at or below
26212 ** the requested locking level, this routine is a no-op.
26213 */
26214 static int semUnlock(sqlite3_file *id, int eFileLock) {
26215   unixFile *pFile = (unixFile*)id;
26216   sem_t *pSem = pFile->pInode->pSem;
26217 
26218   assert( pFile );
26219   assert( pSem );
26220   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
26221            pFile->eFileLock, getpid()));
26222   assert( eFileLock<=SHARED_LOCK );
26223 
26224   /* no-op if possible */
26225   if( pFile->eFileLock==eFileLock ){
26226     return SQLITE_OK;
26227   }
26228 
26229   /* shared can just be set because we always have an exclusive */
26230   if (eFileLock==SHARED_LOCK) {
26231     pFile->eFileLock = eFileLock;
26232     return SQLITE_OK;
26233   }
26234 
26235   /* no, really unlock. */
26236   if ( sem_post(pSem)==-1 ) {
26237     int rc, tErrno = errno;
26238     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
26239     if( IS_LOCK_ERROR(rc) ){
26240       pFile->lastErrno = tErrno;
26241     }
26242     return rc;
26243   }
26244   pFile->eFileLock = NO_LOCK;
26245   return SQLITE_OK;
26246 }
26247 
26248 /*
26249  ** Close a file.
26250  */
26251 static int semClose(sqlite3_file *id) {
26252   if( id ){
26253     unixFile *pFile = (unixFile*)id;
26254     semUnlock(id, NO_LOCK);
26255     assert( pFile );
26256     unixEnterMutex();
26257     releaseInodeInfo(pFile);
26258     unixLeaveMutex();
26259     closeUnixFile(id);
26260   }
26261   return SQLITE_OK;
26262 }
26263 
26264 #endif /* OS_VXWORKS */
26265 /*
26266 ** Named semaphore locking is only available on VxWorks.
26267 **
26268 *************** End of the named semaphore lock implementation ****************
26269 ******************************************************************************/
26270 
26271 
26272 /******************************************************************************
26273 *************************** Begin AFP Locking *********************************
26274 **
26275 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
26276 ** on Apple Macintosh computers - both OS9 and OSX.
26277 **
26278 ** Third-party implementations of AFP are available.  But this code here
26279 ** only works on OSX.
26280 */
26281 
26282 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26283 /*
26284 ** The afpLockingContext structure contains all afp lock specific state
26285 */
26286 typedef struct afpLockingContext afpLockingContext;
26287 struct afpLockingContext {
26288   int reserved;
26289   const char *dbPath;             /* Name of the open file */
26290 };
26291 
26292 struct ByteRangeLockPB2
26293 {
26294   unsigned long long offset;        /* offset to first byte to lock */
26295   unsigned long long length;        /* nbr of bytes to lock */
26296   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
26297   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
26298   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
26299   int fd;                           /* file desc to assoc this lock with */
26300 };
26301 
26302 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
26303 
26304 /*
26305 ** This is a utility for setting or clearing a bit-range lock on an
26306 ** AFP filesystem.
26307 **
26308 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
26309 */
26310 static int afpSetLock(
26311   const char *path,              /* Name of the file to be locked or unlocked */
26312   unixFile *pFile,               /* Open file descriptor on path */
26313   unsigned long long offset,     /* First byte to be locked */
26314   unsigned long long length,     /* Number of bytes to lock */
26315   int setLockFlag                /* True to set lock.  False to clear lock */
26316 ){
26317   struct ByteRangeLockPB2 pb;
26318   int err;
26319 
26320   pb.unLockFlag = setLockFlag ? 0 : 1;
26321   pb.startEndFlag = 0;
26322   pb.offset = offset;
26323   pb.length = length;
26324   pb.fd = pFile->h;
26325 
26326   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
26327     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
26328     offset, length));
26329   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
26330   if ( err==-1 ) {
26331     int rc;
26332     int tErrno = errno;
26333     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
26334              path, tErrno, strerror(tErrno)));
26335 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
26336     rc = SQLITE_BUSY;
26337 #else
26338     rc = sqliteErrorFromPosixError(tErrno,
26339                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
26340 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
26341     if( IS_LOCK_ERROR(rc) ){
26342       pFile->lastErrno = tErrno;
26343     }
26344     return rc;
26345   } else {
26346     return SQLITE_OK;
26347   }
26348 }
26349 
26350 /*
26351 ** This routine checks if there is a RESERVED lock held on the specified
26352 ** file by this or any other process. If such a lock is held, set *pResOut
26353 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26354 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26355 */
26356 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
26357   int rc = SQLITE_OK;
26358   int reserved = 0;
26359   unixFile *pFile = (unixFile*)id;
26360   afpLockingContext *context;
26361 
26362   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26363 
26364   assert( pFile );
26365   context = (afpLockingContext *) pFile->lockingContext;
26366   if( context->reserved ){
26367     *pResOut = 1;
26368     return SQLITE_OK;
26369   }
26370   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
26371 
26372   /* Check if a thread in this process holds such a lock */
26373   if( pFile->pInode->eFileLock>SHARED_LOCK ){
26374     reserved = 1;
26375   }
26376 
26377   /* Otherwise see if some other process holds it.
26378    */
26379   if( !reserved ){
26380     /* lock the RESERVED byte */
26381     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
26382     if( SQLITE_OK==lrc ){
26383       /* if we succeeded in taking the reserved lock, unlock it to restore
26384       ** the original state */
26385       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
26386     } else {
26387       /* if we failed to get the lock then someone else must have it */
26388       reserved = 1;
26389     }
26390     if( IS_LOCK_ERROR(lrc) ){
26391       rc=lrc;
26392     }
26393   }
26394 
26395   unixLeaveMutex();
26396   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
26397 
26398   *pResOut = reserved;
26399   return rc;
26400 }
26401 
26402 /*
26403 ** Lock the file with the lock specified by parameter eFileLock - one
26404 ** of the following:
26405 **
26406 **     (1) SHARED_LOCK
26407 **     (2) RESERVED_LOCK
26408 **     (3) PENDING_LOCK
26409 **     (4) EXCLUSIVE_LOCK
26410 **
26411 ** Sometimes when requesting one lock state, additional lock states
26412 ** are inserted in between.  The locking might fail on one of the later
26413 ** transitions leaving the lock state different from what it started but
26414 ** still short of its goal.  The following chart shows the allowed
26415 ** transitions and the inserted intermediate states:
26416 **
26417 **    UNLOCKED -> SHARED
26418 **    SHARED -> RESERVED
26419 **    SHARED -> (PENDING) -> EXCLUSIVE
26420 **    RESERVED -> (PENDING) -> EXCLUSIVE
26421 **    PENDING -> EXCLUSIVE
26422 **
26423 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26424 ** routine to lower a locking level.
26425 */
26426 static int afpLock(sqlite3_file *id, int eFileLock){
26427   int rc = SQLITE_OK;
26428   unixFile *pFile = (unixFile*)id;
26429   unixInodeInfo *pInode = pFile->pInode;
26430   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26431 
26432   assert( pFile );
26433   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
26434            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
26435            azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
26436 
26437   /* If there is already a lock of this type or more restrictive on the
26438   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
26439   ** unixEnterMutex() hasn't been called yet.
26440   */
26441   if( pFile->eFileLock>=eFileLock ){
26442     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
26443            azFileLock(eFileLock)));
26444     return SQLITE_OK;
26445   }
26446 
26447   /* Make sure the locking sequence is correct
26448   **  (1) We never move from unlocked to anything higher than shared lock.
26449   **  (2) SQLite never explicitly requests a pendig lock.
26450   **  (3) A shared lock is always held when a reserve lock is requested.
26451   */
26452   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
26453   assert( eFileLock!=PENDING_LOCK );
26454   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
26455 
26456   /* This mutex is needed because pFile->pInode is shared across threads
26457   */
26458   unixEnterMutex();
26459   pInode = pFile->pInode;
26460 
26461   /* If some thread using this PID has a lock via a different unixFile*
26462   ** handle that precludes the requested lock, return BUSY.
26463   */
26464   if( (pFile->eFileLock!=pInode->eFileLock &&
26465        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
26466      ){
26467     rc = SQLITE_BUSY;
26468     goto afp_end_lock;
26469   }
26470 
26471   /* If a SHARED lock is requested, and some thread using this PID already
26472   ** has a SHARED or RESERVED lock, then increment reference counts and
26473   ** return SQLITE_OK.
26474   */
26475   if( eFileLock==SHARED_LOCK &&
26476      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
26477     assert( eFileLock==SHARED_LOCK );
26478     assert( pFile->eFileLock==0 );
26479     assert( pInode->nShared>0 );
26480     pFile->eFileLock = SHARED_LOCK;
26481     pInode->nShared++;
26482     pInode->nLock++;
26483     goto afp_end_lock;
26484   }
26485 
26486   /* A PENDING lock is needed before acquiring a SHARED lock and before
26487   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
26488   ** be released.
26489   */
26490   if( eFileLock==SHARED_LOCK
26491       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
26492   ){
26493     int failed;
26494     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
26495     if (failed) {
26496       rc = failed;
26497       goto afp_end_lock;
26498     }
26499   }
26500 
26501   /* If control gets to this point, then actually go ahead and make
26502   ** operating system calls for the specified lock.
26503   */
26504   if( eFileLock==SHARED_LOCK ){
26505     int lrc1, lrc2, lrc1Errno = 0;
26506     long lk, mask;
26507 
26508     assert( pInode->nShared==0 );
26509     assert( pInode->eFileLock==0 );
26510 
26511     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
26512     /* Now get the read-lock SHARED_LOCK */
26513     /* note that the quality of the randomness doesn't matter that much */
26514     lk = random();
26515     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
26516     lrc1 = afpSetLock(context->dbPath, pFile,
26517           SHARED_FIRST+pInode->sharedByte, 1, 1);
26518     if( IS_LOCK_ERROR(lrc1) ){
26519       lrc1Errno = pFile->lastErrno;
26520     }
26521     /* Drop the temporary PENDING lock */
26522     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
26523 
26524     if( IS_LOCK_ERROR(lrc1) ) {
26525       pFile->lastErrno = lrc1Errno;
26526       rc = lrc1;
26527       goto afp_end_lock;
26528     } else if( IS_LOCK_ERROR(lrc2) ){
26529       rc = lrc2;
26530       goto afp_end_lock;
26531     } else if( lrc1 != SQLITE_OK ) {
26532       rc = lrc1;
26533     } else {
26534       pFile->eFileLock = SHARED_LOCK;
26535       pInode->nLock++;
26536       pInode->nShared = 1;
26537     }
26538   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
26539     /* We are trying for an exclusive lock but another thread in this
26540      ** same process is still holding a shared lock. */
26541     rc = SQLITE_BUSY;
26542   }else{
26543     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
26544     ** assumed that there is a SHARED or greater lock on the file
26545     ** already.
26546     */
26547     int failed = 0;
26548     assert( 0!=pFile->eFileLock );
26549     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
26550         /* Acquire a RESERVED lock */
26551         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
26552       if( !failed ){
26553         context->reserved = 1;
26554       }
26555     }
26556     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
26557       /* Acquire an EXCLUSIVE lock */
26558 
26559       /* Remove the shared lock before trying the range.  we'll need to
26560       ** reestablish the shared lock if we can't get the  afpUnlock
26561       */
26562       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
26563                          pInode->sharedByte, 1, 0)) ){
26564         int failed2 = SQLITE_OK;
26565         /* now attemmpt to get the exclusive lock range */
26566         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
26567                                SHARED_SIZE, 1);
26568         if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
26569                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
26570           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
26571           ** a critical I/O error
26572           */
26573           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
26574                SQLITE_IOERR_LOCK;
26575           goto afp_end_lock;
26576         }
26577       }else{
26578         rc = failed;
26579       }
26580     }
26581     if( failed ){
26582       rc = failed;
26583     }
26584   }
26585 
26586   if( rc==SQLITE_OK ){
26587     pFile->eFileLock = eFileLock;
26588     pInode->eFileLock = eFileLock;
26589   }else if( eFileLock==EXCLUSIVE_LOCK ){
26590     pFile->eFileLock = PENDING_LOCK;
26591     pInode->eFileLock = PENDING_LOCK;
26592   }
26593 
26594 afp_end_lock:
26595   unixLeaveMutex();
26596   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
26597          rc==SQLITE_OK ? "ok" : "failed"));
26598   return rc;
26599 }
26600 
26601 /*
26602 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26603 ** must be either NO_LOCK or SHARED_LOCK.
26604 **
26605 ** If the locking level of the file descriptor is already at or below
26606 ** the requested locking level, this routine is a no-op.
26607 */
26608 static int afpUnlock(sqlite3_file *id, int eFileLock) {
26609   int rc = SQLITE_OK;
26610   unixFile *pFile = (unixFile*)id;
26611   unixInodeInfo *pInode;
26612   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26613   int skipShared = 0;
26614 #ifdef SQLITE_TEST
26615   int h = pFile->h;
26616 #endif
26617 
26618   assert( pFile );
26619   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
26620            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26621            getpid()));
26622 
26623   assert( eFileLock<=SHARED_LOCK );
26624   if( pFile->eFileLock<=eFileLock ){
26625     return SQLITE_OK;
26626   }
26627   unixEnterMutex();
26628   pInode = pFile->pInode;
26629   assert( pInode->nShared!=0 );
26630   if( pFile->eFileLock>SHARED_LOCK ){
26631     assert( pInode->eFileLock==pFile->eFileLock );
26632     SimulateIOErrorBenign(1);
26633     SimulateIOError( h=(-1) )
26634     SimulateIOErrorBenign(0);
26635 
26636 #ifdef SQLITE_DEBUG
26637     /* When reducing a lock such that other processes can start
26638     ** reading the database file again, make sure that the
26639     ** transaction counter was updated if any part of the database
26640     ** file changed.  If the transaction counter is not updated,
26641     ** other connections to the same file might not realize that
26642     ** the file has changed and hence might not know to flush their
26643     ** cache.  The use of a stale cache can lead to database corruption.
26644     */
26645     assert( pFile->inNormalWrite==0
26646            || pFile->dbUpdate==0
26647            || pFile->transCntrChng==1 );
26648     pFile->inNormalWrite = 0;
26649 #endif
26650 
26651     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
26652       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
26653       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
26654         /* only re-establish the shared lock if necessary */
26655         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
26656         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
26657       } else {
26658         skipShared = 1;
26659       }
26660     }
26661     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
26662       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
26663     }
26664     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
26665       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
26666       if( !rc ){
26667         context->reserved = 0;
26668       }
26669     }
26670     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
26671       pInode->eFileLock = SHARED_LOCK;
26672     }
26673   }
26674   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
26675 
26676     /* Decrement the shared lock counter.  Release the lock using an
26677     ** OS call only when all threads in this same process have released
26678     ** the lock.
26679     */
26680     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
26681     pInode->nShared--;
26682     if( pInode->nShared==0 ){
26683       SimulateIOErrorBenign(1);
26684       SimulateIOError( h=(-1) )
26685       SimulateIOErrorBenign(0);
26686       if( !skipShared ){
26687         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
26688       }
26689       if( !rc ){
26690         pInode->eFileLock = NO_LOCK;
26691         pFile->eFileLock = NO_LOCK;
26692       }
26693     }
26694     if( rc==SQLITE_OK ){
26695       pInode->nLock--;
26696       assert( pInode->nLock>=0 );
26697       if( pInode->nLock==0 ){
26698         closePendingFds(pFile);
26699       }
26700     }
26701   }
26702 
26703   unixLeaveMutex();
26704   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
26705   return rc;
26706 }
26707 
26708 /*
26709 ** Close a file & cleanup AFP specific locking context
26710 */
26711 static int afpClose(sqlite3_file *id) {
26712   int rc = SQLITE_OK;
26713   if( id ){
26714     unixFile *pFile = (unixFile*)id;
26715     afpUnlock(id, NO_LOCK);
26716     unixEnterMutex();
26717     if( pFile->pInode && pFile->pInode->nLock ){
26718       /* If there are outstanding locks, do not actually close the file just
26719       ** yet because that would clear those locks.  Instead, add the file
26720       ** descriptor to pInode->aPending.  It will be automatically closed when
26721       ** the last lock is cleared.
26722       */
26723       setPendingFd(pFile);
26724     }
26725     releaseInodeInfo(pFile);
26726     sqlite3_free(pFile->lockingContext);
26727     rc = closeUnixFile(id);
26728     unixLeaveMutex();
26729   }
26730   return rc;
26731 }
26732 
26733 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
26734 /*
26735 ** The code above is the AFP lock implementation.  The code is specific
26736 ** to MacOSX and does not work on other unix platforms.  No alternative
26737 ** is available.  If you don't compile for a mac, then the "unix-afp"
26738 ** VFS is not available.
26739 **
26740 ********************* End of the AFP lock implementation **********************
26741 ******************************************************************************/
26742 
26743 /******************************************************************************
26744 *************************** Begin NFS Locking ********************************/
26745 
26746 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26747 /*
26748  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26749  ** must be either NO_LOCK or SHARED_LOCK.
26750  **
26751  ** If the locking level of the file descriptor is already at or below
26752  ** the requested locking level, this routine is a no-op.
26753  */
26754 static int nfsUnlock(sqlite3_file *id, int eFileLock){
26755   return posixUnlock(id, eFileLock, 1);
26756 }
26757 
26758 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
26759 /*
26760 ** The code above is the NFS lock implementation.  The code is specific
26761 ** to MacOSX and does not work on other unix platforms.  No alternative
26762 ** is available.
26763 **
26764 ********************* End of the NFS lock implementation **********************
26765 ******************************************************************************/
26766 
26767 /******************************************************************************
26768 **************** Non-locking sqlite3_file methods *****************************
26769 **
26770 ** The next division contains implementations for all methods of the
26771 ** sqlite3_file object other than the locking methods.  The locking
26772 ** methods were defined in divisions above (one locking method per
26773 ** division).  Those methods that are common to all locking modes
26774 ** are gather together into this division.
26775 */
26776 
26777 /*
26778 ** Seek to the offset passed as the second argument, then read cnt
26779 ** bytes into pBuf. Return the number of bytes actually read.
26780 **
26781 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
26782 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
26783 ** one system to another.  Since SQLite does not define USE_PREAD
26784 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
26785 ** See tickets #2741 and #2681.
26786 **
26787 ** To avoid stomping the errno value on a failed read the lastErrno value
26788 ** is set before returning.
26789 */
26790 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
26791   int got;
26792   int prior = 0;
26793 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
26794   i64 newOffset;
26795 #endif
26796   TIMER_START;
26797   assert( cnt==(cnt&0x1ffff) );
26798   assert( id->h>2 );
26799   cnt &= 0x1ffff;
26800   do{
26801 #if defined(USE_PREAD)
26802     got = osPread(id->h, pBuf, cnt, offset);
26803     SimulateIOError( got = -1 );
26804 #elif defined(USE_PREAD64)
26805     got = osPread64(id->h, pBuf, cnt, offset);
26806     SimulateIOError( got = -1 );
26807 #else
26808     newOffset = lseek(id->h, offset, SEEK_SET);
26809     SimulateIOError( newOffset-- );
26810     if( newOffset!=offset ){
26811       if( newOffset == -1 ){
26812         ((unixFile*)id)->lastErrno = errno;
26813       }else{
26814         ((unixFile*)id)->lastErrno = 0;
26815       }
26816       return -1;
26817     }
26818     got = osRead(id->h, pBuf, cnt);
26819 #endif
26820     if( got==cnt ) break;
26821     if( got<0 ){
26822       if( errno==EINTR ){ got = 1; continue; }
26823       prior = 0;
26824       ((unixFile*)id)->lastErrno = errno;
26825       break;
26826     }else if( got>0 ){
26827       cnt -= got;
26828       offset += got;
26829       prior += got;
26830       pBuf = (void*)(got + (char*)pBuf);
26831     }
26832   }while( got>0 );
26833   TIMER_END;
26834   OSTRACE(("READ    %-3d %5d %7lld %llu\n",
26835             id->h, got+prior, offset-prior, TIMER_ELAPSED));
26836   return got+prior;
26837 }
26838 
26839 /*
26840 ** Read data from a file into a buffer.  Return SQLITE_OK if all
26841 ** bytes were read successfully and SQLITE_IOERR if anything goes
26842 ** wrong.
26843 */
26844 static int unixRead(
26845   sqlite3_file *id,
26846   void *pBuf,
26847   int amt,
26848   sqlite3_int64 offset
26849 ){
26850   unixFile *pFile = (unixFile *)id;
26851   int got;
26852   assert( id );
26853   assert( offset>=0 );
26854   assert( amt>0 );
26855 
26856   /* If this is a database file (not a journal, master-journal or temp
26857   ** file), the bytes in the locking range should never be read or written. */
26858 #if 0
26859   assert( pFile->pUnused==0
26860        || offset>=PENDING_BYTE+512
26861        || offset+amt<=PENDING_BYTE
26862   );
26863 #endif
26864 
26865 #if SQLITE_MAX_MMAP_SIZE>0
26866   /* Deal with as much of this read request as possible by transfering
26867   ** data from the memory mapping using memcpy().  */
26868   if( offset<pFile->mmapSize ){
26869     if( offset+amt <= pFile->mmapSize ){
26870       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
26871       return SQLITE_OK;
26872     }else{
26873       int nCopy = pFile->mmapSize - offset;
26874       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
26875       pBuf = &((u8 *)pBuf)[nCopy];
26876       amt -= nCopy;
26877       offset += nCopy;
26878     }
26879   }
26880 #endif
26881 
26882   got = seekAndRead(pFile, offset, pBuf, amt);
26883   if( got==amt ){
26884     return SQLITE_OK;
26885   }else if( got<0 ){
26886     /* lastErrno set by seekAndRead */
26887     return SQLITE_IOERR_READ;
26888   }else{
26889     pFile->lastErrno = 0; /* not a system error */
26890     /* Unread parts of the buffer must be zero-filled */
26891     memset(&((char*)pBuf)[got], 0, amt-got);
26892     return SQLITE_IOERR_SHORT_READ;
26893   }
26894 }
26895 
26896 /*
26897 ** Attempt to seek the file-descriptor passed as the first argument to
26898 ** absolute offset iOff, then attempt to write nBuf bytes of data from
26899 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
26900 ** return the actual number of bytes written (which may be less than
26901 ** nBuf).
26902 */
26903 static int seekAndWriteFd(
26904   int fd,                         /* File descriptor to write to */
26905   i64 iOff,                       /* File offset to begin writing at */
26906   const void *pBuf,               /* Copy data from this buffer to the file */
26907   int nBuf,                       /* Size of buffer pBuf in bytes */
26908   int *piErrno                    /* OUT: Error number if error occurs */
26909 ){
26910   int rc = 0;                     /* Value returned by system call */
26911 
26912   assert( nBuf==(nBuf&0x1ffff) );
26913   assert( fd>2 );
26914   nBuf &= 0x1ffff;
26915   TIMER_START;
26916 
26917 #if defined(USE_PREAD)
26918   do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
26919 #elif defined(USE_PREAD64)
26920   do{ rc = osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
26921 #else
26922   do{
26923     i64 iSeek = lseek(fd, iOff, SEEK_SET);
26924     SimulateIOError( iSeek-- );
26925 
26926     if( iSeek!=iOff ){
26927       if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
26928       return -1;
26929     }
26930     rc = osWrite(fd, pBuf, nBuf);
26931   }while( rc<0 && errno==EINTR );
26932 #endif
26933 
26934   TIMER_END;
26935   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
26936 
26937   if( rc<0 && piErrno ) *piErrno = errno;
26938   return rc;
26939 }
26940 
26941 
26942 /*
26943 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
26944 ** Return the number of bytes actually read.  Update the offset.
26945 **
26946 ** To avoid stomping the errno value on a failed write the lastErrno value
26947 ** is set before returning.
26948 */
26949 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
26950   return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
26951 }
26952 
26953 
26954 /*
26955 ** Write data from a buffer into a file.  Return SQLITE_OK on success
26956 ** or some other error code on failure.
26957 */
26958 static int unixWrite(
26959   sqlite3_file *id,
26960   const void *pBuf,
26961   int amt,
26962   sqlite3_int64 offset
26963 ){
26964   unixFile *pFile = (unixFile*)id;
26965   int wrote = 0;
26966   assert( id );
26967   assert( amt>0 );
26968 
26969   /* If this is a database file (not a journal, master-journal or temp
26970   ** file), the bytes in the locking range should never be read or written. */
26971 #if 0
26972   assert( pFile->pUnused==0
26973        || offset>=PENDING_BYTE+512
26974        || offset+amt<=PENDING_BYTE
26975   );
26976 #endif
26977 
26978 #ifdef SQLITE_DEBUG
26979   /* If we are doing a normal write to a database file (as opposed to
26980   ** doing a hot-journal rollback or a write to some file other than a
26981   ** normal database file) then record the fact that the database
26982   ** has changed.  If the transaction counter is modified, record that
26983   ** fact too.
26984   */
26985   if( pFile->inNormalWrite ){
26986     pFile->dbUpdate = 1;  /* The database has been modified */
26987     if( offset<=24 && offset+amt>=27 ){
26988       int rc;
26989       char oldCntr[4];
26990       SimulateIOErrorBenign(1);
26991       rc = seekAndRead(pFile, 24, oldCntr, 4);
26992       SimulateIOErrorBenign(0);
26993       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
26994         pFile->transCntrChng = 1;  /* The transaction counter has changed */
26995       }
26996     }
26997   }
26998 #endif
26999 
27000 #if SQLITE_MAX_MMAP_SIZE>0
27001   /* Deal with as much of this write request as possible by transfering
27002   ** data from the memory mapping using memcpy().  */
27003   if( offset<pFile->mmapSize ){
27004     if( offset+amt <= pFile->mmapSize ){
27005       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
27006       return SQLITE_OK;
27007     }else{
27008       int nCopy = pFile->mmapSize - offset;
27009       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
27010       pBuf = &((u8 *)pBuf)[nCopy];
27011       amt -= nCopy;
27012       offset += nCopy;
27013     }
27014   }
27015 #endif
27016 
27017   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
27018     amt -= wrote;
27019     offset += wrote;
27020     pBuf = &((char*)pBuf)[wrote];
27021   }
27022   SimulateIOError(( wrote=(-1), amt=1 ));
27023   SimulateDiskfullError(( wrote=0, amt=1 ));
27024 
27025   if( amt>0 ){
27026     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
27027       /* lastErrno set by seekAndWrite */
27028       return SQLITE_IOERR_WRITE;
27029     }else{
27030       pFile->lastErrno = 0; /* not a system error */
27031       return SQLITE_FULL;
27032     }
27033   }
27034 
27035   return SQLITE_OK;
27036 }
27037 
27038 #ifdef SQLITE_TEST
27039 /*
27040 ** Count the number of fullsyncs and normal syncs.  This is used to test
27041 ** that syncs and fullsyncs are occurring at the right times.
27042 */
27043 SQLITE_API int sqlite3_sync_count = 0;
27044 SQLITE_API int sqlite3_fullsync_count = 0;
27045 #endif
27046 
27047 /*
27048 ** We do not trust systems to provide a working fdatasync().  Some do.
27049 ** Others do no.  To be safe, we will stick with the (slightly slower)
27050 ** fsync(). If you know that your system does support fdatasync() correctly,
27051 ** then simply compile with -Dfdatasync=fdatasync
27052 */
27053 #if !defined(fdatasync)
27054 # define fdatasync fsync
27055 #endif
27056 
27057 /*
27058 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
27059 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
27060 ** only available on Mac OS X.  But that could change.
27061 */
27062 #ifdef F_FULLFSYNC
27063 # define HAVE_FULLFSYNC 1
27064 #else
27065 # define HAVE_FULLFSYNC 0
27066 #endif
27067 
27068 
27069 /*
27070 ** The fsync() system call does not work as advertised on many
27071 ** unix systems.  The following procedure is an attempt to make
27072 ** it work better.
27073 **
27074 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
27075 ** for testing when we want to run through the test suite quickly.
27076 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
27077 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
27078 ** or power failure will likely corrupt the database file.
27079 **
27080 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
27081 ** The idea behind dataOnly is that it should only write the file content
27082 ** to disk, not the inode.  We only set dataOnly if the file size is
27083 ** unchanged since the file size is part of the inode.  However,
27084 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
27085 ** file size has changed.  The only real difference between fdatasync()
27086 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
27087 ** inode if the mtime or owner or other inode attributes have changed.
27088 ** We only care about the file size, not the other file attributes, so
27089 ** as far as SQLite is concerned, an fdatasync() is always adequate.
27090 ** So, we always use fdatasync() if it is available, regardless of
27091 ** the value of the dataOnly flag.
27092 */
27093 static int full_fsync(int fd, int fullSync, int dataOnly){
27094   int rc;
27095 
27096   /* The following "ifdef/elif/else/" block has the same structure as
27097   ** the one below. It is replicated here solely to avoid cluttering
27098   ** up the real code with the UNUSED_PARAMETER() macros.
27099   */
27100 #ifdef SQLITE_NO_SYNC
27101   UNUSED_PARAMETER(fd);
27102   UNUSED_PARAMETER(fullSync);
27103   UNUSED_PARAMETER(dataOnly);
27104 #elif HAVE_FULLFSYNC
27105   UNUSED_PARAMETER(dataOnly);
27106 #else
27107   UNUSED_PARAMETER(fullSync);
27108   UNUSED_PARAMETER(dataOnly);
27109 #endif
27110 
27111   /* Record the number of times that we do a normal fsync() and
27112   ** FULLSYNC.  This is used during testing to verify that this procedure
27113   ** gets called with the correct arguments.
27114   */
27115 #ifdef SQLITE_TEST
27116   if( fullSync ) sqlite3_fullsync_count++;
27117   sqlite3_sync_count++;
27118 #endif
27119 
27120   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
27121   ** no-op
27122   */
27123 #ifdef SQLITE_NO_SYNC
27124   rc = SQLITE_OK;
27125 #elif HAVE_FULLFSYNC
27126   if( fullSync ){
27127     rc = osFcntl(fd, F_FULLFSYNC, 0);
27128   }else{
27129     rc = 1;
27130   }
27131   /* If the FULLFSYNC failed, fall back to attempting an fsync().
27132   ** It shouldn't be possible for fullfsync to fail on the local
27133   ** file system (on OSX), so failure indicates that FULLFSYNC
27134   ** isn't supported for this file system. So, attempt an fsync
27135   ** and (for now) ignore the overhead of a superfluous fcntl call.
27136   ** It'd be better to detect fullfsync support once and avoid
27137   ** the fcntl call every time sync is called.
27138   */
27139   if( rc ) rc = fsync(fd);
27140 
27141 #elif defined(__APPLE__)
27142   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
27143   ** so currently we default to the macro that redefines fdatasync to fsync
27144   */
27145   rc = fsync(fd);
27146 #else
27147   rc = fdatasync(fd);
27148 #if OS_VXWORKS
27149   if( rc==-1 && errno==ENOTSUP ){
27150     rc = fsync(fd);
27151   }
27152 #endif /* OS_VXWORKS */
27153 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
27154 
27155   if( OS_VXWORKS && rc!= -1 ){
27156     rc = 0;
27157   }
27158   return rc;
27159 }
27160 
27161 /*
27162 ** Open a file descriptor to the directory containing file zFilename.
27163 ** If successful, *pFd is set to the opened file descriptor and
27164 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
27165 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
27166 ** value.
27167 **
27168 ** The directory file descriptor is used for only one thing - to
27169 ** fsync() a directory to make sure file creation and deletion events
27170 ** are flushed to disk.  Such fsyncs are not needed on newer
27171 ** journaling filesystems, but are required on older filesystems.
27172 **
27173 ** This routine can be overridden using the xSetSysCall interface.
27174 ** The ability to override this routine was added in support of the
27175 ** chromium sandbox.  Opening a directory is a security risk (we are
27176 ** told) so making it overrideable allows the chromium sandbox to
27177 ** replace this routine with a harmless no-op.  To make this routine
27178 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
27179 ** *pFd set to a negative number.
27180 **
27181 ** If SQLITE_OK is returned, the caller is responsible for closing
27182 ** the file descriptor *pFd using close().
27183 */
27184 static int openDirectory(const char *zFilename, int *pFd){
27185   int ii;
27186   int fd = -1;
27187   char zDirname[MAX_PATHNAME+1];
27188 
27189   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
27190   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
27191   if( ii>0 ){
27192     zDirname[ii] = '\0';
27193     fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
27194     if( fd>=0 ){
27195       OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
27196     }
27197   }
27198   *pFd = fd;
27199   return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
27200 }
27201 
27202 /*
27203 ** Make sure all writes to a particular file are committed to disk.
27204 **
27205 ** If dataOnly==0 then both the file itself and its metadata (file
27206 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
27207 ** file data is synced.
27208 **
27209 ** Under Unix, also make sure that the directory entry for the file
27210 ** has been created by fsync-ing the directory that contains the file.
27211 ** If we do not do this and we encounter a power failure, the directory
27212 ** entry for the journal might not exist after we reboot.  The next
27213 ** SQLite to access the file will not know that the journal exists (because
27214 ** the directory entry for the journal was never created) and the transaction
27215 ** will not roll back - possibly leading to database corruption.
27216 */
27217 static int unixSync(sqlite3_file *id, int flags){
27218   int rc;
27219   unixFile *pFile = (unixFile*)id;
27220 
27221   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
27222   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
27223 
27224   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
27225   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
27226       || (flags&0x0F)==SQLITE_SYNC_FULL
27227   );
27228 
27229   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
27230   ** line is to test that doing so does not cause any problems.
27231   */
27232   SimulateDiskfullError( return SQLITE_FULL );
27233 
27234   assert( pFile );
27235   OSTRACE(("SYNC    %-3d\n", pFile->h));
27236   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
27237   SimulateIOError( rc=1 );
27238   if( rc ){
27239     pFile->lastErrno = errno;
27240     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
27241   }
27242 
27243   /* Also fsync the directory containing the file if the DIRSYNC flag
27244   ** is set.  This is a one-time occurrence.  Many systems (examples: AIX)
27245   ** are unable to fsync a directory, so ignore errors on the fsync.
27246   */
27247   if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
27248     int dirfd;
27249     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
27250             HAVE_FULLFSYNC, isFullsync));
27251     rc = osOpenDirectory(pFile->zPath, &dirfd);
27252     if( rc==SQLITE_OK && dirfd>=0 ){
27253       full_fsync(dirfd, 0, 0);
27254       robust_close(pFile, dirfd, __LINE__);
27255     }else if( rc==SQLITE_CANTOPEN ){
27256       rc = SQLITE_OK;
27257     }
27258     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
27259   }
27260   return rc;
27261 }
27262 
27263 /*
27264 ** Truncate an open file to a specified size
27265 */
27266 static int unixTruncate(sqlite3_file *id, i64 nByte){
27267   unixFile *pFile = (unixFile *)id;
27268   int rc;
27269   assert( pFile );
27270   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
27271 
27272   /* If the user has configured a chunk-size for this file, truncate the
27273   ** file so that it consists of an integer number of chunks (i.e. the
27274   ** actual file size after the operation may be larger than the requested
27275   ** size).
27276   */
27277   if( pFile->szChunk>0 ){
27278     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
27279   }
27280 
27281   rc = robust_ftruncate(pFile->h, (off_t)nByte);
27282   if( rc ){
27283     pFile->lastErrno = errno;
27284     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27285   }else{
27286 #ifdef SQLITE_DEBUG
27287     /* If we are doing a normal write to a database file (as opposed to
27288     ** doing a hot-journal rollback or a write to some file other than a
27289     ** normal database file) and we truncate the file to zero length,
27290     ** that effectively updates the change counter.  This might happen
27291     ** when restoring a database using the backup API from a zero-length
27292     ** source.
27293     */
27294     if( pFile->inNormalWrite && nByte==0 ){
27295       pFile->transCntrChng = 1;
27296     }
27297 #endif
27298 
27299 #if SQLITE_MAX_MMAP_SIZE>0
27300     /* If the file was just truncated to a size smaller than the currently
27301     ** mapped region, reduce the effective mapping size as well. SQLite will
27302     ** use read() and write() to access data beyond this point from now on.
27303     */
27304     if( nByte<pFile->mmapSize ){
27305       pFile->mmapSize = nByte;
27306     }
27307 #endif
27308 
27309     return SQLITE_OK;
27310   }
27311 }
27312 
27313 /*
27314 ** Determine the current size of a file in bytes
27315 */
27316 static int unixFileSize(sqlite3_file *id, i64 *pSize){
27317   int rc;
27318   struct stat buf;
27319   assert( id );
27320   rc = osFstat(((unixFile*)id)->h, &buf);
27321   SimulateIOError( rc=1 );
27322   if( rc!=0 ){
27323     ((unixFile*)id)->lastErrno = errno;
27324     return SQLITE_IOERR_FSTAT;
27325   }
27326   *pSize = buf.st_size;
27327 
27328   /* When opening a zero-size database, the findInodeInfo() procedure
27329   ** writes a single byte into that file in order to work around a bug
27330   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
27331   ** layers, we need to report this file size as zero even though it is
27332   ** really 1.   Ticket #3260.
27333   */
27334   if( *pSize==1 ) *pSize = 0;
27335 
27336 
27337   return SQLITE_OK;
27338 }
27339 
27340 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27341 /*
27342 ** Handler for proxy-locking file-control verbs.  Defined below in the
27343 ** proxying locking division.
27344 */
27345 static int proxyFileControl(sqlite3_file*,int,void*);
27346 #endif
27347 
27348 /*
27349 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
27350 ** file-control operation.  Enlarge the database to nBytes in size
27351 ** (rounded up to the next chunk-size).  If the database is already
27352 ** nBytes or larger, this routine is a no-op.
27353 */
27354 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27355   if( pFile->szChunk>0 ){
27356     i64 nSize;                    /* Required file size */
27357     struct stat buf;              /* Used to hold return values of fstat() */
27358 
27359     if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
27360 
27361     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
27362     if( nSize>(i64)buf.st_size ){
27363 
27364 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
27365       /* The code below is handling the return value of osFallocate()
27366       ** correctly. posix_fallocate() is defined to "returns zero on success,
27367       ** or an error number on  failure". See the manpage for details. */
27368       int err;
27369       do{
27370         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
27371       }while( err==EINTR );
27372       if( err ) return SQLITE_IOERR_WRITE;
27373 #else
27374       /* If the OS does not have posix_fallocate(), fake it. First use
27375       ** ftruncate() to set the file size, then write a single byte to
27376       ** the last byte in each block within the extended region. This
27377       ** is the same technique used by glibc to implement posix_fallocate()
27378       ** on systems that do not have a real fallocate() system call.
27379       */
27380       int nBlk = buf.st_blksize;  /* File-system block size */
27381       i64 iWrite;                 /* Next offset to write to */
27382 
27383       if( robust_ftruncate(pFile->h, nSize) ){
27384         pFile->lastErrno = errno;
27385         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27386       }
27387       iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
27388       while( iWrite<nSize ){
27389         int nWrite = seekAndWrite(pFile, iWrite, "", 1);
27390         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
27391         iWrite += nBlk;
27392       }
27393 #endif
27394     }
27395   }
27396 
27397 #if SQLITE_MAX_MMAP_SIZE>0
27398   if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
27399     int rc;
27400     if( pFile->szChunk<=0 ){
27401       if( robust_ftruncate(pFile->h, nByte) ){
27402         pFile->lastErrno = errno;
27403         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27404       }
27405     }
27406 
27407     rc = unixMapfile(pFile, nByte);
27408     return rc;
27409   }
27410 #endif
27411 
27412   return SQLITE_OK;
27413 }
27414 
27415 /*
27416 ** If *pArg is inititially negative then this is a query.  Set *pArg to
27417 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
27418 **
27419 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
27420 */
27421 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
27422   if( *pArg<0 ){
27423     *pArg = (pFile->ctrlFlags & mask)!=0;
27424   }else if( (*pArg)==0 ){
27425     pFile->ctrlFlags &= ~mask;
27426   }else{
27427     pFile->ctrlFlags |= mask;
27428   }
27429 }
27430 
27431 /* Forward declaration */
27432 static int unixGetTempname(int nBuf, char *zBuf);
27433 
27434 /*
27435 ** Information and control of an open file handle.
27436 */
27437 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
27438   unixFile *pFile = (unixFile*)id;
27439   switch( op ){
27440     case SQLITE_FCNTL_LOCKSTATE: {
27441       *(int*)pArg = pFile->eFileLock;
27442       return SQLITE_OK;
27443     }
27444     case SQLITE_LAST_ERRNO: {
27445       *(int*)pArg = pFile->lastErrno;
27446       return SQLITE_OK;
27447     }
27448     case SQLITE_FCNTL_CHUNK_SIZE: {
27449       pFile->szChunk = *(int *)pArg;
27450       return SQLITE_OK;
27451     }
27452     case SQLITE_FCNTL_SIZE_HINT: {
27453       int rc;
27454       SimulateIOErrorBenign(1);
27455       rc = fcntlSizeHint(pFile, *(i64 *)pArg);
27456       SimulateIOErrorBenign(0);
27457       return rc;
27458     }
27459     case SQLITE_FCNTL_PERSIST_WAL: {
27460       unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
27461       return SQLITE_OK;
27462     }
27463     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
27464       unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
27465       return SQLITE_OK;
27466     }
27467     case SQLITE_FCNTL_VFSNAME: {
27468       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
27469       return SQLITE_OK;
27470     }
27471     case SQLITE_FCNTL_TEMPFILENAME: {
27472       char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
27473       if( zTFile ){
27474         unixGetTempname(pFile->pVfs->mxPathname, zTFile);
27475         *(char**)pArg = zTFile;
27476       }
27477       return SQLITE_OK;
27478     }
27479     case SQLITE_FCNTL_HAS_MOVED: {
27480       *(int*)pArg = fileHasMoved(pFile);
27481       return SQLITE_OK;
27482     }
27483 #if SQLITE_MAX_MMAP_SIZE>0
27484     case SQLITE_FCNTL_MMAP_SIZE: {
27485       i64 newLimit = *(i64*)pArg;
27486       int rc = SQLITE_OK;
27487       if( newLimit>sqlite3GlobalConfig.mxMmap ){
27488         newLimit = sqlite3GlobalConfig.mxMmap;
27489       }
27490       *(i64*)pArg = pFile->mmapSizeMax;
27491       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
27492         pFile->mmapSizeMax = newLimit;
27493         if( pFile->mmapSize>0 ){
27494           unixUnmapfile(pFile);
27495           rc = unixMapfile(pFile, -1);
27496         }
27497       }
27498       return rc;
27499     }
27500 #endif
27501 #ifdef SQLITE_DEBUG
27502     /* The pager calls this method to signal that it has done
27503     ** a rollback and that the database is therefore unchanged and
27504     ** it hence it is OK for the transaction change counter to be
27505     ** unchanged.
27506     */
27507     case SQLITE_FCNTL_DB_UNCHANGED: {
27508       ((unixFile*)id)->dbUpdate = 0;
27509       return SQLITE_OK;
27510     }
27511 #endif
27512 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27513     case SQLITE_SET_LOCKPROXYFILE:
27514     case SQLITE_GET_LOCKPROXYFILE: {
27515       return proxyFileControl(id,op,pArg);
27516     }
27517 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
27518   }
27519   return SQLITE_NOTFOUND;
27520 }
27521 
27522 /*
27523 ** Return the sector size in bytes of the underlying block device for
27524 ** the specified file. This is almost always 512 bytes, but may be
27525 ** larger for some devices.
27526 **
27527 ** SQLite code assumes this function cannot fail. It also assumes that
27528 ** if two files are created in the same file-system directory (i.e.
27529 ** a database and its journal file) that the sector size will be the
27530 ** same for both.
27531 */
27532 #ifndef __QNXNTO__
27533 static int unixSectorSize(sqlite3_file *NotUsed){
27534   UNUSED_PARAMETER(NotUsed);
27535   return SQLITE_DEFAULT_SECTOR_SIZE;
27536 }
27537 #endif
27538 
27539 /*
27540 ** The following version of unixSectorSize() is optimized for QNX.
27541 */
27542 #ifdef __QNXNTO__
27543 #include <sys/dcmd_blk.h>
27544 #include <sys/statvfs.h>
27545 static int unixSectorSize(sqlite3_file *id){
27546   unixFile *pFile = (unixFile*)id;
27547   if( pFile->sectorSize == 0 ){
27548     struct statvfs fsInfo;
27549 
27550     /* Set defaults for non-supported filesystems */
27551     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
27552     pFile->deviceCharacteristics = 0;
27553     if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
27554       return pFile->sectorSize;
27555     }
27556 
27557     if( !strcmp(fsInfo.f_basetype, "tmp") ) {
27558       pFile->sectorSize = fsInfo.f_bsize;
27559       pFile->deviceCharacteristics =
27560         SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
27561         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27562                                       ** the write succeeds */
27563         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27564                                       ** so it is ordered */
27565         0;
27566     }else if( strstr(fsInfo.f_basetype, "etfs") ){
27567       pFile->sectorSize = fsInfo.f_bsize;
27568       pFile->deviceCharacteristics =
27569         /* etfs cluster size writes are atomic */
27570         (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
27571         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27572                                       ** the write succeeds */
27573         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27574                                       ** so it is ordered */
27575         0;
27576     }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
27577       pFile->sectorSize = fsInfo.f_bsize;
27578       pFile->deviceCharacteristics =
27579         SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
27580         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27581                                       ** the write succeeds */
27582         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27583                                       ** so it is ordered */
27584         0;
27585     }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
27586       pFile->sectorSize = fsInfo.f_bsize;
27587       pFile->deviceCharacteristics =
27588         /* full bitset of atomics from max sector size and smaller */
27589         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
27590         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27591                                       ** so it is ordered */
27592         0;
27593     }else if( strstr(fsInfo.f_basetype, "dos") ){
27594       pFile->sectorSize = fsInfo.f_bsize;
27595       pFile->deviceCharacteristics =
27596         /* full bitset of atomics from max sector size and smaller */
27597         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
27598         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27599                                       ** so it is ordered */
27600         0;
27601     }else{
27602       pFile->deviceCharacteristics =
27603         SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
27604         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27605                                       ** the write succeeds */
27606         0;
27607     }
27608   }
27609   /* Last chance verification.  If the sector size isn't a multiple of 512
27610   ** then it isn't valid.*/
27611   if( pFile->sectorSize % 512 != 0 ){
27612     pFile->deviceCharacteristics = 0;
27613     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
27614   }
27615   return pFile->sectorSize;
27616 }
27617 #endif /* __QNXNTO__ */
27618 
27619 /*
27620 ** Return the device characteristics for the file.
27621 **
27622 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
27623 ** However, that choice is contraversial since technically the underlying
27624 ** file system does not always provide powersafe overwrites.  (In other
27625 ** words, after a power-loss event, parts of the file that were never
27626 ** written might end up being altered.)  However, non-PSOW behavior is very,
27627 ** very rare.  And asserting PSOW makes a large reduction in the amount
27628 ** of required I/O for journaling, since a lot of padding is eliminated.
27629 **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
27630 ** available to turn it off and URI query parameter available to turn it off.
27631 */
27632 static int unixDeviceCharacteristics(sqlite3_file *id){
27633   unixFile *p = (unixFile*)id;
27634   int rc = 0;
27635 #ifdef __QNXNTO__
27636   if( p->sectorSize==0 ) unixSectorSize(id);
27637   rc = p->deviceCharacteristics;
27638 #endif
27639   if( p->ctrlFlags & UNIXFILE_PSOW ){
27640     rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
27641   }
27642   return rc;
27643 }
27644 
27645 #ifndef SQLITE_OMIT_WAL
27646 
27647 
27648 /*
27649 ** Object used to represent an shared memory buffer.
27650 **
27651 ** When multiple threads all reference the same wal-index, each thread
27652 ** has its own unixShm object, but they all point to a single instance
27653 ** of this unixShmNode object.  In other words, each wal-index is opened
27654 ** only once per process.
27655 **
27656 ** Each unixShmNode object is connected to a single unixInodeInfo object.
27657 ** We could coalesce this object into unixInodeInfo, but that would mean
27658 ** every open file that does not use shared memory (in other words, most
27659 ** open files) would have to carry around this extra information.  So
27660 ** the unixInodeInfo object contains a pointer to this unixShmNode object
27661 ** and the unixShmNode object is created only when needed.
27662 **
27663 ** unixMutexHeld() must be true when creating or destroying
27664 ** this object or while reading or writing the following fields:
27665 **
27666 **      nRef
27667 **
27668 ** The following fields are read-only after the object is created:
27669 **
27670 **      fid
27671 **      zFilename
27672 **
27673 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
27674 ** unixMutexHeld() is true when reading or writing any other field
27675 ** in this structure.
27676 */
27677 struct unixShmNode {
27678   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
27679   sqlite3_mutex *mutex;      /* Mutex to access this object */
27680   char *zFilename;           /* Name of the mmapped file */
27681   int h;                     /* Open file descriptor */
27682   int szRegion;              /* Size of shared-memory regions */
27683   u16 nRegion;               /* Size of array apRegion */
27684   u8 isReadonly;             /* True if read-only */
27685   char **apRegion;           /* Array of mapped shared-memory regions */
27686   int nRef;                  /* Number of unixShm objects pointing to this */
27687   unixShm *pFirst;           /* All unixShm objects pointing to this */
27688 #ifdef SQLITE_DEBUG
27689   u8 exclMask;               /* Mask of exclusive locks held */
27690   u8 sharedMask;             /* Mask of shared locks held */
27691   u8 nextShmId;              /* Next available unixShm.id value */
27692 #endif
27693 };
27694 
27695 /*
27696 ** Structure used internally by this VFS to record the state of an
27697 ** open shared memory connection.
27698 **
27699 ** The following fields are initialized when this object is created and
27700 ** are read-only thereafter:
27701 **
27702 **    unixShm.pFile
27703 **    unixShm.id
27704 **
27705 ** All other fields are read/write.  The unixShm.pFile->mutex must be held
27706 ** while accessing any read/write fields.
27707 */
27708 struct unixShm {
27709   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
27710   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
27711   u8 hasMutex;               /* True if holding the unixShmNode mutex */
27712   u8 id;                     /* Id of this connection within its unixShmNode */
27713   u16 sharedMask;            /* Mask of shared locks held */
27714   u16 exclMask;              /* Mask of exclusive locks held */
27715 };
27716 
27717 /*
27718 ** Constants used for locking
27719 */
27720 #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
27721 #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
27722 
27723 /*
27724 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
27725 **
27726 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
27727 ** otherwise.
27728 */
27729 static int unixShmSystemLock(
27730   unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
27731   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
27732   int ofst,              /* First byte of the locking range */
27733   int n                  /* Number of bytes to lock */
27734 ){
27735   struct flock f;       /* The posix advisory locking structure */
27736   int rc = SQLITE_OK;   /* Result code form fcntl() */
27737 
27738   /* Access to the unixShmNode object is serialized by the caller */
27739   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
27740 
27741   /* Shared locks never span more than one byte */
27742   assert( n==1 || lockType!=F_RDLCK );
27743 
27744   /* Locks are within range */
27745   assert( n>=1 && n<SQLITE_SHM_NLOCK );
27746 
27747   if( pShmNode->h>=0 ){
27748     /* Initialize the locking parameters */
27749     memset(&f, 0, sizeof(f));
27750     f.l_type = lockType;
27751     f.l_whence = SEEK_SET;
27752     f.l_start = ofst;
27753     f.l_len = n;
27754 
27755     rc = osFcntl(pShmNode->h, F_SETLK, &f);
27756     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
27757   }
27758 
27759   /* Update the global lock state and do debug tracing */
27760 #ifdef SQLITE_DEBUG
27761   { u16 mask;
27762   OSTRACE(("SHM-LOCK "));
27763   mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
27764   if( rc==SQLITE_OK ){
27765     if( lockType==F_UNLCK ){
27766       OSTRACE(("unlock %d ok", ofst));
27767       pShmNode->exclMask &= ~mask;
27768       pShmNode->sharedMask &= ~mask;
27769     }else if( lockType==F_RDLCK ){
27770       OSTRACE(("read-lock %d ok", ofst));
27771       pShmNode->exclMask &= ~mask;
27772       pShmNode->sharedMask |= mask;
27773     }else{
27774       assert( lockType==F_WRLCK );
27775       OSTRACE(("write-lock %d ok", ofst));
27776       pShmNode->exclMask |= mask;
27777       pShmNode->sharedMask &= ~mask;
27778     }
27779   }else{
27780     if( lockType==F_UNLCK ){
27781       OSTRACE(("unlock %d failed", ofst));
27782     }else if( lockType==F_RDLCK ){
27783       OSTRACE(("read-lock failed"));
27784     }else{
27785       assert( lockType==F_WRLCK );
27786       OSTRACE(("write-lock %d failed", ofst));
27787     }
27788   }
27789   OSTRACE((" - afterwards %03x,%03x\n",
27790            pShmNode->sharedMask, pShmNode->exclMask));
27791   }
27792 #endif
27793 
27794   return rc;
27795 }
27796 
27797 
27798 /*
27799 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
27800 **
27801 ** This is not a VFS shared-memory method; it is a utility function called
27802 ** by VFS shared-memory methods.
27803 */
27804 static void unixShmPurge(unixFile *pFd){
27805   unixShmNode *p = pFd->pInode->pShmNode;
27806   assert( unixMutexHeld() );
27807   if( p && p->nRef==0 ){
27808     int i;
27809     assert( p->pInode==pFd->pInode );
27810     sqlite3_mutex_free(p->mutex);
27811     for(i=0; i<p->nRegion; i++){
27812       if( p->h>=0 ){
27813         osMunmap(p->apRegion[i], p->szRegion);
27814       }else{
27815         sqlite3_free(p->apRegion[i]);
27816       }
27817     }
27818     sqlite3_free(p->apRegion);
27819     if( p->h>=0 ){
27820       robust_close(pFd, p->h, __LINE__);
27821       p->h = -1;
27822     }
27823     p->pInode->pShmNode = 0;
27824     sqlite3_free(p);
27825   }
27826 }
27827 
27828 /*
27829 ** Open a shared-memory area associated with open database file pDbFd.
27830 ** This particular implementation uses mmapped files.
27831 **
27832 ** The file used to implement shared-memory is in the same directory
27833 ** as the open database file and has the same name as the open database
27834 ** file with the "-shm" suffix added.  For example, if the database file
27835 ** is "/home/user1/config.db" then the file that is created and mmapped
27836 ** for shared memory will be called "/home/user1/config.db-shm".
27837 **
27838 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
27839 ** some other tmpfs mount. But if a file in a different directory
27840 ** from the database file is used, then differing access permissions
27841 ** or a chroot() might cause two different processes on the same
27842 ** database to end up using different files for shared memory -
27843 ** meaning that their memory would not really be shared - resulting
27844 ** in database corruption.  Nevertheless, this tmpfs file usage
27845 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
27846 ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
27847 ** option results in an incompatible build of SQLite;  builds of SQLite
27848 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
27849 ** same database file at the same time, database corruption will likely
27850 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
27851 ** "unsupported" and may go away in a future SQLite release.
27852 **
27853 ** When opening a new shared-memory file, if no other instances of that
27854 ** file are currently open, in this process or in other processes, then
27855 ** the file must be truncated to zero length or have its header cleared.
27856 **
27857 ** If the original database file (pDbFd) is using the "unix-excl" VFS
27858 ** that means that an exclusive lock is held on the database file and
27859 ** that no other processes are able to read or write the database.  In
27860 ** that case, we do not really need shared memory.  No shared memory
27861 ** file is created.  The shared memory will be simulated with heap memory.
27862 */
27863 static int unixOpenSharedMemory(unixFile *pDbFd){
27864   struct unixShm *p = 0;          /* The connection to be opened */
27865   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
27866   int rc;                         /* Result code */
27867   unixInodeInfo *pInode;          /* The inode of fd */
27868   char *zShmFilename;             /* Name of the file used for SHM */
27869   int nShmFilename;               /* Size of the SHM filename in bytes */
27870 
27871   /* Allocate space for the new unixShm object. */
27872   p = sqlite3_malloc( sizeof(*p) );
27873   if( p==0 ) return SQLITE_NOMEM;
27874   memset(p, 0, sizeof(*p));
27875   assert( pDbFd->pShm==0 );
27876 
27877   /* Check to see if a unixShmNode object already exists. Reuse an existing
27878   ** one if present. Create a new one if necessary.
27879   */
27880   unixEnterMutex();
27881   pInode = pDbFd->pInode;
27882   pShmNode = pInode->pShmNode;
27883   if( pShmNode==0 ){
27884     struct stat sStat;                 /* fstat() info for database file */
27885 
27886     /* Call fstat() to figure out the permissions on the database file. If
27887     ** a new *-shm file is created, an attempt will be made to create it
27888     ** with the same permissions.
27889     */
27890     if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
27891       rc = SQLITE_IOERR_FSTAT;
27892       goto shm_open_err;
27893     }
27894 
27895 #ifdef SQLITE_SHM_DIRECTORY
27896     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
27897 #else
27898     nShmFilename = 6 + (int)strlen(pDbFd->zPath);
27899 #endif
27900     pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
27901     if( pShmNode==0 ){
27902       rc = SQLITE_NOMEM;
27903       goto shm_open_err;
27904     }
27905     memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
27906     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
27907 #ifdef SQLITE_SHM_DIRECTORY
27908     sqlite3_snprintf(nShmFilename, zShmFilename,
27909                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
27910                      (u32)sStat.st_ino, (u32)sStat.st_dev);
27911 #else
27912     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
27913     sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
27914 #endif
27915     pShmNode->h = -1;
27916     pDbFd->pInode->pShmNode = pShmNode;
27917     pShmNode->pInode = pDbFd->pInode;
27918     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
27919     if( pShmNode->mutex==0 ){
27920       rc = SQLITE_NOMEM;
27921       goto shm_open_err;
27922     }
27923 
27924     if( pInode->bProcessLock==0 ){
27925       int openFlags = O_RDWR | O_CREAT;
27926       if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
27927         openFlags = O_RDONLY;
27928         pShmNode->isReadonly = 1;
27929       }
27930       pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
27931       if( pShmNode->h<0 ){
27932         rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
27933         goto shm_open_err;
27934       }
27935 
27936       /* If this process is running as root, make sure that the SHM file
27937       ** is owned by the same user that owns the original database.  Otherwise,
27938       ** the original owner will not be able to connect.
27939       */
27940       osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
27941 
27942       /* Check to see if another process is holding the dead-man switch.
27943       ** If not, truncate the file to zero length.
27944       */
27945       rc = SQLITE_OK;
27946       if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
27947         if( robust_ftruncate(pShmNode->h, 0) ){
27948           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
27949         }
27950       }
27951       if( rc==SQLITE_OK ){
27952         rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
27953       }
27954       if( rc ) goto shm_open_err;
27955     }
27956   }
27957 
27958   /* Make the new connection a child of the unixShmNode */
27959   p->pShmNode = pShmNode;
27960 #ifdef SQLITE_DEBUG
27961   p->id = pShmNode->nextShmId++;
27962 #endif
27963   pShmNode->nRef++;
27964   pDbFd->pShm = p;
27965   unixLeaveMutex();
27966 
27967   /* The reference count on pShmNode has already been incremented under
27968   ** the cover of the unixEnterMutex() mutex and the pointer from the
27969   ** new (struct unixShm) object to the pShmNode has been set. All that is
27970   ** left to do is to link the new object into the linked list starting
27971   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
27972   ** mutex.
27973   */
27974   sqlite3_mutex_enter(pShmNode->mutex);
27975   p->pNext = pShmNode->pFirst;
27976   pShmNode->pFirst = p;
27977   sqlite3_mutex_leave(pShmNode->mutex);
27978   return SQLITE_OK;
27979 
27980   /* Jump here on any error */
27981 shm_open_err:
27982   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
27983   sqlite3_free(p);
27984   unixLeaveMutex();
27985   return rc;
27986 }
27987 
27988 /*
27989 ** This function is called to obtain a pointer to region iRegion of the
27990 ** shared-memory associated with the database file fd. Shared-memory regions
27991 ** are numbered starting from zero. Each shared-memory region is szRegion
27992 ** bytes in size.
27993 **
27994 ** If an error occurs, an error code is returned and *pp is set to NULL.
27995 **
27996 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
27997 ** region has not been allocated (by any client, including one running in a
27998 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
27999 ** bExtend is non-zero and the requested shared-memory region has not yet
28000 ** been allocated, it is allocated by this function.
28001 **
28002 ** If the shared-memory region has already been allocated or is allocated by
28003 ** this call as described above, then it is mapped into this processes
28004 ** address space (if it is not already), *pp is set to point to the mapped
28005 ** memory and SQLITE_OK returned.
28006 */
28007 static int unixShmMap(
28008   sqlite3_file *fd,               /* Handle open on database file */
28009   int iRegion,                    /* Region to retrieve */
28010   int szRegion,                   /* Size of regions */
28011   int bExtend,                    /* True to extend file if necessary */
28012   void volatile **pp              /* OUT: Mapped memory */
28013 ){
28014   unixFile *pDbFd = (unixFile*)fd;
28015   unixShm *p;
28016   unixShmNode *pShmNode;
28017   int rc = SQLITE_OK;
28018 
28019   /* If the shared-memory file has not yet been opened, open it now. */
28020   if( pDbFd->pShm==0 ){
28021     rc = unixOpenSharedMemory(pDbFd);
28022     if( rc!=SQLITE_OK ) return rc;
28023   }
28024 
28025   p = pDbFd->pShm;
28026   pShmNode = p->pShmNode;
28027   sqlite3_mutex_enter(pShmNode->mutex);
28028   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
28029   assert( pShmNode->pInode==pDbFd->pInode );
28030   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28031   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28032 
28033   if( pShmNode->nRegion<=iRegion ){
28034     char **apNew;                      /* New apRegion[] array */
28035     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
28036     struct stat sStat;                 /* Used by fstat() */
28037 
28038     pShmNode->szRegion = szRegion;
28039 
28040     if( pShmNode->h>=0 ){
28041       /* The requested region is not mapped into this processes address space.
28042       ** Check to see if it has been allocated (i.e. if the wal-index file is
28043       ** large enough to contain the requested region).
28044       */
28045       if( osFstat(pShmNode->h, &sStat) ){
28046         rc = SQLITE_IOERR_SHMSIZE;
28047         goto shmpage_out;
28048       }
28049 
28050       if( sStat.st_size<nByte ){
28051         /* The requested memory region does not exist. If bExtend is set to
28052         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
28053         */
28054         if( !bExtend ){
28055           goto shmpage_out;
28056         }
28057 
28058         /* Alternatively, if bExtend is true, extend the file. Do this by
28059         ** writing a single byte to the end of each (OS) page being
28060         ** allocated or extended. Technically, we need only write to the
28061         ** last page in order to extend the file. But writing to all new
28062         ** pages forces the OS to allocate them immediately, which reduces
28063         ** the chances of SIGBUS while accessing the mapped region later on.
28064         */
28065         else{
28066           static const int pgsz = 4096;
28067           int iPg;
28068 
28069           /* Write to the last byte of each newly allocated or extended page */
28070           assert( (nByte % pgsz)==0 );
28071           for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
28072             if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){
28073               const char *zFile = pShmNode->zFilename;
28074               rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
28075               goto shmpage_out;
28076             }
28077           }
28078         }
28079       }
28080     }
28081 
28082     /* Map the requested memory region into this processes address space. */
28083     apNew = (char **)sqlite3_realloc(
28084         pShmNode->apRegion, (iRegion+1)*sizeof(char *)
28085     );
28086     if( !apNew ){
28087       rc = SQLITE_IOERR_NOMEM;
28088       goto shmpage_out;
28089     }
28090     pShmNode->apRegion = apNew;
28091     while(pShmNode->nRegion<=iRegion){
28092       void *pMem;
28093       if( pShmNode->h>=0 ){
28094         pMem = osMmap(0, szRegion,
28095             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
28096             MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
28097         );
28098         if( pMem==MAP_FAILED ){
28099           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
28100           goto shmpage_out;
28101         }
28102       }else{
28103         pMem = sqlite3_malloc(szRegion);
28104         if( pMem==0 ){
28105           rc = SQLITE_NOMEM;
28106           goto shmpage_out;
28107         }
28108         memset(pMem, 0, szRegion);
28109       }
28110       pShmNode->apRegion[pShmNode->nRegion] = pMem;
28111       pShmNode->nRegion++;
28112     }
28113   }
28114 
28115 shmpage_out:
28116   if( pShmNode->nRegion>iRegion ){
28117     *pp = pShmNode->apRegion[iRegion];
28118   }else{
28119     *pp = 0;
28120   }
28121   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
28122   sqlite3_mutex_leave(pShmNode->mutex);
28123   return rc;
28124 }
28125 
28126 /*
28127 ** Change the lock state for a shared-memory segment.
28128 **
28129 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
28130 ** different here than in posix.  In xShmLock(), one can go from unlocked
28131 ** to shared and back or from unlocked to exclusive and back.  But one may
28132 ** not go from shared to exclusive or from exclusive to shared.
28133 */
28134 static int unixShmLock(
28135   sqlite3_file *fd,          /* Database file holding the shared memory */
28136   int ofst,                  /* First lock to acquire or release */
28137   int n,                     /* Number of locks to acquire or release */
28138   int flags                  /* What to do with the lock */
28139 ){
28140   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
28141   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
28142   unixShm *pX;                          /* For looping over all siblings */
28143   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
28144   int rc = SQLITE_OK;                   /* Result code */
28145   u16 mask;                             /* Mask of locks to take or release */
28146 
28147   assert( pShmNode==pDbFd->pInode->pShmNode );
28148   assert( pShmNode->pInode==pDbFd->pInode );
28149   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
28150   assert( n>=1 );
28151   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
28152        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
28153        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
28154        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
28155   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
28156   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28157   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28158 
28159   mask = (1<<(ofst+n)) - (1<<ofst);
28160   assert( n>1 || mask==(1<<ofst) );
28161   sqlite3_mutex_enter(pShmNode->mutex);
28162   if( flags & SQLITE_SHM_UNLOCK ){
28163     u16 allMask = 0; /* Mask of locks held by siblings */
28164 
28165     /* See if any siblings hold this same lock */
28166     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28167       if( pX==p ) continue;
28168       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
28169       allMask |= pX->sharedMask;
28170     }
28171 
28172     /* Unlock the system-level locks */
28173     if( (mask & allMask)==0 ){
28174       rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
28175     }else{
28176       rc = SQLITE_OK;
28177     }
28178 
28179     /* Undo the local locks */
28180     if( rc==SQLITE_OK ){
28181       p->exclMask &= ~mask;
28182       p->sharedMask &= ~mask;
28183     }
28184   }else if( flags & SQLITE_SHM_SHARED ){
28185     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
28186 
28187     /* Find out which shared locks are already held by sibling connections.
28188     ** If any sibling already holds an exclusive lock, go ahead and return
28189     ** SQLITE_BUSY.
28190     */
28191     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28192       if( (pX->exclMask & mask)!=0 ){
28193         rc = SQLITE_BUSY;
28194         break;
28195       }
28196       allShared |= pX->sharedMask;
28197     }
28198 
28199     /* Get shared locks at the system level, if necessary */
28200     if( rc==SQLITE_OK ){
28201       if( (allShared & mask)==0 ){
28202         rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
28203       }else{
28204         rc = SQLITE_OK;
28205       }
28206     }
28207 
28208     /* Get the local shared locks */
28209     if( rc==SQLITE_OK ){
28210       p->sharedMask |= mask;
28211     }
28212   }else{
28213     /* Make sure no sibling connections hold locks that will block this
28214     ** lock.  If any do, return SQLITE_BUSY right away.
28215     */
28216     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28217       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
28218         rc = SQLITE_BUSY;
28219         break;
28220       }
28221     }
28222 
28223     /* Get the exclusive locks at the system level.  Then if successful
28224     ** also mark the local connection as being locked.
28225     */
28226     if( rc==SQLITE_OK ){
28227       rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
28228       if( rc==SQLITE_OK ){
28229         assert( (p->sharedMask & mask)==0 );
28230         p->exclMask |= mask;
28231       }
28232     }
28233   }
28234   sqlite3_mutex_leave(pShmNode->mutex);
28235   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
28236            p->id, getpid(), p->sharedMask, p->exclMask));
28237   return rc;
28238 }
28239 
28240 /*
28241 ** Implement a memory barrier or memory fence on shared memory.
28242 **
28243 ** All loads and stores begun before the barrier must complete before
28244 ** any load or store begun after the barrier.
28245 */
28246 static void unixShmBarrier(
28247   sqlite3_file *fd                /* Database file holding the shared memory */
28248 ){
28249   UNUSED_PARAMETER(fd);
28250   unixEnterMutex();
28251   unixLeaveMutex();
28252 }
28253 
28254 /*
28255 ** Close a connection to shared-memory.  Delete the underlying
28256 ** storage if deleteFlag is true.
28257 **
28258 ** If there is no shared memory associated with the connection then this
28259 ** routine is a harmless no-op.
28260 */
28261 static int unixShmUnmap(
28262   sqlite3_file *fd,               /* The underlying database file */
28263   int deleteFlag                  /* Delete shared-memory if true */
28264 ){
28265   unixShm *p;                     /* The connection to be closed */
28266   unixShmNode *pShmNode;          /* The underlying shared-memory file */
28267   unixShm **pp;                   /* For looping over sibling connections */
28268   unixFile *pDbFd;                /* The underlying database file */
28269 
28270   pDbFd = (unixFile*)fd;
28271   p = pDbFd->pShm;
28272   if( p==0 ) return SQLITE_OK;
28273   pShmNode = p->pShmNode;
28274 
28275   assert( pShmNode==pDbFd->pInode->pShmNode );
28276   assert( pShmNode->pInode==pDbFd->pInode );
28277 
28278   /* Remove connection p from the set of connections associated
28279   ** with pShmNode */
28280   sqlite3_mutex_enter(pShmNode->mutex);
28281   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
28282   *pp = p->pNext;
28283 
28284   /* Free the connection p */
28285   sqlite3_free(p);
28286   pDbFd->pShm = 0;
28287   sqlite3_mutex_leave(pShmNode->mutex);
28288 
28289   /* If pShmNode->nRef has reached 0, then close the underlying
28290   ** shared-memory file, too */
28291   unixEnterMutex();
28292   assert( pShmNode->nRef>0 );
28293   pShmNode->nRef--;
28294   if( pShmNode->nRef==0 ){
28295     if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
28296     unixShmPurge(pDbFd);
28297   }
28298   unixLeaveMutex();
28299 
28300   return SQLITE_OK;
28301 }
28302 
28303 
28304 #else
28305 # define unixShmMap     0
28306 # define unixShmLock    0
28307 # define unixShmBarrier 0
28308 # define unixShmUnmap   0
28309 #endif /* #ifndef SQLITE_OMIT_WAL */
28310 
28311 #if SQLITE_MAX_MMAP_SIZE>0
28312 /*
28313 ** If it is currently memory mapped, unmap file pFd.
28314 */
28315 static void unixUnmapfile(unixFile *pFd){
28316   assert( pFd->nFetchOut==0 );
28317   if( pFd->pMapRegion ){
28318     osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
28319     pFd->pMapRegion = 0;
28320     pFd->mmapSize = 0;
28321     pFd->mmapSizeActual = 0;
28322   }
28323 }
28324 
28325 /*
28326 ** Return the system page size.
28327 */
28328 static int unixGetPagesize(void){
28329 #if HAVE_MREMAP
28330   return 512;
28331 #elif defined(_BSD_SOURCE)
28332   return getpagesize();
28333 #else
28334   return (int)sysconf(_SC_PAGESIZE);
28335 #endif
28336 }
28337 
28338 /*
28339 ** Attempt to set the size of the memory mapping maintained by file
28340 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
28341 **
28342 ** If successful, this function sets the following variables:
28343 **
28344 **       unixFile.pMapRegion
28345 **       unixFile.mmapSize
28346 **       unixFile.mmapSizeActual
28347 **
28348 ** If unsuccessful, an error message is logged via sqlite3_log() and
28349 ** the three variables above are zeroed. In this case SQLite should
28350 ** continue accessing the database using the xRead() and xWrite()
28351 ** methods.
28352 */
28353 static void unixRemapfile(
28354   unixFile *pFd,                  /* File descriptor object */
28355   i64 nNew                        /* Required mapping size */
28356 ){
28357   const char *zErr = "mmap";
28358   int h = pFd->h;                      /* File descriptor open on db file */
28359   u8 *pOrig = (u8 *)pFd->pMapRegion;   /* Pointer to current file mapping */
28360   i64 nOrig = pFd->mmapSizeActual;     /* Size of pOrig region in bytes */
28361   u8 *pNew = 0;                        /* Location of new mapping */
28362   int flags = PROT_READ;               /* Flags to pass to mmap() */
28363 
28364   assert( pFd->nFetchOut==0 );
28365   assert( nNew>pFd->mmapSize );
28366   assert( nNew<=pFd->mmapSizeMax );
28367   assert( nNew>0 );
28368   assert( pFd->mmapSizeActual>=pFd->mmapSize );
28369   assert( MAP_FAILED!=0 );
28370 
28371   if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
28372 
28373   if( pOrig ){
28374     const int szSyspage = unixGetPagesize();
28375     i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
28376     u8 *pReq = &pOrig[nReuse];
28377 
28378     /* Unmap any pages of the existing mapping that cannot be reused. */
28379     if( nReuse!=nOrig ){
28380       osMunmap(pReq, nOrig-nReuse);
28381     }
28382 
28383 #if HAVE_MREMAP
28384     pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
28385     zErr = "mremap";
28386 #else
28387     pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
28388     if( pNew!=MAP_FAILED ){
28389       if( pNew!=pReq ){
28390         osMunmap(pNew, nNew - nReuse);
28391         pNew = 0;
28392       }else{
28393         pNew = pOrig;
28394       }
28395     }
28396 #endif
28397 
28398     /* The attempt to extend the existing mapping failed. Free it. */
28399     if( pNew==MAP_FAILED || pNew==0 ){
28400       osMunmap(pOrig, nReuse);
28401     }
28402   }
28403 
28404   /* If pNew is still NULL, try to create an entirely new mapping. */
28405   if( pNew==0 ){
28406     pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
28407   }
28408 
28409   if( pNew==MAP_FAILED ){
28410     pNew = 0;
28411     nNew = 0;
28412     unixLogError(SQLITE_OK, zErr, pFd->zPath);
28413 
28414     /* If the mmap() above failed, assume that all subsequent mmap() calls
28415     ** will probably fail too. Fall back to using xRead/xWrite exclusively
28416     ** in this case.  */
28417     pFd->mmapSizeMax = 0;
28418   }
28419   pFd->pMapRegion = (void *)pNew;
28420   pFd->mmapSize = pFd->mmapSizeActual = nNew;
28421 }
28422 
28423 /*
28424 ** Memory map or remap the file opened by file-descriptor pFd (if the file
28425 ** is already mapped, the existing mapping is replaced by the new). Or, if
28426 ** there already exists a mapping for this file, and there are still
28427 ** outstanding xFetch() references to it, this function is a no-op.
28428 **
28429 ** If parameter nByte is non-negative, then it is the requested size of
28430 ** the mapping to create. Otherwise, if nByte is less than zero, then the
28431 ** requested size is the size of the file on disk. The actual size of the
28432 ** created mapping is either the requested size or the value configured
28433 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
28434 **
28435 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
28436 ** recreated as a result of outstanding references) or an SQLite error
28437 ** code otherwise.
28438 */
28439 static int unixMapfile(unixFile *pFd, i64 nByte){
28440   i64 nMap = nByte;
28441   int rc;
28442 
28443   assert( nMap>=0 || pFd->nFetchOut==0 );
28444   if( pFd->nFetchOut>0 ) return SQLITE_OK;
28445 
28446   if( nMap<0 ){
28447     struct stat statbuf;          /* Low-level file information */
28448     rc = osFstat(pFd->h, &statbuf);
28449     if( rc!=SQLITE_OK ){
28450       return SQLITE_IOERR_FSTAT;
28451     }
28452     nMap = statbuf.st_size;
28453   }
28454   if( nMap>pFd->mmapSizeMax ){
28455     nMap = pFd->mmapSizeMax;
28456   }
28457 
28458   if( nMap!=pFd->mmapSize ){
28459     if( nMap>0 ){
28460       unixRemapfile(pFd, nMap);
28461     }else{
28462       unixUnmapfile(pFd);
28463     }
28464   }
28465 
28466   return SQLITE_OK;
28467 }
28468 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
28469 
28470 /*
28471 ** If possible, return a pointer to a mapping of file fd starting at offset
28472 ** iOff. The mapping must be valid for at least nAmt bytes.
28473 **
28474 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
28475 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
28476 ** Finally, if an error does occur, return an SQLite error code. The final
28477 ** value of *pp is undefined in this case.
28478 **
28479 ** If this function does return a pointer, the caller must eventually
28480 ** release the reference by calling unixUnfetch().
28481 */
28482 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
28483 #if SQLITE_MAX_MMAP_SIZE>0
28484   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
28485 #endif
28486   *pp = 0;
28487 
28488 #if SQLITE_MAX_MMAP_SIZE>0
28489   if( pFd->mmapSizeMax>0 ){
28490     if( pFd->pMapRegion==0 ){
28491       int rc = unixMapfile(pFd, -1);
28492       if( rc!=SQLITE_OK ) return rc;
28493     }
28494     if( pFd->mmapSize >= iOff+nAmt ){
28495       *pp = &((u8 *)pFd->pMapRegion)[iOff];
28496       pFd->nFetchOut++;
28497     }
28498   }
28499 #endif
28500   return SQLITE_OK;
28501 }
28502 
28503 /*
28504 ** If the third argument is non-NULL, then this function releases a
28505 ** reference obtained by an earlier call to unixFetch(). The second
28506 ** argument passed to this function must be the same as the corresponding
28507 ** argument that was passed to the unixFetch() invocation.
28508 **
28509 ** Or, if the third argument is NULL, then this function is being called
28510 ** to inform the VFS layer that, according to POSIX, any existing mapping
28511 ** may now be invalid and should be unmapped.
28512 */
28513 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
28514 #if SQLITE_MAX_MMAP_SIZE>0
28515   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
28516   UNUSED_PARAMETER(iOff);
28517 
28518   /* If p==0 (unmap the entire file) then there must be no outstanding
28519   ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
28520   ** then there must be at least one outstanding.  */
28521   assert( (p==0)==(pFd->nFetchOut==0) );
28522 
28523   /* If p!=0, it must match the iOff value. */
28524   assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
28525 
28526   if( p ){
28527     pFd->nFetchOut--;
28528   }else{
28529     unixUnmapfile(pFd);
28530   }
28531 
28532   assert( pFd->nFetchOut>=0 );
28533 #else
28534   UNUSED_PARAMETER(fd);
28535   UNUSED_PARAMETER(p);
28536   UNUSED_PARAMETER(iOff);
28537 #endif
28538   return SQLITE_OK;
28539 }
28540 
28541 /*
28542 ** Here ends the implementation of all sqlite3_file methods.
28543 **
28544 ********************** End sqlite3_file Methods *******************************
28545 ******************************************************************************/
28546 
28547 /*
28548 ** This division contains definitions of sqlite3_io_methods objects that
28549 ** implement various file locking strategies.  It also contains definitions
28550 ** of "finder" functions.  A finder-function is used to locate the appropriate
28551 ** sqlite3_io_methods object for a particular database file.  The pAppData
28552 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
28553 ** the correct finder-function for that VFS.
28554 **
28555 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
28556 ** object.  The only interesting finder-function is autolockIoFinder, which
28557 ** looks at the filesystem type and tries to guess the best locking
28558 ** strategy from that.
28559 **
28560 ** For finder-funtion F, two objects are created:
28561 **
28562 **    (1) The real finder-function named "FImpt()".
28563 **
28564 **    (2) A constant pointer to this function named just "F".
28565 **
28566 **
28567 ** A pointer to the F pointer is used as the pAppData value for VFS
28568 ** objects.  We have to do this instead of letting pAppData point
28569 ** directly at the finder-function since C90 rules prevent a void*
28570 ** from be cast into a function pointer.
28571 **
28572 **
28573 ** Each instance of this macro generates two objects:
28574 **
28575 **   *  A constant sqlite3_io_methods object call METHOD that has locking
28576 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
28577 **
28578 **   *  An I/O method finder function called FINDER that returns a pointer
28579 **      to the METHOD object in the previous bullet.
28580 */
28581 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
28582 static const sqlite3_io_methods METHOD = {                                   \
28583    VERSION,                    /* iVersion */                                \
28584    CLOSE,                      /* xClose */                                  \
28585    unixRead,                   /* xRead */                                   \
28586    unixWrite,                  /* xWrite */                                  \
28587    unixTruncate,               /* xTruncate */                               \
28588    unixSync,                   /* xSync */                                   \
28589    unixFileSize,               /* xFileSize */                               \
28590    LOCK,                       /* xLock */                                   \
28591    UNLOCK,                     /* xUnlock */                                 \
28592    CKLOCK,                     /* xCheckReservedLock */                      \
28593    unixFileControl,            /* xFileControl */                            \
28594    unixSectorSize,             /* xSectorSize */                             \
28595    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
28596    unixShmMap,                 /* xShmMap */                                 \
28597    unixShmLock,                /* xShmLock */                                \
28598    unixShmBarrier,             /* xShmBarrier */                             \
28599    unixShmUnmap,               /* xShmUnmap */                               \
28600    unixFetch,                  /* xFetch */                                  \
28601    unixUnfetch,                /* xUnfetch */                                \
28602 };                                                                           \
28603 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
28604   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
28605   return &METHOD;                                                            \
28606 }                                                                            \
28607 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
28608     = FINDER##Impl;
28609 
28610 /*
28611 ** Here are all of the sqlite3_io_methods objects for each of the
28612 ** locking strategies.  Functions that return pointers to these methods
28613 ** are also created.
28614 */
28615 IOMETHODS(
28616   posixIoFinder,            /* Finder function name */
28617   posixIoMethods,           /* sqlite3_io_methods object name */
28618   3,                        /* shared memory and mmap are enabled */
28619   unixClose,                /* xClose method */
28620   unixLock,                 /* xLock method */
28621   unixUnlock,               /* xUnlock method */
28622   unixCheckReservedLock     /* xCheckReservedLock method */
28623 )
28624 IOMETHODS(
28625   nolockIoFinder,           /* Finder function name */
28626   nolockIoMethods,          /* sqlite3_io_methods object name */
28627   1,                        /* shared memory is disabled */
28628   nolockClose,              /* xClose method */
28629   nolockLock,               /* xLock method */
28630   nolockUnlock,             /* xUnlock method */
28631   nolockCheckReservedLock   /* xCheckReservedLock method */
28632 )
28633 IOMETHODS(
28634   dotlockIoFinder,          /* Finder function name */
28635   dotlockIoMethods,         /* sqlite3_io_methods object name */
28636   1,                        /* shared memory is disabled */
28637   dotlockClose,             /* xClose method */
28638   dotlockLock,              /* xLock method */
28639   dotlockUnlock,            /* xUnlock method */
28640   dotlockCheckReservedLock  /* xCheckReservedLock method */
28641 )
28642 
28643 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
28644 IOMETHODS(
28645   flockIoFinder,            /* Finder function name */
28646   flockIoMethods,           /* sqlite3_io_methods object name */
28647   1,                        /* shared memory is disabled */
28648   flockClose,               /* xClose method */
28649   flockLock,                /* xLock method */
28650   flockUnlock,              /* xUnlock method */
28651   flockCheckReservedLock    /* xCheckReservedLock method */
28652 )
28653 #endif
28654 
28655 #if OS_VXWORKS
28656 IOMETHODS(
28657   semIoFinder,              /* Finder function name */
28658   semIoMethods,             /* sqlite3_io_methods object name */
28659   1,                        /* shared memory is disabled */
28660   semClose,                 /* xClose method */
28661   semLock,                  /* xLock method */
28662   semUnlock,                /* xUnlock method */
28663   semCheckReservedLock      /* xCheckReservedLock method */
28664 )
28665 #endif
28666 
28667 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28668 IOMETHODS(
28669   afpIoFinder,              /* Finder function name */
28670   afpIoMethods,             /* sqlite3_io_methods object name */
28671   1,                        /* shared memory is disabled */
28672   afpClose,                 /* xClose method */
28673   afpLock,                  /* xLock method */
28674   afpUnlock,                /* xUnlock method */
28675   afpCheckReservedLock      /* xCheckReservedLock method */
28676 )
28677 #endif
28678 
28679 /*
28680 ** The proxy locking method is a "super-method" in the sense that it
28681 ** opens secondary file descriptors for the conch and lock files and
28682 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
28683 ** secondary files.  For this reason, the division that implements
28684 ** proxy locking is located much further down in the file.  But we need
28685 ** to go ahead and define the sqlite3_io_methods and finder function
28686 ** for proxy locking here.  So we forward declare the I/O methods.
28687 */
28688 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28689 static int proxyClose(sqlite3_file*);
28690 static int proxyLock(sqlite3_file*, int);
28691 static int proxyUnlock(sqlite3_file*, int);
28692 static int proxyCheckReservedLock(sqlite3_file*, int*);
28693 IOMETHODS(
28694   proxyIoFinder,            /* Finder function name */
28695   proxyIoMethods,           /* sqlite3_io_methods object name */
28696   1,                        /* shared memory is disabled */
28697   proxyClose,               /* xClose method */
28698   proxyLock,                /* xLock method */
28699   proxyUnlock,              /* xUnlock method */
28700   proxyCheckReservedLock    /* xCheckReservedLock method */
28701 )
28702 #endif
28703 
28704 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
28705 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28706 IOMETHODS(
28707   nfsIoFinder,               /* Finder function name */
28708   nfsIoMethods,              /* sqlite3_io_methods object name */
28709   1,                         /* shared memory is disabled */
28710   unixClose,                 /* xClose method */
28711   unixLock,                  /* xLock method */
28712   nfsUnlock,                 /* xUnlock method */
28713   unixCheckReservedLock      /* xCheckReservedLock method */
28714 )
28715 #endif
28716 
28717 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28718 /*
28719 ** This "finder" function attempts to determine the best locking strategy
28720 ** for the database file "filePath".  It then returns the sqlite3_io_methods
28721 ** object that implements that strategy.
28722 **
28723 ** This is for MacOSX only.
28724 */
28725 static const sqlite3_io_methods *autolockIoFinderImpl(
28726   const char *filePath,    /* name of the database file */
28727   unixFile *pNew           /* open file object for the database file */
28728 ){
28729   static const struct Mapping {
28730     const char *zFilesystem;              /* Filesystem type name */
28731     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
28732   } aMap[] = {
28733     { "hfs",    &posixIoMethods },
28734     { "ufs",    &posixIoMethods },
28735     { "afpfs",  &afpIoMethods },
28736     { "smbfs",  &afpIoMethods },
28737     { "webdav", &nolockIoMethods },
28738     { 0, 0 }
28739   };
28740   int i;
28741   struct statfs fsInfo;
28742   struct flock lockInfo;
28743 
28744   if( !filePath ){
28745     /* If filePath==NULL that means we are dealing with a transient file
28746     ** that does not need to be locked. */
28747     return &nolockIoMethods;
28748   }
28749   if( statfs(filePath, &fsInfo) != -1 ){
28750     if( fsInfo.f_flags & MNT_RDONLY ){
28751       return &nolockIoMethods;
28752     }
28753     for(i=0; aMap[i].zFilesystem; i++){
28754       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
28755         return aMap[i].pMethods;
28756       }
28757     }
28758   }
28759 
28760   /* Default case. Handles, amongst others, "nfs".
28761   ** Test byte-range lock using fcntl(). If the call succeeds,
28762   ** assume that the file-system supports POSIX style locks.
28763   */
28764   lockInfo.l_len = 1;
28765   lockInfo.l_start = 0;
28766   lockInfo.l_whence = SEEK_SET;
28767   lockInfo.l_type = F_RDLCK;
28768   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28769     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
28770       return &nfsIoMethods;
28771     } else {
28772       return &posixIoMethods;
28773     }
28774   }else{
28775     return &dotlockIoMethods;
28776   }
28777 }
28778 static const sqlite3_io_methods
28779   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28780 
28781 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
28782 
28783 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
28784 /*
28785 ** This "finder" function attempts to determine the best locking strategy
28786 ** for the database file "filePath".  It then returns the sqlite3_io_methods
28787 ** object that implements that strategy.
28788 **
28789 ** This is for VXWorks only.
28790 */
28791 static const sqlite3_io_methods *autolockIoFinderImpl(
28792   const char *filePath,    /* name of the database file */
28793   unixFile *pNew           /* the open file object */
28794 ){
28795   struct flock lockInfo;
28796 
28797   if( !filePath ){
28798     /* If filePath==NULL that means we are dealing with a transient file
28799     ** that does not need to be locked. */
28800     return &nolockIoMethods;
28801   }
28802 
28803   /* Test if fcntl() is supported and use POSIX style locks.
28804   ** Otherwise fall back to the named semaphore method.
28805   */
28806   lockInfo.l_len = 1;
28807   lockInfo.l_start = 0;
28808   lockInfo.l_whence = SEEK_SET;
28809   lockInfo.l_type = F_RDLCK;
28810   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28811     return &posixIoMethods;
28812   }else{
28813     return &semIoMethods;
28814   }
28815 }
28816 static const sqlite3_io_methods
28817   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28818 
28819 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
28820 
28821 /*
28822 ** An abstract type for a pointer to a IO method finder function:
28823 */
28824 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
28825 
28826 
28827 /****************************************************************************
28828 **************************** sqlite3_vfs methods ****************************
28829 **
28830 ** This division contains the implementation of methods on the
28831 ** sqlite3_vfs object.
28832 */
28833 
28834 /*
28835 ** Initialize the contents of the unixFile structure pointed to by pId.
28836 */
28837 static int fillInUnixFile(
28838   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
28839   int h,                  /* Open file descriptor of file being opened */
28840   sqlite3_file *pId,      /* Write to the unixFile structure here */
28841   const char *zFilename,  /* Name of the file being opened */
28842   int ctrlFlags           /* Zero or more UNIXFILE_* values */
28843 ){
28844   const sqlite3_io_methods *pLockingStyle;
28845   unixFile *pNew = (unixFile *)pId;
28846   int rc = SQLITE_OK;
28847 
28848   assert( pNew->pInode==NULL );
28849 
28850   /* Usually the path zFilename should not be a relative pathname. The
28851   ** exception is when opening the proxy "conch" file in builds that
28852   ** include the special Apple locking styles.
28853   */
28854 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28855   assert( zFilename==0 || zFilename[0]=='/'
28856     || pVfs->pAppData==(void*)&autolockIoFinder );
28857 #else
28858   assert( zFilename==0 || zFilename[0]=='/' );
28859 #endif
28860 
28861   /* No locking occurs in temporary files */
28862   assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
28863 
28864   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
28865   pNew->h = h;
28866   pNew->pVfs = pVfs;
28867   pNew->zPath = zFilename;
28868   pNew->ctrlFlags = (u8)ctrlFlags;
28869 #if SQLITE_MAX_MMAP_SIZE>0
28870   pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
28871 #endif
28872   if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
28873                            "psow", SQLITE_POWERSAFE_OVERWRITE) ){
28874     pNew->ctrlFlags |= UNIXFILE_PSOW;
28875   }
28876   if( strcmp(pVfs->zName,"unix-excl")==0 ){
28877     pNew->ctrlFlags |= UNIXFILE_EXCL;
28878   }
28879 
28880 #if OS_VXWORKS
28881   pNew->pId = vxworksFindFileId(zFilename);
28882   if( pNew->pId==0 ){
28883     ctrlFlags |= UNIXFILE_NOLOCK;
28884     rc = SQLITE_NOMEM;
28885   }
28886 #endif
28887 
28888   if( ctrlFlags & UNIXFILE_NOLOCK ){
28889     pLockingStyle = &nolockIoMethods;
28890   }else{
28891     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
28892 #if SQLITE_ENABLE_LOCKING_STYLE
28893     /* Cache zFilename in the locking context (AFP and dotlock override) for
28894     ** proxyLock activation is possible (remote proxy is based on db name)
28895     ** zFilename remains valid until file is closed, to support */
28896     pNew->lockingContext = (void*)zFilename;
28897 #endif
28898   }
28899 
28900   if( pLockingStyle == &posixIoMethods
28901 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28902     || pLockingStyle == &nfsIoMethods
28903 #endif
28904   ){
28905     unixEnterMutex();
28906     rc = findInodeInfo(pNew, &pNew->pInode);
28907     if( rc!=SQLITE_OK ){
28908       /* If an error occurred in findInodeInfo(), close the file descriptor
28909       ** immediately, before releasing the mutex. findInodeInfo() may fail
28910       ** in two scenarios:
28911       **
28912       **   (a) A call to fstat() failed.
28913       **   (b) A malloc failed.
28914       **
28915       ** Scenario (b) may only occur if the process is holding no other
28916       ** file descriptors open on the same file. If there were other file
28917       ** descriptors on this file, then no malloc would be required by
28918       ** findInodeInfo(). If this is the case, it is quite safe to close
28919       ** handle h - as it is guaranteed that no posix locks will be released
28920       ** by doing so.
28921       **
28922       ** If scenario (a) caused the error then things are not so safe. The
28923       ** implicit assumption here is that if fstat() fails, things are in
28924       ** such bad shape that dropping a lock or two doesn't matter much.
28925       */
28926       robust_close(pNew, h, __LINE__);
28927       h = -1;
28928     }
28929     unixLeaveMutex();
28930   }
28931 
28932 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
28933   else if( pLockingStyle == &afpIoMethods ){
28934     /* AFP locking uses the file path so it needs to be included in
28935     ** the afpLockingContext.
28936     */
28937     afpLockingContext *pCtx;
28938     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
28939     if( pCtx==0 ){
28940       rc = SQLITE_NOMEM;
28941     }else{
28942       /* NB: zFilename exists and remains valid until the file is closed
28943       ** according to requirement F11141.  So we do not need to make a
28944       ** copy of the filename. */
28945       pCtx->dbPath = zFilename;
28946       pCtx->reserved = 0;
28947       srandomdev();
28948       unixEnterMutex();
28949       rc = findInodeInfo(pNew, &pNew->pInode);
28950       if( rc!=SQLITE_OK ){
28951         sqlite3_free(pNew->lockingContext);
28952         robust_close(pNew, h, __LINE__);
28953         h = -1;
28954       }
28955       unixLeaveMutex();
28956     }
28957   }
28958 #endif
28959 
28960   else if( pLockingStyle == &dotlockIoMethods ){
28961     /* Dotfile locking uses the file path so it needs to be included in
28962     ** the dotlockLockingContext
28963     */
28964     char *zLockFile;
28965     int nFilename;
28966     assert( zFilename!=0 );
28967     nFilename = (int)strlen(zFilename) + 6;
28968     zLockFile = (char *)sqlite3_malloc(nFilename);
28969     if( zLockFile==0 ){
28970       rc = SQLITE_NOMEM;
28971     }else{
28972       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
28973     }
28974     pNew->lockingContext = zLockFile;
28975   }
28976 
28977 #if OS_VXWORKS
28978   else if( pLockingStyle == &semIoMethods ){
28979     /* Named semaphore locking uses the file path so it needs to be
28980     ** included in the semLockingContext
28981     */
28982     unixEnterMutex();
28983     rc = findInodeInfo(pNew, &pNew->pInode);
28984     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
28985       char *zSemName = pNew->pInode->aSemName;
28986       int n;
28987       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
28988                        pNew->pId->zCanonicalName);
28989       for( n=1; zSemName[n]; n++ )
28990         if( zSemName[n]=='/' ) zSemName[n] = '_';
28991       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
28992       if( pNew->pInode->pSem == SEM_FAILED ){
28993         rc = SQLITE_NOMEM;
28994         pNew->pInode->aSemName[0] = '\0';
28995       }
28996     }
28997     unixLeaveMutex();
28998   }
28999 #endif
29000 
29001   pNew->lastErrno = 0;
29002 #if OS_VXWORKS
29003   if( rc!=SQLITE_OK ){
29004     if( h>=0 ) robust_close(pNew, h, __LINE__);
29005     h = -1;
29006     osUnlink(zFilename);
29007     pNew->ctrlFlags |= UNIXFILE_DELETE;
29008   }
29009 #endif
29010   if( rc!=SQLITE_OK ){
29011     if( h>=0 ) robust_close(pNew, h, __LINE__);
29012   }else{
29013     pNew->pMethod = pLockingStyle;
29014     OpenCounter(+1);
29015     verifyDbFile(pNew);
29016   }
29017   return rc;
29018 }
29019 
29020 /*
29021 ** Return the name of a directory in which to put temporary files.
29022 ** If no suitable temporary file directory can be found, return NULL.
29023 */
29024 static const char *unixTempFileDir(void){
29025   static const char *azDirs[] = {
29026      0,
29027      0,
29028      0,
29029      "/var/tmp",
29030      "/usr/tmp",
29031      "/tmp",
29032      0        /* List terminator */
29033   };
29034   unsigned int i;
29035   struct stat buf;
29036   const char *zDir = 0;
29037 
29038   azDirs[0] = sqlite3_temp_directory;
29039   if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
29040   if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
29041   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
29042     if( zDir==0 ) continue;
29043     if( osStat(zDir, &buf) ) continue;
29044     if( !S_ISDIR(buf.st_mode) ) continue;
29045     if( osAccess(zDir, 07) ) continue;
29046     break;
29047   }
29048   return zDir;
29049 }
29050 
29051 /*
29052 ** Create a temporary file name in zBuf.  zBuf must be allocated
29053 ** by the calling process and must be big enough to hold at least
29054 ** pVfs->mxPathname bytes.
29055 */
29056 static int unixGetTempname(int nBuf, char *zBuf){
29057   static const unsigned char zChars[] =
29058     "abcdefghijklmnopqrstuvwxyz"
29059     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
29060     "0123456789";
29061   unsigned int i, j;
29062   const char *zDir;
29063 
29064   /* It's odd to simulate an io-error here, but really this is just
29065   ** using the io-error infrastructure to test that SQLite handles this
29066   ** function failing.
29067   */
29068   SimulateIOError( return SQLITE_IOERR );
29069 
29070   zDir = unixTempFileDir();
29071   if( zDir==0 ) zDir = ".";
29072 
29073   /* Check that the output buffer is large enough for the temporary file
29074   ** name. If it is not, return SQLITE_ERROR.
29075   */
29076   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
29077     return SQLITE_ERROR;
29078   }
29079 
29080   do{
29081     sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
29082     j = (int)strlen(zBuf);
29083     sqlite3_randomness(15, &zBuf[j]);
29084     for(i=0; i<15; i++, j++){
29085       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
29086     }
29087     zBuf[j] = 0;
29088     zBuf[j+1] = 0;
29089   }while( osAccess(zBuf,0)==0 );
29090   return SQLITE_OK;
29091 }
29092 
29093 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29094 /*
29095 ** Routine to transform a unixFile into a proxy-locking unixFile.
29096 ** Implementation in the proxy-lock division, but used by unixOpen()
29097 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
29098 */
29099 static int proxyTransformUnixFile(unixFile*, const char*);
29100 #endif
29101 
29102 /*
29103 ** Search for an unused file descriptor that was opened on the database
29104 ** file (not a journal or master-journal file) identified by pathname
29105 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
29106 ** argument to this function.
29107 **
29108 ** Such a file descriptor may exist if a database connection was closed
29109 ** but the associated file descriptor could not be closed because some
29110 ** other file descriptor open on the same file is holding a file-lock.
29111 ** Refer to comments in the unixClose() function and the lengthy comment
29112 ** describing "Posix Advisory Locking" at the start of this file for
29113 ** further details. Also, ticket #4018.
29114 **
29115 ** If a suitable file descriptor is found, then it is returned. If no
29116 ** such file descriptor is located, -1 is returned.
29117 */
29118 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
29119   UnixUnusedFd *pUnused = 0;
29120 
29121   /* Do not search for an unused file descriptor on vxworks. Not because
29122   ** vxworks would not benefit from the change (it might, we're not sure),
29123   ** but because no way to test it is currently available. It is better
29124   ** not to risk breaking vxworks support for the sake of such an obscure
29125   ** feature.  */
29126 #if !OS_VXWORKS
29127   struct stat sStat;                   /* Results of stat() call */
29128 
29129   /* A stat() call may fail for various reasons. If this happens, it is
29130   ** almost certain that an open() call on the same path will also fail.
29131   ** For this reason, if an error occurs in the stat() call here, it is
29132   ** ignored and -1 is returned. The caller will try to open a new file
29133   ** descriptor on the same path, fail, and return an error to SQLite.
29134   **
29135   ** Even if a subsequent open() call does succeed, the consequences of
29136   ** not searching for a resusable file descriptor are not dire.  */
29137   if( 0==osStat(zPath, &sStat) ){
29138     unixInodeInfo *pInode;
29139 
29140     unixEnterMutex();
29141     pInode = inodeList;
29142     while( pInode && (pInode->fileId.dev!=sStat.st_dev
29143                      || pInode->fileId.ino!=sStat.st_ino) ){
29144        pInode = pInode->pNext;
29145     }
29146     if( pInode ){
29147       UnixUnusedFd **pp;
29148       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
29149       pUnused = *pp;
29150       if( pUnused ){
29151         *pp = pUnused->pNext;
29152       }
29153     }
29154     unixLeaveMutex();
29155   }
29156 #endif    /* if !OS_VXWORKS */
29157   return pUnused;
29158 }
29159 
29160 /*
29161 ** This function is called by unixOpen() to determine the unix permissions
29162 ** to create new files with. If no error occurs, then SQLITE_OK is returned
29163 ** and a value suitable for passing as the third argument to open(2) is
29164 ** written to *pMode. If an IO error occurs, an SQLite error code is
29165 ** returned and the value of *pMode is not modified.
29166 **
29167 ** In most cases cases, this routine sets *pMode to 0, which will become
29168 ** an indication to robust_open() to create the file using
29169 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
29170 ** But if the file being opened is a WAL or regular journal file, then
29171 ** this function queries the file-system for the permissions on the
29172 ** corresponding database file and sets *pMode to this value. Whenever
29173 ** possible, WAL and journal files are created using the same permissions
29174 ** as the associated database file.
29175 **
29176 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
29177 ** original filename is unavailable.  But 8_3_NAMES is only used for
29178 ** FAT filesystems and permissions do not matter there, so just use
29179 ** the default permissions.
29180 */
29181 static int findCreateFileMode(
29182   const char *zPath,              /* Path of file (possibly) being created */
29183   int flags,                      /* Flags passed as 4th argument to xOpen() */
29184   mode_t *pMode,                  /* OUT: Permissions to open file with */
29185   uid_t *pUid,                    /* OUT: uid to set on the file */
29186   gid_t *pGid                     /* OUT: gid to set on the file */
29187 ){
29188   int rc = SQLITE_OK;             /* Return Code */
29189   *pMode = 0;
29190   *pUid = 0;
29191   *pGid = 0;
29192   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
29193     char zDb[MAX_PATHNAME+1];     /* Database file path */
29194     int nDb;                      /* Number of valid bytes in zDb */
29195     struct stat sStat;            /* Output of stat() on database file */
29196 
29197     /* zPath is a path to a WAL or journal file. The following block derives
29198     ** the path to the associated database file from zPath. This block handles
29199     ** the following naming conventions:
29200     **
29201     **   "<path to db>-journal"
29202     **   "<path to db>-wal"
29203     **   "<path to db>-journalNN"
29204     **   "<path to db>-walNN"
29205     **
29206     ** where NN is a decimal number. The NN naming schemes are
29207     ** used by the test_multiplex.c module.
29208     */
29209     nDb = sqlite3Strlen30(zPath) - 1;
29210 #ifdef SQLITE_ENABLE_8_3_NAMES
29211     while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
29212     if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
29213 #else
29214     while( zPath[nDb]!='-' ){
29215       assert( nDb>0 );
29216       assert( zPath[nDb]!='\n' );
29217       nDb--;
29218     }
29219 #endif
29220     memcpy(zDb, zPath, nDb);
29221     zDb[nDb] = '\0';
29222 
29223     if( 0==osStat(zDb, &sStat) ){
29224       *pMode = sStat.st_mode & 0777;
29225       *pUid = sStat.st_uid;
29226       *pGid = sStat.st_gid;
29227     }else{
29228       rc = SQLITE_IOERR_FSTAT;
29229     }
29230   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
29231     *pMode = 0600;
29232   }
29233   return rc;
29234 }
29235 
29236 /*
29237 ** Open the file zPath.
29238 **
29239 ** Previously, the SQLite OS layer used three functions in place of this
29240 ** one:
29241 **
29242 **     sqlite3OsOpenReadWrite();
29243 **     sqlite3OsOpenReadOnly();
29244 **     sqlite3OsOpenExclusive();
29245 **
29246 ** These calls correspond to the following combinations of flags:
29247 **
29248 **     ReadWrite() ->     (READWRITE | CREATE)
29249 **     ReadOnly()  ->     (READONLY)
29250 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
29251 **
29252 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
29253 ** true, the file was configured to be automatically deleted when the
29254 ** file handle closed. To achieve the same effect using this new
29255 ** interface, add the DELETEONCLOSE flag to those specified above for
29256 ** OpenExclusive().
29257 */
29258 static int unixOpen(
29259   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
29260   const char *zPath,           /* Pathname of file to be opened */
29261   sqlite3_file *pFile,         /* The file descriptor to be filled in */
29262   int flags,                   /* Input flags to control the opening */
29263   int *pOutFlags               /* Output flags returned to SQLite core */
29264 ){
29265   unixFile *p = (unixFile *)pFile;
29266   int fd = -1;                   /* File descriptor returned by open() */
29267   int openFlags = 0;             /* Flags to pass to open() */
29268   int eType = flags&0xFFFFFF00;  /* Type of file to open */
29269   int noLock;                    /* True to omit locking primitives */
29270   int rc = SQLITE_OK;            /* Function Return Code */
29271   int ctrlFlags = 0;             /* UNIXFILE_* flags */
29272 
29273   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
29274   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
29275   int isCreate     = (flags & SQLITE_OPEN_CREATE);
29276   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
29277   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
29278 #if SQLITE_ENABLE_LOCKING_STYLE
29279   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
29280 #endif
29281 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29282   struct statfs fsInfo;
29283 #endif
29284 
29285   /* If creating a master or main-file journal, this function will open
29286   ** a file-descriptor on the directory too. The first time unixSync()
29287   ** is called the directory file descriptor will be fsync()ed and close()d.
29288   */
29289   int syncDir = (isCreate && (
29290         eType==SQLITE_OPEN_MASTER_JOURNAL
29291      || eType==SQLITE_OPEN_MAIN_JOURNAL
29292      || eType==SQLITE_OPEN_WAL
29293   ));
29294 
29295   /* If argument zPath is a NULL pointer, this function is required to open
29296   ** a temporary file. Use this buffer to store the file name in.
29297   */
29298   char zTmpname[MAX_PATHNAME+2];
29299   const char *zName = zPath;
29300 
29301   /* Check the following statements are true:
29302   **
29303   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
29304   **   (b) if CREATE is set, then READWRITE must also be set, and
29305   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
29306   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
29307   */
29308   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
29309   assert(isCreate==0 || isReadWrite);
29310   assert(isExclusive==0 || isCreate);
29311   assert(isDelete==0 || isCreate);
29312 
29313   /* The main DB, main journal, WAL file and master journal are never
29314   ** automatically deleted. Nor are they ever temporary files.  */
29315   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
29316   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
29317   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
29318   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
29319 
29320   /* Assert that the upper layer has set one of the "file-type" flags. */
29321   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
29322        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
29323        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
29324        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
29325   );
29326 
29327   /* Detect a pid change and reset the PRNG.  There is a race condition
29328   ** here such that two or more threads all trying to open databases at
29329   ** the same instant might all reset the PRNG.  But multiple resets
29330   ** are harmless.
29331   */
29332   if( randomnessPid!=getpid() ){
29333     randomnessPid = getpid();
29334     sqlite3_randomness(0,0);
29335   }
29336 
29337   memset(p, 0, sizeof(unixFile));
29338 
29339   if( eType==SQLITE_OPEN_MAIN_DB ){
29340     UnixUnusedFd *pUnused;
29341     pUnused = findReusableFd(zName, flags);
29342     if( pUnused ){
29343       fd = pUnused->fd;
29344     }else{
29345       pUnused = sqlite3_malloc(sizeof(*pUnused));
29346       if( !pUnused ){
29347         return SQLITE_NOMEM;
29348       }
29349     }
29350     p->pUnused = pUnused;
29351 
29352     /* Database filenames are double-zero terminated if they are not
29353     ** URIs with parameters.  Hence, they can always be passed into
29354     ** sqlite3_uri_parameter(). */
29355     assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
29356 
29357   }else if( !zName ){
29358     /* If zName is NULL, the upper layer is requesting a temp file. */
29359     assert(isDelete && !syncDir);
29360     rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
29361     if( rc!=SQLITE_OK ){
29362       return rc;
29363     }
29364     zName = zTmpname;
29365 
29366     /* Generated temporary filenames are always double-zero terminated
29367     ** for use by sqlite3_uri_parameter(). */
29368     assert( zName[strlen(zName)+1]==0 );
29369   }
29370 
29371   /* Determine the value of the flags parameter passed to POSIX function
29372   ** open(). These must be calculated even if open() is not called, as
29373   ** they may be stored as part of the file handle and used by the
29374   ** 'conch file' locking functions later on.  */
29375   if( isReadonly )  openFlags |= O_RDONLY;
29376   if( isReadWrite ) openFlags |= O_RDWR;
29377   if( isCreate )    openFlags |= O_CREAT;
29378   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
29379   openFlags |= (O_LARGEFILE|O_BINARY);
29380 
29381   if( fd<0 ){
29382     mode_t openMode;              /* Permissions to create file with */
29383     uid_t uid;                    /* Userid for the file */
29384     gid_t gid;                    /* Groupid for the file */
29385     rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
29386     if( rc!=SQLITE_OK ){
29387       assert( !p->pUnused );
29388       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
29389       return rc;
29390     }
29391     fd = robust_open(zName, openFlags, openMode);
29392     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
29393     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
29394       /* Failed to open the file for read/write access. Try read-only. */
29395       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
29396       openFlags &= ~(O_RDWR|O_CREAT);
29397       flags |= SQLITE_OPEN_READONLY;
29398       openFlags |= O_RDONLY;
29399       isReadonly = 1;
29400       fd = robust_open(zName, openFlags, openMode);
29401     }
29402     if( fd<0 ){
29403       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
29404       goto open_finished;
29405     }
29406 
29407     /* If this process is running as root and if creating a new rollback
29408     ** journal or WAL file, set the ownership of the journal or WAL to be
29409     ** the same as the original database.
29410     */
29411     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
29412       osFchown(fd, uid, gid);
29413     }
29414   }
29415   assert( fd>=0 );
29416   if( pOutFlags ){
29417     *pOutFlags = flags;
29418   }
29419 
29420   if( p->pUnused ){
29421     p->pUnused->fd = fd;
29422     p->pUnused->flags = flags;
29423   }
29424 
29425   if( isDelete ){
29426 #if OS_VXWORKS
29427     zPath = zName;
29428 #else
29429     osUnlink(zName);
29430 #endif
29431   }
29432 #if SQLITE_ENABLE_LOCKING_STYLE
29433   else{
29434     p->openFlags = openFlags;
29435   }
29436 #endif
29437 
29438   noLock = eType!=SQLITE_OPEN_MAIN_DB;
29439 
29440 
29441 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29442   if( fstatfs(fd, &fsInfo) == -1 ){
29443     ((unixFile*)pFile)->lastErrno = errno;
29444     robust_close(p, fd, __LINE__);
29445     return SQLITE_IOERR_ACCESS;
29446   }
29447   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29448     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
29449   }
29450 #endif
29451 
29452   /* Set up appropriate ctrlFlags */
29453   if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
29454   if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
29455   if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
29456   if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
29457   if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
29458 
29459 #if SQLITE_ENABLE_LOCKING_STYLE
29460 #if SQLITE_PREFER_PROXY_LOCKING
29461   isAutoProxy = 1;
29462 #endif
29463   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
29464     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
29465     int useProxy = 0;
29466 
29467     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29468     ** never use proxy, NULL means use proxy for non-local files only.  */
29469     if( envforce!=NULL ){
29470       useProxy = atoi(envforce)>0;
29471     }else{
29472       if( statfs(zPath, &fsInfo) == -1 ){
29473         /* In theory, the close(fd) call is sub-optimal. If the file opened
29474         ** with fd is a database file, and there are other connections open
29475         ** on that file that are currently holding advisory locks on it,
29476         ** then the call to close() will cancel those locks. In practice,
29477         ** we're assuming that statfs() doesn't fail very often. At least
29478         ** not while other file descriptors opened by the same process on
29479         ** the same file are working.  */
29480         p->lastErrno = errno;
29481         robust_close(p, fd, __LINE__);
29482         rc = SQLITE_IOERR_ACCESS;
29483         goto open_finished;
29484       }
29485       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29486     }
29487     if( useProxy ){
29488       rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
29489       if( rc==SQLITE_OK ){
29490         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29491         if( rc!=SQLITE_OK ){
29492           /* Use unixClose to clean up the resources added in fillInUnixFile
29493           ** and clear all the structure's references.  Specifically,
29494           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
29495           */
29496           unixClose(pFile);
29497           return rc;
29498         }
29499       }
29500       goto open_finished;
29501     }
29502   }
29503 #endif
29504 
29505   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
29506 
29507 open_finished:
29508   if( rc!=SQLITE_OK ){
29509     sqlite3_free(p->pUnused);
29510   }
29511   return rc;
29512 }
29513 
29514 
29515 /*
29516 ** Delete the file at zPath. If the dirSync argument is true, fsync()
29517 ** the directory after deleting the file.
29518 */
29519 static int unixDelete(
29520   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
29521   const char *zPath,        /* Name of file to be deleted */
29522   int dirSync               /* If true, fsync() directory after deleting file */
29523 ){
29524   int rc = SQLITE_OK;
29525   UNUSED_PARAMETER(NotUsed);
29526   SimulateIOError(return SQLITE_IOERR_DELETE);
29527   if( osUnlink(zPath)==(-1) ){
29528     if( errno==ENOENT ){
29529       rc = SQLITE_IOERR_DELETE_NOENT;
29530     }else{
29531       rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29532     }
29533     return rc;
29534   }
29535 #ifndef SQLITE_DISABLE_DIRSYNC
29536   if( (dirSync & 1)!=0 ){
29537     int fd;
29538     rc = osOpenDirectory(zPath, &fd);
29539     if( rc==SQLITE_OK ){
29540 #if OS_VXWORKS
29541       if( fsync(fd)==-1 )
29542 #else
29543       if( fsync(fd) )
29544 #endif
29545       {
29546         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29547       }
29548       robust_close(0, fd, __LINE__);
29549     }else if( rc==SQLITE_CANTOPEN ){
29550       rc = SQLITE_OK;
29551     }
29552   }
29553 #endif
29554   return rc;
29555 }
29556 
29557 /*
29558 ** Test the existence of or access permissions of file zPath. The
29559 ** test performed depends on the value of flags:
29560 **
29561 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
29562 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
29563 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
29564 **
29565 ** Otherwise return 0.
29566 */
29567 static int unixAccess(
29568   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
29569   const char *zPath,      /* Path of the file to examine */
29570   int flags,              /* What do we want to learn about the zPath file? */
29571   int *pResOut            /* Write result boolean here */
29572 ){
29573   int amode = 0;
29574   UNUSED_PARAMETER(NotUsed);
29575   SimulateIOError( return SQLITE_IOERR_ACCESS; );
29576   switch( flags ){
29577     case SQLITE_ACCESS_EXISTS:
29578       amode = F_OK;
29579       break;
29580     case SQLITE_ACCESS_READWRITE:
29581       amode = W_OK|R_OK;
29582       break;
29583     case SQLITE_ACCESS_READ:
29584       amode = R_OK;
29585       break;
29586 
29587     default:
29588       assert(!"Invalid flags argument");
29589   }
29590   *pResOut = (osAccess(zPath, amode)==0);
29591   if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
29592     struct stat buf;
29593     if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
29594       *pResOut = 0;
29595     }
29596   }
29597   return SQLITE_OK;
29598 }
29599 
29600 
29601 /*
29602 ** Turn a relative pathname into a full pathname. The relative path
29603 ** is stored as a nul-terminated string in the buffer pointed to by
29604 ** zPath.
29605 **
29606 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
29607 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
29608 ** this buffer before returning.
29609 */
29610 static int unixFullPathname(
29611   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
29612   const char *zPath,            /* Possibly relative input path */
29613   int nOut,                     /* Size of output buffer in bytes */
29614   char *zOut                    /* Output buffer */
29615 ){
29616 
29617   /* It's odd to simulate an io-error here, but really this is just
29618   ** using the io-error infrastructure to test that SQLite handles this
29619   ** function failing. This function could fail if, for example, the
29620   ** current working directory has been unlinked.
29621   */
29622   SimulateIOError( return SQLITE_ERROR );
29623 
29624   assert( pVfs->mxPathname==MAX_PATHNAME );
29625   UNUSED_PARAMETER(pVfs);
29626 
29627   zOut[nOut-1] = '\0';
29628   if( zPath[0]=='/' ){
29629     sqlite3_snprintf(nOut, zOut, "%s", zPath);
29630   }else{
29631     int nCwd;
29632     if( osGetcwd(zOut, nOut-1)==0 ){
29633       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
29634     }
29635     nCwd = (int)strlen(zOut);
29636     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
29637   }
29638   return SQLITE_OK;
29639 }
29640 
29641 
29642 #ifndef SQLITE_OMIT_LOAD_EXTENSION
29643 /*
29644 ** Interfaces for opening a shared library, finding entry points
29645 ** within the shared library, and closing the shared library.
29646 */
29647 #include <dlfcn.h>
29648 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
29649   UNUSED_PARAMETER(NotUsed);
29650   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
29651 }
29652 
29653 /*
29654 ** SQLite calls this function immediately after a call to unixDlSym() or
29655 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
29656 ** message is available, it is written to zBufOut. If no error message
29657 ** is available, zBufOut is left unmodified and SQLite uses a default
29658 ** error message.
29659 */
29660 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
29661   const char *zErr;
29662   UNUSED_PARAMETER(NotUsed);
29663   unixEnterMutex();
29664   zErr = dlerror();
29665   if( zErr ){
29666     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
29667   }
29668   unixLeaveMutex();
29669 }
29670 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
29671   /*
29672   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
29673   ** cast into a pointer to a function.  And yet the library dlsym() routine
29674   ** returns a void* which is really a pointer to a function.  So how do we
29675   ** use dlsym() with -pedantic-errors?
29676   **
29677   ** Variable x below is defined to be a pointer to a function taking
29678   ** parameters void* and const char* and returning a pointer to a function.
29679   ** We initialize x by assigning it a pointer to the dlsym() function.
29680   ** (That assignment requires a cast.)  Then we call the function that
29681   ** x points to.
29682   **
29683   ** This work-around is unlikely to work correctly on any system where
29684   ** you really cannot cast a function pointer into void*.  But then, on the
29685   ** other hand, dlsym() will not work on such a system either, so we have
29686   ** not really lost anything.
29687   */
29688   void (*(*x)(void*,const char*))(void);
29689   UNUSED_PARAMETER(NotUsed);
29690   x = (void(*(*)(void*,const char*))(void))dlsym;
29691   return (*x)(p, zSym);
29692 }
29693 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
29694   UNUSED_PARAMETER(NotUsed);
29695   dlclose(pHandle);
29696 }
29697 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
29698   #define unixDlOpen  0
29699   #define unixDlError 0
29700   #define unixDlSym   0
29701   #define unixDlClose 0
29702 #endif
29703 
29704 /*
29705 ** Write nBuf bytes of random data to the supplied buffer zBuf.
29706 */
29707 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
29708   UNUSED_PARAMETER(NotUsed);
29709   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
29710 
29711   /* We have to initialize zBuf to prevent valgrind from reporting
29712   ** errors.  The reports issued by valgrind are incorrect - we would
29713   ** prefer that the randomness be increased by making use of the
29714   ** uninitialized space in zBuf - but valgrind errors tend to worry
29715   ** some users.  Rather than argue, it seems easier just to initialize
29716   ** the whole array and silence valgrind, even if that means less randomness
29717   ** in the random seed.
29718   **
29719   ** When testing, initializing zBuf[] to zero is all we do.  That means
29720   ** that we always use the same random number sequence.  This makes the
29721   ** tests repeatable.
29722   */
29723   memset(zBuf, 0, nBuf);
29724   randomnessPid = getpid();
29725 #if !defined(SQLITE_TEST)
29726   {
29727     int fd, got;
29728     fd = robust_open("/dev/urandom", O_RDONLY, 0);
29729     if( fd<0 ){
29730       time_t t;
29731       time(&t);
29732       memcpy(zBuf, &t, sizeof(t));
29733       memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
29734       assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
29735       nBuf = sizeof(t) + sizeof(randomnessPid);
29736     }else{
29737       do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
29738       robust_close(0, fd, __LINE__);
29739     }
29740   }
29741 #endif
29742   return nBuf;
29743 }
29744 
29745 
29746 /*
29747 ** Sleep for a little while.  Return the amount of time slept.
29748 ** The argument is the number of microseconds we want to sleep.
29749 ** The return value is the number of microseconds of sleep actually
29750 ** requested from the underlying operating system, a number which
29751 ** might be greater than or equal to the argument, but not less
29752 ** than the argument.
29753 */
29754 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
29755 #if OS_VXWORKS
29756   struct timespec sp;
29757 
29758   sp.tv_sec = microseconds / 1000000;
29759   sp.tv_nsec = (microseconds % 1000000) * 1000;
29760   nanosleep(&sp, NULL);
29761   UNUSED_PARAMETER(NotUsed);
29762   return microseconds;
29763 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
29764   usleep(microseconds);
29765   UNUSED_PARAMETER(NotUsed);
29766   return microseconds;
29767 #else
29768   int seconds = (microseconds+999999)/1000000;
29769   sleep(seconds);
29770   UNUSED_PARAMETER(NotUsed);
29771   return seconds*1000000;
29772 #endif
29773 }
29774 
29775 /*
29776 ** The following variable, if set to a non-zero value, is interpreted as
29777 ** the number of seconds since 1970 and is used to set the result of
29778 ** sqlite3OsCurrentTime() during testing.
29779 */
29780 #ifdef SQLITE_TEST
29781 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
29782 #endif
29783 
29784 /*
29785 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
29786 ** the current time and date as a Julian Day number times 86_400_000.  In
29787 ** other words, write into *piNow the number of milliseconds since the Julian
29788 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
29789 ** proleptic Gregorian calendar.
29790 **
29791 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
29792 ** cannot be found.
29793 */
29794 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
29795   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
29796   int rc = SQLITE_OK;
29797 #if defined(NO_GETTOD)
29798   time_t t;
29799   time(&t);
29800   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
29801 #elif OS_VXWORKS
29802   struct timespec sNow;
29803   clock_gettime(CLOCK_REALTIME, &sNow);
29804   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
29805 #else
29806   struct timeval sNow;
29807   if( gettimeofday(&sNow, 0)==0 ){
29808     *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
29809   }else{
29810     rc = SQLITE_ERROR;
29811   }
29812 #endif
29813 
29814 #ifdef SQLITE_TEST
29815   if( sqlite3_current_time ){
29816     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
29817   }
29818 #endif
29819   UNUSED_PARAMETER(NotUsed);
29820   return rc;
29821 }
29822 
29823 /*
29824 ** Find the current time (in Universal Coordinated Time).  Write the
29825 ** current time and date as a Julian Day number into *prNow and
29826 ** return 0.  Return 1 if the time and date cannot be found.
29827 */
29828 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
29829   sqlite3_int64 i = 0;
29830   int rc;
29831   UNUSED_PARAMETER(NotUsed);
29832   rc = unixCurrentTimeInt64(0, &i);
29833   *prNow = i/86400000.0;
29834   return rc;
29835 }
29836 
29837 /*
29838 ** We added the xGetLastError() method with the intention of providing
29839 ** better low-level error messages when operating-system problems come up
29840 ** during SQLite operation.  But so far, none of that has been implemented
29841 ** in the core.  So this routine is never called.  For now, it is merely
29842 ** a place-holder.
29843 */
29844 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
29845   UNUSED_PARAMETER(NotUsed);
29846   UNUSED_PARAMETER(NotUsed2);
29847   UNUSED_PARAMETER(NotUsed3);
29848   return 0;
29849 }
29850 
29851 
29852 /*
29853 ************************ End of sqlite3_vfs methods ***************************
29854 ******************************************************************************/
29855 
29856 /******************************************************************************
29857 ************************** Begin Proxy Locking ********************************
29858 **
29859 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
29860 ** other locking methods on secondary lock files.  Proxy locking is a
29861 ** meta-layer over top of the primitive locking implemented above.  For
29862 ** this reason, the division that implements of proxy locking is deferred
29863 ** until late in the file (here) after all of the other I/O methods have
29864 ** been defined - so that the primitive locking methods are available
29865 ** as services to help with the implementation of proxy locking.
29866 **
29867 ****
29868 **
29869 ** The default locking schemes in SQLite use byte-range locks on the
29870 ** database file to coordinate safe, concurrent access by multiple readers
29871 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
29872 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
29873 ** as POSIX read & write locks over fixed set of locations (via fsctl),
29874 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
29875 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
29876 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
29877 ** address in the shared range is taken for a SHARED lock, the entire
29878 ** shared range is taken for an EXCLUSIVE lock):
29879 **
29880 **      PENDING_BYTE        0x40000000
29881 **      RESERVED_BYTE       0x40000001
29882 **      SHARED_RANGE        0x40000002 -> 0x40000200
29883 **
29884 ** This works well on the local file system, but shows a nearly 100x
29885 ** slowdown in read performance on AFP because the AFP client disables
29886 ** the read cache when byte-range locks are present.  Enabling the read
29887 ** cache exposes a cache coherency problem that is present on all OS X
29888 ** supported network file systems.  NFS and AFP both observe the
29889 ** close-to-open semantics for ensuring cache coherency
29890 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
29891 ** address the requirements for concurrent database access by multiple
29892 ** readers and writers
29893 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
29894 **
29895 ** To address the performance and cache coherency issues, proxy file locking
29896 ** changes the way database access is controlled by limiting access to a
29897 ** single host at a time and moving file locks off of the database file
29898 ** and onto a proxy file on the local file system.
29899 **
29900 **
29901 ** Using proxy locks
29902 ** -----------------
29903 **
29904 ** C APIs
29905 **
29906 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
29907 **                       <proxy_path> | ":auto:");
29908 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
29909 **
29910 **
29911 ** SQL pragmas
29912 **
29913 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
29914 **  PRAGMA [database.]lock_proxy_file
29915 **
29916 ** Specifying ":auto:" means that if there is a conch file with a matching
29917 ** host ID in it, the proxy path in the conch file will be used, otherwise
29918 ** a proxy path based on the user's temp dir
29919 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
29920 ** actual proxy file name is generated from the name and path of the
29921 ** database file.  For example:
29922 **
29923 **       For database path "/Users/me/foo.db"
29924 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
29925 **
29926 ** Once a lock proxy is configured for a database connection, it can not
29927 ** be removed, however it may be switched to a different proxy path via
29928 ** the above APIs (assuming the conch file is not being held by another
29929 ** connection or process).
29930 **
29931 **
29932 ** How proxy locking works
29933 ** -----------------------
29934 **
29935 ** Proxy file locking relies primarily on two new supporting files:
29936 **
29937 **   *  conch file to limit access to the database file to a single host
29938 **      at a time
29939 **
29940 **   *  proxy file to act as a proxy for the advisory locks normally
29941 **      taken on the database
29942 **
29943 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
29944 ** by taking an sqlite-style shared lock on the conch file, reading the
29945 ** contents and comparing the host's unique host ID (see below) and lock
29946 ** proxy path against the values stored in the conch.  The conch file is
29947 ** stored in the same directory as the database file and the file name
29948 ** is patterned after the database file name as ".<databasename>-conch".
29949 ** If the conch file does not exist, or it's contents do not match the
29950 ** host ID and/or proxy path, then the lock is escalated to an exclusive
29951 ** lock and the conch file contents is updated with the host ID and proxy
29952 ** path and the lock is downgraded to a shared lock again.  If the conch
29953 ** is held by another process (with a shared lock), the exclusive lock
29954 ** will fail and SQLITE_BUSY is returned.
29955 **
29956 ** The proxy file - a single-byte file used for all advisory file locks
29957 ** normally taken on the database file.   This allows for safe sharing
29958 ** of the database file for multiple readers and writers on the same
29959 ** host (the conch ensures that they all use the same local lock file).
29960 **
29961 ** Requesting the lock proxy does not immediately take the conch, it is
29962 ** only taken when the first request to lock database file is made.
29963 ** This matches the semantics of the traditional locking behavior, where
29964 ** opening a connection to a database file does not take a lock on it.
29965 ** The shared lock and an open file descriptor are maintained until
29966 ** the connection to the database is closed.
29967 **
29968 ** The proxy file and the lock file are never deleted so they only need
29969 ** to be created the first time they are used.
29970 **
29971 ** Configuration options
29972 ** ---------------------
29973 **
29974 **  SQLITE_PREFER_PROXY_LOCKING
29975 **
29976 **       Database files accessed on non-local file systems are
29977 **       automatically configured for proxy locking, lock files are
29978 **       named automatically using the same logic as
29979 **       PRAGMA lock_proxy_file=":auto:"
29980 **
29981 **  SQLITE_PROXY_DEBUG
29982 **
29983 **       Enables the logging of error messages during host id file
29984 **       retrieval and creation
29985 **
29986 **  LOCKPROXYDIR
29987 **
29988 **       Overrides the default directory used for lock proxy files that
29989 **       are named automatically via the ":auto:" setting
29990 **
29991 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
29992 **
29993 **       Permissions to use when creating a directory for storing the
29994 **       lock proxy files, only used when LOCKPROXYDIR is not set.
29995 **
29996 **
29997 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
29998 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
29999 ** force proxy locking to be used for every database file opened, and 0
30000 ** will force automatic proxy locking to be disabled for all database
30001 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
30002 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
30003 */
30004 
30005 /*
30006 ** Proxy locking is only available on MacOSX
30007 */
30008 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
30009 
30010 /*
30011 ** The proxyLockingContext has the path and file structures for the remote
30012 ** and local proxy files in it
30013 */
30014 typedef struct proxyLockingContext proxyLockingContext;
30015 struct proxyLockingContext {
30016   unixFile *conchFile;         /* Open conch file */
30017   char *conchFilePath;         /* Name of the conch file */
30018   unixFile *lockProxy;         /* Open proxy lock file */
30019   char *lockProxyPath;         /* Name of the proxy lock file */
30020   char *dbPath;                /* Name of the open file */
30021   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
30022   void *oldLockingContext;     /* Original lockingcontext to restore on close */
30023   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
30024 };
30025 
30026 /*
30027 ** The proxy lock file path for the database at dbPath is written into lPath,
30028 ** which must point to valid, writable memory large enough for a maxLen length
30029 ** file path.
30030 */
30031 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
30032   int len;
30033   int dbLen;
30034   int i;
30035 
30036 #ifdef LOCKPROXYDIR
30037   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
30038 #else
30039 # ifdef _CS_DARWIN_USER_TEMP_DIR
30040   {
30041     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
30042       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
30043                lPath, errno, getpid()));
30044       return SQLITE_IOERR_LOCK;
30045     }
30046     len = strlcat(lPath, "sqliteplocks", maxLen);
30047   }
30048 # else
30049   len = strlcpy(lPath, "/tmp/", maxLen);
30050 # endif
30051 #endif
30052 
30053   if( lPath[len-1]!='/' ){
30054     len = strlcat(lPath, "/", maxLen);
30055   }
30056 
30057   /* transform the db path to a unique cache name */
30058   dbLen = (int)strlen(dbPath);
30059   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
30060     char c = dbPath[i];
30061     lPath[i+len] = (c=='/')?'_':c;
30062   }
30063   lPath[i+len]='\0';
30064   strlcat(lPath, ":auto:", maxLen);
30065   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
30066   return SQLITE_OK;
30067 }
30068 
30069 /*
30070  ** Creates the lock file and any missing directories in lockPath
30071  */
30072 static int proxyCreateLockPath(const char *lockPath){
30073   int i, len;
30074   char buf[MAXPATHLEN];
30075   int start = 0;
30076 
30077   assert(lockPath!=NULL);
30078   /* try to create all the intermediate directories */
30079   len = (int)strlen(lockPath);
30080   buf[0] = lockPath[0];
30081   for( i=1; i<len; i++ ){
30082     if( lockPath[i] == '/' && (i - start > 0) ){
30083       /* only mkdir if leaf dir != "." or "/" or ".." */
30084       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
30085          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
30086         buf[i]='\0';
30087         if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
30088           int err=errno;
30089           if( err!=EEXIST ) {
30090             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
30091                      "'%s' proxy lock path=%s pid=%d\n",
30092                      buf, strerror(err), lockPath, getpid()));
30093             return err;
30094           }
30095         }
30096       }
30097       start=i+1;
30098     }
30099     buf[i] = lockPath[i];
30100   }
30101   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
30102   return 0;
30103 }
30104 
30105 /*
30106 ** Create a new VFS file descriptor (stored in memory obtained from
30107 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
30108 **
30109 ** The caller is responsible not only for closing the file descriptor
30110 ** but also for freeing the memory associated with the file descriptor.
30111 */
30112 static int proxyCreateUnixFile(
30113     const char *path,        /* path for the new unixFile */
30114     unixFile **ppFile,       /* unixFile created and returned by ref */
30115     int islockfile           /* if non zero missing dirs will be created */
30116 ) {
30117   int fd = -1;
30118   unixFile *pNew;
30119   int rc = SQLITE_OK;
30120   int openFlags = O_RDWR | O_CREAT;
30121   sqlite3_vfs dummyVfs;
30122   int terrno = 0;
30123   UnixUnusedFd *pUnused = NULL;
30124 
30125   /* 1. first try to open/create the file
30126   ** 2. if that fails, and this is a lock file (not-conch), try creating
30127   ** the parent directories and then try again.
30128   ** 3. if that fails, try to open the file read-only
30129   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
30130   */
30131   pUnused = findReusableFd(path, openFlags);
30132   if( pUnused ){
30133     fd = pUnused->fd;
30134   }else{
30135     pUnused = sqlite3_malloc(sizeof(*pUnused));
30136     if( !pUnused ){
30137       return SQLITE_NOMEM;
30138     }
30139   }
30140   if( fd<0 ){
30141     fd = robust_open(path, openFlags, 0);
30142     terrno = errno;
30143     if( fd<0 && errno==ENOENT && islockfile ){
30144       if( proxyCreateLockPath(path) == SQLITE_OK ){
30145         fd = robust_open(path, openFlags, 0);
30146       }
30147     }
30148   }
30149   if( fd<0 ){
30150     openFlags = O_RDONLY;
30151     fd = robust_open(path, openFlags, 0);
30152     terrno = errno;
30153   }
30154   if( fd<0 ){
30155     if( islockfile ){
30156       return SQLITE_BUSY;
30157     }
30158     switch (terrno) {
30159       case EACCES:
30160         return SQLITE_PERM;
30161       case EIO:
30162         return SQLITE_IOERR_LOCK; /* even though it is the conch */
30163       default:
30164         return SQLITE_CANTOPEN_BKPT;
30165     }
30166   }
30167 
30168   pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
30169   if( pNew==NULL ){
30170     rc = SQLITE_NOMEM;
30171     goto end_create_proxy;
30172   }
30173   memset(pNew, 0, sizeof(unixFile));
30174   pNew->openFlags = openFlags;
30175   memset(&dummyVfs, 0, sizeof(dummyVfs));
30176   dummyVfs.pAppData = (void*)&autolockIoFinder;
30177   dummyVfs.zName = "dummy";
30178   pUnused->fd = fd;
30179   pUnused->flags = openFlags;
30180   pNew->pUnused = pUnused;
30181 
30182   rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
30183   if( rc==SQLITE_OK ){
30184     *ppFile = pNew;
30185     return SQLITE_OK;
30186   }
30187 end_create_proxy:
30188   robust_close(pNew, fd, __LINE__);
30189   sqlite3_free(pNew);
30190   sqlite3_free(pUnused);
30191   return rc;
30192 }
30193 
30194 #ifdef SQLITE_TEST
30195 /* simulate multiple hosts by creating unique hostid file paths */
30196 SQLITE_API int sqlite3_hostid_num = 0;
30197 #endif
30198 
30199 #define PROXY_HOSTIDLEN    16  /* conch file host id length */
30200 
30201 /* Not always defined in the headers as it ought to be */
30202 extern int gethostuuid(uuid_t id, const struct timespec *wait);
30203 
30204 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
30205 ** bytes of writable memory.
30206 */
30207 static int proxyGetHostID(unsigned char *pHostID, int *pError){
30208   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
30209   memset(pHostID, 0, PROXY_HOSTIDLEN);
30210 #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
30211                && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
30212   {
30213     static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
30214     if( gethostuuid(pHostID, &timeout) ){
30215       int err = errno;
30216       if( pError ){
30217         *pError = err;
30218       }
30219       return SQLITE_IOERR;
30220     }
30221   }
30222 #else
30223   UNUSED_PARAMETER(pError);
30224 #endif
30225 #ifdef SQLITE_TEST
30226   /* simulate multiple hosts by creating unique hostid file paths */
30227   if( sqlite3_hostid_num != 0){
30228     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
30229   }
30230 #endif
30231 
30232   return SQLITE_OK;
30233 }
30234 
30235 /* The conch file contains the header, host id and lock file path
30236  */
30237 #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
30238 #define PROXY_HEADERLEN    1   /* conch file header length */
30239 #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
30240 #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
30241 
30242 /*
30243 ** Takes an open conch file, copies the contents to a new path and then moves
30244 ** it back.  The newly created file's file descriptor is assigned to the
30245 ** conch file structure and finally the original conch file descriptor is
30246 ** closed.  Returns zero if successful.
30247 */
30248 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
30249   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30250   unixFile *conchFile = pCtx->conchFile;
30251   char tPath[MAXPATHLEN];
30252   char buf[PROXY_MAXCONCHLEN];
30253   char *cPath = pCtx->conchFilePath;
30254   size_t readLen = 0;
30255   size_t pathLen = 0;
30256   char errmsg[64] = "";
30257   int fd = -1;
30258   int rc = -1;
30259   UNUSED_PARAMETER(myHostID);
30260 
30261   /* create a new path by replace the trailing '-conch' with '-break' */
30262   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
30263   if( pathLen>MAXPATHLEN || pathLen<6 ||
30264      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
30265     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
30266     goto end_breaklock;
30267   }
30268   /* read the conch content */
30269   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
30270   if( readLen<PROXY_PATHINDEX ){
30271     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
30272     goto end_breaklock;
30273   }
30274   /* write it out to the temporary break file */
30275   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
30276   if( fd<0 ){
30277     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
30278     goto end_breaklock;
30279   }
30280   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
30281     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
30282     goto end_breaklock;
30283   }
30284   if( rename(tPath, cPath) ){
30285     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
30286     goto end_breaklock;
30287   }
30288   rc = 0;
30289   fprintf(stderr, "broke stale lock on %s\n", cPath);
30290   robust_close(pFile, conchFile->h, __LINE__);
30291   conchFile->h = fd;
30292   conchFile->openFlags = O_RDWR | O_CREAT;
30293 
30294 end_breaklock:
30295   if( rc ){
30296     if( fd>=0 ){
30297       osUnlink(tPath);
30298       robust_close(pFile, fd, __LINE__);
30299     }
30300     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
30301   }
30302   return rc;
30303 }
30304 
30305 /* Take the requested lock on the conch file and break a stale lock if the
30306 ** host id matches.
30307 */
30308 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
30309   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30310   unixFile *conchFile = pCtx->conchFile;
30311   int rc = SQLITE_OK;
30312   int nTries = 0;
30313   struct timespec conchModTime;
30314 
30315   memset(&conchModTime, 0, sizeof(conchModTime));
30316   do {
30317     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30318     nTries ++;
30319     if( rc==SQLITE_BUSY ){
30320       /* If the lock failed (busy):
30321        * 1st try: get the mod time of the conch, wait 0.5s and try again.
30322        * 2nd try: fail if the mod time changed or host id is different, wait
30323        *           10 sec and try again
30324        * 3rd try: break the lock unless the mod time has changed.
30325        */
30326       struct stat buf;
30327       if( osFstat(conchFile->h, &buf) ){
30328         pFile->lastErrno = errno;
30329         return SQLITE_IOERR_LOCK;
30330       }
30331 
30332       if( nTries==1 ){
30333         conchModTime = buf.st_mtimespec;
30334         usleep(500000); /* wait 0.5 sec and try the lock again*/
30335         continue;
30336       }
30337 
30338       assert( nTries>1 );
30339       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
30340          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
30341         return SQLITE_BUSY;
30342       }
30343 
30344       if( nTries==2 ){
30345         char tBuf[PROXY_MAXCONCHLEN];
30346         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
30347         if( len<0 ){
30348           pFile->lastErrno = errno;
30349           return SQLITE_IOERR_LOCK;
30350         }
30351         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
30352           /* don't break the lock if the host id doesn't match */
30353           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
30354             return SQLITE_BUSY;
30355           }
30356         }else{
30357           /* don't break the lock on short read or a version mismatch */
30358           return SQLITE_BUSY;
30359         }
30360         usleep(10000000); /* wait 10 sec and try the lock again */
30361         continue;
30362       }
30363 
30364       assert( nTries==3 );
30365       if( 0==proxyBreakConchLock(pFile, myHostID) ){
30366         rc = SQLITE_OK;
30367         if( lockType==EXCLUSIVE_LOCK ){
30368           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
30369         }
30370         if( !rc ){
30371           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30372         }
30373       }
30374     }
30375   } while( rc==SQLITE_BUSY && nTries<3 );
30376 
30377   return rc;
30378 }
30379 
30380 /* Takes the conch by taking a shared lock and read the contents conch, if
30381 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
30382 ** lockPath means that the lockPath in the conch file will be used if the
30383 ** host IDs match, or a new lock path will be generated automatically
30384 ** and written to the conch file.
30385 */
30386 static int proxyTakeConch(unixFile *pFile){
30387   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30388 
30389   if( pCtx->conchHeld!=0 ){
30390     return SQLITE_OK;
30391   }else{
30392     unixFile *conchFile = pCtx->conchFile;
30393     uuid_t myHostID;
30394     int pError = 0;
30395     char readBuf[PROXY_MAXCONCHLEN];
30396     char lockPath[MAXPATHLEN];
30397     char *tempLockPath = NULL;
30398     int rc = SQLITE_OK;
30399     int createConch = 0;
30400     int hostIdMatch = 0;
30401     int readLen = 0;
30402     int tryOldLockPath = 0;
30403     int forceNewLockPath = 0;
30404 
30405     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
30406              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
30407 
30408     rc = proxyGetHostID(myHostID, &pError);
30409     if( (rc&0xff)==SQLITE_IOERR ){
30410       pFile->lastErrno = pError;
30411       goto end_takeconch;
30412     }
30413     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
30414     if( rc!=SQLITE_OK ){
30415       goto end_takeconch;
30416     }
30417     /* read the existing conch file */
30418     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
30419     if( readLen<0 ){
30420       /* I/O error: lastErrno set by seekAndRead */
30421       pFile->lastErrno = conchFile->lastErrno;
30422       rc = SQLITE_IOERR_READ;
30423       goto end_takeconch;
30424     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
30425              readBuf[0]!=(char)PROXY_CONCHVERSION ){
30426       /* a short read or version format mismatch means we need to create a new
30427       ** conch file.
30428       */
30429       createConch = 1;
30430     }
30431     /* if the host id matches and the lock path already exists in the conch
30432     ** we'll try to use the path there, if we can't open that path, we'll
30433     ** retry with a new auto-generated path
30434     */
30435     do { /* in case we need to try again for an :auto: named lock file */
30436 
30437       if( !createConch && !forceNewLockPath ){
30438         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
30439                                   PROXY_HOSTIDLEN);
30440         /* if the conch has data compare the contents */
30441         if( !pCtx->lockProxyPath ){
30442           /* for auto-named local lock file, just check the host ID and we'll
30443            ** use the local lock file path that's already in there
30444            */
30445           if( hostIdMatch ){
30446             size_t pathLen = (readLen - PROXY_PATHINDEX);
30447 
30448             if( pathLen>=MAXPATHLEN ){
30449               pathLen=MAXPATHLEN-1;
30450             }
30451             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
30452             lockPath[pathLen] = 0;
30453             tempLockPath = lockPath;
30454             tryOldLockPath = 1;
30455             /* create a copy of the lock path if the conch is taken */
30456             goto end_takeconch;
30457           }
30458         }else if( hostIdMatch
30459                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
30460                            readLen-PROXY_PATHINDEX)
30461         ){
30462           /* conch host and lock path match */
30463           goto end_takeconch;
30464         }
30465       }
30466 
30467       /* if the conch isn't writable and doesn't match, we can't take it */
30468       if( (conchFile->openFlags&O_RDWR) == 0 ){
30469         rc = SQLITE_BUSY;
30470         goto end_takeconch;
30471       }
30472 
30473       /* either the conch didn't match or we need to create a new one */
30474       if( !pCtx->lockProxyPath ){
30475         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
30476         tempLockPath = lockPath;
30477         /* create a copy of the lock path _only_ if the conch is taken */
30478       }
30479 
30480       /* update conch with host and path (this will fail if other process
30481       ** has a shared lock already), if the host id matches, use the big
30482       ** stick.
30483       */
30484       futimes(conchFile->h, NULL);
30485       if( hostIdMatch && !createConch ){
30486         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
30487           /* We are trying for an exclusive lock but another thread in this
30488            ** same process is still holding a shared lock. */
30489           rc = SQLITE_BUSY;
30490         } else {
30491           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
30492         }
30493       }else{
30494         rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
30495       }
30496       if( rc==SQLITE_OK ){
30497         char writeBuffer[PROXY_MAXCONCHLEN];
30498         int writeSize = 0;
30499 
30500         writeBuffer[0] = (char)PROXY_CONCHVERSION;
30501         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
30502         if( pCtx->lockProxyPath!=NULL ){
30503           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
30504         }else{
30505           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
30506         }
30507         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
30508         robust_ftruncate(conchFile->h, writeSize);
30509         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
30510         fsync(conchFile->h);
30511         /* If we created a new conch file (not just updated the contents of a
30512          ** valid conch file), try to match the permissions of the database
30513          */
30514         if( rc==SQLITE_OK && createConch ){
30515           struct stat buf;
30516           int err = osFstat(pFile->h, &buf);
30517           if( err==0 ){
30518             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
30519                                         S_IROTH|S_IWOTH);
30520             /* try to match the database file R/W permissions, ignore failure */
30521 #ifndef SQLITE_PROXY_DEBUG
30522             osFchmod(conchFile->h, cmode);
30523 #else
30524             do{
30525               rc = osFchmod(conchFile->h, cmode);
30526             }while( rc==(-1) && errno==EINTR );
30527             if( rc!=0 ){
30528               int code = errno;
30529               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
30530                       cmode, code, strerror(code));
30531             } else {
30532               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
30533             }
30534           }else{
30535             int code = errno;
30536             fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
30537                     err, code, strerror(code));
30538 #endif
30539           }
30540         }
30541       }
30542       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30543 
30544     end_takeconch:
30545       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
30546       if( rc==SQLITE_OK && pFile->openFlags ){
30547         int fd;
30548         if( pFile->h>=0 ){
30549           robust_close(pFile, pFile->h, __LINE__);
30550         }
30551         pFile->h = -1;
30552         fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
30553         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
30554         if( fd>=0 ){
30555           pFile->h = fd;
30556         }else{
30557           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
30558            during locking */
30559         }
30560       }
30561       if( rc==SQLITE_OK && !pCtx->lockProxy ){
30562         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
30563         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
30564         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
30565           /* we couldn't create the proxy lock file with the old lock file path
30566            ** so try again via auto-naming
30567            */
30568           forceNewLockPath = 1;
30569           tryOldLockPath = 0;
30570           continue; /* go back to the do {} while start point, try again */
30571         }
30572       }
30573       if( rc==SQLITE_OK ){
30574         /* Need to make a copy of path if we extracted the value
30575          ** from the conch file or the path was allocated on the stack
30576          */
30577         if( tempLockPath ){
30578           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
30579           if( !pCtx->lockProxyPath ){
30580             rc = SQLITE_NOMEM;
30581           }
30582         }
30583       }
30584       if( rc==SQLITE_OK ){
30585         pCtx->conchHeld = 1;
30586 
30587         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
30588           afpLockingContext *afpCtx;
30589           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
30590           afpCtx->dbPath = pCtx->lockProxyPath;
30591         }
30592       } else {
30593         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30594       }
30595       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
30596                rc==SQLITE_OK?"ok":"failed"));
30597       return rc;
30598     } while (1); /* in case we need to retry the :auto: lock file -
30599                  ** we should never get here except via the 'continue' call. */
30600   }
30601 }
30602 
30603 /*
30604 ** If pFile holds a lock on a conch file, then release that lock.
30605 */
30606 static int proxyReleaseConch(unixFile *pFile){
30607   int rc = SQLITE_OK;         /* Subroutine return code */
30608   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
30609   unixFile *conchFile;        /* Name of the conch file */
30610 
30611   pCtx = (proxyLockingContext *)pFile->lockingContext;
30612   conchFile = pCtx->conchFile;
30613   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
30614            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
30615            getpid()));
30616   if( pCtx->conchHeld>0 ){
30617     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30618   }
30619   pCtx->conchHeld = 0;
30620   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
30621            (rc==SQLITE_OK ? "ok" : "failed")));
30622   return rc;
30623 }
30624 
30625 /*
30626 ** Given the name of a database file, compute the name of its conch file.
30627 ** Store the conch filename in memory obtained from sqlite3_malloc().
30628 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
30629 ** or SQLITE_NOMEM if unable to obtain memory.
30630 **
30631 ** The caller is responsible for ensuring that the allocated memory
30632 ** space is eventually freed.
30633 **
30634 ** *pConchPath is set to NULL if a memory allocation error occurs.
30635 */
30636 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
30637   int i;                        /* Loop counter */
30638   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
30639   char *conchPath;              /* buffer in which to construct conch name */
30640 
30641   /* Allocate space for the conch filename and initialize the name to
30642   ** the name of the original database file. */
30643   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
30644   if( conchPath==0 ){
30645     return SQLITE_NOMEM;
30646   }
30647   memcpy(conchPath, dbPath, len+1);
30648 
30649   /* now insert a "." before the last / character */
30650   for( i=(len-1); i>=0; i-- ){
30651     if( conchPath[i]=='/' ){
30652       i++;
30653       break;
30654     }
30655   }
30656   conchPath[i]='.';
30657   while ( i<len ){
30658     conchPath[i+1]=dbPath[i];
30659     i++;
30660   }
30661 
30662   /* append the "-conch" suffix to the file */
30663   memcpy(&conchPath[i+1], "-conch", 7);
30664   assert( (int)strlen(conchPath) == len+7 );
30665 
30666   return SQLITE_OK;
30667 }
30668 
30669 
30670 /* Takes a fully configured proxy locking-style unix file and switches
30671 ** the local lock file path
30672 */
30673 static int switchLockProxyPath(unixFile *pFile, const char *path) {
30674   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30675   char *oldPath = pCtx->lockProxyPath;
30676   int rc = SQLITE_OK;
30677 
30678   if( pFile->eFileLock!=NO_LOCK ){
30679     return SQLITE_BUSY;
30680   }
30681 
30682   /* nothing to do if the path is NULL, :auto: or matches the existing path */
30683   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
30684     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
30685     return SQLITE_OK;
30686   }else{
30687     unixFile *lockProxy = pCtx->lockProxy;
30688     pCtx->lockProxy=NULL;
30689     pCtx->conchHeld = 0;
30690     if( lockProxy!=NULL ){
30691       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
30692       if( rc ) return rc;
30693       sqlite3_free(lockProxy);
30694     }
30695     sqlite3_free(oldPath);
30696     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
30697   }
30698 
30699   return rc;
30700 }
30701 
30702 /*
30703 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
30704 ** is a string buffer at least MAXPATHLEN+1 characters in size.
30705 **
30706 ** This routine find the filename associated with pFile and writes it
30707 ** int dbPath.
30708 */
30709 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
30710 #if defined(__APPLE__)
30711   if( pFile->pMethod == &afpIoMethods ){
30712     /* afp style keeps a reference to the db path in the filePath field
30713     ** of the struct */
30714     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30715     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
30716   } else
30717 #endif
30718   if( pFile->pMethod == &dotlockIoMethods ){
30719     /* dot lock style uses the locking context to store the dot lock
30720     ** file path */
30721     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
30722     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
30723   }else{
30724     /* all other styles use the locking context to store the db file path */
30725     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30726     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
30727   }
30728   return SQLITE_OK;
30729 }
30730 
30731 /*
30732 ** Takes an already filled in unix file and alters it so all file locking
30733 ** will be performed on the local proxy lock file.  The following fields
30734 ** are preserved in the locking context so that they can be restored and
30735 ** the unix structure properly cleaned up at close time:
30736 **  ->lockingContext
30737 **  ->pMethod
30738 */
30739 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
30740   proxyLockingContext *pCtx;
30741   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
30742   char *lockPath=NULL;
30743   int rc = SQLITE_OK;
30744 
30745   if( pFile->eFileLock!=NO_LOCK ){
30746     return SQLITE_BUSY;
30747   }
30748   proxyGetDbPathForUnixFile(pFile, dbPath);
30749   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
30750     lockPath=NULL;
30751   }else{
30752     lockPath=(char *)path;
30753   }
30754 
30755   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
30756            (lockPath ? lockPath : ":auto:"), getpid()));
30757 
30758   pCtx = sqlite3_malloc( sizeof(*pCtx) );
30759   if( pCtx==0 ){
30760     return SQLITE_NOMEM;
30761   }
30762   memset(pCtx, 0, sizeof(*pCtx));
30763 
30764   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
30765   if( rc==SQLITE_OK ){
30766     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
30767     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
30768       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
30769       ** (c) the file system is read-only, then enable no-locking access.
30770       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
30771       ** that openFlags will have only one of O_RDONLY or O_RDWR.
30772       */
30773       struct statfs fsInfo;
30774       struct stat conchInfo;
30775       int goLockless = 0;
30776 
30777       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
30778         int err = errno;
30779         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
30780           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
30781         }
30782       }
30783       if( goLockless ){
30784         pCtx->conchHeld = -1; /* read only FS/ lockless */
30785         rc = SQLITE_OK;
30786       }
30787     }
30788   }
30789   if( rc==SQLITE_OK && lockPath ){
30790     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
30791   }
30792 
30793   if( rc==SQLITE_OK ){
30794     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
30795     if( pCtx->dbPath==NULL ){
30796       rc = SQLITE_NOMEM;
30797     }
30798   }
30799   if( rc==SQLITE_OK ){
30800     /* all memory is allocated, proxys are created and assigned,
30801     ** switch the locking context and pMethod then return.
30802     */
30803     pCtx->oldLockingContext = pFile->lockingContext;
30804     pFile->lockingContext = pCtx;
30805     pCtx->pOldMethod = pFile->pMethod;
30806     pFile->pMethod = &proxyIoMethods;
30807   }else{
30808     if( pCtx->conchFile ){
30809       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
30810       sqlite3_free(pCtx->conchFile);
30811     }
30812     sqlite3DbFree(0, pCtx->lockProxyPath);
30813     sqlite3_free(pCtx->conchFilePath);
30814     sqlite3_free(pCtx);
30815   }
30816   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
30817            (rc==SQLITE_OK ? "ok" : "failed")));
30818   return rc;
30819 }
30820 
30821 
30822 /*
30823 ** This routine handles sqlite3_file_control() calls that are specific
30824 ** to proxy locking.
30825 */
30826 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
30827   switch( op ){
30828     case SQLITE_GET_LOCKPROXYFILE: {
30829       unixFile *pFile = (unixFile*)id;
30830       if( pFile->pMethod == &proxyIoMethods ){
30831         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30832         proxyTakeConch(pFile);
30833         if( pCtx->lockProxyPath ){
30834           *(const char **)pArg = pCtx->lockProxyPath;
30835         }else{
30836           *(const char **)pArg = ":auto: (not held)";
30837         }
30838       } else {
30839         *(const char **)pArg = NULL;
30840       }
30841       return SQLITE_OK;
30842     }
30843     case SQLITE_SET_LOCKPROXYFILE: {
30844       unixFile *pFile = (unixFile*)id;
30845       int rc = SQLITE_OK;
30846       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
30847       if( pArg==NULL || (const char *)pArg==0 ){
30848         if( isProxyStyle ){
30849           /* turn off proxy locking - not supported */
30850           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
30851         }else{
30852           /* turn off proxy locking - already off - NOOP */
30853           rc = SQLITE_OK;
30854         }
30855       }else{
30856         const char *proxyPath = (const char *)pArg;
30857         if( isProxyStyle ){
30858           proxyLockingContext *pCtx =
30859             (proxyLockingContext*)pFile->lockingContext;
30860           if( !strcmp(pArg, ":auto:")
30861            || (pCtx->lockProxyPath &&
30862                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
30863           ){
30864             rc = SQLITE_OK;
30865           }else{
30866             rc = switchLockProxyPath(pFile, proxyPath);
30867           }
30868         }else{
30869           /* turn on proxy file locking */
30870           rc = proxyTransformUnixFile(pFile, proxyPath);
30871         }
30872       }
30873       return rc;
30874     }
30875     default: {
30876       assert( 0 );  /* The call assures that only valid opcodes are sent */
30877     }
30878   }
30879   /*NOTREACHED*/
30880   return SQLITE_ERROR;
30881 }
30882 
30883 /*
30884 ** Within this division (the proxying locking implementation) the procedures
30885 ** above this point are all utilities.  The lock-related methods of the
30886 ** proxy-locking sqlite3_io_method object follow.
30887 */
30888 
30889 
30890 /*
30891 ** This routine checks if there is a RESERVED lock held on the specified
30892 ** file by this or any other process. If such a lock is held, set *pResOut
30893 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
30894 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
30895 */
30896 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
30897   unixFile *pFile = (unixFile*)id;
30898   int rc = proxyTakeConch(pFile);
30899   if( rc==SQLITE_OK ){
30900     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30901     if( pCtx->conchHeld>0 ){
30902       unixFile *proxy = pCtx->lockProxy;
30903       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
30904     }else{ /* conchHeld < 0 is lockless */
30905       pResOut=0;
30906     }
30907   }
30908   return rc;
30909 }
30910 
30911 /*
30912 ** Lock the file with the lock specified by parameter eFileLock - one
30913 ** of the following:
30914 **
30915 **     (1) SHARED_LOCK
30916 **     (2) RESERVED_LOCK
30917 **     (3) PENDING_LOCK
30918 **     (4) EXCLUSIVE_LOCK
30919 **
30920 ** Sometimes when requesting one lock state, additional lock states
30921 ** are inserted in between.  The locking might fail on one of the later
30922 ** transitions leaving the lock state different from what it started but
30923 ** still short of its goal.  The following chart shows the allowed
30924 ** transitions and the inserted intermediate states:
30925 **
30926 **    UNLOCKED -> SHARED
30927 **    SHARED -> RESERVED
30928 **    SHARED -> (PENDING) -> EXCLUSIVE
30929 **    RESERVED -> (PENDING) -> EXCLUSIVE
30930 **    PENDING -> EXCLUSIVE
30931 **
30932 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
30933 ** routine to lower a locking level.
30934 */
30935 static int proxyLock(sqlite3_file *id, int eFileLock) {
30936   unixFile *pFile = (unixFile*)id;
30937   int rc = proxyTakeConch(pFile);
30938   if( rc==SQLITE_OK ){
30939     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30940     if( pCtx->conchHeld>0 ){
30941       unixFile *proxy = pCtx->lockProxy;
30942       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
30943       pFile->eFileLock = proxy->eFileLock;
30944     }else{
30945       /* conchHeld < 0 is lockless */
30946     }
30947   }
30948   return rc;
30949 }
30950 
30951 
30952 /*
30953 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
30954 ** must be either NO_LOCK or SHARED_LOCK.
30955 **
30956 ** If the locking level of the file descriptor is already at or below
30957 ** the requested locking level, this routine is a no-op.
30958 */
30959 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
30960   unixFile *pFile = (unixFile*)id;
30961   int rc = proxyTakeConch(pFile);
30962   if( rc==SQLITE_OK ){
30963     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30964     if( pCtx->conchHeld>0 ){
30965       unixFile *proxy = pCtx->lockProxy;
30966       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
30967       pFile->eFileLock = proxy->eFileLock;
30968     }else{
30969       /* conchHeld < 0 is lockless */
30970     }
30971   }
30972   return rc;
30973 }
30974 
30975 /*
30976 ** Close a file that uses proxy locks.
30977 */
30978 static int proxyClose(sqlite3_file *id) {
30979   if( id ){
30980     unixFile *pFile = (unixFile*)id;
30981     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30982     unixFile *lockProxy = pCtx->lockProxy;
30983     unixFile *conchFile = pCtx->conchFile;
30984     int rc = SQLITE_OK;
30985 
30986     if( lockProxy ){
30987       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
30988       if( rc ) return rc;
30989       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
30990       if( rc ) return rc;
30991       sqlite3_free(lockProxy);
30992       pCtx->lockProxy = 0;
30993     }
30994     if( conchFile ){
30995       if( pCtx->conchHeld ){
30996         rc = proxyReleaseConch(pFile);
30997         if( rc ) return rc;
30998       }
30999       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
31000       if( rc ) return rc;
31001       sqlite3_free(conchFile);
31002     }
31003     sqlite3DbFree(0, pCtx->lockProxyPath);
31004     sqlite3_free(pCtx->conchFilePath);
31005     sqlite3DbFree(0, pCtx->dbPath);
31006     /* restore the original locking context and pMethod then close it */
31007     pFile->lockingContext = pCtx->oldLockingContext;
31008     pFile->pMethod = pCtx->pOldMethod;
31009     sqlite3_free(pCtx);
31010     return pFile->pMethod->xClose(id);
31011   }
31012   return SQLITE_OK;
31013 }
31014 
31015 
31016 
31017 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
31018 /*
31019 ** The proxy locking style is intended for use with AFP filesystems.
31020 ** And since AFP is only supported on MacOSX, the proxy locking is also
31021 ** restricted to MacOSX.
31022 **
31023 **
31024 ******************* End of the proxy lock implementation **********************
31025 ******************************************************************************/
31026 
31027 /*
31028 ** Initialize the operating system interface.
31029 **
31030 ** This routine registers all VFS implementations for unix-like operating
31031 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
31032 ** should be the only routines in this file that are visible from other
31033 ** files.
31034 **
31035 ** This routine is called once during SQLite initialization and by a
31036 ** single thread.  The memory allocation and mutex subsystems have not
31037 ** necessarily been initialized when this routine is called, and so they
31038 ** should not be used.
31039 */
31040 SQLITE_API int sqlite3_os_init(void){
31041   /*
31042   ** The following macro defines an initializer for an sqlite3_vfs object.
31043   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
31044   ** to the "finder" function.  (pAppData is a pointer to a pointer because
31045   ** silly C90 rules prohibit a void* from being cast to a function pointer
31046   ** and so we have to go through the intermediate pointer to avoid problems
31047   ** when compiling with -pedantic-errors on GCC.)
31048   **
31049   ** The FINDER parameter to this macro is the name of the pointer to the
31050   ** finder-function.  The finder-function returns a pointer to the
31051   ** sqlite_io_methods object that implements the desired locking
31052   ** behaviors.  See the division above that contains the IOMETHODS
31053   ** macro for addition information on finder-functions.
31054   **
31055   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
31056   ** object.  But the "autolockIoFinder" available on MacOSX does a little
31057   ** more than that; it looks at the filesystem type that hosts the
31058   ** database file and tries to choose an locking method appropriate for
31059   ** that filesystem time.
31060   */
31061   #define UNIXVFS(VFSNAME, FINDER) {                        \
31062     3,                    /* iVersion */                    \
31063     sizeof(unixFile),     /* szOsFile */                    \
31064     MAX_PATHNAME,         /* mxPathname */                  \
31065     0,                    /* pNext */                       \
31066     VFSNAME,              /* zName */                       \
31067     (void*)&FINDER,       /* pAppData */                    \
31068     unixOpen,             /* xOpen */                       \
31069     unixDelete,           /* xDelete */                     \
31070     unixAccess,           /* xAccess */                     \
31071     unixFullPathname,     /* xFullPathname */               \
31072     unixDlOpen,           /* xDlOpen */                     \
31073     unixDlError,          /* xDlError */                    \
31074     unixDlSym,            /* xDlSym */                      \
31075     unixDlClose,          /* xDlClose */                    \
31076     unixRandomness,       /* xRandomness */                 \
31077     unixSleep,            /* xSleep */                      \
31078     unixCurrentTime,      /* xCurrentTime */                \
31079     unixGetLastError,     /* xGetLastError */               \
31080     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
31081     unixSetSystemCall,    /* xSetSystemCall */              \
31082     unixGetSystemCall,    /* xGetSystemCall */              \
31083     unixNextSystemCall,   /* xNextSystemCall */             \
31084   }
31085 
31086   /*
31087   ** All default VFSes for unix are contained in the following array.
31088   **
31089   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
31090   ** by the SQLite core when the VFS is registered.  So the following
31091   ** array cannot be const.
31092   */
31093   static sqlite3_vfs aVfs[] = {
31094 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
31095     UNIXVFS("unix",          autolockIoFinder ),
31096 #else
31097     UNIXVFS("unix",          posixIoFinder ),
31098 #endif
31099     UNIXVFS("unix-none",     nolockIoFinder ),
31100     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
31101     UNIXVFS("unix-excl",     posixIoFinder ),
31102 #if OS_VXWORKS
31103     UNIXVFS("unix-namedsem", semIoFinder ),
31104 #endif
31105 #if SQLITE_ENABLE_LOCKING_STYLE
31106     UNIXVFS("unix-posix",    posixIoFinder ),
31107 #if !OS_VXWORKS
31108     UNIXVFS("unix-flock",    flockIoFinder ),
31109 #endif
31110 #endif
31111 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
31112     UNIXVFS("unix-afp",      afpIoFinder ),
31113     UNIXVFS("unix-nfs",      nfsIoFinder ),
31114     UNIXVFS("unix-proxy",    proxyIoFinder ),
31115 #endif
31116   };
31117   unsigned int i;          /* Loop counter */
31118 
31119   /* Double-check that the aSyscall[] array has been constructed
31120   ** correctly.  See ticket [bb3a86e890c8e96ab] */
31121   assert( ArraySize(aSyscall)==24 );
31122 
31123   /* Register all VFSes defined in the aVfs[] array */
31124   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31125     sqlite3_vfs_register(&aVfs[i], i==0);
31126   }
31127   return SQLITE_OK;
31128 }
31129 
31130 /*
31131 ** Shutdown the operating system interface.
31132 **
31133 ** Some operating systems might need to do some cleanup in this routine,
31134 ** to release dynamically allocated objects.  But not on unix.
31135 ** This routine is a no-op for unix.
31136 */
31137 SQLITE_API int sqlite3_os_end(void){
31138   return SQLITE_OK;
31139 }
31140 
31141 #endif /* SQLITE_OS_UNIX */
31142 
31143 /************** End of os_unix.c *********************************************/
31144 /************** Begin file os_win.c ******************************************/
31145 /*
31146 ** 2004 May 22
31147 **
31148 ** The author disclaims copyright to this source code.  In place of
31149 ** a legal notice, here is a blessing:
31150 **
31151 **    May you do good and not evil.
31152 **    May you find forgiveness for yourself and forgive others.
31153 **    May you share freely, never taking more than you give.
31154 **
31155 ******************************************************************************
31156 **
31157 ** This file contains code that is specific to Windows.
31158 */
31159 #if SQLITE_OS_WIN               /* This file is used for Windows only */
31160 
31161 #ifdef __CYGWIN__
31162 # include <sys/cygwin.h>
31163 # include <errno.h> /* amalgamator: keep */
31164 #endif
31165 
31166 /*
31167 ** Include code that is common to all os_*.c files
31168 */
31169 /************** Include os_common.h in the middle of os_win.c ****************/
31170 /************** Begin file os_common.h ***************************************/
31171 /*
31172 ** 2004 May 22
31173 **
31174 ** The author disclaims copyright to this source code.  In place of
31175 ** a legal notice, here is a blessing:
31176 **
31177 **    May you do good and not evil.
31178 **    May you find forgiveness for yourself and forgive others.
31179 **    May you share freely, never taking more than you give.
31180 **
31181 ******************************************************************************
31182 **
31183 ** This file contains macros and a little bit of code that is common to
31184 ** all of the platform-specific files (os_*.c) and is #included into those
31185 ** files.
31186 **
31187 ** This file should be #included by the os_*.c files only.  It is not a
31188 ** general purpose header file.
31189 */
31190 #ifndef _OS_COMMON_H_
31191 #define _OS_COMMON_H_
31192 
31193 /*
31194 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
31195 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
31196 ** switch.  The following code should catch this problem at compile-time.
31197 */
31198 #ifdef MEMORY_DEBUG
31199 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
31200 #endif
31201 
31202 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
31203 # ifndef SQLITE_DEBUG_OS_TRACE
31204 #   define SQLITE_DEBUG_OS_TRACE 0
31205 # endif
31206   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
31207 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
31208 #else
31209 # define OSTRACE(X)
31210 #endif
31211 
31212 /*
31213 ** Macros for performance tracing.  Normally turned off.  Only works
31214 ** on i486 hardware.
31215 */
31216 #ifdef SQLITE_PERFORMANCE_TRACE
31217 
31218 /*
31219 ** hwtime.h contains inline assembler code for implementing
31220 ** high-performance timing routines.
31221 */
31222 /************** Include hwtime.h in the middle of os_common.h ****************/
31223 /************** Begin file hwtime.h ******************************************/
31224 /*
31225 ** 2008 May 27
31226 **
31227 ** The author disclaims copyright to this source code.  In place of
31228 ** a legal notice, here is a blessing:
31229 **
31230 **    May you do good and not evil.
31231 **    May you find forgiveness for yourself and forgive others.
31232 **    May you share freely, never taking more than you give.
31233 **
31234 ******************************************************************************
31235 **
31236 ** This file contains inline asm code for retrieving "high-performance"
31237 ** counters for x86 class CPUs.
31238 */
31239 #ifndef _HWTIME_H_
31240 #define _HWTIME_H_
31241 
31242 /*
31243 ** The following routine only works on pentium-class (or newer) processors.
31244 ** It uses the RDTSC opcode to read the cycle count value out of the
31245 ** processor and returns that value.  This can be used for high-res
31246 ** profiling.
31247 */
31248 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
31249       (defined(i386) || defined(__i386__) || defined(_M_IX86))
31250 
31251   #if defined(__GNUC__)
31252 
31253   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31254      unsigned int lo, hi;
31255      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
31256      return (sqlite_uint64)hi << 32 | lo;
31257   }
31258 
31259   #elif defined(_MSC_VER)
31260 
31261   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
31262      __asm {
31263         rdtsc
31264         ret       ; return value at EDX:EAX
31265      }
31266   }
31267 
31268   #endif
31269 
31270 #elif (defined(__GNUC__) && defined(__x86_64__))
31271 
31272   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31273       unsigned long val;
31274       __asm__ __volatile__ ("rdtsc" : "=A" (val));
31275       return val;
31276   }
31277 
31278 #elif (defined(__GNUC__) && defined(__ppc__))
31279 
31280   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31281       unsigned long long retval;
31282       unsigned long junk;
31283       __asm__ __volatile__ ("\n\
31284           1:      mftbu   %1\n\
31285                   mftb    %L0\n\
31286                   mftbu   %0\n\
31287                   cmpw    %0,%1\n\
31288                   bne     1b"
31289                   : "=r" (retval), "=r" (junk));
31290       return retval;
31291   }
31292 
31293 #else
31294 
31295   #error Need implementation of sqlite3Hwtime() for your platform.
31296 
31297   /*
31298   ** To compile without implementing sqlite3Hwtime() for your platform,
31299   ** you can remove the above #error and use the following
31300   ** stub function.  You will lose timing support for many
31301   ** of the debugging and testing utilities, but it should at
31302   ** least compile and run.
31303   */
31304 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
31305 
31306 #endif
31307 
31308 #endif /* !defined(_HWTIME_H_) */
31309 
31310 /************** End of hwtime.h **********************************************/
31311 /************** Continuing where we left off in os_common.h ******************/
31312 
31313 static sqlite_uint64 g_start;
31314 static sqlite_uint64 g_elapsed;
31315 #define TIMER_START       g_start=sqlite3Hwtime()
31316 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
31317 #define TIMER_ELAPSED     g_elapsed
31318 #else
31319 #define TIMER_START
31320 #define TIMER_END
31321 #define TIMER_ELAPSED     ((sqlite_uint64)0)
31322 #endif
31323 
31324 /*
31325 ** If we compile with the SQLITE_TEST macro set, then the following block
31326 ** of code will give us the ability to simulate a disk I/O error.  This
31327 ** is used for testing the I/O recovery logic.
31328 */
31329 #ifdef SQLITE_TEST
31330 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
31331 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
31332 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
31333 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
31334 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
31335 SQLITE_API int sqlite3_diskfull_pending = 0;
31336 SQLITE_API int sqlite3_diskfull = 0;
31337 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
31338 #define SimulateIOError(CODE)  \
31339   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
31340        || sqlite3_io_error_pending-- == 1 )  \
31341               { local_ioerr(); CODE; }
31342 static void local_ioerr(){
31343   IOTRACE(("IOERR\n"));
31344   sqlite3_io_error_hit++;
31345   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
31346 }
31347 #define SimulateDiskfullError(CODE) \
31348    if( sqlite3_diskfull_pending ){ \
31349      if( sqlite3_diskfull_pending == 1 ){ \
31350        local_ioerr(); \
31351        sqlite3_diskfull = 1; \
31352        sqlite3_io_error_hit = 1; \
31353        CODE; \
31354      }else{ \
31355        sqlite3_diskfull_pending--; \
31356      } \
31357    }
31358 #else
31359 #define SimulateIOErrorBenign(X)
31360 #define SimulateIOError(A)
31361 #define SimulateDiskfullError(A)
31362 #endif
31363 
31364 /*
31365 ** When testing, keep a count of the number of open files.
31366 */
31367 #ifdef SQLITE_TEST
31368 SQLITE_API int sqlite3_open_file_count = 0;
31369 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
31370 #else
31371 #define OpenCounter(X)
31372 #endif
31373 
31374 #endif /* !defined(_OS_COMMON_H_) */
31375 
31376 /************** End of os_common.h *******************************************/
31377 /************** Continuing where we left off in os_win.c *********************/
31378 
31379 /*
31380 ** Compiling and using WAL mode requires several APIs that are only
31381 ** available in Windows platforms based on the NT kernel.
31382 */
31383 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
31384 #  error "WAL mode requires support from the Windows NT kernel, compile\
31385  with SQLITE_OMIT_WAL."
31386 #endif
31387 
31388 /*
31389 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
31390 ** based on the sub-platform)?
31391 */
31392 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
31393 #  define SQLITE_WIN32_HAS_ANSI
31394 #endif
31395 
31396 /*
31397 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
31398 ** based on the sub-platform)?
31399 */
31400 #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
31401     !defined(SQLITE_WIN32_NO_WIDE)
31402 #  define SQLITE_WIN32_HAS_WIDE
31403 #endif
31404 
31405 /*
31406 ** Make sure at least one set of Win32 APIs is available.
31407 */
31408 #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
31409 #  error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
31410  must be defined."
31411 #endif
31412 
31413 /*
31414 ** Define the required Windows SDK version constants if they are not
31415 ** already available.
31416 */
31417 #ifndef NTDDI_WIN8
31418 #  define NTDDI_WIN8                        0x06020000
31419 #endif
31420 
31421 #ifndef NTDDI_WINBLUE
31422 #  define NTDDI_WINBLUE                     0x06030000
31423 #endif
31424 
31425 /*
31426 ** Check if the GetVersionEx[AW] functions should be considered deprecated
31427 ** and avoid using them in that case.  It should be noted here that if the
31428 ** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
31429 ** (whether via this block or via being manually specified), that implies
31430 ** the underlying operating system will always be based on the Windows NT
31431 ** Kernel.
31432 */
31433 #ifndef SQLITE_WIN32_GETVERSIONEX
31434 #  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
31435 #    define SQLITE_WIN32_GETVERSIONEX   0
31436 #  else
31437 #    define SQLITE_WIN32_GETVERSIONEX   1
31438 #  endif
31439 #endif
31440 
31441 /*
31442 ** This constant should already be defined (in the "WinDef.h" SDK file).
31443 */
31444 #ifndef MAX_PATH
31445 #  define MAX_PATH                      (260)
31446 #endif
31447 
31448 /*
31449 ** Maximum pathname length (in chars) for Win32.  This should normally be
31450 ** MAX_PATH.
31451 */
31452 #ifndef SQLITE_WIN32_MAX_PATH_CHARS
31453 #  define SQLITE_WIN32_MAX_PATH_CHARS   (MAX_PATH)
31454 #endif
31455 
31456 /*
31457 ** This constant should already be defined (in the "WinNT.h" SDK file).
31458 */
31459 #ifndef UNICODE_STRING_MAX_CHARS
31460 #  define UNICODE_STRING_MAX_CHARS      (32767)
31461 #endif
31462 
31463 /*
31464 ** Maximum pathname length (in chars) for WinNT.  This should normally be
31465 ** UNICODE_STRING_MAX_CHARS.
31466 */
31467 #ifndef SQLITE_WINNT_MAX_PATH_CHARS
31468 #  define SQLITE_WINNT_MAX_PATH_CHARS   (UNICODE_STRING_MAX_CHARS)
31469 #endif
31470 
31471 /*
31472 ** Maximum pathname length (in bytes) for Win32.  The MAX_PATH macro is in
31473 ** characters, so we allocate 4 bytes per character assuming worst-case of
31474 ** 4-bytes-per-character for UTF8.
31475 */
31476 #ifndef SQLITE_WIN32_MAX_PATH_BYTES
31477 #  define SQLITE_WIN32_MAX_PATH_BYTES   (SQLITE_WIN32_MAX_PATH_CHARS*4)
31478 #endif
31479 
31480 /*
31481 ** Maximum pathname length (in bytes) for WinNT.  This should normally be
31482 ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
31483 */
31484 #ifndef SQLITE_WINNT_MAX_PATH_BYTES
31485 #  define SQLITE_WINNT_MAX_PATH_BYTES   \
31486                             (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
31487 #endif
31488 
31489 /*
31490 ** Maximum error message length (in chars) for WinRT.
31491 */
31492 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
31493 #  define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
31494 #endif
31495 
31496 /*
31497 ** Returns non-zero if the character should be treated as a directory
31498 ** separator.
31499 */
31500 #ifndef winIsDirSep
31501 #  define winIsDirSep(a)                (((a) == '/') || ((a) == '\\'))
31502 #endif
31503 
31504 /*
31505 ** This macro is used when a local variable is set to a value that is
31506 ** [sometimes] not used by the code (e.g. via conditional compilation).
31507 */
31508 #ifndef UNUSED_VARIABLE_VALUE
31509 #  define UNUSED_VARIABLE_VALUE(x) (void)(x)
31510 #endif
31511 
31512 /*
31513 ** Returns the character that should be used as the directory separator.
31514 */
31515 #ifndef winGetDirSep
31516 #  define winGetDirSep()                '\\'
31517 #endif
31518 
31519 /*
31520 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
31521 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
31522 ** are not present in the header file)?
31523 */
31524 #if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
31525 /*
31526 ** Two of the file mapping APIs are different under WinRT.  Figure out which
31527 ** set we need.
31528 */
31529 #if SQLITE_OS_WINRT
31530 WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
31531         LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
31532 
31533 WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
31534 #else
31535 #if defined(SQLITE_WIN32_HAS_ANSI)
31536 WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
31537         DWORD, DWORD, DWORD, LPCSTR);
31538 #endif /* defined(SQLITE_WIN32_HAS_ANSI) */
31539 
31540 #if defined(SQLITE_WIN32_HAS_WIDE)
31541 WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
31542         DWORD, DWORD, DWORD, LPCWSTR);
31543 #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
31544 
31545 WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
31546 #endif /* SQLITE_OS_WINRT */
31547 
31548 /*
31549 ** This file mapping API is common to both Win32 and WinRT.
31550 */
31551 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
31552 #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
31553 
31554 /*
31555 ** Some Microsoft compilers lack this definition.
31556 */
31557 #ifndef INVALID_FILE_ATTRIBUTES
31558 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
31559 #endif
31560 
31561 #ifndef FILE_FLAG_MASK
31562 # define FILE_FLAG_MASK          (0xFF3C0000)
31563 #endif
31564 
31565 #ifndef FILE_ATTRIBUTE_MASK
31566 # define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
31567 #endif
31568 
31569 #ifndef SQLITE_OMIT_WAL
31570 /* Forward references to structures used for WAL */
31571 typedef struct winShm winShm;           /* A connection to shared-memory */
31572 typedef struct winShmNode winShmNode;   /* A region of shared-memory */
31573 #endif
31574 
31575 /*
31576 ** WinCE lacks native support for file locking so we have to fake it
31577 ** with some code of our own.
31578 */
31579 #if SQLITE_OS_WINCE
31580 typedef struct winceLock {
31581   int nReaders;       /* Number of reader locks obtained */
31582   BOOL bPending;      /* Indicates a pending lock has been obtained */
31583   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
31584   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
31585 } winceLock;
31586 #endif
31587 
31588 /*
31589 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
31590 ** portability layer.
31591 */
31592 typedef struct winFile winFile;
31593 struct winFile {
31594   const sqlite3_io_methods *pMethod; /*** Must be first ***/
31595   sqlite3_vfs *pVfs;      /* The VFS used to open this file */
31596   HANDLE h;               /* Handle for accessing the file */
31597   u8 locktype;            /* Type of lock currently held on this file */
31598   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
31599   u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
31600   DWORD lastErrno;        /* The Windows errno from the last I/O error */
31601 #ifndef SQLITE_OMIT_WAL
31602   winShm *pShm;           /* Instance of shared memory on this file */
31603 #endif
31604   const char *zPath;      /* Full pathname of this file */
31605   int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
31606 #if SQLITE_OS_WINCE
31607   LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
31608   HANDLE hMutex;          /* Mutex used to control access to shared lock */
31609   HANDLE hShared;         /* Shared memory segment used for locking */
31610   winceLock local;        /* Locks obtained by this instance of winFile */
31611   winceLock *shared;      /* Global shared lock memory for the file  */
31612 #endif
31613 #if SQLITE_MAX_MMAP_SIZE>0
31614   int nFetchOut;                /* Number of outstanding xFetch references */
31615   HANDLE hMap;                  /* Handle for accessing memory mapping */
31616   void *pMapRegion;             /* Area memory mapped */
31617   sqlite3_int64 mmapSize;       /* Usable size of mapped region */
31618   sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
31619   sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
31620 #endif
31621 };
31622 
31623 /*
31624 ** Allowed values for winFile.ctrlFlags
31625 */
31626 #define WINFILE_RDONLY          0x02   /* Connection is read only */
31627 #define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
31628 #define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
31629 
31630 /*
31631  * The size of the buffer used by sqlite3_win32_write_debug().
31632  */
31633 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
31634 #  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
31635 #endif
31636 
31637 /*
31638  * The value used with sqlite3_win32_set_directory() to specify that
31639  * the data directory should be changed.
31640  */
31641 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
31642 #  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
31643 #endif
31644 
31645 /*
31646  * The value used with sqlite3_win32_set_directory() to specify that
31647  * the temporary directory should be changed.
31648  */
31649 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
31650 #  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
31651 #endif
31652 
31653 /*
31654  * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
31655  * various Win32 API heap functions instead of our own.
31656  */
31657 #ifdef SQLITE_WIN32_MALLOC
31658 
31659 /*
31660  * If this is non-zero, an isolated heap will be created by the native Win32
31661  * allocator subsystem; otherwise, the default process heap will be used.  This
31662  * setting has no effect when compiling for WinRT.  By default, this is enabled
31663  * and an isolated heap will be created to store all allocated data.
31664  *
31665  ******************************************************************************
31666  * WARNING: It is important to note that when this setting is non-zero and the
31667  *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
31668  *          function), all data that was allocated using the isolated heap will
31669  *          be freed immediately and any attempt to access any of that freed
31670  *          data will almost certainly result in an immediate access violation.
31671  ******************************************************************************
31672  */
31673 #ifndef SQLITE_WIN32_HEAP_CREATE
31674 #  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
31675 #endif
31676 
31677 /*
31678  * The initial size of the Win32-specific heap.  This value may be zero.
31679  */
31680 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
31681 #  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
31682                                        (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
31683 #endif
31684 
31685 /*
31686  * The maximum size of the Win32-specific heap.  This value may be zero.
31687  */
31688 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
31689 #  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
31690 #endif
31691 
31692 /*
31693  * The extra flags to use in calls to the Win32 heap APIs.  This value may be
31694  * zero for the default behavior.
31695  */
31696 #ifndef SQLITE_WIN32_HEAP_FLAGS
31697 #  define SQLITE_WIN32_HEAP_FLAGS     (0)
31698 #endif
31699 
31700 
31701 /*
31702 ** The winMemData structure stores information required by the Win32-specific
31703 ** sqlite3_mem_methods implementation.
31704 */
31705 typedef struct winMemData winMemData;
31706 struct winMemData {
31707 #ifndef NDEBUG
31708   u32 magic1;   /* Magic number to detect structure corruption. */
31709 #endif
31710   HANDLE hHeap; /* The handle to our heap. */
31711   BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
31712 #ifndef NDEBUG
31713   u32 magic2;   /* Magic number to detect structure corruption. */
31714 #endif
31715 };
31716 
31717 #ifndef NDEBUG
31718 #define WINMEM_MAGIC1     0x42b2830b
31719 #define WINMEM_MAGIC2     0xbd4d7cf4
31720 #endif
31721 
31722 static struct winMemData win_mem_data = {
31723 #ifndef NDEBUG
31724   WINMEM_MAGIC1,
31725 #endif
31726   NULL, FALSE
31727 #ifndef NDEBUG
31728   ,WINMEM_MAGIC2
31729 #endif
31730 };
31731 
31732 #ifndef NDEBUG
31733 #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
31734 #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
31735 #define winMemAssertMagic()  winMemAssertMagic1(); winMemAssertMagic2();
31736 #else
31737 #define winMemAssertMagic()
31738 #endif
31739 
31740 #define winMemGetDataPtr()  &win_mem_data
31741 #define winMemGetHeap()     win_mem_data.hHeap
31742 #define winMemGetOwned()    win_mem_data.bOwned
31743 
31744 static void *winMemMalloc(int nBytes);
31745 static void winMemFree(void *pPrior);
31746 static void *winMemRealloc(void *pPrior, int nBytes);
31747 static int winMemSize(void *p);
31748 static int winMemRoundup(int n);
31749 static int winMemInit(void *pAppData);
31750 static void winMemShutdown(void *pAppData);
31751 
31752 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
31753 #endif /* SQLITE_WIN32_MALLOC */
31754 
31755 /*
31756 ** The following variable is (normally) set once and never changes
31757 ** thereafter.  It records whether the operating system is Win9x
31758 ** or WinNT.
31759 **
31760 ** 0:   Operating system unknown.
31761 ** 1:   Operating system is Win9x.
31762 ** 2:   Operating system is WinNT.
31763 **
31764 ** In order to facilitate testing on a WinNT system, the test fixture
31765 ** can manually set this value to 1 to emulate Win98 behavior.
31766 */
31767 #ifdef SQLITE_TEST
31768 SQLITE_API int sqlite3_os_type = 0;
31769 #elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
31770       defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
31771 static int sqlite3_os_type = 0;
31772 #endif
31773 
31774 #ifndef SYSCALL
31775 #  define SYSCALL sqlite3_syscall_ptr
31776 #endif
31777 
31778 /*
31779 ** This function is not available on Windows CE or WinRT.
31780  */
31781 
31782 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
31783 #  define osAreFileApisANSI()       1
31784 #endif
31785 
31786 /*
31787 ** Many system calls are accessed through pointer-to-functions so that
31788 ** they may be overridden at runtime to facilitate fault injection during
31789 ** testing and sandboxing.  The following array holds the names and pointers
31790 ** to all overrideable system calls.
31791 */
31792 static struct win_syscall {
31793   const char *zName;            /* Name of the system call */
31794   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
31795   sqlite3_syscall_ptr pDefault; /* Default value */
31796 } aSyscall[] = {
31797 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
31798   { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
31799 #else
31800   { "AreFileApisANSI",         (SYSCALL)0,                       0 },
31801 #endif
31802 
31803 #ifndef osAreFileApisANSI
31804 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
31805 #endif
31806 
31807 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
31808   { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
31809 #else
31810   { "CharLowerW",              (SYSCALL)0,                       0 },
31811 #endif
31812 
31813 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
31814 
31815 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
31816   { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
31817 #else
31818   { "CharUpperW",              (SYSCALL)0,                       0 },
31819 #endif
31820 
31821 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
31822 
31823   { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
31824 
31825 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
31826 
31827 #if defined(SQLITE_WIN32_HAS_ANSI)
31828   { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
31829 #else
31830   { "CreateFileA",             (SYSCALL)0,                       0 },
31831 #endif
31832 
31833 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
31834         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
31835 
31836 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31837   { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
31838 #else
31839   { "CreateFileW",             (SYSCALL)0,                       0 },
31840 #endif
31841 
31842 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
31843         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
31844 
31845 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
31846         !defined(SQLITE_OMIT_WAL))
31847   { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
31848 #else
31849   { "CreateFileMappingA",      (SYSCALL)0,                       0 },
31850 #endif
31851 
31852 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
31853         DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
31854 
31855 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
31856         !defined(SQLITE_OMIT_WAL))
31857   { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
31858 #else
31859   { "CreateFileMappingW",      (SYSCALL)0,                       0 },
31860 #endif
31861 
31862 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
31863         DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
31864 
31865 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31866   { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
31867 #else
31868   { "CreateMutexW",            (SYSCALL)0,                       0 },
31869 #endif
31870 
31871 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
31872         LPCWSTR))aSyscall[8].pCurrent)
31873 
31874 #if defined(SQLITE_WIN32_HAS_ANSI)
31875   { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
31876 #else
31877   { "DeleteFileA",             (SYSCALL)0,                       0 },
31878 #endif
31879 
31880 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
31881 
31882 #if defined(SQLITE_WIN32_HAS_WIDE)
31883   { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
31884 #else
31885   { "DeleteFileW",             (SYSCALL)0,                       0 },
31886 #endif
31887 
31888 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
31889 
31890 #if SQLITE_OS_WINCE
31891   { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
31892 #else
31893   { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
31894 #endif
31895 
31896 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
31897         LPFILETIME))aSyscall[11].pCurrent)
31898 
31899 #if SQLITE_OS_WINCE
31900   { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
31901 #else
31902   { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
31903 #endif
31904 
31905 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
31906         LPSYSTEMTIME))aSyscall[12].pCurrent)
31907 
31908   { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
31909 
31910 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
31911 
31912 #if defined(SQLITE_WIN32_HAS_ANSI)
31913   { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
31914 #else
31915   { "FormatMessageA",          (SYSCALL)0,                       0 },
31916 #endif
31917 
31918 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
31919         DWORD,va_list*))aSyscall[14].pCurrent)
31920 
31921 #if defined(SQLITE_WIN32_HAS_WIDE)
31922   { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
31923 #else
31924   { "FormatMessageW",          (SYSCALL)0,                       0 },
31925 #endif
31926 
31927 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
31928         DWORD,va_list*))aSyscall[15].pCurrent)
31929 
31930 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
31931   { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
31932 #else
31933   { "FreeLibrary",             (SYSCALL)0,                       0 },
31934 #endif
31935 
31936 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
31937 
31938   { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
31939 
31940 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
31941 
31942 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
31943   { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
31944 #else
31945   { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
31946 #endif
31947 
31948 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
31949         LPDWORD))aSyscall[18].pCurrent)
31950 
31951 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31952   { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
31953 #else
31954   { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
31955 #endif
31956 
31957 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
31958         LPDWORD))aSyscall[19].pCurrent)
31959 
31960 #if defined(SQLITE_WIN32_HAS_ANSI)
31961   { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
31962 #else
31963   { "GetFileAttributesA",      (SYSCALL)0,                       0 },
31964 #endif
31965 
31966 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
31967 
31968 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31969   { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
31970 #else
31971   { "GetFileAttributesW",      (SYSCALL)0,                       0 },
31972 #endif
31973 
31974 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
31975 
31976 #if defined(SQLITE_WIN32_HAS_WIDE)
31977   { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
31978 #else
31979   { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
31980 #endif
31981 
31982 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
31983         LPVOID))aSyscall[22].pCurrent)
31984 
31985 #if !SQLITE_OS_WINRT
31986   { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
31987 #else
31988   { "GetFileSize",             (SYSCALL)0,                       0 },
31989 #endif
31990 
31991 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
31992 
31993 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
31994   { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
31995 #else
31996   { "GetFullPathNameA",        (SYSCALL)0,                       0 },
31997 #endif
31998 
31999 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
32000         LPSTR*))aSyscall[24].pCurrent)
32001 
32002 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
32003   { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
32004 #else
32005   { "GetFullPathNameW",        (SYSCALL)0,                       0 },
32006 #endif
32007 
32008 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
32009         LPWSTR*))aSyscall[25].pCurrent)
32010 
32011   { "GetLastError",            (SYSCALL)GetLastError,            0 },
32012 
32013 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
32014 
32015 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
32016 #if SQLITE_OS_WINCE
32017   /* The GetProcAddressA() routine is only available on Windows CE. */
32018   { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
32019 #else
32020   /* All other Windows platforms expect GetProcAddress() to take
32021   ** an ANSI string regardless of the _UNICODE setting */
32022   { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
32023 #endif
32024 #else
32025   { "GetProcAddressA",         (SYSCALL)0,                       0 },
32026 #endif
32027 
32028 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
32029         LPCSTR))aSyscall[27].pCurrent)
32030 
32031 #if !SQLITE_OS_WINRT
32032   { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
32033 #else
32034   { "GetSystemInfo",           (SYSCALL)0,                       0 },
32035 #endif
32036 
32037 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
32038 
32039   { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
32040 
32041 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
32042 
32043 #if !SQLITE_OS_WINCE
32044   { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
32045 #else
32046   { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
32047 #endif
32048 
32049 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
32050         LPFILETIME))aSyscall[30].pCurrent)
32051 
32052 #if defined(SQLITE_WIN32_HAS_ANSI)
32053   { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
32054 #else
32055   { "GetTempPathA",            (SYSCALL)0,                       0 },
32056 #endif
32057 
32058 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
32059 
32060 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
32061   { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
32062 #else
32063   { "GetTempPathW",            (SYSCALL)0,                       0 },
32064 #endif
32065 
32066 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
32067 
32068 #if !SQLITE_OS_WINRT
32069   { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
32070 #else
32071   { "GetTickCount",            (SYSCALL)0,                       0 },
32072 #endif
32073 
32074 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
32075 
32076 #if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \
32077         SQLITE_WIN32_GETVERSIONEX
32078   { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
32079 #else
32080   { "GetVersionExA",           (SYSCALL)0,                       0 },
32081 #endif
32082 
32083 #define osGetVersionExA ((BOOL(WINAPI*)( \
32084         LPOSVERSIONINFOA))aSyscall[34].pCurrent)
32085 
32086 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
32087         defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
32088   { "GetVersionExW",           (SYSCALL)GetVersionExW,           0 },
32089 #else
32090   { "GetVersionExW",           (SYSCALL)0,                       0 },
32091 #endif
32092 
32093 #define osGetVersionExW ((BOOL(WINAPI*)( \
32094         LPOSVERSIONINFOW))aSyscall[35].pCurrent)
32095 
32096   { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
32097 
32098 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
32099         SIZE_T))aSyscall[36].pCurrent)
32100 
32101 #if !SQLITE_OS_WINRT
32102   { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
32103 #else
32104   { "HeapCreate",              (SYSCALL)0,                       0 },
32105 #endif
32106 
32107 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
32108         SIZE_T))aSyscall[37].pCurrent)
32109 
32110 #if !SQLITE_OS_WINRT
32111   { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
32112 #else
32113   { "HeapDestroy",             (SYSCALL)0,                       0 },
32114 #endif
32115 
32116 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
32117 
32118   { "HeapFree",                (SYSCALL)HeapFree,                0 },
32119 
32120 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
32121 
32122   { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
32123 
32124 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
32125         SIZE_T))aSyscall[40].pCurrent)
32126 
32127   { "HeapSize",                (SYSCALL)HeapSize,                0 },
32128 
32129 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
32130         LPCVOID))aSyscall[41].pCurrent)
32131 
32132 #if !SQLITE_OS_WINRT
32133   { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
32134 #else
32135   { "HeapValidate",            (SYSCALL)0,                       0 },
32136 #endif
32137 
32138 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
32139         LPCVOID))aSyscall[42].pCurrent)
32140 
32141 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32142   { "HeapCompact",             (SYSCALL)HeapCompact,             0 },
32143 #else
32144   { "HeapCompact",             (SYSCALL)0,                       0 },
32145 #endif
32146 
32147 #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
32148 
32149 #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
32150   { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
32151 #else
32152   { "LoadLibraryA",            (SYSCALL)0,                       0 },
32153 #endif
32154 
32155 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
32156 
32157 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
32158         !defined(SQLITE_OMIT_LOAD_EXTENSION)
32159   { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
32160 #else
32161   { "LoadLibraryW",            (SYSCALL)0,                       0 },
32162 #endif
32163 
32164 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
32165 
32166 #if !SQLITE_OS_WINRT
32167   { "LocalFree",               (SYSCALL)LocalFree,               0 },
32168 #else
32169   { "LocalFree",               (SYSCALL)0,                       0 },
32170 #endif
32171 
32172 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
32173 
32174 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32175   { "LockFile",                (SYSCALL)LockFile,                0 },
32176 #else
32177   { "LockFile",                (SYSCALL)0,                       0 },
32178 #endif
32179 
32180 #ifndef osLockFile
32181 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32182         DWORD))aSyscall[47].pCurrent)
32183 #endif
32184 
32185 #if !SQLITE_OS_WINCE
32186   { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
32187 #else
32188   { "LockFileEx",              (SYSCALL)0,                       0 },
32189 #endif
32190 
32191 #ifndef osLockFileEx
32192 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
32193         LPOVERLAPPED))aSyscall[48].pCurrent)
32194 #endif
32195 
32196 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
32197   { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
32198 #else
32199   { "MapViewOfFile",           (SYSCALL)0,                       0 },
32200 #endif
32201 
32202 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32203         SIZE_T))aSyscall[49].pCurrent)
32204 
32205   { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
32206 
32207 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
32208         int))aSyscall[50].pCurrent)
32209 
32210   { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
32211 
32212 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
32213         LARGE_INTEGER*))aSyscall[51].pCurrent)
32214 
32215   { "ReadFile",                (SYSCALL)ReadFile,                0 },
32216 
32217 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
32218         LPOVERLAPPED))aSyscall[52].pCurrent)
32219 
32220   { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
32221 
32222 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
32223 
32224 #if !SQLITE_OS_WINRT
32225   { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
32226 #else
32227   { "SetFilePointer",          (SYSCALL)0,                       0 },
32228 #endif
32229 
32230 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
32231         DWORD))aSyscall[54].pCurrent)
32232 
32233 #if !SQLITE_OS_WINRT
32234   { "Sleep",                   (SYSCALL)Sleep,                   0 },
32235 #else
32236   { "Sleep",                   (SYSCALL)0,                       0 },
32237 #endif
32238 
32239 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
32240 
32241   { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
32242 
32243 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
32244         LPFILETIME))aSyscall[56].pCurrent)
32245 
32246 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32247   { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
32248 #else
32249   { "UnlockFile",              (SYSCALL)0,                       0 },
32250 #endif
32251 
32252 #ifndef osUnlockFile
32253 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32254         DWORD))aSyscall[57].pCurrent)
32255 #endif
32256 
32257 #if !SQLITE_OS_WINCE
32258   { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
32259 #else
32260   { "UnlockFileEx",            (SYSCALL)0,                       0 },
32261 #endif
32262 
32263 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32264         LPOVERLAPPED))aSyscall[58].pCurrent)
32265 
32266 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
32267   { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
32268 #else
32269   { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
32270 #endif
32271 
32272 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
32273 
32274   { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
32275 
32276 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
32277         LPCSTR,LPBOOL))aSyscall[60].pCurrent)
32278 
32279   { "WriteFile",               (SYSCALL)WriteFile,               0 },
32280 
32281 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
32282         LPOVERLAPPED))aSyscall[61].pCurrent)
32283 
32284 #if SQLITE_OS_WINRT
32285   { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
32286 #else
32287   { "CreateEventExW",          (SYSCALL)0,                       0 },
32288 #endif
32289 
32290 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
32291         DWORD,DWORD))aSyscall[62].pCurrent)
32292 
32293 #if !SQLITE_OS_WINRT
32294   { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
32295 #else
32296   { "WaitForSingleObject",     (SYSCALL)0,                       0 },
32297 #endif
32298 
32299 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
32300         DWORD))aSyscall[63].pCurrent)
32301 
32302 #if SQLITE_OS_WINRT
32303   { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
32304 #else
32305   { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
32306 #endif
32307 
32308 #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
32309         BOOL))aSyscall[64].pCurrent)
32310 
32311 #if SQLITE_OS_WINRT
32312   { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
32313 #else
32314   { "SetFilePointerEx",        (SYSCALL)0,                       0 },
32315 #endif
32316 
32317 #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
32318         PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
32319 
32320 #if SQLITE_OS_WINRT
32321   { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
32322 #else
32323   { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
32324 #endif
32325 
32326 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
32327         FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
32328 
32329 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
32330   { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
32331 #else
32332   { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
32333 #endif
32334 
32335 #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
32336         SIZE_T))aSyscall[67].pCurrent)
32337 
32338 #if SQLITE_OS_WINRT
32339   { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
32340 #else
32341   { "CreateFile2",             (SYSCALL)0,                       0 },
32342 #endif
32343 
32344 #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
32345         LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
32346 
32347 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
32348   { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
32349 #else
32350   { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
32351 #endif
32352 
32353 #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
32354         DWORD))aSyscall[69].pCurrent)
32355 
32356 #if SQLITE_OS_WINRT
32357   { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
32358 #else
32359   { "GetTickCount64",          (SYSCALL)0,                       0 },
32360 #endif
32361 
32362 #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
32363 
32364 #if SQLITE_OS_WINRT
32365   { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
32366 #else
32367   { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
32368 #endif
32369 
32370 #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
32371         LPSYSTEM_INFO))aSyscall[71].pCurrent)
32372 
32373 #if defined(SQLITE_WIN32_HAS_ANSI)
32374   { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
32375 #else
32376   { "OutputDebugStringA",      (SYSCALL)0,                       0 },
32377 #endif
32378 
32379 #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
32380 
32381 #if defined(SQLITE_WIN32_HAS_WIDE)
32382   { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
32383 #else
32384   { "OutputDebugStringW",      (SYSCALL)0,                       0 },
32385 #endif
32386 
32387 #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
32388 
32389   { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
32390 
32391 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
32392 
32393 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
32394   { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
32395 #else
32396   { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
32397 #endif
32398 
32399 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
32400         LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
32401 
32402 }; /* End of the overrideable system calls */
32403 
32404 /*
32405 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
32406 ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
32407 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
32408 ** system call named zName.
32409 */
32410 static int winSetSystemCall(
32411   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
32412   const char *zName,            /* Name of system call to override */
32413   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
32414 ){
32415   unsigned int i;
32416   int rc = SQLITE_NOTFOUND;
32417 
32418   UNUSED_PARAMETER(pNotUsed);
32419   if( zName==0 ){
32420     /* If no zName is given, restore all system calls to their default
32421     ** settings and return NULL
32422     */
32423     rc = SQLITE_OK;
32424     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32425       if( aSyscall[i].pDefault ){
32426         aSyscall[i].pCurrent = aSyscall[i].pDefault;
32427       }
32428     }
32429   }else{
32430     /* If zName is specified, operate on only the one system call
32431     ** specified.
32432     */
32433     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32434       if( strcmp(zName, aSyscall[i].zName)==0 ){
32435         if( aSyscall[i].pDefault==0 ){
32436           aSyscall[i].pDefault = aSyscall[i].pCurrent;
32437         }
32438         rc = SQLITE_OK;
32439         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
32440         aSyscall[i].pCurrent = pNewFunc;
32441         break;
32442       }
32443     }
32444   }
32445   return rc;
32446 }
32447 
32448 /*
32449 ** Return the value of a system call.  Return NULL if zName is not a
32450 ** recognized system call name.  NULL is also returned if the system call
32451 ** is currently undefined.
32452 */
32453 static sqlite3_syscall_ptr winGetSystemCall(
32454   sqlite3_vfs *pNotUsed,
32455   const char *zName
32456 ){
32457   unsigned int i;
32458 
32459   UNUSED_PARAMETER(pNotUsed);
32460   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32461     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
32462   }
32463   return 0;
32464 }
32465 
32466 /*
32467 ** Return the name of the first system call after zName.  If zName==NULL
32468 ** then return the name of the first system call.  Return NULL if zName
32469 ** is the last system call or if zName is not the name of a valid
32470 ** system call.
32471 */
32472 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
32473   int i = -1;
32474 
32475   UNUSED_PARAMETER(p);
32476   if( zName ){
32477     for(i=0; i<ArraySize(aSyscall)-1; i++){
32478       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
32479     }
32480   }
32481   for(i++; i<ArraySize(aSyscall); i++){
32482     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
32483   }
32484   return 0;
32485 }
32486 
32487 #ifdef SQLITE_WIN32_MALLOC
32488 /*
32489 ** If a Win32 native heap has been configured, this function will attempt to
32490 ** compact it.  Upon success, SQLITE_OK will be returned.  Upon failure, one
32491 ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned.  The
32492 ** "pnLargest" argument, if non-zero, will be used to return the size of the
32493 ** largest committed free block in the heap, in bytes.
32494 */
32495 SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
32496   int rc = SQLITE_OK;
32497   UINT nLargest = 0;
32498   HANDLE hHeap;
32499 
32500   winMemAssertMagic();
32501   hHeap = winMemGetHeap();
32502   assert( hHeap!=0 );
32503   assert( hHeap!=INVALID_HANDLE_VALUE );
32504 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32505   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32506 #endif
32507 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32508   if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
32509     DWORD lastErrno = osGetLastError();
32510     if( lastErrno==NO_ERROR ){
32511       sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
32512                   (void*)hHeap);
32513       rc = SQLITE_NOMEM;
32514     }else{
32515       sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
32516                   osGetLastError(), (void*)hHeap);
32517       rc = SQLITE_ERROR;
32518     }
32519   }
32520 #else
32521   sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
32522               (void*)hHeap);
32523   rc = SQLITE_NOTFOUND;
32524 #endif
32525   if( pnLargest ) *pnLargest = nLargest;
32526   return rc;
32527 }
32528 
32529 /*
32530 ** If a Win32 native heap has been configured, this function will attempt to
32531 ** destroy and recreate it.  If the Win32 native heap is not isolated and/or
32532 ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
32533 ** be returned and no changes will be made to the Win32 native heap.
32534 */
32535 SQLITE_API int sqlite3_win32_reset_heap(){
32536   int rc;
32537   MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
32538   MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
32539   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
32540   MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
32541   sqlite3_mutex_enter(pMaster);
32542   sqlite3_mutex_enter(pMem);
32543   winMemAssertMagic();
32544   if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
32545     /*
32546     ** At this point, there should be no outstanding memory allocations on
32547     ** the heap.  Also, since both the master and memsys locks are currently
32548     ** being held by us, no other function (i.e. from another thread) should
32549     ** be able to even access the heap.  Attempt to destroy and recreate our
32550     ** isolated Win32 native heap now.
32551     */
32552     assert( winMemGetHeap()!=NULL );
32553     assert( winMemGetOwned() );
32554     assert( sqlite3_memory_used()==0 );
32555     winMemShutdown(winMemGetDataPtr());
32556     assert( winMemGetHeap()==NULL );
32557     assert( !winMemGetOwned() );
32558     assert( sqlite3_memory_used()==0 );
32559     rc = winMemInit(winMemGetDataPtr());
32560     assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
32561     assert( rc!=SQLITE_OK || winMemGetOwned() );
32562     assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
32563   }else{
32564     /*
32565     ** The Win32 native heap cannot be modified because it may be in use.
32566     */
32567     rc = SQLITE_BUSY;
32568   }
32569   sqlite3_mutex_leave(pMem);
32570   sqlite3_mutex_leave(pMaster);
32571   return rc;
32572 }
32573 #endif /* SQLITE_WIN32_MALLOC */
32574 
32575 /*
32576 ** This function outputs the specified (ANSI) string to the Win32 debugger
32577 ** (if available).
32578 */
32579 
32580 SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
32581   char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
32582   int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
32583   if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
32584   assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
32585 #if defined(SQLITE_WIN32_HAS_ANSI)
32586   if( nMin>0 ){
32587     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
32588     memcpy(zDbgBuf, zBuf, nMin);
32589     osOutputDebugStringA(zDbgBuf);
32590   }else{
32591     osOutputDebugStringA(zBuf);
32592   }
32593 #elif defined(SQLITE_WIN32_HAS_WIDE)
32594   memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
32595   if ( osMultiByteToWideChar(
32596           osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
32597           nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
32598     return;
32599   }
32600   osOutputDebugStringW((LPCWSTR)zDbgBuf);
32601 #else
32602   if( nMin>0 ){
32603     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
32604     memcpy(zDbgBuf, zBuf, nMin);
32605     fprintf(stderr, "%s", zDbgBuf);
32606   }else{
32607     fprintf(stderr, "%s", zBuf);
32608   }
32609 #endif
32610 }
32611 
32612 /*
32613 ** The following routine suspends the current thread for at least ms
32614 ** milliseconds.  This is equivalent to the Win32 Sleep() interface.
32615 */
32616 #if SQLITE_OS_WINRT
32617 static HANDLE sleepObj = NULL;
32618 #endif
32619 
32620 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
32621 #if SQLITE_OS_WINRT
32622   if ( sleepObj==NULL ){
32623     sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
32624                                 SYNCHRONIZE);
32625   }
32626   assert( sleepObj!=NULL );
32627   osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
32628 #else
32629   osSleep(milliseconds);
32630 #endif
32631 }
32632 
32633 /*
32634 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
32635 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
32636 **
32637 ** Here is an interesting observation:  Win95, Win98, and WinME lack
32638 ** the LockFileEx() API.  But we can still statically link against that
32639 ** API as long as we don't call it when running Win95/98/ME.  A call to
32640 ** this routine is used to determine if the host is Win95/98/ME or
32641 ** WinNT/2K/XP so that we will know whether or not we can safely call
32642 ** the LockFileEx() API.
32643 */
32644 
32645 #if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
32646 # define osIsNT()  (1)
32647 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
32648 # define osIsNT()  (1)
32649 #elif !defined(SQLITE_WIN32_HAS_WIDE)
32650 # define osIsNT()  (0)
32651 #else
32652   static int osIsNT(void){
32653     if( sqlite3_os_type==0 ){
32654 #if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
32655       OSVERSIONINFOW sInfo;
32656       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32657       osGetVersionExW(&sInfo);
32658 #else
32659       OSVERSIONINFOA sInfo;
32660       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32661       osGetVersionExA(&sInfo);
32662 #endif
32663       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
32664     }
32665     return sqlite3_os_type==2;
32666   }
32667 #endif
32668 
32669 #ifdef SQLITE_WIN32_MALLOC
32670 /*
32671 ** Allocate nBytes of memory.
32672 */
32673 static void *winMemMalloc(int nBytes){
32674   HANDLE hHeap;
32675   void *p;
32676 
32677   winMemAssertMagic();
32678   hHeap = winMemGetHeap();
32679   assert( hHeap!=0 );
32680   assert( hHeap!=INVALID_HANDLE_VALUE );
32681 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32682   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32683 #endif
32684   assert( nBytes>=0 );
32685   p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32686   if( !p ){
32687     sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
32688                 nBytes, osGetLastError(), (void*)hHeap);
32689   }
32690   return p;
32691 }
32692 
32693 /*
32694 ** Free memory.
32695 */
32696 static void winMemFree(void *pPrior){
32697   HANDLE hHeap;
32698 
32699   winMemAssertMagic();
32700   hHeap = winMemGetHeap();
32701   assert( hHeap!=0 );
32702   assert( hHeap!=INVALID_HANDLE_VALUE );
32703 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32704   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32705 #endif
32706   if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
32707   if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
32708     sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
32709                 pPrior, osGetLastError(), (void*)hHeap);
32710   }
32711 }
32712 
32713 /*
32714 ** Change the size of an existing memory allocation
32715 */
32716 static void *winMemRealloc(void *pPrior, int nBytes){
32717   HANDLE hHeap;
32718   void *p;
32719 
32720   winMemAssertMagic();
32721   hHeap = winMemGetHeap();
32722   assert( hHeap!=0 );
32723   assert( hHeap!=INVALID_HANDLE_VALUE );
32724 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32725   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32726 #endif
32727   assert( nBytes>=0 );
32728   if( !pPrior ){
32729     p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32730   }else{
32731     p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
32732   }
32733   if( !p ){
32734     sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
32735                 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
32736                 (void*)hHeap);
32737   }
32738   return p;
32739 }
32740 
32741 /*
32742 ** Return the size of an outstanding allocation, in bytes.
32743 */
32744 static int winMemSize(void *p){
32745   HANDLE hHeap;
32746   SIZE_T n;
32747 
32748   winMemAssertMagic();
32749   hHeap = winMemGetHeap();
32750   assert( hHeap!=0 );
32751   assert( hHeap!=INVALID_HANDLE_VALUE );
32752 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32753   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, p) );
32754 #endif
32755   if( !p ) return 0;
32756   n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
32757   if( n==(SIZE_T)-1 ){
32758     sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
32759                 p, osGetLastError(), (void*)hHeap);
32760     return 0;
32761   }
32762   return (int)n;
32763 }
32764 
32765 /*
32766 ** Round up a request size to the next valid allocation size.
32767 */
32768 static int winMemRoundup(int n){
32769   return n;
32770 }
32771 
32772 /*
32773 ** Initialize this module.
32774 */
32775 static int winMemInit(void *pAppData){
32776   winMemData *pWinMemData = (winMemData *)pAppData;
32777 
32778   if( !pWinMemData ) return SQLITE_ERROR;
32779   assert( pWinMemData->magic1==WINMEM_MAGIC1 );
32780   assert( pWinMemData->magic2==WINMEM_MAGIC2 );
32781 
32782 #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
32783   if( !pWinMemData->hHeap ){
32784     DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
32785     DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
32786     if( dwMaximumSize==0 ){
32787       dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
32788     }else if( dwInitialSize>dwMaximumSize ){
32789       dwInitialSize = dwMaximumSize;
32790     }
32791     pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
32792                                       dwInitialSize, dwMaximumSize);
32793     if( !pWinMemData->hHeap ){
32794       sqlite3_log(SQLITE_NOMEM,
32795           "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
32796           osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
32797           dwMaximumSize);
32798       return SQLITE_NOMEM;
32799     }
32800     pWinMemData->bOwned = TRUE;
32801     assert( pWinMemData->bOwned );
32802   }
32803 #else
32804   pWinMemData->hHeap = osGetProcessHeap();
32805   if( !pWinMemData->hHeap ){
32806     sqlite3_log(SQLITE_NOMEM,
32807         "failed to GetProcessHeap (%lu)", osGetLastError());
32808     return SQLITE_NOMEM;
32809   }
32810   pWinMemData->bOwned = FALSE;
32811   assert( !pWinMemData->bOwned );
32812 #endif
32813   assert( pWinMemData->hHeap!=0 );
32814   assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
32815 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32816   assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32817 #endif
32818   return SQLITE_OK;
32819 }
32820 
32821 /*
32822 ** Deinitialize this module.
32823 */
32824 static void winMemShutdown(void *pAppData){
32825   winMemData *pWinMemData = (winMemData *)pAppData;
32826 
32827   if( !pWinMemData ) return;
32828   assert( pWinMemData->magic1==WINMEM_MAGIC1 );
32829   assert( pWinMemData->magic2==WINMEM_MAGIC2 );
32830 
32831   if( pWinMemData->hHeap ){
32832     assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
32833 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32834     assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32835 #endif
32836     if( pWinMemData->bOwned ){
32837       if( !osHeapDestroy(pWinMemData->hHeap) ){
32838         sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
32839                     osGetLastError(), (void*)pWinMemData->hHeap);
32840       }
32841       pWinMemData->bOwned = FALSE;
32842     }
32843     pWinMemData->hHeap = NULL;
32844   }
32845 }
32846 
32847 /*
32848 ** Populate the low-level memory allocation function pointers in
32849 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
32850 ** arguments specify the block of memory to manage.
32851 **
32852 ** This routine is only called by sqlite3_config(), and therefore
32853 ** is not required to be threadsafe (it is not).
32854 */
32855 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
32856   static const sqlite3_mem_methods winMemMethods = {
32857     winMemMalloc,
32858     winMemFree,
32859     winMemRealloc,
32860     winMemSize,
32861     winMemRoundup,
32862     winMemInit,
32863     winMemShutdown,
32864     &win_mem_data
32865   };
32866   return &winMemMethods;
32867 }
32868 
32869 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
32870   sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
32871 }
32872 #endif /* SQLITE_WIN32_MALLOC */
32873 
32874 /*
32875 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
32876 **
32877 ** Space to hold the returned string is obtained from malloc.
32878 */
32879 static LPWSTR winUtf8ToUnicode(const char *zFilename){
32880   int nChar;
32881   LPWSTR zWideFilename;
32882 
32883   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32884   if( nChar==0 ){
32885     return 0;
32886   }
32887   zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
32888   if( zWideFilename==0 ){
32889     return 0;
32890   }
32891   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
32892                                 nChar);
32893   if( nChar==0 ){
32894     sqlite3_free(zWideFilename);
32895     zWideFilename = 0;
32896   }
32897   return zWideFilename;
32898 }
32899 
32900 /*
32901 ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
32902 ** obtained from sqlite3_malloc().
32903 */
32904 static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
32905   int nByte;
32906   char *zFilename;
32907 
32908   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32909   if( nByte == 0 ){
32910     return 0;
32911   }
32912   zFilename = sqlite3MallocZero( nByte );
32913   if( zFilename==0 ){
32914     return 0;
32915   }
32916   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
32917                                 0, 0);
32918   if( nByte == 0 ){
32919     sqlite3_free(zFilename);
32920     zFilename = 0;
32921   }
32922   return zFilename;
32923 }
32924 
32925 /*
32926 ** Convert an ANSI string to Microsoft Unicode, based on the
32927 ** current codepage settings for file apis.
32928 **
32929 ** Space to hold the returned string is obtained
32930 ** from sqlite3_malloc.
32931 */
32932 static LPWSTR winMbcsToUnicode(const char *zFilename){
32933   int nByte;
32934   LPWSTR zMbcsFilename;
32935   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32936 
32937   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
32938                                 0)*sizeof(WCHAR);
32939   if( nByte==0 ){
32940     return 0;
32941   }
32942   zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
32943   if( zMbcsFilename==0 ){
32944     return 0;
32945   }
32946   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
32947                                 nByte);
32948   if( nByte==0 ){
32949     sqlite3_free(zMbcsFilename);
32950     zMbcsFilename = 0;
32951   }
32952   return zMbcsFilename;
32953 }
32954 
32955 /*
32956 ** Convert Microsoft Unicode to multi-byte character string, based on the
32957 ** user's ANSI codepage.
32958 **
32959 ** Space to hold the returned string is obtained from
32960 ** sqlite3_malloc().
32961 */
32962 static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
32963   int nByte;
32964   char *zFilename;
32965   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32966 
32967   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
32968   if( nByte == 0 ){
32969     return 0;
32970   }
32971   zFilename = sqlite3MallocZero( nByte );
32972   if( zFilename==0 ){
32973     return 0;
32974   }
32975   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
32976                                 nByte, 0, 0);
32977   if( nByte == 0 ){
32978     sqlite3_free(zFilename);
32979     zFilename = 0;
32980   }
32981   return zFilename;
32982 }
32983 
32984 /*
32985 ** Convert multibyte character string to UTF-8.  Space to hold the
32986 ** returned string is obtained from sqlite3_malloc().
32987 */
32988 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32989   char *zFilenameUtf8;
32990   LPWSTR zTmpWide;
32991 
32992   zTmpWide = winMbcsToUnicode(zFilename);
32993   if( zTmpWide==0 ){
32994     return 0;
32995   }
32996   zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
32997   sqlite3_free(zTmpWide);
32998   return zFilenameUtf8;
32999 }
33000 
33001 /*
33002 ** Convert UTF-8 to multibyte character string.  Space to hold the
33003 ** returned string is obtained from sqlite3_malloc().
33004 */
33005 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
33006   char *zFilenameMbcs;
33007   LPWSTR zTmpWide;
33008 
33009   zTmpWide = winUtf8ToUnicode(zFilename);
33010   if( zTmpWide==0 ){
33011     return 0;
33012   }
33013   zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
33014   sqlite3_free(zTmpWide);
33015   return zFilenameMbcs;
33016 }
33017 
33018 /*
33019 ** This function sets the data directory or the temporary directory based on
33020 ** the provided arguments.  The type argument must be 1 in order to set the
33021 ** data directory or 2 in order to set the temporary directory.  The zValue
33022 ** argument is the name of the directory to use.  The return value will be
33023 ** SQLITE_OK if successful.
33024 */
33025 SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
33026   char **ppDirectory = 0;
33027 #ifndef SQLITE_OMIT_AUTOINIT
33028   int rc = sqlite3_initialize();
33029   if( rc ) return rc;
33030 #endif
33031   if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
33032     ppDirectory = &sqlite3_data_directory;
33033   }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
33034     ppDirectory = &sqlite3_temp_directory;
33035   }
33036   assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
33037           || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
33038   );
33039   assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
33040   if( ppDirectory ){
33041     char *zValueUtf8 = 0;
33042     if( zValue && zValue[0] ){
33043       zValueUtf8 = winUnicodeToUtf8(zValue);
33044       if ( zValueUtf8==0 ){
33045         return SQLITE_NOMEM;
33046       }
33047     }
33048     sqlite3_free(*ppDirectory);
33049     *ppDirectory = zValueUtf8;
33050     return SQLITE_OK;
33051   }
33052   return SQLITE_ERROR;
33053 }
33054 
33055 /*
33056 ** The return value of winGetLastErrorMsg
33057 ** is zero if the error message fits in the buffer, or non-zero
33058 ** otherwise (if the message was truncated).
33059 */
33060 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
33061   /* FormatMessage returns 0 on failure.  Otherwise it
33062   ** returns the number of TCHARs written to the output
33063   ** buffer, excluding the terminating null char.
33064   */
33065   DWORD dwLen = 0;
33066   char *zOut = 0;
33067 
33068   if( osIsNT() ){
33069 #if SQLITE_OS_WINRT
33070     WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
33071     dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
33072                              FORMAT_MESSAGE_IGNORE_INSERTS,
33073                              NULL,
33074                              lastErrno,
33075                              0,
33076                              zTempWide,
33077                              SQLITE_WIN32_MAX_ERRMSG_CHARS,
33078                              0);
33079 #else
33080     LPWSTR zTempWide = NULL;
33081     dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33082                              FORMAT_MESSAGE_FROM_SYSTEM |
33083                              FORMAT_MESSAGE_IGNORE_INSERTS,
33084                              NULL,
33085                              lastErrno,
33086                              0,
33087                              (LPWSTR) &zTempWide,
33088                              0,
33089                              0);
33090 #endif
33091     if( dwLen > 0 ){
33092       /* allocate a buffer and convert to UTF8 */
33093       sqlite3BeginBenignMalloc();
33094       zOut = winUnicodeToUtf8(zTempWide);
33095       sqlite3EndBenignMalloc();
33096 #if !SQLITE_OS_WINRT
33097       /* free the system buffer allocated by FormatMessage */
33098       osLocalFree(zTempWide);
33099 #endif
33100     }
33101   }
33102 #ifdef SQLITE_WIN32_HAS_ANSI
33103   else{
33104     char *zTemp = NULL;
33105     dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33106                              FORMAT_MESSAGE_FROM_SYSTEM |
33107                              FORMAT_MESSAGE_IGNORE_INSERTS,
33108                              NULL,
33109                              lastErrno,
33110                              0,
33111                              (LPSTR) &zTemp,
33112                              0,
33113                              0);
33114     if( dwLen > 0 ){
33115       /* allocate a buffer and convert to UTF8 */
33116       sqlite3BeginBenignMalloc();
33117       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
33118       sqlite3EndBenignMalloc();
33119       /* free the system buffer allocated by FormatMessage */
33120       osLocalFree(zTemp);
33121     }
33122   }
33123 #endif
33124   if( 0 == dwLen ){
33125     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
33126   }else{
33127     /* copy a maximum of nBuf chars to output buffer */
33128     sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
33129     /* free the UTF8 buffer */
33130     sqlite3_free(zOut);
33131   }
33132   return 0;
33133 }
33134 
33135 /*
33136 **
33137 ** This function - winLogErrorAtLine() - is only ever called via the macro
33138 ** winLogError().
33139 **
33140 ** This routine is invoked after an error occurs in an OS function.
33141 ** It logs a message using sqlite3_log() containing the current value of
33142 ** error code and, if possible, the human-readable equivalent from
33143 ** FormatMessage.
33144 **
33145 ** The first argument passed to the macro should be the error code that
33146 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33147 ** The two subsequent arguments should be the name of the OS function that
33148 ** failed and the associated file-system path, if any.
33149 */
33150 #define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
33151 static int winLogErrorAtLine(
33152   int errcode,                    /* SQLite error code */
33153   DWORD lastErrno,                /* Win32 last error */
33154   const char *zFunc,              /* Name of OS function that failed */
33155   const char *zPath,              /* File path associated with error */
33156   int iLine                       /* Source line number where error occurred */
33157 ){
33158   char zMsg[500];                 /* Human readable error text */
33159   int i;                          /* Loop counter */
33160 
33161   zMsg[0] = 0;
33162   winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
33163   assert( errcode!=SQLITE_OK );
33164   if( zPath==0 ) zPath = "";
33165   for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
33166   zMsg[i] = 0;
33167   sqlite3_log(errcode,
33168       "os_win.c:%d: (%lu) %s(%s) - %s",
33169       iLine, lastErrno, zFunc, zPath, zMsg
33170   );
33171 
33172   return errcode;
33173 }
33174 
33175 /*
33176 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
33177 ** will be retried following a locking error - probably caused by
33178 ** antivirus software.  Also the initial delay before the first retry.
33179 ** The delay increases linearly with each retry.
33180 */
33181 #ifndef SQLITE_WIN32_IOERR_RETRY
33182 # define SQLITE_WIN32_IOERR_RETRY 10
33183 #endif
33184 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
33185 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
33186 #endif
33187 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
33188 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
33189 
33190 /*
33191 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
33192 ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
33193 ** to give up with an error.
33194 */
33195 static int winRetryIoerr(int *pnRetry, DWORD *pError){
33196   DWORD e = osGetLastError();
33197   if( *pnRetry>=winIoerrRetry ){
33198     if( pError ){
33199       *pError = e;
33200     }
33201     return 0;
33202   }
33203   if( e==ERROR_ACCESS_DENIED ||
33204       e==ERROR_LOCK_VIOLATION ||
33205       e==ERROR_SHARING_VIOLATION ){
33206     sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
33207     ++*pnRetry;
33208     return 1;
33209   }
33210   if( pError ){
33211     *pError = e;
33212   }
33213   return 0;
33214 }
33215 
33216 /*
33217 ** Log a I/O error retry episode.
33218 */
33219 static void winLogIoerr(int nRetry){
33220   if( nRetry ){
33221     sqlite3_log(SQLITE_IOERR,
33222       "delayed %dms for lock/sharing conflict",
33223       winIoerrRetryDelay*nRetry*(nRetry+1)/2
33224     );
33225   }
33226 }
33227 
33228 #if SQLITE_OS_WINCE
33229 /*************************************************************************
33230 ** This section contains code for WinCE only.
33231 */
33232 #if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
33233 /*
33234 ** The MSVC CRT on Windows CE may not have a localtime() function.  So
33235 ** create a substitute.
33236 */
33237 /* #include <time.h> */
33238 struct tm *__cdecl localtime(const time_t *t)
33239 {
33240   static struct tm y;
33241   FILETIME uTm, lTm;
33242   SYSTEMTIME pTm;
33243   sqlite3_int64 t64;
33244   t64 = *t;
33245   t64 = (t64 + 11644473600)*10000000;
33246   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
33247   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
33248   osFileTimeToLocalFileTime(&uTm,&lTm);
33249   osFileTimeToSystemTime(&lTm,&pTm);
33250   y.tm_year = pTm.wYear - 1900;
33251   y.tm_mon = pTm.wMonth - 1;
33252   y.tm_wday = pTm.wDayOfWeek;
33253   y.tm_mday = pTm.wDay;
33254   y.tm_hour = pTm.wHour;
33255   y.tm_min = pTm.wMinute;
33256   y.tm_sec = pTm.wSecond;
33257   return &y;
33258 }
33259 #endif
33260 
33261 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
33262 
33263 /*
33264 ** Acquire a lock on the handle h
33265 */
33266 static void winceMutexAcquire(HANDLE h){
33267    DWORD dwErr;
33268    do {
33269      dwErr = osWaitForSingleObject(h, INFINITE);
33270    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
33271 }
33272 /*
33273 ** Release a lock acquired by winceMutexAcquire()
33274 */
33275 #define winceMutexRelease(h) ReleaseMutex(h)
33276 
33277 /*
33278 ** Create the mutex and shared memory used for locking in the file
33279 ** descriptor pFile
33280 */
33281 static int winceCreateLock(const char *zFilename, winFile *pFile){
33282   LPWSTR zTok;
33283   LPWSTR zName;
33284   DWORD lastErrno;
33285   BOOL bLogged = FALSE;
33286   BOOL bInit = TRUE;
33287 
33288   zName = winUtf8ToUnicode(zFilename);
33289   if( zName==0 ){
33290     /* out of memory */
33291     return SQLITE_IOERR_NOMEM;
33292   }
33293 
33294   /* Initialize the local lockdata */
33295   memset(&pFile->local, 0, sizeof(pFile->local));
33296 
33297   /* Replace the backslashes from the filename and lowercase it
33298   ** to derive a mutex name. */
33299   zTok = osCharLowerW(zName);
33300   for (;*zTok;zTok++){
33301     if (*zTok == '\\') *zTok = '_';
33302   }
33303 
33304   /* Create/open the named mutex */
33305   pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
33306   if (!pFile->hMutex){
33307     pFile->lastErrno = osGetLastError();
33308     sqlite3_free(zName);
33309     return winLogError(SQLITE_IOERR, pFile->lastErrno,
33310                        "winceCreateLock1", zFilename);
33311   }
33312 
33313   /* Acquire the mutex before continuing */
33314   winceMutexAcquire(pFile->hMutex);
33315 
33316   /* Since the names of named mutexes, semaphores, file mappings etc are
33317   ** case-sensitive, take advantage of that by uppercasing the mutex name
33318   ** and using that as the shared filemapping name.
33319   */
33320   osCharUpperW(zName);
33321   pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
33322                                         PAGE_READWRITE, 0, sizeof(winceLock),
33323                                         zName);
33324 
33325   /* Set a flag that indicates we're the first to create the memory so it
33326   ** must be zero-initialized */
33327   lastErrno = osGetLastError();
33328   if (lastErrno == ERROR_ALREADY_EXISTS){
33329     bInit = FALSE;
33330   }
33331 
33332   sqlite3_free(zName);
33333 
33334   /* If we succeeded in making the shared memory handle, map it. */
33335   if( pFile->hShared ){
33336     pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
33337              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
33338     /* If mapping failed, close the shared memory handle and erase it */
33339     if( !pFile->shared ){
33340       pFile->lastErrno = osGetLastError();
33341       winLogError(SQLITE_IOERR, pFile->lastErrno,
33342                   "winceCreateLock2", zFilename);
33343       bLogged = TRUE;
33344       osCloseHandle(pFile->hShared);
33345       pFile->hShared = NULL;
33346     }
33347   }
33348 
33349   /* If shared memory could not be created, then close the mutex and fail */
33350   if( pFile->hShared==NULL ){
33351     if( !bLogged ){
33352       pFile->lastErrno = lastErrno;
33353       winLogError(SQLITE_IOERR, pFile->lastErrno,
33354                   "winceCreateLock3", zFilename);
33355       bLogged = TRUE;
33356     }
33357     winceMutexRelease(pFile->hMutex);
33358     osCloseHandle(pFile->hMutex);
33359     pFile->hMutex = NULL;
33360     return SQLITE_IOERR;
33361   }
33362 
33363   /* Initialize the shared memory if we're supposed to */
33364   if( bInit ){
33365     memset(pFile->shared, 0, sizeof(winceLock));
33366   }
33367 
33368   winceMutexRelease(pFile->hMutex);
33369   return SQLITE_OK;
33370 }
33371 
33372 /*
33373 ** Destroy the part of winFile that deals with wince locks
33374 */
33375 static void winceDestroyLock(winFile *pFile){
33376   if (pFile->hMutex){
33377     /* Acquire the mutex */
33378     winceMutexAcquire(pFile->hMutex);
33379 
33380     /* The following blocks should probably assert in debug mode, but they
33381        are to cleanup in case any locks remained open */
33382     if (pFile->local.nReaders){
33383       pFile->shared->nReaders --;
33384     }
33385     if (pFile->local.bReserved){
33386       pFile->shared->bReserved = FALSE;
33387     }
33388     if (pFile->local.bPending){
33389       pFile->shared->bPending = FALSE;
33390     }
33391     if (pFile->local.bExclusive){
33392       pFile->shared->bExclusive = FALSE;
33393     }
33394 
33395     /* De-reference and close our copy of the shared memory handle */
33396     osUnmapViewOfFile(pFile->shared);
33397     osCloseHandle(pFile->hShared);
33398 
33399     /* Done with the mutex */
33400     winceMutexRelease(pFile->hMutex);
33401     osCloseHandle(pFile->hMutex);
33402     pFile->hMutex = NULL;
33403   }
33404 }
33405 
33406 /*
33407 ** An implementation of the LockFile() API of Windows for CE
33408 */
33409 static BOOL winceLockFile(
33410   LPHANDLE phFile,
33411   DWORD dwFileOffsetLow,
33412   DWORD dwFileOffsetHigh,
33413   DWORD nNumberOfBytesToLockLow,
33414   DWORD nNumberOfBytesToLockHigh
33415 ){
33416   winFile *pFile = HANDLE_TO_WINFILE(phFile);
33417   BOOL bReturn = FALSE;
33418 
33419   UNUSED_PARAMETER(dwFileOffsetHigh);
33420   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
33421 
33422   if (!pFile->hMutex) return TRUE;
33423   winceMutexAcquire(pFile->hMutex);
33424 
33425   /* Wanting an exclusive lock? */
33426   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
33427        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
33428     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
33429        pFile->shared->bExclusive = TRUE;
33430        pFile->local.bExclusive = TRUE;
33431        bReturn = TRUE;
33432     }
33433   }
33434 
33435   /* Want a read-only lock? */
33436   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
33437            nNumberOfBytesToLockLow == 1){
33438     if (pFile->shared->bExclusive == 0){
33439       pFile->local.nReaders ++;
33440       if (pFile->local.nReaders == 1){
33441         pFile->shared->nReaders ++;
33442       }
33443       bReturn = TRUE;
33444     }
33445   }
33446 
33447   /* Want a pending lock? */
33448   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
33449            && nNumberOfBytesToLockLow == 1){
33450     /* If no pending lock has been acquired, then acquire it */
33451     if (pFile->shared->bPending == 0) {
33452       pFile->shared->bPending = TRUE;
33453       pFile->local.bPending = TRUE;
33454       bReturn = TRUE;
33455     }
33456   }
33457 
33458   /* Want a reserved lock? */
33459   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
33460            && nNumberOfBytesToLockLow == 1){
33461     if (pFile->shared->bReserved == 0) {
33462       pFile->shared->bReserved = TRUE;
33463       pFile->local.bReserved = TRUE;
33464       bReturn = TRUE;
33465     }
33466   }
33467 
33468   winceMutexRelease(pFile->hMutex);
33469   return bReturn;
33470 }
33471 
33472 /*
33473 ** An implementation of the UnlockFile API of Windows for CE
33474 */
33475 static BOOL winceUnlockFile(
33476   LPHANDLE phFile,
33477   DWORD dwFileOffsetLow,
33478   DWORD dwFileOffsetHigh,
33479   DWORD nNumberOfBytesToUnlockLow,
33480   DWORD nNumberOfBytesToUnlockHigh
33481 ){
33482   winFile *pFile = HANDLE_TO_WINFILE(phFile);
33483   BOOL bReturn = FALSE;
33484 
33485   UNUSED_PARAMETER(dwFileOffsetHigh);
33486   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
33487 
33488   if (!pFile->hMutex) return TRUE;
33489   winceMutexAcquire(pFile->hMutex);
33490 
33491   /* Releasing a reader lock or an exclusive lock */
33492   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
33493     /* Did we have an exclusive lock? */
33494     if (pFile->local.bExclusive){
33495       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
33496       pFile->local.bExclusive = FALSE;
33497       pFile->shared->bExclusive = FALSE;
33498       bReturn = TRUE;
33499     }
33500 
33501     /* Did we just have a reader lock? */
33502     else if (pFile->local.nReaders){
33503       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
33504              || nNumberOfBytesToUnlockLow == 1);
33505       pFile->local.nReaders --;
33506       if (pFile->local.nReaders == 0)
33507       {
33508         pFile->shared->nReaders --;
33509       }
33510       bReturn = TRUE;
33511     }
33512   }
33513 
33514   /* Releasing a pending lock */
33515   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
33516            && nNumberOfBytesToUnlockLow == 1){
33517     if (pFile->local.bPending){
33518       pFile->local.bPending = FALSE;
33519       pFile->shared->bPending = FALSE;
33520       bReturn = TRUE;
33521     }
33522   }
33523   /* Releasing a reserved lock */
33524   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
33525            && nNumberOfBytesToUnlockLow == 1){
33526     if (pFile->local.bReserved) {
33527       pFile->local.bReserved = FALSE;
33528       pFile->shared->bReserved = FALSE;
33529       bReturn = TRUE;
33530     }
33531   }
33532 
33533   winceMutexRelease(pFile->hMutex);
33534   return bReturn;
33535 }
33536 /*
33537 ** End of the special code for wince
33538 *****************************************************************************/
33539 #endif /* SQLITE_OS_WINCE */
33540 
33541 /*
33542 ** Lock a file region.
33543 */
33544 static BOOL winLockFile(
33545   LPHANDLE phFile,
33546   DWORD flags,
33547   DWORD offsetLow,
33548   DWORD offsetHigh,
33549   DWORD numBytesLow,
33550   DWORD numBytesHigh
33551 ){
33552 #if SQLITE_OS_WINCE
33553   /*
33554   ** NOTE: Windows CE is handled differently here due its lack of the Win32
33555   **       API LockFile.
33556   */
33557   return winceLockFile(phFile, offsetLow, offsetHigh,
33558                        numBytesLow, numBytesHigh);
33559 #else
33560   if( osIsNT() ){
33561     OVERLAPPED ovlp;
33562     memset(&ovlp, 0, sizeof(OVERLAPPED));
33563     ovlp.Offset = offsetLow;
33564     ovlp.OffsetHigh = offsetHigh;
33565     return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
33566   }else{
33567     return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
33568                       numBytesHigh);
33569   }
33570 #endif
33571 }
33572 
33573 /*
33574 ** Unlock a file region.
33575  */
33576 static BOOL winUnlockFile(
33577   LPHANDLE phFile,
33578   DWORD offsetLow,
33579   DWORD offsetHigh,
33580   DWORD numBytesLow,
33581   DWORD numBytesHigh
33582 ){
33583 #if SQLITE_OS_WINCE
33584   /*
33585   ** NOTE: Windows CE is handled differently here due its lack of the Win32
33586   **       API UnlockFile.
33587   */
33588   return winceUnlockFile(phFile, offsetLow, offsetHigh,
33589                          numBytesLow, numBytesHigh);
33590 #else
33591   if( osIsNT() ){
33592     OVERLAPPED ovlp;
33593     memset(&ovlp, 0, sizeof(OVERLAPPED));
33594     ovlp.Offset = offsetLow;
33595     ovlp.OffsetHigh = offsetHigh;
33596     return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
33597   }else{
33598     return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
33599                         numBytesHigh);
33600   }
33601 #endif
33602 }
33603 
33604 /*****************************************************************************
33605 ** The next group of routines implement the I/O methods specified
33606 ** by the sqlite3_io_methods object.
33607 ******************************************************************************/
33608 
33609 /*
33610 ** Some Microsoft compilers lack this definition.
33611 */
33612 #ifndef INVALID_SET_FILE_POINTER
33613 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
33614 #endif
33615 
33616 /*
33617 ** Move the current position of the file handle passed as the first
33618 ** argument to offset iOffset within the file. If successful, return 0.
33619 ** Otherwise, set pFile->lastErrno and return non-zero.
33620 */
33621 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
33622 #if !SQLITE_OS_WINRT
33623   LONG upperBits;                 /* Most sig. 32 bits of new offset */
33624   LONG lowerBits;                 /* Least sig. 32 bits of new offset */
33625   DWORD dwRet;                    /* Value returned by SetFilePointer() */
33626   DWORD lastErrno;                /* Value returned by GetLastError() */
33627 
33628   OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
33629 
33630   upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
33631   lowerBits = (LONG)(iOffset & 0xffffffff);
33632 
33633   /* API oddity: If successful, SetFilePointer() returns a dword
33634   ** containing the lower 32-bits of the new file-offset. Or, if it fails,
33635   ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
33636   ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
33637   ** whether an error has actually occurred, it is also necessary to call
33638   ** GetLastError().
33639   */
33640   dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
33641 
33642   if( (dwRet==INVALID_SET_FILE_POINTER
33643       && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
33644     pFile->lastErrno = lastErrno;
33645     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
33646                 "winSeekFile", pFile->zPath);
33647     OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
33648     return 1;
33649   }
33650 
33651   OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
33652   return 0;
33653 #else
33654   /*
33655   ** Same as above, except that this implementation works for WinRT.
33656   */
33657 
33658   LARGE_INTEGER x;                /* The new offset */
33659   BOOL bRet;                      /* Value returned by SetFilePointerEx() */
33660 
33661   x.QuadPart = iOffset;
33662   bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
33663 
33664   if(!bRet){
33665     pFile->lastErrno = osGetLastError();
33666     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
33667                 "winSeekFile", pFile->zPath);
33668     OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
33669     return 1;
33670   }
33671 
33672   OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
33673   return 0;
33674 #endif
33675 }
33676 
33677 #if SQLITE_MAX_MMAP_SIZE>0
33678 /* Forward references to VFS helper methods used for memory mapped files */
33679 static int winMapfile(winFile*, sqlite3_int64);
33680 static int winUnmapfile(winFile*);
33681 #endif
33682 
33683 /*
33684 ** Close a file.
33685 **
33686 ** It is reported that an attempt to close a handle might sometimes
33687 ** fail.  This is a very unreasonable result, but Windows is notorious
33688 ** for being unreasonable so I do not doubt that it might happen.  If
33689 ** the close fails, we pause for 100 milliseconds and try again.  As
33690 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
33691 ** giving up and returning an error.
33692 */
33693 #define MX_CLOSE_ATTEMPT 3
33694 static int winClose(sqlite3_file *id){
33695   int rc, cnt = 0;
33696   winFile *pFile = (winFile*)id;
33697 
33698   assert( id!=0 );
33699 #ifndef SQLITE_OMIT_WAL
33700   assert( pFile->pShm==0 );
33701 #endif
33702   assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
33703   OSTRACE(("CLOSE file=%p\n", pFile->h));
33704 
33705 #if SQLITE_MAX_MMAP_SIZE>0
33706   winUnmapfile(pFile);
33707 #endif
33708 
33709   do{
33710     rc = osCloseHandle(pFile->h);
33711     /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
33712   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
33713 #if SQLITE_OS_WINCE
33714 #define WINCE_DELETION_ATTEMPTS 3
33715   winceDestroyLock(pFile);
33716   if( pFile->zDeleteOnClose ){
33717     int cnt = 0;
33718     while(
33719            osDeleteFileW(pFile->zDeleteOnClose)==0
33720         && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
33721         && cnt++ < WINCE_DELETION_ATTEMPTS
33722     ){
33723        sqlite3_win32_sleep(100);  /* Wait a little before trying again */
33724     }
33725     sqlite3_free(pFile->zDeleteOnClose);
33726   }
33727 #endif
33728   if( rc ){
33729     pFile->h = NULL;
33730   }
33731   OpenCounter(-1);
33732   OSTRACE(("CLOSE file=%p, rc=%s\n", pFile->h, rc ? "ok" : "failed"));
33733   return rc ? SQLITE_OK
33734             : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
33735                           "winClose", pFile->zPath);
33736 }
33737 
33738 /*
33739 ** Read data from a file into a buffer.  Return SQLITE_OK if all
33740 ** bytes were read successfully and SQLITE_IOERR if anything goes
33741 ** wrong.
33742 */
33743 static int winRead(
33744   sqlite3_file *id,          /* File to read from */
33745   void *pBuf,                /* Write content into this buffer */
33746   int amt,                   /* Number of bytes to read */
33747   sqlite3_int64 offset       /* Begin reading at this offset */
33748 ){
33749 #if !SQLITE_OS_WINCE
33750   OVERLAPPED overlapped;          /* The offset for ReadFile. */
33751 #endif
33752   winFile *pFile = (winFile*)id;  /* file handle */
33753   DWORD nRead;                    /* Number of bytes actually read from file */
33754   int nRetry = 0;                 /* Number of retrys */
33755 
33756   assert( id!=0 );
33757   assert( amt>0 );
33758   assert( offset>=0 );
33759   SimulateIOError(return SQLITE_IOERR_READ);
33760   OSTRACE(("READ file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
33761            pFile->h, pBuf, amt, offset, pFile->locktype));
33762 
33763 #if SQLITE_MAX_MMAP_SIZE>0
33764   /* Deal with as much of this read request as possible by transfering
33765   ** data from the memory mapping using memcpy().  */
33766   if( offset<pFile->mmapSize ){
33767     if( offset+amt <= pFile->mmapSize ){
33768       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
33769       OSTRACE(("READ-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
33770       return SQLITE_OK;
33771     }else{
33772       int nCopy = (int)(pFile->mmapSize - offset);
33773       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
33774       pBuf = &((u8 *)pBuf)[nCopy];
33775       amt -= nCopy;
33776       offset += nCopy;
33777     }
33778   }
33779 #endif
33780 
33781 #if SQLITE_OS_WINCE
33782   if( winSeekFile(pFile, offset) ){
33783     OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
33784     return SQLITE_FULL;
33785   }
33786   while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
33787 #else
33788   memset(&overlapped, 0, sizeof(OVERLAPPED));
33789   overlapped.Offset = (LONG)(offset & 0xffffffff);
33790   overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
33791   while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
33792          osGetLastError()!=ERROR_HANDLE_EOF ){
33793 #endif
33794     DWORD lastErrno;
33795     if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
33796     pFile->lastErrno = lastErrno;
33797     OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
33798     return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
33799                        "winRead", pFile->zPath);
33800   }
33801   winLogIoerr(nRetry);
33802   if( nRead<(DWORD)amt ){
33803     /* Unread parts of the buffer must be zero-filled */
33804     memset(&((char*)pBuf)[nRead], 0, amt-nRead);
33805     OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
33806     return SQLITE_IOERR_SHORT_READ;
33807   }
33808 
33809   OSTRACE(("READ file=%p, rc=SQLITE_OK\n", pFile->h));
33810   return SQLITE_OK;
33811 }
33812 
33813 /*
33814 ** Write data from a buffer into a file.  Return SQLITE_OK on success
33815 ** or some other error code on failure.
33816 */
33817 static int winWrite(
33818   sqlite3_file *id,               /* File to write into */
33819   const void *pBuf,               /* The bytes to be written */
33820   int amt,                        /* Number of bytes to write */
33821   sqlite3_int64 offset            /* Offset into the file to begin writing at */
33822 ){
33823   int rc = 0;                     /* True if error has occurred, else false */
33824   winFile *pFile = (winFile*)id;  /* File handle */
33825   int nRetry = 0;                 /* Number of retries */
33826 
33827   assert( amt>0 );
33828   assert( pFile );
33829   SimulateIOError(return SQLITE_IOERR_WRITE);
33830   SimulateDiskfullError(return SQLITE_FULL);
33831 
33832   OSTRACE(("WRITE file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
33833            pFile->h, pBuf, amt, offset, pFile->locktype));
33834 
33835 #if SQLITE_MAX_MMAP_SIZE>0
33836   /* Deal with as much of this write request as possible by transfering
33837   ** data from the memory mapping using memcpy().  */
33838   if( offset<pFile->mmapSize ){
33839     if( offset+amt <= pFile->mmapSize ){
33840       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
33841       OSTRACE(("WRITE-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
33842       return SQLITE_OK;
33843     }else{
33844       int nCopy = (int)(pFile->mmapSize - offset);
33845       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
33846       pBuf = &((u8 *)pBuf)[nCopy];
33847       amt -= nCopy;
33848       offset += nCopy;
33849     }
33850   }
33851 #endif
33852 
33853 #if SQLITE_OS_WINCE
33854   rc = winSeekFile(pFile, offset);
33855   if( rc==0 ){
33856 #else
33857   {
33858 #endif
33859 #if !SQLITE_OS_WINCE
33860     OVERLAPPED overlapped;        /* The offset for WriteFile. */
33861 #endif
33862     u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
33863     int nRem = amt;               /* Number of bytes yet to be written */
33864     DWORD nWrite;                 /* Bytes written by each WriteFile() call */
33865     DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
33866 
33867 #if !SQLITE_OS_WINCE
33868     memset(&overlapped, 0, sizeof(OVERLAPPED));
33869     overlapped.Offset = (LONG)(offset & 0xffffffff);
33870     overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
33871 #endif
33872 
33873     while( nRem>0 ){
33874 #if SQLITE_OS_WINCE
33875       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
33876 #else
33877       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
33878 #endif
33879         if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
33880         break;
33881       }
33882       assert( nWrite==0 || nWrite<=(DWORD)nRem );
33883       if( nWrite==0 || nWrite>(DWORD)nRem ){
33884         lastErrno = osGetLastError();
33885         break;
33886       }
33887 #if !SQLITE_OS_WINCE
33888       offset += nWrite;
33889       overlapped.Offset = (LONG)(offset & 0xffffffff);
33890       overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
33891 #endif
33892       aRem += nWrite;
33893       nRem -= nWrite;
33894     }
33895     if( nRem>0 ){
33896       pFile->lastErrno = lastErrno;
33897       rc = 1;
33898     }
33899   }
33900 
33901   if( rc ){
33902     if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
33903        || ( pFile->lastErrno==ERROR_DISK_FULL )){
33904       OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
33905       return winLogError(SQLITE_FULL, pFile->lastErrno,
33906                          "winWrite1", pFile->zPath);
33907     }
33908     OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
33909     return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
33910                        "winWrite2", pFile->zPath);
33911   }else{
33912     winLogIoerr(nRetry);
33913   }
33914   OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
33915   return SQLITE_OK;
33916 }
33917 
33918 /*
33919 ** Truncate an open file to a specified size
33920 */
33921 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
33922   winFile *pFile = (winFile*)id;  /* File handle object */
33923   int rc = SQLITE_OK;             /* Return code for this function */
33924   DWORD lastErrno;
33925 
33926   assert( pFile );
33927   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
33928   OSTRACE(("TRUNCATE file=%p, size=%lld, lock=%d\n",
33929            pFile->h, nByte, pFile->locktype));
33930 
33931   /* If the user has configured a chunk-size for this file, truncate the
33932   ** file so that it consists of an integer number of chunks (i.e. the
33933   ** actual file size after the operation may be larger than the requested
33934   ** size).
33935   */
33936   if( pFile->szChunk>0 ){
33937     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
33938   }
33939 
33940   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33941   if( winSeekFile(pFile, nByte) ){
33942     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33943                      "winTruncate1", pFile->zPath);
33944   }else if( 0==osSetEndOfFile(pFile->h) &&
33945             ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33946     pFile->lastErrno = lastErrno;
33947     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33948                      "winTruncate2", pFile->zPath);
33949   }
33950 
33951 #if SQLITE_MAX_MMAP_SIZE>0
33952   /* If the file was truncated to a size smaller than the currently
33953   ** mapped region, reduce the effective mapping size as well. SQLite will
33954   ** use read() and write() to access data beyond this point from now on.
33955   */
33956   if( pFile->pMapRegion && nByte<pFile->mmapSize ){
33957     pFile->mmapSize = nByte;
33958   }
33959 #endif
33960 
33961   OSTRACE(("TRUNCATE file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
33962   return rc;
33963 }
33964 
33965 #ifdef SQLITE_TEST
33966 /*
33967 ** Count the number of fullsyncs and normal syncs.  This is used to test
33968 ** that syncs and fullsyncs are occuring at the right times.
33969 */
33970 SQLITE_API int sqlite3_sync_count = 0;
33971 SQLITE_API int sqlite3_fullsync_count = 0;
33972 #endif
33973 
33974 /*
33975 ** Make sure all writes to a particular file are committed to disk.
33976 */
33977 static int winSync(sqlite3_file *id, int flags){
33978 #ifndef SQLITE_NO_SYNC
33979   /*
33980   ** Used only when SQLITE_NO_SYNC is not defined.
33981    */
33982   BOOL rc;
33983 #endif
33984 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
33985     (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
33986   /*
33987   ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
33988   ** OSTRACE() macros.
33989    */
33990   winFile *pFile = (winFile*)id;
33991 #else
33992   UNUSED_PARAMETER(id);
33993 #endif
33994 
33995   assert( pFile );
33996   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
33997   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
33998       || (flags&0x0F)==SQLITE_SYNC_FULL
33999   );
34000 
34001   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
34002   ** line is to test that doing so does not cause any problems.
34003   */
34004   SimulateDiskfullError( return SQLITE_FULL );
34005 
34006   OSTRACE(("SYNC file=%p, flags=%x, lock=%d\n",
34007            pFile->h, flags, pFile->locktype));
34008 
34009 #ifndef SQLITE_TEST
34010   UNUSED_PARAMETER(flags);
34011 #else
34012   if( (flags&0x0F)==SQLITE_SYNC_FULL ){
34013     sqlite3_fullsync_count++;
34014   }
34015   sqlite3_sync_count++;
34016 #endif
34017 
34018   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
34019   ** no-op
34020   */
34021 #ifdef SQLITE_NO_SYNC
34022   OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
34023   return SQLITE_OK;
34024 #else
34025   rc = osFlushFileBuffers(pFile->h);
34026   SimulateIOError( rc=FALSE );
34027   if( rc ){
34028     OSTRACE(("SYNC file=%p, rc=SQLITE_OK\n", pFile->h));
34029     return SQLITE_OK;
34030   }else{
34031     pFile->lastErrno = osGetLastError();
34032     OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
34033     return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
34034                        "winSync", pFile->zPath);
34035   }
34036 #endif
34037 }
34038 
34039 /*
34040 ** Determine the current size of a file in bytes
34041 */
34042 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
34043   winFile *pFile = (winFile*)id;
34044   int rc = SQLITE_OK;
34045 
34046   assert( id!=0 );
34047   assert( pSize!=0 );
34048   SimulateIOError(return SQLITE_IOERR_FSTAT);
34049   OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
34050 
34051 #if SQLITE_OS_WINRT
34052   {
34053     FILE_STANDARD_INFO info;
34054     if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
34055                                      &info, sizeof(info)) ){
34056       *pSize = info.EndOfFile.QuadPart;
34057     }else{
34058       pFile->lastErrno = osGetLastError();
34059       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
34060                        "winFileSize", pFile->zPath);
34061     }
34062   }
34063 #else
34064   {
34065     DWORD upperBits;
34066     DWORD lowerBits;
34067     DWORD lastErrno;
34068 
34069     lowerBits = osGetFileSize(pFile->h, &upperBits);
34070     *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
34071     if(   (lowerBits == INVALID_FILE_SIZE)
34072        && ((lastErrno = osGetLastError())!=NO_ERROR) ){
34073       pFile->lastErrno = lastErrno;
34074       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
34075                        "winFileSize", pFile->zPath);
34076     }
34077   }
34078 #endif
34079   OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
34080            pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
34081   return rc;
34082 }
34083 
34084 /*
34085 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
34086 */
34087 #ifndef LOCKFILE_FAIL_IMMEDIATELY
34088 # define LOCKFILE_FAIL_IMMEDIATELY 1
34089 #endif
34090 
34091 #ifndef LOCKFILE_EXCLUSIVE_LOCK
34092 # define LOCKFILE_EXCLUSIVE_LOCK 2
34093 #endif
34094 
34095 /*
34096 ** Historically, SQLite has used both the LockFile and LockFileEx functions.
34097 ** When the LockFile function was used, it was always expected to fail
34098 ** immediately if the lock could not be obtained.  Also, it always expected to
34099 ** obtain an exclusive lock.  These flags are used with the LockFileEx function
34100 ** and reflect those expectations; therefore, they should not be changed.
34101 */
34102 #ifndef SQLITE_LOCKFILE_FLAGS
34103 # define SQLITE_LOCKFILE_FLAGS   (LOCKFILE_FAIL_IMMEDIATELY | \
34104                                   LOCKFILE_EXCLUSIVE_LOCK)
34105 #endif
34106 
34107 /*
34108 ** Currently, SQLite never calls the LockFileEx function without wanting the
34109 ** call to fail immediately if the lock cannot be obtained.
34110 */
34111 #ifndef SQLITE_LOCKFILEEX_FLAGS
34112 # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
34113 #endif
34114 
34115 /*
34116 ** Acquire a reader lock.
34117 ** Different API routines are called depending on whether or not this
34118 ** is Win9x or WinNT.
34119 */
34120 static int winGetReadLock(winFile *pFile){
34121   int res;
34122   OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
34123   if( osIsNT() ){
34124 #if SQLITE_OS_WINCE
34125     /*
34126     ** NOTE: Windows CE is handled differently here due its lack of the Win32
34127     **       API LockFileEx.
34128     */
34129     res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
34130 #else
34131     res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
34132                       SHARED_SIZE, 0);
34133 #endif
34134   }
34135 #ifdef SQLITE_WIN32_HAS_ANSI
34136   else{
34137     int lk;
34138     sqlite3_randomness(sizeof(lk), &lk);
34139     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
34140     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
34141                       SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
34142   }
34143 #endif
34144   if( res == 0 ){
34145     pFile->lastErrno = osGetLastError();
34146     /* No need to log a failure to lock */
34147   }
34148   OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34149   return res;
34150 }
34151 
34152 /*
34153 ** Undo a readlock
34154 */
34155 static int winUnlockReadLock(winFile *pFile){
34156   int res;
34157   DWORD lastErrno;
34158   OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
34159   if( osIsNT() ){
34160     res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34161   }
34162 #ifdef SQLITE_WIN32_HAS_ANSI
34163   else{
34164     res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
34165   }
34166 #endif
34167   if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
34168     pFile->lastErrno = lastErrno;
34169     winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
34170                 "winUnlockReadLock", pFile->zPath);
34171   }
34172   OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34173   return res;
34174 }
34175 
34176 /*
34177 ** Lock the file with the lock specified by parameter locktype - one
34178 ** of the following:
34179 **
34180 **     (1) SHARED_LOCK
34181 **     (2) RESERVED_LOCK
34182 **     (3) PENDING_LOCK
34183 **     (4) EXCLUSIVE_LOCK
34184 **
34185 ** Sometimes when requesting one lock state, additional lock states
34186 ** are inserted in between.  The locking might fail on one of the later
34187 ** transitions leaving the lock state different from what it started but
34188 ** still short of its goal.  The following chart shows the allowed
34189 ** transitions and the inserted intermediate states:
34190 **
34191 **    UNLOCKED -> SHARED
34192 **    SHARED -> RESERVED
34193 **    SHARED -> (PENDING) -> EXCLUSIVE
34194 **    RESERVED -> (PENDING) -> EXCLUSIVE
34195 **    PENDING -> EXCLUSIVE
34196 **
34197 ** This routine will only increase a lock.  The winUnlock() routine
34198 ** erases all locks at once and returns us immediately to locking level 0.
34199 ** It is not possible to lower the locking level one step at a time.  You
34200 ** must go straight to locking level 0.
34201 */
34202 static int winLock(sqlite3_file *id, int locktype){
34203   int rc = SQLITE_OK;    /* Return code from subroutines */
34204   int res = 1;           /* Result of a Windows lock call */
34205   int newLocktype;       /* Set pFile->locktype to this value before exiting */
34206   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
34207   winFile *pFile = (winFile*)id;
34208   DWORD lastErrno = NO_ERROR;
34209 
34210   assert( id!=0 );
34211   OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
34212            pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
34213 
34214   /* If there is already a lock of this type or more restrictive on the
34215   ** OsFile, do nothing. Don't use the end_lock: exit path, as
34216   ** sqlite3OsEnterMutex() hasn't been called yet.
34217   */
34218   if( pFile->locktype>=locktype ){
34219     OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
34220     return SQLITE_OK;
34221   }
34222 
34223   /* Make sure the locking sequence is correct
34224   */
34225   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
34226   assert( locktype!=PENDING_LOCK );
34227   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
34228 
34229   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
34230   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
34231   ** the PENDING_LOCK byte is temporary.
34232   */
34233   newLocktype = pFile->locktype;
34234   if(   (pFile->locktype==NO_LOCK)
34235      || (   (locktype==EXCLUSIVE_LOCK)
34236          && (pFile->locktype==RESERVED_LOCK))
34237   ){
34238     int cnt = 3;
34239     while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
34240                                          PENDING_BYTE, 0, 1, 0))==0 ){
34241       /* Try 3 times to get the pending lock.  This is needed to work
34242       ** around problems caused by indexing and/or anti-virus software on
34243       ** Windows systems.
34244       ** If you are using this code as a model for alternative VFSes, do not
34245       ** copy this retry logic.  It is a hack intended for Windows only.
34246       */
34247       OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
34248                pFile->h, cnt, sqlite3ErrName(res)));
34249       if( cnt ) sqlite3_win32_sleep(1);
34250     }
34251     gotPendingLock = res;
34252     if( !res ){
34253       lastErrno = osGetLastError();
34254     }
34255   }
34256 
34257   /* Acquire a shared lock
34258   */
34259   if( locktype==SHARED_LOCK && res ){
34260     assert( pFile->locktype==NO_LOCK );
34261     res = winGetReadLock(pFile);
34262     if( res ){
34263       newLocktype = SHARED_LOCK;
34264     }else{
34265       lastErrno = osGetLastError();
34266     }
34267   }
34268 
34269   /* Acquire a RESERVED lock
34270   */
34271   if( locktype==RESERVED_LOCK && res ){
34272     assert( pFile->locktype==SHARED_LOCK );
34273     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
34274     if( res ){
34275       newLocktype = RESERVED_LOCK;
34276     }else{
34277       lastErrno = osGetLastError();
34278     }
34279   }
34280 
34281   /* Acquire a PENDING lock
34282   */
34283   if( locktype==EXCLUSIVE_LOCK && res ){
34284     newLocktype = PENDING_LOCK;
34285     gotPendingLock = 0;
34286   }
34287 
34288   /* Acquire an EXCLUSIVE lock
34289   */
34290   if( locktype==EXCLUSIVE_LOCK && res ){
34291     assert( pFile->locktype>=SHARED_LOCK );
34292     res = winUnlockReadLock(pFile);
34293     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
34294                       SHARED_SIZE, 0);
34295     if( res ){
34296       newLocktype = EXCLUSIVE_LOCK;
34297     }else{
34298       lastErrno = osGetLastError();
34299       winGetReadLock(pFile);
34300     }
34301   }
34302 
34303   /* If we are holding a PENDING lock that ought to be released, then
34304   ** release it now.
34305   */
34306   if( gotPendingLock && locktype==SHARED_LOCK ){
34307     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
34308   }
34309 
34310   /* Update the state of the lock has held in the file descriptor then
34311   ** return the appropriate result code.
34312   */
34313   if( res ){
34314     rc = SQLITE_OK;
34315   }else{
34316     pFile->lastErrno = lastErrno;
34317     rc = SQLITE_BUSY;
34318     OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
34319              pFile->h, locktype, newLocktype));
34320   }
34321   pFile->locktype = (u8)newLocktype;
34322   OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
34323            pFile->h, pFile->locktype, sqlite3ErrName(rc)));
34324   return rc;
34325 }
34326 
34327 /*
34328 ** This routine checks if there is a RESERVED lock held on the specified
34329 ** file by this or any other process. If such a lock is held, return
34330 ** non-zero, otherwise zero.
34331 */
34332 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
34333   int rc;
34334   winFile *pFile = (winFile*)id;
34335 
34336   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34337   OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
34338 
34339   assert( id!=0 );
34340   if( pFile->locktype>=RESERVED_LOCK ){
34341     rc = 1;
34342     OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
34343   }else{
34344     rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
34345     if( rc ){
34346       winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
34347     }
34348     rc = !rc;
34349     OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
34350   }
34351   *pResOut = rc;
34352   OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
34353            pFile->h, pResOut, *pResOut));
34354   return SQLITE_OK;
34355 }
34356 
34357 /*
34358 ** Lower the locking level on file descriptor id to locktype.  locktype
34359 ** must be either NO_LOCK or SHARED_LOCK.
34360 **
34361 ** If the locking level of the file descriptor is already at or below
34362 ** the requested locking level, this routine is a no-op.
34363 **
34364 ** It is not possible for this routine to fail if the second argument
34365 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
34366 ** might return SQLITE_IOERR;
34367 */
34368 static int winUnlock(sqlite3_file *id, int locktype){
34369   int type;
34370   winFile *pFile = (winFile*)id;
34371   int rc = SQLITE_OK;
34372   assert( pFile!=0 );
34373   assert( locktype<=SHARED_LOCK );
34374   OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
34375            pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
34376   type = pFile->locktype;
34377   if( type>=EXCLUSIVE_LOCK ){
34378     winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34379     if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
34380       /* This should never happen.  We should always be able to
34381       ** reacquire the read lock */
34382       rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
34383                        "winUnlock", pFile->zPath);
34384     }
34385   }
34386   if( type>=RESERVED_LOCK ){
34387     winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
34388   }
34389   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
34390     winUnlockReadLock(pFile);
34391   }
34392   if( type>=PENDING_LOCK ){
34393     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
34394   }
34395   pFile->locktype = (u8)locktype;
34396   OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
34397            pFile->h, pFile->locktype, sqlite3ErrName(rc)));
34398   return rc;
34399 }
34400 
34401 /*
34402 ** If *pArg is inititially negative then this is a query.  Set *pArg to
34403 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
34404 **
34405 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
34406 */
34407 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
34408   if( *pArg<0 ){
34409     *pArg = (pFile->ctrlFlags & mask)!=0;
34410   }else if( (*pArg)==0 ){
34411     pFile->ctrlFlags &= ~mask;
34412   }else{
34413     pFile->ctrlFlags |= mask;
34414   }
34415 }
34416 
34417 /* Forward references to VFS helper methods used for temporary files */
34418 static int winGetTempname(sqlite3_vfs *, char **);
34419 static int winIsDir(const void *);
34420 static BOOL winIsDriveLetterAndColon(const char *);
34421 
34422 /*
34423 ** Control and query of the open file handle.
34424 */
34425 static int winFileControl(sqlite3_file *id, int op, void *pArg){
34426   winFile *pFile = (winFile*)id;
34427   OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
34428   switch( op ){
34429     case SQLITE_FCNTL_LOCKSTATE: {
34430       *(int*)pArg = pFile->locktype;
34431       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34432       return SQLITE_OK;
34433     }
34434     case SQLITE_LAST_ERRNO: {
34435       *(int*)pArg = (int)pFile->lastErrno;
34436       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34437       return SQLITE_OK;
34438     }
34439     case SQLITE_FCNTL_CHUNK_SIZE: {
34440       pFile->szChunk = *(int *)pArg;
34441       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34442       return SQLITE_OK;
34443     }
34444     case SQLITE_FCNTL_SIZE_HINT: {
34445       if( pFile->szChunk>0 ){
34446         sqlite3_int64 oldSz;
34447         int rc = winFileSize(id, &oldSz);
34448         if( rc==SQLITE_OK ){
34449           sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
34450           if( newSz>oldSz ){
34451             SimulateIOErrorBenign(1);
34452             rc = winTruncate(id, newSz);
34453             SimulateIOErrorBenign(0);
34454           }
34455         }
34456         OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
34457         return rc;
34458       }
34459       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34460       return SQLITE_OK;
34461     }
34462     case SQLITE_FCNTL_PERSIST_WAL: {
34463       winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
34464       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34465       return SQLITE_OK;
34466     }
34467     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
34468       winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
34469       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34470       return SQLITE_OK;
34471     }
34472     case SQLITE_FCNTL_VFSNAME: {
34473       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
34474       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34475       return SQLITE_OK;
34476     }
34477     case SQLITE_FCNTL_WIN32_AV_RETRY: {
34478       int *a = (int*)pArg;
34479       if( a[0]>0 ){
34480         winIoerrRetry = a[0];
34481       }else{
34482         a[0] = winIoerrRetry;
34483       }
34484       if( a[1]>0 ){
34485         winIoerrRetryDelay = a[1];
34486       }else{
34487         a[1] = winIoerrRetryDelay;
34488       }
34489       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34490       return SQLITE_OK;
34491     }
34492     case SQLITE_FCNTL_TEMPFILENAME: {
34493       char *zTFile = 0;
34494       int rc = winGetTempname(pFile->pVfs, &zTFile);
34495       if( rc==SQLITE_OK ){
34496         *(char**)pArg = zTFile;
34497       }
34498       OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
34499       return rc;
34500     }
34501 #if SQLITE_MAX_MMAP_SIZE>0
34502     case SQLITE_FCNTL_MMAP_SIZE: {
34503       i64 newLimit = *(i64*)pArg;
34504       int rc = SQLITE_OK;
34505       if( newLimit>sqlite3GlobalConfig.mxMmap ){
34506         newLimit = sqlite3GlobalConfig.mxMmap;
34507       }
34508       *(i64*)pArg = pFile->mmapSizeMax;
34509       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
34510         pFile->mmapSizeMax = newLimit;
34511         if( pFile->mmapSize>0 ){
34512           winUnmapfile(pFile);
34513           rc = winMapfile(pFile, -1);
34514         }
34515       }
34516       OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
34517       return rc;
34518     }
34519 #endif
34520   }
34521   OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
34522   return SQLITE_NOTFOUND;
34523 }
34524 
34525 /*
34526 ** Return the sector size in bytes of the underlying block device for
34527 ** the specified file. This is almost always 512 bytes, but may be
34528 ** larger for some devices.
34529 **
34530 ** SQLite code assumes this function cannot fail. It also assumes that
34531 ** if two files are created in the same file-system directory (i.e.
34532 ** a database and its journal file) that the sector size will be the
34533 ** same for both.
34534 */
34535 static int winSectorSize(sqlite3_file *id){
34536   (void)id;
34537   return SQLITE_DEFAULT_SECTOR_SIZE;
34538 }
34539 
34540 /*
34541 ** Return a vector of device characteristics.
34542 */
34543 static int winDeviceCharacteristics(sqlite3_file *id){
34544   winFile *p = (winFile*)id;
34545   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
34546          ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
34547 }
34548 
34549 /*
34550 ** Windows will only let you create file view mappings
34551 ** on allocation size granularity boundaries.
34552 ** During sqlite3_os_init() we do a GetSystemInfo()
34553 ** to get the granularity size.
34554 */
34555 static SYSTEM_INFO winSysInfo;
34556 
34557 #ifndef SQLITE_OMIT_WAL
34558 
34559 /*
34560 ** Helper functions to obtain and relinquish the global mutex. The
34561 ** global mutex is used to protect the winLockInfo objects used by
34562 ** this file, all of which may be shared by multiple threads.
34563 **
34564 ** Function winShmMutexHeld() is used to assert() that the global mutex
34565 ** is held when required. This function is only used as part of assert()
34566 ** statements. e.g.
34567 **
34568 **   winShmEnterMutex()
34569 **     assert( winShmMutexHeld() );
34570 **   winShmLeaveMutex()
34571 */
34572 static void winShmEnterMutex(void){
34573   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
34574 }
34575 static void winShmLeaveMutex(void){
34576   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
34577 }
34578 #ifndef NDEBUG
34579 static int winShmMutexHeld(void) {
34580   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
34581 }
34582 #endif
34583 
34584 /*
34585 ** Object used to represent a single file opened and mmapped to provide
34586 ** shared memory.  When multiple threads all reference the same
34587 ** log-summary, each thread has its own winFile object, but they all
34588 ** point to a single instance of this object.  In other words, each
34589 ** log-summary is opened only once per process.
34590 **
34591 ** winShmMutexHeld() must be true when creating or destroying
34592 ** this object or while reading or writing the following fields:
34593 **
34594 **      nRef
34595 **      pNext
34596 **
34597 ** The following fields are read-only after the object is created:
34598 **
34599 **      fid
34600 **      zFilename
34601 **
34602 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
34603 ** winShmMutexHeld() is true when reading or writing any other field
34604 ** in this structure.
34605 **
34606 */
34607 struct winShmNode {
34608   sqlite3_mutex *mutex;      /* Mutex to access this object */
34609   char *zFilename;           /* Name of the file */
34610   winFile hFile;             /* File handle from winOpen */
34611 
34612   int szRegion;              /* Size of shared-memory regions */
34613   int nRegion;               /* Size of array apRegion */
34614   struct ShmRegion {
34615     HANDLE hMap;             /* File handle from CreateFileMapping */
34616     void *pMap;
34617   } *aRegion;
34618   DWORD lastErrno;           /* The Windows errno from the last I/O error */
34619 
34620   int nRef;                  /* Number of winShm objects pointing to this */
34621   winShm *pFirst;            /* All winShm objects pointing to this */
34622   winShmNode *pNext;         /* Next in list of all winShmNode objects */
34623 #ifdef SQLITE_DEBUG
34624   u8 nextShmId;              /* Next available winShm.id value */
34625 #endif
34626 };
34627 
34628 /*
34629 ** A global array of all winShmNode objects.
34630 **
34631 ** The winShmMutexHeld() must be true while reading or writing this list.
34632 */
34633 static winShmNode *winShmNodeList = 0;
34634 
34635 /*
34636 ** Structure used internally by this VFS to record the state of an
34637 ** open shared memory connection.
34638 **
34639 ** The following fields are initialized when this object is created and
34640 ** are read-only thereafter:
34641 **
34642 **    winShm.pShmNode
34643 **    winShm.id
34644 **
34645 ** All other fields are read/write.  The winShm.pShmNode->mutex must be held
34646 ** while accessing any read/write fields.
34647 */
34648 struct winShm {
34649   winShmNode *pShmNode;      /* The underlying winShmNode object */
34650   winShm *pNext;             /* Next winShm with the same winShmNode */
34651   u8 hasMutex;               /* True if holding the winShmNode mutex */
34652   u16 sharedMask;            /* Mask of shared locks held */
34653   u16 exclMask;              /* Mask of exclusive locks held */
34654 #ifdef SQLITE_DEBUG
34655   u8 id;                     /* Id of this connection with its winShmNode */
34656 #endif
34657 };
34658 
34659 /*
34660 ** Constants used for locking
34661 */
34662 #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
34663 #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
34664 
34665 /*
34666 ** Apply advisory locks for all n bytes beginning at ofst.
34667 */
34668 #define _SHM_UNLCK  1
34669 #define _SHM_RDLCK  2
34670 #define _SHM_WRLCK  3
34671 static int winShmSystemLock(
34672   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
34673   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
34674   int ofst,             /* Offset to first byte to be locked/unlocked */
34675   int nByte             /* Number of bytes to lock or unlock */
34676 ){
34677   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
34678 
34679   /* Access to the winShmNode object is serialized by the caller */
34680   assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
34681 
34682   OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
34683            pFile->hFile.h, lockType, ofst, nByte));
34684 
34685   /* Release/Acquire the system-level lock */
34686   if( lockType==_SHM_UNLCK ){
34687     rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
34688   }else{
34689     /* Initialize the locking parameters */
34690     DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
34691     if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
34692     rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
34693   }
34694 
34695   if( rc!= 0 ){
34696     rc = SQLITE_OK;
34697   }else{
34698     pFile->lastErrno =  osGetLastError();
34699     rc = SQLITE_BUSY;
34700   }
34701 
34702   OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
34703            pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
34704            "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
34705 
34706   return rc;
34707 }
34708 
34709 /* Forward references to VFS methods */
34710 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
34711 static int winDelete(sqlite3_vfs *,const char*,int);
34712 
34713 /*
34714 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
34715 **
34716 ** This is not a VFS shared-memory method; it is a utility function called
34717 ** by VFS shared-memory methods.
34718 */
34719 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
34720   winShmNode **pp;
34721   winShmNode *p;
34722   assert( winShmMutexHeld() );
34723   OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
34724            osGetCurrentProcessId(), deleteFlag));
34725   pp = &winShmNodeList;
34726   while( (p = *pp)!=0 ){
34727     if( p->nRef==0 ){
34728       int i;
34729       if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
34730       for(i=0; i<p->nRegion; i++){
34731         BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
34732         OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
34733                  osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
34734         UNUSED_VARIABLE_VALUE(bRc);
34735         bRc = osCloseHandle(p->aRegion[i].hMap);
34736         OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
34737                  osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
34738         UNUSED_VARIABLE_VALUE(bRc);
34739       }
34740       if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
34741         SimulateIOErrorBenign(1);
34742         winClose((sqlite3_file *)&p->hFile);
34743         SimulateIOErrorBenign(0);
34744       }
34745       if( deleteFlag ){
34746         SimulateIOErrorBenign(1);
34747         sqlite3BeginBenignMalloc();
34748         winDelete(pVfs, p->zFilename, 0);
34749         sqlite3EndBenignMalloc();
34750         SimulateIOErrorBenign(0);
34751       }
34752       *pp = p->pNext;
34753       sqlite3_free(p->aRegion);
34754       sqlite3_free(p);
34755     }else{
34756       pp = &p->pNext;
34757     }
34758   }
34759 }
34760 
34761 /*
34762 ** Open the shared-memory area associated with database file pDbFd.
34763 **
34764 ** When opening a new shared-memory file, if no other instances of that
34765 ** file are currently open, in this process or in other processes, then
34766 ** the file must be truncated to zero length or have its header cleared.
34767 */
34768 static int winOpenSharedMemory(winFile *pDbFd){
34769   struct winShm *p;                  /* The connection to be opened */
34770   struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
34771   int rc;                            /* Result code */
34772   struct winShmNode *pNew;           /* Newly allocated winShmNode */
34773   int nName;                         /* Size of zName in bytes */
34774 
34775   assert( pDbFd->pShm==0 );    /* Not previously opened */
34776 
34777   /* Allocate space for the new sqlite3_shm object.  Also speculatively
34778   ** allocate space for a new winShmNode and filename.
34779   */
34780   p = sqlite3MallocZero( sizeof(*p) );
34781   if( p==0 ) return SQLITE_IOERR_NOMEM;
34782   nName = sqlite3Strlen30(pDbFd->zPath);
34783   pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
34784   if( pNew==0 ){
34785     sqlite3_free(p);
34786     return SQLITE_IOERR_NOMEM;
34787   }
34788   pNew->zFilename = (char*)&pNew[1];
34789   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
34790   sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
34791 
34792   /* Look to see if there is an existing winShmNode that can be used.
34793   ** If no matching winShmNode currently exists, create a new one.
34794   */
34795   winShmEnterMutex();
34796   for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
34797     /* TBD need to come up with better match here.  Perhaps
34798     ** use FILE_ID_BOTH_DIR_INFO Structure.
34799     */
34800     if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
34801   }
34802   if( pShmNode ){
34803     sqlite3_free(pNew);
34804   }else{
34805     pShmNode = pNew;
34806     pNew = 0;
34807     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
34808     pShmNode->pNext = winShmNodeList;
34809     winShmNodeList = pShmNode;
34810 
34811     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
34812     if( pShmNode->mutex==0 ){
34813       rc = SQLITE_IOERR_NOMEM;
34814       goto shm_open_err;
34815     }
34816 
34817     rc = winOpen(pDbFd->pVfs,
34818                  pShmNode->zFilename,             /* Name of the file (UTF-8) */
34819                  (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
34820                  SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
34821                  0);
34822     if( SQLITE_OK!=rc ){
34823       goto shm_open_err;
34824     }
34825 
34826     /* Check to see if another process is holding the dead-man switch.
34827     ** If not, truncate the file to zero length.
34828     */
34829     if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
34830       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
34831       if( rc!=SQLITE_OK ){
34832         rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
34833                          "winOpenShm", pDbFd->zPath);
34834       }
34835     }
34836     if( rc==SQLITE_OK ){
34837       winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
34838       rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
34839     }
34840     if( rc ) goto shm_open_err;
34841   }
34842 
34843   /* Make the new connection a child of the winShmNode */
34844   p->pShmNode = pShmNode;
34845 #ifdef SQLITE_DEBUG
34846   p->id = pShmNode->nextShmId++;
34847 #endif
34848   pShmNode->nRef++;
34849   pDbFd->pShm = p;
34850   winShmLeaveMutex();
34851 
34852   /* The reference count on pShmNode has already been incremented under
34853   ** the cover of the winShmEnterMutex() mutex and the pointer from the
34854   ** new (struct winShm) object to the pShmNode has been set. All that is
34855   ** left to do is to link the new object into the linked list starting
34856   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
34857   ** mutex.
34858   */
34859   sqlite3_mutex_enter(pShmNode->mutex);
34860   p->pNext = pShmNode->pFirst;
34861   pShmNode->pFirst = p;
34862   sqlite3_mutex_leave(pShmNode->mutex);
34863   return SQLITE_OK;
34864 
34865   /* Jump here on any error */
34866 shm_open_err:
34867   winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
34868   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
34869   sqlite3_free(p);
34870   sqlite3_free(pNew);
34871   winShmLeaveMutex();
34872   return rc;
34873 }
34874 
34875 /*
34876 ** Close a connection to shared-memory.  Delete the underlying
34877 ** storage if deleteFlag is true.
34878 */
34879 static int winShmUnmap(
34880   sqlite3_file *fd,          /* Database holding shared memory */
34881   int deleteFlag             /* Delete after closing if true */
34882 ){
34883   winFile *pDbFd;       /* Database holding shared-memory */
34884   winShm *p;            /* The connection to be closed */
34885   winShmNode *pShmNode; /* The underlying shared-memory file */
34886   winShm **pp;          /* For looping over sibling connections */
34887 
34888   pDbFd = (winFile*)fd;
34889   p = pDbFd->pShm;
34890   if( p==0 ) return SQLITE_OK;
34891   pShmNode = p->pShmNode;
34892 
34893   /* Remove connection p from the set of connections associated
34894   ** with pShmNode */
34895   sqlite3_mutex_enter(pShmNode->mutex);
34896   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
34897   *pp = p->pNext;
34898 
34899   /* Free the connection p */
34900   sqlite3_free(p);
34901   pDbFd->pShm = 0;
34902   sqlite3_mutex_leave(pShmNode->mutex);
34903 
34904   /* If pShmNode->nRef has reached 0, then close the underlying
34905   ** shared-memory file, too */
34906   winShmEnterMutex();
34907   assert( pShmNode->nRef>0 );
34908   pShmNode->nRef--;
34909   if( pShmNode->nRef==0 ){
34910     winShmPurge(pDbFd->pVfs, deleteFlag);
34911   }
34912   winShmLeaveMutex();
34913 
34914   return SQLITE_OK;
34915 }
34916 
34917 /*
34918 ** Change the lock state for a shared-memory segment.
34919 */
34920 static int winShmLock(
34921   sqlite3_file *fd,          /* Database file holding the shared memory */
34922   int ofst,                  /* First lock to acquire or release */
34923   int n,                     /* Number of locks to acquire or release */
34924   int flags                  /* What to do with the lock */
34925 ){
34926   winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
34927   winShm *p = pDbFd->pShm;              /* The shared memory being locked */
34928   winShm *pX;                           /* For looping over all siblings */
34929   winShmNode *pShmNode = p->pShmNode;
34930   int rc = SQLITE_OK;                   /* Result code */
34931   u16 mask;                             /* Mask of locks to take or release */
34932 
34933   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
34934   assert( n>=1 );
34935   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
34936        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
34937        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
34938        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
34939   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
34940 
34941   mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
34942   assert( n>1 || mask==(1<<ofst) );
34943   sqlite3_mutex_enter(pShmNode->mutex);
34944   if( flags & SQLITE_SHM_UNLOCK ){
34945     u16 allMask = 0; /* Mask of locks held by siblings */
34946 
34947     /* See if any siblings hold this same lock */
34948     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
34949       if( pX==p ) continue;
34950       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
34951       allMask |= pX->sharedMask;
34952     }
34953 
34954     /* Unlock the system-level locks */
34955     if( (mask & allMask)==0 ){
34956       rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
34957     }else{
34958       rc = SQLITE_OK;
34959     }
34960 
34961     /* Undo the local locks */
34962     if( rc==SQLITE_OK ){
34963       p->exclMask &= ~mask;
34964       p->sharedMask &= ~mask;
34965     }
34966   }else if( flags & SQLITE_SHM_SHARED ){
34967     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
34968 
34969     /* Find out which shared locks are already held by sibling connections.
34970     ** If any sibling already holds an exclusive lock, go ahead and return
34971     ** SQLITE_BUSY.
34972     */
34973     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
34974       if( (pX->exclMask & mask)!=0 ){
34975         rc = SQLITE_BUSY;
34976         break;
34977       }
34978       allShared |= pX->sharedMask;
34979     }
34980 
34981     /* Get shared locks at the system level, if necessary */
34982     if( rc==SQLITE_OK ){
34983       if( (allShared & mask)==0 ){
34984         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
34985       }else{
34986         rc = SQLITE_OK;
34987       }
34988     }
34989 
34990     /* Get the local shared locks */
34991     if( rc==SQLITE_OK ){
34992       p->sharedMask |= mask;
34993     }
34994   }else{
34995     /* Make sure no sibling connections hold locks that will block this
34996     ** lock.  If any do, return SQLITE_BUSY right away.
34997     */
34998     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
34999       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
35000         rc = SQLITE_BUSY;
35001         break;
35002       }
35003     }
35004 
35005     /* Get the exclusive locks at the system level.  Then if successful
35006     ** also mark the local connection as being locked.
35007     */
35008     if( rc==SQLITE_OK ){
35009       rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
35010       if( rc==SQLITE_OK ){
35011         assert( (p->sharedMask & mask)==0 );
35012         p->exclMask |= mask;
35013       }
35014     }
35015   }
35016   sqlite3_mutex_leave(pShmNode->mutex);
35017   OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
35018            osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
35019            sqlite3ErrName(rc)));
35020   return rc;
35021 }
35022 
35023 /*
35024 ** Implement a memory barrier or memory fence on shared memory.
35025 **
35026 ** All loads and stores begun before the barrier must complete before
35027 ** any load or store begun after the barrier.
35028 */
35029 static void winShmBarrier(
35030   sqlite3_file *fd          /* Database holding the shared memory */
35031 ){
35032   UNUSED_PARAMETER(fd);
35033   /* MemoryBarrier(); // does not work -- do not know why not */
35034   winShmEnterMutex();
35035   winShmLeaveMutex();
35036 }
35037 
35038 /*
35039 ** This function is called to obtain a pointer to region iRegion of the
35040 ** shared-memory associated with the database file fd. Shared-memory regions
35041 ** are numbered starting from zero. Each shared-memory region is szRegion
35042 ** bytes in size.
35043 **
35044 ** If an error occurs, an error code is returned and *pp is set to NULL.
35045 **
35046 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
35047 ** region has not been allocated (by any client, including one running in a
35048 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
35049 ** isWrite is non-zero and the requested shared-memory region has not yet
35050 ** been allocated, it is allocated by this function.
35051 **
35052 ** If the shared-memory region has already been allocated or is allocated by
35053 ** this call as described above, then it is mapped into this processes
35054 ** address space (if it is not already), *pp is set to point to the mapped
35055 ** memory and SQLITE_OK returned.
35056 */
35057 static int winShmMap(
35058   sqlite3_file *fd,               /* Handle open on database file */
35059   int iRegion,                    /* Region to retrieve */
35060   int szRegion,                   /* Size of regions */
35061   int isWrite,                    /* True to extend file if necessary */
35062   void volatile **pp              /* OUT: Mapped memory */
35063 ){
35064   winFile *pDbFd = (winFile*)fd;
35065   winShm *p = pDbFd->pShm;
35066   winShmNode *pShmNode;
35067   int rc = SQLITE_OK;
35068 
35069   if( !p ){
35070     rc = winOpenSharedMemory(pDbFd);
35071     if( rc!=SQLITE_OK ) return rc;
35072     p = pDbFd->pShm;
35073   }
35074   pShmNode = p->pShmNode;
35075 
35076   sqlite3_mutex_enter(pShmNode->mutex);
35077   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
35078 
35079   if( pShmNode->nRegion<=iRegion ){
35080     struct ShmRegion *apNew;           /* New aRegion[] array */
35081     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
35082     sqlite3_int64 sz;                  /* Current size of wal-index file */
35083 
35084     pShmNode->szRegion = szRegion;
35085 
35086     /* The requested region is not mapped into this processes address space.
35087     ** Check to see if it has been allocated (i.e. if the wal-index file is
35088     ** large enough to contain the requested region).
35089     */
35090     rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
35091     if( rc!=SQLITE_OK ){
35092       rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
35093                        "winShmMap1", pDbFd->zPath);
35094       goto shmpage_out;
35095     }
35096 
35097     if( sz<nByte ){
35098       /* The requested memory region does not exist. If isWrite is set to
35099       ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
35100       **
35101       ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
35102       ** the requested memory region.
35103       */
35104       if( !isWrite ) goto shmpage_out;
35105       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
35106       if( rc!=SQLITE_OK ){
35107         rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
35108                          "winShmMap2", pDbFd->zPath);
35109         goto shmpage_out;
35110       }
35111     }
35112 
35113     /* Map the requested memory region into this processes address space. */
35114     apNew = (struct ShmRegion *)sqlite3_realloc(
35115         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
35116     );
35117     if( !apNew ){
35118       rc = SQLITE_IOERR_NOMEM;
35119       goto shmpage_out;
35120     }
35121     pShmNode->aRegion = apNew;
35122 
35123     while( pShmNode->nRegion<=iRegion ){
35124       HANDLE hMap = NULL;         /* file-mapping handle */
35125       void *pMap = 0;             /* Mapped memory region */
35126 
35127 #if SQLITE_OS_WINRT
35128       hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
35129           NULL, PAGE_READWRITE, nByte, NULL
35130       );
35131 #elif defined(SQLITE_WIN32_HAS_WIDE)
35132       hMap = osCreateFileMappingW(pShmNode->hFile.h,
35133           NULL, PAGE_READWRITE, 0, nByte, NULL
35134       );
35135 #elif defined(SQLITE_WIN32_HAS_ANSI)
35136       hMap = osCreateFileMappingA(pShmNode->hFile.h,
35137           NULL, PAGE_READWRITE, 0, nByte, NULL
35138       );
35139 #endif
35140       OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
35141                osGetCurrentProcessId(), pShmNode->nRegion, nByte,
35142                hMap ? "ok" : "failed"));
35143       if( hMap ){
35144         int iOffset = pShmNode->nRegion*szRegion;
35145         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
35146 #if SQLITE_OS_WINRT
35147         pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
35148             iOffset - iOffsetShift, szRegion + iOffsetShift
35149         );
35150 #else
35151         pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
35152             0, iOffset - iOffsetShift, szRegion + iOffsetShift
35153         );
35154 #endif
35155         OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
35156                  osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
35157                  szRegion, pMap ? "ok" : "failed"));
35158       }
35159       if( !pMap ){
35160         pShmNode->lastErrno = osGetLastError();
35161         rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
35162                          "winShmMap3", pDbFd->zPath);
35163         if( hMap ) osCloseHandle(hMap);
35164         goto shmpage_out;
35165       }
35166 
35167       pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
35168       pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
35169       pShmNode->nRegion++;
35170     }
35171   }
35172 
35173 shmpage_out:
35174   if( pShmNode->nRegion>iRegion ){
35175     int iOffset = iRegion*szRegion;
35176     int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
35177     char *p = (char *)pShmNode->aRegion[iRegion].pMap;
35178     *pp = (void *)&p[iOffsetShift];
35179   }else{
35180     *pp = 0;
35181   }
35182   sqlite3_mutex_leave(pShmNode->mutex);
35183   return rc;
35184 }
35185 
35186 #else
35187 # define winShmMap     0
35188 # define winShmLock    0
35189 # define winShmBarrier 0
35190 # define winShmUnmap   0
35191 #endif /* #ifndef SQLITE_OMIT_WAL */
35192 
35193 /*
35194 ** Cleans up the mapped region of the specified file, if any.
35195 */
35196 #if SQLITE_MAX_MMAP_SIZE>0
35197 static int winUnmapfile(winFile *pFile){
35198   assert( pFile!=0 );
35199   OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
35200            "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
35201            osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
35202            pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
35203   if( pFile->pMapRegion ){
35204     if( !osUnmapViewOfFile(pFile->pMapRegion) ){
35205       pFile->lastErrno = osGetLastError();
35206       OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
35207                "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
35208                pFile->pMapRegion));
35209       return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
35210                          "winUnmapfile1", pFile->zPath);
35211     }
35212     pFile->pMapRegion = 0;
35213     pFile->mmapSize = 0;
35214     pFile->mmapSizeActual = 0;
35215   }
35216   if( pFile->hMap!=NULL ){
35217     if( !osCloseHandle(pFile->hMap) ){
35218       pFile->lastErrno = osGetLastError();
35219       OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
35220                osGetCurrentProcessId(), pFile, pFile->hMap));
35221       return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
35222                          "winUnmapfile2", pFile->zPath);
35223     }
35224     pFile->hMap = NULL;
35225   }
35226   OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
35227            osGetCurrentProcessId(), pFile));
35228   return SQLITE_OK;
35229 }
35230 
35231 /*
35232 ** Memory map or remap the file opened by file-descriptor pFd (if the file
35233 ** is already mapped, the existing mapping is replaced by the new). Or, if
35234 ** there already exists a mapping for this file, and there are still
35235 ** outstanding xFetch() references to it, this function is a no-op.
35236 **
35237 ** If parameter nByte is non-negative, then it is the requested size of
35238 ** the mapping to create. Otherwise, if nByte is less than zero, then the
35239 ** requested size is the size of the file on disk. The actual size of the
35240 ** created mapping is either the requested size or the value configured
35241 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
35242 **
35243 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
35244 ** recreated as a result of outstanding references) or an SQLite error
35245 ** code otherwise.
35246 */
35247 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
35248   sqlite3_int64 nMap = nByte;
35249   int rc;
35250 
35251   assert( nMap>=0 || pFd->nFetchOut==0 );
35252   OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
35253            osGetCurrentProcessId(), pFd, nByte));
35254 
35255   if( pFd->nFetchOut>0 ) return SQLITE_OK;
35256 
35257   if( nMap<0 ){
35258     rc = winFileSize((sqlite3_file*)pFd, &nMap);
35259     if( rc ){
35260       OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
35261                osGetCurrentProcessId(), pFd));
35262       return SQLITE_IOERR_FSTAT;
35263     }
35264   }
35265   if( nMap>pFd->mmapSizeMax ){
35266     nMap = pFd->mmapSizeMax;
35267   }
35268   nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
35269 
35270   if( nMap==0 && pFd->mmapSize>0 ){
35271     winUnmapfile(pFd);
35272   }
35273   if( nMap!=pFd->mmapSize ){
35274     void *pNew = 0;
35275     DWORD protect = PAGE_READONLY;
35276     DWORD flags = FILE_MAP_READ;
35277 
35278     winUnmapfile(pFd);
35279     if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
35280       protect = PAGE_READWRITE;
35281       flags |= FILE_MAP_WRITE;
35282     }
35283 #if SQLITE_OS_WINRT
35284     pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
35285 #elif defined(SQLITE_WIN32_HAS_WIDE)
35286     pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
35287                                 (DWORD)((nMap>>32) & 0xffffffff),
35288                                 (DWORD)(nMap & 0xffffffff), NULL);
35289 #elif defined(SQLITE_WIN32_HAS_ANSI)
35290     pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
35291                                 (DWORD)((nMap>>32) & 0xffffffff),
35292                                 (DWORD)(nMap & 0xffffffff), NULL);
35293 #endif
35294     if( pFd->hMap==NULL ){
35295       pFd->lastErrno = osGetLastError();
35296       rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
35297                        "winMapfile1", pFd->zPath);
35298       /* Log the error, but continue normal operation using xRead/xWrite */
35299       OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
35300                osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
35301       return SQLITE_OK;
35302     }
35303     assert( (nMap % winSysInfo.dwPageSize)==0 );
35304     assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
35305 #if SQLITE_OS_WINRT
35306     pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
35307 #else
35308     pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
35309 #endif
35310     if( pNew==NULL ){
35311       osCloseHandle(pFd->hMap);
35312       pFd->hMap = NULL;
35313       pFd->lastErrno = osGetLastError();
35314       rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
35315                        "winMapfile2", pFd->zPath);
35316       /* Log the error, but continue normal operation using xRead/xWrite */
35317       OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
35318                osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
35319       return SQLITE_OK;
35320     }
35321     pFd->pMapRegion = pNew;
35322     pFd->mmapSize = nMap;
35323     pFd->mmapSizeActual = nMap;
35324   }
35325 
35326   OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
35327            osGetCurrentProcessId(), pFd));
35328   return SQLITE_OK;
35329 }
35330 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
35331 
35332 /*
35333 ** If possible, return a pointer to a mapping of file fd starting at offset
35334 ** iOff. The mapping must be valid for at least nAmt bytes.
35335 **
35336 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
35337 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
35338 ** Finally, if an error does occur, return an SQLite error code. The final
35339 ** value of *pp is undefined in this case.
35340 **
35341 ** If this function does return a pointer, the caller must eventually
35342 ** release the reference by calling winUnfetch().
35343 */
35344 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
35345 #if SQLITE_MAX_MMAP_SIZE>0
35346   winFile *pFd = (winFile*)fd;   /* The underlying database file */
35347 #endif
35348   *pp = 0;
35349 
35350   OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
35351            osGetCurrentProcessId(), fd, iOff, nAmt, pp));
35352 
35353 #if SQLITE_MAX_MMAP_SIZE>0
35354   if( pFd->mmapSizeMax>0 ){
35355     if( pFd->pMapRegion==0 ){
35356       int rc = winMapfile(pFd, -1);
35357       if( rc!=SQLITE_OK ){
35358         OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
35359                  osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
35360         return rc;
35361       }
35362     }
35363     if( pFd->mmapSize >= iOff+nAmt ){
35364       *pp = &((u8 *)pFd->pMapRegion)[iOff];
35365       pFd->nFetchOut++;
35366     }
35367   }
35368 #endif
35369 
35370   OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
35371            osGetCurrentProcessId(), fd, pp, *pp));
35372   return SQLITE_OK;
35373 }
35374 
35375 /*
35376 ** If the third argument is non-NULL, then this function releases a
35377 ** reference obtained by an earlier call to winFetch(). The second
35378 ** argument passed to this function must be the same as the corresponding
35379 ** argument that was passed to the winFetch() invocation.
35380 **
35381 ** Or, if the third argument is NULL, then this function is being called
35382 ** to inform the VFS layer that, according to POSIX, any existing mapping
35383 ** may now be invalid and should be unmapped.
35384 */
35385 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
35386 #if SQLITE_MAX_MMAP_SIZE>0
35387   winFile *pFd = (winFile*)fd;   /* The underlying database file */
35388 
35389   /* If p==0 (unmap the entire file) then there must be no outstanding
35390   ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
35391   ** then there must be at least one outstanding.  */
35392   assert( (p==0)==(pFd->nFetchOut==0) );
35393 
35394   /* If p!=0, it must match the iOff value. */
35395   assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
35396 
35397   OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
35398            osGetCurrentProcessId(), pFd, iOff, p));
35399 
35400   if( p ){
35401     pFd->nFetchOut--;
35402   }else{
35403     /* FIXME:  If Windows truly always prevents truncating or deleting a
35404     ** file while a mapping is held, then the following winUnmapfile() call
35405     ** is unnecessary can can be omitted - potentially improving
35406     ** performance.  */
35407     winUnmapfile(pFd);
35408   }
35409 
35410   assert( pFd->nFetchOut>=0 );
35411 #endif
35412 
35413   OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
35414            osGetCurrentProcessId(), fd));
35415   return SQLITE_OK;
35416 }
35417 
35418 /*
35419 ** Here ends the implementation of all sqlite3_file methods.
35420 **
35421 ********************** End sqlite3_file Methods *******************************
35422 ******************************************************************************/
35423 
35424 /*
35425 ** This vector defines all the methods that can operate on an
35426 ** sqlite3_file for win32.
35427 */
35428 static const sqlite3_io_methods winIoMethod = {
35429   3,                              /* iVersion */
35430   winClose,                       /* xClose */
35431   winRead,                        /* xRead */
35432   winWrite,                       /* xWrite */
35433   winTruncate,                    /* xTruncate */
35434   winSync,                        /* xSync */
35435   winFileSize,                    /* xFileSize */
35436   winLock,                        /* xLock */
35437   winUnlock,                      /* xUnlock */
35438   winCheckReservedLock,           /* xCheckReservedLock */
35439   winFileControl,                 /* xFileControl */
35440   winSectorSize,                  /* xSectorSize */
35441   winDeviceCharacteristics,       /* xDeviceCharacteristics */
35442   winShmMap,                      /* xShmMap */
35443   winShmLock,                     /* xShmLock */
35444   winShmBarrier,                  /* xShmBarrier */
35445   winShmUnmap,                    /* xShmUnmap */
35446   winFetch,                       /* xFetch */
35447   winUnfetch                      /* xUnfetch */
35448 };
35449 
35450 /****************************************************************************
35451 **************************** sqlite3_vfs methods ****************************
35452 **
35453 ** This division contains the implementation of methods on the
35454 ** sqlite3_vfs object.
35455 */
35456 
35457 #if defined(__CYGWIN__)
35458 /*
35459 ** Convert a filename from whatever the underlying operating system
35460 ** supports for filenames into UTF-8.  Space to hold the result is
35461 ** obtained from malloc and must be freed by the calling function.
35462 */
35463 static char *winConvertToUtf8Filename(const void *zFilename){
35464   char *zConverted = 0;
35465   if( osIsNT() ){
35466     zConverted = winUnicodeToUtf8(zFilename);
35467   }
35468 #ifdef SQLITE_WIN32_HAS_ANSI
35469   else{
35470     zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
35471   }
35472 #endif
35473   /* caller will handle out of memory */
35474   return zConverted;
35475 }
35476 #endif
35477 
35478 /*
35479 ** Convert a UTF-8 filename into whatever form the underlying
35480 ** operating system wants filenames in.  Space to hold the result
35481 ** is obtained from malloc and must be freed by the calling
35482 ** function.
35483 */
35484 static void *winConvertFromUtf8Filename(const char *zFilename){
35485   void *zConverted = 0;
35486   if( osIsNT() ){
35487     zConverted = winUtf8ToUnicode(zFilename);
35488   }
35489 #ifdef SQLITE_WIN32_HAS_ANSI
35490   else{
35491     zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
35492   }
35493 #endif
35494   /* caller will handle out of memory */
35495   return zConverted;
35496 }
35497 
35498 /*
35499 ** This function returns non-zero if the specified UTF-8 string buffer
35500 ** ends with a directory separator character or one was successfully
35501 ** added to it.
35502 */
35503 static int winMakeEndInDirSep(int nBuf, char *zBuf){
35504   if( zBuf ){
35505     int nLen = sqlite3Strlen30(zBuf);
35506     if( nLen>0 ){
35507       if( winIsDirSep(zBuf[nLen-1]) ){
35508         return 1;
35509       }else if( nLen+1<nBuf ){
35510         zBuf[nLen] = winGetDirSep();
35511         zBuf[nLen+1] = '\0';
35512         return 1;
35513       }
35514     }
35515   }
35516   return 0;
35517 }
35518 
35519 /*
35520 ** Create a temporary file name and store the resulting pointer into pzBuf.
35521 ** The pointer returned in pzBuf must be freed via sqlite3_free().
35522 */
35523 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
35524   static char zChars[] =
35525     "abcdefghijklmnopqrstuvwxyz"
35526     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35527     "0123456789";
35528   size_t i, j;
35529   int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
35530   int nMax, nBuf, nDir, nLen;
35531   char *zBuf;
35532 
35533   /* It's odd to simulate an io-error here, but really this is just
35534   ** using the io-error infrastructure to test that SQLite handles this
35535   ** function failing.
35536   */
35537   SimulateIOError( return SQLITE_IOERR );
35538 
35539   /* Allocate a temporary buffer to store the fully qualified file
35540   ** name for the temporary file.  If this fails, we cannot continue.
35541   */
35542   nMax = pVfs->mxPathname; nBuf = nMax + 2;
35543   zBuf = sqlite3MallocZero( nBuf );
35544   if( !zBuf ){
35545     OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35546     return SQLITE_IOERR_NOMEM;
35547   }
35548 
35549   /* Figure out the effective temporary directory.  First, check if one
35550   ** has been explicitly set by the application; otherwise, use the one
35551   ** configured by the operating system.
35552   */
35553   nDir = nMax - (nPre + 15);
35554   assert( nDir>0 );
35555   if( sqlite3_temp_directory ){
35556     int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
35557     if( nDirLen>0 ){
35558       if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
35559         nDirLen++;
35560       }
35561       if( nDirLen>nDir ){
35562         sqlite3_free(zBuf);
35563         OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
35564         return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
35565       }
35566       sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
35567     }
35568   }
35569 #if defined(__CYGWIN__)
35570   else{
35571     static const char *azDirs[] = {
35572        0, /* getenv("SQLITE_TMPDIR") */
35573        0, /* getenv("TMPDIR") */
35574        0, /* getenv("TMP") */
35575        0, /* getenv("TEMP") */
35576        0, /* getenv("USERPROFILE") */
35577        "/var/tmp",
35578        "/usr/tmp",
35579        "/tmp",
35580        ".",
35581        0        /* List terminator */
35582     };
35583     unsigned int i;
35584     const char *zDir = 0;
35585 
35586     if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
35587     if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
35588     if( !azDirs[2] ) azDirs[2] = getenv("TMP");
35589     if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
35590     if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
35591     for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
35592       void *zConverted;
35593       if( zDir==0 ) continue;
35594       /* If the path starts with a drive letter followed by the colon
35595       ** character, assume it is already a native Win32 path; otherwise,
35596       ** it must be converted to a native Win32 path via the Cygwin API
35597       ** prior to using it.
35598       */
35599       if( winIsDriveLetterAndColon(zDir) ){
35600         zConverted = winConvertFromUtf8Filename(zDir);
35601         if( !zConverted ){
35602           sqlite3_free(zBuf);
35603           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35604           return SQLITE_IOERR_NOMEM;
35605         }
35606         if( winIsDir(zConverted) ){
35607           sqlite3_snprintf(nMax, zBuf, "%s", zDir);
35608           sqlite3_free(zConverted);
35609           break;
35610         }
35611         sqlite3_free(zConverted);
35612       }else{
35613         zConverted = sqlite3MallocZero( nMax+1 );
35614         if( !zConverted ){
35615           sqlite3_free(zBuf);
35616           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35617           return SQLITE_IOERR_NOMEM;
35618         }
35619         if( cygwin_conv_path(
35620                 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
35621                 zConverted, nMax+1)<0 ){
35622           sqlite3_free(zConverted);
35623           sqlite3_free(zBuf);
35624           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
35625           return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
35626                              "winGetTempname2", zDir);
35627         }
35628         if( winIsDir(zConverted) ){
35629           /* At this point, we know the candidate directory exists and should
35630           ** be used.  However, we may need to convert the string containing
35631           ** its name into UTF-8 (i.e. if it is UTF-16 right now).
35632           */
35633           char *zUtf8 = winConvertToUtf8Filename(zConverted);
35634           if( !zUtf8 ){
35635             sqlite3_free(zConverted);
35636             sqlite3_free(zBuf);
35637             OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35638             return SQLITE_IOERR_NOMEM;
35639           }
35640           sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
35641           sqlite3_free(zUtf8);
35642           sqlite3_free(zConverted);
35643           break;
35644         }
35645         sqlite3_free(zConverted);
35646       }
35647     }
35648   }
35649 #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
35650   else if( osIsNT() ){
35651     char *zMulti;
35652     LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
35653     if( !zWidePath ){
35654       sqlite3_free(zBuf);
35655       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35656       return SQLITE_IOERR_NOMEM;
35657     }
35658     if( osGetTempPathW(nMax, zWidePath)==0 ){
35659       sqlite3_free(zWidePath);
35660       sqlite3_free(zBuf);
35661       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
35662       return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
35663                          "winGetTempname2", 0);
35664     }
35665     zMulti = winUnicodeToUtf8(zWidePath);
35666     if( zMulti ){
35667       sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
35668       sqlite3_free(zMulti);
35669       sqlite3_free(zWidePath);
35670     }else{
35671       sqlite3_free(zWidePath);
35672       sqlite3_free(zBuf);
35673       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35674       return SQLITE_IOERR_NOMEM;
35675     }
35676   }
35677 #ifdef SQLITE_WIN32_HAS_ANSI
35678   else{
35679     char *zUtf8;
35680     char *zMbcsPath = sqlite3MallocZero( nMax );
35681     if( !zMbcsPath ){
35682       sqlite3_free(zBuf);
35683       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35684       return SQLITE_IOERR_NOMEM;
35685     }
35686     if( osGetTempPathA(nMax, zMbcsPath)==0 ){
35687       sqlite3_free(zBuf);
35688       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
35689       return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
35690                          "winGetTempname3", 0);
35691     }
35692     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
35693     if( zUtf8 ){
35694       sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
35695       sqlite3_free(zUtf8);
35696     }else{
35697       sqlite3_free(zBuf);
35698       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35699       return SQLITE_IOERR_NOMEM;
35700     }
35701   }
35702 #endif /* SQLITE_WIN32_HAS_ANSI */
35703 #endif /* !SQLITE_OS_WINRT */
35704 
35705   /*
35706   ** Check to make sure the temporary directory ends with an appropriate
35707   ** separator.  If it does not and there is not enough space left to add
35708   ** one, fail.
35709   */
35710   if( !winMakeEndInDirSep(nDir+1, zBuf) ){
35711     sqlite3_free(zBuf);
35712     OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
35713     return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
35714   }
35715 
35716   /*
35717   ** Check that the output buffer is large enough for the temporary file
35718   ** name in the following format:
35719   **
35720   **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
35721   **
35722   ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
35723   ** account for the space used by the 15 character random suffix and the
35724   ** two trailing NUL characters.  The final directory separator character
35725   ** has already added if it was not already present.
35726   */
35727   nLen = sqlite3Strlen30(zBuf);
35728   if( (nLen + nPre + 17) > nBuf ){
35729     sqlite3_free(zBuf);
35730     OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
35731     return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
35732   }
35733 
35734   sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
35735 
35736   j = sqlite3Strlen30(zBuf);
35737   sqlite3_randomness(15, &zBuf[j]);
35738   for(i=0; i<15; i++, j++){
35739     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
35740   }
35741   zBuf[j] = 0;
35742   zBuf[j+1] = 0;
35743   *pzBuf = zBuf;
35744 
35745   OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
35746   return SQLITE_OK;
35747 }
35748 
35749 /*
35750 ** Return TRUE if the named file is really a directory.  Return false if
35751 ** it is something other than a directory, or if there is any kind of memory
35752 ** allocation failure.
35753 */
35754 static int winIsDir(const void *zConverted){
35755   DWORD attr;
35756   int rc = 0;
35757   DWORD lastErrno;
35758 
35759   if( osIsNT() ){
35760     int cnt = 0;
35761     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35762     memset(&sAttrData, 0, sizeof(sAttrData));
35763     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
35764                              GetFileExInfoStandard,
35765                              &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
35766     if( !rc ){
35767       return 0; /* Invalid name? */
35768     }
35769     attr = sAttrData.dwFileAttributes;
35770 #if SQLITE_OS_WINCE==0
35771   }else{
35772     attr = osGetFileAttributesA((char*)zConverted);
35773 #endif
35774   }
35775   return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
35776 }
35777 
35778 /*
35779 ** Open a file.
35780 */
35781 static int winOpen(
35782   sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
35783   const char *zName,        /* Name of the file (UTF-8) */
35784   sqlite3_file *id,         /* Write the SQLite file handle here */
35785   int flags,                /* Open mode flags */
35786   int *pOutFlags            /* Status return flags */
35787 ){
35788   HANDLE h;
35789   DWORD lastErrno = 0;
35790   DWORD dwDesiredAccess;
35791   DWORD dwShareMode;
35792   DWORD dwCreationDisposition;
35793   DWORD dwFlagsAndAttributes = 0;
35794 #if SQLITE_OS_WINCE
35795   int isTemp = 0;
35796 #endif
35797   winFile *pFile = (winFile*)id;
35798   void *zConverted;              /* Filename in OS encoding */
35799   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
35800   int cnt = 0;
35801 
35802   /* If argument zPath is a NULL pointer, this function is required to open
35803   ** a temporary file. Use this buffer to store the file name in.
35804   */
35805   char *zTmpname = 0; /* For temporary filename, if necessary. */
35806 
35807   int rc = SQLITE_OK;            /* Function Return Code */
35808 #if !defined(NDEBUG) || SQLITE_OS_WINCE
35809   int eType = flags&0xFFFFFF00;  /* Type of file to open */
35810 #endif
35811 
35812   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
35813   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
35814   int isCreate     = (flags & SQLITE_OPEN_CREATE);
35815   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
35816   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
35817 
35818 #ifndef NDEBUG
35819   int isOpenJournal = (isCreate && (
35820         eType==SQLITE_OPEN_MASTER_JOURNAL
35821      || eType==SQLITE_OPEN_MAIN_JOURNAL
35822      || eType==SQLITE_OPEN_WAL
35823   ));
35824 #endif
35825 
35826   OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
35827            zUtf8Name, id, flags, pOutFlags));
35828 
35829   /* Check the following statements are true:
35830   **
35831   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
35832   **   (b) if CREATE is set, then READWRITE must also be set, and
35833   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
35834   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
35835   */
35836   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
35837   assert(isCreate==0 || isReadWrite);
35838   assert(isExclusive==0 || isCreate);
35839   assert(isDelete==0 || isCreate);
35840 
35841   /* The main DB, main journal, WAL file and master journal are never
35842   ** automatically deleted. Nor are they ever temporary files.  */
35843   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
35844   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
35845   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
35846   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
35847 
35848   /* Assert that the upper layer has set one of the "file-type" flags. */
35849   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
35850        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
35851        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
35852        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
35853   );
35854 
35855   assert( pFile!=0 );
35856   memset(pFile, 0, sizeof(winFile));
35857   pFile->h = INVALID_HANDLE_VALUE;
35858 
35859 #if SQLITE_OS_WINRT
35860   if( !zUtf8Name && !sqlite3_temp_directory ){
35861     sqlite3_log(SQLITE_ERROR,
35862         "sqlite3_temp_directory variable should be set for WinRT");
35863   }
35864 #endif
35865 
35866   /* If the second argument to this function is NULL, generate a
35867   ** temporary file name to use
35868   */
35869   if( !zUtf8Name ){
35870     assert( isDelete && !isOpenJournal );
35871     rc = winGetTempname(pVfs, &zTmpname);
35872     if( rc!=SQLITE_OK ){
35873       OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
35874       return rc;
35875     }
35876     zUtf8Name = zTmpname;
35877   }
35878 
35879   /* Database filenames are double-zero terminated if they are not
35880   ** URIs with parameters.  Hence, they can always be passed into
35881   ** sqlite3_uri_parameter().
35882   */
35883   assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
35884        zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
35885 
35886   /* Convert the filename to the system encoding. */
35887   zConverted = winConvertFromUtf8Filename(zUtf8Name);
35888   if( zConverted==0 ){
35889     sqlite3_free(zTmpname);
35890     OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
35891     return SQLITE_IOERR_NOMEM;
35892   }
35893 
35894   if( winIsDir(zConverted) ){
35895     sqlite3_free(zConverted);
35896     sqlite3_free(zTmpname);
35897     OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
35898     return SQLITE_CANTOPEN_ISDIR;
35899   }
35900 
35901   if( isReadWrite ){
35902     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
35903   }else{
35904     dwDesiredAccess = GENERIC_READ;
35905   }
35906 
35907   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
35908   ** created. SQLite doesn't use it to indicate "exclusive access"
35909   ** as it is usually understood.
35910   */
35911   if( isExclusive ){
35912     /* Creates a new file, only if it does not already exist. */
35913     /* If the file exists, it fails. */
35914     dwCreationDisposition = CREATE_NEW;
35915   }else if( isCreate ){
35916     /* Open existing file, or create if it doesn't exist */
35917     dwCreationDisposition = OPEN_ALWAYS;
35918   }else{
35919     /* Opens a file, only if it exists. */
35920     dwCreationDisposition = OPEN_EXISTING;
35921   }
35922 
35923   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
35924 
35925   if( isDelete ){
35926 #if SQLITE_OS_WINCE
35927     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
35928     isTemp = 1;
35929 #else
35930     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
35931                                | FILE_ATTRIBUTE_HIDDEN
35932                                | FILE_FLAG_DELETE_ON_CLOSE;
35933 #endif
35934   }else{
35935     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
35936   }
35937   /* Reports from the internet are that performance is always
35938   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
35939 #if SQLITE_OS_WINCE
35940   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
35941 #endif
35942 
35943   if( osIsNT() ){
35944 #if SQLITE_OS_WINRT
35945     CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
35946     extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
35947     extendedParameters.dwFileAttributes =
35948             dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
35949     extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
35950     extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
35951     extendedParameters.lpSecurityAttributes = NULL;
35952     extendedParameters.hTemplateFile = NULL;
35953     while( (h = osCreateFile2((LPCWSTR)zConverted,
35954                               dwDesiredAccess,
35955                               dwShareMode,
35956                               dwCreationDisposition,
35957                               &extendedParameters))==INVALID_HANDLE_VALUE &&
35958                               winRetryIoerr(&cnt, &lastErrno) ){
35959                /* Noop */
35960     }
35961 #else
35962     while( (h = osCreateFileW((LPCWSTR)zConverted,
35963                               dwDesiredAccess,
35964                               dwShareMode, NULL,
35965                               dwCreationDisposition,
35966                               dwFlagsAndAttributes,
35967                               NULL))==INVALID_HANDLE_VALUE &&
35968                               winRetryIoerr(&cnt, &lastErrno) ){
35969                /* Noop */
35970     }
35971 #endif
35972   }
35973 #ifdef SQLITE_WIN32_HAS_ANSI
35974   else{
35975     while( (h = osCreateFileA((LPCSTR)zConverted,
35976                               dwDesiredAccess,
35977                               dwShareMode, NULL,
35978                               dwCreationDisposition,
35979                               dwFlagsAndAttributes,
35980                               NULL))==INVALID_HANDLE_VALUE &&
35981                               winRetryIoerr(&cnt, &lastErrno) ){
35982                /* Noop */
35983     }
35984   }
35985 #endif
35986   winLogIoerr(cnt);
35987 
35988   OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
35989            dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
35990 
35991   if( h==INVALID_HANDLE_VALUE ){
35992     pFile->lastErrno = lastErrno;
35993     winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
35994     sqlite3_free(zConverted);
35995     sqlite3_free(zTmpname);
35996     if( isReadWrite && !isExclusive ){
35997       return winOpen(pVfs, zName, id,
35998          ((flags|SQLITE_OPEN_READONLY) &
35999                      ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
36000          pOutFlags);
36001     }else{
36002       return SQLITE_CANTOPEN_BKPT;
36003     }
36004   }
36005 
36006   if( pOutFlags ){
36007     if( isReadWrite ){
36008       *pOutFlags = SQLITE_OPEN_READWRITE;
36009     }else{
36010       *pOutFlags = SQLITE_OPEN_READONLY;
36011     }
36012   }
36013 
36014   OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
36015            "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
36016            *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
36017 
36018 #if SQLITE_OS_WINCE
36019   if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
36020        && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
36021   ){
36022     osCloseHandle(h);
36023     sqlite3_free(zConverted);
36024     sqlite3_free(zTmpname);
36025     OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
36026     return rc;
36027   }
36028   if( isTemp ){
36029     pFile->zDeleteOnClose = zConverted;
36030   }else
36031 #endif
36032   {
36033     sqlite3_free(zConverted);
36034   }
36035 
36036   sqlite3_free(zTmpname);
36037   pFile->pMethod = &winIoMethod;
36038   pFile->pVfs = pVfs;
36039   pFile->h = h;
36040   if( isReadonly ){
36041     pFile->ctrlFlags |= WINFILE_RDONLY;
36042   }
36043   if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
36044     pFile->ctrlFlags |= WINFILE_PSOW;
36045   }
36046   pFile->lastErrno = NO_ERROR;
36047   pFile->zPath = zName;
36048 #if SQLITE_MAX_MMAP_SIZE>0
36049   pFile->hMap = NULL;
36050   pFile->pMapRegion = 0;
36051   pFile->mmapSize = 0;
36052   pFile->mmapSizeActual = 0;
36053   pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
36054 #endif
36055 
36056   OpenCounter(+1);
36057   return rc;
36058 }
36059 
36060 /*
36061 ** Delete the named file.
36062 **
36063 ** Note that Windows does not allow a file to be deleted if some other
36064 ** process has it open.  Sometimes a virus scanner or indexing program
36065 ** will open a journal file shortly after it is created in order to do
36066 ** whatever it does.  While this other process is holding the
36067 ** file open, we will be unable to delete it.  To work around this
36068 ** problem, we delay 100 milliseconds and try to delete again.  Up
36069 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
36070 ** up and returning an error.
36071 */
36072 static int winDelete(
36073   sqlite3_vfs *pVfs,          /* Not used on win32 */
36074   const char *zFilename,      /* Name of file to delete */
36075   int syncDir                 /* Not used on win32 */
36076 ){
36077   int cnt = 0;
36078   int rc;
36079   DWORD attr;
36080   DWORD lastErrno = 0;
36081   void *zConverted;
36082   UNUSED_PARAMETER(pVfs);
36083   UNUSED_PARAMETER(syncDir);
36084 
36085   SimulateIOError(return SQLITE_IOERR_DELETE);
36086   OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
36087 
36088   zConverted = winConvertFromUtf8Filename(zFilename);
36089   if( zConverted==0 ){
36090     OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
36091     return SQLITE_IOERR_NOMEM;
36092   }
36093   if( osIsNT() ){
36094     do {
36095 #if SQLITE_OS_WINRT
36096       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
36097       memset(&sAttrData, 0, sizeof(sAttrData));
36098       if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
36099                                   &sAttrData) ){
36100         attr = sAttrData.dwFileAttributes;
36101       }else{
36102         lastErrno = osGetLastError();
36103         if( lastErrno==ERROR_FILE_NOT_FOUND
36104          || lastErrno==ERROR_PATH_NOT_FOUND ){
36105           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
36106         }else{
36107           rc = SQLITE_ERROR;
36108         }
36109         break;
36110       }
36111 #else
36112       attr = osGetFileAttributesW(zConverted);
36113 #endif
36114       if ( attr==INVALID_FILE_ATTRIBUTES ){
36115         lastErrno = osGetLastError();
36116         if( lastErrno==ERROR_FILE_NOT_FOUND
36117          || lastErrno==ERROR_PATH_NOT_FOUND ){
36118           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
36119         }else{
36120           rc = SQLITE_ERROR;
36121         }
36122         break;
36123       }
36124       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
36125         rc = SQLITE_ERROR; /* Files only. */
36126         break;
36127       }
36128       if ( osDeleteFileW(zConverted) ){
36129         rc = SQLITE_OK; /* Deleted OK. */
36130         break;
36131       }
36132       if ( !winRetryIoerr(&cnt, &lastErrno) ){
36133         rc = SQLITE_ERROR; /* No more retries. */
36134         break;
36135       }
36136     } while(1);
36137   }
36138 #ifdef SQLITE_WIN32_HAS_ANSI
36139   else{
36140     do {
36141       attr = osGetFileAttributesA(zConverted);
36142       if ( attr==INVALID_FILE_ATTRIBUTES ){
36143         lastErrno = osGetLastError();
36144         if( lastErrno==ERROR_FILE_NOT_FOUND
36145          || lastErrno==ERROR_PATH_NOT_FOUND ){
36146           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
36147         }else{
36148           rc = SQLITE_ERROR;
36149         }
36150         break;
36151       }
36152       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
36153         rc = SQLITE_ERROR; /* Files only. */
36154         break;
36155       }
36156       if ( osDeleteFileA(zConverted) ){
36157         rc = SQLITE_OK; /* Deleted OK. */
36158         break;
36159       }
36160       if ( !winRetryIoerr(&cnt, &lastErrno) ){
36161         rc = SQLITE_ERROR; /* No more retries. */
36162         break;
36163       }
36164     } while(1);
36165   }
36166 #endif
36167   if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
36168     rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
36169   }else{
36170     winLogIoerr(cnt);
36171   }
36172   sqlite3_free(zConverted);
36173   OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
36174   return rc;
36175 }
36176 
36177 /*
36178 ** Check the existence and status of a file.
36179 */
36180 static int winAccess(
36181   sqlite3_vfs *pVfs,         /* Not used on win32 */
36182   const char *zFilename,     /* Name of file to check */
36183   int flags,                 /* Type of test to make on this file */
36184   int *pResOut               /* OUT: Result */
36185 ){
36186   DWORD attr;
36187   int rc = 0;
36188   DWORD lastErrno = 0;
36189   void *zConverted;
36190   UNUSED_PARAMETER(pVfs);
36191 
36192   SimulateIOError( return SQLITE_IOERR_ACCESS; );
36193   OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
36194            zFilename, flags, pResOut));
36195 
36196   zConverted = winConvertFromUtf8Filename(zFilename);
36197   if( zConverted==0 ){
36198     OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
36199     return SQLITE_IOERR_NOMEM;
36200   }
36201   if( osIsNT() ){
36202     int cnt = 0;
36203     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
36204     memset(&sAttrData, 0, sizeof(sAttrData));
36205     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
36206                              GetFileExInfoStandard,
36207                              &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
36208     if( rc ){
36209       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
36210       ** as if it does not exist.
36211       */
36212       if(    flags==SQLITE_ACCESS_EXISTS
36213           && sAttrData.nFileSizeHigh==0
36214           && sAttrData.nFileSizeLow==0 ){
36215         attr = INVALID_FILE_ATTRIBUTES;
36216       }else{
36217         attr = sAttrData.dwFileAttributes;
36218       }
36219     }else{
36220       winLogIoerr(cnt);
36221       if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
36222         sqlite3_free(zConverted);
36223         return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
36224                            zFilename);
36225       }else{
36226         attr = INVALID_FILE_ATTRIBUTES;
36227       }
36228     }
36229   }
36230 #ifdef SQLITE_WIN32_HAS_ANSI
36231   else{
36232     attr = osGetFileAttributesA((char*)zConverted);
36233   }
36234 #endif
36235   sqlite3_free(zConverted);
36236   switch( flags ){
36237     case SQLITE_ACCESS_READ:
36238     case SQLITE_ACCESS_EXISTS:
36239       rc = attr!=INVALID_FILE_ATTRIBUTES;
36240       break;
36241     case SQLITE_ACCESS_READWRITE:
36242       rc = attr!=INVALID_FILE_ATTRIBUTES &&
36243              (attr & FILE_ATTRIBUTE_READONLY)==0;
36244       break;
36245     default:
36246       assert(!"Invalid flags argument");
36247   }
36248   *pResOut = rc;
36249   OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
36250            zFilename, pResOut, *pResOut));
36251   return SQLITE_OK;
36252 }
36253 
36254 /*
36255 ** Returns non-zero if the specified path name starts with a drive letter
36256 ** followed by a colon character.
36257 */
36258 static BOOL winIsDriveLetterAndColon(
36259   const char *zPathname
36260 ){
36261   return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
36262 }
36263 
36264 /*
36265 ** Returns non-zero if the specified path name should be used verbatim.  If
36266 ** non-zero is returned from this function, the calling function must simply
36267 ** use the provided path name verbatim -OR- resolve it into a full path name
36268 ** using the GetFullPathName Win32 API function (if available).
36269 */
36270 static BOOL winIsVerbatimPathname(
36271   const char *zPathname
36272 ){
36273   /*
36274   ** If the path name starts with a forward slash or a backslash, it is either
36275   ** a legal UNC name, a volume relative path, or an absolute path name in the
36276   ** "Unix" format on Windows.  There is no easy way to differentiate between
36277   ** the final two cases; therefore, we return the safer return value of TRUE
36278   ** so that callers of this function will simply use it verbatim.
36279   */
36280   if ( winIsDirSep(zPathname[0]) ){
36281     return TRUE;
36282   }
36283 
36284   /*
36285   ** If the path name starts with a letter and a colon it is either a volume
36286   ** relative path or an absolute path.  Callers of this function must not
36287   ** attempt to treat it as a relative path name (i.e. they should simply use
36288   ** it verbatim).
36289   */
36290   if ( winIsDriveLetterAndColon(zPathname) ){
36291     return TRUE;
36292   }
36293 
36294   /*
36295   ** If we get to this point, the path name should almost certainly be a purely
36296   ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
36297   */
36298   return FALSE;
36299 }
36300 
36301 /*
36302 ** Turn a relative pathname into a full pathname.  Write the full
36303 ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
36304 ** bytes in size.
36305 */
36306 static int winFullPathname(
36307   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
36308   const char *zRelative,        /* Possibly relative input path */
36309   int nFull,                    /* Size of output buffer in bytes */
36310   char *zFull                   /* Output buffer */
36311 ){
36312 
36313 #if defined(__CYGWIN__)
36314   SimulateIOError( return SQLITE_ERROR );
36315   UNUSED_PARAMETER(nFull);
36316   assert( nFull>=pVfs->mxPathname );
36317   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
36318     /*
36319     ** NOTE: We are dealing with a relative path name and the data
36320     **       directory has been set.  Therefore, use it as the basis
36321     **       for converting the relative path name to an absolute
36322     **       one by prepending the data directory and a slash.
36323     */
36324     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
36325     if( !zOut ){
36326       return SQLITE_IOERR_NOMEM;
36327     }
36328     if( cygwin_conv_path(
36329             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
36330             CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
36331       sqlite3_free(zOut);
36332       return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
36333                          "winFullPathname1", zRelative);
36334     }else{
36335       char *zUtf8 = winConvertToUtf8Filename(zOut);
36336       if( !zUtf8 ){
36337         sqlite3_free(zOut);
36338         return SQLITE_IOERR_NOMEM;
36339       }
36340       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
36341                        sqlite3_data_directory, winGetDirSep(), zUtf8);
36342       sqlite3_free(zUtf8);
36343       sqlite3_free(zOut);
36344     }
36345   }else{
36346     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
36347     if( !zOut ){
36348       return SQLITE_IOERR_NOMEM;
36349     }
36350     if( cygwin_conv_path(
36351             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
36352             zRelative, zOut, pVfs->mxPathname+1)<0 ){
36353       sqlite3_free(zOut);
36354       return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
36355                          "winFullPathname2", zRelative);
36356     }else{
36357       char *zUtf8 = winConvertToUtf8Filename(zOut);
36358       if( !zUtf8 ){
36359         sqlite3_free(zOut);
36360         return SQLITE_IOERR_NOMEM;
36361       }
36362       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
36363       sqlite3_free(zUtf8);
36364       sqlite3_free(zOut);
36365     }
36366   }
36367   return SQLITE_OK;
36368 #endif
36369 
36370 #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
36371   SimulateIOError( return SQLITE_ERROR );
36372   /* WinCE has no concept of a relative pathname, or so I am told. */
36373   /* WinRT has no way to convert a relative path to an absolute one. */
36374   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
36375     /*
36376     ** NOTE: We are dealing with a relative path name and the data
36377     **       directory has been set.  Therefore, use it as the basis
36378     **       for converting the relative path name to an absolute
36379     **       one by prepending the data directory and a backslash.
36380     */
36381     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
36382                      sqlite3_data_directory, winGetDirSep(), zRelative);
36383   }else{
36384     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
36385   }
36386   return SQLITE_OK;
36387 #endif
36388 
36389 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
36390   DWORD nByte;
36391   void *zConverted;
36392   char *zOut;
36393 
36394   /* If this path name begins with "/X:", where "X" is any alphabetic
36395   ** character, discard the initial "/" from the pathname.
36396   */
36397   if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
36398     zRelative++;
36399   }
36400 
36401   /* It's odd to simulate an io-error here, but really this is just
36402   ** using the io-error infrastructure to test that SQLite handles this
36403   ** function failing. This function could fail if, for example, the
36404   ** current working directory has been unlinked.
36405   */
36406   SimulateIOError( return SQLITE_ERROR );
36407   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
36408     /*
36409     ** NOTE: We are dealing with a relative path name and the data
36410     **       directory has been set.  Therefore, use it as the basis
36411     **       for converting the relative path name to an absolute
36412     **       one by prepending the data directory and a backslash.
36413     */
36414     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
36415                      sqlite3_data_directory, winGetDirSep(), zRelative);
36416     return SQLITE_OK;
36417   }
36418   zConverted = winConvertFromUtf8Filename(zRelative);
36419   if( zConverted==0 ){
36420     return SQLITE_IOERR_NOMEM;
36421   }
36422   if( osIsNT() ){
36423     LPWSTR zTemp;
36424     nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
36425     if( nByte==0 ){
36426       sqlite3_free(zConverted);
36427       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36428                          "winFullPathname1", zRelative);
36429     }
36430     nByte += 3;
36431     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
36432     if( zTemp==0 ){
36433       sqlite3_free(zConverted);
36434       return SQLITE_IOERR_NOMEM;
36435     }
36436     nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
36437     if( nByte==0 ){
36438       sqlite3_free(zConverted);
36439       sqlite3_free(zTemp);
36440       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36441                          "winFullPathname2", zRelative);
36442     }
36443     sqlite3_free(zConverted);
36444     zOut = winUnicodeToUtf8(zTemp);
36445     sqlite3_free(zTemp);
36446   }
36447 #ifdef SQLITE_WIN32_HAS_ANSI
36448   else{
36449     char *zTemp;
36450     nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
36451     if( nByte==0 ){
36452       sqlite3_free(zConverted);
36453       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36454                          "winFullPathname3", zRelative);
36455     }
36456     nByte += 3;
36457     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
36458     if( zTemp==0 ){
36459       sqlite3_free(zConverted);
36460       return SQLITE_IOERR_NOMEM;
36461     }
36462     nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
36463     if( nByte==0 ){
36464       sqlite3_free(zConverted);
36465       sqlite3_free(zTemp);
36466       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36467                          "winFullPathname4", zRelative);
36468     }
36469     sqlite3_free(zConverted);
36470     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
36471     sqlite3_free(zTemp);
36472   }
36473 #endif
36474   if( zOut ){
36475     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
36476     sqlite3_free(zOut);
36477     return SQLITE_OK;
36478   }else{
36479     return SQLITE_IOERR_NOMEM;
36480   }
36481 #endif
36482 }
36483 
36484 #ifndef SQLITE_OMIT_LOAD_EXTENSION
36485 /*
36486 ** Interfaces for opening a shared library, finding entry points
36487 ** within the shared library, and closing the shared library.
36488 */
36489 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
36490   HANDLE h;
36491 #if defined(__CYGWIN__)
36492   int nFull = pVfs->mxPathname+1;
36493   char *zFull = sqlite3MallocZero( nFull );
36494   void *zConverted = 0;
36495   if( zFull==0 ){
36496     OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
36497     return 0;
36498   }
36499   if( winFullPathname(pVfs, zFilename, nFull, zFull)!=SQLITE_OK ){
36500     sqlite3_free(zFull);
36501     OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
36502     return 0;
36503   }
36504   zConverted = winConvertFromUtf8Filename(zFull);
36505   sqlite3_free(zFull);
36506 #else
36507   void *zConverted = winConvertFromUtf8Filename(zFilename);
36508   UNUSED_PARAMETER(pVfs);
36509 #endif
36510   if( zConverted==0 ){
36511     OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)0));
36512     return 0;
36513   }
36514   if( osIsNT() ){
36515 #if SQLITE_OS_WINRT
36516     h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
36517 #else
36518     h = osLoadLibraryW((LPCWSTR)zConverted);
36519 #endif
36520   }
36521 #ifdef SQLITE_WIN32_HAS_ANSI
36522   else{
36523     h = osLoadLibraryA((char*)zConverted);
36524   }
36525 #endif
36526   OSTRACE(("DLOPEN name=%s, handle=%p\n", zFilename, (void*)h));
36527   sqlite3_free(zConverted);
36528   return (void*)h;
36529 }
36530 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
36531   UNUSED_PARAMETER(pVfs);
36532   winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
36533 }
36534 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
36535   FARPROC proc;
36536   UNUSED_PARAMETER(pVfs);
36537   proc = osGetProcAddressA((HANDLE)pH, zSym);
36538   OSTRACE(("DLSYM handle=%p, symbol=%s, address=%p\n",
36539            (void*)pH, zSym, (void*)proc));
36540   return (void(*)(void))proc;
36541 }
36542 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
36543   UNUSED_PARAMETER(pVfs);
36544   osFreeLibrary((HANDLE)pHandle);
36545   OSTRACE(("DLCLOSE handle=%p\n", (void*)pHandle));
36546 }
36547 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
36548   #define winDlOpen  0
36549   #define winDlError 0
36550   #define winDlSym   0
36551   #define winDlClose 0
36552 #endif
36553 
36554 
36555 /*
36556 ** Write up to nBuf bytes of randomness into zBuf.
36557 */
36558 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
36559   int n = 0;
36560   UNUSED_PARAMETER(pVfs);
36561 #if defined(SQLITE_TEST)
36562   n = nBuf;
36563   memset(zBuf, 0, nBuf);
36564 #else
36565   if( sizeof(SYSTEMTIME)<=nBuf-n ){
36566     SYSTEMTIME x;
36567     osGetSystemTime(&x);
36568     memcpy(&zBuf[n], &x, sizeof(x));
36569     n += sizeof(x);
36570   }
36571   if( sizeof(DWORD)<=nBuf-n ){
36572     DWORD pid = osGetCurrentProcessId();
36573     memcpy(&zBuf[n], &pid, sizeof(pid));
36574     n += sizeof(pid);
36575   }
36576 #if SQLITE_OS_WINRT
36577   if( sizeof(ULONGLONG)<=nBuf-n ){
36578     ULONGLONG cnt = osGetTickCount64();
36579     memcpy(&zBuf[n], &cnt, sizeof(cnt));
36580     n += sizeof(cnt);
36581   }
36582 #else
36583   if( sizeof(DWORD)<=nBuf-n ){
36584     DWORD cnt = osGetTickCount();
36585     memcpy(&zBuf[n], &cnt, sizeof(cnt));
36586     n += sizeof(cnt);
36587   }
36588 #endif
36589   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
36590     LARGE_INTEGER i;
36591     osQueryPerformanceCounter(&i);
36592     memcpy(&zBuf[n], &i, sizeof(i));
36593     n += sizeof(i);
36594   }
36595 #endif
36596   return n;
36597 }
36598 
36599 
36600 /*
36601 ** Sleep for a little while.  Return the amount of time slept.
36602 */
36603 static int winSleep(sqlite3_vfs *pVfs, int microsec){
36604   sqlite3_win32_sleep((microsec+999)/1000);
36605   UNUSED_PARAMETER(pVfs);
36606   return ((microsec+999)/1000)*1000;
36607 }
36608 
36609 /*
36610 ** The following variable, if set to a non-zero value, is interpreted as
36611 ** the number of seconds since 1970 and is used to set the result of
36612 ** sqlite3OsCurrentTime() during testing.
36613 */
36614 #ifdef SQLITE_TEST
36615 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
36616 #endif
36617 
36618 /*
36619 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
36620 ** the current time and date as a Julian Day number times 86_400_000.  In
36621 ** other words, write into *piNow the number of milliseconds since the Julian
36622 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
36623 ** proleptic Gregorian calendar.
36624 **
36625 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
36626 ** cannot be found.
36627 */
36628 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
36629   /* FILETIME structure is a 64-bit value representing the number of
36630      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
36631   */
36632   FILETIME ft;
36633   static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
36634 #ifdef SQLITE_TEST
36635   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
36636 #endif
36637   /* 2^32 - to avoid use of LL and warnings in gcc */
36638   static const sqlite3_int64 max32BitValue =
36639       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
36640       (sqlite3_int64)294967296;
36641 
36642 #if SQLITE_OS_WINCE
36643   SYSTEMTIME time;
36644   osGetSystemTime(&time);
36645   /* if SystemTimeToFileTime() fails, it returns zero. */
36646   if (!osSystemTimeToFileTime(&time,&ft)){
36647     return SQLITE_ERROR;
36648   }
36649 #else
36650   osGetSystemTimeAsFileTime( &ft );
36651 #endif
36652 
36653   *piNow = winFiletimeEpoch +
36654             ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
36655                (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
36656 
36657 #ifdef SQLITE_TEST
36658   if( sqlite3_current_time ){
36659     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
36660   }
36661 #endif
36662   UNUSED_PARAMETER(pVfs);
36663   return SQLITE_OK;
36664 }
36665 
36666 /*
36667 ** Find the current time (in Universal Coordinated Time).  Write the
36668 ** current time and date as a Julian Day number into *prNow and
36669 ** return 0.  Return 1 if the time and date cannot be found.
36670 */
36671 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
36672   int rc;
36673   sqlite3_int64 i;
36674   rc = winCurrentTimeInt64(pVfs, &i);
36675   if( !rc ){
36676     *prNow = i/86400000.0;
36677   }
36678   return rc;
36679 }
36680 
36681 /*
36682 ** The idea is that this function works like a combination of
36683 ** GetLastError() and FormatMessage() on Windows (or errno and
36684 ** strerror_r() on Unix). After an error is returned by an OS
36685 ** function, SQLite calls this function with zBuf pointing to
36686 ** a buffer of nBuf bytes. The OS layer should populate the
36687 ** buffer with a nul-terminated UTF-8 encoded error message
36688 ** describing the last IO error to have occurred within the calling
36689 ** thread.
36690 **
36691 ** If the error message is too large for the supplied buffer,
36692 ** it should be truncated. The return value of xGetLastError
36693 ** is zero if the error message fits in the buffer, or non-zero
36694 ** otherwise (if the message was truncated). If non-zero is returned,
36695 ** then it is not necessary to include the nul-terminator character
36696 ** in the output buffer.
36697 **
36698 ** Not supplying an error message will have no adverse effect
36699 ** on SQLite. It is fine to have an implementation that never
36700 ** returns an error message:
36701 **
36702 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
36703 **     assert(zBuf[0]=='\0');
36704 **     return 0;
36705 **   }
36706 **
36707 ** However if an error message is supplied, it will be incorporated
36708 ** by sqlite into the error message available to the user using
36709 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
36710 */
36711 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
36712   UNUSED_PARAMETER(pVfs);
36713   return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
36714 }
36715 
36716 /*
36717 ** Initialize and deinitialize the operating system interface.
36718 */
36719 SQLITE_API int sqlite3_os_init(void){
36720   static sqlite3_vfs winVfs = {
36721     3,                   /* iVersion */
36722     sizeof(winFile),     /* szOsFile */
36723     SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
36724     0,                   /* pNext */
36725     "win32",             /* zName */
36726     0,                   /* pAppData */
36727     winOpen,             /* xOpen */
36728     winDelete,           /* xDelete */
36729     winAccess,           /* xAccess */
36730     winFullPathname,     /* xFullPathname */
36731     winDlOpen,           /* xDlOpen */
36732     winDlError,          /* xDlError */
36733     winDlSym,            /* xDlSym */
36734     winDlClose,          /* xDlClose */
36735     winRandomness,       /* xRandomness */
36736     winSleep,            /* xSleep */
36737     winCurrentTime,      /* xCurrentTime */
36738     winGetLastError,     /* xGetLastError */
36739     winCurrentTimeInt64, /* xCurrentTimeInt64 */
36740     winSetSystemCall,    /* xSetSystemCall */
36741     winGetSystemCall,    /* xGetSystemCall */
36742     winNextSystemCall,   /* xNextSystemCall */
36743   };
36744 #if defined(SQLITE_WIN32_HAS_WIDE)
36745   static sqlite3_vfs winLongPathVfs = {
36746     3,                   /* iVersion */
36747     sizeof(winFile),     /* szOsFile */
36748     SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
36749     0,                   /* pNext */
36750     "win32-longpath",    /* zName */
36751     0,                   /* pAppData */
36752     winOpen,             /* xOpen */
36753     winDelete,           /* xDelete */
36754     winAccess,           /* xAccess */
36755     winFullPathname,     /* xFullPathname */
36756     winDlOpen,           /* xDlOpen */
36757     winDlError,          /* xDlError */
36758     winDlSym,            /* xDlSym */
36759     winDlClose,          /* xDlClose */
36760     winRandomness,       /* xRandomness */
36761     winSleep,            /* xSleep */
36762     winCurrentTime,      /* xCurrentTime */
36763     winGetLastError,     /* xGetLastError */
36764     winCurrentTimeInt64, /* xCurrentTimeInt64 */
36765     winSetSystemCall,    /* xSetSystemCall */
36766     winGetSystemCall,    /* xGetSystemCall */
36767     winNextSystemCall,   /* xNextSystemCall */
36768   };
36769 #endif
36770 
36771   /* Double-check that the aSyscall[] array has been constructed
36772   ** correctly.  See ticket [bb3a86e890c8e96ab] */
36773   assert( ArraySize(aSyscall)==76 );
36774 
36775   /* get memory map allocation granularity */
36776   memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
36777 #if SQLITE_OS_WINRT
36778   osGetNativeSystemInfo(&winSysInfo);
36779 #else
36780   osGetSystemInfo(&winSysInfo);
36781 #endif
36782   assert( winSysInfo.dwAllocationGranularity>0 );
36783   assert( winSysInfo.dwPageSize>0 );
36784 
36785   sqlite3_vfs_register(&winVfs, 1);
36786 
36787 #if defined(SQLITE_WIN32_HAS_WIDE)
36788   sqlite3_vfs_register(&winLongPathVfs, 0);
36789 #endif
36790 
36791   return SQLITE_OK;
36792 }
36793 
36794 SQLITE_API int sqlite3_os_end(void){
36795 #if SQLITE_OS_WINRT
36796   if( sleepObj!=NULL ){
36797     osCloseHandle(sleepObj);
36798     sleepObj = NULL;
36799   }
36800 #endif
36801   return SQLITE_OK;
36802 }
36803 
36804 #endif /* SQLITE_OS_WIN */
36805 
36806 /************** End of os_win.c **********************************************/
36807 /************** Begin file bitvec.c ******************************************/
36808 /*
36809 ** 2008 February 16
36810 **
36811 ** The author disclaims copyright to this source code.  In place of
36812 ** a legal notice, here is a blessing:
36813 **
36814 **    May you do good and not evil.
36815 **    May you find forgiveness for yourself and forgive others.
36816 **    May you share freely, never taking more than you give.
36817 **
36818 *************************************************************************
36819 ** This file implements an object that represents a fixed-length
36820 ** bitmap.  Bits are numbered starting with 1.
36821 **
36822 ** A bitmap is used to record which pages of a database file have been
36823 ** journalled during a transaction, or which pages have the "dont-write"
36824 ** property.  Usually only a few pages are meet either condition.
36825 ** So the bitmap is usually sparse and has low cardinality.
36826 ** But sometimes (for example when during a DROP of a large table) most
36827 ** or all of the pages in a database can get journalled.  In those cases,
36828 ** the bitmap becomes dense with high cardinality.  The algorithm needs
36829 ** to handle both cases well.
36830 **
36831 ** The size of the bitmap is fixed when the object is created.
36832 **
36833 ** All bits are clear when the bitmap is created.  Individual bits
36834 ** may be set or cleared one at a time.
36835 **
36836 ** Test operations are about 100 times more common that set operations.
36837 ** Clear operations are exceedingly rare.  There are usually between
36838 ** 5 and 500 set operations per Bitvec object, though the number of sets can
36839 ** sometimes grow into tens of thousands or larger.  The size of the
36840 ** Bitvec object is the number of pages in the database file at the
36841 ** start of a transaction, and is thus usually less than a few thousand,
36842 ** but can be as large as 2 billion for a really big database.
36843 */
36844 
36845 /* Size of the Bitvec structure in bytes. */
36846 #define BITVEC_SZ        512
36847 
36848 /* Round the union size down to the nearest pointer boundary, since that's how
36849 ** it will be aligned within the Bitvec struct. */
36850 #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
36851 
36852 /* Type of the array "element" for the bitmap representation.
36853 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
36854 ** Setting this to the "natural word" size of your CPU may improve
36855 ** performance. */
36856 #define BITVEC_TELEM     u8
36857 /* Size, in bits, of the bitmap element. */
36858 #define BITVEC_SZELEM    8
36859 /* Number of elements in a bitmap array. */
36860 #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
36861 /* Number of bits in the bitmap array. */
36862 #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
36863 
36864 /* Number of u32 values in hash table. */
36865 #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
36866 /* Maximum number of entries in hash table before
36867 ** sub-dividing and re-hashing. */
36868 #define BITVEC_MXHASH    (BITVEC_NINT/2)
36869 /* Hashing function for the aHash representation.
36870 ** Empirical testing showed that the *37 multiplier
36871 ** (an arbitrary prime)in the hash function provided
36872 ** no fewer collisions than the no-op *1. */
36873 #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
36874 
36875 #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
36876 
36877 
36878 /*
36879 ** A bitmap is an instance of the following structure.
36880 **
36881 ** This bitmap records the existence of zero or more bits
36882 ** with values between 1 and iSize, inclusive.
36883 **
36884 ** There are three possible representations of the bitmap.
36885 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
36886 ** bitmap.  The least significant bit is bit 1.
36887 **
36888 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
36889 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
36890 **
36891 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
36892 ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
36893 ** handles up to iDivisor separate values of i.  apSub[0] holds
36894 ** values between 1 and iDivisor.  apSub[1] holds values between
36895 ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
36896 ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
36897 ** to hold deal with values between 1 and iDivisor.
36898 */
36899 struct Bitvec {
36900   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
36901   u32 nSet;       /* Number of bits that are set - only valid for aHash
36902                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
36903                   ** this would be 125. */
36904   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
36905                   /* Should >=0 for apSub element. */
36906                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
36907                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
36908   union {
36909     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
36910     u32 aHash[BITVEC_NINT];      /* Hash table representation */
36911     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
36912   } u;
36913 };
36914 
36915 /*
36916 ** Create a new bitmap object able to handle bits between 0 and iSize,
36917 ** inclusive.  Return a pointer to the new object.  Return NULL if
36918 ** malloc fails.
36919 */
36920 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
36921   Bitvec *p;
36922   assert( sizeof(*p)==BITVEC_SZ );
36923   p = sqlite3MallocZero( sizeof(*p) );
36924   if( p ){
36925     p->iSize = iSize;
36926   }
36927   return p;
36928 }
36929 
36930 /*
36931 ** Check to see if the i-th bit is set.  Return true or false.
36932 ** If p is NULL (if the bitmap has not been created) or if
36933 ** i is out of range, then return false.
36934 */
36935 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
36936   if( p==0 ) return 0;
36937   if( i>p->iSize || i==0 ) return 0;
36938   i--;
36939   while( p->iDivisor ){
36940     u32 bin = i/p->iDivisor;
36941     i = i%p->iDivisor;
36942     p = p->u.apSub[bin];
36943     if (!p) {
36944       return 0;
36945     }
36946   }
36947   if( p->iSize<=BITVEC_NBIT ){
36948     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
36949   } else{
36950     u32 h = BITVEC_HASH(i++);
36951     while( p->u.aHash[h] ){
36952       if( p->u.aHash[h]==i ) return 1;
36953       h = (h+1) % BITVEC_NINT;
36954     }
36955     return 0;
36956   }
36957 }
36958 
36959 /*
36960 ** Set the i-th bit.  Return 0 on success and an error code if
36961 ** anything goes wrong.
36962 **
36963 ** This routine might cause sub-bitmaps to be allocated.  Failing
36964 ** to get the memory needed to hold the sub-bitmap is the only
36965 ** that can go wrong with an insert, assuming p and i are valid.
36966 **
36967 ** The calling function must ensure that p is a valid Bitvec object
36968 ** and that the value for "i" is within range of the Bitvec object.
36969 ** Otherwise the behavior is undefined.
36970 */
36971 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
36972   u32 h;
36973   if( p==0 ) return SQLITE_OK;
36974   assert( i>0 );
36975   assert( i<=p->iSize );
36976   i--;
36977   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
36978     u32 bin = i/p->iDivisor;
36979     i = i%p->iDivisor;
36980     if( p->u.apSub[bin]==0 ){
36981       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
36982       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
36983     }
36984     p = p->u.apSub[bin];
36985   }
36986   if( p->iSize<=BITVEC_NBIT ){
36987     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
36988     return SQLITE_OK;
36989   }
36990   h = BITVEC_HASH(i++);
36991   /* if there wasn't a hash collision, and this doesn't */
36992   /* completely fill the hash, then just add it without */
36993   /* worring about sub-dividing and re-hashing. */
36994   if( !p->u.aHash[h] ){
36995     if (p->nSet<(BITVEC_NINT-1)) {
36996       goto bitvec_set_end;
36997     } else {
36998       goto bitvec_set_rehash;
36999     }
37000   }
37001   /* there was a collision, check to see if it's already */
37002   /* in hash, if not, try to find a spot for it */
37003   do {
37004     if( p->u.aHash[h]==i ) return SQLITE_OK;
37005     h++;
37006     if( h>=BITVEC_NINT ) h = 0;
37007   } while( p->u.aHash[h] );
37008   /* we didn't find it in the hash.  h points to the first */
37009   /* available free spot. check to see if this is going to */
37010   /* make our hash too "full".  */
37011 bitvec_set_rehash:
37012   if( p->nSet>=BITVEC_MXHASH ){
37013     unsigned int j;
37014     int rc;
37015     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
37016     if( aiValues==0 ){
37017       return SQLITE_NOMEM;
37018     }else{
37019       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
37020       memset(p->u.apSub, 0, sizeof(p->u.apSub));
37021       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
37022       rc = sqlite3BitvecSet(p, i);
37023       for(j=0; j<BITVEC_NINT; j++){
37024         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
37025       }
37026       sqlite3StackFree(0, aiValues);
37027       return rc;
37028     }
37029   }
37030 bitvec_set_end:
37031   p->nSet++;
37032   p->u.aHash[h] = i;
37033   return SQLITE_OK;
37034 }
37035 
37036 /*
37037 ** Clear the i-th bit.
37038 **
37039 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
37040 ** that BitvecClear can use to rebuilt its hash table.
37041 */
37042 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
37043   if( p==0 ) return;
37044   assert( i>0 );
37045   i--;
37046   while( p->iDivisor ){
37047     u32 bin = i/p->iDivisor;
37048     i = i%p->iDivisor;
37049     p = p->u.apSub[bin];
37050     if (!p) {
37051       return;
37052     }
37053   }
37054   if( p->iSize<=BITVEC_NBIT ){
37055     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
37056   }else{
37057     unsigned int j;
37058     u32 *aiValues = pBuf;
37059     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
37060     memset(p->u.aHash, 0, sizeof(p->u.aHash));
37061     p->nSet = 0;
37062     for(j=0; j<BITVEC_NINT; j++){
37063       if( aiValues[j] && aiValues[j]!=(i+1) ){
37064         u32 h = BITVEC_HASH(aiValues[j]-1);
37065         p->nSet++;
37066         while( p->u.aHash[h] ){
37067           h++;
37068           if( h>=BITVEC_NINT ) h = 0;
37069         }
37070         p->u.aHash[h] = aiValues[j];
37071       }
37072     }
37073   }
37074 }
37075 
37076 /*
37077 ** Destroy a bitmap object.  Reclaim all memory used.
37078 */
37079 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
37080   if( p==0 ) return;
37081   if( p->iDivisor ){
37082     unsigned int i;
37083     for(i=0; i<BITVEC_NPTR; i++){
37084       sqlite3BitvecDestroy(p->u.apSub[i]);
37085     }
37086   }
37087   sqlite3_free(p);
37088 }
37089 
37090 /*
37091 ** Return the value of the iSize parameter specified when Bitvec *p
37092 ** was created.
37093 */
37094 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
37095   return p->iSize;
37096 }
37097 
37098 #ifndef SQLITE_OMIT_BUILTIN_TEST
37099 /*
37100 ** Let V[] be an array of unsigned characters sufficient to hold
37101 ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
37102 ** Then the following macros can be used to set, clear, or test
37103 ** individual bits within V.
37104 */
37105 #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
37106 #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
37107 #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
37108 
37109 /*
37110 ** This routine runs an extensive test of the Bitvec code.
37111 **
37112 ** The input is an array of integers that acts as a program
37113 ** to test the Bitvec.  The integers are opcodes followed
37114 ** by 0, 1, or 3 operands, depending on the opcode.  Another
37115 ** opcode follows immediately after the last operand.
37116 **
37117 ** There are 6 opcodes numbered from 0 through 5.  0 is the
37118 ** "halt" opcode and causes the test to end.
37119 **
37120 **    0          Halt and return the number of errors
37121 **    1 N S X    Set N bits beginning with S and incrementing by X
37122 **    2 N S X    Clear N bits beginning with S and incrementing by X
37123 **    3 N        Set N randomly chosen bits
37124 **    4 N        Clear N randomly chosen bits
37125 **    5 N S X    Set N bits from S increment X in array only, not in bitvec
37126 **
37127 ** The opcodes 1 through 4 perform set and clear operations are performed
37128 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
37129 ** Opcode 5 works on the linear array only, not on the Bitvec.
37130 ** Opcode 5 is used to deliberately induce a fault in order to
37131 ** confirm that error detection works.
37132 **
37133 ** At the conclusion of the test the linear array is compared
37134 ** against the Bitvec object.  If there are any differences,
37135 ** an error is returned.  If they are the same, zero is returned.
37136 **
37137 ** If a memory allocation error occurs, return -1.
37138 */
37139 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
37140   Bitvec *pBitvec = 0;
37141   unsigned char *pV = 0;
37142   int rc = -1;
37143   int i, nx, pc, op;
37144   void *pTmpSpace;
37145 
37146   /* Allocate the Bitvec to be tested and a linear array of
37147   ** bits to act as the reference */
37148   pBitvec = sqlite3BitvecCreate( sz );
37149   pV = sqlite3MallocZero( (sz+7)/8 + 1 );
37150   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
37151   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
37152 
37153   /* NULL pBitvec tests */
37154   sqlite3BitvecSet(0, 1);
37155   sqlite3BitvecClear(0, 1, pTmpSpace);
37156 
37157   /* Run the program */
37158   pc = 0;
37159   while( (op = aOp[pc])!=0 ){
37160     switch( op ){
37161       case 1:
37162       case 2:
37163       case 5: {
37164         nx = 4;
37165         i = aOp[pc+2] - 1;
37166         aOp[pc+2] += aOp[pc+3];
37167         break;
37168       }
37169       case 3:
37170       case 4:
37171       default: {
37172         nx = 2;
37173         sqlite3_randomness(sizeof(i), &i);
37174         break;
37175       }
37176     }
37177     if( (--aOp[pc+1]) > 0 ) nx = 0;
37178     pc += nx;
37179     i = (i & 0x7fffffff)%sz;
37180     if( (op & 1)!=0 ){
37181       SETBIT(pV, (i+1));
37182       if( op!=5 ){
37183         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
37184       }
37185     }else{
37186       CLEARBIT(pV, (i+1));
37187       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
37188     }
37189   }
37190 
37191   /* Test to make sure the linear array exactly matches the
37192   ** Bitvec object.  Start with the assumption that they do
37193   ** match (rc==0).  Change rc to non-zero if a discrepancy
37194   ** is found.
37195   */
37196   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
37197           + sqlite3BitvecTest(pBitvec, 0)
37198           + (sqlite3BitvecSize(pBitvec) - sz);
37199   for(i=1; i<=sz; i++){
37200     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
37201       rc = i;
37202       break;
37203     }
37204   }
37205 
37206   /* Free allocated structure */
37207 bitvec_end:
37208   sqlite3_free(pTmpSpace);
37209   sqlite3_free(pV);
37210   sqlite3BitvecDestroy(pBitvec);
37211   return rc;
37212 }
37213 #endif /* SQLITE_OMIT_BUILTIN_TEST */
37214 
37215 /************** End of bitvec.c **********************************************/
37216 /************** Begin file pcache.c ******************************************/
37217 /*
37218 ** 2008 August 05
37219 **
37220 ** The author disclaims copyright to this source code.  In place of
37221 ** a legal notice, here is a blessing:
37222 **
37223 **    May you do good and not evil.
37224 **    May you find forgiveness for yourself and forgive others.
37225 **    May you share freely, never taking more than you give.
37226 **
37227 *************************************************************************
37228 ** This file implements that page cache.
37229 */
37230 
37231 /*
37232 ** A complete page cache is an instance of this structure.
37233 */
37234 struct PCache {
37235   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
37236   PgHdr *pSynced;                     /* Last synced page in dirty page list */
37237   int nRef;                           /* Number of referenced pages */
37238   int szCache;                        /* Configured cache size */
37239   int szPage;                         /* Size of every page in this cache */
37240   int szExtra;                        /* Size of extra space for each page */
37241   u8 bPurgeable;                      /* True if pages are on backing store */
37242   u8 eCreate;                         /* eCreate value for for xFetch() */
37243   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
37244   void *pStress;                      /* Argument to xStress */
37245   sqlite3_pcache *pCache;             /* Pluggable cache module */
37246   PgHdr *pPage1;                      /* Reference to page 1 */
37247 };
37248 
37249 /*
37250 ** Some of the assert() macros in this code are too expensive to run
37251 ** even during normal debugging.  Use them only rarely on long-running
37252 ** tests.  Enable the expensive asserts using the
37253 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
37254 */
37255 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
37256 # define expensive_assert(X)  assert(X)
37257 #else
37258 # define expensive_assert(X)
37259 #endif
37260 
37261 /********************************** Linked List Management ********************/
37262 
37263 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
37264 /*
37265 ** Check that the pCache->pSynced variable is set correctly. If it
37266 ** is not, either fail an assert or return zero. Otherwise, return
37267 ** non-zero. This is only used in debugging builds, as follows:
37268 **
37269 **   expensive_assert( pcacheCheckSynced(pCache) );
37270 */
37271 static int pcacheCheckSynced(PCache *pCache){
37272   PgHdr *p;
37273   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
37274     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
37275   }
37276   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
37277 }
37278 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
37279 
37280 /*
37281 ** Remove page pPage from the list of dirty pages.
37282 */
37283 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
37284   PCache *p = pPage->pCache;
37285 
37286   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
37287   assert( pPage->pDirtyPrev || pPage==p->pDirty );
37288 
37289   /* Update the PCache1.pSynced variable if necessary. */
37290   if( p->pSynced==pPage ){
37291     PgHdr *pSynced = pPage->pDirtyPrev;
37292     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
37293       pSynced = pSynced->pDirtyPrev;
37294     }
37295     p->pSynced = pSynced;
37296   }
37297 
37298   if( pPage->pDirtyNext ){
37299     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
37300   }else{
37301     assert( pPage==p->pDirtyTail );
37302     p->pDirtyTail = pPage->pDirtyPrev;
37303   }
37304   if( pPage->pDirtyPrev ){
37305     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
37306   }else{
37307     assert( pPage==p->pDirty );
37308     p->pDirty = pPage->pDirtyNext;
37309     if( p->pDirty==0 && p->bPurgeable ){
37310       assert( p->eCreate==1 );
37311       p->eCreate = 2;
37312     }
37313   }
37314   pPage->pDirtyNext = 0;
37315   pPage->pDirtyPrev = 0;
37316 
37317   expensive_assert( pcacheCheckSynced(p) );
37318 }
37319 
37320 /*
37321 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
37322 ** pPage).
37323 */
37324 static void pcacheAddToDirtyList(PgHdr *pPage){
37325   PCache *p = pPage->pCache;
37326 
37327   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
37328 
37329   pPage->pDirtyNext = p->pDirty;
37330   if( pPage->pDirtyNext ){
37331     assert( pPage->pDirtyNext->pDirtyPrev==0 );
37332     pPage->pDirtyNext->pDirtyPrev = pPage;
37333   }else if( p->bPurgeable ){
37334     assert( p->eCreate==2 );
37335     p->eCreate = 1;
37336   }
37337   p->pDirty = pPage;
37338   if( !p->pDirtyTail ){
37339     p->pDirtyTail = pPage;
37340   }
37341   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
37342     p->pSynced = pPage;
37343   }
37344   expensive_assert( pcacheCheckSynced(p) );
37345 }
37346 
37347 /*
37348 ** Wrapper around the pluggable caches xUnpin method. If the cache is
37349 ** being used for an in-memory database, this function is a no-op.
37350 */
37351 static void pcacheUnpin(PgHdr *p){
37352   PCache *pCache = p->pCache;
37353   if( pCache->bPurgeable ){
37354     if( p->pgno==1 ){
37355       pCache->pPage1 = 0;
37356     }
37357     sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
37358   }
37359 }
37360 
37361 /*************************************************** General Interfaces ******
37362 **
37363 ** Initialize and shutdown the page cache subsystem. Neither of these
37364 ** functions are threadsafe.
37365 */
37366 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
37367   if( sqlite3GlobalConfig.pcache2.xInit==0 ){
37368     /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
37369     ** built-in default page cache is used instead of the application defined
37370     ** page cache. */
37371     sqlite3PCacheSetDefault();
37372   }
37373   return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
37374 }
37375 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
37376   if( sqlite3GlobalConfig.pcache2.xShutdown ){
37377     /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
37378     sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
37379   }
37380 }
37381 
37382 /*
37383 ** Return the size in bytes of a PCache object.
37384 */
37385 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
37386 
37387 /*
37388 ** Create a new PCache object. Storage space to hold the object
37389 ** has already been allocated and is passed in as the p pointer.
37390 ** The caller discovers how much space needs to be allocated by
37391 ** calling sqlite3PcacheSize().
37392 */
37393 SQLITE_PRIVATE void sqlite3PcacheOpen(
37394   int szPage,                  /* Size of every page */
37395   int szExtra,                 /* Extra space associated with each page */
37396   int bPurgeable,              /* True if pages are on backing store */
37397   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
37398   void *pStress,               /* Argument to xStress */
37399   PCache *p                    /* Preallocated space for the PCache */
37400 ){
37401   memset(p, 0, sizeof(PCache));
37402   p->szPage = szPage;
37403   p->szExtra = szExtra;
37404   p->bPurgeable = bPurgeable;
37405   p->eCreate = 2;
37406   p->xStress = xStress;
37407   p->pStress = pStress;
37408   p->szCache = 100;
37409 }
37410 
37411 /*
37412 ** Change the page size for PCache object. The caller must ensure that there
37413 ** are no outstanding page references when this function is called.
37414 */
37415 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
37416   assert( pCache->nRef==0 && pCache->pDirty==0 );
37417   if( pCache->pCache ){
37418     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
37419     pCache->pCache = 0;
37420     pCache->pPage1 = 0;
37421   }
37422   pCache->szPage = szPage;
37423 }
37424 
37425 /*
37426 ** Compute the number of pages of cache requested.
37427 */
37428 static int numberOfCachePages(PCache *p){
37429   if( p->szCache>=0 ){
37430     return p->szCache;
37431   }else{
37432     return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
37433   }
37434 }
37435 
37436 /*
37437 ** Try to obtain a page from the cache.
37438 */
37439 SQLITE_PRIVATE int sqlite3PcacheFetch(
37440   PCache *pCache,       /* Obtain the page from this cache */
37441   Pgno pgno,            /* Page number to obtain */
37442   int createFlag,       /* If true, create page if it does not exist already */
37443   PgHdr **ppPage        /* Write the page here */
37444 ){
37445   sqlite3_pcache_page *pPage;
37446   PgHdr *pPgHdr = 0;
37447   int eCreate;
37448 
37449   assert( pCache!=0 );
37450   assert( createFlag==1 || createFlag==0 );
37451   assert( pgno>0 );
37452 
37453   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
37454   ** allocate it now.
37455   */
37456   if( !pCache->pCache ){
37457     sqlite3_pcache *p;
37458     if( !createFlag ){
37459       *ppPage = 0;
37460       return SQLITE_OK;
37461     }
37462     p = sqlite3GlobalConfig.pcache2.xCreate(
37463         pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
37464     );
37465     if( !p ){
37466       return SQLITE_NOMEM;
37467     }
37468     sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
37469     pCache->pCache = p;
37470   }
37471 
37472   /* eCreate defines what to do if the page does not exist.
37473   **    0     Do not allocate a new page.  (createFlag==0)
37474   **    1     Allocate a new page if doing so is inexpensive.
37475   **          (createFlag==1 AND bPurgeable AND pDirty)
37476   **    2     Allocate a new page even it doing so is difficult.
37477   **          (createFlag==1 AND !(bPurgeable AND pDirty)
37478   */
37479   eCreate = createFlag==0 ? 0 : pCache->eCreate;
37480   assert( (createFlag*(1+(!pCache->bPurgeable||!pCache->pDirty)))==eCreate );
37481   pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
37482   if( !pPage && eCreate==1 ){
37483     PgHdr *pPg;
37484 
37485     /* Find a dirty page to write-out and recycle. First try to find a
37486     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
37487     ** cleared), but if that is not possible settle for any other
37488     ** unreferenced dirty page.
37489     */
37490     expensive_assert( pcacheCheckSynced(pCache) );
37491     for(pPg=pCache->pSynced;
37492         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
37493         pPg=pPg->pDirtyPrev
37494     );
37495     pCache->pSynced = pPg;
37496     if( !pPg ){
37497       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
37498     }
37499     if( pPg ){
37500       int rc;
37501 #ifdef SQLITE_LOG_CACHE_SPILL
37502       sqlite3_log(SQLITE_FULL,
37503                   "spill page %d making room for %d - cache used: %d/%d",
37504                   pPg->pgno, pgno,
37505                   sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
37506                   numberOfCachePages(pCache));
37507 #endif
37508       rc = pCache->xStress(pCache->pStress, pPg);
37509       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
37510         return rc;
37511       }
37512     }
37513 
37514     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
37515   }
37516 
37517   if( pPage ){
37518     pPgHdr = (PgHdr *)pPage->pExtra;
37519 
37520     if( !pPgHdr->pPage ){
37521       memset(pPgHdr, 0, sizeof(PgHdr));
37522       pPgHdr->pPage = pPage;
37523       pPgHdr->pData = pPage->pBuf;
37524       pPgHdr->pExtra = (void *)&pPgHdr[1];
37525       memset(pPgHdr->pExtra, 0, pCache->szExtra);
37526       pPgHdr->pCache = pCache;
37527       pPgHdr->pgno = pgno;
37528     }
37529     assert( pPgHdr->pCache==pCache );
37530     assert( pPgHdr->pgno==pgno );
37531     assert( pPgHdr->pData==pPage->pBuf );
37532     assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
37533 
37534     if( 0==pPgHdr->nRef ){
37535       pCache->nRef++;
37536     }
37537     pPgHdr->nRef++;
37538     if( pgno==1 ){
37539       pCache->pPage1 = pPgHdr;
37540     }
37541   }
37542   *ppPage = pPgHdr;
37543   return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
37544 }
37545 
37546 /*
37547 ** Decrement the reference count on a page. If the page is clean and the
37548 ** reference count drops to 0, then it is made elible for recycling.
37549 */
37550 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
37551   assert( p->nRef>0 );
37552   p->nRef--;
37553   if( p->nRef==0 ){
37554     PCache *pCache = p->pCache;
37555     pCache->nRef--;
37556     if( (p->flags&PGHDR_DIRTY)==0 ){
37557       pcacheUnpin(p);
37558     }else{
37559       /* Move the page to the head of the dirty list. */
37560       pcacheRemoveFromDirtyList(p);
37561       pcacheAddToDirtyList(p);
37562     }
37563   }
37564 }
37565 
37566 /*
37567 ** Increase the reference count of a supplied page by 1.
37568 */
37569 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
37570   assert(p->nRef>0);
37571   p->nRef++;
37572 }
37573 
37574 /*
37575 ** Drop a page from the cache. There must be exactly one reference to the
37576 ** page. This function deletes that reference, so after it returns the
37577 ** page pointed to by p is invalid.
37578 */
37579 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
37580   PCache *pCache;
37581   assert( p->nRef==1 );
37582   if( p->flags&PGHDR_DIRTY ){
37583     pcacheRemoveFromDirtyList(p);
37584   }
37585   pCache = p->pCache;
37586   pCache->nRef--;
37587   if( p->pgno==1 ){
37588     pCache->pPage1 = 0;
37589   }
37590   sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
37591 }
37592 
37593 /*
37594 ** Make sure the page is marked as dirty. If it isn't dirty already,
37595 ** make it so.
37596 */
37597 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
37598   p->flags &= ~PGHDR_DONT_WRITE;
37599   assert( p->nRef>0 );
37600   if( 0==(p->flags & PGHDR_DIRTY) ){
37601     p->flags |= PGHDR_DIRTY;
37602     pcacheAddToDirtyList( p);
37603   }
37604 }
37605 
37606 /*
37607 ** Make sure the page is marked as clean. If it isn't clean already,
37608 ** make it so.
37609 */
37610 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
37611   if( (p->flags & PGHDR_DIRTY) ){
37612     pcacheRemoveFromDirtyList(p);
37613     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
37614     if( p->nRef==0 ){
37615       pcacheUnpin(p);
37616     }
37617   }
37618 }
37619 
37620 /*
37621 ** Make every page in the cache clean.
37622 */
37623 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
37624   PgHdr *p;
37625   while( (p = pCache->pDirty)!=0 ){
37626     sqlite3PcacheMakeClean(p);
37627   }
37628 }
37629 
37630 /*
37631 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
37632 */
37633 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
37634   PgHdr *p;
37635   for(p=pCache->pDirty; p; p=p->pDirtyNext){
37636     p->flags &= ~PGHDR_NEED_SYNC;
37637   }
37638   pCache->pSynced = pCache->pDirtyTail;
37639 }
37640 
37641 /*
37642 ** Change the page number of page p to newPgno.
37643 */
37644 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
37645   PCache *pCache = p->pCache;
37646   assert( p->nRef>0 );
37647   assert( newPgno>0 );
37648   sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
37649   p->pgno = newPgno;
37650   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
37651     pcacheRemoveFromDirtyList(p);
37652     pcacheAddToDirtyList(p);
37653   }
37654 }
37655 
37656 /*
37657 ** Drop every cache entry whose page number is greater than "pgno". The
37658 ** caller must ensure that there are no outstanding references to any pages
37659 ** other than page 1 with a page number greater than pgno.
37660 **
37661 ** If there is a reference to page 1 and the pgno parameter passed to this
37662 ** function is 0, then the data area associated with page 1 is zeroed, but
37663 ** the page object is not dropped.
37664 */
37665 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
37666   if( pCache->pCache ){
37667     PgHdr *p;
37668     PgHdr *pNext;
37669     for(p=pCache->pDirty; p; p=pNext){
37670       pNext = p->pDirtyNext;
37671       /* This routine never gets call with a positive pgno except right
37672       ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
37673       ** it must be that pgno==0.
37674       */
37675       assert( p->pgno>0 );
37676       if( ALWAYS(p->pgno>pgno) ){
37677         assert( p->flags&PGHDR_DIRTY );
37678         sqlite3PcacheMakeClean(p);
37679       }
37680     }
37681     if( pgno==0 && pCache->pPage1 ){
37682       memset(pCache->pPage1->pData, 0, pCache->szPage);
37683       pgno = 1;
37684     }
37685     sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
37686   }
37687 }
37688 
37689 /*
37690 ** Close a cache.
37691 */
37692 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
37693   if( pCache->pCache ){
37694     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
37695   }
37696 }
37697 
37698 /*
37699 ** Discard the contents of the cache.
37700 */
37701 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
37702   sqlite3PcacheTruncate(pCache, 0);
37703 }
37704 
37705 /*
37706 ** Merge two lists of pages connected by pDirty and in pgno order.
37707 ** Do not both fixing the pDirtyPrev pointers.
37708 */
37709 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
37710   PgHdr result, *pTail;
37711   pTail = &result;
37712   while( pA && pB ){
37713     if( pA->pgno<pB->pgno ){
37714       pTail->pDirty = pA;
37715       pTail = pA;
37716       pA = pA->pDirty;
37717     }else{
37718       pTail->pDirty = pB;
37719       pTail = pB;
37720       pB = pB->pDirty;
37721     }
37722   }
37723   if( pA ){
37724     pTail->pDirty = pA;
37725   }else if( pB ){
37726     pTail->pDirty = pB;
37727   }else{
37728     pTail->pDirty = 0;
37729   }
37730   return result.pDirty;
37731 }
37732 
37733 /*
37734 ** Sort the list of pages in accending order by pgno.  Pages are
37735 ** connected by pDirty pointers.  The pDirtyPrev pointers are
37736 ** corrupted by this sort.
37737 **
37738 ** Since there cannot be more than 2^31 distinct pages in a database,
37739 ** there cannot be more than 31 buckets required by the merge sorter.
37740 ** One extra bucket is added to catch overflow in case something
37741 ** ever changes to make the previous sentence incorrect.
37742 */
37743 #define N_SORT_BUCKET  32
37744 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
37745   PgHdr *a[N_SORT_BUCKET], *p;
37746   int i;
37747   memset(a, 0, sizeof(a));
37748   while( pIn ){
37749     p = pIn;
37750     pIn = p->pDirty;
37751     p->pDirty = 0;
37752     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
37753       if( a[i]==0 ){
37754         a[i] = p;
37755         break;
37756       }else{
37757         p = pcacheMergeDirtyList(a[i], p);
37758         a[i] = 0;
37759       }
37760     }
37761     if( NEVER(i==N_SORT_BUCKET-1) ){
37762       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
37763       ** the input list.  But that is impossible.
37764       */
37765       a[i] = pcacheMergeDirtyList(a[i], p);
37766     }
37767   }
37768   p = a[0];
37769   for(i=1; i<N_SORT_BUCKET; i++){
37770     p = pcacheMergeDirtyList(p, a[i]);
37771   }
37772   return p;
37773 }
37774 
37775 /*
37776 ** Return a list of all dirty pages in the cache, sorted by page number.
37777 */
37778 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
37779   PgHdr *p;
37780   for(p=pCache->pDirty; p; p=p->pDirtyNext){
37781     p->pDirty = p->pDirtyNext;
37782   }
37783   return pcacheSortDirtyList(pCache->pDirty);
37784 }
37785 
37786 /*
37787 ** Return the total number of referenced pages held by the cache.
37788 */
37789 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
37790   return pCache->nRef;
37791 }
37792 
37793 /*
37794 ** Return the number of references to the page supplied as an argument.
37795 */
37796 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
37797   return p->nRef;
37798 }
37799 
37800 /*
37801 ** Return the total number of pages in the cache.
37802 */
37803 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
37804   int nPage = 0;
37805   if( pCache->pCache ){
37806     nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
37807   }
37808   return nPage;
37809 }
37810 
37811 #ifdef SQLITE_TEST
37812 /*
37813 ** Get the suggested cache-size value.
37814 */
37815 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
37816   return numberOfCachePages(pCache);
37817 }
37818 #endif
37819 
37820 /*
37821 ** Set the suggested cache-size value.
37822 */
37823 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
37824   pCache->szCache = mxPage;
37825   if( pCache->pCache ){
37826     sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
37827                                            numberOfCachePages(pCache));
37828   }
37829 }
37830 
37831 /*
37832 ** Free up as much memory as possible from the page cache.
37833 */
37834 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
37835   if( pCache->pCache ){
37836     sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
37837   }
37838 }
37839 
37840 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
37841 /*
37842 ** For all dirty pages currently in the cache, invoke the specified
37843 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
37844 ** defined.
37845 */
37846 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
37847   PgHdr *pDirty;
37848   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
37849     xIter(pDirty);
37850   }
37851 }
37852 #endif
37853 
37854 /************** End of pcache.c **********************************************/
37855 /************** Begin file pcache1.c *****************************************/
37856 /*
37857 ** 2008 November 05
37858 **
37859 ** The author disclaims copyright to this source code.  In place of
37860 ** a legal notice, here is a blessing:
37861 **
37862 **    May you do good and not evil.
37863 **    May you find forgiveness for yourself and forgive others.
37864 **    May you share freely, never taking more than you give.
37865 **
37866 *************************************************************************
37867 **
37868 ** This file implements the default page cache implementation (the
37869 ** sqlite3_pcache interface). It also contains part of the implementation
37870 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
37871 ** If the default page cache implementation is overriden, then neither of
37872 ** these two features are available.
37873 */
37874 
37875 
37876 typedef struct PCache1 PCache1;
37877 typedef struct PgHdr1 PgHdr1;
37878 typedef struct PgFreeslot PgFreeslot;
37879 typedef struct PGroup PGroup;
37880 
37881 /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set
37882 ** of one or more PCaches that are able to recycle each others unpinned
37883 ** pages when they are under memory pressure.  A PGroup is an instance of
37884 ** the following object.
37885 **
37886 ** This page cache implementation works in one of two modes:
37887 **
37888 **   (1)  Every PCache is the sole member of its own PGroup.  There is
37889 **        one PGroup per PCache.
37890 **
37891 **   (2)  There is a single global PGroup that all PCaches are a member
37892 **        of.
37893 **
37894 ** Mode 1 uses more memory (since PCache instances are not able to rob
37895 ** unused pages from other PCaches) but it also operates without a mutex,
37896 ** and is therefore often faster.  Mode 2 requires a mutex in order to be
37897 ** threadsafe, but recycles pages more efficiently.
37898 **
37899 ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
37900 ** PGroup which is the pcache1.grp global variable and its mutex is
37901 ** SQLITE_MUTEX_STATIC_LRU.
37902 */
37903 struct PGroup {
37904   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
37905   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
37906   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
37907   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
37908   unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
37909   PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
37910 };
37911 
37912 /* Each page cache is an instance of the following object.  Every
37913 ** open database file (including each in-memory database and each
37914 ** temporary or transient database) has a single page cache which
37915 ** is an instance of this object.
37916 **
37917 ** Pointers to structures of this type are cast and returned as
37918 ** opaque sqlite3_pcache* handles.
37919 */
37920 struct PCache1 {
37921   /* Cache configuration parameters. Page size (szPage) and the purgeable
37922   ** flag (bPurgeable) are set when the cache is created. nMax may be
37923   ** modified at any time by a call to the pcache1Cachesize() method.
37924   ** The PGroup mutex must be held when accessing nMax.
37925   */
37926   PGroup *pGroup;                     /* PGroup this cache belongs to */
37927   int szPage;                         /* Size of allocated pages in bytes */
37928   int szExtra;                        /* Size of extra space in bytes */
37929   int bPurgeable;                     /* True if cache is purgeable */
37930   unsigned int nMin;                  /* Minimum number of pages reserved */
37931   unsigned int nMax;                  /* Configured "cache_size" value */
37932   unsigned int n90pct;                /* nMax*9/10 */
37933   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
37934 
37935   /* Hash table of all pages. The following variables may only be accessed
37936   ** when the accessor is holding the PGroup mutex.
37937   */
37938   unsigned int nRecyclable;           /* Number of pages in the LRU list */
37939   unsigned int nPage;                 /* Total number of pages in apHash */
37940   unsigned int nHash;                 /* Number of slots in apHash[] */
37941   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
37942 };
37943 
37944 /*
37945 ** Each cache entry is represented by an instance of the following
37946 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
37947 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
37948 ** in memory.
37949 */
37950 struct PgHdr1 {
37951   sqlite3_pcache_page page;
37952   unsigned int iKey;             /* Key value (page number) */
37953   u8 isPinned;                   /* Page in use, not on the LRU list */
37954   PgHdr1 *pNext;                 /* Next in hash table chain */
37955   PCache1 *pCache;               /* Cache that currently owns this page */
37956   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
37957   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
37958 };
37959 
37960 /*
37961 ** Free slots in the allocator used to divide up the buffer provided using
37962 ** the SQLITE_CONFIG_PAGECACHE mechanism.
37963 */
37964 struct PgFreeslot {
37965   PgFreeslot *pNext;  /* Next free slot */
37966 };
37967 
37968 /*
37969 ** Global data used by this cache.
37970 */
37971 static SQLITE_WSD struct PCacheGlobal {
37972   PGroup grp;                    /* The global PGroup for mode (2) */
37973 
37974   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
37975   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
37976   ** fixed at sqlite3_initialize() time and do not require mutex protection.
37977   ** The nFreeSlot and pFree values do require mutex protection.
37978   */
37979   int isInit;                    /* True if initialized */
37980   int szSlot;                    /* Size of each free slot */
37981   int nSlot;                     /* The number of pcache slots */
37982   int nReserve;                  /* Try to keep nFreeSlot above this */
37983   void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
37984   /* Above requires no mutex.  Use mutex below for variable that follow. */
37985   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
37986   PgFreeslot *pFree;             /* Free page blocks */
37987   int nFreeSlot;                 /* Number of unused pcache slots */
37988   /* The following value requires a mutex to change.  We skip the mutex on
37989   ** reading because (1) most platforms read a 32-bit integer atomically and
37990   ** (2) even if an incorrect value is read, no great harm is done since this
37991   ** is really just an optimization. */
37992   int bUnderPressure;            /* True if low on PAGECACHE memory */
37993 } pcache1_g;
37994 
37995 /*
37996 ** All code in this file should access the global structure above via the
37997 ** alias "pcache1". This ensures that the WSD emulation is used when
37998 ** compiling for systems that do not support real WSD.
37999 */
38000 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
38001 
38002 /*
38003 ** Macros to enter and leave the PCache LRU mutex.
38004 */
38005 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
38006 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
38007 
38008 /******************************************************************************/
38009 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
38010 
38011 /*
38012 ** This function is called during initialization if a static buffer is
38013 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
38014 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
38015 ** enough to contain 'n' buffers of 'sz' bytes each.
38016 **
38017 ** This routine is called from sqlite3_initialize() and so it is guaranteed
38018 ** to be serialized already.  There is no need for further mutexing.
38019 */
38020 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
38021   if( pcache1.isInit ){
38022     PgFreeslot *p;
38023     sz = ROUNDDOWN8(sz);
38024     pcache1.szSlot = sz;
38025     pcache1.nSlot = pcache1.nFreeSlot = n;
38026     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
38027     pcache1.pStart = pBuf;
38028     pcache1.pFree = 0;
38029     pcache1.bUnderPressure = 0;
38030     while( n-- ){
38031       p = (PgFreeslot*)pBuf;
38032       p->pNext = pcache1.pFree;
38033       pcache1.pFree = p;
38034       pBuf = (void*)&((char*)pBuf)[sz];
38035     }
38036     pcache1.pEnd = pBuf;
38037   }
38038 }
38039 
38040 /*
38041 ** Malloc function used within this file to allocate space from the buffer
38042 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
38043 ** such buffer exists or there is no space left in it, this function falls
38044 ** back to sqlite3Malloc().
38045 **
38046 ** Multiple threads can run this routine at the same time.  Global variables
38047 ** in pcache1 need to be protected via mutex.
38048 */
38049 static void *pcache1Alloc(int nByte){
38050   void *p = 0;
38051   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
38052   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
38053   if( nByte<=pcache1.szSlot ){
38054     sqlite3_mutex_enter(pcache1.mutex);
38055     p = (PgHdr1 *)pcache1.pFree;
38056     if( p ){
38057       pcache1.pFree = pcache1.pFree->pNext;
38058       pcache1.nFreeSlot--;
38059       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
38060       assert( pcache1.nFreeSlot>=0 );
38061       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
38062     }
38063     sqlite3_mutex_leave(pcache1.mutex);
38064   }
38065   if( p==0 ){
38066     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
38067     ** it from sqlite3Malloc instead.
38068     */
38069     p = sqlite3Malloc(nByte);
38070 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
38071     if( p ){
38072       int sz = sqlite3MallocSize(p);
38073       sqlite3_mutex_enter(pcache1.mutex);
38074       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
38075       sqlite3_mutex_leave(pcache1.mutex);
38076     }
38077 #endif
38078     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
38079   }
38080   return p;
38081 }
38082 
38083 /*
38084 ** Free an allocated buffer obtained from pcache1Alloc().
38085 */
38086 static int pcache1Free(void *p){
38087   int nFreed = 0;
38088   if( p==0 ) return 0;
38089   if( p>=pcache1.pStart && p<pcache1.pEnd ){
38090     PgFreeslot *pSlot;
38091     sqlite3_mutex_enter(pcache1.mutex);
38092     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
38093     pSlot = (PgFreeslot*)p;
38094     pSlot->pNext = pcache1.pFree;
38095     pcache1.pFree = pSlot;
38096     pcache1.nFreeSlot++;
38097     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
38098     assert( pcache1.nFreeSlot<=pcache1.nSlot );
38099     sqlite3_mutex_leave(pcache1.mutex);
38100   }else{
38101     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
38102     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
38103     nFreed = sqlite3MallocSize(p);
38104 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
38105     sqlite3_mutex_enter(pcache1.mutex);
38106     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
38107     sqlite3_mutex_leave(pcache1.mutex);
38108 #endif
38109     sqlite3_free(p);
38110   }
38111   return nFreed;
38112 }
38113 
38114 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
38115 /*
38116 ** Return the size of a pcache allocation
38117 */
38118 static int pcache1MemSize(void *p){
38119   if( p>=pcache1.pStart && p<pcache1.pEnd ){
38120     return pcache1.szSlot;
38121   }else{
38122     int iSize;
38123     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
38124     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
38125     iSize = sqlite3MallocSize(p);
38126     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
38127     return iSize;
38128   }
38129 }
38130 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
38131 
38132 /*
38133 ** Allocate a new page object initially associated with cache pCache.
38134 */
38135 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
38136   PgHdr1 *p = 0;
38137   void *pPg;
38138 
38139   /* The group mutex must be released before pcache1Alloc() is called. This
38140   ** is because it may call sqlite3_release_memory(), which assumes that
38141   ** this mutex is not held. */
38142   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
38143   pcache1LeaveMutex(pCache->pGroup);
38144 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
38145   pPg = pcache1Alloc(pCache->szPage);
38146   p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
38147   if( !pPg || !p ){
38148     pcache1Free(pPg);
38149     sqlite3_free(p);
38150     pPg = 0;
38151   }
38152 #else
38153   pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
38154   p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
38155 #endif
38156   pcache1EnterMutex(pCache->pGroup);
38157 
38158   if( pPg ){
38159     p->page.pBuf = pPg;
38160     p->page.pExtra = &p[1];
38161     if( pCache->bPurgeable ){
38162       pCache->pGroup->nCurrentPage++;
38163     }
38164     return p;
38165   }
38166   return 0;
38167 }
38168 
38169 /*
38170 ** Free a page object allocated by pcache1AllocPage().
38171 **
38172 ** The pointer is allowed to be NULL, which is prudent.  But it turns out
38173 ** that the current implementation happens to never call this routine
38174 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
38175 */
38176 static void pcache1FreePage(PgHdr1 *p){
38177   if( ALWAYS(p) ){
38178     PCache1 *pCache = p->pCache;
38179     assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
38180     pcache1Free(p->page.pBuf);
38181 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
38182     sqlite3_free(p);
38183 #endif
38184     if( pCache->bPurgeable ){
38185       pCache->pGroup->nCurrentPage--;
38186     }
38187   }
38188 }
38189 
38190 /*
38191 ** Malloc function used by SQLite to obtain space from the buffer configured
38192 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
38193 ** exists, this function falls back to sqlite3Malloc().
38194 */
38195 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
38196   return pcache1Alloc(sz);
38197 }
38198 
38199 /*
38200 ** Free an allocated buffer obtained from sqlite3PageMalloc().
38201 */
38202 SQLITE_PRIVATE void sqlite3PageFree(void *p){
38203   pcache1Free(p);
38204 }
38205 
38206 
38207 /*
38208 ** Return true if it desirable to avoid allocating a new page cache
38209 ** entry.
38210 **
38211 ** If memory was allocated specifically to the page cache using
38212 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
38213 ** it is desirable to avoid allocating a new page cache entry because
38214 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
38215 ** for all page cache needs and we should not need to spill the
38216 ** allocation onto the heap.
38217 **
38218 ** Or, the heap is used for all page cache memory but the heap is
38219 ** under memory pressure, then again it is desirable to avoid
38220 ** allocating a new page cache entry in order to avoid stressing
38221 ** the heap even further.
38222 */
38223 static int pcache1UnderMemoryPressure(PCache1 *pCache){
38224   if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
38225     return pcache1.bUnderPressure;
38226   }else{
38227     return sqlite3HeapNearlyFull();
38228   }
38229 }
38230 
38231 /******************************************************************************/
38232 /******** General Implementation Functions ************************************/
38233 
38234 /*
38235 ** This function is used to resize the hash table used by the cache passed
38236 ** as the first argument.
38237 **
38238 ** The PCache mutex must be held when this function is called.
38239 */
38240 static int pcache1ResizeHash(PCache1 *p){
38241   PgHdr1 **apNew;
38242   unsigned int nNew;
38243   unsigned int i;
38244 
38245   assert( sqlite3_mutex_held(p->pGroup->mutex) );
38246 
38247   nNew = p->nHash*2;
38248   if( nNew<256 ){
38249     nNew = 256;
38250   }
38251 
38252   pcache1LeaveMutex(p->pGroup);
38253   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
38254   apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
38255   if( p->nHash ){ sqlite3EndBenignMalloc(); }
38256   pcache1EnterMutex(p->pGroup);
38257   if( apNew ){
38258     for(i=0; i<p->nHash; i++){
38259       PgHdr1 *pPage;
38260       PgHdr1 *pNext = p->apHash[i];
38261       while( (pPage = pNext)!=0 ){
38262         unsigned int h = pPage->iKey % nNew;
38263         pNext = pPage->pNext;
38264         pPage->pNext = apNew[h];
38265         apNew[h] = pPage;
38266       }
38267     }
38268     sqlite3_free(p->apHash);
38269     p->apHash = apNew;
38270     p->nHash = nNew;
38271   }
38272 
38273   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
38274 }
38275 
38276 /*
38277 ** This function is used internally to remove the page pPage from the
38278 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
38279 ** LRU list, then this function is a no-op.
38280 **
38281 ** The PGroup mutex must be held when this function is called.
38282 */
38283 static void pcache1PinPage(PgHdr1 *pPage){
38284   PCache1 *pCache;
38285   PGroup *pGroup;
38286 
38287   assert( pPage!=0 );
38288   assert( pPage->isPinned==0 );
38289   pCache = pPage->pCache;
38290   pGroup = pCache->pGroup;
38291   assert( pPage->pLruNext || pPage==pGroup->pLruTail );
38292   assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
38293   assert( sqlite3_mutex_held(pGroup->mutex) );
38294   if( pPage->pLruPrev ){
38295     pPage->pLruPrev->pLruNext = pPage->pLruNext;
38296   }else{
38297     pGroup->pLruHead = pPage->pLruNext;
38298   }
38299   if( pPage->pLruNext ){
38300     pPage->pLruNext->pLruPrev = pPage->pLruPrev;
38301   }else{
38302     pGroup->pLruTail = pPage->pLruPrev;
38303   }
38304   pPage->pLruNext = 0;
38305   pPage->pLruPrev = 0;
38306   pPage->isPinned = 1;
38307   pCache->nRecyclable--;
38308 }
38309 
38310 
38311 /*
38312 ** Remove the page supplied as an argument from the hash table
38313 ** (PCache1.apHash structure) that it is currently stored in.
38314 **
38315 ** The PGroup mutex must be held when this function is called.
38316 */
38317 static void pcache1RemoveFromHash(PgHdr1 *pPage){
38318   unsigned int h;
38319   PCache1 *pCache = pPage->pCache;
38320   PgHdr1 **pp;
38321 
38322   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
38323   h = pPage->iKey % pCache->nHash;
38324   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
38325   *pp = (*pp)->pNext;
38326 
38327   pCache->nPage--;
38328 }
38329 
38330 /*
38331 ** If there are currently more than nMaxPage pages allocated, try
38332 ** to recycle pages to reduce the number allocated to nMaxPage.
38333 */
38334 static void pcache1EnforceMaxPage(PGroup *pGroup){
38335   assert( sqlite3_mutex_held(pGroup->mutex) );
38336   while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
38337     PgHdr1 *p = pGroup->pLruTail;
38338     assert( p->pCache->pGroup==pGroup );
38339     assert( p->isPinned==0 );
38340     pcache1PinPage(p);
38341     pcache1RemoveFromHash(p);
38342     pcache1FreePage(p);
38343   }
38344 }
38345 
38346 /*
38347 ** Discard all pages from cache pCache with a page number (key value)
38348 ** greater than or equal to iLimit. Any pinned pages that meet this
38349 ** criteria are unpinned before they are discarded.
38350 **
38351 ** The PCache mutex must be held when this function is called.
38352 */
38353 static void pcache1TruncateUnsafe(
38354   PCache1 *pCache,             /* The cache to truncate */
38355   unsigned int iLimit          /* Drop pages with this pgno or larger */
38356 ){
38357   TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
38358   unsigned int h;
38359   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
38360   for(h=0; h<pCache->nHash; h++){
38361     PgHdr1 **pp = &pCache->apHash[h];
38362     PgHdr1 *pPage;
38363     while( (pPage = *pp)!=0 ){
38364       if( pPage->iKey>=iLimit ){
38365         pCache->nPage--;
38366         *pp = pPage->pNext;
38367         if( !pPage->isPinned ) pcache1PinPage(pPage);
38368         pcache1FreePage(pPage);
38369       }else{
38370         pp = &pPage->pNext;
38371         TESTONLY( nPage++; )
38372       }
38373     }
38374   }
38375   assert( pCache->nPage==nPage );
38376 }
38377 
38378 /******************************************************************************/
38379 /******** sqlite3_pcache Methods **********************************************/
38380 
38381 /*
38382 ** Implementation of the sqlite3_pcache.xInit method.
38383 */
38384 static int pcache1Init(void *NotUsed){
38385   UNUSED_PARAMETER(NotUsed);
38386   assert( pcache1.isInit==0 );
38387   memset(&pcache1, 0, sizeof(pcache1));
38388   if( sqlite3GlobalConfig.bCoreMutex ){
38389     pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
38390     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
38391   }
38392   pcache1.grp.mxPinned = 10;
38393   pcache1.isInit = 1;
38394   return SQLITE_OK;
38395 }
38396 
38397 /*
38398 ** Implementation of the sqlite3_pcache.xShutdown method.
38399 ** Note that the static mutex allocated in xInit does
38400 ** not need to be freed.
38401 */
38402 static void pcache1Shutdown(void *NotUsed){
38403   UNUSED_PARAMETER(NotUsed);
38404   assert( pcache1.isInit!=0 );
38405   memset(&pcache1, 0, sizeof(pcache1));
38406 }
38407 
38408 /*
38409 ** Implementation of the sqlite3_pcache.xCreate method.
38410 **
38411 ** Allocate a new cache.
38412 */
38413 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
38414   PCache1 *pCache;      /* The newly created page cache */
38415   PGroup *pGroup;       /* The group the new page cache will belong to */
38416   int sz;               /* Bytes of memory required to allocate the new cache */
38417 
38418   /*
38419   ** The separateCache variable is true if each PCache has its own private
38420   ** PGroup.  In other words, separateCache is true for mode (1) where no
38421   ** mutexing is required.
38422   **
38423   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
38424   **
38425   **   *  Always use a unified cache in single-threaded applications
38426   **
38427   **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
38428   **      use separate caches (mode-1)
38429   */
38430 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
38431   const int separateCache = 0;
38432 #else
38433   int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
38434 #endif
38435 
38436   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
38437   assert( szExtra < 300 );
38438 
38439   sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
38440   pCache = (PCache1 *)sqlite3MallocZero(sz);
38441   if( pCache ){
38442     if( separateCache ){
38443       pGroup = (PGroup*)&pCache[1];
38444       pGroup->mxPinned = 10;
38445     }else{
38446       pGroup = &pcache1.grp;
38447     }
38448     pCache->pGroup = pGroup;
38449     pCache->szPage = szPage;
38450     pCache->szExtra = szExtra;
38451     pCache->bPurgeable = (bPurgeable ? 1 : 0);
38452     if( bPurgeable ){
38453       pCache->nMin = 10;
38454       pcache1EnterMutex(pGroup);
38455       pGroup->nMinPage += pCache->nMin;
38456       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
38457       pcache1LeaveMutex(pGroup);
38458     }
38459   }
38460   return (sqlite3_pcache *)pCache;
38461 }
38462 
38463 /*
38464 ** Implementation of the sqlite3_pcache.xCachesize method.
38465 **
38466 ** Configure the cache_size limit for a cache.
38467 */
38468 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
38469   PCache1 *pCache = (PCache1 *)p;
38470   if( pCache->bPurgeable ){
38471     PGroup *pGroup = pCache->pGroup;
38472     pcache1EnterMutex(pGroup);
38473     pGroup->nMaxPage += (nMax - pCache->nMax);
38474     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
38475     pCache->nMax = nMax;
38476     pCache->n90pct = pCache->nMax*9/10;
38477     pcache1EnforceMaxPage(pGroup);
38478     pcache1LeaveMutex(pGroup);
38479   }
38480 }
38481 
38482 /*
38483 ** Implementation of the sqlite3_pcache.xShrink method.
38484 **
38485 ** Free up as much memory as possible.
38486 */
38487 static void pcache1Shrink(sqlite3_pcache *p){
38488   PCache1 *pCache = (PCache1*)p;
38489   if( pCache->bPurgeable ){
38490     PGroup *pGroup = pCache->pGroup;
38491     int savedMaxPage;
38492     pcache1EnterMutex(pGroup);
38493     savedMaxPage = pGroup->nMaxPage;
38494     pGroup->nMaxPage = 0;
38495     pcache1EnforceMaxPage(pGroup);
38496     pGroup->nMaxPage = savedMaxPage;
38497     pcache1LeaveMutex(pGroup);
38498   }
38499 }
38500 
38501 /*
38502 ** Implementation of the sqlite3_pcache.xPagecount method.
38503 */
38504 static int pcache1Pagecount(sqlite3_pcache *p){
38505   int n;
38506   PCache1 *pCache = (PCache1*)p;
38507   pcache1EnterMutex(pCache->pGroup);
38508   n = pCache->nPage;
38509   pcache1LeaveMutex(pCache->pGroup);
38510   return n;
38511 }
38512 
38513 /*
38514 ** Implementation of the sqlite3_pcache.xFetch method.
38515 **
38516 ** Fetch a page by key value.
38517 **
38518 ** Whether or not a new page may be allocated by this function depends on
38519 ** the value of the createFlag argument.  0 means do not allocate a new
38520 ** page.  1 means allocate a new page if space is easily available.  2
38521 ** means to try really hard to allocate a new page.
38522 **
38523 ** For a non-purgeable cache (a cache used as the storage for an in-memory
38524 ** database) there is really no difference between createFlag 1 and 2.  So
38525 ** the calling function (pcache.c) will never have a createFlag of 1 on
38526 ** a non-purgeable cache.
38527 **
38528 ** There are three different approaches to obtaining space for a page,
38529 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
38530 **
38531 **   1. Regardless of the value of createFlag, the cache is searched for a
38532 **      copy of the requested page. If one is found, it is returned.
38533 **
38534 **   2. If createFlag==0 and the page is not already in the cache, NULL is
38535 **      returned.
38536 **
38537 **   3. If createFlag is 1, and the page is not already in the cache, then
38538 **      return NULL (do not allocate a new page) if any of the following
38539 **      conditions are true:
38540 **
38541 **       (a) the number of pages pinned by the cache is greater than
38542 **           PCache1.nMax, or
38543 **
38544 **       (b) the number of pages pinned by the cache is greater than
38545 **           the sum of nMax for all purgeable caches, less the sum of
38546 **           nMin for all other purgeable caches, or
38547 **
38548 **   4. If none of the first three conditions apply and the cache is marked
38549 **      as purgeable, and if one of the following is true:
38550 **
38551 **       (a) The number of pages allocated for the cache is already
38552 **           PCache1.nMax, or
38553 **
38554 **       (b) The number of pages allocated for all purgeable caches is
38555 **           already equal to or greater than the sum of nMax for all
38556 **           purgeable caches,
38557 **
38558 **       (c) The system is under memory pressure and wants to avoid
38559 **           unnecessary pages cache entry allocations
38560 **
38561 **      then attempt to recycle a page from the LRU list. If it is the right
38562 **      size, return the recycled buffer. Otherwise, free the buffer and
38563 **      proceed to step 5.
38564 **
38565 **   5. Otherwise, allocate and return a new page buffer.
38566 */
38567 static sqlite3_pcache_page *pcache1Fetch(
38568   sqlite3_pcache *p,
38569   unsigned int iKey,
38570   int createFlag
38571 ){
38572   unsigned int nPinned;
38573   PCache1 *pCache = (PCache1 *)p;
38574   PGroup *pGroup;
38575   PgHdr1 *pPage = 0;
38576 
38577   assert( offsetof(PgHdr1,page)==0 );
38578   assert( pCache->bPurgeable || createFlag!=1 );
38579   assert( pCache->bPurgeable || pCache->nMin==0 );
38580   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
38581   assert( pCache->nMin==0 || pCache->bPurgeable );
38582   pcache1EnterMutex(pGroup = pCache->pGroup);
38583 
38584   /* Step 1: Search the hash table for an existing entry. */
38585   if( pCache->nHash>0 ){
38586     unsigned int h = iKey % pCache->nHash;
38587     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
38588   }
38589 
38590   /* Step 2: Abort if no existing page is found and createFlag is 0 */
38591   if( pPage ){
38592     if( !pPage->isPinned ) pcache1PinPage(pPage);
38593     goto fetch_out;
38594   }
38595   if( createFlag==0 ){
38596     goto fetch_out;
38597   }
38598 
38599   /* The pGroup local variable will normally be initialized by the
38600   ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
38601   ** then pcache1EnterMutex() is a no-op, so we have to initialize the
38602   ** local variable here.  Delaying the initialization of pGroup is an
38603   ** optimization:  The common case is to exit the module before reaching
38604   ** this point.
38605   */
38606 #ifdef SQLITE_MUTEX_OMIT
38607   pGroup = pCache->pGroup;
38608 #endif
38609 
38610   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
38611   assert( pCache->nPage >= pCache->nRecyclable );
38612   nPinned = pCache->nPage - pCache->nRecyclable;
38613   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
38614   assert( pCache->n90pct == pCache->nMax*9/10 );
38615   if( createFlag==1 && (
38616         nPinned>=pGroup->mxPinned
38617      || nPinned>=pCache->n90pct
38618      || pcache1UnderMemoryPressure(pCache)
38619   )){
38620     goto fetch_out;
38621   }
38622 
38623   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
38624     goto fetch_out;
38625   }
38626   assert( pCache->nHash>0 && pCache->apHash );
38627 
38628   /* Step 4. Try to recycle a page. */
38629   if( pCache->bPurgeable && pGroup->pLruTail && (
38630          (pCache->nPage+1>=pCache->nMax)
38631       || pGroup->nCurrentPage>=pGroup->nMaxPage
38632       || pcache1UnderMemoryPressure(pCache)
38633   )){
38634     PCache1 *pOther;
38635     pPage = pGroup->pLruTail;
38636     assert( pPage->isPinned==0 );
38637     pcache1RemoveFromHash(pPage);
38638     pcache1PinPage(pPage);
38639     pOther = pPage->pCache;
38640 
38641     /* We want to verify that szPage and szExtra are the same for pOther
38642     ** and pCache.  Assert that we can verify this by comparing sums. */
38643     assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
38644     assert( pCache->szExtra<512 );
38645     assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
38646     assert( pOther->szExtra<512 );
38647 
38648     if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
38649       pcache1FreePage(pPage);
38650       pPage = 0;
38651     }else{
38652       pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
38653     }
38654   }
38655 
38656   /* Step 5. If a usable page buffer has still not been found,
38657   ** attempt to allocate a new one.
38658   */
38659   if( !pPage ){
38660     if( createFlag==1 ) sqlite3BeginBenignMalloc();
38661     pPage = pcache1AllocPage(pCache);
38662     if( createFlag==1 ) sqlite3EndBenignMalloc();
38663   }
38664 
38665   if( pPage ){
38666     unsigned int h = iKey % pCache->nHash;
38667     pCache->nPage++;
38668     pPage->iKey = iKey;
38669     pPage->pNext = pCache->apHash[h];
38670     pPage->pCache = pCache;
38671     pPage->pLruPrev = 0;
38672     pPage->pLruNext = 0;
38673     pPage->isPinned = 1;
38674     *(void **)pPage->page.pExtra = 0;
38675     pCache->apHash[h] = pPage;
38676   }
38677 
38678 fetch_out:
38679   if( pPage && iKey>pCache->iMaxKey ){
38680     pCache->iMaxKey = iKey;
38681   }
38682   pcache1LeaveMutex(pGroup);
38683   return (sqlite3_pcache_page*)pPage;
38684 }
38685 
38686 
38687 /*
38688 ** Implementation of the sqlite3_pcache.xUnpin method.
38689 **
38690 ** Mark a page as unpinned (eligible for asynchronous recycling).
38691 */
38692 static void pcache1Unpin(
38693   sqlite3_pcache *p,
38694   sqlite3_pcache_page *pPg,
38695   int reuseUnlikely
38696 ){
38697   PCache1 *pCache = (PCache1 *)p;
38698   PgHdr1 *pPage = (PgHdr1 *)pPg;
38699   PGroup *pGroup = pCache->pGroup;
38700 
38701   assert( pPage->pCache==pCache );
38702   pcache1EnterMutex(pGroup);
38703 
38704   /* It is an error to call this function if the page is already
38705   ** part of the PGroup LRU list.
38706   */
38707   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
38708   assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
38709   assert( pPage->isPinned==1 );
38710 
38711   if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
38712     pcache1RemoveFromHash(pPage);
38713     pcache1FreePage(pPage);
38714   }else{
38715     /* Add the page to the PGroup LRU list. */
38716     if( pGroup->pLruHead ){
38717       pGroup->pLruHead->pLruPrev = pPage;
38718       pPage->pLruNext = pGroup->pLruHead;
38719       pGroup->pLruHead = pPage;
38720     }else{
38721       pGroup->pLruTail = pPage;
38722       pGroup->pLruHead = pPage;
38723     }
38724     pCache->nRecyclable++;
38725     pPage->isPinned = 0;
38726   }
38727 
38728   pcache1LeaveMutex(pCache->pGroup);
38729 }
38730 
38731 /*
38732 ** Implementation of the sqlite3_pcache.xRekey method.
38733 */
38734 static void pcache1Rekey(
38735   sqlite3_pcache *p,
38736   sqlite3_pcache_page *pPg,
38737   unsigned int iOld,
38738   unsigned int iNew
38739 ){
38740   PCache1 *pCache = (PCache1 *)p;
38741   PgHdr1 *pPage = (PgHdr1 *)pPg;
38742   PgHdr1 **pp;
38743   unsigned int h;
38744   assert( pPage->iKey==iOld );
38745   assert( pPage->pCache==pCache );
38746 
38747   pcache1EnterMutex(pCache->pGroup);
38748 
38749   h = iOld%pCache->nHash;
38750   pp = &pCache->apHash[h];
38751   while( (*pp)!=pPage ){
38752     pp = &(*pp)->pNext;
38753   }
38754   *pp = pPage->pNext;
38755 
38756   h = iNew%pCache->nHash;
38757   pPage->iKey = iNew;
38758   pPage->pNext = pCache->apHash[h];
38759   pCache->apHash[h] = pPage;
38760   if( iNew>pCache->iMaxKey ){
38761     pCache->iMaxKey = iNew;
38762   }
38763 
38764   pcache1LeaveMutex(pCache->pGroup);
38765 }
38766 
38767 /*
38768 ** Implementation of the sqlite3_pcache.xTruncate method.
38769 **
38770 ** Discard all unpinned pages in the cache with a page number equal to
38771 ** or greater than parameter iLimit. Any pinned pages with a page number
38772 ** equal to or greater than iLimit are implicitly unpinned.
38773 */
38774 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
38775   PCache1 *pCache = (PCache1 *)p;
38776   pcache1EnterMutex(pCache->pGroup);
38777   if( iLimit<=pCache->iMaxKey ){
38778     pcache1TruncateUnsafe(pCache, iLimit);
38779     pCache->iMaxKey = iLimit-1;
38780   }
38781   pcache1LeaveMutex(pCache->pGroup);
38782 }
38783 
38784 /*
38785 ** Implementation of the sqlite3_pcache.xDestroy method.
38786 **
38787 ** Destroy a cache allocated using pcache1Create().
38788 */
38789 static void pcache1Destroy(sqlite3_pcache *p){
38790   PCache1 *pCache = (PCache1 *)p;
38791   PGroup *pGroup = pCache->pGroup;
38792   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
38793   pcache1EnterMutex(pGroup);
38794   pcache1TruncateUnsafe(pCache, 0);
38795   assert( pGroup->nMaxPage >= pCache->nMax );
38796   pGroup->nMaxPage -= pCache->nMax;
38797   assert( pGroup->nMinPage >= pCache->nMin );
38798   pGroup->nMinPage -= pCache->nMin;
38799   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
38800   pcache1EnforceMaxPage(pGroup);
38801   pcache1LeaveMutex(pGroup);
38802   sqlite3_free(pCache->apHash);
38803   sqlite3_free(pCache);
38804 }
38805 
38806 /*
38807 ** This function is called during initialization (sqlite3_initialize()) to
38808 ** install the default pluggable cache module, assuming the user has not
38809 ** already provided an alternative.
38810 */
38811 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
38812   static const sqlite3_pcache_methods2 defaultMethods = {
38813     1,                       /* iVersion */
38814     0,                       /* pArg */
38815     pcache1Init,             /* xInit */
38816     pcache1Shutdown,         /* xShutdown */
38817     pcache1Create,           /* xCreate */
38818     pcache1Cachesize,        /* xCachesize */
38819     pcache1Pagecount,        /* xPagecount */
38820     pcache1Fetch,            /* xFetch */
38821     pcache1Unpin,            /* xUnpin */
38822     pcache1Rekey,            /* xRekey */
38823     pcache1Truncate,         /* xTruncate */
38824     pcache1Destroy,          /* xDestroy */
38825     pcache1Shrink            /* xShrink */
38826   };
38827   sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
38828 }
38829 
38830 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
38831 /*
38832 ** This function is called to free superfluous dynamically allocated memory
38833 ** held by the pager system. Memory in use by any SQLite pager allocated
38834 ** by the current thread may be sqlite3_free()ed.
38835 **
38836 ** nReq is the number of bytes of memory required. Once this much has
38837 ** been released, the function returns. The return value is the total number
38838 ** of bytes of memory released.
38839 */
38840 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
38841   int nFree = 0;
38842   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
38843   assert( sqlite3_mutex_notheld(pcache1.mutex) );
38844   if( pcache1.pStart==0 ){
38845     PgHdr1 *p;
38846     pcache1EnterMutex(&pcache1.grp);
38847     while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
38848       nFree += pcache1MemSize(p->page.pBuf);
38849 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
38850       nFree += sqlite3MemSize(p);
38851 #endif
38852       assert( p->isPinned==0 );
38853       pcache1PinPage(p);
38854       pcache1RemoveFromHash(p);
38855       pcache1FreePage(p);
38856     }
38857     pcache1LeaveMutex(&pcache1.grp);
38858   }
38859   return nFree;
38860 }
38861 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
38862 
38863 #ifdef SQLITE_TEST
38864 /*
38865 ** This function is used by test procedures to inspect the internal state
38866 ** of the global cache.
38867 */
38868 SQLITE_PRIVATE void sqlite3PcacheStats(
38869   int *pnCurrent,      /* OUT: Total number of pages cached */
38870   int *pnMax,          /* OUT: Global maximum cache size */
38871   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
38872   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
38873 ){
38874   PgHdr1 *p;
38875   int nRecyclable = 0;
38876   for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
38877     assert( p->isPinned==0 );
38878     nRecyclable++;
38879   }
38880   *pnCurrent = pcache1.grp.nCurrentPage;
38881   *pnMax = (int)pcache1.grp.nMaxPage;
38882   *pnMin = (int)pcache1.grp.nMinPage;
38883   *pnRecyclable = nRecyclable;
38884 }
38885 #endif
38886 
38887 /************** End of pcache1.c *********************************************/
38888 /************** Begin file rowset.c ******************************************/
38889 /*
38890 ** 2008 December 3
38891 **
38892 ** The author disclaims copyright to this source code.  In place of
38893 ** a legal notice, here is a blessing:
38894 **
38895 **    May you do good and not evil.
38896 **    May you find forgiveness for yourself and forgive others.
38897 **    May you share freely, never taking more than you give.
38898 **
38899 *************************************************************************
38900 **
38901 ** This module implements an object we call a "RowSet".
38902 **
38903 ** The RowSet object is a collection of rowids.  Rowids
38904 ** are inserted into the RowSet in an arbitrary order.  Inserts
38905 ** can be intermixed with tests to see if a given rowid has been
38906 ** previously inserted into the RowSet.
38907 **
38908 ** After all inserts are finished, it is possible to extract the
38909 ** elements of the RowSet in sorted order.  Once this extraction
38910 ** process has started, no new elements may be inserted.
38911 **
38912 ** Hence, the primitive operations for a RowSet are:
38913 **
38914 **    CREATE
38915 **    INSERT
38916 **    TEST
38917 **    SMALLEST
38918 **    DESTROY
38919 **
38920 ** The CREATE and DESTROY primitives are the constructor and destructor,
38921 ** obviously.  The INSERT primitive adds a new element to the RowSet.
38922 ** TEST checks to see if an element is already in the RowSet.  SMALLEST
38923 ** extracts the least value from the RowSet.
38924 **
38925 ** The INSERT primitive might allocate additional memory.  Memory is
38926 ** allocated in chunks so most INSERTs do no allocation.  There is an
38927 ** upper bound on the size of allocated memory.  No memory is freed
38928 ** until DESTROY.
38929 **
38930 ** The TEST primitive includes a "batch" number.  The TEST primitive
38931 ** will only see elements that were inserted before the last change
38932 ** in the batch number.  In other words, if an INSERT occurs between
38933 ** two TESTs where the TESTs have the same batch nubmer, then the
38934 ** value added by the INSERT will not be visible to the second TEST.
38935 ** The initial batch number is zero, so if the very first TEST contains
38936 ** a non-zero batch number, it will see all prior INSERTs.
38937 **
38938 ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
38939 ** that is attempted.
38940 **
38941 ** The cost of an INSERT is roughly constant.  (Sometime new memory
38942 ** has to be allocated on an INSERT.)  The cost of a TEST with a new
38943 ** batch number is O(NlogN) where N is the number of elements in the RowSet.
38944 ** The cost of a TEST using the same batch number is O(logN).  The cost
38945 ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
38946 ** primitives are constant time.  The cost of DESTROY is O(N).
38947 **
38948 ** There is an added cost of O(N) when switching between TEST and
38949 ** SMALLEST primitives.
38950 */
38951 
38952 
38953 /*
38954 ** Target size for allocation chunks.
38955 */
38956 #define ROWSET_ALLOCATION_SIZE 1024
38957 
38958 /*
38959 ** The number of rowset entries per allocation chunk.
38960 */
38961 #define ROWSET_ENTRY_PER_CHUNK  \
38962                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
38963 
38964 /*
38965 ** Each entry in a RowSet is an instance of the following object.
38966 **
38967 ** This same object is reused to store a linked list of trees of RowSetEntry
38968 ** objects.  In that alternative use, pRight points to the next entry
38969 ** in the list, pLeft points to the tree, and v is unused.  The
38970 ** RowSet.pForest value points to the head of this forest list.
38971 */
38972 struct RowSetEntry {
38973   i64 v;                        /* ROWID value for this entry */
38974   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
38975   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
38976 };
38977 
38978 /*
38979 ** RowSetEntry objects are allocated in large chunks (instances of the
38980 ** following structure) to reduce memory allocation overhead.  The
38981 ** chunks are kept on a linked list so that they can be deallocated
38982 ** when the RowSet is destroyed.
38983 */
38984 struct RowSetChunk {
38985   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
38986   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
38987 };
38988 
38989 /*
38990 ** A RowSet in an instance of the following structure.
38991 **
38992 ** A typedef of this structure if found in sqliteInt.h.
38993 */
38994 struct RowSet {
38995   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
38996   sqlite3 *db;                   /* The database connection */
38997   struct RowSetEntry *pEntry;    /* List of entries using pRight */
38998   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
38999   struct RowSetEntry *pFresh;    /* Source of new entry objects */
39000   struct RowSetEntry *pForest;   /* List of binary trees of entries */
39001   u16 nFresh;                    /* Number of objects on pFresh */
39002   u8 rsFlags;                    /* Various flags */
39003   u8 iBatch;                     /* Current insert batch */
39004 };
39005 
39006 /*
39007 ** Allowed values for RowSet.rsFlags
39008 */
39009 #define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
39010 #define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
39011 
39012 /*
39013 ** Turn bulk memory into a RowSet object.  N bytes of memory
39014 ** are available at pSpace.  The db pointer is used as a memory context
39015 ** for any subsequent allocations that need to occur.
39016 ** Return a pointer to the new RowSet object.
39017 **
39018 ** It must be the case that N is sufficient to make a Rowset.  If not
39019 ** an assertion fault occurs.
39020 **
39021 ** If N is larger than the minimum, use the surplus as an initial
39022 ** allocation of entries available to be filled.
39023 */
39024 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
39025   RowSet *p;
39026   assert( N >= ROUND8(sizeof(*p)) );
39027   p = pSpace;
39028   p->pChunk = 0;
39029   p->db = db;
39030   p->pEntry = 0;
39031   p->pLast = 0;
39032   p->pForest = 0;
39033   p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
39034   p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
39035   p->rsFlags = ROWSET_SORTED;
39036   p->iBatch = 0;
39037   return p;
39038 }
39039 
39040 /*
39041 ** Deallocate all chunks from a RowSet.  This frees all memory that
39042 ** the RowSet has allocated over its lifetime.  This routine is
39043 ** the destructor for the RowSet.
39044 */
39045 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
39046   struct RowSetChunk *pChunk, *pNextChunk;
39047   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
39048     pNextChunk = pChunk->pNextChunk;
39049     sqlite3DbFree(p->db, pChunk);
39050   }
39051   p->pChunk = 0;
39052   p->nFresh = 0;
39053   p->pEntry = 0;
39054   p->pLast = 0;
39055   p->pForest = 0;
39056   p->rsFlags = ROWSET_SORTED;
39057 }
39058 
39059 /*
39060 ** Allocate a new RowSetEntry object that is associated with the
39061 ** given RowSet.  Return a pointer to the new and completely uninitialized
39062 ** objected.
39063 **
39064 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
39065 ** routine returns NULL.
39066 */
39067 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
39068   assert( p!=0 );
39069   if( p->nFresh==0 ){
39070     struct RowSetChunk *pNew;
39071     pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
39072     if( pNew==0 ){
39073       return 0;
39074     }
39075     pNew->pNextChunk = p->pChunk;
39076     p->pChunk = pNew;
39077     p->pFresh = pNew->aEntry;
39078     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
39079   }
39080   p->nFresh--;
39081   return p->pFresh++;
39082 }
39083 
39084 /*
39085 ** Insert a new value into a RowSet.
39086 **
39087 ** The mallocFailed flag of the database connection is set if a
39088 ** memory allocation fails.
39089 */
39090 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
39091   struct RowSetEntry *pEntry;  /* The new entry */
39092   struct RowSetEntry *pLast;   /* The last prior entry */
39093 
39094   /* This routine is never called after sqlite3RowSetNext() */
39095   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
39096 
39097   pEntry = rowSetEntryAlloc(p);
39098   if( pEntry==0 ) return;
39099   pEntry->v = rowid;
39100   pEntry->pRight = 0;
39101   pLast = p->pLast;
39102   if( pLast ){
39103     if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
39104       p->rsFlags &= ~ROWSET_SORTED;
39105     }
39106     pLast->pRight = pEntry;
39107   }else{
39108     p->pEntry = pEntry;
39109   }
39110   p->pLast = pEntry;
39111 }
39112 
39113 /*
39114 ** Merge two lists of RowSetEntry objects.  Remove duplicates.
39115 **
39116 ** The input lists are connected via pRight pointers and are
39117 ** assumed to each already be in sorted order.
39118 */
39119 static struct RowSetEntry *rowSetEntryMerge(
39120   struct RowSetEntry *pA,    /* First sorted list to be merged */
39121   struct RowSetEntry *pB     /* Second sorted list to be merged */
39122 ){
39123   struct RowSetEntry head;
39124   struct RowSetEntry *pTail;
39125 
39126   pTail = &head;
39127   while( pA && pB ){
39128     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
39129     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
39130     if( pA->v<pB->v ){
39131       pTail->pRight = pA;
39132       pA = pA->pRight;
39133       pTail = pTail->pRight;
39134     }else if( pB->v<pA->v ){
39135       pTail->pRight = pB;
39136       pB = pB->pRight;
39137       pTail = pTail->pRight;
39138     }else{
39139       pA = pA->pRight;
39140     }
39141   }
39142   if( pA ){
39143     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
39144     pTail->pRight = pA;
39145   }else{
39146     assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
39147     pTail->pRight = pB;
39148   }
39149   return head.pRight;
39150 }
39151 
39152 /*
39153 ** Sort all elements on the list of RowSetEntry objects into order of
39154 ** increasing v.
39155 */
39156 static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
39157   unsigned int i;
39158   struct RowSetEntry *pNext, *aBucket[40];
39159 
39160   memset(aBucket, 0, sizeof(aBucket));
39161   while( pIn ){
39162     pNext = pIn->pRight;
39163     pIn->pRight = 0;
39164     for(i=0; aBucket[i]; i++){
39165       pIn = rowSetEntryMerge(aBucket[i], pIn);
39166       aBucket[i] = 0;
39167     }
39168     aBucket[i] = pIn;
39169     pIn = pNext;
39170   }
39171   pIn = 0;
39172   for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
39173     pIn = rowSetEntryMerge(pIn, aBucket[i]);
39174   }
39175   return pIn;
39176 }
39177 
39178 
39179 /*
39180 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
39181 ** Convert this tree into a linked list connected by the pRight pointers
39182 ** and return pointers to the first and last elements of the new list.
39183 */
39184 static void rowSetTreeToList(
39185   struct RowSetEntry *pIn,         /* Root of the input tree */
39186   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
39187   struct RowSetEntry **ppLast      /* Write tail of the output list here */
39188 ){
39189   assert( pIn!=0 );
39190   if( pIn->pLeft ){
39191     struct RowSetEntry *p;
39192     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
39193     p->pRight = pIn;
39194   }else{
39195     *ppFirst = pIn;
39196   }
39197   if( pIn->pRight ){
39198     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
39199   }else{
39200     *ppLast = pIn;
39201   }
39202   assert( (*ppLast)->pRight==0 );
39203 }
39204 
39205 
39206 /*
39207 ** Convert a sorted list of elements (connected by pRight) into a binary
39208 ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
39209 ** node taken from the head of *ppList.  A depth of 2 means a tree with
39210 ** three nodes.  And so forth.
39211 **
39212 ** Use as many entries from the input list as required and update the
39213 ** *ppList to point to the unused elements of the list.  If the input
39214 ** list contains too few elements, then construct an incomplete tree
39215 ** and leave *ppList set to NULL.
39216 **
39217 ** Return a pointer to the root of the constructed binary tree.
39218 */
39219 static struct RowSetEntry *rowSetNDeepTree(
39220   struct RowSetEntry **ppList,
39221   int iDepth
39222 ){
39223   struct RowSetEntry *p;         /* Root of the new tree */
39224   struct RowSetEntry *pLeft;     /* Left subtree */
39225   if( *ppList==0 ){
39226     return 0;
39227   }
39228   if( iDepth==1 ){
39229     p = *ppList;
39230     *ppList = p->pRight;
39231     p->pLeft = p->pRight = 0;
39232     return p;
39233   }
39234   pLeft = rowSetNDeepTree(ppList, iDepth-1);
39235   p = *ppList;
39236   if( p==0 ){
39237     return pLeft;
39238   }
39239   p->pLeft = pLeft;
39240   *ppList = p->pRight;
39241   p->pRight = rowSetNDeepTree(ppList, iDepth-1);
39242   return p;
39243 }
39244 
39245 /*
39246 ** Convert a sorted list of elements into a binary tree. Make the tree
39247 ** as deep as it needs to be in order to contain the entire list.
39248 */
39249 static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
39250   int iDepth;           /* Depth of the tree so far */
39251   struct RowSetEntry *p;       /* Current tree root */
39252   struct RowSetEntry *pLeft;   /* Left subtree */
39253 
39254   assert( pList!=0 );
39255   p = pList;
39256   pList = p->pRight;
39257   p->pLeft = p->pRight = 0;
39258   for(iDepth=1; pList; iDepth++){
39259     pLeft = p;
39260     p = pList;
39261     pList = p->pRight;
39262     p->pLeft = pLeft;
39263     p->pRight = rowSetNDeepTree(&pList, iDepth);
39264   }
39265   return p;
39266 }
39267 
39268 /*
39269 ** Take all the entries on p->pEntry and on the trees in p->pForest and
39270 ** sort them all together into one big ordered list on p->pEntry.
39271 **
39272 ** This routine should only be called once in the life of a RowSet.
39273 */
39274 static void rowSetToList(RowSet *p){
39275 
39276   /* This routine is called only once */
39277   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
39278 
39279   if( (p->rsFlags & ROWSET_SORTED)==0 ){
39280     p->pEntry = rowSetEntrySort(p->pEntry);
39281   }
39282 
39283   /* While this module could theoretically support it, sqlite3RowSetNext()
39284   ** is never called after sqlite3RowSetText() for the same RowSet.  So
39285   ** there is never a forest to deal with.  Should this change, simply
39286   ** remove the assert() and the #if 0. */
39287   assert( p->pForest==0 );
39288 #if 0
39289   while( p->pForest ){
39290     struct RowSetEntry *pTree = p->pForest->pLeft;
39291     if( pTree ){
39292       struct RowSetEntry *pHead, *pTail;
39293       rowSetTreeToList(pTree, &pHead, &pTail);
39294       p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
39295     }
39296     p->pForest = p->pForest->pRight;
39297   }
39298 #endif
39299   p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
39300 }
39301 
39302 /*
39303 ** Extract the smallest element from the RowSet.
39304 ** Write the element into *pRowid.  Return 1 on success.  Return
39305 ** 0 if the RowSet is already empty.
39306 **
39307 ** After this routine has been called, the sqlite3RowSetInsert()
39308 ** routine may not be called again.
39309 */
39310 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
39311   assert( p!=0 );
39312 
39313   /* Merge the forest into a single sorted list on first call */
39314   if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
39315 
39316   /* Return the next entry on the list */
39317   if( p->pEntry ){
39318     *pRowid = p->pEntry->v;
39319     p->pEntry = p->pEntry->pRight;
39320     if( p->pEntry==0 ){
39321       sqlite3RowSetClear(p);
39322     }
39323     return 1;
39324   }else{
39325     return 0;
39326   }
39327 }
39328 
39329 /*
39330 ** Check to see if element iRowid was inserted into the rowset as
39331 ** part of any insert batch prior to iBatch.  Return 1 or 0.
39332 **
39333 ** If this is the first test of a new batch and if there exist entires
39334 ** on pRowSet->pEntry, then sort those entires into the forest at
39335 ** pRowSet->pForest so that they can be tested.
39336 */
39337 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
39338   struct RowSetEntry *p, *pTree;
39339 
39340   /* This routine is never called after sqlite3RowSetNext() */
39341   assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
39342 
39343   /* Sort entries into the forest on the first test of a new batch
39344   */
39345   if( iBatch!=pRowSet->iBatch ){
39346     p = pRowSet->pEntry;
39347     if( p ){
39348       struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
39349       if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
39350         p = rowSetEntrySort(p);
39351       }
39352       for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
39353         ppPrevTree = &pTree->pRight;
39354         if( pTree->pLeft==0 ){
39355           pTree->pLeft = rowSetListToTree(p);
39356           break;
39357         }else{
39358           struct RowSetEntry *pAux, *pTail;
39359           rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
39360           pTree->pLeft = 0;
39361           p = rowSetEntryMerge(pAux, p);
39362         }
39363       }
39364       if( pTree==0 ){
39365         *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
39366         if( pTree ){
39367           pTree->v = 0;
39368           pTree->pRight = 0;
39369           pTree->pLeft = rowSetListToTree(p);
39370         }
39371       }
39372       pRowSet->pEntry = 0;
39373       pRowSet->pLast = 0;
39374       pRowSet->rsFlags |= ROWSET_SORTED;
39375     }
39376     pRowSet->iBatch = iBatch;
39377   }
39378 
39379   /* Test to see if the iRowid value appears anywhere in the forest.
39380   ** Return 1 if it does and 0 if not.
39381   */
39382   for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
39383     p = pTree->pLeft;
39384     while( p ){
39385       if( p->v<iRowid ){
39386         p = p->pRight;
39387       }else if( p->v>iRowid ){
39388         p = p->pLeft;
39389       }else{
39390         return 1;
39391       }
39392     }
39393   }
39394   return 0;
39395 }
39396 
39397 /************** End of rowset.c **********************************************/
39398 /************** Begin file pager.c *******************************************/
39399 /*
39400 ** 2001 September 15
39401 **
39402 ** The author disclaims copyright to this source code.  In place of
39403 ** a legal notice, here is a blessing:
39404 **
39405 **    May you do good and not evil.
39406 **    May you find forgiveness for yourself and forgive others.
39407 **    May you share freely, never taking more than you give.
39408 **
39409 *************************************************************************
39410 ** This is the implementation of the page cache subsystem or "pager".
39411 **
39412 ** The pager is used to access a database disk file.  It implements
39413 ** atomic commit and rollback through the use of a journal file that
39414 ** is separate from the database file.  The pager also implements file
39415 ** locking to prevent two processes from writing the same database
39416 ** file simultaneously, or one process from reading the database while
39417 ** another is writing.
39418 */
39419 #ifndef SQLITE_OMIT_DISKIO
39420 /************** Include wal.h in the middle of pager.c ***********************/
39421 /************** Begin file wal.h *********************************************/
39422 /*
39423 ** 2010 February 1
39424 **
39425 ** The author disclaims copyright to this source code.  In place of
39426 ** a legal notice, here is a blessing:
39427 **
39428 **    May you do good and not evil.
39429 **    May you find forgiveness for yourself and forgive others.
39430 **    May you share freely, never taking more than you give.
39431 **
39432 *************************************************************************
39433 ** This header file defines the interface to the write-ahead logging
39434 ** system. Refer to the comments below and the header comment attached to
39435 ** the implementation of each function in log.c for further details.
39436 */
39437 
39438 #ifndef _WAL_H_
39439 #define _WAL_H_
39440 
39441 
39442 /* Additional values that can be added to the sync_flags argument of
39443 ** sqlite3WalFrames():
39444 */
39445 #define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
39446 #define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
39447 
39448 #ifdef SQLITE_OMIT_WAL
39449 # define sqlite3WalOpen(x,y,z)                   0
39450 # define sqlite3WalLimit(x,y)
39451 # define sqlite3WalClose(w,x,y,z)                0
39452 # define sqlite3WalBeginReadTransaction(y,z)     0
39453 # define sqlite3WalEndReadTransaction(z)
39454 # define sqlite3WalDbsize(y)                     0
39455 # define sqlite3WalBeginWriteTransaction(y)      0
39456 # define sqlite3WalEndWriteTransaction(x)        0
39457 # define sqlite3WalUndo(x,y,z)                   0
39458 # define sqlite3WalSavepoint(y,z)
39459 # define sqlite3WalSavepointUndo(y,z)            0
39460 # define sqlite3WalFrames(u,v,w,x,y,z)           0
39461 # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
39462 # define sqlite3WalCallback(z)                   0
39463 # define sqlite3WalExclusiveMode(y,z)            0
39464 # define sqlite3WalHeapMemory(z)                 0
39465 # define sqlite3WalFramesize(z)                  0
39466 # define sqlite3WalFindFrame(x,y,z)              0
39467 #else
39468 
39469 #define WAL_SAVEPOINT_NDATA 4
39470 
39471 /* Connection to a write-ahead log (WAL) file.
39472 ** There is one object of this type for each pager.
39473 */
39474 typedef struct Wal Wal;
39475 
39476 /* Open and close a connection to a write-ahead log. */
39477 SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
39478 SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
39479 
39480 /* Set the limiting size of a WAL file. */
39481 SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
39482 
39483 /* Used by readers to open (lock) and close (unlock) a snapshot.  A
39484 ** snapshot is like a read-transaction.  It is the state of the database
39485 ** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
39486 ** preserves the current state even if the other threads or processes
39487 ** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
39488 ** transaction and releases the lock.
39489 */
39490 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
39491 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
39492 
39493 /* Read a page from the write-ahead log, if it is present. */
39494 SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
39495 SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
39496 
39497 /* If the WAL is not empty, return the size of the database. */
39498 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
39499 
39500 /* Obtain or release the WRITER lock. */
39501 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
39502 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
39503 
39504 /* Undo any frames written (but not committed) to the log */
39505 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
39506 
39507 /* Return an integer that records the current (uncommitted) write
39508 ** position in the WAL */
39509 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
39510 
39511 /* Move the write position of the WAL back to iFrame.  Called in
39512 ** response to a ROLLBACK TO command. */
39513 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
39514 
39515 /* Write a frame or frames to the log. */
39516 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
39517 
39518 /* Copy pages from the log to the database file */
39519 SQLITE_PRIVATE int sqlite3WalCheckpoint(
39520   Wal *pWal,                      /* Write-ahead log connection */
39521   int eMode,                      /* One of PASSIVE, FULL and RESTART */
39522   int (*xBusy)(void*),            /* Function to call when busy */
39523   void *pBusyArg,                 /* Context argument for xBusyHandler */
39524   int sync_flags,                 /* Flags to sync db file with (or 0) */
39525   int nBuf,                       /* Size of buffer nBuf */
39526   u8 *zBuf,                       /* Temporary buffer to use */
39527   int *pnLog,                     /* OUT: Number of frames in WAL */
39528   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
39529 );
39530 
39531 /* Return the value to pass to a sqlite3_wal_hook callback, the
39532 ** number of frames in the WAL at the point of the last commit since
39533 ** sqlite3WalCallback() was called.  If no commits have occurred since
39534 ** the last call, then return 0.
39535 */
39536 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
39537 
39538 /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
39539 ** by the pager layer on the database file.
39540 */
39541 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
39542 
39543 /* Return true if the argument is non-NULL and the WAL module is using
39544 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
39545 ** WAL module is using shared-memory, return false.
39546 */
39547 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
39548 
39549 #ifdef SQLITE_ENABLE_ZIPVFS
39550 /* If the WAL file is not empty, return the number of bytes of content
39551 ** stored in each frame (i.e. the db page-size when the WAL was created).
39552 */
39553 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
39554 #endif
39555 
39556 #endif /* ifndef SQLITE_OMIT_WAL */
39557 #endif /* _WAL_H_ */
39558 
39559 /************** End of wal.h *************************************************/
39560 /************** Continuing where we left off in pager.c **********************/
39561 
39562 
39563 /******************* NOTES ON THE DESIGN OF THE PAGER ************************
39564 **
39565 ** This comment block describes invariants that hold when using a rollback
39566 ** journal.  These invariants do not apply for journal_mode=WAL,
39567 ** journal_mode=MEMORY, or journal_mode=OFF.
39568 **
39569 ** Within this comment block, a page is deemed to have been synced
39570 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
39571 ** Otherwise, the page is not synced until the xSync method of the VFS
39572 ** is called successfully on the file containing the page.
39573 **
39574 ** Definition:  A page of the database file is said to be "overwriteable" if
39575 ** one or more of the following are true about the page:
39576 **
39577 **     (a)  The original content of the page as it was at the beginning of
39578 **          the transaction has been written into the rollback journal and
39579 **          synced.
39580 **
39581 **     (b)  The page was a freelist leaf page at the start of the transaction.
39582 **
39583 **     (c)  The page number is greater than the largest page that existed in
39584 **          the database file at the start of the transaction.
39585 **
39586 ** (1) A page of the database file is never overwritten unless one of the
39587 **     following are true:
39588 **
39589 **     (a) The page and all other pages on the same sector are overwriteable.
39590 **
39591 **     (b) The atomic page write optimization is enabled, and the entire
39592 **         transaction other than the update of the transaction sequence
39593 **         number consists of a single page change.
39594 **
39595 ** (2) The content of a page written into the rollback journal exactly matches
39596 **     both the content in the database when the rollback journal was written
39597 **     and the content in the database at the beginning of the current
39598 **     transaction.
39599 **
39600 ** (3) Writes to the database file are an integer multiple of the page size
39601 **     in length and are aligned on a page boundary.
39602 **
39603 ** (4) Reads from the database file are either aligned on a page boundary and
39604 **     an integer multiple of the page size in length or are taken from the
39605 **     first 100 bytes of the database file.
39606 **
39607 ** (5) All writes to the database file are synced prior to the rollback journal
39608 **     being deleted, truncated, or zeroed.
39609 **
39610 ** (6) If a master journal file is used, then all writes to the database file
39611 **     are synced prior to the master journal being deleted.
39612 **
39613 ** Definition: Two databases (or the same database at two points it time)
39614 ** are said to be "logically equivalent" if they give the same answer to
39615 ** all queries.  Note in particular the content of freelist leaf
39616 ** pages can be changed arbitarily without effecting the logical equivalence
39617 ** of the database.
39618 **
39619 ** (7) At any time, if any subset, including the empty set and the total set,
39620 **     of the unsynced changes to a rollback journal are removed and the
39621 **     journal is rolled back, the resulting database file will be logical
39622 **     equivalent to the database file at the beginning of the transaction.
39623 **
39624 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
39625 **     is called to restore the database file to the same size it was at
39626 **     the beginning of the transaction.  (In some VFSes, the xTruncate
39627 **     method is a no-op, but that does not change the fact the SQLite will
39628 **     invoke it.)
39629 **
39630 ** (9) Whenever the database file is modified, at least one bit in the range
39631 **     of bytes from 24 through 39 inclusive will be changed prior to releasing
39632 **     the EXCLUSIVE lock, thus signaling other connections on the same
39633 **     database to flush their caches.
39634 **
39635 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
39636 **      than one billion transactions.
39637 **
39638 ** (11) A database file is well-formed at the beginning and at the conclusion
39639 **      of every transaction.
39640 **
39641 ** (12) An EXCLUSIVE lock is held on the database file when writing to
39642 **      the database file.
39643 **
39644 ** (13) A SHARED lock is held on the database file while reading any
39645 **      content out of the database file.
39646 **
39647 ******************************************************************************/
39648 
39649 /*
39650 ** Macros for troubleshooting.  Normally turned off
39651 */
39652 #if 0
39653 int sqlite3PagerTrace=1;  /* True to enable tracing */
39654 #define sqlite3DebugPrintf printf
39655 #define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
39656 #else
39657 #define PAGERTRACE(X)
39658 #endif
39659 
39660 /*
39661 ** The following two macros are used within the PAGERTRACE() macros above
39662 ** to print out file-descriptors.
39663 **
39664 ** PAGERID() takes a pointer to a Pager struct as its argument. The
39665 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
39666 ** struct as its argument.
39667 */
39668 #define PAGERID(p) ((int)(p->fd))
39669 #define FILEHANDLEID(fd) ((int)fd)
39670 
39671 /*
39672 ** The Pager.eState variable stores the current 'state' of a pager. A
39673 ** pager may be in any one of the seven states shown in the following
39674 ** state diagram.
39675 **
39676 **                            OPEN <------+------+
39677 **                              |         |      |
39678 **                              V         |      |
39679 **               +---------> READER-------+      |
39680 **               |              |                |
39681 **               |              V                |
39682 **               |<-------WRITER_LOCKED------> ERROR
39683 **               |              |                ^
39684 **               |              V                |
39685 **               |<------WRITER_CACHEMOD-------->|
39686 **               |              |                |
39687 **               |              V                |
39688 **               |<-------WRITER_DBMOD---------->|
39689 **               |              |                |
39690 **               |              V                |
39691 **               +<------WRITER_FINISHED-------->+
39692 **
39693 **
39694 ** List of state transitions and the C [function] that performs each:
39695 **
39696 **   OPEN              -> READER              [sqlite3PagerSharedLock]
39697 **   READER            -> OPEN                [pager_unlock]
39698 **
39699 **   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
39700 **   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
39701 **   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
39702 **   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
39703 **   WRITER_***        -> READER              [pager_end_transaction]
39704 **
39705 **   WRITER_***        -> ERROR               [pager_error]
39706 **   ERROR             -> OPEN                [pager_unlock]
39707 **
39708 **
39709 **  OPEN:
39710 **
39711 **    The pager starts up in this state. Nothing is guaranteed in this
39712 **    state - the file may or may not be locked and the database size is
39713 **    unknown. The database may not be read or written.
39714 **
39715 **    * No read or write transaction is active.
39716 **    * Any lock, or no lock at all, may be held on the database file.
39717 **    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
39718 **
39719 **  READER:
39720 **
39721 **    In this state all the requirements for reading the database in
39722 **    rollback (non-WAL) mode are met. Unless the pager is (or recently
39723 **    was) in exclusive-locking mode, a user-level read transaction is
39724 **    open. The database size is known in this state.
39725 **
39726 **    A connection running with locking_mode=normal enters this state when
39727 **    it opens a read-transaction on the database and returns to state
39728 **    OPEN after the read-transaction is completed. However a connection
39729 **    running in locking_mode=exclusive (including temp databases) remains in
39730 **    this state even after the read-transaction is closed. The only way
39731 **    a locking_mode=exclusive connection can transition from READER to OPEN
39732 **    is via the ERROR state (see below).
39733 **
39734 **    * A read transaction may be active (but a write-transaction cannot).
39735 **    * A SHARED or greater lock is held on the database file.
39736 **    * The dbSize variable may be trusted (even if a user-level read
39737 **      transaction is not active). The dbOrigSize and dbFileSize variables
39738 **      may not be trusted at this point.
39739 **    * If the database is a WAL database, then the WAL connection is open.
39740 **    * Even if a read-transaction is not open, it is guaranteed that
39741 **      there is no hot-journal in the file-system.
39742 **
39743 **  WRITER_LOCKED:
39744 **
39745 **    The pager moves to this state from READER when a write-transaction
39746 **    is first opened on the database. In WRITER_LOCKED state, all locks
39747 **    required to start a write-transaction are held, but no actual
39748 **    modifications to the cache or database have taken place.
39749 **
39750 **    In rollback mode, a RESERVED or (if the transaction was opened with
39751 **    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
39752 **    moving to this state, but the journal file is not written to or opened
39753 **    to in this state. If the transaction is committed or rolled back while
39754 **    in WRITER_LOCKED state, all that is required is to unlock the database
39755 **    file.
39756 **
39757 **    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
39758 **    If the connection is running with locking_mode=exclusive, an attempt
39759 **    is made to obtain an EXCLUSIVE lock on the database file.
39760 **
39761 **    * A write transaction is active.
39762 **    * If the connection is open in rollback-mode, a RESERVED or greater
39763 **      lock is held on the database file.
39764 **    * If the connection is open in WAL-mode, a WAL write transaction
39765 **      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
39766 **      called).
39767 **    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
39768 **    * The contents of the pager cache have not been modified.
39769 **    * The journal file may or may not be open.
39770 **    * Nothing (not even the first header) has been written to the journal.
39771 **
39772 **  WRITER_CACHEMOD:
39773 **
39774 **    A pager moves from WRITER_LOCKED state to this state when a page is
39775 **    first modified by the upper layer. In rollback mode the journal file
39776 **    is opened (if it is not already open) and a header written to the
39777 **    start of it. The database file on disk has not been modified.
39778 **
39779 **    * A write transaction is active.
39780 **    * A RESERVED or greater lock is held on the database file.
39781 **    * The journal file is open and the first header has been written
39782 **      to it, but the header has not been synced to disk.
39783 **    * The contents of the page cache have been modified.
39784 **
39785 **  WRITER_DBMOD:
39786 **
39787 **    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
39788 **    when it modifies the contents of the database file. WAL connections
39789 **    never enter this state (since they do not modify the database file,
39790 **    just the log file).
39791 **
39792 **    * A write transaction is active.
39793 **    * An EXCLUSIVE or greater lock is held on the database file.
39794 **    * The journal file is open and the first header has been written
39795 **      and synced to disk.
39796 **    * The contents of the page cache have been modified (and possibly
39797 **      written to disk).
39798 **
39799 **  WRITER_FINISHED:
39800 **
39801 **    It is not possible for a WAL connection to enter this state.
39802 **
39803 **    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
39804 **    state after the entire transaction has been successfully written into the
39805 **    database file. In this state the transaction may be committed simply
39806 **    by finalizing the journal file. Once in WRITER_FINISHED state, it is
39807 **    not possible to modify the database further. At this point, the upper
39808 **    layer must either commit or rollback the transaction.
39809 **
39810 **    * A write transaction is active.
39811 **    * An EXCLUSIVE or greater lock is held on the database file.
39812 **    * All writing and syncing of journal and database data has finished.
39813 **      If no error occurred, all that remains is to finalize the journal to
39814 **      commit the transaction. If an error did occur, the caller will need
39815 **      to rollback the transaction.
39816 **
39817 **  ERROR:
39818 **
39819 **    The ERROR state is entered when an IO or disk-full error (including
39820 **    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
39821 **    difficult to be sure that the in-memory pager state (cache contents,
39822 **    db size etc.) are consistent with the contents of the file-system.
39823 **
39824 **    Temporary pager files may enter the ERROR state, but in-memory pagers
39825 **    cannot.
39826 **
39827 **    For example, if an IO error occurs while performing a rollback,
39828 **    the contents of the page-cache may be left in an inconsistent state.
39829 **    At this point it would be dangerous to change back to READER state
39830 **    (as usually happens after a rollback). Any subsequent readers might
39831 **    report database corruption (due to the inconsistent cache), and if
39832 **    they upgrade to writers, they may inadvertently corrupt the database
39833 **    file. To avoid this hazard, the pager switches into the ERROR state
39834 **    instead of READER following such an error.
39835 **
39836 **    Once it has entered the ERROR state, any attempt to use the pager
39837 **    to read or write data returns an error. Eventually, once all
39838 **    outstanding transactions have been abandoned, the pager is able to
39839 **    transition back to OPEN state, discarding the contents of the
39840 **    page-cache and any other in-memory state at the same time. Everything
39841 **    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
39842 **    when a read-transaction is next opened on the pager (transitioning
39843 **    the pager into READER state). At that point the system has recovered
39844 **    from the error.
39845 **
39846 **    Specifically, the pager jumps into the ERROR state if:
39847 **
39848 **      1. An error occurs while attempting a rollback. This happens in
39849 **         function sqlite3PagerRollback().
39850 **
39851 **      2. An error occurs while attempting to finalize a journal file
39852 **         following a commit in function sqlite3PagerCommitPhaseTwo().
39853 **
39854 **      3. An error occurs while attempting to write to the journal or
39855 **         database file in function pagerStress() in order to free up
39856 **         memory.
39857 **
39858 **    In other cases, the error is returned to the b-tree layer. The b-tree
39859 **    layer then attempts a rollback operation. If the error condition
39860 **    persists, the pager enters the ERROR state via condition (1) above.
39861 **
39862 **    Condition (3) is necessary because it can be triggered by a read-only
39863 **    statement executed within a transaction. In this case, if the error
39864 **    code were simply returned to the user, the b-tree layer would not
39865 **    automatically attempt a rollback, as it assumes that an error in a
39866 **    read-only statement cannot leave the pager in an internally inconsistent
39867 **    state.
39868 **
39869 **    * The Pager.errCode variable is set to something other than SQLITE_OK.
39870 **    * There are one or more outstanding references to pages (after the
39871 **      last reference is dropped the pager should move back to OPEN state).
39872 **    * The pager is not an in-memory pager.
39873 **
39874 **
39875 ** Notes:
39876 **
39877 **   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
39878 **     connection is open in WAL mode. A WAL connection is always in one
39879 **     of the first four states.
39880 **
39881 **   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
39882 **     state. There are two exceptions: immediately after exclusive-mode has
39883 **     been turned on (and before any read or write transactions are
39884 **     executed), and when the pager is leaving the "error state".
39885 **
39886 **   * See also: assert_pager_state().
39887 */
39888 #define PAGER_OPEN                  0
39889 #define PAGER_READER                1
39890 #define PAGER_WRITER_LOCKED         2
39891 #define PAGER_WRITER_CACHEMOD       3
39892 #define PAGER_WRITER_DBMOD          4
39893 #define PAGER_WRITER_FINISHED       5
39894 #define PAGER_ERROR                 6
39895 
39896 /*
39897 ** The Pager.eLock variable is almost always set to one of the
39898 ** following locking-states, according to the lock currently held on
39899 ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
39900 ** This variable is kept up to date as locks are taken and released by
39901 ** the pagerLockDb() and pagerUnlockDb() wrappers.
39902 **
39903 ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
39904 ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
39905 ** the operation was successful. In these circumstances pagerLockDb() and
39906 ** pagerUnlockDb() take a conservative approach - eLock is always updated
39907 ** when unlocking the file, and only updated when locking the file if the
39908 ** VFS call is successful. This way, the Pager.eLock variable may be set
39909 ** to a less exclusive (lower) value than the lock that is actually held
39910 ** at the system level, but it is never set to a more exclusive value.
39911 **
39912 ** This is usually safe. If an xUnlock fails or appears to fail, there may
39913 ** be a few redundant xLock() calls or a lock may be held for longer than
39914 ** required, but nothing really goes wrong.
39915 **
39916 ** The exception is when the database file is unlocked as the pager moves
39917 ** from ERROR to OPEN state. At this point there may be a hot-journal file
39918 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
39919 ** transition, by the same pager or any other). If the call to xUnlock()
39920 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
39921 ** can confuse the call to xCheckReservedLock() call made later as part
39922 ** of hot-journal detection.
39923 **
39924 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
39925 ** lock held by this process or any others". So xCheckReservedLock may
39926 ** return true because the caller itself is holding an EXCLUSIVE lock (but
39927 ** doesn't know it because of a previous error in xUnlock). If this happens
39928 ** a hot-journal may be mistaken for a journal being created by an active
39929 ** transaction in another process, causing SQLite to read from the database
39930 ** without rolling it back.
39931 **
39932 ** To work around this, if a call to xUnlock() fails when unlocking the
39933 ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
39934 ** is only changed back to a real locking state after a successful call
39935 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
39936 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
39937 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
39938 ** lock on the database file before attempting to roll it back. See function
39939 ** PagerSharedLock() for more detail.
39940 **
39941 ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
39942 ** PAGER_OPEN state.
39943 */
39944 #define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
39945 
39946 /*
39947 ** A macro used for invoking the codec if there is one
39948 */
39949 #ifdef SQLITE_HAS_CODEC
39950 # define CODEC1(P,D,N,X,E) \
39951     if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
39952 # define CODEC2(P,D,N,X,E,O) \
39953     if( P->xCodec==0 ){ O=(char*)D; }else \
39954     if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
39955 #else
39956 # define CODEC1(P,D,N,X,E)   /* NO-OP */
39957 # define CODEC2(P,D,N,X,E,O) O=(char*)D
39958 #endif
39959 
39960 /*
39961 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method
39962 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
39963 ** This could conceivably cause corruption following a power failure on
39964 ** such a system. This is currently an undocumented limit.
39965 */
39966 #define MAX_SECTOR_SIZE 0x10000
39967 
39968 /*
39969 ** An instance of the following structure is allocated for each active
39970 ** savepoint and statement transaction in the system. All such structures
39971 ** are stored in the Pager.aSavepoint[] array, which is allocated and
39972 ** resized using sqlite3Realloc().
39973 **
39974 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
39975 ** set to 0. If a journal-header is written into the main journal while
39976 ** the savepoint is active, then iHdrOffset is set to the byte offset
39977 ** immediately following the last journal record written into the main
39978 ** journal before the journal-header. This is required during savepoint
39979 ** rollback (see pagerPlaybackSavepoint()).
39980 */
39981 typedef struct PagerSavepoint PagerSavepoint;
39982 struct PagerSavepoint {
39983   i64 iOffset;                 /* Starting offset in main journal */
39984   i64 iHdrOffset;              /* See above */
39985   Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
39986   Pgno nOrig;                  /* Original number of pages in file */
39987   Pgno iSubRec;                /* Index of first record in sub-journal */
39988 #ifndef SQLITE_OMIT_WAL
39989   u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
39990 #endif
39991 };
39992 
39993 /*
39994 ** Bits of the Pager.doNotSpill flag.  See further description below.
39995 */
39996 #define SPILLFLAG_OFF         0x01      /* Never spill cache.  Set via pragma */
39997 #define SPILLFLAG_ROLLBACK    0x02      /* Current rolling back, so do not spill */
39998 #define SPILLFLAG_NOSYNC      0x04      /* Spill is ok, but do not sync */
39999 
40000 /*
40001 ** A open page cache is an instance of struct Pager. A description of
40002 ** some of the more important member variables follows:
40003 **
40004 ** eState
40005 **
40006 **   The current 'state' of the pager object. See the comment and state
40007 **   diagram above for a description of the pager state.
40008 **
40009 ** eLock
40010 **
40011 **   For a real on-disk database, the current lock held on the database file -
40012 **   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
40013 **
40014 **   For a temporary or in-memory database (neither of which require any
40015 **   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
40016 **   databases always have Pager.exclusiveMode==1, this tricks the pager
40017 **   logic into thinking that it already has all the locks it will ever
40018 **   need (and no reason to release them).
40019 **
40020 **   In some (obscure) circumstances, this variable may also be set to
40021 **   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
40022 **   details.
40023 **
40024 ** changeCountDone
40025 **
40026 **   This boolean variable is used to make sure that the change-counter
40027 **   (the 4-byte header field at byte offset 24 of the database file) is
40028 **   not updated more often than necessary.
40029 **
40030 **   It is set to true when the change-counter field is updated, which
40031 **   can only happen if an exclusive lock is held on the database file.
40032 **   It is cleared (set to false) whenever an exclusive lock is
40033 **   relinquished on the database file. Each time a transaction is committed,
40034 **   The changeCountDone flag is inspected. If it is true, the work of
40035 **   updating the change-counter is omitted for the current transaction.
40036 **
40037 **   This mechanism means that when running in exclusive mode, a connection
40038 **   need only update the change-counter once, for the first transaction
40039 **   committed.
40040 **
40041 ** setMaster
40042 **
40043 **   When PagerCommitPhaseOne() is called to commit a transaction, it may
40044 **   (or may not) specify a master-journal name to be written into the
40045 **   journal file before it is synced to disk.
40046 **
40047 **   Whether or not a journal file contains a master-journal pointer affects
40048 **   the way in which the journal file is finalized after the transaction is
40049 **   committed or rolled back when running in "journal_mode=PERSIST" mode.
40050 **   If a journal file does not contain a master-journal pointer, it is
40051 **   finalized by overwriting the first journal header with zeroes. If
40052 **   it does contain a master-journal pointer the journal file is finalized
40053 **   by truncating it to zero bytes, just as if the connection were
40054 **   running in "journal_mode=truncate" mode.
40055 **
40056 **   Journal files that contain master journal pointers cannot be finalized
40057 **   simply by overwriting the first journal-header with zeroes, as the
40058 **   master journal pointer could interfere with hot-journal rollback of any
40059 **   subsequently interrupted transaction that reuses the journal file.
40060 **
40061 **   The flag is cleared as soon as the journal file is finalized (either
40062 **   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
40063 **   journal file from being successfully finalized, the setMaster flag
40064 **   is cleared anyway (and the pager will move to ERROR state).
40065 **
40066 ** doNotSpill
40067 **
40068 **   This variables control the behavior of cache-spills  (calls made by
40069 **   the pcache module to the pagerStress() routine to write cached data
40070 **   to the file-system in order to free up memory).
40071 **
40072 **   When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
40073 **   writing to the database from pagerStress() is disabled altogether.
40074 **   The SPILLFLAG_ROLLBACK case is done in a very obscure case that
40075 **   comes up during savepoint rollback that requires the pcache module
40076 **   to allocate a new page to prevent the journal file from being written
40077 **   while it is being traversed by code in pager_playback().  The SPILLFLAG_OFF
40078 **   case is a user preference.
40079 **
40080 **   If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
40081 **   is permitted, but syncing the journal file is not. This flag is set
40082 **   by sqlite3PagerWrite() when the file-system sector-size is larger than
40083 **   the database page-size in order to prevent a journal sync from happening
40084 **   in between the journalling of two pages on the same sector.
40085 **
40086 ** subjInMemory
40087 **
40088 **   This is a boolean variable. If true, then any required sub-journal
40089 **   is opened as an in-memory journal file. If false, then in-memory
40090 **   sub-journals are only used for in-memory pager files.
40091 **
40092 **   This variable is updated by the upper layer each time a new
40093 **   write-transaction is opened.
40094 **
40095 ** dbSize, dbOrigSize, dbFileSize
40096 **
40097 **   Variable dbSize is set to the number of pages in the database file.
40098 **   It is valid in PAGER_READER and higher states (all states except for
40099 **   OPEN and ERROR).
40100 **
40101 **   dbSize is set based on the size of the database file, which may be
40102 **   larger than the size of the database (the value stored at offset
40103 **   28 of the database header by the btree). If the size of the file
40104 **   is not an integer multiple of the page-size, the value stored in
40105 **   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
40106 **   Except, any file that is greater than 0 bytes in size is considered
40107 **   to have at least one page. (i.e. a 1KB file with 2K page-size leads
40108 **   to dbSize==1).
40109 **
40110 **   During a write-transaction, if pages with page-numbers greater than
40111 **   dbSize are modified in the cache, dbSize is updated accordingly.
40112 **   Similarly, if the database is truncated using PagerTruncateImage(),
40113 **   dbSize is updated.
40114 **
40115 **   Variables dbOrigSize and dbFileSize are valid in states
40116 **   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
40117 **   variable at the start of the transaction. It is used during rollback,
40118 **   and to determine whether or not pages need to be journalled before
40119 **   being modified.
40120 **
40121 **   Throughout a write-transaction, dbFileSize contains the size of
40122 **   the file on disk in pages. It is set to a copy of dbSize when the
40123 **   write-transaction is first opened, and updated when VFS calls are made
40124 **   to write or truncate the database file on disk.
40125 **
40126 **   The only reason the dbFileSize variable is required is to suppress
40127 **   unnecessary calls to xTruncate() after committing a transaction. If,
40128 **   when a transaction is committed, the dbFileSize variable indicates
40129 **   that the database file is larger than the database image (Pager.dbSize),
40130 **   pager_truncate() is called. The pager_truncate() call uses xFilesize()
40131 **   to measure the database file on disk, and then truncates it if required.
40132 **   dbFileSize is not used when rolling back a transaction. In this case
40133 **   pager_truncate() is called unconditionally (which means there may be
40134 **   a call to xFilesize() that is not strictly required). In either case,
40135 **   pager_truncate() may cause the file to become smaller or larger.
40136 **
40137 ** dbHintSize
40138 **
40139 **   The dbHintSize variable is used to limit the number of calls made to
40140 **   the VFS xFileControl(FCNTL_SIZE_HINT) method.
40141 **
40142 **   dbHintSize is set to a copy of the dbSize variable when a
40143 **   write-transaction is opened (at the same time as dbFileSize and
40144 **   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
40145 **   dbHintSize is increased to the number of pages that correspond to the
40146 **   size-hint passed to the method call. See pager_write_pagelist() for
40147 **   details.
40148 **
40149 ** errCode
40150 **
40151 **   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
40152 **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
40153 **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
40154 **   sub-codes.
40155 */
40156 struct Pager {
40157   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
40158   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
40159   u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
40160   u8 useJournal;              /* Use a rollback journal on this file */
40161   u8 noSync;                  /* Do not sync the journal if true */
40162   u8 fullSync;                /* Do extra syncs of the journal for robustness */
40163   u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
40164   u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
40165   u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
40166   u8 tempFile;                /* zFilename is a temporary file */
40167   u8 readOnly;                /* True for a read-only database */
40168   u8 memDb;                   /* True to inhibit all file I/O */
40169 
40170   /**************************************************************************
40171   ** The following block contains those class members that change during
40172   ** routine opertion.  Class members not in this block are either fixed
40173   ** when the pager is first created or else only change when there is a
40174   ** significant mode change (such as changing the page_size, locking_mode,
40175   ** or the journal_mode).  From another view, these class members describe
40176   ** the "state" of the pager, while other class members describe the
40177   ** "configuration" of the pager.
40178   */
40179   u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
40180   u8 eLock;                   /* Current lock held on database file */
40181   u8 changeCountDone;         /* Set after incrementing the change-counter */
40182   u8 setMaster;               /* True if a m-j name has been written to jrnl */
40183   u8 doNotSpill;              /* Do not spill the cache when non-zero */
40184   u8 subjInMemory;            /* True to use in-memory sub-journals */
40185   Pgno dbSize;                /* Number of pages in the database */
40186   Pgno dbOrigSize;            /* dbSize before the current transaction */
40187   Pgno dbFileSize;            /* Number of pages in the database file */
40188   Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
40189   int errCode;                /* One of several kinds of errors */
40190   int nRec;                   /* Pages journalled since last j-header written */
40191   u32 cksumInit;              /* Quasi-random value added to every checksum */
40192   u32 nSubRec;                /* Number of records written to sub-journal */
40193   Bitvec *pInJournal;         /* One bit for each page in the database file */
40194   sqlite3_file *fd;           /* File descriptor for database */
40195   sqlite3_file *jfd;          /* File descriptor for main journal */
40196   sqlite3_file *sjfd;         /* File descriptor for sub-journal */
40197   i64 journalOff;             /* Current write offset in the journal file */
40198   i64 journalHdr;             /* Byte offset to previous journal header */
40199   sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
40200   PagerSavepoint *aSavepoint; /* Array of active savepoints */
40201   int nSavepoint;             /* Number of elements in aSavepoint[] */
40202   char dbFileVers[16];        /* Changes whenever database file changes */
40203 
40204   u8 bUseFetch;               /* True to use xFetch() */
40205   int nMmapOut;               /* Number of mmap pages currently outstanding */
40206   sqlite3_int64 szMmap;       /* Desired maximum mmap size */
40207   PgHdr *pMmapFreelist;       /* List of free mmap page headers (pDirty) */
40208   /*
40209   ** End of the routinely-changing class members
40210   ***************************************************************************/
40211 
40212   u16 nExtra;                 /* Add this many bytes to each in-memory page */
40213   i16 nReserve;               /* Number of unused bytes at end of each page */
40214   u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
40215   u32 sectorSize;             /* Assumed sector size during rollback */
40216   int pageSize;               /* Number of bytes in a page */
40217   Pgno mxPgno;                /* Maximum allowed size of the database */
40218   i64 journalSizeLimit;       /* Size limit for persistent journal files */
40219   char *zFilename;            /* Name of the database file */
40220   char *zJournal;             /* Name of the journal file */
40221   int (*xBusyHandler)(void*); /* Function to call when busy */
40222   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
40223   int aStat[3];               /* Total cache hits, misses and writes */
40224 #ifdef SQLITE_TEST
40225   int nRead;                  /* Database pages read */
40226 #endif
40227   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
40228 #ifdef SQLITE_HAS_CODEC
40229   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
40230   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
40231   void (*xCodecFree)(void*);             /* Destructor for the codec */
40232   void *pCodec;               /* First argument to xCodec... methods */
40233 #endif
40234   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
40235   PCache *pPCache;            /* Pointer to page cache object */
40236 #ifndef SQLITE_OMIT_WAL
40237   Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
40238   char *zWal;                 /* File name for write-ahead log */
40239 #endif
40240 };
40241 
40242 /*
40243 ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
40244 ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
40245 ** or CACHE_WRITE to sqlite3_db_status().
40246 */
40247 #define PAGER_STAT_HIT   0
40248 #define PAGER_STAT_MISS  1
40249 #define PAGER_STAT_WRITE 2
40250 
40251 /*
40252 ** The following global variables hold counters used for
40253 ** testing purposes only.  These variables do not exist in
40254 ** a non-testing build.  These variables are not thread-safe.
40255 */
40256 #ifdef SQLITE_TEST
40257 SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
40258 SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
40259 SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
40260 # define PAGER_INCR(v)  v++
40261 #else
40262 # define PAGER_INCR(v)
40263 #endif
40264 
40265 
40266 
40267 /*
40268 ** Journal files begin with the following magic string.  The data
40269 ** was obtained from /dev/random.  It is used only as a sanity check.
40270 **
40271 ** Since version 2.8.0, the journal format contains additional sanity
40272 ** checking information.  If the power fails while the journal is being
40273 ** written, semi-random garbage data might appear in the journal
40274 ** file after power is restored.  If an attempt is then made
40275 ** to roll the journal back, the database could be corrupted.  The additional
40276 ** sanity checking data is an attempt to discover the garbage in the
40277 ** journal and ignore it.
40278 **
40279 ** The sanity checking information for the new journal format consists
40280 ** of a 32-bit checksum on each page of data.  The checksum covers both
40281 ** the page number and the pPager->pageSize bytes of data for the page.
40282 ** This cksum is initialized to a 32-bit random value that appears in the
40283 ** journal file right after the header.  The random initializer is important,
40284 ** because garbage data that appears at the end of a journal is likely
40285 ** data that was once in other files that have now been deleted.  If the
40286 ** garbage data came from an obsolete journal file, the checksums might
40287 ** be correct.  But by initializing the checksum to random value which
40288 ** is different for every journal, we minimize that risk.
40289 */
40290 static const unsigned char aJournalMagic[] = {
40291   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
40292 };
40293 
40294 /*
40295 ** The size of the of each page record in the journal is given by
40296 ** the following macro.
40297 */
40298 #define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
40299 
40300 /*
40301 ** The journal header size for this pager. This is usually the same
40302 ** size as a single disk sector. See also setSectorSize().
40303 */
40304 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
40305 
40306 /*
40307 ** The macro MEMDB is true if we are dealing with an in-memory database.
40308 ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
40309 ** the value of MEMDB will be a constant and the compiler will optimize
40310 ** out code that would never execute.
40311 */
40312 #ifdef SQLITE_OMIT_MEMORYDB
40313 # define MEMDB 0
40314 #else
40315 # define MEMDB pPager->memDb
40316 #endif
40317 
40318 /*
40319 ** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
40320 ** interfaces to access the database using memory-mapped I/O.
40321 */
40322 #if SQLITE_MAX_MMAP_SIZE>0
40323 # define USEFETCH(x) ((x)->bUseFetch)
40324 #else
40325 # define USEFETCH(x) 0
40326 #endif
40327 
40328 /*
40329 ** The maximum legal page number is (2^31 - 1).
40330 */
40331 #define PAGER_MAX_PGNO 2147483647
40332 
40333 /*
40334 ** The argument to this macro is a file descriptor (type sqlite3_file*).
40335 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
40336 **
40337 ** This is so that expressions can be written as:
40338 **
40339 **   if( isOpen(pPager->jfd) ){ ...
40340 **
40341 ** instead of
40342 **
40343 **   if( pPager->jfd->pMethods ){ ...
40344 */
40345 #define isOpen(pFd) ((pFd)->pMethods)
40346 
40347 /*
40348 ** Return true if this pager uses a write-ahead log instead of the usual
40349 ** rollback journal. Otherwise false.
40350 */
40351 #ifndef SQLITE_OMIT_WAL
40352 static int pagerUseWal(Pager *pPager){
40353   return (pPager->pWal!=0);
40354 }
40355 #else
40356 # define pagerUseWal(x) 0
40357 # define pagerRollbackWal(x) 0
40358 # define pagerWalFrames(v,w,x,y) 0
40359 # define pagerOpenWalIfPresent(z) SQLITE_OK
40360 # define pagerBeginReadTransaction(z) SQLITE_OK
40361 #endif
40362 
40363 #ifndef NDEBUG
40364 /*
40365 ** Usage:
40366 **
40367 **   assert( assert_pager_state(pPager) );
40368 **
40369 ** This function runs many asserts to try to find inconsistencies in
40370 ** the internal state of the Pager object.
40371 */
40372 static int assert_pager_state(Pager *p){
40373   Pager *pPager = p;
40374 
40375   /* State must be valid. */
40376   assert( p->eState==PAGER_OPEN
40377        || p->eState==PAGER_READER
40378        || p->eState==PAGER_WRITER_LOCKED
40379        || p->eState==PAGER_WRITER_CACHEMOD
40380        || p->eState==PAGER_WRITER_DBMOD
40381        || p->eState==PAGER_WRITER_FINISHED
40382        || p->eState==PAGER_ERROR
40383   );
40384 
40385   /* Regardless of the current state, a temp-file connection always behaves
40386   ** as if it has an exclusive lock on the database file. It never updates
40387   ** the change-counter field, so the changeCountDone flag is always set.
40388   */
40389   assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
40390   assert( p->tempFile==0 || pPager->changeCountDone );
40391 
40392   /* If the useJournal flag is clear, the journal-mode must be "OFF".
40393   ** And if the journal-mode is "OFF", the journal file must not be open.
40394   */
40395   assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
40396   assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
40397 
40398   /* Check that MEMDB implies noSync. And an in-memory journal. Since
40399   ** this means an in-memory pager performs no IO at all, it cannot encounter
40400   ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
40401   ** a journal file. (although the in-memory journal implementation may
40402   ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
40403   ** is therefore not possible for an in-memory pager to enter the ERROR
40404   ** state.
40405   */
40406   if( MEMDB ){
40407     assert( p->noSync );
40408     assert( p->journalMode==PAGER_JOURNALMODE_OFF
40409          || p->journalMode==PAGER_JOURNALMODE_MEMORY
40410     );
40411     assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
40412     assert( pagerUseWal(p)==0 );
40413   }
40414 
40415   /* If changeCountDone is set, a RESERVED lock or greater must be held
40416   ** on the file.
40417   */
40418   assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
40419   assert( p->eLock!=PENDING_LOCK );
40420 
40421   switch( p->eState ){
40422     case PAGER_OPEN:
40423       assert( !MEMDB );
40424       assert( pPager->errCode==SQLITE_OK );
40425       assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
40426       break;
40427 
40428     case PAGER_READER:
40429       assert( pPager->errCode==SQLITE_OK );
40430       assert( p->eLock!=UNKNOWN_LOCK );
40431       assert( p->eLock>=SHARED_LOCK );
40432       break;
40433 
40434     case PAGER_WRITER_LOCKED:
40435       assert( p->eLock!=UNKNOWN_LOCK );
40436       assert( pPager->errCode==SQLITE_OK );
40437       if( !pagerUseWal(pPager) ){
40438         assert( p->eLock>=RESERVED_LOCK );
40439       }
40440       assert( pPager->dbSize==pPager->dbOrigSize );
40441       assert( pPager->dbOrigSize==pPager->dbFileSize );
40442       assert( pPager->dbOrigSize==pPager->dbHintSize );
40443       assert( pPager->setMaster==0 );
40444       break;
40445 
40446     case PAGER_WRITER_CACHEMOD:
40447       assert( p->eLock!=UNKNOWN_LOCK );
40448       assert( pPager->errCode==SQLITE_OK );
40449       if( !pagerUseWal(pPager) ){
40450         /* It is possible that if journal_mode=wal here that neither the
40451         ** journal file nor the WAL file are open. This happens during
40452         ** a rollback transaction that switches from journal_mode=off
40453         ** to journal_mode=wal.
40454         */
40455         assert( p->eLock>=RESERVED_LOCK );
40456         assert( isOpen(p->jfd)
40457              || p->journalMode==PAGER_JOURNALMODE_OFF
40458              || p->journalMode==PAGER_JOURNALMODE_WAL
40459         );
40460       }
40461       assert( pPager->dbOrigSize==pPager->dbFileSize );
40462       assert( pPager->dbOrigSize==pPager->dbHintSize );
40463       break;
40464 
40465     case PAGER_WRITER_DBMOD:
40466       assert( p->eLock==EXCLUSIVE_LOCK );
40467       assert( pPager->errCode==SQLITE_OK );
40468       assert( !pagerUseWal(pPager) );
40469       assert( p->eLock>=EXCLUSIVE_LOCK );
40470       assert( isOpen(p->jfd)
40471            || p->journalMode==PAGER_JOURNALMODE_OFF
40472            || p->journalMode==PAGER_JOURNALMODE_WAL
40473       );
40474       assert( pPager->dbOrigSize<=pPager->dbHintSize );
40475       break;
40476 
40477     case PAGER_WRITER_FINISHED:
40478       assert( p->eLock==EXCLUSIVE_LOCK );
40479       assert( pPager->errCode==SQLITE_OK );
40480       assert( !pagerUseWal(pPager) );
40481       assert( isOpen(p->jfd)
40482            || p->journalMode==PAGER_JOURNALMODE_OFF
40483            || p->journalMode==PAGER_JOURNALMODE_WAL
40484       );
40485       break;
40486 
40487     case PAGER_ERROR:
40488       /* There must be at least one outstanding reference to the pager if
40489       ** in ERROR state. Otherwise the pager should have already dropped
40490       ** back to OPEN state.
40491       */
40492       assert( pPager->errCode!=SQLITE_OK );
40493       assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
40494       break;
40495   }
40496 
40497   return 1;
40498 }
40499 #endif /* ifndef NDEBUG */
40500 
40501 #ifdef SQLITE_DEBUG
40502 /*
40503 ** Return a pointer to a human readable string in a static buffer
40504 ** containing the state of the Pager object passed as an argument. This
40505 ** is intended to be used within debuggers. For example, as an alternative
40506 ** to "print *pPager" in gdb:
40507 **
40508 ** (gdb) printf "%s", print_pager_state(pPager)
40509 */
40510 static char *print_pager_state(Pager *p){
40511   static char zRet[1024];
40512 
40513   sqlite3_snprintf(1024, zRet,
40514       "Filename:      %s\n"
40515       "State:         %s errCode=%d\n"
40516       "Lock:          %s\n"
40517       "Locking mode:  locking_mode=%s\n"
40518       "Journal mode:  journal_mode=%s\n"
40519       "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
40520       "Journal:       journalOff=%lld journalHdr=%lld\n"
40521       "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
40522       , p->zFilename
40523       , p->eState==PAGER_OPEN            ? "OPEN" :
40524         p->eState==PAGER_READER          ? "READER" :
40525         p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
40526         p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
40527         p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
40528         p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
40529         p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
40530       , (int)p->errCode
40531       , p->eLock==NO_LOCK         ? "NO_LOCK" :
40532         p->eLock==RESERVED_LOCK   ? "RESERVED" :
40533         p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
40534         p->eLock==SHARED_LOCK     ? "SHARED" :
40535         p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
40536       , p->exclusiveMode ? "exclusive" : "normal"
40537       , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
40538         p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
40539         p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
40540         p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
40541         p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
40542         p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
40543       , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
40544       , p->journalOff, p->journalHdr
40545       , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
40546   );
40547 
40548   return zRet;
40549 }
40550 #endif
40551 
40552 /*
40553 ** Return true if it is necessary to write page *pPg into the sub-journal.
40554 ** A page needs to be written into the sub-journal if there exists one
40555 ** or more open savepoints for which:
40556 **
40557 **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
40558 **   * The bit corresponding to the page-number is not set in
40559 **     PagerSavepoint.pInSavepoint.
40560 */
40561 static int subjRequiresPage(PgHdr *pPg){
40562   Pager *pPager = pPg->pPager;
40563   PagerSavepoint *p;
40564   Pgno pgno = pPg->pgno;
40565   int i;
40566   for(i=0; i<pPager->nSavepoint; i++){
40567     p = &pPager->aSavepoint[i];
40568     if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
40569       return 1;
40570     }
40571   }
40572   return 0;
40573 }
40574 
40575 /*
40576 ** Return true if the page is already in the journal file.
40577 */
40578 static int pageInJournal(Pager *pPager, PgHdr *pPg){
40579   return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
40580 }
40581 
40582 /*
40583 ** Read a 32-bit integer from the given file descriptor.  Store the integer
40584 ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
40585 ** error code is something goes wrong.
40586 **
40587 ** All values are stored on disk as big-endian.
40588 */
40589 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
40590   unsigned char ac[4];
40591   int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
40592   if( rc==SQLITE_OK ){
40593     *pRes = sqlite3Get4byte(ac);
40594   }
40595   return rc;
40596 }
40597 
40598 /*
40599 ** Write a 32-bit integer into a string buffer in big-endian byte order.
40600 */
40601 #define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
40602 
40603 
40604 /*
40605 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
40606 ** on success or an error code is something goes wrong.
40607 */
40608 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
40609   char ac[4];
40610   put32bits(ac, val);
40611   return sqlite3OsWrite(fd, ac, 4, offset);
40612 }
40613 
40614 /*
40615 ** Unlock the database file to level eLock, which must be either NO_LOCK
40616 ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
40617 ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
40618 **
40619 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
40620 ** called, do not modify it. See the comment above the #define of
40621 ** UNKNOWN_LOCK for an explanation of this.
40622 */
40623 static int pagerUnlockDb(Pager *pPager, int eLock){
40624   int rc = SQLITE_OK;
40625 
40626   assert( !pPager->exclusiveMode || pPager->eLock==eLock );
40627   assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
40628   assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
40629   if( isOpen(pPager->fd) ){
40630     assert( pPager->eLock>=eLock );
40631     rc = sqlite3OsUnlock(pPager->fd, eLock);
40632     if( pPager->eLock!=UNKNOWN_LOCK ){
40633       pPager->eLock = (u8)eLock;
40634     }
40635     IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
40636   }
40637   return rc;
40638 }
40639 
40640 /*
40641 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
40642 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
40643 ** Pager.eLock variable to the new locking state.
40644 **
40645 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
40646 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
40647 ** See the comment above the #define of UNKNOWN_LOCK for an explanation
40648 ** of this.
40649 */
40650 static int pagerLockDb(Pager *pPager, int eLock){
40651   int rc = SQLITE_OK;
40652 
40653   assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
40654   if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
40655     rc = sqlite3OsLock(pPager->fd, eLock);
40656     if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
40657       pPager->eLock = (u8)eLock;
40658       IOTRACE(("LOCK %p %d\n", pPager, eLock))
40659     }
40660   }
40661   return rc;
40662 }
40663 
40664 /*
40665 ** This function determines whether or not the atomic-write optimization
40666 ** can be used with this pager. The optimization can be used if:
40667 **
40668 **  (a) the value returned by OsDeviceCharacteristics() indicates that
40669 **      a database page may be written atomically, and
40670 **  (b) the value returned by OsSectorSize() is less than or equal
40671 **      to the page size.
40672 **
40673 ** The optimization is also always enabled for temporary files. It is
40674 ** an error to call this function if pPager is opened on an in-memory
40675 ** database.
40676 **
40677 ** If the optimization cannot be used, 0 is returned. If it can be used,
40678 ** then the value returned is the size of the journal file when it
40679 ** contains rollback data for exactly one page.
40680 */
40681 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
40682 static int jrnlBufferSize(Pager *pPager){
40683   assert( !MEMDB );
40684   if( !pPager->tempFile ){
40685     int dc;                           /* Device characteristics */
40686     int nSector;                      /* Sector size */
40687     int szPage;                       /* Page size */
40688 
40689     assert( isOpen(pPager->fd) );
40690     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
40691     nSector = pPager->sectorSize;
40692     szPage = pPager->pageSize;
40693 
40694     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
40695     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
40696     if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
40697       return 0;
40698     }
40699   }
40700 
40701   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
40702 }
40703 #endif
40704 
40705 /*
40706 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
40707 ** on the cache using a hash function.  This is used for testing
40708 ** and debugging only.
40709 */
40710 #ifdef SQLITE_CHECK_PAGES
40711 /*
40712 ** Return a 32-bit hash of the page data for pPage.
40713 */
40714 static u32 pager_datahash(int nByte, unsigned char *pData){
40715   u32 hash = 0;
40716   int i;
40717   for(i=0; i<nByte; i++){
40718     hash = (hash*1039) + pData[i];
40719   }
40720   return hash;
40721 }
40722 static u32 pager_pagehash(PgHdr *pPage){
40723   return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
40724 }
40725 static void pager_set_pagehash(PgHdr *pPage){
40726   pPage->pageHash = pager_pagehash(pPage);
40727 }
40728 
40729 /*
40730 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
40731 ** is defined, and NDEBUG is not defined, an assert() statement checks
40732 ** that the page is either dirty or still matches the calculated page-hash.
40733 */
40734 #define CHECK_PAGE(x) checkPage(x)
40735 static void checkPage(PgHdr *pPg){
40736   Pager *pPager = pPg->pPager;
40737   assert( pPager->eState!=PAGER_ERROR );
40738   assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
40739 }
40740 
40741 #else
40742 #define pager_datahash(X,Y)  0
40743 #define pager_pagehash(X)  0
40744 #define pager_set_pagehash(X)
40745 #define CHECK_PAGE(x)
40746 #endif  /* SQLITE_CHECK_PAGES */
40747 
40748 /*
40749 ** When this is called the journal file for pager pPager must be open.
40750 ** This function attempts to read a master journal file name from the
40751 ** end of the file and, if successful, copies it into memory supplied
40752 ** by the caller. See comments above writeMasterJournal() for the format
40753 ** used to store a master journal file name at the end of a journal file.
40754 **
40755 ** zMaster must point to a buffer of at least nMaster bytes allocated by
40756 ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
40757 ** enough space to write the master journal name). If the master journal
40758 ** name in the journal is longer than nMaster bytes (including a
40759 ** nul-terminator), then this is handled as if no master journal name
40760 ** were present in the journal.
40761 **
40762 ** If a master journal file name is present at the end of the journal
40763 ** file, then it is copied into the buffer pointed to by zMaster. A
40764 ** nul-terminator byte is appended to the buffer following the master
40765 ** journal file name.
40766 **
40767 ** If it is determined that no master journal file name is present
40768 ** zMaster[0] is set to 0 and SQLITE_OK returned.
40769 **
40770 ** If an error occurs while reading from the journal file, an SQLite
40771 ** error code is returned.
40772 */
40773 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
40774   int rc;                    /* Return code */
40775   u32 len;                   /* Length in bytes of master journal name */
40776   i64 szJ;                   /* Total size in bytes of journal file pJrnl */
40777   u32 cksum;                 /* MJ checksum value read from journal */
40778   u32 u;                     /* Unsigned loop counter */
40779   unsigned char aMagic[8];   /* A buffer to hold the magic header */
40780   zMaster[0] = '\0';
40781 
40782   if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
40783    || szJ<16
40784    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
40785    || len>=nMaster
40786    || len==0
40787    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
40788    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
40789    || memcmp(aMagic, aJournalMagic, 8)
40790    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
40791   ){
40792     return rc;
40793   }
40794 
40795   /* See if the checksum matches the master journal name */
40796   for(u=0; u<len; u++){
40797     cksum -= zMaster[u];
40798   }
40799   if( cksum ){
40800     /* If the checksum doesn't add up, then one or more of the disk sectors
40801     ** containing the master journal filename is corrupted. This means
40802     ** definitely roll back, so just return SQLITE_OK and report a (nul)
40803     ** master-journal filename.
40804     */
40805     len = 0;
40806   }
40807   zMaster[len] = '\0';
40808 
40809   return SQLITE_OK;
40810 }
40811 
40812 /*
40813 ** Return the offset of the sector boundary at or immediately
40814 ** following the value in pPager->journalOff, assuming a sector
40815 ** size of pPager->sectorSize bytes.
40816 **
40817 ** i.e for a sector size of 512:
40818 **
40819 **   Pager.journalOff          Return value
40820 **   ---------------------------------------
40821 **   0                         0
40822 **   512                       512
40823 **   100                       512
40824 **   2000                      2048
40825 **
40826 */
40827 static i64 journalHdrOffset(Pager *pPager){
40828   i64 offset = 0;
40829   i64 c = pPager->journalOff;
40830   if( c ){
40831     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
40832   }
40833   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
40834   assert( offset>=c );
40835   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
40836   return offset;
40837 }
40838 
40839 /*
40840 ** The journal file must be open when this function is called.
40841 **
40842 ** This function is a no-op if the journal file has not been written to
40843 ** within the current transaction (i.e. if Pager.journalOff==0).
40844 **
40845 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
40846 ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
40847 ** zero the 28-byte header at the start of the journal file. In either case,
40848 ** if the pager is not in no-sync mode, sync the journal file immediately
40849 ** after writing or truncating it.
40850 **
40851 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
40852 ** following the truncation or zeroing described above the size of the
40853 ** journal file in bytes is larger than this value, then truncate the
40854 ** journal file to Pager.journalSizeLimit bytes. The journal file does
40855 ** not need to be synced following this operation.
40856 **
40857 ** If an IO error occurs, abandon processing and return the IO error code.
40858 ** Otherwise, return SQLITE_OK.
40859 */
40860 static int zeroJournalHdr(Pager *pPager, int doTruncate){
40861   int rc = SQLITE_OK;                               /* Return code */
40862   assert( isOpen(pPager->jfd) );
40863   if( pPager->journalOff ){
40864     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
40865 
40866     IOTRACE(("JZEROHDR %p\n", pPager))
40867     if( doTruncate || iLimit==0 ){
40868       rc = sqlite3OsTruncate(pPager->jfd, 0);
40869     }else{
40870       static const char zeroHdr[28] = {0};
40871       rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
40872     }
40873     if( rc==SQLITE_OK && !pPager->noSync ){
40874       rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
40875     }
40876 
40877     /* At this point the transaction is committed but the write lock
40878     ** is still held on the file. If there is a size limit configured for
40879     ** the persistent journal and the journal file currently consumes more
40880     ** space than that limit allows for, truncate it now. There is no need
40881     ** to sync the file following this operation.
40882     */
40883     if( rc==SQLITE_OK && iLimit>0 ){
40884       i64 sz;
40885       rc = sqlite3OsFileSize(pPager->jfd, &sz);
40886       if( rc==SQLITE_OK && sz>iLimit ){
40887         rc = sqlite3OsTruncate(pPager->jfd, iLimit);
40888       }
40889     }
40890   }
40891   return rc;
40892 }
40893 
40894 /*
40895 ** The journal file must be open when this routine is called. A journal
40896 ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
40897 ** current location.
40898 **
40899 ** The format for the journal header is as follows:
40900 ** - 8 bytes: Magic identifying journal format.
40901 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
40902 ** - 4 bytes: Random number used for page hash.
40903 ** - 4 bytes: Initial database page count.
40904 ** - 4 bytes: Sector size used by the process that wrote this journal.
40905 ** - 4 bytes: Database page size.
40906 **
40907 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
40908 */
40909 static int writeJournalHdr(Pager *pPager){
40910   int rc = SQLITE_OK;                 /* Return code */
40911   char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
40912   u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
40913   u32 nWrite;                         /* Bytes of header sector written */
40914   int ii;                             /* Loop counter */
40915 
40916   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
40917 
40918   if( nHeader>JOURNAL_HDR_SZ(pPager) ){
40919     nHeader = JOURNAL_HDR_SZ(pPager);
40920   }
40921 
40922   /* If there are active savepoints and any of them were created
40923   ** since the most recent journal header was written, update the
40924   ** PagerSavepoint.iHdrOffset fields now.
40925   */
40926   for(ii=0; ii<pPager->nSavepoint; ii++){
40927     if( pPager->aSavepoint[ii].iHdrOffset==0 ){
40928       pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
40929     }
40930   }
40931 
40932   pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
40933 
40934   /*
40935   ** Write the nRec Field - the number of page records that follow this
40936   ** journal header. Normally, zero is written to this value at this time.
40937   ** After the records are added to the journal (and the journal synced,
40938   ** if in full-sync mode), the zero is overwritten with the true number
40939   ** of records (see syncJournal()).
40940   **
40941   ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
40942   ** reading the journal this value tells SQLite to assume that the
40943   ** rest of the journal file contains valid page records. This assumption
40944   ** is dangerous, as if a failure occurred whilst writing to the journal
40945   ** file it may contain some garbage data. There are two scenarios
40946   ** where this risk can be ignored:
40947   **
40948   **   * When the pager is in no-sync mode. Corruption can follow a
40949   **     power failure in this case anyway.
40950   **
40951   **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
40952   **     that garbage data is never appended to the journal file.
40953   */
40954   assert( isOpen(pPager->fd) || pPager->noSync );
40955   if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
40956    || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
40957   ){
40958     memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
40959     put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
40960   }else{
40961     memset(zHeader, 0, sizeof(aJournalMagic)+4);
40962   }
40963 
40964   /* The random check-hash initializer */
40965   sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
40966   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
40967   /* The initial database size */
40968   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
40969   /* The assumed sector size for this process */
40970   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
40971 
40972   /* The page size */
40973   put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
40974 
40975   /* Initializing the tail of the buffer is not necessary.  Everything
40976   ** works find if the following memset() is omitted.  But initializing
40977   ** the memory prevents valgrind from complaining, so we are willing to
40978   ** take the performance hit.
40979   */
40980   memset(&zHeader[sizeof(aJournalMagic)+20], 0,
40981          nHeader-(sizeof(aJournalMagic)+20));
40982 
40983   /* In theory, it is only necessary to write the 28 bytes that the
40984   ** journal header consumes to the journal file here. Then increment the
40985   ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
40986   ** record is written to the following sector (leaving a gap in the file
40987   ** that will be implicitly filled in by the OS).
40988   **
40989   ** However it has been discovered that on some systems this pattern can
40990   ** be significantly slower than contiguously writing data to the file,
40991   ** even if that means explicitly writing data to the block of
40992   ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
40993   ** is done.
40994   **
40995   ** The loop is required here in case the sector-size is larger than the
40996   ** database page size. Since the zHeader buffer is only Pager.pageSize
40997   ** bytes in size, more than one call to sqlite3OsWrite() may be required
40998   ** to populate the entire journal header sector.
40999   */
41000   for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
41001     IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
41002     rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
41003     assert( pPager->journalHdr <= pPager->journalOff );
41004     pPager->journalOff += nHeader;
41005   }
41006 
41007   return rc;
41008 }
41009 
41010 /*
41011 ** The journal file must be open when this is called. A journal header file
41012 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
41013 ** file. The current location in the journal file is given by
41014 ** pPager->journalOff. See comments above function writeJournalHdr() for
41015 ** a description of the journal header format.
41016 **
41017 ** If the header is read successfully, *pNRec is set to the number of
41018 ** page records following this header and *pDbSize is set to the size of the
41019 ** database before the transaction began, in pages. Also, pPager->cksumInit
41020 ** is set to the value read from the journal header. SQLITE_OK is returned
41021 ** in this case.
41022 **
41023 ** If the journal header file appears to be corrupted, SQLITE_DONE is
41024 ** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
41025 ** cannot be read from the journal file an error code is returned.
41026 */
41027 static int readJournalHdr(
41028   Pager *pPager,               /* Pager object */
41029   int isHot,
41030   i64 journalSize,             /* Size of the open journal file in bytes */
41031   u32 *pNRec,                  /* OUT: Value read from the nRec field */
41032   u32 *pDbSize                 /* OUT: Value of original database size field */
41033 ){
41034   int rc;                      /* Return code */
41035   unsigned char aMagic[8];     /* A buffer to hold the magic header */
41036   i64 iHdrOff;                 /* Offset of journal header being read */
41037 
41038   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
41039 
41040   /* Advance Pager.journalOff to the start of the next sector. If the
41041   ** journal file is too small for there to be a header stored at this
41042   ** point, return SQLITE_DONE.
41043   */
41044   pPager->journalOff = journalHdrOffset(pPager);
41045   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
41046     return SQLITE_DONE;
41047   }
41048   iHdrOff = pPager->journalOff;
41049 
41050   /* Read in the first 8 bytes of the journal header. If they do not match
41051   ** the  magic string found at the start of each journal header, return
41052   ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
41053   ** proceed.
41054   */
41055   if( isHot || iHdrOff!=pPager->journalHdr ){
41056     rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
41057     if( rc ){
41058       return rc;
41059     }
41060     if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
41061       return SQLITE_DONE;
41062     }
41063   }
41064 
41065   /* Read the first three 32-bit fields of the journal header: The nRec
41066   ** field, the checksum-initializer and the database size at the start
41067   ** of the transaction. Return an error code if anything goes wrong.
41068   */
41069   if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
41070    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
41071    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
41072   ){
41073     return rc;
41074   }
41075 
41076   if( pPager->journalOff==0 ){
41077     u32 iPageSize;               /* Page-size field of journal header */
41078     u32 iSectorSize;             /* Sector-size field of journal header */
41079 
41080     /* Read the page-size and sector-size journal header fields. */
41081     if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
41082      || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
41083     ){
41084       return rc;
41085     }
41086 
41087     /* Versions of SQLite prior to 3.5.8 set the page-size field of the
41088     ** journal header to zero. In this case, assume that the Pager.pageSize
41089     ** variable is already set to the correct page size.
41090     */
41091     if( iPageSize==0 ){
41092       iPageSize = pPager->pageSize;
41093     }
41094 
41095     /* Check that the values read from the page-size and sector-size fields
41096     ** are within range. To be 'in range', both values need to be a power
41097     ** of two greater than or equal to 512 or 32, and not greater than their
41098     ** respective compile time maximum limits.
41099     */
41100     if( iPageSize<512                  || iSectorSize<32
41101      || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
41102      || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0
41103     ){
41104       /* If the either the page-size or sector-size in the journal-header is
41105       ** invalid, then the process that wrote the journal-header must have
41106       ** crashed before the header was synced. In this case stop reading
41107       ** the journal file here.
41108       */
41109       return SQLITE_DONE;
41110     }
41111 
41112     /* Update the page-size to match the value read from the journal.
41113     ** Use a testcase() macro to make sure that malloc failure within
41114     ** PagerSetPagesize() is tested.
41115     */
41116     rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
41117     testcase( rc!=SQLITE_OK );
41118 
41119     /* Update the assumed sector-size to match the value used by
41120     ** the process that created this journal. If this journal was
41121     ** created by a process other than this one, then this routine
41122     ** is being called from within pager_playback(). The local value
41123     ** of Pager.sectorSize is restored at the end of that routine.
41124     */
41125     pPager->sectorSize = iSectorSize;
41126   }
41127 
41128   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
41129   return rc;
41130 }
41131 
41132 
41133 /*
41134 ** Write the supplied master journal name into the journal file for pager
41135 ** pPager at the current location. The master journal name must be the last
41136 ** thing written to a journal file. If the pager is in full-sync mode, the
41137 ** journal file descriptor is advanced to the next sector boundary before
41138 ** anything is written. The format is:
41139 **
41140 **   + 4 bytes: PAGER_MJ_PGNO.
41141 **   + N bytes: Master journal filename in utf-8.
41142 **   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
41143 **   + 4 bytes: Master journal name checksum.
41144 **   + 8 bytes: aJournalMagic[].
41145 **
41146 ** The master journal page checksum is the sum of the bytes in the master
41147 ** journal name, where each byte is interpreted as a signed 8-bit integer.
41148 **
41149 ** If zMaster is a NULL pointer (occurs for a single database transaction),
41150 ** this call is a no-op.
41151 */
41152 static int writeMasterJournal(Pager *pPager, const char *zMaster){
41153   int rc;                          /* Return code */
41154   int nMaster;                     /* Length of string zMaster */
41155   i64 iHdrOff;                     /* Offset of header in journal file */
41156   i64 jrnlSize;                    /* Size of journal file on disk */
41157   u32 cksum = 0;                   /* Checksum of string zMaster */
41158 
41159   assert( pPager->setMaster==0 );
41160   assert( !pagerUseWal(pPager) );
41161 
41162   if( !zMaster
41163    || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
41164    || pPager->journalMode==PAGER_JOURNALMODE_OFF
41165   ){
41166     return SQLITE_OK;
41167   }
41168   pPager->setMaster = 1;
41169   assert( isOpen(pPager->jfd) );
41170   assert( pPager->journalHdr <= pPager->journalOff );
41171 
41172   /* Calculate the length in bytes and the checksum of zMaster */
41173   for(nMaster=0; zMaster[nMaster]; nMaster++){
41174     cksum += zMaster[nMaster];
41175   }
41176 
41177   /* If in full-sync mode, advance to the next disk sector before writing
41178   ** the master journal name. This is in case the previous page written to
41179   ** the journal has already been synced.
41180   */
41181   if( pPager->fullSync ){
41182     pPager->journalOff = journalHdrOffset(pPager);
41183   }
41184   iHdrOff = pPager->journalOff;
41185 
41186   /* Write the master journal data to the end of the journal file. If
41187   ** an error occurs, return the error code to the caller.
41188   */
41189   if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
41190    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
41191    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
41192    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
41193    || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
41194   ){
41195     return rc;
41196   }
41197   pPager->journalOff += (nMaster+20);
41198 
41199   /* If the pager is in peristent-journal mode, then the physical
41200   ** journal-file may extend past the end of the master-journal name
41201   ** and 8 bytes of magic data just written to the file. This is
41202   ** dangerous because the code to rollback a hot-journal file
41203   ** will not be able to find the master-journal name to determine
41204   ** whether or not the journal is hot.
41205   **
41206   ** Easiest thing to do in this scenario is to truncate the journal
41207   ** file to the required size.
41208   */
41209   if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
41210    && jrnlSize>pPager->journalOff
41211   ){
41212     rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
41213   }
41214   return rc;
41215 }
41216 
41217 /*
41218 ** Find a page in the hash table given its page number. Return
41219 ** a pointer to the page or NULL if the requested page is not
41220 ** already in memory.
41221 */
41222 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
41223   PgHdr *p = 0;                     /* Return value */
41224 
41225   /* It is not possible for a call to PcacheFetch() with createFlag==0 to
41226   ** fail, since no attempt to allocate dynamic memory will be made.
41227   */
41228   (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
41229   return p;
41230 }
41231 
41232 /*
41233 ** Discard the entire contents of the in-memory page-cache.
41234 */
41235 static void pager_reset(Pager *pPager){
41236   sqlite3BackupRestart(pPager->pBackup);
41237   sqlite3PcacheClear(pPager->pPCache);
41238 }
41239 
41240 /*
41241 ** Free all structures in the Pager.aSavepoint[] array and set both
41242 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
41243 ** if it is open and the pager is not in exclusive mode.
41244 */
41245 static void releaseAllSavepoints(Pager *pPager){
41246   int ii;               /* Iterator for looping through Pager.aSavepoint */
41247   for(ii=0; ii<pPager->nSavepoint; ii++){
41248     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
41249   }
41250   if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
41251     sqlite3OsClose(pPager->sjfd);
41252   }
41253   sqlite3_free(pPager->aSavepoint);
41254   pPager->aSavepoint = 0;
41255   pPager->nSavepoint = 0;
41256   pPager->nSubRec = 0;
41257 }
41258 
41259 /*
41260 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint
41261 ** bitvecs of all open savepoints. Return SQLITE_OK if successful
41262 ** or SQLITE_NOMEM if a malloc failure occurs.
41263 */
41264 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
41265   int ii;                   /* Loop counter */
41266   int rc = SQLITE_OK;       /* Result code */
41267 
41268   for(ii=0; ii<pPager->nSavepoint; ii++){
41269     PagerSavepoint *p = &pPager->aSavepoint[ii];
41270     if( pgno<=p->nOrig ){
41271       rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
41272       testcase( rc==SQLITE_NOMEM );
41273       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
41274     }
41275   }
41276   return rc;
41277 }
41278 
41279 /*
41280 ** This function is a no-op if the pager is in exclusive mode and not
41281 ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
41282 ** state.
41283 **
41284 ** If the pager is not in exclusive-access mode, the database file is
41285 ** completely unlocked. If the file is unlocked and the file-system does
41286 ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
41287 ** closed (if it is open).
41288 **
41289 ** If the pager is in ERROR state when this function is called, the
41290 ** contents of the pager cache are discarded before switching back to
41291 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
41292 ** or not, any journal file left in the file-system will be treated
41293 ** as a hot-journal and rolled back the next time a read-transaction
41294 ** is opened (by this or by any other connection).
41295 */
41296 static void pager_unlock(Pager *pPager){
41297 
41298   assert( pPager->eState==PAGER_READER
41299        || pPager->eState==PAGER_OPEN
41300        || pPager->eState==PAGER_ERROR
41301   );
41302 
41303   sqlite3BitvecDestroy(pPager->pInJournal);
41304   pPager->pInJournal = 0;
41305   releaseAllSavepoints(pPager);
41306 
41307   if( pagerUseWal(pPager) ){
41308     assert( !isOpen(pPager->jfd) );
41309     sqlite3WalEndReadTransaction(pPager->pWal);
41310     pPager->eState = PAGER_OPEN;
41311   }else if( !pPager->exclusiveMode ){
41312     int rc;                       /* Error code returned by pagerUnlockDb() */
41313     int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
41314 
41315     /* If the operating system support deletion of open files, then
41316     ** close the journal file when dropping the database lock.  Otherwise
41317     ** another connection with journal_mode=delete might delete the file
41318     ** out from under us.
41319     */
41320     assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
41321     assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
41322     assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
41323     assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
41324     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
41325     assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
41326     if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
41327      || 1!=(pPager->journalMode & 5)
41328     ){
41329       sqlite3OsClose(pPager->jfd);
41330     }
41331 
41332     /* If the pager is in the ERROR state and the call to unlock the database
41333     ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
41334     ** above the #define for UNKNOWN_LOCK for an explanation of why this
41335     ** is necessary.
41336     */
41337     rc = pagerUnlockDb(pPager, NO_LOCK);
41338     if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
41339       pPager->eLock = UNKNOWN_LOCK;
41340     }
41341 
41342     /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
41343     ** without clearing the error code. This is intentional - the error
41344     ** code is cleared and the cache reset in the block below.
41345     */
41346     assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
41347     pPager->changeCountDone = 0;
41348     pPager->eState = PAGER_OPEN;
41349   }
41350 
41351   /* If Pager.errCode is set, the contents of the pager cache cannot be
41352   ** trusted. Now that there are no outstanding references to the pager,
41353   ** it can safely move back to PAGER_OPEN state. This happens in both
41354   ** normal and exclusive-locking mode.
41355   */
41356   if( pPager->errCode ){
41357     assert( !MEMDB );
41358     pager_reset(pPager);
41359     pPager->changeCountDone = pPager->tempFile;
41360     pPager->eState = PAGER_OPEN;
41361     pPager->errCode = SQLITE_OK;
41362     if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
41363   }
41364 
41365   pPager->journalOff = 0;
41366   pPager->journalHdr = 0;
41367   pPager->setMaster = 0;
41368 }
41369 
41370 /*
41371 ** This function is called whenever an IOERR or FULL error that requires
41372 ** the pager to transition into the ERROR state may ahve occurred.
41373 ** The first argument is a pointer to the pager structure, the second
41374 ** the error-code about to be returned by a pager API function. The
41375 ** value returned is a copy of the second argument to this function.
41376 **
41377 ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
41378 ** IOERR sub-codes, the pager enters the ERROR state and the error code
41379 ** is stored in Pager.errCode. While the pager remains in the ERROR state,
41380 ** all major API calls on the Pager will immediately return Pager.errCode.
41381 **
41382 ** The ERROR state indicates that the contents of the pager-cache
41383 ** cannot be trusted. This state can be cleared by completely discarding
41384 ** the contents of the pager-cache. If a transaction was active when
41385 ** the persistent error occurred, then the rollback journal may need
41386 ** to be replayed to restore the contents of the database file (as if
41387 ** it were a hot-journal).
41388 */
41389 static int pager_error(Pager *pPager, int rc){
41390   int rc2 = rc & 0xff;
41391   assert( rc==SQLITE_OK || !MEMDB );
41392   assert(
41393        pPager->errCode==SQLITE_FULL ||
41394        pPager->errCode==SQLITE_OK ||
41395        (pPager->errCode & 0xff)==SQLITE_IOERR
41396   );
41397   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
41398     pPager->errCode = rc;
41399     pPager->eState = PAGER_ERROR;
41400   }
41401   return rc;
41402 }
41403 
41404 static int pager_truncate(Pager *pPager, Pgno nPage);
41405 
41406 /*
41407 ** This routine ends a transaction. A transaction is usually ended by
41408 ** either a COMMIT or a ROLLBACK operation. This routine may be called
41409 ** after rollback of a hot-journal, or if an error occurs while opening
41410 ** the journal file or writing the very first journal-header of a
41411 ** database transaction.
41412 **
41413 ** This routine is never called in PAGER_ERROR state. If it is called
41414 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
41415 ** exclusive than a RESERVED lock, it is a no-op.
41416 **
41417 ** Otherwise, any active savepoints are released.
41418 **
41419 ** If the journal file is open, then it is "finalized". Once a journal
41420 ** file has been finalized it is not possible to use it to roll back a
41421 ** transaction. Nor will it be considered to be a hot-journal by this
41422 ** or any other database connection. Exactly how a journal is finalized
41423 ** depends on whether or not the pager is running in exclusive mode and
41424 ** the current journal-mode (Pager.journalMode value), as follows:
41425 **
41426 **   journalMode==MEMORY
41427 **     Journal file descriptor is simply closed. This destroys an
41428 **     in-memory journal.
41429 **
41430 **   journalMode==TRUNCATE
41431 **     Journal file is truncated to zero bytes in size.
41432 **
41433 **   journalMode==PERSIST
41434 **     The first 28 bytes of the journal file are zeroed. This invalidates
41435 **     the first journal header in the file, and hence the entire journal
41436 **     file. An invalid journal file cannot be rolled back.
41437 **
41438 **   journalMode==DELETE
41439 **     The journal file is closed and deleted using sqlite3OsDelete().
41440 **
41441 **     If the pager is running in exclusive mode, this method of finalizing
41442 **     the journal file is never used. Instead, if the journalMode is
41443 **     DELETE and the pager is in exclusive mode, the method described under
41444 **     journalMode==PERSIST is used instead.
41445 **
41446 ** After the journal is finalized, the pager moves to PAGER_READER state.
41447 ** If running in non-exclusive rollback mode, the lock on the file is
41448 ** downgraded to a SHARED_LOCK.
41449 **
41450 ** SQLITE_OK is returned if no error occurs. If an error occurs during
41451 ** any of the IO operations to finalize the journal file or unlock the
41452 ** database then the IO error code is returned to the user. If the
41453 ** operation to finalize the journal file fails, then the code still
41454 ** tries to unlock the database file if not in exclusive mode. If the
41455 ** unlock operation fails as well, then the first error code related
41456 ** to the first error encountered (the journal finalization one) is
41457 ** returned.
41458 */
41459 static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
41460   int rc = SQLITE_OK;      /* Error code from journal finalization operation */
41461   int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
41462 
41463   /* Do nothing if the pager does not have an open write transaction
41464   ** or at least a RESERVED lock. This function may be called when there
41465   ** is no write-transaction active but a RESERVED or greater lock is
41466   ** held under two circumstances:
41467   **
41468   **   1. After a successful hot-journal rollback, it is called with
41469   **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
41470   **
41471   **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
41472   **      lock switches back to locking_mode=normal and then executes a
41473   **      read-transaction, this function is called with eState==PAGER_READER
41474   **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
41475   */
41476   assert( assert_pager_state(pPager) );
41477   assert( pPager->eState!=PAGER_ERROR );
41478   if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
41479     return SQLITE_OK;
41480   }
41481 
41482   releaseAllSavepoints(pPager);
41483   assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
41484   if( isOpen(pPager->jfd) ){
41485     assert( !pagerUseWal(pPager) );
41486 
41487     /* Finalize the journal file. */
41488     if( sqlite3IsMemJournal(pPager->jfd) ){
41489       assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
41490       sqlite3OsClose(pPager->jfd);
41491     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
41492       if( pPager->journalOff==0 ){
41493         rc = SQLITE_OK;
41494       }else{
41495         rc = sqlite3OsTruncate(pPager->jfd, 0);
41496       }
41497       pPager->journalOff = 0;
41498     }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
41499       || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
41500     ){
41501       rc = zeroJournalHdr(pPager, hasMaster);
41502       pPager->journalOff = 0;
41503     }else{
41504       /* This branch may be executed with Pager.journalMode==MEMORY if
41505       ** a hot-journal was just rolled back. In this case the journal
41506       ** file should be closed and deleted. If this connection writes to
41507       ** the database file, it will do so using an in-memory journal.
41508       */
41509       int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
41510       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
41511            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
41512            || pPager->journalMode==PAGER_JOURNALMODE_WAL
41513       );
41514       sqlite3OsClose(pPager->jfd);
41515       if( bDelete ){
41516         rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
41517       }
41518     }
41519   }
41520 
41521 #ifdef SQLITE_CHECK_PAGES
41522   sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
41523   if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
41524     PgHdr *p = pager_lookup(pPager, 1);
41525     if( p ){
41526       p->pageHash = 0;
41527       sqlite3PagerUnrefNotNull(p);
41528     }
41529   }
41530 #endif
41531 
41532   sqlite3BitvecDestroy(pPager->pInJournal);
41533   pPager->pInJournal = 0;
41534   pPager->nRec = 0;
41535   sqlite3PcacheCleanAll(pPager->pPCache);
41536   sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
41537 
41538   if( pagerUseWal(pPager) ){
41539     /* Drop the WAL write-lock, if any. Also, if the connection was in
41540     ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
41541     ** lock held on the database file.
41542     */
41543     rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
41544     assert( rc2==SQLITE_OK );
41545   }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
41546     /* This branch is taken when committing a transaction in rollback-journal
41547     ** mode if the database file on disk is larger than the database image.
41548     ** At this point the journal has been finalized and the transaction
41549     ** successfully committed, but the EXCLUSIVE lock is still held on the
41550     ** file. So it is safe to truncate the database file to its minimum
41551     ** required size.  */
41552     assert( pPager->eLock==EXCLUSIVE_LOCK );
41553     rc = pager_truncate(pPager, pPager->dbSize);
41554   }
41555 
41556   if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
41557     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
41558     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
41559   }
41560 
41561   if( !pPager->exclusiveMode
41562    && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
41563   ){
41564     rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
41565     pPager->changeCountDone = 0;
41566   }
41567   pPager->eState = PAGER_READER;
41568   pPager->setMaster = 0;
41569 
41570   return (rc==SQLITE_OK?rc2:rc);
41571 }
41572 
41573 /*
41574 ** Execute a rollback if a transaction is active and unlock the
41575 ** database file.
41576 **
41577 ** If the pager has already entered the ERROR state, do not attempt
41578 ** the rollback at this time. Instead, pager_unlock() is called. The
41579 ** call to pager_unlock() will discard all in-memory pages, unlock
41580 ** the database file and move the pager back to OPEN state. If this
41581 ** means that there is a hot-journal left in the file-system, the next
41582 ** connection to obtain a shared lock on the pager (which may be this one)
41583 ** will roll it back.
41584 **
41585 ** If the pager has not already entered the ERROR state, but an IO or
41586 ** malloc error occurs during a rollback, then this will itself cause
41587 ** the pager to enter the ERROR state. Which will be cleared by the
41588 ** call to pager_unlock(), as described above.
41589 */
41590 static void pagerUnlockAndRollback(Pager *pPager){
41591   if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
41592     assert( assert_pager_state(pPager) );
41593     if( pPager->eState>=PAGER_WRITER_LOCKED ){
41594       sqlite3BeginBenignMalloc();
41595       sqlite3PagerRollback(pPager);
41596       sqlite3EndBenignMalloc();
41597     }else if( !pPager->exclusiveMode ){
41598       assert( pPager->eState==PAGER_READER );
41599       pager_end_transaction(pPager, 0, 0);
41600     }
41601   }
41602   pager_unlock(pPager);
41603 }
41604 
41605 /*
41606 ** Parameter aData must point to a buffer of pPager->pageSize bytes
41607 ** of data. Compute and return a checksum based ont the contents of the
41608 ** page of data and the current value of pPager->cksumInit.
41609 **
41610 ** This is not a real checksum. It is really just the sum of the
41611 ** random initial value (pPager->cksumInit) and every 200th byte
41612 ** of the page data, starting with byte offset (pPager->pageSize%200).
41613 ** Each byte is interpreted as an 8-bit unsigned integer.
41614 **
41615 ** Changing the formula used to compute this checksum results in an
41616 ** incompatible journal file format.
41617 **
41618 ** If journal corruption occurs due to a power failure, the most likely
41619 ** scenario is that one end or the other of the record will be changed.
41620 ** It is much less likely that the two ends of the journal record will be
41621 ** correct and the middle be corrupt.  Thus, this "checksum" scheme,
41622 ** though fast and simple, catches the mostly likely kind of corruption.
41623 */
41624 static u32 pager_cksum(Pager *pPager, const u8 *aData){
41625   u32 cksum = pPager->cksumInit;         /* Checksum value to return */
41626   int i = pPager->pageSize-200;          /* Loop counter */
41627   while( i>0 ){
41628     cksum += aData[i];
41629     i -= 200;
41630   }
41631   return cksum;
41632 }
41633 
41634 /*
41635 ** Report the current page size and number of reserved bytes back
41636 ** to the codec.
41637 */
41638 #ifdef SQLITE_HAS_CODEC
41639 static void pagerReportSize(Pager *pPager){
41640   if( pPager->xCodecSizeChng ){
41641     pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
41642                            (int)pPager->nReserve);
41643   }
41644 }
41645 #else
41646 # define pagerReportSize(X)     /* No-op if we do not support a codec */
41647 #endif
41648 
41649 /*
41650 ** Read a single page from either the journal file (if isMainJrnl==1) or
41651 ** from the sub-journal (if isMainJrnl==0) and playback that page.
41652 ** The page begins at offset *pOffset into the file. The *pOffset
41653 ** value is increased to the start of the next page in the journal.
41654 **
41655 ** The main rollback journal uses checksums - the statement journal does
41656 ** not.
41657 **
41658 ** If the page number of the page record read from the (sub-)journal file
41659 ** is greater than the current value of Pager.dbSize, then playback is
41660 ** skipped and SQLITE_OK is returned.
41661 **
41662 ** If pDone is not NULL, then it is a record of pages that have already
41663 ** been played back.  If the page at *pOffset has already been played back
41664 ** (if the corresponding pDone bit is set) then skip the playback.
41665 ** Make sure the pDone bit corresponding to the *pOffset page is set
41666 ** prior to returning.
41667 **
41668 ** If the page record is successfully read from the (sub-)journal file
41669 ** and played back, then SQLITE_OK is returned. If an IO error occurs
41670 ** while reading the record from the (sub-)journal file or while writing
41671 ** to the database file, then the IO error code is returned. If data
41672 ** is successfully read from the (sub-)journal file but appears to be
41673 ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
41674 ** two circumstances:
41675 **
41676 **   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
41677 **   * If the record is being rolled back from the main journal file
41678 **     and the checksum field does not match the record content.
41679 **
41680 ** Neither of these two scenarios are possible during a savepoint rollback.
41681 **
41682 ** If this is a savepoint rollback, then memory may have to be dynamically
41683 ** allocated by this function. If this is the case and an allocation fails,
41684 ** SQLITE_NOMEM is returned.
41685 */
41686 static int pager_playback_one_page(
41687   Pager *pPager,                /* The pager being played back */
41688   i64 *pOffset,                 /* Offset of record to playback */
41689   Bitvec *pDone,                /* Bitvec of pages already played back */
41690   int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
41691   int isSavepnt                 /* True for a savepoint rollback */
41692 ){
41693   int rc;
41694   PgHdr *pPg;                   /* An existing page in the cache */
41695   Pgno pgno;                    /* The page number of a page in journal */
41696   u32 cksum;                    /* Checksum used for sanity checking */
41697   char *aData;                  /* Temporary storage for the page */
41698   sqlite3_file *jfd;            /* The file descriptor for the journal file */
41699   int isSynced;                 /* True if journal page is synced */
41700 
41701   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
41702   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
41703   assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
41704   assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
41705 
41706   aData = pPager->pTmpSpace;
41707   assert( aData );         /* Temp storage must have already been allocated */
41708   assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
41709 
41710   /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
41711   ** or savepoint rollback done at the request of the caller) or this is
41712   ** a hot-journal rollback. If it is a hot-journal rollback, the pager
41713   ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
41714   ** only reads from the main journal, not the sub-journal.
41715   */
41716   assert( pPager->eState>=PAGER_WRITER_CACHEMOD
41717        || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
41718   );
41719   assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
41720 
41721   /* Read the page number and page data from the journal or sub-journal
41722   ** file. Return an error code to the caller if an IO error occurs.
41723   */
41724   jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
41725   rc = read32bits(jfd, *pOffset, &pgno);
41726   if( rc!=SQLITE_OK ) return rc;
41727   rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
41728   if( rc!=SQLITE_OK ) return rc;
41729   *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
41730 
41731   /* Sanity checking on the page.  This is more important that I originally
41732   ** thought.  If a power failure occurs while the journal is being written,
41733   ** it could cause invalid data to be written into the journal.  We need to
41734   ** detect this invalid data (with high probability) and ignore it.
41735   */
41736   if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
41737     assert( !isSavepnt );
41738     return SQLITE_DONE;
41739   }
41740   if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
41741     return SQLITE_OK;
41742   }
41743   if( isMainJrnl ){
41744     rc = read32bits(jfd, (*pOffset)-4, &cksum);
41745     if( rc ) return rc;
41746     if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
41747       return SQLITE_DONE;
41748     }
41749   }
41750 
41751   /* If this page has already been played by before during the current
41752   ** rollback, then don't bother to play it back again.
41753   */
41754   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
41755     return rc;
41756   }
41757 
41758   /* When playing back page 1, restore the nReserve setting
41759   */
41760   if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
41761     pPager->nReserve = ((u8*)aData)[20];
41762     pagerReportSize(pPager);
41763   }
41764 
41765   /* If the pager is in CACHEMOD state, then there must be a copy of this
41766   ** page in the pager cache. In this case just update the pager cache,
41767   ** not the database file. The page is left marked dirty in this case.
41768   **
41769   ** An exception to the above rule: If the database is in no-sync mode
41770   ** and a page is moved during an incremental vacuum then the page may
41771   ** not be in the pager cache. Later: if a malloc() or IO error occurs
41772   ** during a Movepage() call, then the page may not be in the cache
41773   ** either. So the condition described in the above paragraph is not
41774   ** assert()able.
41775   **
41776   ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
41777   ** pager cache if it exists and the main file. The page is then marked
41778   ** not dirty. Since this code is only executed in PAGER_OPEN state for
41779   ** a hot-journal rollback, it is guaranteed that the page-cache is empty
41780   ** if the pager is in OPEN state.
41781   **
41782   ** Ticket #1171:  The statement journal might contain page content that is
41783   ** different from the page content at the start of the transaction.
41784   ** This occurs when a page is changed prior to the start of a statement
41785   ** then changed again within the statement.  When rolling back such a
41786   ** statement we must not write to the original database unless we know
41787   ** for certain that original page contents are synced into the main rollback
41788   ** journal.  Otherwise, a power loss might leave modified data in the
41789   ** database file without an entry in the rollback journal that can
41790   ** restore the database to its original form.  Two conditions must be
41791   ** met before writing to the database files. (1) the database must be
41792   ** locked.  (2) we know that the original page content is fully synced
41793   ** in the main journal either because the page is not in cache or else
41794   ** the page is marked as needSync==0.
41795   **
41796   ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
41797   ** is possible to fail a statement on a database that does not yet exist.
41798   ** Do not attempt to write if database file has never been opened.
41799   */
41800   if( pagerUseWal(pPager) ){
41801     pPg = 0;
41802   }else{
41803     pPg = pager_lookup(pPager, pgno);
41804   }
41805   assert( pPg || !MEMDB );
41806   assert( pPager->eState!=PAGER_OPEN || pPg==0 );
41807   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
41808            PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
41809            (isMainJrnl?"main-journal":"sub-journal")
41810   ));
41811   if( isMainJrnl ){
41812     isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
41813   }else{
41814     isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
41815   }
41816   if( isOpen(pPager->fd)
41817    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
41818    && isSynced
41819   ){
41820     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
41821     testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
41822     assert( !pagerUseWal(pPager) );
41823     rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
41824     if( pgno>pPager->dbFileSize ){
41825       pPager->dbFileSize = pgno;
41826     }
41827     if( pPager->pBackup ){
41828       CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
41829       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
41830       CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
41831     }
41832   }else if( !isMainJrnl && pPg==0 ){
41833     /* If this is a rollback of a savepoint and data was not written to
41834     ** the database and the page is not in-memory, there is a potential
41835     ** problem. When the page is next fetched by the b-tree layer, it
41836     ** will be read from the database file, which may or may not be
41837     ** current.
41838     **
41839     ** There are a couple of different ways this can happen. All are quite
41840     ** obscure. When running in synchronous mode, this can only happen
41841     ** if the page is on the free-list at the start of the transaction, then
41842     ** populated, then moved using sqlite3PagerMovepage().
41843     **
41844     ** The solution is to add an in-memory page to the cache containing
41845     ** the data just read from the sub-journal. Mark the page as dirty
41846     ** and if the pager requires a journal-sync, then mark the page as
41847     ** requiring a journal-sync before it is written.
41848     */
41849     assert( isSavepnt );
41850     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
41851     pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
41852     rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
41853     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
41854     pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
41855     if( rc!=SQLITE_OK ) return rc;
41856     pPg->flags &= ~PGHDR_NEED_READ;
41857     sqlite3PcacheMakeDirty(pPg);
41858   }
41859   if( pPg ){
41860     /* No page should ever be explicitly rolled back that is in use, except
41861     ** for page 1 which is held in use in order to keep the lock on the
41862     ** database active. However such a page may be rolled back as a result
41863     ** of an internal error resulting in an automatic call to
41864     ** sqlite3PagerRollback().
41865     */
41866     void *pData;
41867     pData = pPg->pData;
41868     memcpy(pData, (u8*)aData, pPager->pageSize);
41869     pPager->xReiniter(pPg);
41870     if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
41871       /* If the contents of this page were just restored from the main
41872       ** journal file, then its content must be as they were when the
41873       ** transaction was first opened. In this case we can mark the page
41874       ** as clean, since there will be no need to write it out to the
41875       ** database.
41876       **
41877       ** There is one exception to this rule. If the page is being rolled
41878       ** back as part of a savepoint (or statement) rollback from an
41879       ** unsynced portion of the main journal file, then it is not safe
41880       ** to mark the page as clean. This is because marking the page as
41881       ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
41882       ** already in the journal file (recorded in Pager.pInJournal) and
41883       ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
41884       ** again within this transaction, it will be marked as dirty but
41885       ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
41886       ** be written out into the database file before its journal file
41887       ** segment is synced. If a crash occurs during or following this,
41888       ** database corruption may ensue.
41889       */
41890       assert( !pagerUseWal(pPager) );
41891       sqlite3PcacheMakeClean(pPg);
41892     }
41893     pager_set_pagehash(pPg);
41894 
41895     /* If this was page 1, then restore the value of Pager.dbFileVers.
41896     ** Do this before any decoding. */
41897     if( pgno==1 ){
41898       memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
41899     }
41900 
41901     /* Decode the page just read from disk */
41902     CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
41903     sqlite3PcacheRelease(pPg);
41904   }
41905   return rc;
41906 }
41907 
41908 /*
41909 ** Parameter zMaster is the name of a master journal file. A single journal
41910 ** file that referred to the master journal file has just been rolled back.
41911 ** This routine checks if it is possible to delete the master journal file,
41912 ** and does so if it is.
41913 **
41914 ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
41915 ** available for use within this function.
41916 **
41917 ** When a master journal file is created, it is populated with the names
41918 ** of all of its child journals, one after another, formatted as utf-8
41919 ** encoded text. The end of each child journal file is marked with a
41920 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
41921 ** file for a transaction involving two databases might be:
41922 **
41923 **   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
41924 **
41925 ** A master journal file may only be deleted once all of its child
41926 ** journals have been rolled back.
41927 **
41928 ** This function reads the contents of the master-journal file into
41929 ** memory and loops through each of the child journal names. For
41930 ** each child journal, it checks if:
41931 **
41932 **   * if the child journal exists, and if so
41933 **   * if the child journal contains a reference to master journal
41934 **     file zMaster
41935 **
41936 ** If a child journal can be found that matches both of the criteria
41937 ** above, this function returns without doing anything. Otherwise, if
41938 ** no such child journal can be found, file zMaster is deleted from
41939 ** the file-system using sqlite3OsDelete().
41940 **
41941 ** If an IO error within this function, an error code is returned. This
41942 ** function allocates memory by calling sqlite3Malloc(). If an allocation
41943 ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
41944 ** occur, SQLITE_OK is returned.
41945 **
41946 ** TODO: This function allocates a single block of memory to load
41947 ** the entire contents of the master journal file. This could be
41948 ** a couple of kilobytes or so - potentially larger than the page
41949 ** size.
41950 */
41951 static int pager_delmaster(Pager *pPager, const char *zMaster){
41952   sqlite3_vfs *pVfs = pPager->pVfs;
41953   int rc;                   /* Return code */
41954   sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
41955   sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
41956   char *zMasterJournal = 0; /* Contents of master journal file */
41957   i64 nMasterJournal;       /* Size of master journal file */
41958   char *zJournal;           /* Pointer to one journal within MJ file */
41959   char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
41960   int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
41961 
41962   /* Allocate space for both the pJournal and pMaster file descriptors.
41963   ** If successful, open the master journal file for reading.
41964   */
41965   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
41966   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
41967   if( !pMaster ){
41968     rc = SQLITE_NOMEM;
41969   }else{
41970     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
41971     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
41972   }
41973   if( rc!=SQLITE_OK ) goto delmaster_out;
41974 
41975   /* Load the entire master journal file into space obtained from
41976   ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
41977   ** sufficient space (in zMasterPtr) to hold the names of master
41978   ** journal files extracted from regular rollback-journals.
41979   */
41980   rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
41981   if( rc!=SQLITE_OK ) goto delmaster_out;
41982   nMasterPtr = pVfs->mxPathname+1;
41983   zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
41984   if( !zMasterJournal ){
41985     rc = SQLITE_NOMEM;
41986     goto delmaster_out;
41987   }
41988   zMasterPtr = &zMasterJournal[nMasterJournal+1];
41989   rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
41990   if( rc!=SQLITE_OK ) goto delmaster_out;
41991   zMasterJournal[nMasterJournal] = 0;
41992 
41993   zJournal = zMasterJournal;
41994   while( (zJournal-zMasterJournal)<nMasterJournal ){
41995     int exists;
41996     rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
41997     if( rc!=SQLITE_OK ){
41998       goto delmaster_out;
41999     }
42000     if( exists ){
42001       /* One of the journals pointed to by the master journal exists.
42002       ** Open it and check if it points at the master journal. If
42003       ** so, return without deleting the master journal file.
42004       */
42005       int c;
42006       int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
42007       rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
42008       if( rc!=SQLITE_OK ){
42009         goto delmaster_out;
42010       }
42011 
42012       rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
42013       sqlite3OsClose(pJournal);
42014       if( rc!=SQLITE_OK ){
42015         goto delmaster_out;
42016       }
42017 
42018       c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
42019       if( c ){
42020         /* We have a match. Do not delete the master journal file. */
42021         goto delmaster_out;
42022       }
42023     }
42024     zJournal += (sqlite3Strlen30(zJournal)+1);
42025   }
42026 
42027   sqlite3OsClose(pMaster);
42028   rc = sqlite3OsDelete(pVfs, zMaster, 0);
42029 
42030 delmaster_out:
42031   sqlite3_free(zMasterJournal);
42032   if( pMaster ){
42033     sqlite3OsClose(pMaster);
42034     assert( !isOpen(pJournal) );
42035     sqlite3_free(pMaster);
42036   }
42037   return rc;
42038 }
42039 
42040 
42041 /*
42042 ** This function is used to change the actual size of the database
42043 ** file in the file-system. This only happens when committing a transaction,
42044 ** or rolling back a transaction (including rolling back a hot-journal).
42045 **
42046 ** If the main database file is not open, or the pager is not in either
42047 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
42048 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
42049 ** If the file on disk is currently larger than nPage pages, then use the VFS
42050 ** xTruncate() method to truncate it.
42051 **
42052 ** Or, it might might be the case that the file on disk is smaller than
42053 ** nPage pages. Some operating system implementations can get confused if
42054 ** you try to truncate a file to some size that is larger than it
42055 ** currently is, so detect this case and write a single zero byte to
42056 ** the end of the new file instead.
42057 **
42058 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
42059 ** the database file, return the error code to the caller.
42060 */
42061 static int pager_truncate(Pager *pPager, Pgno nPage){
42062   int rc = SQLITE_OK;
42063   assert( pPager->eState!=PAGER_ERROR );
42064   assert( pPager->eState!=PAGER_READER );
42065 
42066   if( isOpen(pPager->fd)
42067    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
42068   ){
42069     i64 currentSize, newSize;
42070     int szPage = pPager->pageSize;
42071     assert( pPager->eLock==EXCLUSIVE_LOCK );
42072     /* TODO: Is it safe to use Pager.dbFileSize here? */
42073     rc = sqlite3OsFileSize(pPager->fd, &currentSize);
42074     newSize = szPage*(i64)nPage;
42075     if( rc==SQLITE_OK && currentSize!=newSize ){
42076       if( currentSize>newSize ){
42077         rc = sqlite3OsTruncate(pPager->fd, newSize);
42078       }else if( (currentSize+szPage)<=newSize ){
42079         char *pTmp = pPager->pTmpSpace;
42080         memset(pTmp, 0, szPage);
42081         testcase( (newSize-szPage) == currentSize );
42082         testcase( (newSize-szPage) >  currentSize );
42083         rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
42084       }
42085       if( rc==SQLITE_OK ){
42086         pPager->dbFileSize = nPage;
42087       }
42088     }
42089   }
42090   return rc;
42091 }
42092 
42093 /*
42094 ** Return a sanitized version of the sector-size of OS file pFile. The
42095 ** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
42096 */
42097 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
42098   int iRet = sqlite3OsSectorSize(pFile);
42099   if( iRet<32 ){
42100     iRet = 512;
42101   }else if( iRet>MAX_SECTOR_SIZE ){
42102     assert( MAX_SECTOR_SIZE>=512 );
42103     iRet = MAX_SECTOR_SIZE;
42104   }
42105   return iRet;
42106 }
42107 
42108 /*
42109 ** Set the value of the Pager.sectorSize variable for the given
42110 ** pager based on the value returned by the xSectorSize method
42111 ** of the open database file. The sector size will be used used
42112 ** to determine the size and alignment of journal header and
42113 ** master journal pointers within created journal files.
42114 **
42115 ** For temporary files the effective sector size is always 512 bytes.
42116 **
42117 ** Otherwise, for non-temporary files, the effective sector size is
42118 ** the value returned by the xSectorSize() method rounded up to 32 if
42119 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
42120 ** is greater than MAX_SECTOR_SIZE.
42121 **
42122 ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
42123 ** the effective sector size to its minimum value (512).  The purpose of
42124 ** pPager->sectorSize is to define the "blast radius" of bytes that
42125 ** might change if a crash occurs while writing to a single byte in
42126 ** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
42127 ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
42128 ** size.  For backwards compatibility of the rollback journal file format,
42129 ** we cannot reduce the effective sector size below 512.
42130 */
42131 static void setSectorSize(Pager *pPager){
42132   assert( isOpen(pPager->fd) || pPager->tempFile );
42133 
42134   if( pPager->tempFile
42135    || (sqlite3OsDeviceCharacteristics(pPager->fd) &
42136               SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
42137   ){
42138     /* Sector size doesn't matter for temporary files. Also, the file
42139     ** may not have been opened yet, in which case the OsSectorSize()
42140     ** call will segfault. */
42141     pPager->sectorSize = 512;
42142   }else{
42143     pPager->sectorSize = sqlite3SectorSize(pPager->fd);
42144   }
42145 }
42146 
42147 /*
42148 ** Playback the journal and thus restore the database file to
42149 ** the state it was in before we started making changes.
42150 **
42151 ** The journal file format is as follows:
42152 **
42153 **  (1)  8 byte prefix.  A copy of aJournalMagic[].
42154 **  (2)  4 byte big-endian integer which is the number of valid page records
42155 **       in the journal.  If this value is 0xffffffff, then compute the
42156 **       number of page records from the journal size.
42157 **  (3)  4 byte big-endian integer which is the initial value for the
42158 **       sanity checksum.
42159 **  (4)  4 byte integer which is the number of pages to truncate the
42160 **       database to during a rollback.
42161 **  (5)  4 byte big-endian integer which is the sector size.  The header
42162 **       is this many bytes in size.
42163 **  (6)  4 byte big-endian integer which is the page size.
42164 **  (7)  zero padding out to the next sector size.
42165 **  (8)  Zero or more pages instances, each as follows:
42166 **        +  4 byte page number.
42167 **        +  pPager->pageSize bytes of data.
42168 **        +  4 byte checksum
42169 **
42170 ** When we speak of the journal header, we mean the first 7 items above.
42171 ** Each entry in the journal is an instance of the 8th item.
42172 **
42173 ** Call the value from the second bullet "nRec".  nRec is the number of
42174 ** valid page entries in the journal.  In most cases, you can compute the
42175 ** value of nRec from the size of the journal file.  But if a power
42176 ** failure occurred while the journal was being written, it could be the
42177 ** case that the size of the journal file had already been increased but
42178 ** the extra entries had not yet made it safely to disk.  In such a case,
42179 ** the value of nRec computed from the file size would be too large.  For
42180 ** that reason, we always use the nRec value in the header.
42181 **
42182 ** If the nRec value is 0xffffffff it means that nRec should be computed
42183 ** from the file size.  This value is used when the user selects the
42184 ** no-sync option for the journal.  A power failure could lead to corruption
42185 ** in this case.  But for things like temporary table (which will be
42186 ** deleted when the power is restored) we don't care.
42187 **
42188 ** If the file opened as the journal file is not a well-formed
42189 ** journal file then all pages up to the first corrupted page are rolled
42190 ** back (or no pages if the journal header is corrupted). The journal file
42191 ** is then deleted and SQLITE_OK returned, just as if no corruption had
42192 ** been encountered.
42193 **
42194 ** If an I/O or malloc() error occurs, the journal-file is not deleted
42195 ** and an error code is returned.
42196 **
42197 ** The isHot parameter indicates that we are trying to rollback a journal
42198 ** that might be a hot journal.  Or, it could be that the journal is
42199 ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
42200 ** If the journal really is hot, reset the pager cache prior rolling
42201 ** back any content.  If the journal is merely persistent, no reset is
42202 ** needed.
42203 */
42204 static int pager_playback(Pager *pPager, int isHot){
42205   sqlite3_vfs *pVfs = pPager->pVfs;
42206   i64 szJ;                 /* Size of the journal file in bytes */
42207   u32 nRec;                /* Number of Records in the journal */
42208   u32 u;                   /* Unsigned loop counter */
42209   Pgno mxPg = 0;           /* Size of the original file in pages */
42210   int rc;                  /* Result code of a subroutine */
42211   int res = 1;             /* Value returned by sqlite3OsAccess() */
42212   char *zMaster = 0;       /* Name of master journal file if any */
42213   int needPagerReset;      /* True to reset page prior to first page rollback */
42214   int nPlayback = 0;       /* Total number of pages restored from journal */
42215 
42216   /* Figure out how many records are in the journal.  Abort early if
42217   ** the journal is empty.
42218   */
42219   assert( isOpen(pPager->jfd) );
42220   rc = sqlite3OsFileSize(pPager->jfd, &szJ);
42221   if( rc!=SQLITE_OK ){
42222     goto end_playback;
42223   }
42224 
42225   /* Read the master journal name from the journal, if it is present.
42226   ** If a master journal file name is specified, but the file is not
42227   ** present on disk, then the journal is not hot and does not need to be
42228   ** played back.
42229   **
42230   ** TODO: Technically the following is an error because it assumes that
42231   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
42232   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
42233   **  mxPathname is 512, which is the same as the minimum allowable value
42234   ** for pageSize.
42235   */
42236   zMaster = pPager->pTmpSpace;
42237   rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
42238   if( rc==SQLITE_OK && zMaster[0] ){
42239     rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
42240   }
42241   zMaster = 0;
42242   if( rc!=SQLITE_OK || !res ){
42243     goto end_playback;
42244   }
42245   pPager->journalOff = 0;
42246   needPagerReset = isHot;
42247 
42248   /* This loop terminates either when a readJournalHdr() or
42249   ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
42250   ** occurs.
42251   */
42252   while( 1 ){
42253     /* Read the next journal header from the journal file.  If there are
42254     ** not enough bytes left in the journal file for a complete header, or
42255     ** it is corrupted, then a process must have failed while writing it.
42256     ** This indicates nothing more needs to be rolled back.
42257     */
42258     rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
42259     if( rc!=SQLITE_OK ){
42260       if( rc==SQLITE_DONE ){
42261         rc = SQLITE_OK;
42262       }
42263       goto end_playback;
42264     }
42265 
42266     /* If nRec is 0xffffffff, then this journal was created by a process
42267     ** working in no-sync mode. This means that the rest of the journal
42268     ** file consists of pages, there are no more journal headers. Compute
42269     ** the value of nRec based on this assumption.
42270     */
42271     if( nRec==0xffffffff ){
42272       assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
42273       nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
42274     }
42275 
42276     /* If nRec is 0 and this rollback is of a transaction created by this
42277     ** process and if this is the final header in the journal, then it means
42278     ** that this part of the journal was being filled but has not yet been
42279     ** synced to disk.  Compute the number of pages based on the remaining
42280     ** size of the file.
42281     **
42282     ** The third term of the test was added to fix ticket #2565.
42283     ** When rolling back a hot journal, nRec==0 always means that the next
42284     ** chunk of the journal contains zero pages to be rolled back.  But
42285     ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
42286     ** the journal, it means that the journal might contain additional
42287     ** pages that need to be rolled back and that the number of pages
42288     ** should be computed based on the journal file size.
42289     */
42290     if( nRec==0 && !isHot &&
42291         pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
42292       nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
42293     }
42294 
42295     /* If this is the first header read from the journal, truncate the
42296     ** database file back to its original size.
42297     */
42298     if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
42299       rc = pager_truncate(pPager, mxPg);
42300       if( rc!=SQLITE_OK ){
42301         goto end_playback;
42302       }
42303       pPager->dbSize = mxPg;
42304     }
42305 
42306     /* Copy original pages out of the journal and back into the
42307     ** database file and/or page cache.
42308     */
42309     for(u=0; u<nRec; u++){
42310       if( needPagerReset ){
42311         pager_reset(pPager);
42312         needPagerReset = 0;
42313       }
42314       rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
42315       if( rc==SQLITE_OK ){
42316         nPlayback++;
42317       }else{
42318         if( rc==SQLITE_DONE ){
42319           pPager->journalOff = szJ;
42320           break;
42321         }else if( rc==SQLITE_IOERR_SHORT_READ ){
42322           /* If the journal has been truncated, simply stop reading and
42323           ** processing the journal. This might happen if the journal was
42324           ** not completely written and synced prior to a crash.  In that
42325           ** case, the database should have never been written in the
42326           ** first place so it is OK to simply abandon the rollback. */
42327           rc = SQLITE_OK;
42328           goto end_playback;
42329         }else{
42330           /* If we are unable to rollback, quit and return the error
42331           ** code.  This will cause the pager to enter the error state
42332           ** so that no further harm will be done.  Perhaps the next
42333           ** process to come along will be able to rollback the database.
42334           */
42335           goto end_playback;
42336         }
42337       }
42338     }
42339   }
42340   /*NOTREACHED*/
42341   assert( 0 );
42342 
42343 end_playback:
42344   /* Following a rollback, the database file should be back in its original
42345   ** state prior to the start of the transaction, so invoke the
42346   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
42347   ** assertion that the transaction counter was modified.
42348   */
42349 #ifdef SQLITE_DEBUG
42350   if( pPager->fd->pMethods ){
42351     sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
42352   }
42353 #endif
42354 
42355   /* If this playback is happening automatically as a result of an IO or
42356   ** malloc error that occurred after the change-counter was updated but
42357   ** before the transaction was committed, then the change-counter
42358   ** modification may just have been reverted. If this happens in exclusive
42359   ** mode, then subsequent transactions performed by the connection will not
42360   ** update the change-counter at all. This may lead to cache inconsistency
42361   ** problems for other processes at some point in the future. So, just
42362   ** in case this has happened, clear the changeCountDone flag now.
42363   */
42364   pPager->changeCountDone = pPager->tempFile;
42365 
42366   if( rc==SQLITE_OK ){
42367     zMaster = pPager->pTmpSpace;
42368     rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
42369     testcase( rc!=SQLITE_OK );
42370   }
42371   if( rc==SQLITE_OK
42372    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
42373   ){
42374     rc = sqlite3PagerSync(pPager, 0);
42375   }
42376   if( rc==SQLITE_OK ){
42377     rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
42378     testcase( rc!=SQLITE_OK );
42379   }
42380   if( rc==SQLITE_OK && zMaster[0] && res ){
42381     /* If there was a master journal and this routine will return success,
42382     ** see if it is possible to delete the master journal.
42383     */
42384     rc = pager_delmaster(pPager, zMaster);
42385     testcase( rc!=SQLITE_OK );
42386   }
42387   if( isHot && nPlayback ){
42388     sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
42389                 nPlayback, pPager->zJournal);
42390   }
42391 
42392   /* The Pager.sectorSize variable may have been updated while rolling
42393   ** back a journal created by a process with a different sector size
42394   ** value. Reset it to the correct value for this process.
42395   */
42396   setSectorSize(pPager);
42397   return rc;
42398 }
42399 
42400 
42401 /*
42402 ** Read the content for page pPg out of the database file and into
42403 ** pPg->pData. A shared lock or greater must be held on the database
42404 ** file before this function is called.
42405 **
42406 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
42407 ** the value read from the database file.
42408 **
42409 ** If an IO error occurs, then the IO error is returned to the caller.
42410 ** Otherwise, SQLITE_OK is returned.
42411 */
42412 static int readDbPage(PgHdr *pPg, u32 iFrame){
42413   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
42414   Pgno pgno = pPg->pgno;       /* Page number to read */
42415   int rc = SQLITE_OK;          /* Return code */
42416   int pgsz = pPager->pageSize; /* Number of bytes to read */
42417 
42418   assert( pPager->eState>=PAGER_READER && !MEMDB );
42419   assert( isOpen(pPager->fd) );
42420 
42421 #ifndef SQLITE_OMIT_WAL
42422   if( iFrame ){
42423     /* Try to pull the page from the write-ahead log. */
42424     rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
42425   }else
42426 #endif
42427   {
42428     i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
42429     rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
42430     if( rc==SQLITE_IOERR_SHORT_READ ){
42431       rc = SQLITE_OK;
42432     }
42433   }
42434 
42435   if( pgno==1 ){
42436     if( rc ){
42437       /* If the read is unsuccessful, set the dbFileVers[] to something
42438       ** that will never be a valid file version.  dbFileVers[] is a copy
42439       ** of bytes 24..39 of the database.  Bytes 28..31 should always be
42440       ** zero or the size of the database in page. Bytes 32..35 and 35..39
42441       ** should be page numbers which are never 0xffffffff.  So filling
42442       ** pPager->dbFileVers[] with all 0xff bytes should suffice.
42443       **
42444       ** For an encrypted database, the situation is more complex:  bytes
42445       ** 24..39 of the database are white noise.  But the probability of
42446       ** white noising equaling 16 bytes of 0xff is vanishingly small so
42447       ** we should still be ok.
42448       */
42449       memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
42450     }else{
42451       u8 *dbFileVers = &((u8*)pPg->pData)[24];
42452       memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
42453     }
42454   }
42455   CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
42456 
42457   PAGER_INCR(sqlite3_pager_readdb_count);
42458   PAGER_INCR(pPager->nRead);
42459   IOTRACE(("PGIN %p %d\n", pPager, pgno));
42460   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
42461                PAGERID(pPager), pgno, pager_pagehash(pPg)));
42462 
42463   return rc;
42464 }
42465 
42466 /*
42467 ** Update the value of the change-counter at offsets 24 and 92 in
42468 ** the header and the sqlite version number at offset 96.
42469 **
42470 ** This is an unconditional update.  See also the pager_incr_changecounter()
42471 ** routine which only updates the change-counter if the update is actually
42472 ** needed, as determined by the pPager->changeCountDone state variable.
42473 */
42474 static void pager_write_changecounter(PgHdr *pPg){
42475   u32 change_counter;
42476 
42477   /* Increment the value just read and write it back to byte 24. */
42478   change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
42479   put32bits(((char*)pPg->pData)+24, change_counter);
42480 
42481   /* Also store the SQLite version number in bytes 96..99 and in
42482   ** bytes 92..95 store the change counter for which the version number
42483   ** is valid. */
42484   put32bits(((char*)pPg->pData)+92, change_counter);
42485   put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
42486 }
42487 
42488 #ifndef SQLITE_OMIT_WAL
42489 /*
42490 ** This function is invoked once for each page that has already been
42491 ** written into the log file when a WAL transaction is rolled back.
42492 ** Parameter iPg is the page number of said page. The pCtx argument
42493 ** is actually a pointer to the Pager structure.
42494 **
42495 ** If page iPg is present in the cache, and has no outstanding references,
42496 ** it is discarded. Otherwise, if there are one or more outstanding
42497 ** references, the page content is reloaded from the database. If the
42498 ** attempt to reload content from the database is required and fails,
42499 ** return an SQLite error code. Otherwise, SQLITE_OK.
42500 */
42501 static int pagerUndoCallback(void *pCtx, Pgno iPg){
42502   int rc = SQLITE_OK;
42503   Pager *pPager = (Pager *)pCtx;
42504   PgHdr *pPg;
42505 
42506   assert( pagerUseWal(pPager) );
42507   pPg = sqlite3PagerLookup(pPager, iPg);
42508   if( pPg ){
42509     if( sqlite3PcachePageRefcount(pPg)==1 ){
42510       sqlite3PcacheDrop(pPg);
42511     }else{
42512       u32 iFrame = 0;
42513       rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
42514       if( rc==SQLITE_OK ){
42515         rc = readDbPage(pPg, iFrame);
42516       }
42517       if( rc==SQLITE_OK ){
42518         pPager->xReiniter(pPg);
42519       }
42520       sqlite3PagerUnrefNotNull(pPg);
42521     }
42522   }
42523 
42524   /* Normally, if a transaction is rolled back, any backup processes are
42525   ** updated as data is copied out of the rollback journal and into the
42526   ** database. This is not generally possible with a WAL database, as
42527   ** rollback involves simply truncating the log file. Therefore, if one
42528   ** or more frames have already been written to the log (and therefore
42529   ** also copied into the backup databases) as part of this transaction,
42530   ** the backups must be restarted.
42531   */
42532   sqlite3BackupRestart(pPager->pBackup);
42533 
42534   return rc;
42535 }
42536 
42537 /*
42538 ** This function is called to rollback a transaction on a WAL database.
42539 */
42540 static int pagerRollbackWal(Pager *pPager){
42541   int rc;                         /* Return Code */
42542   PgHdr *pList;                   /* List of dirty pages to revert */
42543 
42544   /* For all pages in the cache that are currently dirty or have already
42545   ** been written (but not committed) to the log file, do one of the
42546   ** following:
42547   **
42548   **   + Discard the cached page (if refcount==0), or
42549   **   + Reload page content from the database (if refcount>0).
42550   */
42551   pPager->dbSize = pPager->dbOrigSize;
42552   rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
42553   pList = sqlite3PcacheDirtyList(pPager->pPCache);
42554   while( pList && rc==SQLITE_OK ){
42555     PgHdr *pNext = pList->pDirty;
42556     rc = pagerUndoCallback((void *)pPager, pList->pgno);
42557     pList = pNext;
42558   }
42559 
42560   return rc;
42561 }
42562 
42563 /*
42564 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
42565 ** the contents of the list of pages headed by pList (connected by pDirty),
42566 ** this function notifies any active backup processes that the pages have
42567 ** changed.
42568 **
42569 ** The list of pages passed into this routine is always sorted by page number.
42570 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
42571 */
42572 static int pagerWalFrames(
42573   Pager *pPager,                  /* Pager object */
42574   PgHdr *pList,                   /* List of frames to log */
42575   Pgno nTruncate,                 /* Database size after this commit */
42576   int isCommit                    /* True if this is a commit */
42577 ){
42578   int rc;                         /* Return code */
42579   int nList;                      /* Number of pages in pList */
42580 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
42581   PgHdr *p;                       /* For looping over pages */
42582 #endif
42583 
42584   assert( pPager->pWal );
42585   assert( pList );
42586 #ifdef SQLITE_DEBUG
42587   /* Verify that the page list is in accending order */
42588   for(p=pList; p && p->pDirty; p=p->pDirty){
42589     assert( p->pgno < p->pDirty->pgno );
42590   }
42591 #endif
42592 
42593   assert( pList->pDirty==0 || isCommit );
42594   if( isCommit ){
42595     /* If a WAL transaction is being committed, there is no point in writing
42596     ** any pages with page numbers greater than nTruncate into the WAL file.
42597     ** They will never be read by any client. So remove them from the pDirty
42598     ** list here. */
42599     PgHdr *p;
42600     PgHdr **ppNext = &pList;
42601     nList = 0;
42602     for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
42603       if( p->pgno<=nTruncate ){
42604         ppNext = &p->pDirty;
42605         nList++;
42606       }
42607     }
42608     assert( pList );
42609   }else{
42610     nList = 1;
42611   }
42612   pPager->aStat[PAGER_STAT_WRITE] += nList;
42613 
42614   if( pList->pgno==1 ) pager_write_changecounter(pList);
42615   rc = sqlite3WalFrames(pPager->pWal,
42616       pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
42617   );
42618   if( rc==SQLITE_OK && pPager->pBackup ){
42619     PgHdr *p;
42620     for(p=pList; p; p=p->pDirty){
42621       sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
42622     }
42623   }
42624 
42625 #ifdef SQLITE_CHECK_PAGES
42626   pList = sqlite3PcacheDirtyList(pPager->pPCache);
42627   for(p=pList; p; p=p->pDirty){
42628     pager_set_pagehash(p);
42629   }
42630 #endif
42631 
42632   return rc;
42633 }
42634 
42635 /*
42636 ** Begin a read transaction on the WAL.
42637 **
42638 ** This routine used to be called "pagerOpenSnapshot()" because it essentially
42639 ** makes a snapshot of the database at the current point in time and preserves
42640 ** that snapshot for use by the reader in spite of concurrently changes by
42641 ** other writers or checkpointers.
42642 */
42643 static int pagerBeginReadTransaction(Pager *pPager){
42644   int rc;                         /* Return code */
42645   int changed = 0;                /* True if cache must be reset */
42646 
42647   assert( pagerUseWal(pPager) );
42648   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
42649 
42650   /* sqlite3WalEndReadTransaction() was not called for the previous
42651   ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
42652   ** are in locking_mode=NORMAL and EndRead() was previously called,
42653   ** the duplicate call is harmless.
42654   */
42655   sqlite3WalEndReadTransaction(pPager->pWal);
42656 
42657   rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
42658   if( rc!=SQLITE_OK || changed ){
42659     pager_reset(pPager);
42660     if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
42661   }
42662 
42663   return rc;
42664 }
42665 #endif
42666 
42667 /*
42668 ** This function is called as part of the transition from PAGER_OPEN
42669 ** to PAGER_READER state to determine the size of the database file
42670 ** in pages (assuming the page size currently stored in Pager.pageSize).
42671 **
42672 ** If no error occurs, SQLITE_OK is returned and the size of the database
42673 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
42674 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
42675 */
42676 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
42677   Pgno nPage;                     /* Value to return via *pnPage */
42678 
42679   /* Query the WAL sub-system for the database size. The WalDbsize()
42680   ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
42681   ** if the database size is not available. The database size is not
42682   ** available from the WAL sub-system if the log file is empty or
42683   ** contains no valid committed transactions.
42684   */
42685   assert( pPager->eState==PAGER_OPEN );
42686   assert( pPager->eLock>=SHARED_LOCK );
42687   nPage = sqlite3WalDbsize(pPager->pWal);
42688 
42689   /* If the database size was not available from the WAL sub-system,
42690   ** determine it based on the size of the database file. If the size
42691   ** of the database file is not an integer multiple of the page-size,
42692   ** round down to the nearest page. Except, any file larger than 0
42693   ** bytes in size is considered to contain at least one page.
42694   */
42695   if( nPage==0 ){
42696     i64 n = 0;                    /* Size of db file in bytes */
42697     assert( isOpen(pPager->fd) || pPager->tempFile );
42698     if( isOpen(pPager->fd) ){
42699       int rc = sqlite3OsFileSize(pPager->fd, &n);
42700       if( rc!=SQLITE_OK ){
42701         return rc;
42702       }
42703     }
42704     nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
42705   }
42706 
42707   /* If the current number of pages in the file is greater than the
42708   ** configured maximum pager number, increase the allowed limit so
42709   ** that the file can be read.
42710   */
42711   if( nPage>pPager->mxPgno ){
42712     pPager->mxPgno = (Pgno)nPage;
42713   }
42714 
42715   *pnPage = nPage;
42716   return SQLITE_OK;
42717 }
42718 
42719 #ifndef SQLITE_OMIT_WAL
42720 /*
42721 ** Check if the *-wal file that corresponds to the database opened by pPager
42722 ** exists if the database is not empy, or verify that the *-wal file does
42723 ** not exist (by deleting it) if the database file is empty.
42724 **
42725 ** If the database is not empty and the *-wal file exists, open the pager
42726 ** in WAL mode.  If the database is empty or if no *-wal file exists and
42727 ** if no error occurs, make sure Pager.journalMode is not set to
42728 ** PAGER_JOURNALMODE_WAL.
42729 **
42730 ** Return SQLITE_OK or an error code.
42731 **
42732 ** The caller must hold a SHARED lock on the database file to call this
42733 ** function. Because an EXCLUSIVE lock on the db file is required to delete
42734 ** a WAL on a none-empty database, this ensures there is no race condition
42735 ** between the xAccess() below and an xDelete() being executed by some
42736 ** other connection.
42737 */
42738 static int pagerOpenWalIfPresent(Pager *pPager){
42739   int rc = SQLITE_OK;
42740   assert( pPager->eState==PAGER_OPEN );
42741   assert( pPager->eLock>=SHARED_LOCK );
42742 
42743   if( !pPager->tempFile ){
42744     int isWal;                    /* True if WAL file exists */
42745     Pgno nPage;                   /* Size of the database file */
42746 
42747     rc = pagerPagecount(pPager, &nPage);
42748     if( rc ) return rc;
42749     if( nPage==0 ){
42750       rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
42751       if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
42752       isWal = 0;
42753     }else{
42754       rc = sqlite3OsAccess(
42755           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
42756       );
42757     }
42758     if( rc==SQLITE_OK ){
42759       if( isWal ){
42760         testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
42761         rc = sqlite3PagerOpenWal(pPager, 0);
42762       }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
42763         pPager->journalMode = PAGER_JOURNALMODE_DELETE;
42764       }
42765     }
42766   }
42767   return rc;
42768 }
42769 #endif
42770 
42771 /*
42772 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
42773 ** the entire master journal file. The case pSavepoint==NULL occurs when
42774 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
42775 ** savepoint.
42776 **
42777 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
42778 ** being rolled back), then the rollback consists of up to three stages,
42779 ** performed in the order specified:
42780 **
42781 **   * Pages are played back from the main journal starting at byte
42782 **     offset PagerSavepoint.iOffset and continuing to
42783 **     PagerSavepoint.iHdrOffset, or to the end of the main journal
42784 **     file if PagerSavepoint.iHdrOffset is zero.
42785 **
42786 **   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
42787 **     back starting from the journal header immediately following
42788 **     PagerSavepoint.iHdrOffset to the end of the main journal file.
42789 **
42790 **   * Pages are then played back from the sub-journal file, starting
42791 **     with the PagerSavepoint.iSubRec and continuing to the end of
42792 **     the journal file.
42793 **
42794 ** Throughout the rollback process, each time a page is rolled back, the
42795 ** corresponding bit is set in a bitvec structure (variable pDone in the
42796 ** implementation below). This is used to ensure that a page is only
42797 ** rolled back the first time it is encountered in either journal.
42798 **
42799 ** If pSavepoint is NULL, then pages are only played back from the main
42800 ** journal file. There is no need for a bitvec in this case.
42801 **
42802 ** In either case, before playback commences the Pager.dbSize variable
42803 ** is reset to the value that it held at the start of the savepoint
42804 ** (or transaction). No page with a page-number greater than this value
42805 ** is played back. If one is encountered it is simply skipped.
42806 */
42807 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
42808   i64 szJ;                 /* Effective size of the main journal */
42809   i64 iHdrOff;             /* End of first segment of main-journal records */
42810   int rc = SQLITE_OK;      /* Return code */
42811   Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
42812 
42813   assert( pPager->eState!=PAGER_ERROR );
42814   assert( pPager->eState>=PAGER_WRITER_LOCKED );
42815 
42816   /* Allocate a bitvec to use to store the set of pages rolled back */
42817   if( pSavepoint ){
42818     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
42819     if( !pDone ){
42820       return SQLITE_NOMEM;
42821     }
42822   }
42823 
42824   /* Set the database size back to the value it was before the savepoint
42825   ** being reverted was opened.
42826   */
42827   pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
42828   pPager->changeCountDone = pPager->tempFile;
42829 
42830   if( !pSavepoint && pagerUseWal(pPager) ){
42831     return pagerRollbackWal(pPager);
42832   }
42833 
42834   /* Use pPager->journalOff as the effective size of the main rollback
42835   ** journal.  The actual file might be larger than this in
42836   ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
42837   ** past pPager->journalOff is off-limits to us.
42838   */
42839   szJ = pPager->journalOff;
42840   assert( pagerUseWal(pPager)==0 || szJ==0 );
42841 
42842   /* Begin by rolling back records from the main journal starting at
42843   ** PagerSavepoint.iOffset and continuing to the next journal header.
42844   ** There might be records in the main journal that have a page number
42845   ** greater than the current database size (pPager->dbSize) but those
42846   ** will be skipped automatically.  Pages are added to pDone as they
42847   ** are played back.
42848   */
42849   if( pSavepoint && !pagerUseWal(pPager) ){
42850     iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
42851     pPager->journalOff = pSavepoint->iOffset;
42852     while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
42853       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
42854     }
42855     assert( rc!=SQLITE_DONE );
42856   }else{
42857     pPager->journalOff = 0;
42858   }
42859 
42860   /* Continue rolling back records out of the main journal starting at
42861   ** the first journal header seen and continuing until the effective end
42862   ** of the main journal file.  Continue to skip out-of-range pages and
42863   ** continue adding pages rolled back to pDone.
42864   */
42865   while( rc==SQLITE_OK && pPager->journalOff<szJ ){
42866     u32 ii;            /* Loop counter */
42867     u32 nJRec = 0;     /* Number of Journal Records */
42868     u32 dummy;
42869     rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
42870     assert( rc!=SQLITE_DONE );
42871 
42872     /*
42873     ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
42874     ** test is related to ticket #2565.  See the discussion in the
42875     ** pager_playback() function for additional information.
42876     */
42877     if( nJRec==0
42878      && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
42879     ){
42880       nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
42881     }
42882     for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
42883       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
42884     }
42885     assert( rc!=SQLITE_DONE );
42886   }
42887   assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
42888 
42889   /* Finally,  rollback pages from the sub-journal.  Page that were
42890   ** previously rolled back out of the main journal (and are hence in pDone)
42891   ** will be skipped.  Out-of-range pages are also skipped.
42892   */
42893   if( pSavepoint ){
42894     u32 ii;            /* Loop counter */
42895     i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
42896 
42897     if( pagerUseWal(pPager) ){
42898       rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
42899     }
42900     for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
42901       assert( offset==(i64)ii*(4+pPager->pageSize) );
42902       rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
42903     }
42904     assert( rc!=SQLITE_DONE );
42905   }
42906 
42907   sqlite3BitvecDestroy(pDone);
42908   if( rc==SQLITE_OK ){
42909     pPager->journalOff = szJ;
42910   }
42911 
42912   return rc;
42913 }
42914 
42915 /*
42916 ** Change the maximum number of in-memory pages that are allowed.
42917 */
42918 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
42919   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
42920 }
42921 
42922 /*
42923 ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
42924 */
42925 static void pagerFixMaplimit(Pager *pPager){
42926 #if SQLITE_MAX_MMAP_SIZE>0
42927   sqlite3_file *fd = pPager->fd;
42928   if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
42929     sqlite3_int64 sz;
42930     sz = pPager->szMmap;
42931     pPager->bUseFetch = (sz>0);
42932     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
42933   }
42934 #endif
42935 }
42936 
42937 /*
42938 ** Change the maximum size of any memory mapping made of the database file.
42939 */
42940 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
42941   pPager->szMmap = szMmap;
42942   pagerFixMaplimit(pPager);
42943 }
42944 
42945 /*
42946 ** Free as much memory as possible from the pager.
42947 */
42948 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
42949   sqlite3PcacheShrink(pPager->pPCache);
42950 }
42951 
42952 /*
42953 ** Adjust settings of the pager to those specified in the pgFlags parameter.
42954 **
42955 ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
42956 ** of the database to damage due to OS crashes or power failures by
42957 ** changing the number of syncs()s when writing the journals.
42958 ** There are three levels:
42959 **
42960 **    OFF       sqlite3OsSync() is never called.  This is the default
42961 **              for temporary and transient files.
42962 **
42963 **    NORMAL    The journal is synced once before writes begin on the
42964 **              database.  This is normally adequate protection, but
42965 **              it is theoretically possible, though very unlikely,
42966 **              that an inopertune power failure could leave the journal
42967 **              in a state which would cause damage to the database
42968 **              when it is rolled back.
42969 **
42970 **    FULL      The journal is synced twice before writes begin on the
42971 **              database (with some additional information - the nRec field
42972 **              of the journal header - being written in between the two
42973 **              syncs).  If we assume that writing a
42974 **              single disk sector is atomic, then this mode provides
42975 **              assurance that the journal will not be corrupted to the
42976 **              point of causing damage to the database during rollback.
42977 **
42978 ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
42979 ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
42980 ** prior to the start of checkpoint and that the database file is synced
42981 ** at the conclusion of the checkpoint if the entire content of the WAL
42982 ** was written back into the database.  But no sync operations occur for
42983 ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
42984 ** file is synced following each commit operation, in addition to the
42985 ** syncs associated with NORMAL.
42986 **
42987 ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
42988 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
42989 ** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
42990 ** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
42991 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
42992 ** synchronous=FULL versus synchronous=NORMAL setting determines when
42993 ** the xSync primitive is called and is relevant to all platforms.
42994 **
42995 ** Numeric values associated with these states are OFF==1, NORMAL=2,
42996 ** and FULL=3.
42997 */
42998 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
42999 SQLITE_PRIVATE void sqlite3PagerSetFlags(
43000   Pager *pPager,        /* The pager to set safety level for */
43001   unsigned pgFlags      /* Various flags */
43002 ){
43003   unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
43004   assert( level>=1 && level<=3 );
43005   pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
43006   pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
43007   if( pPager->noSync ){
43008     pPager->syncFlags = 0;
43009     pPager->ckptSyncFlags = 0;
43010   }else if( pgFlags & PAGER_FULLFSYNC ){
43011     pPager->syncFlags = SQLITE_SYNC_FULL;
43012     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
43013   }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
43014     pPager->syncFlags = SQLITE_SYNC_NORMAL;
43015     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
43016   }else{
43017     pPager->syncFlags = SQLITE_SYNC_NORMAL;
43018     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
43019   }
43020   pPager->walSyncFlags = pPager->syncFlags;
43021   if( pPager->fullSync ){
43022     pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
43023   }
43024   if( pgFlags & PAGER_CACHESPILL ){
43025     pPager->doNotSpill &= ~SPILLFLAG_OFF;
43026   }else{
43027     pPager->doNotSpill |= SPILLFLAG_OFF;
43028   }
43029 }
43030 #endif
43031 
43032 /*
43033 ** The following global variable is incremented whenever the library
43034 ** attempts to open a temporary file.  This information is used for
43035 ** testing and analysis only.
43036 */
43037 #ifdef SQLITE_TEST
43038 SQLITE_API int sqlite3_opentemp_count = 0;
43039 #endif
43040 
43041 /*
43042 ** Open a temporary file.
43043 **
43044 ** Write the file descriptor into *pFile. Return SQLITE_OK on success
43045 ** or some other error code if we fail. The OS will automatically
43046 ** delete the temporary file when it is closed.
43047 **
43048 ** The flags passed to the VFS layer xOpen() call are those specified
43049 ** by parameter vfsFlags ORed with the following:
43050 **
43051 **     SQLITE_OPEN_READWRITE
43052 **     SQLITE_OPEN_CREATE
43053 **     SQLITE_OPEN_EXCLUSIVE
43054 **     SQLITE_OPEN_DELETEONCLOSE
43055 */
43056 static int pagerOpentemp(
43057   Pager *pPager,        /* The pager object */
43058   sqlite3_file *pFile,  /* Write the file descriptor here */
43059   int vfsFlags          /* Flags passed through to the VFS */
43060 ){
43061   int rc;               /* Return code */
43062 
43063 #ifdef SQLITE_TEST
43064   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
43065 #endif
43066 
43067   vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
43068             SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
43069   rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
43070   assert( rc!=SQLITE_OK || isOpen(pFile) );
43071   return rc;
43072 }
43073 
43074 /*
43075 ** Set the busy handler function.
43076 **
43077 ** The pager invokes the busy-handler if sqlite3OsLock() returns
43078 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
43079 ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
43080 ** lock. It does *not* invoke the busy handler when upgrading from
43081 ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
43082 ** (which occurs during hot-journal rollback). Summary:
43083 **
43084 **   Transition                        | Invokes xBusyHandler
43085 **   --------------------------------------------------------
43086 **   NO_LOCK       -> SHARED_LOCK      | Yes
43087 **   SHARED_LOCK   -> RESERVED_LOCK    | No
43088 **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
43089 **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
43090 **
43091 ** If the busy-handler callback returns non-zero, the lock is
43092 ** retried. If it returns zero, then the SQLITE_BUSY error is
43093 ** returned to the caller of the pager API function.
43094 */
43095 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
43096   Pager *pPager,                       /* Pager object */
43097   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
43098   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
43099 ){
43100   pPager->xBusyHandler = xBusyHandler;
43101   pPager->pBusyHandlerArg = pBusyHandlerArg;
43102 
43103   if( isOpen(pPager->fd) ){
43104     void **ap = (void **)&pPager->xBusyHandler;
43105     assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
43106     assert( ap[1]==pBusyHandlerArg );
43107     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
43108   }
43109 }
43110 
43111 /*
43112 ** Change the page size used by the Pager object. The new page size
43113 ** is passed in *pPageSize.
43114 **
43115 ** If the pager is in the error state when this function is called, it
43116 ** is a no-op. The value returned is the error state error code (i.e.
43117 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
43118 **
43119 ** Otherwise, if all of the following are true:
43120 **
43121 **   * the new page size (value of *pPageSize) is valid (a power
43122 **     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
43123 **
43124 **   * there are no outstanding page references, and
43125 **
43126 **   * the database is either not an in-memory database or it is
43127 **     an in-memory database that currently consists of zero pages.
43128 **
43129 ** then the pager object page size is set to *pPageSize.
43130 **
43131 ** If the page size is changed, then this function uses sqlite3PagerMalloc()
43132 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
43133 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
43134 ** In all other cases, SQLITE_OK is returned.
43135 **
43136 ** If the page size is not changed, either because one of the enumerated
43137 ** conditions above is not true, the pager was in error state when this
43138 ** function was called, or because the memory allocation attempt failed,
43139 ** then *pPageSize is set to the old, retained page size before returning.
43140 */
43141 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
43142   int rc = SQLITE_OK;
43143 
43144   /* It is not possible to do a full assert_pager_state() here, as this
43145   ** function may be called from within PagerOpen(), before the state
43146   ** of the Pager object is internally consistent.
43147   **
43148   ** At one point this function returned an error if the pager was in
43149   ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
43150   ** there is at least one outstanding page reference, this function
43151   ** is a no-op for that case anyhow.
43152   */
43153 
43154   u32 pageSize = *pPageSize;
43155   assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
43156   if( (pPager->memDb==0 || pPager->dbSize==0)
43157    && sqlite3PcacheRefCount(pPager->pPCache)==0
43158    && pageSize && pageSize!=(u32)pPager->pageSize
43159   ){
43160     char *pNew = NULL;             /* New temp space */
43161     i64 nByte = 0;
43162 
43163     if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
43164       rc = sqlite3OsFileSize(pPager->fd, &nByte);
43165     }
43166     if( rc==SQLITE_OK ){
43167       pNew = (char *)sqlite3PageMalloc(pageSize);
43168       if( !pNew ) rc = SQLITE_NOMEM;
43169     }
43170 
43171     if( rc==SQLITE_OK ){
43172       pager_reset(pPager);
43173       pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
43174       pPager->pageSize = pageSize;
43175       sqlite3PageFree(pPager->pTmpSpace);
43176       pPager->pTmpSpace = pNew;
43177       sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
43178     }
43179   }
43180 
43181   *pPageSize = pPager->pageSize;
43182   if( rc==SQLITE_OK ){
43183     if( nReserve<0 ) nReserve = pPager->nReserve;
43184     assert( nReserve>=0 && nReserve<1000 );
43185     pPager->nReserve = (i16)nReserve;
43186     pagerReportSize(pPager);
43187     pagerFixMaplimit(pPager);
43188   }
43189   return rc;
43190 }
43191 
43192 /*
43193 ** Return a pointer to the "temporary page" buffer held internally
43194 ** by the pager.  This is a buffer that is big enough to hold the
43195 ** entire content of a database page.  This buffer is used internally
43196 ** during rollback and will be overwritten whenever a rollback
43197 ** occurs.  But other modules are free to use it too, as long as
43198 ** no rollbacks are happening.
43199 */
43200 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
43201   return pPager->pTmpSpace;
43202 }
43203 
43204 /*
43205 ** Attempt to set the maximum database page count if mxPage is positive.
43206 ** Make no changes if mxPage is zero or negative.  And never reduce the
43207 ** maximum page count below the current size of the database.
43208 **
43209 ** Regardless of mxPage, return the current maximum page count.
43210 */
43211 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
43212   if( mxPage>0 ){
43213     pPager->mxPgno = mxPage;
43214   }
43215   assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
43216   assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
43217   return pPager->mxPgno;
43218 }
43219 
43220 /*
43221 ** The following set of routines are used to disable the simulated
43222 ** I/O error mechanism.  These routines are used to avoid simulated
43223 ** errors in places where we do not care about errors.
43224 **
43225 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
43226 ** and generate no code.
43227 */
43228 #ifdef SQLITE_TEST
43229 SQLITE_API extern int sqlite3_io_error_pending;
43230 SQLITE_API extern int sqlite3_io_error_hit;
43231 static int saved_cnt;
43232 void disable_simulated_io_errors(void){
43233   saved_cnt = sqlite3_io_error_pending;
43234   sqlite3_io_error_pending = -1;
43235 }
43236 void enable_simulated_io_errors(void){
43237   sqlite3_io_error_pending = saved_cnt;
43238 }
43239 #else
43240 # define disable_simulated_io_errors()
43241 # define enable_simulated_io_errors()
43242 #endif
43243 
43244 /*
43245 ** Read the first N bytes from the beginning of the file into memory
43246 ** that pDest points to.
43247 **
43248 ** If the pager was opened on a transient file (zFilename==""), or
43249 ** opened on a file less than N bytes in size, the output buffer is
43250 ** zeroed and SQLITE_OK returned. The rationale for this is that this
43251 ** function is used to read database headers, and a new transient or
43252 ** zero sized database has a header than consists entirely of zeroes.
43253 **
43254 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
43255 ** the error code is returned to the caller and the contents of the
43256 ** output buffer undefined.
43257 */
43258 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
43259   int rc = SQLITE_OK;
43260   memset(pDest, 0, N);
43261   assert( isOpen(pPager->fd) || pPager->tempFile );
43262 
43263   /* This routine is only called by btree immediately after creating
43264   ** the Pager object.  There has not been an opportunity to transition
43265   ** to WAL mode yet.
43266   */
43267   assert( !pagerUseWal(pPager) );
43268 
43269   if( isOpen(pPager->fd) ){
43270     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
43271     rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
43272     if( rc==SQLITE_IOERR_SHORT_READ ){
43273       rc = SQLITE_OK;
43274     }
43275   }
43276   return rc;
43277 }
43278 
43279 /*
43280 ** This function may only be called when a read-transaction is open on
43281 ** the pager. It returns the total number of pages in the database.
43282 **
43283 ** However, if the file is between 1 and <page-size> bytes in size, then
43284 ** this is considered a 1 page file.
43285 */
43286 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
43287   assert( pPager->eState>=PAGER_READER );
43288   assert( pPager->eState!=PAGER_WRITER_FINISHED );
43289   *pnPage = (int)pPager->dbSize;
43290 }
43291 
43292 
43293 /*
43294 ** Try to obtain a lock of type locktype on the database file. If
43295 ** a similar or greater lock is already held, this function is a no-op
43296 ** (returning SQLITE_OK immediately).
43297 **
43298 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
43299 ** the busy callback if the lock is currently not available. Repeat
43300 ** until the busy callback returns false or until the attempt to
43301 ** obtain the lock succeeds.
43302 **
43303 ** Return SQLITE_OK on success and an error code if we cannot obtain
43304 ** the lock. If the lock is obtained successfully, set the Pager.state
43305 ** variable to locktype before returning.
43306 */
43307 static int pager_wait_on_lock(Pager *pPager, int locktype){
43308   int rc;                              /* Return code */
43309 
43310   /* Check that this is either a no-op (because the requested lock is
43311   ** already held, or one of the transistions that the busy-handler
43312   ** may be invoked during, according to the comment above
43313   ** sqlite3PagerSetBusyhandler().
43314   */
43315   assert( (pPager->eLock>=locktype)
43316        || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
43317        || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
43318   );
43319 
43320   do {
43321     rc = pagerLockDb(pPager, locktype);
43322   }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
43323   return rc;
43324 }
43325 
43326 /*
43327 ** Function assertTruncateConstraint(pPager) checks that one of the
43328 ** following is true for all dirty pages currently in the page-cache:
43329 **
43330 **   a) The page number is less than or equal to the size of the
43331 **      current database image, in pages, OR
43332 **
43333 **   b) if the page content were written at this time, it would not
43334 **      be necessary to write the current content out to the sub-journal
43335 **      (as determined by function subjRequiresPage()).
43336 **
43337 ** If the condition asserted by this function were not true, and the
43338 ** dirty page were to be discarded from the cache via the pagerStress()
43339 ** routine, pagerStress() would not write the current page content to
43340 ** the database file. If a savepoint transaction were rolled back after
43341 ** this happened, the correct behavior would be to restore the current
43342 ** content of the page. However, since this content is not present in either
43343 ** the database file or the portion of the rollback journal and
43344 ** sub-journal rolled back the content could not be restored and the
43345 ** database image would become corrupt. It is therefore fortunate that
43346 ** this circumstance cannot arise.
43347 */
43348 #if defined(SQLITE_DEBUG)
43349 static void assertTruncateConstraintCb(PgHdr *pPg){
43350   assert( pPg->flags&PGHDR_DIRTY );
43351   assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
43352 }
43353 static void assertTruncateConstraint(Pager *pPager){
43354   sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
43355 }
43356 #else
43357 # define assertTruncateConstraint(pPager)
43358 #endif
43359 
43360 /*
43361 ** Truncate the in-memory database file image to nPage pages. This
43362 ** function does not actually modify the database file on disk. It
43363 ** just sets the internal state of the pager object so that the
43364 ** truncation will be done when the current transaction is committed.
43365 **
43366 ** This function is only called right before committing a transaction.
43367 ** Once this function has been called, the transaction must either be
43368 ** rolled back or committed. It is not safe to call this function and
43369 ** then continue writing to the database.
43370 */
43371 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
43372   assert( pPager->dbSize>=nPage );
43373   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
43374   pPager->dbSize = nPage;
43375 
43376   /* At one point the code here called assertTruncateConstraint() to
43377   ** ensure that all pages being truncated away by this operation are,
43378   ** if one or more savepoints are open, present in the savepoint
43379   ** journal so that they can be restored if the savepoint is rolled
43380   ** back. This is no longer necessary as this function is now only
43381   ** called right before committing a transaction. So although the
43382   ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
43383   ** they cannot be rolled back. So the assertTruncateConstraint() call
43384   ** is no longer correct. */
43385 }
43386 
43387 
43388 /*
43389 ** This function is called before attempting a hot-journal rollback. It
43390 ** syncs the journal file to disk, then sets pPager->journalHdr to the
43391 ** size of the journal file so that the pager_playback() routine knows
43392 ** that the entire journal file has been synced.
43393 **
43394 ** Syncing a hot-journal to disk before attempting to roll it back ensures
43395 ** that if a power-failure occurs during the rollback, the process that
43396 ** attempts rollback following system recovery sees the same journal
43397 ** content as this process.
43398 **
43399 ** If everything goes as planned, SQLITE_OK is returned. Otherwise,
43400 ** an SQLite error code.
43401 */
43402 static int pagerSyncHotJournal(Pager *pPager){
43403   int rc = SQLITE_OK;
43404   if( !pPager->noSync ){
43405     rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
43406   }
43407   if( rc==SQLITE_OK ){
43408     rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
43409   }
43410   return rc;
43411 }
43412 
43413 /*
43414 ** Obtain a reference to a memory mapped page object for page number pgno.
43415 ** The new object will use the pointer pData, obtained from xFetch().
43416 ** If successful, set *ppPage to point to the new page reference
43417 ** and return SQLITE_OK. Otherwise, return an SQLite error code and set
43418 ** *ppPage to zero.
43419 **
43420 ** Page references obtained by calling this function should be released
43421 ** by calling pagerReleaseMapPage().
43422 */
43423 static int pagerAcquireMapPage(
43424   Pager *pPager,                  /* Pager object */
43425   Pgno pgno,                      /* Page number */
43426   void *pData,                    /* xFetch()'d data for this page */
43427   PgHdr **ppPage                  /* OUT: Acquired page object */
43428 ){
43429   PgHdr *p;                       /* Memory mapped page to return */
43430 
43431   if( pPager->pMmapFreelist ){
43432     *ppPage = p = pPager->pMmapFreelist;
43433     pPager->pMmapFreelist = p->pDirty;
43434     p->pDirty = 0;
43435     memset(p->pExtra, 0, pPager->nExtra);
43436   }else{
43437     *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
43438     if( p==0 ){
43439       sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
43440       return SQLITE_NOMEM;
43441     }
43442     p->pExtra = (void *)&p[1];
43443     p->flags = PGHDR_MMAP;
43444     p->nRef = 1;
43445     p->pPager = pPager;
43446   }
43447 
43448   assert( p->pExtra==(void *)&p[1] );
43449   assert( p->pPage==0 );
43450   assert( p->flags==PGHDR_MMAP );
43451   assert( p->pPager==pPager );
43452   assert( p->nRef==1 );
43453 
43454   p->pgno = pgno;
43455   p->pData = pData;
43456   pPager->nMmapOut++;
43457 
43458   return SQLITE_OK;
43459 }
43460 
43461 /*
43462 ** Release a reference to page pPg. pPg must have been returned by an
43463 ** earlier call to pagerAcquireMapPage().
43464 */
43465 static void pagerReleaseMapPage(PgHdr *pPg){
43466   Pager *pPager = pPg->pPager;
43467   pPager->nMmapOut--;
43468   pPg->pDirty = pPager->pMmapFreelist;
43469   pPager->pMmapFreelist = pPg;
43470 
43471   assert( pPager->fd->pMethods->iVersion>=3 );
43472   sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
43473 }
43474 
43475 /*
43476 ** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
43477 */
43478 static void pagerFreeMapHdrs(Pager *pPager){
43479   PgHdr *p;
43480   PgHdr *pNext;
43481   for(p=pPager->pMmapFreelist; p; p=pNext){
43482     pNext = p->pDirty;
43483     sqlite3_free(p);
43484   }
43485 }
43486 
43487 
43488 /*
43489 ** Shutdown the page cache.  Free all memory and close all files.
43490 **
43491 ** If a transaction was in progress when this routine is called, that
43492 ** transaction is rolled back.  All outstanding pages are invalidated
43493 ** and their memory is freed.  Any attempt to use a page associated
43494 ** with this page cache after this function returns will likely
43495 ** result in a coredump.
43496 **
43497 ** This function always succeeds. If a transaction is active an attempt
43498 ** is made to roll it back. If an error occurs during the rollback
43499 ** a hot journal may be left in the filesystem but no error is returned
43500 ** to the caller.
43501 */
43502 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
43503   u8 *pTmp = (u8 *)pPager->pTmpSpace;
43504 
43505   assert( assert_pager_state(pPager) );
43506   disable_simulated_io_errors();
43507   sqlite3BeginBenignMalloc();
43508   pagerFreeMapHdrs(pPager);
43509   /* pPager->errCode = 0; */
43510   pPager->exclusiveMode = 0;
43511 #ifndef SQLITE_OMIT_WAL
43512   sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
43513   pPager->pWal = 0;
43514 #endif
43515   pager_reset(pPager);
43516   if( MEMDB ){
43517     pager_unlock(pPager);
43518   }else{
43519     /* If it is open, sync the journal file before calling UnlockAndRollback.
43520     ** If this is not done, then an unsynced portion of the open journal
43521     ** file may be played back into the database. If a power failure occurs
43522     ** while this is happening, the database could become corrupt.
43523     **
43524     ** If an error occurs while trying to sync the journal, shift the pager
43525     ** into the ERROR state. This causes UnlockAndRollback to unlock the
43526     ** database and close the journal file without attempting to roll it
43527     ** back or finalize it. The next database user will have to do hot-journal
43528     ** rollback before accessing the database file.
43529     */
43530     if( isOpen(pPager->jfd) ){
43531       pager_error(pPager, pagerSyncHotJournal(pPager));
43532     }
43533     pagerUnlockAndRollback(pPager);
43534   }
43535   sqlite3EndBenignMalloc();
43536   enable_simulated_io_errors();
43537   PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
43538   IOTRACE(("CLOSE %p\n", pPager))
43539   sqlite3OsClose(pPager->jfd);
43540   sqlite3OsClose(pPager->fd);
43541   sqlite3PageFree(pTmp);
43542   sqlite3PcacheClose(pPager->pPCache);
43543 
43544 #ifdef SQLITE_HAS_CODEC
43545   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
43546 #endif
43547 
43548   assert( !pPager->aSavepoint && !pPager->pInJournal );
43549   assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
43550 
43551   sqlite3_free(pPager);
43552   return SQLITE_OK;
43553 }
43554 
43555 #if !defined(NDEBUG) || defined(SQLITE_TEST)
43556 /*
43557 ** Return the page number for page pPg.
43558 */
43559 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
43560   return pPg->pgno;
43561 }
43562 #endif
43563 
43564 /*
43565 ** Increment the reference count for page pPg.
43566 */
43567 SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
43568   sqlite3PcacheRef(pPg);
43569 }
43570 
43571 /*
43572 ** Sync the journal. In other words, make sure all the pages that have
43573 ** been written to the journal have actually reached the surface of the
43574 ** disk and can be restored in the event of a hot-journal rollback.
43575 **
43576 ** If the Pager.noSync flag is set, then this function is a no-op.
43577 ** Otherwise, the actions required depend on the journal-mode and the
43578 ** device characteristics of the file-system, as follows:
43579 **
43580 **   * If the journal file is an in-memory journal file, no action need
43581 **     be taken.
43582 **
43583 **   * Otherwise, if the device does not support the SAFE_APPEND property,
43584 **     then the nRec field of the most recently written journal header
43585 **     is updated to contain the number of journal records that have
43586 **     been written following it. If the pager is operating in full-sync
43587 **     mode, then the journal file is synced before this field is updated.
43588 **
43589 **   * If the device does not support the SEQUENTIAL property, then
43590 **     journal file is synced.
43591 **
43592 ** Or, in pseudo-code:
43593 **
43594 **   if( NOT <in-memory journal> ){
43595 **     if( NOT SAFE_APPEND ){
43596 **       if( <full-sync mode> ) xSync(<journal file>);
43597 **       <update nRec field>
43598 **     }
43599 **     if( NOT SEQUENTIAL ) xSync(<journal file>);
43600 **   }
43601 **
43602 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
43603 ** page currently held in memory before returning SQLITE_OK. If an IO
43604 ** error is encountered, then the IO error code is returned to the caller.
43605 */
43606 static int syncJournal(Pager *pPager, int newHdr){
43607   int rc;                         /* Return code */
43608 
43609   assert( pPager->eState==PAGER_WRITER_CACHEMOD
43610        || pPager->eState==PAGER_WRITER_DBMOD
43611   );
43612   assert( assert_pager_state(pPager) );
43613   assert( !pagerUseWal(pPager) );
43614 
43615   rc = sqlite3PagerExclusiveLock(pPager);
43616   if( rc!=SQLITE_OK ) return rc;
43617 
43618   if( !pPager->noSync ){
43619     assert( !pPager->tempFile );
43620     if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
43621       const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
43622       assert( isOpen(pPager->jfd) );
43623 
43624       if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
43625         /* This block deals with an obscure problem. If the last connection
43626         ** that wrote to this database was operating in persistent-journal
43627         ** mode, then the journal file may at this point actually be larger
43628         ** than Pager.journalOff bytes. If the next thing in the journal
43629         ** file happens to be a journal-header (written as part of the
43630         ** previous connection's transaction), and a crash or power-failure
43631         ** occurs after nRec is updated but before this connection writes
43632         ** anything else to the journal file (or commits/rolls back its
43633         ** transaction), then SQLite may become confused when doing the
43634         ** hot-journal rollback following recovery. It may roll back all
43635         ** of this connections data, then proceed to rolling back the old,
43636         ** out-of-date data that follows it. Database corruption.
43637         **
43638         ** To work around this, if the journal file does appear to contain
43639         ** a valid header following Pager.journalOff, then write a 0x00
43640         ** byte to the start of it to prevent it from being recognized.
43641         **
43642         ** Variable iNextHdrOffset is set to the offset at which this
43643         ** problematic header will occur, if it exists. aMagic is used
43644         ** as a temporary buffer to inspect the first couple of bytes of
43645         ** the potential journal header.
43646         */
43647         i64 iNextHdrOffset;
43648         u8 aMagic[8];
43649         u8 zHeader[sizeof(aJournalMagic)+4];
43650 
43651         memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
43652         put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
43653 
43654         iNextHdrOffset = journalHdrOffset(pPager);
43655         rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
43656         if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
43657           static const u8 zerobyte = 0;
43658           rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
43659         }
43660         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
43661           return rc;
43662         }
43663 
43664         /* Write the nRec value into the journal file header. If in
43665         ** full-synchronous mode, sync the journal first. This ensures that
43666         ** all data has really hit the disk before nRec is updated to mark
43667         ** it as a candidate for rollback.
43668         **
43669         ** This is not required if the persistent media supports the
43670         ** SAFE_APPEND property. Because in this case it is not possible
43671         ** for garbage data to be appended to the file, the nRec field
43672         ** is populated with 0xFFFFFFFF when the journal header is written
43673         ** and never needs to be updated.
43674         */
43675         if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
43676           PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
43677           IOTRACE(("JSYNC %p\n", pPager))
43678           rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
43679           if( rc!=SQLITE_OK ) return rc;
43680         }
43681         IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
43682         rc = sqlite3OsWrite(
43683             pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
43684         );
43685         if( rc!=SQLITE_OK ) return rc;
43686       }
43687       if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
43688         PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
43689         IOTRACE(("JSYNC %p\n", pPager))
43690         rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
43691           (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
43692         );
43693         if( rc!=SQLITE_OK ) return rc;
43694       }
43695 
43696       pPager->journalHdr = pPager->journalOff;
43697       if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
43698         pPager->nRec = 0;
43699         rc = writeJournalHdr(pPager);
43700         if( rc!=SQLITE_OK ) return rc;
43701       }
43702     }else{
43703       pPager->journalHdr = pPager->journalOff;
43704     }
43705   }
43706 
43707   /* Unless the pager is in noSync mode, the journal file was just
43708   ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
43709   ** all pages.
43710   */
43711   sqlite3PcacheClearSyncFlags(pPager->pPCache);
43712   pPager->eState = PAGER_WRITER_DBMOD;
43713   assert( assert_pager_state(pPager) );
43714   return SQLITE_OK;
43715 }
43716 
43717 /*
43718 ** The argument is the first in a linked list of dirty pages connected
43719 ** by the PgHdr.pDirty pointer. This function writes each one of the
43720 ** in-memory pages in the list to the database file. The argument may
43721 ** be NULL, representing an empty list. In this case this function is
43722 ** a no-op.
43723 **
43724 ** The pager must hold at least a RESERVED lock when this function
43725 ** is called. Before writing anything to the database file, this lock
43726 ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
43727 ** SQLITE_BUSY is returned and no data is written to the database file.
43728 **
43729 ** If the pager is a temp-file pager and the actual file-system file
43730 ** is not yet open, it is created and opened before any data is
43731 ** written out.
43732 **
43733 ** Once the lock has been upgraded and, if necessary, the file opened,
43734 ** the pages are written out to the database file in list order. Writing
43735 ** a page is skipped if it meets either of the following criteria:
43736 **
43737 **   * The page number is greater than Pager.dbSize, or
43738 **   * The PGHDR_DONT_WRITE flag is set on the page.
43739 **
43740 ** If writing out a page causes the database file to grow, Pager.dbFileSize
43741 ** is updated accordingly. If page 1 is written out, then the value cached
43742 ** in Pager.dbFileVers[] is updated to match the new value stored in
43743 ** the database file.
43744 **
43745 ** If everything is successful, SQLITE_OK is returned. If an IO error
43746 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
43747 ** be obtained, SQLITE_BUSY is returned.
43748 */
43749 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
43750   int rc = SQLITE_OK;                  /* Return code */
43751 
43752   /* This function is only called for rollback pagers in WRITER_DBMOD state. */
43753   assert( !pagerUseWal(pPager) );
43754   assert( pPager->eState==PAGER_WRITER_DBMOD );
43755   assert( pPager->eLock==EXCLUSIVE_LOCK );
43756 
43757   /* If the file is a temp-file has not yet been opened, open it now. It
43758   ** is not possible for rc to be other than SQLITE_OK if this branch
43759   ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
43760   */
43761   if( !isOpen(pPager->fd) ){
43762     assert( pPager->tempFile && rc==SQLITE_OK );
43763     rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
43764   }
43765 
43766   /* Before the first write, give the VFS a hint of what the final
43767   ** file size will be.
43768   */
43769   assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
43770   if( rc==SQLITE_OK
43771    && pPager->dbHintSize<pPager->dbSize
43772    && (pList->pDirty || pList->pgno>pPager->dbHintSize)
43773   ){
43774     sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
43775     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
43776     pPager->dbHintSize = pPager->dbSize;
43777   }
43778 
43779   while( rc==SQLITE_OK && pList ){
43780     Pgno pgno = pList->pgno;
43781 
43782     /* If there are dirty pages in the page cache with page numbers greater
43783     ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
43784     ** make the file smaller (presumably by auto-vacuum code). Do not write
43785     ** any such pages to the file.
43786     **
43787     ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
43788     ** set (set by sqlite3PagerDontWrite()).
43789     */
43790     if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
43791       i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
43792       char *pData;                                   /* Data to write */
43793 
43794       assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
43795       if( pList->pgno==1 ) pager_write_changecounter(pList);
43796 
43797       /* Encode the database */
43798       CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
43799 
43800       /* Write out the page data. */
43801       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
43802 
43803       /* If page 1 was just written, update Pager.dbFileVers to match
43804       ** the value now stored in the database file. If writing this
43805       ** page caused the database file to grow, update dbFileSize.
43806       */
43807       if( pgno==1 ){
43808         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
43809       }
43810       if( pgno>pPager->dbFileSize ){
43811         pPager->dbFileSize = pgno;
43812       }
43813       pPager->aStat[PAGER_STAT_WRITE]++;
43814 
43815       /* Update any backup objects copying the contents of this pager. */
43816       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
43817 
43818       PAGERTRACE(("STORE %d page %d hash(%08x)\n",
43819                    PAGERID(pPager), pgno, pager_pagehash(pList)));
43820       IOTRACE(("PGOUT %p %d\n", pPager, pgno));
43821       PAGER_INCR(sqlite3_pager_writedb_count);
43822     }else{
43823       PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
43824     }
43825     pager_set_pagehash(pList);
43826     pList = pList->pDirty;
43827   }
43828 
43829   return rc;
43830 }
43831 
43832 /*
43833 ** Ensure that the sub-journal file is open. If it is already open, this
43834 ** function is a no-op.
43835 **
43836 ** SQLITE_OK is returned if everything goes according to plan. An
43837 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
43838 ** fails.
43839 */
43840 static int openSubJournal(Pager *pPager){
43841   int rc = SQLITE_OK;
43842   if( !isOpen(pPager->sjfd) ){
43843     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
43844       sqlite3MemJournalOpen(pPager->sjfd);
43845     }else{
43846       rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
43847     }
43848   }
43849   return rc;
43850 }
43851 
43852 /*
43853 ** Append a record of the current state of page pPg to the sub-journal.
43854 ** It is the callers responsibility to use subjRequiresPage() to check
43855 ** that it is really required before calling this function.
43856 **
43857 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
43858 ** for all open savepoints before returning.
43859 **
43860 ** This function returns SQLITE_OK if everything is successful, an IO
43861 ** error code if the attempt to write to the sub-journal fails, or
43862 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
43863 ** bitvec.
43864 */
43865 static int subjournalPage(PgHdr *pPg){
43866   int rc = SQLITE_OK;
43867   Pager *pPager = pPg->pPager;
43868   if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
43869 
43870     /* Open the sub-journal, if it has not already been opened */
43871     assert( pPager->useJournal );
43872     assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
43873     assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
43874     assert( pagerUseWal(pPager)
43875          || pageInJournal(pPager, pPg)
43876          || pPg->pgno>pPager->dbOrigSize
43877     );
43878     rc = openSubJournal(pPager);
43879 
43880     /* If the sub-journal was opened successfully (or was already open),
43881     ** write the journal record into the file.  */
43882     if( rc==SQLITE_OK ){
43883       void *pData = pPg->pData;
43884       i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
43885       char *pData2;
43886 
43887       CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
43888       PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
43889       rc = write32bits(pPager->sjfd, offset, pPg->pgno);
43890       if( rc==SQLITE_OK ){
43891         rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
43892       }
43893     }
43894   }
43895   if( rc==SQLITE_OK ){
43896     pPager->nSubRec++;
43897     assert( pPager->nSavepoint>0 );
43898     rc = addToSavepointBitvecs(pPager, pPg->pgno);
43899   }
43900   return rc;
43901 }
43902 
43903 /*
43904 ** This function is called by the pcache layer when it has reached some
43905 ** soft memory limit. The first argument is a pointer to a Pager object
43906 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
43907 ** database). The second argument is a reference to a page that is
43908 ** currently dirty but has no outstanding references. The page
43909 ** is always associated with the Pager object passed as the first
43910 ** argument.
43911 **
43912 ** The job of this function is to make pPg clean by writing its contents
43913 ** out to the database file, if possible. This may involve syncing the
43914 ** journal file.
43915 **
43916 ** If successful, sqlite3PcacheMakeClean() is called on the page and
43917 ** SQLITE_OK returned. If an IO error occurs while trying to make the
43918 ** page clean, the IO error code is returned. If the page cannot be
43919 ** made clean for some other reason, but no error occurs, then SQLITE_OK
43920 ** is returned by sqlite3PcacheMakeClean() is not called.
43921 */
43922 static int pagerStress(void *p, PgHdr *pPg){
43923   Pager *pPager = (Pager *)p;
43924   int rc = SQLITE_OK;
43925 
43926   assert( pPg->pPager==pPager );
43927   assert( pPg->flags&PGHDR_DIRTY );
43928 
43929   /* The doNotSpill NOSYNC bit is set during times when doing a sync of
43930   ** journal (and adding a new header) is not allowed.  This occurs
43931   ** during calls to sqlite3PagerWrite() while trying to journal multiple
43932   ** pages belonging to the same sector.
43933   **
43934   ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
43935   ** regardless of whether or not a sync is required.  This is set during
43936   ** a rollback or by user request, respectively.
43937   **
43938   ** Spilling is also prohibited when in an error state since that could
43939   ** lead to database corruption.   In the current implementaton it
43940   ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
43941   ** while in the error state, hence it is impossible for this routine to
43942   ** be called in the error state.  Nevertheless, we include a NEVER()
43943   ** test for the error state as a safeguard against future changes.
43944   */
43945   if( NEVER(pPager->errCode) ) return SQLITE_OK;
43946   testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
43947   testcase( pPager->doNotSpill & SPILLFLAG_OFF );
43948   testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
43949   if( pPager->doNotSpill
43950    && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
43951       || (pPg->flags & PGHDR_NEED_SYNC)!=0)
43952   ){
43953     return SQLITE_OK;
43954   }
43955 
43956   pPg->pDirty = 0;
43957   if( pagerUseWal(pPager) ){
43958     /* Write a single frame for this page to the log. */
43959     if( subjRequiresPage(pPg) ){
43960       rc = subjournalPage(pPg);
43961     }
43962     if( rc==SQLITE_OK ){
43963       rc = pagerWalFrames(pPager, pPg, 0, 0);
43964     }
43965   }else{
43966 
43967     /* Sync the journal file if required. */
43968     if( pPg->flags&PGHDR_NEED_SYNC
43969      || pPager->eState==PAGER_WRITER_CACHEMOD
43970     ){
43971       rc = syncJournal(pPager, 1);
43972     }
43973 
43974     /* If the page number of this page is larger than the current size of
43975     ** the database image, it may need to be written to the sub-journal.
43976     ** This is because the call to pager_write_pagelist() below will not
43977     ** actually write data to the file in this case.
43978     **
43979     ** Consider the following sequence of events:
43980     **
43981     **   BEGIN;
43982     **     <journal page X>
43983     **     <modify page X>
43984     **     SAVEPOINT sp;
43985     **       <shrink database file to Y pages>
43986     **       pagerStress(page X)
43987     **     ROLLBACK TO sp;
43988     **
43989     ** If (X>Y), then when pagerStress is called page X will not be written
43990     ** out to the database file, but will be dropped from the cache. Then,
43991     ** following the "ROLLBACK TO sp" statement, reading page X will read
43992     ** data from the database file. This will be the copy of page X as it
43993     ** was when the transaction started, not as it was when "SAVEPOINT sp"
43994     ** was executed.
43995     **
43996     ** The solution is to write the current data for page X into the
43997     ** sub-journal file now (if it is not already there), so that it will
43998     ** be restored to its current value when the "ROLLBACK TO sp" is
43999     ** executed.
44000     */
44001     if( NEVER(
44002         rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
44003     ) ){
44004       rc = subjournalPage(pPg);
44005     }
44006 
44007     /* Write the contents of the page out to the database file. */
44008     if( rc==SQLITE_OK ){
44009       assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
44010       rc = pager_write_pagelist(pPager, pPg);
44011     }
44012   }
44013 
44014   /* Mark the page as clean. */
44015   if( rc==SQLITE_OK ){
44016     PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
44017     sqlite3PcacheMakeClean(pPg);
44018   }
44019 
44020   return pager_error(pPager, rc);
44021 }
44022 
44023 
44024 /*
44025 ** Allocate and initialize a new Pager object and put a pointer to it
44026 ** in *ppPager. The pager should eventually be freed by passing it
44027 ** to sqlite3PagerClose().
44028 **
44029 ** The zFilename argument is the path to the database file to open.
44030 ** If zFilename is NULL then a randomly-named temporary file is created
44031 ** and used as the file to be cached. Temporary files are be deleted
44032 ** automatically when they are closed. If zFilename is ":memory:" then
44033 ** all information is held in cache. It is never written to disk.
44034 ** This can be used to implement an in-memory database.
44035 **
44036 ** The nExtra parameter specifies the number of bytes of space allocated
44037 ** along with each page reference. This space is available to the user
44038 ** via the sqlite3PagerGetExtra() API.
44039 **
44040 ** The flags argument is used to specify properties that affect the
44041 ** operation of the pager. It should be passed some bitwise combination
44042 ** of the PAGER_* flags.
44043 **
44044 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
44045 ** of the xOpen() method of the supplied VFS when opening files.
44046 **
44047 ** If the pager object is allocated and the specified file opened
44048 ** successfully, SQLITE_OK is returned and *ppPager set to point to
44049 ** the new pager object. If an error occurs, *ppPager is set to NULL
44050 ** and error code returned. This function may return SQLITE_NOMEM
44051 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
44052 ** various SQLITE_IO_XXX errors.
44053 */
44054 SQLITE_PRIVATE int sqlite3PagerOpen(
44055   sqlite3_vfs *pVfs,       /* The virtual file system to use */
44056   Pager **ppPager,         /* OUT: Return the Pager structure here */
44057   const char *zFilename,   /* Name of the database file to open */
44058   int nExtra,              /* Extra bytes append to each in-memory page */
44059   int flags,               /* flags controlling this file */
44060   int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
44061   void (*xReinit)(DbPage*) /* Function to reinitialize pages */
44062 ){
44063   u8 *pPtr;
44064   Pager *pPager = 0;       /* Pager object to allocate and return */
44065   int rc = SQLITE_OK;      /* Return code */
44066   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
44067   int memDb = 0;           /* True if this is an in-memory file */
44068   int readOnly = 0;        /* True if this is a read-only file */
44069   int journalFileSize;     /* Bytes to allocate for each journal fd */
44070   char *zPathname = 0;     /* Full path to database file */
44071   int nPathname = 0;       /* Number of bytes in zPathname */
44072   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
44073   int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
44074   u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
44075   const char *zUri = 0;    /* URI args to copy */
44076   int nUri = 0;            /* Number of bytes of URI args at *zUri */
44077 
44078   /* Figure out how much space is required for each journal file-handle
44079   ** (there are two of them, the main journal and the sub-journal). This
44080   ** is the maximum space required for an in-memory journal file handle
44081   ** and a regular journal file-handle. Note that a "regular journal-handle"
44082   ** may be a wrapper capable of caching the first portion of the journal
44083   ** file in memory to implement the atomic-write optimization (see
44084   ** source file journal.c).
44085   */
44086   if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
44087     journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
44088   }else{
44089     journalFileSize = ROUND8(sqlite3MemJournalSize());
44090   }
44091 
44092   /* Set the output variable to NULL in case an error occurs. */
44093   *ppPager = 0;
44094 
44095 #ifndef SQLITE_OMIT_MEMORYDB
44096   if( flags & PAGER_MEMORY ){
44097     memDb = 1;
44098     if( zFilename && zFilename[0] ){
44099       zPathname = sqlite3DbStrDup(0, zFilename);
44100       if( zPathname==0  ) return SQLITE_NOMEM;
44101       nPathname = sqlite3Strlen30(zPathname);
44102       zFilename = 0;
44103     }
44104   }
44105 #endif
44106 
44107   /* Compute and store the full pathname in an allocated buffer pointed
44108   ** to by zPathname, length nPathname. Or, if this is a temporary file,
44109   ** leave both nPathname and zPathname set to 0.
44110   */
44111   if( zFilename && zFilename[0] ){
44112     const char *z;
44113     nPathname = pVfs->mxPathname+1;
44114     zPathname = sqlite3DbMallocRaw(0, nPathname*2);
44115     if( zPathname==0 ){
44116       return SQLITE_NOMEM;
44117     }
44118     zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
44119     rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
44120     nPathname = sqlite3Strlen30(zPathname);
44121     z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
44122     while( *z ){
44123       z += sqlite3Strlen30(z)+1;
44124       z += sqlite3Strlen30(z)+1;
44125     }
44126     nUri = (int)(&z[1] - zUri);
44127     assert( nUri>=0 );
44128     if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
44129       /* This branch is taken when the journal path required by
44130       ** the database being opened will be more than pVfs->mxPathname
44131       ** bytes in length. This means the database cannot be opened,
44132       ** as it will not be possible to open the journal file or even
44133       ** check for a hot-journal before reading.
44134       */
44135       rc = SQLITE_CANTOPEN_BKPT;
44136     }
44137     if( rc!=SQLITE_OK ){
44138       sqlite3DbFree(0, zPathname);
44139       return rc;
44140     }
44141   }
44142 
44143   /* Allocate memory for the Pager structure, PCache object, the
44144   ** three file descriptors, the database file name and the journal
44145   ** file name. The layout in memory is as follows:
44146   **
44147   **     Pager object                    (sizeof(Pager) bytes)
44148   **     PCache object                   (sqlite3PcacheSize() bytes)
44149   **     Database file handle            (pVfs->szOsFile bytes)
44150   **     Sub-journal file handle         (journalFileSize bytes)
44151   **     Main journal file handle        (journalFileSize bytes)
44152   **     Database file name              (nPathname+1 bytes)
44153   **     Journal file name               (nPathname+8+1 bytes)
44154   */
44155   pPtr = (u8 *)sqlite3MallocZero(
44156     ROUND8(sizeof(*pPager)) +      /* Pager structure */
44157     ROUND8(pcacheSize) +           /* PCache object */
44158     ROUND8(pVfs->szOsFile) +       /* The main db file */
44159     journalFileSize * 2 +          /* The two journal files */
44160     nPathname + 1 + nUri +         /* zFilename */
44161     nPathname + 8 + 2              /* zJournal */
44162 #ifndef SQLITE_OMIT_WAL
44163     + nPathname + 4 + 2            /* zWal */
44164 #endif
44165   );
44166   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
44167   if( !pPtr ){
44168     sqlite3DbFree(0, zPathname);
44169     return SQLITE_NOMEM;
44170   }
44171   pPager =              (Pager*)(pPtr);
44172   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
44173   pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
44174   pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
44175   pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
44176   pPager->zFilename =    (char*)(pPtr += journalFileSize);
44177   assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
44178 
44179   /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
44180   if( zPathname ){
44181     assert( nPathname>0 );
44182     pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
44183     memcpy(pPager->zFilename, zPathname, nPathname);
44184     if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
44185     memcpy(pPager->zJournal, zPathname, nPathname);
44186     memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
44187     sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
44188 #ifndef SQLITE_OMIT_WAL
44189     pPager->zWal = &pPager->zJournal[nPathname+8+1];
44190     memcpy(pPager->zWal, zPathname, nPathname);
44191     memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
44192     sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
44193 #endif
44194     sqlite3DbFree(0, zPathname);
44195   }
44196   pPager->pVfs = pVfs;
44197   pPager->vfsFlags = vfsFlags;
44198 
44199   /* Open the pager file.
44200   */
44201   if( zFilename && zFilename[0] ){
44202     int fout = 0;                    /* VFS flags returned by xOpen() */
44203     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
44204     assert( !memDb );
44205     readOnly = (fout&SQLITE_OPEN_READONLY);
44206 
44207     /* If the file was successfully opened for read/write access,
44208     ** choose a default page size in case we have to create the
44209     ** database file. The default page size is the maximum of:
44210     **
44211     **    + SQLITE_DEFAULT_PAGE_SIZE,
44212     **    + The value returned by sqlite3OsSectorSize()
44213     **    + The largest page size that can be written atomically.
44214     */
44215     if( rc==SQLITE_OK && !readOnly ){
44216       setSectorSize(pPager);
44217       assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
44218       if( szPageDflt<pPager->sectorSize ){
44219         if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
44220           szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
44221         }else{
44222           szPageDflt = (u32)pPager->sectorSize;
44223         }
44224       }
44225 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
44226       {
44227         int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
44228         int ii;
44229         assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
44230         assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
44231         assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
44232         for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
44233           if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
44234             szPageDflt = ii;
44235           }
44236         }
44237       }
44238 #endif
44239     }
44240   }else{
44241     /* If a temporary file is requested, it is not opened immediately.
44242     ** In this case we accept the default page size and delay actually
44243     ** opening the file until the first call to OsWrite().
44244     **
44245     ** This branch is also run for an in-memory database. An in-memory
44246     ** database is the same as a temp-file that is never written out to
44247     ** disk and uses an in-memory rollback journal.
44248     */
44249     tempFile = 1;
44250     pPager->eState = PAGER_READER;
44251     pPager->eLock = EXCLUSIVE_LOCK;
44252     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
44253   }
44254 
44255   /* The following call to PagerSetPagesize() serves to set the value of
44256   ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
44257   */
44258   if( rc==SQLITE_OK ){
44259     assert( pPager->memDb==0 );
44260     rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
44261     testcase( rc!=SQLITE_OK );
44262   }
44263 
44264   /* If an error occurred in either of the blocks above, free the
44265   ** Pager structure and close the file.
44266   */
44267   if( rc!=SQLITE_OK ){
44268     assert( !pPager->pTmpSpace );
44269     sqlite3OsClose(pPager->fd);
44270     sqlite3_free(pPager);
44271     return rc;
44272   }
44273 
44274   /* Initialize the PCache object. */
44275   assert( nExtra<1000 );
44276   nExtra = ROUND8(nExtra);
44277   sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
44278                     !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
44279 
44280   PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
44281   IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
44282 
44283   pPager->useJournal = (u8)useJournal;
44284   /* pPager->stmtOpen = 0; */
44285   /* pPager->stmtInUse = 0; */
44286   /* pPager->nRef = 0; */
44287   /* pPager->stmtSize = 0; */
44288   /* pPager->stmtJSize = 0; */
44289   /* pPager->nPage = 0; */
44290   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
44291   /* pPager->state = PAGER_UNLOCK; */
44292 #if 0
44293   assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
44294 #endif
44295   /* pPager->errMask = 0; */
44296   pPager->tempFile = (u8)tempFile;
44297   assert( tempFile==PAGER_LOCKINGMODE_NORMAL
44298           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
44299   assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
44300   pPager->exclusiveMode = (u8)tempFile;
44301   pPager->changeCountDone = pPager->tempFile;
44302   pPager->memDb = (u8)memDb;
44303   pPager->readOnly = (u8)readOnly;
44304   assert( useJournal || pPager->tempFile );
44305   pPager->noSync = pPager->tempFile;
44306   if( pPager->noSync ){
44307     assert( pPager->fullSync==0 );
44308     assert( pPager->syncFlags==0 );
44309     assert( pPager->walSyncFlags==0 );
44310     assert( pPager->ckptSyncFlags==0 );
44311   }else{
44312     pPager->fullSync = 1;
44313     pPager->syncFlags = SQLITE_SYNC_NORMAL;
44314     pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
44315     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
44316   }
44317   /* pPager->pFirst = 0; */
44318   /* pPager->pFirstSynced = 0; */
44319   /* pPager->pLast = 0; */
44320   pPager->nExtra = (u16)nExtra;
44321   pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
44322   assert( isOpen(pPager->fd) || tempFile );
44323   setSectorSize(pPager);
44324   if( !useJournal ){
44325     pPager->journalMode = PAGER_JOURNALMODE_OFF;
44326   }else if( memDb ){
44327     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
44328   }
44329   /* pPager->xBusyHandler = 0; */
44330   /* pPager->pBusyHandlerArg = 0; */
44331   pPager->xReiniter = xReinit;
44332   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
44333   /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
44334 
44335   *ppPager = pPager;
44336   return SQLITE_OK;
44337 }
44338 
44339 
44340 /* Verify that the database file has not be deleted or renamed out from
44341 ** under the pager.  Return SQLITE_OK if the database is still were it ought
44342 ** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
44343 ** code from sqlite3OsAccess()) if the database has gone missing.
44344 */
44345 static int databaseIsUnmoved(Pager *pPager){
44346   int bHasMoved = 0;
44347   int rc;
44348 
44349   if( pPager->tempFile ) return SQLITE_OK;
44350   if( pPager->dbSize==0 ) return SQLITE_OK;
44351   assert( pPager->zFilename && pPager->zFilename[0] );
44352   rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
44353   if( rc==SQLITE_NOTFOUND ){
44354     /* If the HAS_MOVED file-control is unimplemented, assume that the file
44355     ** has not been moved.  That is the historical behavior of SQLite: prior to
44356     ** version 3.8.3, it never checked */
44357     rc = SQLITE_OK;
44358   }else if( rc==SQLITE_OK && bHasMoved ){
44359     rc = SQLITE_READONLY_DBMOVED;
44360   }
44361   return rc;
44362 }
44363 
44364 
44365 /*
44366 ** This function is called after transitioning from PAGER_UNLOCK to
44367 ** PAGER_SHARED state. It tests if there is a hot journal present in
44368 ** the file-system for the given pager. A hot journal is one that
44369 ** needs to be played back. According to this function, a hot-journal
44370 ** file exists if the following criteria are met:
44371 **
44372 **   * The journal file exists in the file system, and
44373 **   * No process holds a RESERVED or greater lock on the database file, and
44374 **   * The database file itself is greater than 0 bytes in size, and
44375 **   * The first byte of the journal file exists and is not 0x00.
44376 **
44377 ** If the current size of the database file is 0 but a journal file
44378 ** exists, that is probably an old journal left over from a prior
44379 ** database with the same name. In this case the journal file is
44380 ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
44381 ** is returned.
44382 **
44383 ** This routine does not check if there is a master journal filename
44384 ** at the end of the file. If there is, and that master journal file
44385 ** does not exist, then the journal file is not really hot. In this
44386 ** case this routine will return a false-positive. The pager_playback()
44387 ** routine will discover that the journal file is not really hot and
44388 ** will not roll it back.
44389 **
44390 ** If a hot-journal file is found to exist, *pExists is set to 1 and
44391 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
44392 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
44393 ** to determine whether or not a hot-journal file exists, the IO error
44394 ** code is returned and the value of *pExists is undefined.
44395 */
44396 static int hasHotJournal(Pager *pPager, int *pExists){
44397   sqlite3_vfs * const pVfs = pPager->pVfs;
44398   int rc = SQLITE_OK;           /* Return code */
44399   int exists = 1;               /* True if a journal file is present */
44400   int jrnlOpen = !!isOpen(pPager->jfd);
44401 
44402   assert( pPager->useJournal );
44403   assert( isOpen(pPager->fd) );
44404   assert( pPager->eState==PAGER_OPEN );
44405 
44406   assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
44407     SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
44408   ));
44409 
44410   *pExists = 0;
44411   if( !jrnlOpen ){
44412     rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
44413   }
44414   if( rc==SQLITE_OK && exists ){
44415     int locked = 0;             /* True if some process holds a RESERVED lock */
44416 
44417     /* Race condition here:  Another process might have been holding the
44418     ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
44419     ** call above, but then delete the journal and drop the lock before
44420     ** we get to the following sqlite3OsCheckReservedLock() call.  If that
44421     ** is the case, this routine might think there is a hot journal when
44422     ** in fact there is none.  This results in a false-positive which will
44423     ** be dealt with by the playback routine.  Ticket #3883.
44424     */
44425     rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
44426     if( rc==SQLITE_OK && !locked ){
44427       Pgno nPage;                 /* Number of pages in database file */
44428 
44429       rc = pagerPagecount(pPager, &nPage);
44430       if( rc==SQLITE_OK ){
44431         /* If the database is zero pages in size, that means that either (1) the
44432         ** journal is a remnant from a prior database with the same name where
44433         ** the database file but not the journal was deleted, or (2) the initial
44434         ** transaction that populates a new database is being rolled back.
44435         ** In either case, the journal file can be deleted.  However, take care
44436         ** not to delete the journal file if it is already open due to
44437         ** journal_mode=PERSIST.
44438         */
44439         if( nPage==0 && !jrnlOpen ){
44440           sqlite3BeginBenignMalloc();
44441           if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
44442             sqlite3OsDelete(pVfs, pPager->zJournal, 0);
44443             if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
44444           }
44445           sqlite3EndBenignMalloc();
44446         }else{
44447           /* The journal file exists and no other connection has a reserved
44448           ** or greater lock on the database file. Now check that there is
44449           ** at least one non-zero bytes at the start of the journal file.
44450           ** If there is, then we consider this journal to be hot. If not,
44451           ** it can be ignored.
44452           */
44453           if( !jrnlOpen ){
44454             int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
44455             rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
44456           }
44457           if( rc==SQLITE_OK ){
44458             u8 first = 0;
44459             rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
44460             if( rc==SQLITE_IOERR_SHORT_READ ){
44461               rc = SQLITE_OK;
44462             }
44463             if( !jrnlOpen ){
44464               sqlite3OsClose(pPager->jfd);
44465             }
44466             *pExists = (first!=0);
44467           }else if( rc==SQLITE_CANTOPEN ){
44468             /* If we cannot open the rollback journal file in order to see if
44469             ** its has a zero header, that might be due to an I/O error, or
44470             ** it might be due to the race condition described above and in
44471             ** ticket #3883.  Either way, assume that the journal is hot.
44472             ** This might be a false positive.  But if it is, then the
44473             ** automatic journal playback and recovery mechanism will deal
44474             ** with it under an EXCLUSIVE lock where we do not need to
44475             ** worry so much with race conditions.
44476             */
44477             *pExists = 1;
44478             rc = SQLITE_OK;
44479           }
44480         }
44481       }
44482     }
44483   }
44484 
44485   return rc;
44486 }
44487 
44488 /*
44489 ** This function is called to obtain a shared lock on the database file.
44490 ** It is illegal to call sqlite3PagerAcquire() until after this function
44491 ** has been successfully called. If a shared-lock is already held when
44492 ** this function is called, it is a no-op.
44493 **
44494 ** The following operations are also performed by this function.
44495 **
44496 **   1) If the pager is currently in PAGER_OPEN state (no lock held
44497 **      on the database file), then an attempt is made to obtain a
44498 **      SHARED lock on the database file. Immediately after obtaining
44499 **      the SHARED lock, the file-system is checked for a hot-journal,
44500 **      which is played back if present. Following any hot-journal
44501 **      rollback, the contents of the cache are validated by checking
44502 **      the 'change-counter' field of the database file header and
44503 **      discarded if they are found to be invalid.
44504 **
44505 **   2) If the pager is running in exclusive-mode, and there are currently
44506 **      no outstanding references to any pages, and is in the error state,
44507 **      then an attempt is made to clear the error state by discarding
44508 **      the contents of the page cache and rolling back any open journal
44509 **      file.
44510 **
44511 ** If everything is successful, SQLITE_OK is returned. If an IO error
44512 ** occurs while locking the database, checking for a hot-journal file or
44513 ** rolling back a journal file, the IO error code is returned.
44514 */
44515 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
44516   int rc = SQLITE_OK;                /* Return code */
44517 
44518   /* This routine is only called from b-tree and only when there are no
44519   ** outstanding pages. This implies that the pager state should either
44520   ** be OPEN or READER. READER is only possible if the pager is or was in
44521   ** exclusive access mode.
44522   */
44523   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
44524   assert( assert_pager_state(pPager) );
44525   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
44526   if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
44527 
44528   if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
44529     int bHotJournal = 1;          /* True if there exists a hot journal-file */
44530 
44531     assert( !MEMDB );
44532 
44533     rc = pager_wait_on_lock(pPager, SHARED_LOCK);
44534     if( rc!=SQLITE_OK ){
44535       assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
44536       goto failed;
44537     }
44538 
44539     /* If a journal file exists, and there is no RESERVED lock on the
44540     ** database file, then it either needs to be played back or deleted.
44541     */
44542     if( pPager->eLock<=SHARED_LOCK ){
44543       rc = hasHotJournal(pPager, &bHotJournal);
44544     }
44545     if( rc!=SQLITE_OK ){
44546       goto failed;
44547     }
44548     if( bHotJournal ){
44549       if( pPager->readOnly ){
44550         rc = SQLITE_READONLY_ROLLBACK;
44551         goto failed;
44552       }
44553 
44554       /* Get an EXCLUSIVE lock on the database file. At this point it is
44555       ** important that a RESERVED lock is not obtained on the way to the
44556       ** EXCLUSIVE lock. If it were, another process might open the
44557       ** database file, detect the RESERVED lock, and conclude that the
44558       ** database is safe to read while this process is still rolling the
44559       ** hot-journal back.
44560       **
44561       ** Because the intermediate RESERVED lock is not requested, any
44562       ** other process attempting to access the database file will get to
44563       ** this point in the code and fail to obtain its own EXCLUSIVE lock
44564       ** on the database file.
44565       **
44566       ** Unless the pager is in locking_mode=exclusive mode, the lock is
44567       ** downgraded to SHARED_LOCK before this function returns.
44568       */
44569       rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
44570       if( rc!=SQLITE_OK ){
44571         goto failed;
44572       }
44573 
44574       /* If it is not already open and the file exists on disk, open the
44575       ** journal for read/write access. Write access is required because
44576       ** in exclusive-access mode the file descriptor will be kept open
44577       ** and possibly used for a transaction later on. Also, write-access
44578       ** is usually required to finalize the journal in journal_mode=persist
44579       ** mode (and also for journal_mode=truncate on some systems).
44580       **
44581       ** If the journal does not exist, it usually means that some
44582       ** other connection managed to get in and roll it back before
44583       ** this connection obtained the exclusive lock above. Or, it
44584       ** may mean that the pager was in the error-state when this
44585       ** function was called and the journal file does not exist.
44586       */
44587       if( !isOpen(pPager->jfd) ){
44588         sqlite3_vfs * const pVfs = pPager->pVfs;
44589         int bExists;              /* True if journal file exists */
44590         rc = sqlite3OsAccess(
44591             pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
44592         if( rc==SQLITE_OK && bExists ){
44593           int fout = 0;
44594           int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
44595           assert( !pPager->tempFile );
44596           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
44597           assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
44598           if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
44599             rc = SQLITE_CANTOPEN_BKPT;
44600             sqlite3OsClose(pPager->jfd);
44601           }
44602         }
44603       }
44604 
44605       /* Playback and delete the journal.  Drop the database write
44606       ** lock and reacquire the read lock. Purge the cache before
44607       ** playing back the hot-journal so that we don't end up with
44608       ** an inconsistent cache.  Sync the hot journal before playing
44609       ** it back since the process that crashed and left the hot journal
44610       ** probably did not sync it and we are required to always sync
44611       ** the journal before playing it back.
44612       */
44613       if( isOpen(pPager->jfd) ){
44614         assert( rc==SQLITE_OK );
44615         rc = pagerSyncHotJournal(pPager);
44616         if( rc==SQLITE_OK ){
44617           rc = pager_playback(pPager, 1);
44618           pPager->eState = PAGER_OPEN;
44619         }
44620       }else if( !pPager->exclusiveMode ){
44621         pagerUnlockDb(pPager, SHARED_LOCK);
44622       }
44623 
44624       if( rc!=SQLITE_OK ){
44625         /* This branch is taken if an error occurs while trying to open
44626         ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
44627         ** pager_unlock() routine will be called before returning to unlock
44628         ** the file. If the unlock attempt fails, then Pager.eLock must be
44629         ** set to UNKNOWN_LOCK (see the comment above the #define for
44630         ** UNKNOWN_LOCK above for an explanation).
44631         **
44632         ** In order to get pager_unlock() to do this, set Pager.eState to
44633         ** PAGER_ERROR now. This is not actually counted as a transition
44634         ** to ERROR state in the state diagram at the top of this file,
44635         ** since we know that the same call to pager_unlock() will very
44636         ** shortly transition the pager object to the OPEN state. Calling
44637         ** assert_pager_state() would fail now, as it should not be possible
44638         ** to be in ERROR state when there are zero outstanding page
44639         ** references.
44640         */
44641         pager_error(pPager, rc);
44642         goto failed;
44643       }
44644 
44645       assert( pPager->eState==PAGER_OPEN );
44646       assert( (pPager->eLock==SHARED_LOCK)
44647            || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
44648       );
44649     }
44650 
44651     if( !pPager->tempFile && (
44652         pPager->pBackup
44653      || sqlite3PcachePagecount(pPager->pPCache)>0
44654      || USEFETCH(pPager)
44655     )){
44656       /* The shared-lock has just been acquired on the database file
44657       ** and there are already pages in the cache (from a previous
44658       ** read or write transaction).  Check to see if the database
44659       ** has been modified.  If the database has changed, flush the
44660       ** cache.
44661       **
44662       ** Database changes is detected by looking at 15 bytes beginning
44663       ** at offset 24 into the file.  The first 4 of these 16 bytes are
44664       ** a 32-bit counter that is incremented with each change.  The
44665       ** other bytes change randomly with each file change when
44666       ** a codec is in use.
44667       **
44668       ** There is a vanishingly small chance that a change will not be
44669       ** detected.  The chance of an undetected change is so small that
44670       ** it can be neglected.
44671       */
44672       Pgno nPage = 0;
44673       char dbFileVers[sizeof(pPager->dbFileVers)];
44674 
44675       rc = pagerPagecount(pPager, &nPage);
44676       if( rc ) goto failed;
44677 
44678       if( nPage>0 ){
44679         IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
44680         rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
44681         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
44682           goto failed;
44683         }
44684       }else{
44685         memset(dbFileVers, 0, sizeof(dbFileVers));
44686       }
44687 
44688       if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
44689         pager_reset(pPager);
44690 
44691         /* Unmap the database file. It is possible that external processes
44692         ** may have truncated the database file and then extended it back
44693         ** to its original size while this process was not holding a lock.
44694         ** In this case there may exist a Pager.pMap mapping that appears
44695         ** to be the right size but is not actually valid. Avoid this
44696         ** possibility by unmapping the db here. */
44697         if( USEFETCH(pPager) ){
44698           sqlite3OsUnfetch(pPager->fd, 0, 0);
44699         }
44700       }
44701     }
44702 
44703     /* If there is a WAL file in the file-system, open this database in WAL
44704     ** mode. Otherwise, the following function call is a no-op.
44705     */
44706     rc = pagerOpenWalIfPresent(pPager);
44707 #ifndef SQLITE_OMIT_WAL
44708     assert( pPager->pWal==0 || rc==SQLITE_OK );
44709 #endif
44710   }
44711 
44712   if( pagerUseWal(pPager) ){
44713     assert( rc==SQLITE_OK );
44714     rc = pagerBeginReadTransaction(pPager);
44715   }
44716 
44717   if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
44718     rc = pagerPagecount(pPager, &pPager->dbSize);
44719   }
44720 
44721  failed:
44722   if( rc!=SQLITE_OK ){
44723     assert( !MEMDB );
44724     pager_unlock(pPager);
44725     assert( pPager->eState==PAGER_OPEN );
44726   }else{
44727     pPager->eState = PAGER_READER;
44728   }
44729   return rc;
44730 }
44731 
44732 /*
44733 ** If the reference count has reached zero, rollback any active
44734 ** transaction and unlock the pager.
44735 **
44736 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
44737 ** the rollback journal, the unlock is not performed and there is
44738 ** nothing to rollback, so this routine is a no-op.
44739 */
44740 static void pagerUnlockIfUnused(Pager *pPager){
44741   if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
44742     pagerUnlockAndRollback(pPager);
44743   }
44744 }
44745 
44746 /*
44747 ** Acquire a reference to page number pgno in pager pPager (a page
44748 ** reference has type DbPage*). If the requested reference is
44749 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
44750 **
44751 ** If the requested page is already in the cache, it is returned.
44752 ** Otherwise, a new page object is allocated and populated with data
44753 ** read from the database file. In some cases, the pcache module may
44754 ** choose not to allocate a new page object and may reuse an existing
44755 ** object with no outstanding references.
44756 **
44757 ** The extra data appended to a page is always initialized to zeros the
44758 ** first time a page is loaded into memory. If the page requested is
44759 ** already in the cache when this function is called, then the extra
44760 ** data is left as it was when the page object was last used.
44761 **
44762 ** If the database image is smaller than the requested page or if a
44763 ** non-zero value is passed as the noContent parameter and the
44764 ** requested page is not already stored in the cache, then no
44765 ** actual disk read occurs. In this case the memory image of the
44766 ** page is initialized to all zeros.
44767 **
44768 ** If noContent is true, it means that we do not care about the contents
44769 ** of the page. This occurs in two scenarios:
44770 **
44771 **   a) When reading a free-list leaf page from the database, and
44772 **
44773 **   b) When a savepoint is being rolled back and we need to load
44774 **      a new page into the cache to be filled with the data read
44775 **      from the savepoint journal.
44776 **
44777 ** If noContent is true, then the data returned is zeroed instead of
44778 ** being read from the database. Additionally, the bits corresponding
44779 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
44780 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
44781 ** savepoints are set. This means if the page is made writable at any
44782 ** point in the future, using a call to sqlite3PagerWrite(), its contents
44783 ** will not be journaled. This saves IO.
44784 **
44785 ** The acquisition might fail for several reasons.  In all cases,
44786 ** an appropriate error code is returned and *ppPage is set to NULL.
44787 **
44788 ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
44789 ** to find a page in the in-memory cache first.  If the page is not already
44790 ** in memory, this routine goes to disk to read it in whereas Lookup()
44791 ** just returns 0.  This routine acquires a read-lock the first time it
44792 ** has to go to disk, and could also playback an old journal if necessary.
44793 ** Since Lookup() never goes to disk, it never has to deal with locks
44794 ** or journal files.
44795 */
44796 SQLITE_PRIVATE int sqlite3PagerAcquire(
44797   Pager *pPager,      /* The pager open on the database file */
44798   Pgno pgno,          /* Page number to fetch */
44799   DbPage **ppPage,    /* Write a pointer to the page here */
44800   int flags           /* PAGER_GET_XXX flags */
44801 ){
44802   int rc = SQLITE_OK;
44803   PgHdr *pPg = 0;
44804   u32 iFrame = 0;                 /* Frame to read from WAL file */
44805   const int noContent = (flags & PAGER_GET_NOCONTENT);
44806 
44807   /* It is acceptable to use a read-only (mmap) page for any page except
44808   ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
44809   ** flag was specified by the caller. And so long as the db is not a
44810   ** temporary or in-memory database.  */
44811   const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
44812    && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
44813 #ifdef SQLITE_HAS_CODEC
44814    && pPager->xCodec==0
44815 #endif
44816   );
44817 
44818   assert( pPager->eState>=PAGER_READER );
44819   assert( assert_pager_state(pPager) );
44820   assert( noContent==0 || bMmapOk==0 );
44821 
44822   if( pgno==0 ){
44823     return SQLITE_CORRUPT_BKPT;
44824   }
44825 
44826   /* If the pager is in the error state, return an error immediately.
44827   ** Otherwise, request the page from the PCache layer. */
44828   if( pPager->errCode!=SQLITE_OK ){
44829     rc = pPager->errCode;
44830   }else{
44831 
44832     if( bMmapOk && pagerUseWal(pPager) ){
44833       rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
44834       if( rc!=SQLITE_OK ) goto pager_acquire_err;
44835     }
44836 
44837     if( bMmapOk && iFrame==0 ){
44838       void *pData = 0;
44839 
44840       rc = sqlite3OsFetch(pPager->fd,
44841           (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
44842       );
44843 
44844       if( rc==SQLITE_OK && pData ){
44845         if( pPager->eState>PAGER_READER ){
44846           (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
44847         }
44848         if( pPg==0 ){
44849           rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
44850         }else{
44851           sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
44852         }
44853         if( pPg ){
44854           assert( rc==SQLITE_OK );
44855           *ppPage = pPg;
44856           return SQLITE_OK;
44857         }
44858       }
44859       if( rc!=SQLITE_OK ){
44860         goto pager_acquire_err;
44861       }
44862     }
44863 
44864     rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
44865   }
44866 
44867   if( rc!=SQLITE_OK ){
44868     /* Either the call to sqlite3PcacheFetch() returned an error or the
44869     ** pager was already in the error-state when this function was called.
44870     ** Set pPg to 0 and jump to the exception handler.  */
44871     pPg = 0;
44872     goto pager_acquire_err;
44873   }
44874   assert( (*ppPage)->pgno==pgno );
44875   assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
44876 
44877   if( (*ppPage)->pPager && !noContent ){
44878     /* In this case the pcache already contains an initialized copy of
44879     ** the page. Return without further ado.  */
44880     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
44881     pPager->aStat[PAGER_STAT_HIT]++;
44882     return SQLITE_OK;
44883 
44884   }else{
44885     /* The pager cache has created a new page. Its content needs to
44886     ** be initialized.  */
44887 
44888     pPg = *ppPage;
44889     pPg->pPager = pPager;
44890 
44891     /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
44892     ** number greater than this, or the unused locking-page, is requested. */
44893     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
44894       rc = SQLITE_CORRUPT_BKPT;
44895       goto pager_acquire_err;
44896     }
44897 
44898     if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
44899       if( pgno>pPager->mxPgno ){
44900         rc = SQLITE_FULL;
44901         goto pager_acquire_err;
44902       }
44903       if( noContent ){
44904         /* Failure to set the bits in the InJournal bit-vectors is benign.
44905         ** It merely means that we might do some extra work to journal a
44906         ** page that does not need to be journaled.  Nevertheless, be sure
44907         ** to test the case where a malloc error occurs while trying to set
44908         ** a bit in a bit vector.
44909         */
44910         sqlite3BeginBenignMalloc();
44911         if( pgno<=pPager->dbOrigSize ){
44912           TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
44913           testcase( rc==SQLITE_NOMEM );
44914         }
44915         TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
44916         testcase( rc==SQLITE_NOMEM );
44917         sqlite3EndBenignMalloc();
44918       }
44919       memset(pPg->pData, 0, pPager->pageSize);
44920       IOTRACE(("ZERO %p %d\n", pPager, pgno));
44921     }else{
44922       if( pagerUseWal(pPager) && bMmapOk==0 ){
44923         rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
44924         if( rc!=SQLITE_OK ) goto pager_acquire_err;
44925       }
44926       assert( pPg->pPager==pPager );
44927       pPager->aStat[PAGER_STAT_MISS]++;
44928       rc = readDbPage(pPg, iFrame);
44929       if( rc!=SQLITE_OK ){
44930         goto pager_acquire_err;
44931       }
44932     }
44933     pager_set_pagehash(pPg);
44934   }
44935 
44936   return SQLITE_OK;
44937 
44938 pager_acquire_err:
44939   assert( rc!=SQLITE_OK );
44940   if( pPg ){
44941     sqlite3PcacheDrop(pPg);
44942   }
44943   pagerUnlockIfUnused(pPager);
44944 
44945   *ppPage = 0;
44946   return rc;
44947 }
44948 
44949 /*
44950 ** Acquire a page if it is already in the in-memory cache.  Do
44951 ** not read the page from disk.  Return a pointer to the page,
44952 ** or 0 if the page is not in cache.
44953 **
44954 ** See also sqlite3PagerGet().  The difference between this routine
44955 ** and sqlite3PagerGet() is that _get() will go to the disk and read
44956 ** in the page if the page is not already in cache.  This routine
44957 ** returns NULL if the page is not in cache or if a disk I/O error
44958 ** has ever happened.
44959 */
44960 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
44961   PgHdr *pPg = 0;
44962   assert( pPager!=0 );
44963   assert( pgno!=0 );
44964   assert( pPager->pPCache!=0 );
44965   assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
44966   sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
44967   return pPg;
44968 }
44969 
44970 /*
44971 ** Release a page reference.
44972 **
44973 ** If the number of references to the page drop to zero, then the
44974 ** page is added to the LRU list.  When all references to all pages
44975 ** are released, a rollback occurs and the lock on the database is
44976 ** removed.
44977 */
44978 SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
44979   Pager *pPager;
44980   assert( pPg!=0 );
44981   pPager = pPg->pPager;
44982   if( pPg->flags & PGHDR_MMAP ){
44983     pagerReleaseMapPage(pPg);
44984   }else{
44985     sqlite3PcacheRelease(pPg);
44986   }
44987   pagerUnlockIfUnused(pPager);
44988 }
44989 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
44990   if( pPg ) sqlite3PagerUnrefNotNull(pPg);
44991 }
44992 
44993 /*
44994 ** This function is called at the start of every write transaction.
44995 ** There must already be a RESERVED or EXCLUSIVE lock on the database
44996 ** file when this routine is called.
44997 **
44998 ** Open the journal file for pager pPager and write a journal header
44999 ** to the start of it. If there are active savepoints, open the sub-journal
45000 ** as well. This function is only used when the journal file is being
45001 ** opened to write a rollback log for a transaction. It is not used
45002 ** when opening a hot journal file to roll it back.
45003 **
45004 ** If the journal file is already open (as it may be in exclusive mode),
45005 ** then this function just writes a journal header to the start of the
45006 ** already open file.
45007 **
45008 ** Whether or not the journal file is opened by this function, the
45009 ** Pager.pInJournal bitvec structure is allocated.
45010 **
45011 ** Return SQLITE_OK if everything is successful. Otherwise, return
45012 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
45013 ** an IO error code if opening or writing the journal file fails.
45014 */
45015 static int pager_open_journal(Pager *pPager){
45016   int rc = SQLITE_OK;                        /* Return code */
45017   sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
45018 
45019   assert( pPager->eState==PAGER_WRITER_LOCKED );
45020   assert( assert_pager_state(pPager) );
45021   assert( pPager->pInJournal==0 );
45022 
45023   /* If already in the error state, this function is a no-op.  But on
45024   ** the other hand, this routine is never called if we are already in
45025   ** an error state. */
45026   if( NEVER(pPager->errCode) ) return pPager->errCode;
45027 
45028   if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
45029     pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
45030     if( pPager->pInJournal==0 ){
45031       return SQLITE_NOMEM;
45032     }
45033 
45034     /* Open the journal file if it is not already open. */
45035     if( !isOpen(pPager->jfd) ){
45036       if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
45037         sqlite3MemJournalOpen(pPager->jfd);
45038       }else{
45039         const int flags =                   /* VFS flags to open journal file */
45040           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
45041           (pPager->tempFile ?
45042             (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
45043             (SQLITE_OPEN_MAIN_JOURNAL)
45044           );
45045 
45046         /* Verify that the database still has the same name as it did when
45047         ** it was originally opened. */
45048         rc = databaseIsUnmoved(pPager);
45049         if( rc==SQLITE_OK ){
45050 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
45051           rc = sqlite3JournalOpen(
45052               pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
45053           );
45054 #else
45055           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
45056 #endif
45057         }
45058       }
45059       assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
45060     }
45061 
45062 
45063     /* Write the first journal header to the journal file and open
45064     ** the sub-journal if necessary.
45065     */
45066     if( rc==SQLITE_OK ){
45067       /* TODO: Check if all of these are really required. */
45068       pPager->nRec = 0;
45069       pPager->journalOff = 0;
45070       pPager->setMaster = 0;
45071       pPager->journalHdr = 0;
45072       rc = writeJournalHdr(pPager);
45073     }
45074   }
45075 
45076   if( rc!=SQLITE_OK ){
45077     sqlite3BitvecDestroy(pPager->pInJournal);
45078     pPager->pInJournal = 0;
45079   }else{
45080     assert( pPager->eState==PAGER_WRITER_LOCKED );
45081     pPager->eState = PAGER_WRITER_CACHEMOD;
45082   }
45083 
45084   return rc;
45085 }
45086 
45087 /*
45088 ** Begin a write-transaction on the specified pager object. If a
45089 ** write-transaction has already been opened, this function is a no-op.
45090 **
45091 ** If the exFlag argument is false, then acquire at least a RESERVED
45092 ** lock on the database file. If exFlag is true, then acquire at least
45093 ** an EXCLUSIVE lock. If such a lock is already held, no locking
45094 ** functions need be called.
45095 **
45096 ** If the subjInMemory argument is non-zero, then any sub-journal opened
45097 ** within this transaction will be opened as an in-memory file. This
45098 ** has no effect if the sub-journal is already opened (as it may be when
45099 ** running in exclusive mode) or if the transaction does not require a
45100 ** sub-journal. If the subjInMemory argument is zero, then any required
45101 ** sub-journal is implemented in-memory if pPager is an in-memory database,
45102 ** or using a temporary file otherwise.
45103 */
45104 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
45105   int rc = SQLITE_OK;
45106 
45107   if( pPager->errCode ) return pPager->errCode;
45108   assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
45109   pPager->subjInMemory = (u8)subjInMemory;
45110 
45111   if( ALWAYS(pPager->eState==PAGER_READER) ){
45112     assert( pPager->pInJournal==0 );
45113 
45114     if( pagerUseWal(pPager) ){
45115       /* If the pager is configured to use locking_mode=exclusive, and an
45116       ** exclusive lock on the database is not already held, obtain it now.
45117       */
45118       if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
45119         rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
45120         if( rc!=SQLITE_OK ){
45121           return rc;
45122         }
45123         sqlite3WalExclusiveMode(pPager->pWal, 1);
45124       }
45125 
45126       /* Grab the write lock on the log file. If successful, upgrade to
45127       ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
45128       ** The busy-handler is not invoked if another connection already
45129       ** holds the write-lock. If possible, the upper layer will call it.
45130       */
45131       rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
45132     }else{
45133       /* Obtain a RESERVED lock on the database file. If the exFlag parameter
45134       ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
45135       ** busy-handler callback can be used when upgrading to the EXCLUSIVE
45136       ** lock, but not when obtaining the RESERVED lock.
45137       */
45138       rc = pagerLockDb(pPager, RESERVED_LOCK);
45139       if( rc==SQLITE_OK && exFlag ){
45140         rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
45141       }
45142     }
45143 
45144     if( rc==SQLITE_OK ){
45145       /* Change to WRITER_LOCKED state.
45146       **
45147       ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
45148       ** when it has an open transaction, but never to DBMOD or FINISHED.
45149       ** This is because in those states the code to roll back savepoint
45150       ** transactions may copy data from the sub-journal into the database
45151       ** file as well as into the page cache. Which would be incorrect in
45152       ** WAL mode.
45153       */
45154       pPager->eState = PAGER_WRITER_LOCKED;
45155       pPager->dbHintSize = pPager->dbSize;
45156       pPager->dbFileSize = pPager->dbSize;
45157       pPager->dbOrigSize = pPager->dbSize;
45158       pPager->journalOff = 0;
45159     }
45160 
45161     assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
45162     assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
45163     assert( assert_pager_state(pPager) );
45164   }
45165 
45166   PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
45167   return rc;
45168 }
45169 
45170 /*
45171 ** Mark a single data page as writeable. The page is written into the
45172 ** main journal or sub-journal as required. If the page is written into
45173 ** one of the journals, the corresponding bit is set in the
45174 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
45175 ** of any open savepoints as appropriate.
45176 */
45177 static int pager_write(PgHdr *pPg){
45178   Pager *pPager = pPg->pPager;
45179   int rc = SQLITE_OK;
45180   int inJournal;
45181 
45182   /* This routine is not called unless a write-transaction has already
45183   ** been started. The journal file may or may not be open at this point.
45184   ** It is never called in the ERROR state.
45185   */
45186   assert( pPager->eState==PAGER_WRITER_LOCKED
45187        || pPager->eState==PAGER_WRITER_CACHEMOD
45188        || pPager->eState==PAGER_WRITER_DBMOD
45189   );
45190   assert( assert_pager_state(pPager) );
45191   assert( pPager->errCode==0 );
45192   assert( pPager->readOnly==0 );
45193 
45194   CHECK_PAGE(pPg);
45195 
45196   /* The journal file needs to be opened. Higher level routines have already
45197   ** obtained the necessary locks to begin the write-transaction, but the
45198   ** rollback journal might not yet be open. Open it now if this is the case.
45199   **
45200   ** This is done before calling sqlite3PcacheMakeDirty() on the page.
45201   ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
45202   ** an error might occur and the pager would end up in WRITER_LOCKED state
45203   ** with pages marked as dirty in the cache.
45204   */
45205   if( pPager->eState==PAGER_WRITER_LOCKED ){
45206     rc = pager_open_journal(pPager);
45207     if( rc!=SQLITE_OK ) return rc;
45208   }
45209   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
45210   assert( assert_pager_state(pPager) );
45211 
45212   /* Mark the page as dirty.  If the page has already been written
45213   ** to the journal then we can return right away.
45214   */
45215   sqlite3PcacheMakeDirty(pPg);
45216   inJournal = pageInJournal(pPager, pPg);
45217   if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
45218     assert( !pagerUseWal(pPager) );
45219   }else{
45220 
45221     /* The transaction journal now exists and we have a RESERVED or an
45222     ** EXCLUSIVE lock on the main database file.  Write the current page to
45223     ** the transaction journal if it is not there already.
45224     */
45225     if( !inJournal && !pagerUseWal(pPager) ){
45226       assert( pagerUseWal(pPager)==0 );
45227       if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
45228         u32 cksum;
45229         char *pData2;
45230         i64 iOff = pPager->journalOff;
45231 
45232         /* We should never write to the journal file the page that
45233         ** contains the database locks.  The following assert verifies
45234         ** that we do not. */
45235         assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
45236 
45237         assert( pPager->journalHdr<=pPager->journalOff );
45238         CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
45239         cksum = pager_cksum(pPager, (u8*)pData2);
45240 
45241         /* Even if an IO or diskfull error occurs while journalling the
45242         ** page in the block above, set the need-sync flag for the page.
45243         ** Otherwise, when the transaction is rolled back, the logic in
45244         ** playback_one_page() will think that the page needs to be restored
45245         ** in the database file. And if an IO error occurs while doing so,
45246         ** then corruption may follow.
45247         */
45248         pPg->flags |= PGHDR_NEED_SYNC;
45249 
45250         rc = write32bits(pPager->jfd, iOff, pPg->pgno);
45251         if( rc!=SQLITE_OK ) return rc;
45252         rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
45253         if( rc!=SQLITE_OK ) return rc;
45254         rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
45255         if( rc!=SQLITE_OK ) return rc;
45256 
45257         IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
45258                  pPager->journalOff, pPager->pageSize));
45259         PAGER_INCR(sqlite3_pager_writej_count);
45260         PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
45261              PAGERID(pPager), pPg->pgno,
45262              ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
45263 
45264         pPager->journalOff += 8 + pPager->pageSize;
45265         pPager->nRec++;
45266         assert( pPager->pInJournal!=0 );
45267         rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
45268         testcase( rc==SQLITE_NOMEM );
45269         assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
45270         rc |= addToSavepointBitvecs(pPager, pPg->pgno);
45271         if( rc!=SQLITE_OK ){
45272           assert( rc==SQLITE_NOMEM );
45273           return rc;
45274         }
45275       }else{
45276         if( pPager->eState!=PAGER_WRITER_DBMOD ){
45277           pPg->flags |= PGHDR_NEED_SYNC;
45278         }
45279         PAGERTRACE(("APPEND %d page %d needSync=%d\n",
45280                 PAGERID(pPager), pPg->pgno,
45281                ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
45282       }
45283     }
45284 
45285     /* If the statement journal is open and the page is not in it,
45286     ** then write the current page to the statement journal.  Note that
45287     ** the statement journal format differs from the standard journal format
45288     ** in that it omits the checksums and the header.
45289     */
45290     if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
45291       rc = subjournalPage(pPg);
45292     }
45293   }
45294 
45295   /* Update the database size and return.
45296   */
45297   if( pPager->dbSize<pPg->pgno ){
45298     pPager->dbSize = pPg->pgno;
45299   }
45300   return rc;
45301 }
45302 
45303 /*
45304 ** Mark a data page as writeable. This routine must be called before
45305 ** making changes to a page. The caller must check the return value
45306 ** of this function and be careful not to change any page data unless
45307 ** this routine returns SQLITE_OK.
45308 **
45309 ** The difference between this function and pager_write() is that this
45310 ** function also deals with the special case where 2 or more pages
45311 ** fit on a single disk sector. In this case all co-resident pages
45312 ** must have been written to the journal file before returning.
45313 **
45314 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
45315 ** as appropriate. Otherwise, SQLITE_OK.
45316 */
45317 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
45318   int rc = SQLITE_OK;
45319 
45320   PgHdr *pPg = pDbPage;
45321   Pager *pPager = pPg->pPager;
45322 
45323   assert( (pPg->flags & PGHDR_MMAP)==0 );
45324   assert( pPager->eState>=PAGER_WRITER_LOCKED );
45325   assert( pPager->eState!=PAGER_ERROR );
45326   assert( assert_pager_state(pPager) );
45327 
45328   if( pPager->sectorSize > (u32)pPager->pageSize ){
45329     Pgno nPageCount;          /* Total number of pages in database file */
45330     Pgno pg1;                 /* First page of the sector pPg is located on. */
45331     int nPage = 0;            /* Number of pages starting at pg1 to journal */
45332     int ii;                   /* Loop counter */
45333     int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
45334     Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
45335 
45336     /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
45337     ** a journal header to be written between the pages journaled by
45338     ** this function.
45339     */
45340     assert( !MEMDB );
45341     assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
45342     pPager->doNotSpill |= SPILLFLAG_NOSYNC;
45343 
45344     /* This trick assumes that both the page-size and sector-size are
45345     ** an integer power of 2. It sets variable pg1 to the identifier
45346     ** of the first page of the sector pPg is located on.
45347     */
45348     pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
45349 
45350     nPageCount = pPager->dbSize;
45351     if( pPg->pgno>nPageCount ){
45352       nPage = (pPg->pgno - pg1)+1;
45353     }else if( (pg1+nPagePerSector-1)>nPageCount ){
45354       nPage = nPageCount+1-pg1;
45355     }else{
45356       nPage = nPagePerSector;
45357     }
45358     assert(nPage>0);
45359     assert(pg1<=pPg->pgno);
45360     assert((pg1+nPage)>pPg->pgno);
45361 
45362     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
45363       Pgno pg = pg1+ii;
45364       PgHdr *pPage;
45365       if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
45366         if( pg!=PAGER_MJ_PGNO(pPager) ){
45367           rc = sqlite3PagerGet(pPager, pg, &pPage);
45368           if( rc==SQLITE_OK ){
45369             rc = pager_write(pPage);
45370             if( pPage->flags&PGHDR_NEED_SYNC ){
45371               needSync = 1;
45372             }
45373             sqlite3PagerUnrefNotNull(pPage);
45374           }
45375         }
45376       }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
45377         if( pPage->flags&PGHDR_NEED_SYNC ){
45378           needSync = 1;
45379         }
45380         sqlite3PagerUnrefNotNull(pPage);
45381       }
45382     }
45383 
45384     /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
45385     ** starting at pg1, then it needs to be set for all of them. Because
45386     ** writing to any of these nPage pages may damage the others, the
45387     ** journal file must contain sync()ed copies of all of them
45388     ** before any of them can be written out to the database file.
45389     */
45390     if( rc==SQLITE_OK && needSync ){
45391       assert( !MEMDB );
45392       for(ii=0; ii<nPage; ii++){
45393         PgHdr *pPage = pager_lookup(pPager, pg1+ii);
45394         if( pPage ){
45395           pPage->flags |= PGHDR_NEED_SYNC;
45396           sqlite3PagerUnrefNotNull(pPage);
45397         }
45398       }
45399     }
45400 
45401     assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
45402     pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
45403   }else{
45404     rc = pager_write(pDbPage);
45405   }
45406   return rc;
45407 }
45408 
45409 /*
45410 ** Return TRUE if the page given in the argument was previously passed
45411 ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
45412 ** to change the content of the page.
45413 */
45414 #ifndef NDEBUG
45415 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
45416   return pPg->flags&PGHDR_DIRTY;
45417 }
45418 #endif
45419 
45420 /*
45421 ** A call to this routine tells the pager that it is not necessary to
45422 ** write the information on page pPg back to the disk, even though
45423 ** that page might be marked as dirty.  This happens, for example, when
45424 ** the page has been added as a leaf of the freelist and so its
45425 ** content no longer matters.
45426 **
45427 ** The overlying software layer calls this routine when all of the data
45428 ** on the given page is unused. The pager marks the page as clean so
45429 ** that it does not get written to disk.
45430 **
45431 ** Tests show that this optimization can quadruple the speed of large
45432 ** DELETE operations.
45433 */
45434 SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
45435   Pager *pPager = pPg->pPager;
45436   if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
45437     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
45438     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
45439     pPg->flags |= PGHDR_DONT_WRITE;
45440     pager_set_pagehash(pPg);
45441   }
45442 }
45443 
45444 /*
45445 ** This routine is called to increment the value of the database file
45446 ** change-counter, stored as a 4-byte big-endian integer starting at
45447 ** byte offset 24 of the pager file.  The secondary change counter at
45448 ** 92 is also updated, as is the SQLite version number at offset 96.
45449 **
45450 ** But this only happens if the pPager->changeCountDone flag is false.
45451 ** To avoid excess churning of page 1, the update only happens once.
45452 ** See also the pager_write_changecounter() routine that does an
45453 ** unconditional update of the change counters.
45454 **
45455 ** If the isDirectMode flag is zero, then this is done by calling
45456 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
45457 ** page data. In this case the file will be updated when the current
45458 ** transaction is committed.
45459 **
45460 ** The isDirectMode flag may only be non-zero if the library was compiled
45461 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
45462 ** if isDirect is non-zero, then the database file is updated directly
45463 ** by writing an updated version of page 1 using a call to the
45464 ** sqlite3OsWrite() function.
45465 */
45466 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
45467   int rc = SQLITE_OK;
45468 
45469   assert( pPager->eState==PAGER_WRITER_CACHEMOD
45470        || pPager->eState==PAGER_WRITER_DBMOD
45471   );
45472   assert( assert_pager_state(pPager) );
45473 
45474   /* Declare and initialize constant integer 'isDirect'. If the
45475   ** atomic-write optimization is enabled in this build, then isDirect
45476   ** is initialized to the value passed as the isDirectMode parameter
45477   ** to this function. Otherwise, it is always set to zero.
45478   **
45479   ** The idea is that if the atomic-write optimization is not
45480   ** enabled at compile time, the compiler can omit the tests of
45481   ** 'isDirect' below, as well as the block enclosed in the
45482   ** "if( isDirect )" condition.
45483   */
45484 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
45485 # define DIRECT_MODE 0
45486   assert( isDirectMode==0 );
45487   UNUSED_PARAMETER(isDirectMode);
45488 #else
45489 # define DIRECT_MODE isDirectMode
45490 #endif
45491 
45492   if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
45493     PgHdr *pPgHdr;                /* Reference to page 1 */
45494 
45495     assert( !pPager->tempFile && isOpen(pPager->fd) );
45496 
45497     /* Open page 1 of the file for writing. */
45498     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
45499     assert( pPgHdr==0 || rc==SQLITE_OK );
45500 
45501     /* If page one was fetched successfully, and this function is not
45502     ** operating in direct-mode, make page 1 writable.  When not in
45503     ** direct mode, page 1 is always held in cache and hence the PagerGet()
45504     ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
45505     */
45506     if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
45507       rc = sqlite3PagerWrite(pPgHdr);
45508     }
45509 
45510     if( rc==SQLITE_OK ){
45511       /* Actually do the update of the change counter */
45512       pager_write_changecounter(pPgHdr);
45513 
45514       /* If running in direct mode, write the contents of page 1 to the file. */
45515       if( DIRECT_MODE ){
45516         const void *zBuf;
45517         assert( pPager->dbFileSize>0 );
45518         CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
45519         if( rc==SQLITE_OK ){
45520           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
45521           pPager->aStat[PAGER_STAT_WRITE]++;
45522         }
45523         if( rc==SQLITE_OK ){
45524           /* Update the pager's copy of the change-counter. Otherwise, the
45525           ** next time a read transaction is opened the cache will be
45526           ** flushed (as the change-counter values will not match).  */
45527           const void *pCopy = (const void *)&((const char *)zBuf)[24];
45528           memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
45529           pPager->changeCountDone = 1;
45530         }
45531       }else{
45532         pPager->changeCountDone = 1;
45533       }
45534     }
45535 
45536     /* Release the page reference. */
45537     sqlite3PagerUnref(pPgHdr);
45538   }
45539   return rc;
45540 }
45541 
45542 /*
45543 ** Sync the database file to disk. This is a no-op for in-memory databases
45544 ** or pages with the Pager.noSync flag set.
45545 **
45546 ** If successful, or if called on a pager for which it is a no-op, this
45547 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
45548 */
45549 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
45550   int rc = SQLITE_OK;
45551 
45552   if( isOpen(pPager->fd) ){
45553     void *pArg = (void*)zMaster;
45554     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
45555     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
45556   }
45557   if( rc==SQLITE_OK && !pPager->noSync ){
45558     assert( !MEMDB );
45559     rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
45560   }
45561   return rc;
45562 }
45563 
45564 /*
45565 ** This function may only be called while a write-transaction is active in
45566 ** rollback. If the connection is in WAL mode, this call is a no-op.
45567 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on
45568 ** the database file, an attempt is made to obtain one.
45569 **
45570 ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
45571 ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
45572 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
45573 ** returned.
45574 */
45575 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
45576   int rc = SQLITE_OK;
45577   assert( pPager->eState==PAGER_WRITER_CACHEMOD
45578        || pPager->eState==PAGER_WRITER_DBMOD
45579        || pPager->eState==PAGER_WRITER_LOCKED
45580   );
45581   assert( assert_pager_state(pPager) );
45582   if( 0==pagerUseWal(pPager) ){
45583     rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
45584   }
45585   return rc;
45586 }
45587 
45588 /*
45589 ** Sync the database file for the pager pPager. zMaster points to the name
45590 ** of a master journal file that should be written into the individual
45591 ** journal file. zMaster may be NULL, which is interpreted as no master
45592 ** journal (a single database transaction).
45593 **
45594 ** This routine ensures that:
45595 **
45596 **   * The database file change-counter is updated,
45597 **   * the journal is synced (unless the atomic-write optimization is used),
45598 **   * all dirty pages are written to the database file,
45599 **   * the database file is truncated (if required), and
45600 **   * the database file synced.
45601 **
45602 ** The only thing that remains to commit the transaction is to finalize
45603 ** (delete, truncate or zero the first part of) the journal file (or
45604 ** delete the master journal file if specified).
45605 **
45606 ** Note that if zMaster==NULL, this does not overwrite a previous value
45607 ** passed to an sqlite3PagerCommitPhaseOne() call.
45608 **
45609 ** If the final parameter - noSync - is true, then the database file itself
45610 ** is not synced. The caller must call sqlite3PagerSync() directly to
45611 ** sync the database file before calling CommitPhaseTwo() to delete the
45612 ** journal file in this case.
45613 */
45614 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
45615   Pager *pPager,                  /* Pager object */
45616   const char *zMaster,            /* If not NULL, the master journal name */
45617   int noSync                      /* True to omit the xSync on the db file */
45618 ){
45619   int rc = SQLITE_OK;             /* Return code */
45620 
45621   assert( pPager->eState==PAGER_WRITER_LOCKED
45622        || pPager->eState==PAGER_WRITER_CACHEMOD
45623        || pPager->eState==PAGER_WRITER_DBMOD
45624        || pPager->eState==PAGER_ERROR
45625   );
45626   assert( assert_pager_state(pPager) );
45627 
45628   /* If a prior error occurred, report that error again. */
45629   if( NEVER(pPager->errCode) ) return pPager->errCode;
45630 
45631   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
45632       pPager->zFilename, zMaster, pPager->dbSize));
45633 
45634   /* If no database changes have been made, return early. */
45635   if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
45636 
45637   if( MEMDB ){
45638     /* If this is an in-memory db, or no pages have been written to, or this
45639     ** function has already been called, it is mostly a no-op.  However, any
45640     ** backup in progress needs to be restarted.
45641     */
45642     sqlite3BackupRestart(pPager->pBackup);
45643   }else{
45644     if( pagerUseWal(pPager) ){
45645       PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
45646       PgHdr *pPageOne = 0;
45647       if( pList==0 ){
45648         /* Must have at least one page for the WAL commit flag.
45649         ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
45650         rc = sqlite3PagerGet(pPager, 1, &pPageOne);
45651         pList = pPageOne;
45652         pList->pDirty = 0;
45653       }
45654       assert( rc==SQLITE_OK );
45655       if( ALWAYS(pList) ){
45656         rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
45657       }
45658       sqlite3PagerUnref(pPageOne);
45659       if( rc==SQLITE_OK ){
45660         sqlite3PcacheCleanAll(pPager->pPCache);
45661       }
45662     }else{
45663       /* The following block updates the change-counter. Exactly how it
45664       ** does this depends on whether or not the atomic-update optimization
45665       ** was enabled at compile time, and if this transaction meets the
45666       ** runtime criteria to use the operation:
45667       **
45668       **    * The file-system supports the atomic-write property for
45669       **      blocks of size page-size, and
45670       **    * This commit is not part of a multi-file transaction, and
45671       **    * Exactly one page has been modified and store in the journal file.
45672       **
45673       ** If the optimization was not enabled at compile time, then the
45674       ** pager_incr_changecounter() function is called to update the change
45675       ** counter in 'indirect-mode'. If the optimization is compiled in but
45676       ** is not applicable to this transaction, call sqlite3JournalCreate()
45677       ** to make sure the journal file has actually been created, then call
45678       ** pager_incr_changecounter() to update the change-counter in indirect
45679       ** mode.
45680       **
45681       ** Otherwise, if the optimization is both enabled and applicable,
45682       ** then call pager_incr_changecounter() to update the change-counter
45683       ** in 'direct' mode. In this case the journal file will never be
45684       ** created for this transaction.
45685       */
45686   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
45687       PgHdr *pPg;
45688       assert( isOpen(pPager->jfd)
45689            || pPager->journalMode==PAGER_JOURNALMODE_OFF
45690            || pPager->journalMode==PAGER_JOURNALMODE_WAL
45691       );
45692       if( !zMaster && isOpen(pPager->jfd)
45693        && pPager->journalOff==jrnlBufferSize(pPager)
45694        && pPager->dbSize>=pPager->dbOrigSize
45695        && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
45696       ){
45697         /* Update the db file change counter via the direct-write method. The
45698         ** following call will modify the in-memory representation of page 1
45699         ** to include the updated change counter and then write page 1
45700         ** directly to the database file. Because of the atomic-write
45701         ** property of the host file-system, this is safe.
45702         */
45703         rc = pager_incr_changecounter(pPager, 1);
45704       }else{
45705         rc = sqlite3JournalCreate(pPager->jfd);
45706         if( rc==SQLITE_OK ){
45707           rc = pager_incr_changecounter(pPager, 0);
45708         }
45709       }
45710   #else
45711       rc = pager_incr_changecounter(pPager, 0);
45712   #endif
45713       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45714 
45715       /* Write the master journal name into the journal file. If a master
45716       ** journal file name has already been written to the journal file,
45717       ** or if zMaster is NULL (no master journal), then this call is a no-op.
45718       */
45719       rc = writeMasterJournal(pPager, zMaster);
45720       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45721 
45722       /* Sync the journal file and write all dirty pages to the database.
45723       ** If the atomic-update optimization is being used, this sync will not
45724       ** create the journal file or perform any real IO.
45725       **
45726       ** Because the change-counter page was just modified, unless the
45727       ** atomic-update optimization is used it is almost certain that the
45728       ** journal requires a sync here. However, in locking_mode=exclusive
45729       ** on a system under memory pressure it is just possible that this is
45730       ** not the case. In this case it is likely enough that the redundant
45731       ** xSync() call will be changed to a no-op by the OS anyhow.
45732       */
45733       rc = syncJournal(pPager, 0);
45734       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45735 
45736       rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
45737       if( rc!=SQLITE_OK ){
45738         assert( rc!=SQLITE_IOERR_BLOCKED );
45739         goto commit_phase_one_exit;
45740       }
45741       sqlite3PcacheCleanAll(pPager->pPCache);
45742 
45743       /* If the file on disk is smaller than the database image, use
45744       ** pager_truncate to grow the file here. This can happen if the database
45745       ** image was extended as part of the current transaction and then the
45746       ** last page in the db image moved to the free-list. In this case the
45747       ** last page is never written out to disk, leaving the database file
45748       ** undersized. Fix this now if it is the case.  */
45749       if( pPager->dbSize>pPager->dbFileSize ){
45750         Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
45751         assert( pPager->eState==PAGER_WRITER_DBMOD );
45752         rc = pager_truncate(pPager, nNew);
45753         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45754       }
45755 
45756       /* Finally, sync the database file. */
45757       if( !noSync ){
45758         rc = sqlite3PagerSync(pPager, zMaster);
45759       }
45760       IOTRACE(("DBSYNC %p\n", pPager))
45761     }
45762   }
45763 
45764 commit_phase_one_exit:
45765   if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
45766     pPager->eState = PAGER_WRITER_FINISHED;
45767   }
45768   return rc;
45769 }
45770 
45771 
45772 /*
45773 ** When this function is called, the database file has been completely
45774 ** updated to reflect the changes made by the current transaction and
45775 ** synced to disk. The journal file still exists in the file-system
45776 ** though, and if a failure occurs at this point it will eventually
45777 ** be used as a hot-journal and the current transaction rolled back.
45778 **
45779 ** This function finalizes the journal file, either by deleting,
45780 ** truncating or partially zeroing it, so that it cannot be used
45781 ** for hot-journal rollback. Once this is done the transaction is
45782 ** irrevocably committed.
45783 **
45784 ** If an error occurs, an IO error code is returned and the pager
45785 ** moves into the error state. Otherwise, SQLITE_OK is returned.
45786 */
45787 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
45788   int rc = SQLITE_OK;                  /* Return code */
45789 
45790   /* This routine should not be called if a prior error has occurred.
45791   ** But if (due to a coding error elsewhere in the system) it does get
45792   ** called, just return the same error code without doing anything. */
45793   if( NEVER(pPager->errCode) ) return pPager->errCode;
45794 
45795   assert( pPager->eState==PAGER_WRITER_LOCKED
45796        || pPager->eState==PAGER_WRITER_FINISHED
45797        || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
45798   );
45799   assert( assert_pager_state(pPager) );
45800 
45801   /* An optimization. If the database was not actually modified during
45802   ** this transaction, the pager is running in exclusive-mode and is
45803   ** using persistent journals, then this function is a no-op.
45804   **
45805   ** The start of the journal file currently contains a single journal
45806   ** header with the nRec field set to 0. If such a journal is used as
45807   ** a hot-journal during hot-journal rollback, 0 changes will be made
45808   ** to the database file. So there is no need to zero the journal
45809   ** header. Since the pager is in exclusive mode, there is no need
45810   ** to drop any locks either.
45811   */
45812   if( pPager->eState==PAGER_WRITER_LOCKED
45813    && pPager->exclusiveMode
45814    && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
45815   ){
45816     assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
45817     pPager->eState = PAGER_READER;
45818     return SQLITE_OK;
45819   }
45820 
45821   PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
45822   rc = pager_end_transaction(pPager, pPager->setMaster, 1);
45823   return pager_error(pPager, rc);
45824 }
45825 
45826 /*
45827 ** If a write transaction is open, then all changes made within the
45828 ** transaction are reverted and the current write-transaction is closed.
45829 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
45830 ** state if an error occurs.
45831 **
45832 ** If the pager is already in PAGER_ERROR state when this function is called,
45833 ** it returns Pager.errCode immediately. No work is performed in this case.
45834 **
45835 ** Otherwise, in rollback mode, this function performs two functions:
45836 **
45837 **   1) It rolls back the journal file, restoring all database file and
45838 **      in-memory cache pages to the state they were in when the transaction
45839 **      was opened, and
45840 **
45841 **   2) It finalizes the journal file, so that it is not used for hot
45842 **      rollback at any point in the future.
45843 **
45844 ** Finalization of the journal file (task 2) is only performed if the
45845 ** rollback is successful.
45846 **
45847 ** In WAL mode, all cache-entries containing data modified within the
45848 ** current transaction are either expelled from the cache or reverted to
45849 ** their pre-transaction state by re-reading data from the database or
45850 ** WAL files. The WAL transaction is then closed.
45851 */
45852 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
45853   int rc = SQLITE_OK;                  /* Return code */
45854   PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
45855 
45856   /* PagerRollback() is a no-op if called in READER or OPEN state. If
45857   ** the pager is already in the ERROR state, the rollback is not
45858   ** attempted here. Instead, the error code is returned to the caller.
45859   */
45860   assert( assert_pager_state(pPager) );
45861   if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
45862   if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
45863 
45864   if( pagerUseWal(pPager) ){
45865     int rc2;
45866     rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
45867     rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
45868     if( rc==SQLITE_OK ) rc = rc2;
45869   }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
45870     int eState = pPager->eState;
45871     rc = pager_end_transaction(pPager, 0, 0);
45872     if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
45873       /* This can happen using journal_mode=off. Move the pager to the error
45874       ** state to indicate that the contents of the cache may not be trusted.
45875       ** Any active readers will get SQLITE_ABORT.
45876       */
45877       pPager->errCode = SQLITE_ABORT;
45878       pPager->eState = PAGER_ERROR;
45879       return rc;
45880     }
45881   }else{
45882     rc = pager_playback(pPager, 0);
45883   }
45884 
45885   assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
45886   assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
45887           || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
45888           || rc==SQLITE_CANTOPEN
45889   );
45890 
45891   /* If an error occurs during a ROLLBACK, we can no longer trust the pager
45892   ** cache. So call pager_error() on the way out to make any error persistent.
45893   */
45894   return pager_error(pPager, rc);
45895 }
45896 
45897 /*
45898 ** Return TRUE if the database file is opened read-only.  Return FALSE
45899 ** if the database is (in theory) writable.
45900 */
45901 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
45902   return pPager->readOnly;
45903 }
45904 
45905 /*
45906 ** Return the number of references to the pager.
45907 */
45908 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
45909   return sqlite3PcacheRefCount(pPager->pPCache);
45910 }
45911 
45912 /*
45913 ** Return the approximate number of bytes of memory currently
45914 ** used by the pager and its associated cache.
45915 */
45916 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
45917   int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
45918                                      + 5*sizeof(void*);
45919   return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
45920            + sqlite3MallocSize(pPager)
45921            + pPager->pageSize;
45922 }
45923 
45924 /*
45925 ** Return the number of references to the specified page.
45926 */
45927 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
45928   return sqlite3PcachePageRefcount(pPage);
45929 }
45930 
45931 #ifdef SQLITE_TEST
45932 /*
45933 ** This routine is used for testing and analysis only.
45934 */
45935 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
45936   static int a[11];
45937   a[0] = sqlite3PcacheRefCount(pPager->pPCache);
45938   a[1] = sqlite3PcachePagecount(pPager->pPCache);
45939   a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
45940   a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
45941   a[4] = pPager->eState;
45942   a[5] = pPager->errCode;
45943   a[6] = pPager->aStat[PAGER_STAT_HIT];
45944   a[7] = pPager->aStat[PAGER_STAT_MISS];
45945   a[8] = 0;  /* Used to be pPager->nOvfl */
45946   a[9] = pPager->nRead;
45947   a[10] = pPager->aStat[PAGER_STAT_WRITE];
45948   return a;
45949 }
45950 #endif
45951 
45952 /*
45953 ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
45954 ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
45955 ** current cache hit or miss count, according to the value of eStat. If the
45956 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
45957 ** returning.
45958 */
45959 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
45960 
45961   assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
45962        || eStat==SQLITE_DBSTATUS_CACHE_MISS
45963        || eStat==SQLITE_DBSTATUS_CACHE_WRITE
45964   );
45965 
45966   assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
45967   assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
45968   assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
45969 
45970   *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
45971   if( reset ){
45972     pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
45973   }
45974 }
45975 
45976 /*
45977 ** Return true if this is an in-memory pager.
45978 */
45979 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
45980   return MEMDB;
45981 }
45982 
45983 /*
45984 ** Check that there are at least nSavepoint savepoints open. If there are
45985 ** currently less than nSavepoints open, then open one or more savepoints
45986 ** to make up the difference. If the number of savepoints is already
45987 ** equal to nSavepoint, then this function is a no-op.
45988 **
45989 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
45990 ** occurs while opening the sub-journal file, then an IO error code is
45991 ** returned. Otherwise, SQLITE_OK.
45992 */
45993 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
45994   int rc = SQLITE_OK;                       /* Return code */
45995   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
45996 
45997   assert( pPager->eState>=PAGER_WRITER_LOCKED );
45998   assert( assert_pager_state(pPager) );
45999 
46000   if( nSavepoint>nCurrent && pPager->useJournal ){
46001     int ii;                                 /* Iterator variable */
46002     PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
46003 
46004     /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
46005     ** if the allocation fails. Otherwise, zero the new portion in case a
46006     ** malloc failure occurs while populating it in the for(...) loop below.
46007     */
46008     aNew = (PagerSavepoint *)sqlite3Realloc(
46009         pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
46010     );
46011     if( !aNew ){
46012       return SQLITE_NOMEM;
46013     }
46014     memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
46015     pPager->aSavepoint = aNew;
46016 
46017     /* Populate the PagerSavepoint structures just allocated. */
46018     for(ii=nCurrent; ii<nSavepoint; ii++){
46019       aNew[ii].nOrig = pPager->dbSize;
46020       if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
46021         aNew[ii].iOffset = pPager->journalOff;
46022       }else{
46023         aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
46024       }
46025       aNew[ii].iSubRec = pPager->nSubRec;
46026       aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
46027       if( !aNew[ii].pInSavepoint ){
46028         return SQLITE_NOMEM;
46029       }
46030       if( pagerUseWal(pPager) ){
46031         sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
46032       }
46033       pPager->nSavepoint = ii+1;
46034     }
46035     assert( pPager->nSavepoint==nSavepoint );
46036     assertTruncateConstraint(pPager);
46037   }
46038 
46039   return rc;
46040 }
46041 
46042 /*
46043 ** This function is called to rollback or release (commit) a savepoint.
46044 ** The savepoint to release or rollback need not be the most recently
46045 ** created savepoint.
46046 **
46047 ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
46048 ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
46049 ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
46050 ** that have occurred since the specified savepoint was created.
46051 **
46052 ** The savepoint to rollback or release is identified by parameter
46053 ** iSavepoint. A value of 0 means to operate on the outermost savepoint
46054 ** (the first created). A value of (Pager.nSavepoint-1) means operate
46055 ** on the most recently created savepoint. If iSavepoint is greater than
46056 ** (Pager.nSavepoint-1), then this function is a no-op.
46057 **
46058 ** If a negative value is passed to this function, then the current
46059 ** transaction is rolled back. This is different to calling
46060 ** sqlite3PagerRollback() because this function does not terminate
46061 ** the transaction or unlock the database, it just restores the
46062 ** contents of the database to its original state.
46063 **
46064 ** In any case, all savepoints with an index greater than iSavepoint
46065 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
46066 ** then savepoint iSavepoint is also destroyed.
46067 **
46068 ** This function may return SQLITE_NOMEM if a memory allocation fails,
46069 ** or an IO error code if an IO error occurs while rolling back a
46070 ** savepoint. If no errors occur, SQLITE_OK is returned.
46071 */
46072 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
46073   int rc = pPager->errCode;       /* Return code */
46074 
46075   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
46076   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
46077 
46078   if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
46079     int ii;            /* Iterator variable */
46080     int nNew;          /* Number of remaining savepoints after this op. */
46081 
46082     /* Figure out how many savepoints will still be active after this
46083     ** operation. Store this value in nNew. Then free resources associated
46084     ** with any savepoints that are destroyed by this operation.
46085     */
46086     nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
46087     for(ii=nNew; ii<pPager->nSavepoint; ii++){
46088       sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
46089     }
46090     pPager->nSavepoint = nNew;
46091 
46092     /* If this is a release of the outermost savepoint, truncate
46093     ** the sub-journal to zero bytes in size. */
46094     if( op==SAVEPOINT_RELEASE ){
46095       if( nNew==0 && isOpen(pPager->sjfd) ){
46096         /* Only truncate if it is an in-memory sub-journal. */
46097         if( sqlite3IsMemJournal(pPager->sjfd) ){
46098           rc = sqlite3OsTruncate(pPager->sjfd, 0);
46099           assert( rc==SQLITE_OK );
46100         }
46101         pPager->nSubRec = 0;
46102       }
46103     }
46104     /* Else this is a rollback operation, playback the specified savepoint.
46105     ** If this is a temp-file, it is possible that the journal file has
46106     ** not yet been opened. In this case there have been no changes to
46107     ** the database file, so the playback operation can be skipped.
46108     */
46109     else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
46110       PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
46111       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
46112       assert(rc!=SQLITE_DONE);
46113     }
46114   }
46115 
46116   return rc;
46117 }
46118 
46119 /*
46120 ** Return the full pathname of the database file.
46121 **
46122 ** Except, if the pager is in-memory only, then return an empty string if
46123 ** nullIfMemDb is true.  This routine is called with nullIfMemDb==1 when
46124 ** used to report the filename to the user, for compatibility with legacy
46125 ** behavior.  But when the Btree needs to know the filename for matching to
46126 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
46127 ** participate in shared-cache.
46128 */
46129 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
46130   return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
46131 }
46132 
46133 /*
46134 ** Return the VFS structure for the pager.
46135 */
46136 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
46137   return pPager->pVfs;
46138 }
46139 
46140 /*
46141 ** Return the file handle for the database file associated
46142 ** with the pager.  This might return NULL if the file has
46143 ** not yet been opened.
46144 */
46145 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
46146   return pPager->fd;
46147 }
46148 
46149 /*
46150 ** Return the full pathname of the journal file.
46151 */
46152 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
46153   return pPager->zJournal;
46154 }
46155 
46156 /*
46157 ** Return true if fsync() calls are disabled for this pager.  Return FALSE
46158 ** if fsync()s are executed normally.
46159 */
46160 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
46161   return pPager->noSync;
46162 }
46163 
46164 #ifdef SQLITE_HAS_CODEC
46165 /*
46166 ** Set or retrieve the codec for this pager
46167 */
46168 SQLITE_PRIVATE void sqlite3PagerSetCodec(
46169   Pager *pPager,
46170   void *(*xCodec)(void*,void*,Pgno,int),
46171   void (*xCodecSizeChng)(void*,int,int),
46172   void (*xCodecFree)(void*),
46173   void *pCodec
46174 ){
46175   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
46176   pPager->xCodec = pPager->memDb ? 0 : xCodec;
46177   pPager->xCodecSizeChng = xCodecSizeChng;
46178   pPager->xCodecFree = xCodecFree;
46179   pPager->pCodec = pCodec;
46180   pagerReportSize(pPager);
46181 }
46182 SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
46183   return pPager->pCodec;
46184 }
46185 
46186 /*
46187 ** This function is called by the wal module when writing page content
46188 ** into the log file.
46189 **
46190 ** This function returns a pointer to a buffer containing the encrypted
46191 ** page content. If a malloc fails, this function may return NULL.
46192 */
46193 SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
46194   void *aData = 0;
46195   CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
46196   return aData;
46197 }
46198 
46199 /*
46200 ** Return the current pager state
46201 */
46202 SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){
46203   return pPager->eState;
46204 }
46205 #endif /* SQLITE_HAS_CODEC */
46206 
46207 #ifndef SQLITE_OMIT_AUTOVACUUM
46208 /*
46209 ** Move the page pPg to location pgno in the file.
46210 **
46211 ** There must be no references to the page previously located at
46212 ** pgno (which we call pPgOld) though that page is allowed to be
46213 ** in cache.  If the page previously located at pgno is not already
46214 ** in the rollback journal, it is not put there by by this routine.
46215 **
46216 ** References to the page pPg remain valid. Updating any
46217 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
46218 ** allocated along with the page) is the responsibility of the caller.
46219 **
46220 ** A transaction must be active when this routine is called. It used to be
46221 ** required that a statement transaction was not active, but this restriction
46222 ** has been removed (CREATE INDEX needs to move a page when a statement
46223 ** transaction is active).
46224 **
46225 ** If the fourth argument, isCommit, is non-zero, then this page is being
46226 ** moved as part of a database reorganization just before the transaction
46227 ** is being committed. In this case, it is guaranteed that the database page
46228 ** pPg refers to will not be written to again within this transaction.
46229 **
46230 ** This function may return SQLITE_NOMEM or an IO error code if an error
46231 ** occurs. Otherwise, it returns SQLITE_OK.
46232 */
46233 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
46234   PgHdr *pPgOld;               /* The page being overwritten. */
46235   Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
46236   int rc;                      /* Return code */
46237   Pgno origPgno;               /* The original page number */
46238 
46239   assert( pPg->nRef>0 );
46240   assert( pPager->eState==PAGER_WRITER_CACHEMOD
46241        || pPager->eState==PAGER_WRITER_DBMOD
46242   );
46243   assert( assert_pager_state(pPager) );
46244 
46245   /* In order to be able to rollback, an in-memory database must journal
46246   ** the page we are moving from.
46247   */
46248   if( MEMDB ){
46249     rc = sqlite3PagerWrite(pPg);
46250     if( rc ) return rc;
46251   }
46252 
46253   /* If the page being moved is dirty and has not been saved by the latest
46254   ** savepoint, then save the current contents of the page into the
46255   ** sub-journal now. This is required to handle the following scenario:
46256   **
46257   **   BEGIN;
46258   **     <journal page X, then modify it in memory>
46259   **     SAVEPOINT one;
46260   **       <Move page X to location Y>
46261   **     ROLLBACK TO one;
46262   **
46263   ** If page X were not written to the sub-journal here, it would not
46264   ** be possible to restore its contents when the "ROLLBACK TO one"
46265   ** statement were is processed.
46266   **
46267   ** subjournalPage() may need to allocate space to store pPg->pgno into
46268   ** one or more savepoint bitvecs. This is the reason this function
46269   ** may return SQLITE_NOMEM.
46270   */
46271   if( pPg->flags&PGHDR_DIRTY
46272    && subjRequiresPage(pPg)
46273    && SQLITE_OK!=(rc = subjournalPage(pPg))
46274   ){
46275     return rc;
46276   }
46277 
46278   PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
46279       PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
46280   IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
46281 
46282   /* If the journal needs to be sync()ed before page pPg->pgno can
46283   ** be written to, store pPg->pgno in local variable needSyncPgno.
46284   **
46285   ** If the isCommit flag is set, there is no need to remember that
46286   ** the journal needs to be sync()ed before database page pPg->pgno
46287   ** can be written to. The caller has already promised not to write to it.
46288   */
46289   if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
46290     needSyncPgno = pPg->pgno;
46291     assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
46292             pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
46293     assert( pPg->flags&PGHDR_DIRTY );
46294   }
46295 
46296   /* If the cache contains a page with page-number pgno, remove it
46297   ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
46298   ** page pgno before the 'move' operation, it needs to be retained
46299   ** for the page moved there.
46300   */
46301   pPg->flags &= ~PGHDR_NEED_SYNC;
46302   pPgOld = pager_lookup(pPager, pgno);
46303   assert( !pPgOld || pPgOld->nRef==1 );
46304   if( pPgOld ){
46305     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
46306     if( MEMDB ){
46307       /* Do not discard pages from an in-memory database since we might
46308       ** need to rollback later.  Just move the page out of the way. */
46309       sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
46310     }else{
46311       sqlite3PcacheDrop(pPgOld);
46312     }
46313   }
46314 
46315   origPgno = pPg->pgno;
46316   sqlite3PcacheMove(pPg, pgno);
46317   sqlite3PcacheMakeDirty(pPg);
46318 
46319   /* For an in-memory database, make sure the original page continues
46320   ** to exist, in case the transaction needs to roll back.  Use pPgOld
46321   ** as the original page since it has already been allocated.
46322   */
46323   if( MEMDB ){
46324     assert( pPgOld );
46325     sqlite3PcacheMove(pPgOld, origPgno);
46326     sqlite3PagerUnrefNotNull(pPgOld);
46327   }
46328 
46329   if( needSyncPgno ){
46330     /* If needSyncPgno is non-zero, then the journal file needs to be
46331     ** sync()ed before any data is written to database file page needSyncPgno.
46332     ** Currently, no such page exists in the page-cache and the
46333     ** "is journaled" bitvec flag has been set. This needs to be remedied by
46334     ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
46335     ** flag.
46336     **
46337     ** If the attempt to load the page into the page-cache fails, (due
46338     ** to a malloc() or IO failure), clear the bit in the pInJournal[]
46339     ** array. Otherwise, if the page is loaded and written again in
46340     ** this transaction, it may be written to the database file before
46341     ** it is synced into the journal file. This way, it may end up in
46342     ** the journal file twice, but that is not a problem.
46343     */
46344     PgHdr *pPgHdr;
46345     rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
46346     if( rc!=SQLITE_OK ){
46347       if( needSyncPgno<=pPager->dbOrigSize ){
46348         assert( pPager->pTmpSpace!=0 );
46349         sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
46350       }
46351       return rc;
46352     }
46353     pPgHdr->flags |= PGHDR_NEED_SYNC;
46354     sqlite3PcacheMakeDirty(pPgHdr);
46355     sqlite3PagerUnrefNotNull(pPgHdr);
46356   }
46357 
46358   return SQLITE_OK;
46359 }
46360 #endif
46361 
46362 /*
46363 ** Return a pointer to the data for the specified page.
46364 */
46365 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
46366   assert( pPg->nRef>0 || pPg->pPager->memDb );
46367   return pPg->pData;
46368 }
46369 
46370 /*
46371 ** Return a pointer to the Pager.nExtra bytes of "extra" space
46372 ** allocated along with the specified page.
46373 */
46374 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
46375   return pPg->pExtra;
46376 }
46377 
46378 /*
46379 ** Get/set the locking-mode for this pager. Parameter eMode must be one
46380 ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
46381 ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
46382 ** the locking-mode is set to the value specified.
46383 **
46384 ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
46385 ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
46386 ** locking-mode.
46387 */
46388 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
46389   assert( eMode==PAGER_LOCKINGMODE_QUERY
46390             || eMode==PAGER_LOCKINGMODE_NORMAL
46391             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
46392   assert( PAGER_LOCKINGMODE_QUERY<0 );
46393   assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
46394   assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
46395   if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
46396     pPager->exclusiveMode = (u8)eMode;
46397   }
46398   return (int)pPager->exclusiveMode;
46399 }
46400 
46401 /*
46402 ** Set the journal-mode for this pager. Parameter eMode must be one of:
46403 **
46404 **    PAGER_JOURNALMODE_DELETE
46405 **    PAGER_JOURNALMODE_TRUNCATE
46406 **    PAGER_JOURNALMODE_PERSIST
46407 **    PAGER_JOURNALMODE_OFF
46408 **    PAGER_JOURNALMODE_MEMORY
46409 **    PAGER_JOURNALMODE_WAL
46410 **
46411 ** The journalmode is set to the value specified if the change is allowed.
46412 ** The change may be disallowed for the following reasons:
46413 **
46414 **   *  An in-memory database can only have its journal_mode set to _OFF
46415 **      or _MEMORY.
46416 **
46417 **   *  Temporary databases cannot have _WAL journalmode.
46418 **
46419 ** The returned indicate the current (possibly updated) journal-mode.
46420 */
46421 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
46422   u8 eOld = pPager->journalMode;    /* Prior journalmode */
46423 
46424 #ifdef SQLITE_DEBUG
46425   /* The print_pager_state() routine is intended to be used by the debugger
46426   ** only.  We invoke it once here to suppress a compiler warning. */
46427   print_pager_state(pPager);
46428 #endif
46429 
46430 
46431   /* The eMode parameter is always valid */
46432   assert(      eMode==PAGER_JOURNALMODE_DELETE
46433             || eMode==PAGER_JOURNALMODE_TRUNCATE
46434             || eMode==PAGER_JOURNALMODE_PERSIST
46435             || eMode==PAGER_JOURNALMODE_OFF
46436             || eMode==PAGER_JOURNALMODE_WAL
46437             || eMode==PAGER_JOURNALMODE_MEMORY );
46438 
46439   /* This routine is only called from the OP_JournalMode opcode, and
46440   ** the logic there will never allow a temporary file to be changed
46441   ** to WAL mode.
46442   */
46443   assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
46444 
46445   /* Do allow the journalmode of an in-memory database to be set to
46446   ** anything other than MEMORY or OFF
46447   */
46448   if( MEMDB ){
46449     assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
46450     if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
46451       eMode = eOld;
46452     }
46453   }
46454 
46455   if( eMode!=eOld ){
46456 
46457     /* Change the journal mode. */
46458     assert( pPager->eState!=PAGER_ERROR );
46459     pPager->journalMode = (u8)eMode;
46460 
46461     /* When transistioning from TRUNCATE or PERSIST to any other journal
46462     ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
46463     ** delete the journal file.
46464     */
46465     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
46466     assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
46467     assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
46468     assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
46469     assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
46470     assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
46471 
46472     assert( isOpen(pPager->fd) || pPager->exclusiveMode );
46473     if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
46474 
46475       /* In this case we would like to delete the journal file. If it is
46476       ** not possible, then that is not a problem. Deleting the journal file
46477       ** here is an optimization only.
46478       **
46479       ** Before deleting the journal file, obtain a RESERVED lock on the
46480       ** database file. This ensures that the journal file is not deleted
46481       ** while it is in use by some other client.
46482       */
46483       sqlite3OsClose(pPager->jfd);
46484       if( pPager->eLock>=RESERVED_LOCK ){
46485         sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
46486       }else{
46487         int rc = SQLITE_OK;
46488         int state = pPager->eState;
46489         assert( state==PAGER_OPEN || state==PAGER_READER );
46490         if( state==PAGER_OPEN ){
46491           rc = sqlite3PagerSharedLock(pPager);
46492         }
46493         if( pPager->eState==PAGER_READER ){
46494           assert( rc==SQLITE_OK );
46495           rc = pagerLockDb(pPager, RESERVED_LOCK);
46496         }
46497         if( rc==SQLITE_OK ){
46498           sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
46499         }
46500         if( rc==SQLITE_OK && state==PAGER_READER ){
46501           pagerUnlockDb(pPager, SHARED_LOCK);
46502         }else if( state==PAGER_OPEN ){
46503           pager_unlock(pPager);
46504         }
46505         assert( state==pPager->eState );
46506       }
46507     }
46508   }
46509 
46510   /* Return the new journal mode */
46511   return (int)pPager->journalMode;
46512 }
46513 
46514 /*
46515 ** Return the current journal mode.
46516 */
46517 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
46518   return (int)pPager->journalMode;
46519 }
46520 
46521 /*
46522 ** Return TRUE if the pager is in a state where it is OK to change the
46523 ** journalmode.  Journalmode changes can only happen when the database
46524 ** is unmodified.
46525 */
46526 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
46527   assert( assert_pager_state(pPager) );
46528   if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
46529   if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
46530   return 1;
46531 }
46532 
46533 /*
46534 ** Get/set the size-limit used for persistent journal files.
46535 **
46536 ** Setting the size limit to -1 means no limit is enforced.
46537 ** An attempt to set a limit smaller than -1 is a no-op.
46538 */
46539 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
46540   if( iLimit>=-1 ){
46541     pPager->journalSizeLimit = iLimit;
46542     sqlite3WalLimit(pPager->pWal, iLimit);
46543   }
46544   return pPager->journalSizeLimit;
46545 }
46546 
46547 /*
46548 ** Return a pointer to the pPager->pBackup variable. The backup module
46549 ** in backup.c maintains the content of this variable. This module
46550 ** uses it opaquely as an argument to sqlite3BackupRestart() and
46551 ** sqlite3BackupUpdate() only.
46552 */
46553 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
46554   return &pPager->pBackup;
46555 }
46556 
46557 #ifndef SQLITE_OMIT_VACUUM
46558 /*
46559 ** Unless this is an in-memory or temporary database, clear the pager cache.
46560 */
46561 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
46562   if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
46563 }
46564 #endif
46565 
46566 #ifndef SQLITE_OMIT_WAL
46567 /*
46568 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
46569 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
46570 ** or wal_blocking_checkpoint() API functions.
46571 **
46572 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
46573 */
46574 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
46575   int rc = SQLITE_OK;
46576   if( pPager->pWal ){
46577     rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
46578         pPager->xBusyHandler, pPager->pBusyHandlerArg,
46579         pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
46580         pnLog, pnCkpt
46581     );
46582   }
46583   return rc;
46584 }
46585 
46586 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
46587   return sqlite3WalCallback(pPager->pWal);
46588 }
46589 
46590 /*
46591 ** Return true if the underlying VFS for the given pager supports the
46592 ** primitives necessary for write-ahead logging.
46593 */
46594 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
46595   const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
46596   return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
46597 }
46598 
46599 /*
46600 ** Attempt to take an exclusive lock on the database file. If a PENDING lock
46601 ** is obtained instead, immediately release it.
46602 */
46603 static int pagerExclusiveLock(Pager *pPager){
46604   int rc;                         /* Return code */
46605 
46606   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
46607   rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
46608   if( rc!=SQLITE_OK ){
46609     /* If the attempt to grab the exclusive lock failed, release the
46610     ** pending lock that may have been obtained instead.  */
46611     pagerUnlockDb(pPager, SHARED_LOCK);
46612   }
46613 
46614   return rc;
46615 }
46616 
46617 /*
46618 ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
46619 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
46620 ** lock on the database file and use heap-memory to store the wal-index
46621 ** in. Otherwise, use the normal shared-memory.
46622 */
46623 static int pagerOpenWal(Pager *pPager){
46624   int rc = SQLITE_OK;
46625 
46626   assert( pPager->pWal==0 && pPager->tempFile==0 );
46627   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
46628 
46629   /* If the pager is already in exclusive-mode, the WAL module will use
46630   ** heap-memory for the wal-index instead of the VFS shared-memory
46631   ** implementation. Take the exclusive lock now, before opening the WAL
46632   ** file, to make sure this is safe.
46633   */
46634   if( pPager->exclusiveMode ){
46635     rc = pagerExclusiveLock(pPager);
46636   }
46637 
46638   /* Open the connection to the log file. If this operation fails,
46639   ** (e.g. due to malloc() failure), return an error code.
46640   */
46641   if( rc==SQLITE_OK ){
46642     rc = sqlite3WalOpen(pPager->pVfs,
46643         pPager->fd, pPager->zWal, pPager->exclusiveMode,
46644         pPager->journalSizeLimit, &pPager->pWal
46645     );
46646   }
46647   pagerFixMaplimit(pPager);
46648 
46649   return rc;
46650 }
46651 
46652 
46653 /*
46654 ** The caller must be holding a SHARED lock on the database file to call
46655 ** this function.
46656 **
46657 ** If the pager passed as the first argument is open on a real database
46658 ** file (not a temp file or an in-memory database), and the WAL file
46659 ** is not already open, make an attempt to open it now. If successful,
46660 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does
46661 ** not support the xShmXXX() methods, return an error code. *pbOpen is
46662 ** not modified in either case.
46663 **
46664 ** If the pager is open on a temp-file (or in-memory database), or if
46665 ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
46666 ** without doing anything.
46667 */
46668 SQLITE_PRIVATE int sqlite3PagerOpenWal(
46669   Pager *pPager,                  /* Pager object */
46670   int *pbOpen                     /* OUT: Set to true if call is a no-op */
46671 ){
46672   int rc = SQLITE_OK;             /* Return code */
46673 
46674   assert( assert_pager_state(pPager) );
46675   assert( pPager->eState==PAGER_OPEN   || pbOpen );
46676   assert( pPager->eState==PAGER_READER || !pbOpen );
46677   assert( pbOpen==0 || *pbOpen==0 );
46678   assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
46679 
46680   if( !pPager->tempFile && !pPager->pWal ){
46681     if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
46682 
46683     /* Close any rollback journal previously open */
46684     sqlite3OsClose(pPager->jfd);
46685 
46686     rc = pagerOpenWal(pPager);
46687     if( rc==SQLITE_OK ){
46688       pPager->journalMode = PAGER_JOURNALMODE_WAL;
46689       pPager->eState = PAGER_OPEN;
46690     }
46691   }else{
46692     *pbOpen = 1;
46693   }
46694 
46695   return rc;
46696 }
46697 
46698 /*
46699 ** This function is called to close the connection to the log file prior
46700 ** to switching from WAL to rollback mode.
46701 **
46702 ** Before closing the log file, this function attempts to take an
46703 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
46704 ** error (SQLITE_BUSY) is returned and the log connection is not closed.
46705 ** If successful, the EXCLUSIVE lock is not released before returning.
46706 */
46707 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
46708   int rc = SQLITE_OK;
46709 
46710   assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
46711 
46712   /* If the log file is not already open, but does exist in the file-system,
46713   ** it may need to be checkpointed before the connection can switch to
46714   ** rollback mode. Open it now so this can happen.
46715   */
46716   if( !pPager->pWal ){
46717     int logexists = 0;
46718     rc = pagerLockDb(pPager, SHARED_LOCK);
46719     if( rc==SQLITE_OK ){
46720       rc = sqlite3OsAccess(
46721           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
46722       );
46723     }
46724     if( rc==SQLITE_OK && logexists ){
46725       rc = pagerOpenWal(pPager);
46726     }
46727   }
46728 
46729   /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
46730   ** the database file, the log and log-summary files will be deleted.
46731   */
46732   if( rc==SQLITE_OK && pPager->pWal ){
46733     rc = pagerExclusiveLock(pPager);
46734     if( rc==SQLITE_OK ){
46735       rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
46736                            pPager->pageSize, (u8*)pPager->pTmpSpace);
46737       pPager->pWal = 0;
46738       pagerFixMaplimit(pPager);
46739     }
46740   }
46741   return rc;
46742 }
46743 
46744 #endif /* !SQLITE_OMIT_WAL */
46745 
46746 #ifdef SQLITE_ENABLE_ZIPVFS
46747 /*
46748 ** A read-lock must be held on the pager when this function is called. If
46749 ** the pager is in WAL mode and the WAL file currently contains one or more
46750 ** frames, return the size in bytes of the page images stored within the
46751 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
46752 ** is empty, return 0.
46753 */
46754 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
46755   assert( pPager->eState==PAGER_READER );
46756   return sqlite3WalFramesize(pPager->pWal);
46757 }
46758 #endif
46759 
46760 #endif /* SQLITE_OMIT_DISKIO */
46761 
46762 /************** End of pager.c ***********************************************/
46763 /************** Begin file wal.c *********************************************/
46764 /*
46765 ** 2010 February 1
46766 **
46767 ** The author disclaims copyright to this source code.  In place of
46768 ** a legal notice, here is a blessing:
46769 **
46770 **    May you do good and not evil.
46771 **    May you find forgiveness for yourself and forgive others.
46772 **    May you share freely, never taking more than you give.
46773 **
46774 *************************************************************************
46775 **
46776 ** This file contains the implementation of a write-ahead log (WAL) used in
46777 ** "journal_mode=WAL" mode.
46778 **
46779 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
46780 **
46781 ** A WAL file consists of a header followed by zero or more "frames".
46782 ** Each frame records the revised content of a single page from the
46783 ** database file.  All changes to the database are recorded by writing
46784 ** frames into the WAL.  Transactions commit when a frame is written that
46785 ** contains a commit marker.  A single WAL can and usually does record
46786 ** multiple transactions.  Periodically, the content of the WAL is
46787 ** transferred back into the database file in an operation called a
46788 ** "checkpoint".
46789 **
46790 ** A single WAL file can be used multiple times.  In other words, the
46791 ** WAL can fill up with frames and then be checkpointed and then new
46792 ** frames can overwrite the old ones.  A WAL always grows from beginning
46793 ** toward the end.  Checksums and counters attached to each frame are
46794 ** used to determine which frames within the WAL are valid and which
46795 ** are leftovers from prior checkpoints.
46796 **
46797 ** The WAL header is 32 bytes in size and consists of the following eight
46798 ** big-endian 32-bit unsigned integer values:
46799 **
46800 **     0: Magic number.  0x377f0682 or 0x377f0683
46801 **     4: File format version.  Currently 3007000
46802 **     8: Database page size.  Example: 1024
46803 **    12: Checkpoint sequence number
46804 **    16: Salt-1, random integer incremented with each checkpoint
46805 **    20: Salt-2, a different random integer changing with each ckpt
46806 **    24: Checksum-1 (first part of checksum for first 24 bytes of header).
46807 **    28: Checksum-2 (second part of checksum for first 24 bytes of header).
46808 **
46809 ** Immediately following the wal-header are zero or more frames. Each
46810 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
46811 ** of page data. The frame-header is six big-endian 32-bit unsigned
46812 ** integer values, as follows:
46813 **
46814 **     0: Page number.
46815 **     4: For commit records, the size of the database image in pages
46816 **        after the commit. For all other records, zero.
46817 **     8: Salt-1 (copied from the header)
46818 **    12: Salt-2 (copied from the header)
46819 **    16: Checksum-1.
46820 **    20: Checksum-2.
46821 **
46822 ** A frame is considered valid if and only if the following conditions are
46823 ** true:
46824 **
46825 **    (1) The salt-1 and salt-2 values in the frame-header match
46826 **        salt values in the wal-header
46827 **
46828 **    (2) The checksum values in the final 8 bytes of the frame-header
46829 **        exactly match the checksum computed consecutively on the
46830 **        WAL header and the first 8 bytes and the content of all frames
46831 **        up to and including the current frame.
46832 **
46833 ** The checksum is computed using 32-bit big-endian integers if the
46834 ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
46835 ** is computed using little-endian if the magic number is 0x377f0682.
46836 ** The checksum values are always stored in the frame header in a
46837 ** big-endian format regardless of which byte order is used to compute
46838 ** the checksum.  The checksum is computed by interpreting the input as
46839 ** an even number of unsigned 32-bit integers: x[0] through x[N].  The
46840 ** algorithm used for the checksum is as follows:
46841 **
46842 **   for i from 0 to n-1 step 2:
46843 **     s0 += x[i] + s1;
46844 **     s1 += x[i+1] + s0;
46845 **   endfor
46846 **
46847 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
46848 ** in reverse order (the largest fibonacci weight occurs on the first element
46849 ** of the sequence being summed.)  The s1 value spans all 32-bit
46850 ** terms of the sequence whereas s0 omits the final term.
46851 **
46852 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
46853 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
46854 ** The VFS.xSync operations serve as write barriers - all writes launched
46855 ** before the xSync must complete before any write that launches after the
46856 ** xSync begins.
46857 **
46858 ** After each checkpoint, the salt-1 value is incremented and the salt-2
46859 ** value is randomized.  This prevents old and new frames in the WAL from
46860 ** being considered valid at the same time and being checkpointing together
46861 ** following a crash.
46862 **
46863 ** READER ALGORITHM
46864 **
46865 ** To read a page from the database (call it page number P), a reader
46866 ** first checks the WAL to see if it contains page P.  If so, then the
46867 ** last valid instance of page P that is a followed by a commit frame
46868 ** or is a commit frame itself becomes the value read.  If the WAL
46869 ** contains no copies of page P that are valid and which are a commit
46870 ** frame or are followed by a commit frame, then page P is read from
46871 ** the database file.
46872 **
46873 ** To start a read transaction, the reader records the index of the last
46874 ** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
46875 ** for all subsequent read operations.  New transactions can be appended
46876 ** to the WAL, but as long as the reader uses its original mxFrame value
46877 ** and ignores the newly appended content, it will see a consistent snapshot
46878 ** of the database from a single point in time.  This technique allows
46879 ** multiple concurrent readers to view different versions of the database
46880 ** content simultaneously.
46881 **
46882 ** The reader algorithm in the previous paragraphs works correctly, but
46883 ** because frames for page P can appear anywhere within the WAL, the
46884 ** reader has to scan the entire WAL looking for page P frames.  If the
46885 ** WAL is large (multiple megabytes is typical) that scan can be slow,
46886 ** and read performance suffers.  To overcome this problem, a separate
46887 ** data structure called the wal-index is maintained to expedite the
46888 ** search for frames of a particular page.
46889 **
46890 ** WAL-INDEX FORMAT
46891 **
46892 ** Conceptually, the wal-index is shared memory, though VFS implementations
46893 ** might choose to implement the wal-index using a mmapped file.  Because
46894 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
46895 ** on a network filesystem.  All users of the database must be able to
46896 ** share memory.
46897 **
46898 ** The wal-index is transient.  After a crash, the wal-index can (and should
46899 ** be) reconstructed from the original WAL file.  In fact, the VFS is required
46900 ** to either truncate or zero the header of the wal-index when the last
46901 ** connection to it closes.  Because the wal-index is transient, it can
46902 ** use an architecture-specific format; it does not have to be cross-platform.
46903 ** Hence, unlike the database and WAL file formats which store all values
46904 ** as big endian, the wal-index can store multi-byte values in the native
46905 ** byte order of the host computer.
46906 **
46907 ** The purpose of the wal-index is to answer this question quickly:  Given
46908 ** a page number P and a maximum frame index M, return the index of the
46909 ** last frame in the wal before frame M for page P in the WAL, or return
46910 ** NULL if there are no frames for page P in the WAL prior to M.
46911 **
46912 ** The wal-index consists of a header region, followed by an one or
46913 ** more index blocks.
46914 **
46915 ** The wal-index header contains the total number of frames within the WAL
46916 ** in the mxFrame field.
46917 **
46918 ** Each index block except for the first contains information on
46919 ** HASHTABLE_NPAGE frames. The first index block contains information on
46920 ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
46921 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
46922 ** first index block are the same size as all other index blocks in the
46923 ** wal-index.
46924 **
46925 ** Each index block contains two sections, a page-mapping that contains the
46926 ** database page number associated with each wal frame, and a hash-table
46927 ** that allows readers to query an index block for a specific page number.
46928 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
46929 ** for the first index block) 32-bit page numbers. The first entry in the
46930 ** first index-block contains the database page number corresponding to the
46931 ** first frame in the WAL file. The first entry in the second index block
46932 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
46933 ** the log, and so on.
46934 **
46935 ** The last index block in a wal-index usually contains less than the full
46936 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
46937 ** depending on the contents of the WAL file. This does not change the
46938 ** allocated size of the page-mapping array - the page-mapping array merely
46939 ** contains unused entries.
46940 **
46941 ** Even without using the hash table, the last frame for page P
46942 ** can be found by scanning the page-mapping sections of each index block
46943 ** starting with the last index block and moving toward the first, and
46944 ** within each index block, starting at the end and moving toward the
46945 ** beginning.  The first entry that equals P corresponds to the frame
46946 ** holding the content for that page.
46947 **
46948 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
46949 ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
46950 ** hash table for each page number in the mapping section, so the hash
46951 ** table is never more than half full.  The expected number of collisions
46952 ** prior to finding a match is 1.  Each entry of the hash table is an
46953 ** 1-based index of an entry in the mapping section of the same
46954 ** index block.   Let K be the 1-based index of the largest entry in
46955 ** the mapping section.  (For index blocks other than the last, K will
46956 ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
46957 ** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
46958 ** contain a value of 0.
46959 **
46960 ** To look for page P in the hash table, first compute a hash iKey on
46961 ** P as follows:
46962 **
46963 **      iKey = (P * 383) % HASHTABLE_NSLOT
46964 **
46965 ** Then start scanning entries of the hash table, starting with iKey
46966 ** (wrapping around to the beginning when the end of the hash table is
46967 ** reached) until an unused hash slot is found. Let the first unused slot
46968 ** be at index iUnused.  (iUnused might be less than iKey if there was
46969 ** wrap-around.) Because the hash table is never more than half full,
46970 ** the search is guaranteed to eventually hit an unused entry.  Let
46971 ** iMax be the value between iKey and iUnused, closest to iUnused,
46972 ** where aHash[iMax]==P.  If there is no iMax entry (if there exists
46973 ** no hash slot such that aHash[i]==p) then page P is not in the
46974 ** current index block.  Otherwise the iMax-th mapping entry of the
46975 ** current index block corresponds to the last entry that references
46976 ** page P.
46977 **
46978 ** A hash search begins with the last index block and moves toward the
46979 ** first index block, looking for entries corresponding to page P.  On
46980 ** average, only two or three slots in each index block need to be
46981 ** examined in order to either find the last entry for page P, or to
46982 ** establish that no such entry exists in the block.  Each index block
46983 ** holds over 4000 entries.  So two or three index blocks are sufficient
46984 ** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
46985 ** comparisons (on average) suffice to either locate a frame in the
46986 ** WAL or to establish that the frame does not exist in the WAL.  This
46987 ** is much faster than scanning the entire 10MB WAL.
46988 **
46989 ** Note that entries are added in order of increasing K.  Hence, one
46990 ** reader might be using some value K0 and a second reader that started
46991 ** at a later time (after additional transactions were added to the WAL
46992 ** and to the wal-index) might be using a different value K1, where K1>K0.
46993 ** Both readers can use the same hash table and mapping section to get
46994 ** the correct result.  There may be entries in the hash table with
46995 ** K>K0 but to the first reader, those entries will appear to be unused
46996 ** slots in the hash table and so the first reader will get an answer as
46997 ** if no values greater than K0 had ever been inserted into the hash table
46998 ** in the first place - which is what reader one wants.  Meanwhile, the
46999 ** second reader using K1 will see additional values that were inserted
47000 ** later, which is exactly what reader two wants.
47001 **
47002 ** When a rollback occurs, the value of K is decreased. Hash table entries
47003 ** that correspond to frames greater than the new K value are removed
47004 ** from the hash table at this point.
47005 */
47006 #ifndef SQLITE_OMIT_WAL
47007 
47008 
47009 /*
47010 ** Trace output macros
47011 */
47012 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
47013 SQLITE_PRIVATE int sqlite3WalTrace = 0;
47014 # define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
47015 #else
47016 # define WALTRACE(X)
47017 #endif
47018 
47019 /*
47020 ** The maximum (and only) versions of the wal and wal-index formats
47021 ** that may be interpreted by this version of SQLite.
47022 **
47023 ** If a client begins recovering a WAL file and finds that (a) the checksum
47024 ** values in the wal-header are correct and (b) the version field is not
47025 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
47026 **
47027 ** Similarly, if a client successfully reads a wal-index header (i.e. the
47028 ** checksum test is successful) and finds that the version field is not
47029 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
47030 ** returns SQLITE_CANTOPEN.
47031 */
47032 #define WAL_MAX_VERSION      3007000
47033 #define WALINDEX_MAX_VERSION 3007000
47034 
47035 /*
47036 ** Indices of various locking bytes.   WAL_NREADER is the number
47037 ** of available reader locks and should be at least 3.
47038 */
47039 #define WAL_WRITE_LOCK         0
47040 #define WAL_ALL_BUT_WRITE      1
47041 #define WAL_CKPT_LOCK          1
47042 #define WAL_RECOVER_LOCK       2
47043 #define WAL_READ_LOCK(I)       (3+(I))
47044 #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
47045 
47046 
47047 /* Object declarations */
47048 typedef struct WalIndexHdr WalIndexHdr;
47049 typedef struct WalIterator WalIterator;
47050 typedef struct WalCkptInfo WalCkptInfo;
47051 
47052 
47053 /*
47054 ** The following object holds a copy of the wal-index header content.
47055 **
47056 ** The actual header in the wal-index consists of two copies of this
47057 ** object.
47058 **
47059 ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
47060 ** Or it can be 1 to represent a 65536-byte page.  The latter case was
47061 ** added in 3.7.1 when support for 64K pages was added.
47062 */
47063 struct WalIndexHdr {
47064   u32 iVersion;                   /* Wal-index version */
47065   u32 unused;                     /* Unused (padding) field */
47066   u32 iChange;                    /* Counter incremented each transaction */
47067   u8 isInit;                      /* 1 when initialized */
47068   u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
47069   u16 szPage;                     /* Database page size in bytes. 1==64K */
47070   u32 mxFrame;                    /* Index of last valid frame in the WAL */
47071   u32 nPage;                      /* Size of database in pages */
47072   u32 aFrameCksum[2];             /* Checksum of last frame in log */
47073   u32 aSalt[2];                   /* Two salt values copied from WAL header */
47074   u32 aCksum[2];                  /* Checksum over all prior fields */
47075 };
47076 
47077 /*
47078 ** A copy of the following object occurs in the wal-index immediately
47079 ** following the second copy of the WalIndexHdr.  This object stores
47080 ** information used by checkpoint.
47081 **
47082 ** nBackfill is the number of frames in the WAL that have been written
47083 ** back into the database. (We call the act of moving content from WAL to
47084 ** database "backfilling".)  The nBackfill number is never greater than
47085 ** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
47086 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
47087 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
47088 ** mxFrame back to zero when the WAL is reset.
47089 **
47090 ** There is one entry in aReadMark[] for each reader lock.  If a reader
47091 ** holds read-lock K, then the value in aReadMark[K] is no greater than
47092 ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
47093 ** for any aReadMark[] means that entry is unused.  aReadMark[0] is
47094 ** a special case; its value is never used and it exists as a place-holder
47095 ** to avoid having to offset aReadMark[] indexs by one.  Readers holding
47096 ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
47097 ** directly from the database.
47098 **
47099 ** The value of aReadMark[K] may only be changed by a thread that
47100 ** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
47101 ** aReadMark[K] cannot changed while there is a reader is using that mark
47102 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
47103 **
47104 ** The checkpointer may only transfer frames from WAL to database where
47105 ** the frame numbers are less than or equal to every aReadMark[] that is
47106 ** in use (that is, every aReadMark[j] for which there is a corresponding
47107 ** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
47108 ** largest value and will increase an unused aReadMark[] to mxFrame if there
47109 ** is not already an aReadMark[] equal to mxFrame.  The exception to the
47110 ** previous sentence is when nBackfill equals mxFrame (meaning that everything
47111 ** in the WAL has been backfilled into the database) then new readers
47112 ** will choose aReadMark[0] which has value 0 and hence such reader will
47113 ** get all their all content directly from the database file and ignore
47114 ** the WAL.
47115 **
47116 ** Writers normally append new frames to the end of the WAL.  However,
47117 ** if nBackfill equals mxFrame (meaning that all WAL content has been
47118 ** written back into the database) and if no readers are using the WAL
47119 ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
47120 ** the writer will first "reset" the WAL back to the beginning and start
47121 ** writing new content beginning at frame 1.
47122 **
47123 ** We assume that 32-bit loads are atomic and so no locks are needed in
47124 ** order to read from any aReadMark[] entries.
47125 */
47126 struct WalCkptInfo {
47127   u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
47128   u32 aReadMark[WAL_NREADER];     /* Reader marks */
47129 };
47130 #define READMARK_NOT_USED  0xffffffff
47131 
47132 
47133 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
47134 ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
47135 ** only support mandatory file-locks, we do not read or write data
47136 ** from the region of the file on which locks are applied.
47137 */
47138 #define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
47139 #define WALINDEX_LOCK_RESERVED 16
47140 #define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
47141 
47142 /* Size of header before each frame in wal */
47143 #define WAL_FRAME_HDRSIZE 24
47144 
47145 /* Size of write ahead log header, including checksum. */
47146 /* #define WAL_HDRSIZE 24 */
47147 #define WAL_HDRSIZE 32
47148 
47149 /* WAL magic value. Either this value, or the same value with the least
47150 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
47151 ** big-endian format in the first 4 bytes of a WAL file.
47152 **
47153 ** If the LSB is set, then the checksums for each frame within the WAL
47154 ** file are calculated by treating all data as an array of 32-bit
47155 ** big-endian words. Otherwise, they are calculated by interpreting
47156 ** all data as 32-bit little-endian words.
47157 */
47158 #define WAL_MAGIC 0x377f0682
47159 
47160 /*
47161 ** Return the offset of frame iFrame in the write-ahead log file,
47162 ** assuming a database page size of szPage bytes. The offset returned
47163 ** is to the start of the write-ahead log frame-header.
47164 */
47165 #define walFrameOffset(iFrame, szPage) (                               \
47166   WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
47167 )
47168 
47169 /*
47170 ** An open write-ahead log file is represented by an instance of the
47171 ** following object.
47172 */
47173 struct Wal {
47174   sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
47175   sqlite3_file *pDbFd;       /* File handle for the database file */
47176   sqlite3_file *pWalFd;      /* File handle for WAL file */
47177   u32 iCallback;             /* Value to pass to log callback (or 0) */
47178   i64 mxWalSize;             /* Truncate WAL to this size upon reset */
47179   int nWiData;               /* Size of array apWiData */
47180   int szFirstBlock;          /* Size of first block written to WAL file */
47181   volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
47182   u32 szPage;                /* Database page size */
47183   i16 readLock;              /* Which read lock is being held.  -1 for none */
47184   u8 syncFlags;              /* Flags to use to sync header writes */
47185   u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
47186   u8 writeLock;              /* True if in a write transaction */
47187   u8 ckptLock;               /* True if holding a checkpoint lock */
47188   u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
47189   u8 truncateOnCommit;       /* True to truncate WAL file on commit */
47190   u8 syncHeader;             /* Fsync the WAL header if true */
47191   u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
47192   WalIndexHdr hdr;           /* Wal-index header for current transaction */
47193   const char *zWalName;      /* Name of WAL file */
47194   u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
47195 #ifdef SQLITE_DEBUG
47196   u8 lockError;              /* True if a locking error has occurred */
47197 #endif
47198 };
47199 
47200 /*
47201 ** Candidate values for Wal.exclusiveMode.
47202 */
47203 #define WAL_NORMAL_MODE     0
47204 #define WAL_EXCLUSIVE_MODE  1
47205 #define WAL_HEAPMEMORY_MODE 2
47206 
47207 /*
47208 ** Possible values for WAL.readOnly
47209 */
47210 #define WAL_RDWR        0    /* Normal read/write connection */
47211 #define WAL_RDONLY      1    /* The WAL file is readonly */
47212 #define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
47213 
47214 /*
47215 ** Each page of the wal-index mapping contains a hash-table made up of
47216 ** an array of HASHTABLE_NSLOT elements of the following type.
47217 */
47218 typedef u16 ht_slot;
47219 
47220 /*
47221 ** This structure is used to implement an iterator that loops through
47222 ** all frames in the WAL in database page order. Where two or more frames
47223 ** correspond to the same database page, the iterator visits only the
47224 ** frame most recently written to the WAL (in other words, the frame with
47225 ** the largest index).
47226 **
47227 ** The internals of this structure are only accessed by:
47228 **
47229 **   walIteratorInit() - Create a new iterator,
47230 **   walIteratorNext() - Step an iterator,
47231 **   walIteratorFree() - Free an iterator.
47232 **
47233 ** This functionality is used by the checkpoint code (see walCheckpoint()).
47234 */
47235 struct WalIterator {
47236   int iPrior;                     /* Last result returned from the iterator */
47237   int nSegment;                   /* Number of entries in aSegment[] */
47238   struct WalSegment {
47239     int iNext;                    /* Next slot in aIndex[] not yet returned */
47240     ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
47241     u32 *aPgno;                   /* Array of page numbers. */
47242     int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
47243     int iZero;                    /* Frame number associated with aPgno[0] */
47244   } aSegment[1];                  /* One for every 32KB page in the wal-index */
47245 };
47246 
47247 /*
47248 ** Define the parameters of the hash tables in the wal-index file. There
47249 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
47250 ** wal-index.
47251 **
47252 ** Changing any of these constants will alter the wal-index format and
47253 ** create incompatibilities.
47254 */
47255 #define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
47256 #define HASHTABLE_HASH_1     383                  /* Should be prime */
47257 #define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
47258 
47259 /*
47260 ** The block of page numbers associated with the first hash-table in a
47261 ** wal-index is smaller than usual. This is so that there is a complete
47262 ** hash-table on each aligned 32KB page of the wal-index.
47263 */
47264 #define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
47265 
47266 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
47267 #define WALINDEX_PGSZ   (                                         \
47268     sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
47269 )
47270 
47271 /*
47272 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
47273 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
47274 ** numbered from zero.
47275 **
47276 ** If this call is successful, *ppPage is set to point to the wal-index
47277 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
47278 ** then an SQLite error code is returned and *ppPage is set to 0.
47279 */
47280 static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
47281   int rc = SQLITE_OK;
47282 
47283   /* Enlarge the pWal->apWiData[] array if required */
47284   if( pWal->nWiData<=iPage ){
47285     int nByte = sizeof(u32*)*(iPage+1);
47286     volatile u32 **apNew;
47287     apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
47288     if( !apNew ){
47289       *ppPage = 0;
47290       return SQLITE_NOMEM;
47291     }
47292     memset((void*)&apNew[pWal->nWiData], 0,
47293            sizeof(u32*)*(iPage+1-pWal->nWiData));
47294     pWal->apWiData = apNew;
47295     pWal->nWiData = iPage+1;
47296   }
47297 
47298   /* Request a pointer to the required page from the VFS */
47299   if( pWal->apWiData[iPage]==0 ){
47300     if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
47301       pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
47302       if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
47303     }else{
47304       rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
47305           pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
47306       );
47307       if( rc==SQLITE_READONLY ){
47308         pWal->readOnly |= WAL_SHM_RDONLY;
47309         rc = SQLITE_OK;
47310       }
47311     }
47312   }
47313 
47314   *ppPage = pWal->apWiData[iPage];
47315   assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
47316   return rc;
47317 }
47318 
47319 /*
47320 ** Return a pointer to the WalCkptInfo structure in the wal-index.
47321 */
47322 static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
47323   assert( pWal->nWiData>0 && pWal->apWiData[0] );
47324   return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
47325 }
47326 
47327 /*
47328 ** Return a pointer to the WalIndexHdr structure in the wal-index.
47329 */
47330 static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
47331   assert( pWal->nWiData>0 && pWal->apWiData[0] );
47332   return (volatile WalIndexHdr*)pWal->apWiData[0];
47333 }
47334 
47335 /*
47336 ** The argument to this macro must be of type u32. On a little-endian
47337 ** architecture, it returns the u32 value that results from interpreting
47338 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
47339 ** returns the value that would be produced by intepreting the 4 bytes
47340 ** of the input value as a little-endian integer.
47341 */
47342 #define BYTESWAP32(x) ( \
47343     (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
47344   + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
47345 )
47346 
47347 /*
47348 ** Generate or extend an 8 byte checksum based on the data in
47349 ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
47350 ** initial values of 0 and 0 if aIn==NULL).
47351 **
47352 ** The checksum is written back into aOut[] before returning.
47353 **
47354 ** nByte must be a positive multiple of 8.
47355 */
47356 static void walChecksumBytes(
47357   int nativeCksum, /* True for native byte-order, false for non-native */
47358   u8 *a,           /* Content to be checksummed */
47359   int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
47360   const u32 *aIn,  /* Initial checksum value input */
47361   u32 *aOut        /* OUT: Final checksum value output */
47362 ){
47363   u32 s1, s2;
47364   u32 *aData = (u32 *)a;
47365   u32 *aEnd = (u32 *)&a[nByte];
47366 
47367   if( aIn ){
47368     s1 = aIn[0];
47369     s2 = aIn[1];
47370   }else{
47371     s1 = s2 = 0;
47372   }
47373 
47374   assert( nByte>=8 );
47375   assert( (nByte&0x00000007)==0 );
47376 
47377   if( nativeCksum ){
47378     do {
47379       s1 += *aData++ + s2;
47380       s2 += *aData++ + s1;
47381     }while( aData<aEnd );
47382   }else{
47383     do {
47384       s1 += BYTESWAP32(aData[0]) + s2;
47385       s2 += BYTESWAP32(aData[1]) + s1;
47386       aData += 2;
47387     }while( aData<aEnd );
47388   }
47389 
47390   aOut[0] = s1;
47391   aOut[1] = s2;
47392 }
47393 
47394 static void walShmBarrier(Wal *pWal){
47395   if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
47396     sqlite3OsShmBarrier(pWal->pDbFd);
47397   }
47398 }
47399 
47400 /*
47401 ** Write the header information in pWal->hdr into the wal-index.
47402 **
47403 ** The checksum on pWal->hdr is updated before it is written.
47404 */
47405 static void walIndexWriteHdr(Wal *pWal){
47406   volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
47407   const int nCksum = offsetof(WalIndexHdr, aCksum);
47408 
47409   assert( pWal->writeLock );
47410   pWal->hdr.isInit = 1;
47411   pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
47412   walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
47413   memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
47414   walShmBarrier(pWal);
47415   memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
47416 }
47417 
47418 /*
47419 ** This function encodes a single frame header and writes it to a buffer
47420 ** supplied by the caller. A frame-header is made up of a series of
47421 ** 4-byte big-endian integers, as follows:
47422 **
47423 **     0: Page number.
47424 **     4: For commit records, the size of the database image in pages
47425 **        after the commit. For all other records, zero.
47426 **     8: Salt-1 (copied from the wal-header)
47427 **    12: Salt-2 (copied from the wal-header)
47428 **    16: Checksum-1.
47429 **    20: Checksum-2.
47430 */
47431 static void walEncodeFrame(
47432   Wal *pWal,                      /* The write-ahead log */
47433   u32 iPage,                      /* Database page number for frame */
47434   u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
47435   u8 *aData,                      /* Pointer to page data */
47436   u8 *aFrame                      /* OUT: Write encoded frame here */
47437 ){
47438   int nativeCksum;                /* True for native byte-order checksums */
47439   u32 *aCksum = pWal->hdr.aFrameCksum;
47440   assert( WAL_FRAME_HDRSIZE==24 );
47441   sqlite3Put4byte(&aFrame[0], iPage);
47442   sqlite3Put4byte(&aFrame[4], nTruncate);
47443   memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
47444 
47445   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
47446   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
47447   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
47448 
47449   sqlite3Put4byte(&aFrame[16], aCksum[0]);
47450   sqlite3Put4byte(&aFrame[20], aCksum[1]);
47451 }
47452 
47453 /*
47454 ** Check to see if the frame with header in aFrame[] and content
47455 ** in aData[] is valid.  If it is a valid frame, fill *piPage and
47456 ** *pnTruncate and return true.  Return if the frame is not valid.
47457 */
47458 static int walDecodeFrame(
47459   Wal *pWal,                      /* The write-ahead log */
47460   u32 *piPage,                    /* OUT: Database page number for frame */
47461   u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
47462   u8 *aData,                      /* Pointer to page data (for checksum) */
47463   u8 *aFrame                      /* Frame data */
47464 ){
47465   int nativeCksum;                /* True for native byte-order checksums */
47466   u32 *aCksum = pWal->hdr.aFrameCksum;
47467   u32 pgno;                       /* Page number of the frame */
47468   assert( WAL_FRAME_HDRSIZE==24 );
47469 
47470   /* A frame is only valid if the salt values in the frame-header
47471   ** match the salt values in the wal-header.
47472   */
47473   if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
47474     return 0;
47475   }
47476 
47477   /* A frame is only valid if the page number is creater than zero.
47478   */
47479   pgno = sqlite3Get4byte(&aFrame[0]);
47480   if( pgno==0 ){
47481     return 0;
47482   }
47483 
47484   /* A frame is only valid if a checksum of the WAL header,
47485   ** all prior frams, the first 16 bytes of this frame-header,
47486   ** and the frame-data matches the checksum in the last 8
47487   ** bytes of this frame-header.
47488   */
47489   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
47490   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
47491   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
47492   if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
47493    || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
47494   ){
47495     /* Checksum failed. */
47496     return 0;
47497   }
47498 
47499   /* If we reach this point, the frame is valid.  Return the page number
47500   ** and the new database size.
47501   */
47502   *piPage = pgno;
47503   *pnTruncate = sqlite3Get4byte(&aFrame[4]);
47504   return 1;
47505 }
47506 
47507 
47508 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
47509 /*
47510 ** Names of locks.  This routine is used to provide debugging output and is not
47511 ** a part of an ordinary build.
47512 */
47513 static const char *walLockName(int lockIdx){
47514   if( lockIdx==WAL_WRITE_LOCK ){
47515     return "WRITE-LOCK";
47516   }else if( lockIdx==WAL_CKPT_LOCK ){
47517     return "CKPT-LOCK";
47518   }else if( lockIdx==WAL_RECOVER_LOCK ){
47519     return "RECOVER-LOCK";
47520   }else{
47521     static char zName[15];
47522     sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
47523                      lockIdx-WAL_READ_LOCK(0));
47524     return zName;
47525   }
47526 }
47527 #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
47528 
47529 
47530 /*
47531 ** Set or release locks on the WAL.  Locks are either shared or exclusive.
47532 ** A lock cannot be moved directly between shared and exclusive - it must go
47533 ** through the unlocked state first.
47534 **
47535 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
47536 */
47537 static int walLockShared(Wal *pWal, int lockIdx){
47538   int rc;
47539   if( pWal->exclusiveMode ) return SQLITE_OK;
47540   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
47541                         SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
47542   WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
47543             walLockName(lockIdx), rc ? "failed" : "ok"));
47544   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
47545   return rc;
47546 }
47547 static void walUnlockShared(Wal *pWal, int lockIdx){
47548   if( pWal->exclusiveMode ) return;
47549   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
47550                          SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
47551   WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
47552 }
47553 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
47554   int rc;
47555   if( pWal->exclusiveMode ) return SQLITE_OK;
47556   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
47557                         SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
47558   WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
47559             walLockName(lockIdx), n, rc ? "failed" : "ok"));
47560   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
47561   return rc;
47562 }
47563 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
47564   if( pWal->exclusiveMode ) return;
47565   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
47566                          SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
47567   WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
47568              walLockName(lockIdx), n));
47569 }
47570 
47571 /*
47572 ** Compute a hash on a page number.  The resulting hash value must land
47573 ** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
47574 ** the hash to the next value in the event of a collision.
47575 */
47576 static int walHash(u32 iPage){
47577   assert( iPage>0 );
47578   assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
47579   return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
47580 }
47581 static int walNextHash(int iPriorHash){
47582   return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
47583 }
47584 
47585 /*
47586 ** Return pointers to the hash table and page number array stored on
47587 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
47588 ** numbered starting from 0.
47589 **
47590 ** Set output variable *paHash to point to the start of the hash table
47591 ** in the wal-index file. Set *piZero to one less than the frame
47592 ** number of the first frame indexed by this hash table. If a
47593 ** slot in the hash table is set to N, it refers to frame number
47594 ** (*piZero+N) in the log.
47595 **
47596 ** Finally, set *paPgno so that *paPgno[1] is the page number of the
47597 ** first frame indexed by the hash table, frame (*piZero+1).
47598 */
47599 static int walHashGet(
47600   Wal *pWal,                      /* WAL handle */
47601   int iHash,                      /* Find the iHash'th table */
47602   volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
47603   volatile u32 **paPgno,          /* OUT: Pointer to page number array */
47604   u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
47605 ){
47606   int rc;                         /* Return code */
47607   volatile u32 *aPgno;
47608 
47609   rc = walIndexPage(pWal, iHash, &aPgno);
47610   assert( rc==SQLITE_OK || iHash>0 );
47611 
47612   if( rc==SQLITE_OK ){
47613     u32 iZero;
47614     volatile ht_slot *aHash;
47615 
47616     aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
47617     if( iHash==0 ){
47618       aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
47619       iZero = 0;
47620     }else{
47621       iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
47622     }
47623 
47624     *paPgno = &aPgno[-1];
47625     *paHash = aHash;
47626     *piZero = iZero;
47627   }
47628   return rc;
47629 }
47630 
47631 /*
47632 ** Return the number of the wal-index page that contains the hash-table
47633 ** and page-number array that contain entries corresponding to WAL frame
47634 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
47635 ** are numbered starting from 0.
47636 */
47637 static int walFramePage(u32 iFrame){
47638   int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
47639   assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
47640        && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
47641        && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
47642        && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
47643        && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
47644   );
47645   return iHash;
47646 }
47647 
47648 /*
47649 ** Return the page number associated with frame iFrame in this WAL.
47650 */
47651 static u32 walFramePgno(Wal *pWal, u32 iFrame){
47652   int iHash = walFramePage(iFrame);
47653   if( iHash==0 ){
47654     return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
47655   }
47656   return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
47657 }
47658 
47659 /*
47660 ** Remove entries from the hash table that point to WAL slots greater
47661 ** than pWal->hdr.mxFrame.
47662 **
47663 ** This function is called whenever pWal->hdr.mxFrame is decreased due
47664 ** to a rollback or savepoint.
47665 **
47666 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
47667 ** updated.  Any later hash tables will be automatically cleared when
47668 ** pWal->hdr.mxFrame advances to the point where those hash tables are
47669 ** actually needed.
47670 */
47671 static void walCleanupHash(Wal *pWal){
47672   volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
47673   volatile u32 *aPgno = 0;        /* Page number array for hash table */
47674   u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
47675   int iLimit = 0;                 /* Zero values greater than this */
47676   int nByte;                      /* Number of bytes to zero in aPgno[] */
47677   int i;                          /* Used to iterate through aHash[] */
47678 
47679   assert( pWal->writeLock );
47680   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
47681   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
47682   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
47683 
47684   if( pWal->hdr.mxFrame==0 ) return;
47685 
47686   /* Obtain pointers to the hash-table and page-number array containing
47687   ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
47688   ** that the page said hash-table and array reside on is already mapped.
47689   */
47690   assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
47691   assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
47692   walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
47693 
47694   /* Zero all hash-table entries that correspond to frame numbers greater
47695   ** than pWal->hdr.mxFrame.
47696   */
47697   iLimit = pWal->hdr.mxFrame - iZero;
47698   assert( iLimit>0 );
47699   for(i=0; i<HASHTABLE_NSLOT; i++){
47700     if( aHash[i]>iLimit ){
47701       aHash[i] = 0;
47702     }
47703   }
47704 
47705   /* Zero the entries in the aPgno array that correspond to frames with
47706   ** frame numbers greater than pWal->hdr.mxFrame.
47707   */
47708   nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
47709   memset((void *)&aPgno[iLimit+1], 0, nByte);
47710 
47711 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
47712   /* Verify that the every entry in the mapping region is still reachable
47713   ** via the hash table even after the cleanup.
47714   */
47715   if( iLimit ){
47716     int i;           /* Loop counter */
47717     int iKey;        /* Hash key */
47718     for(i=1; i<=iLimit; i++){
47719       for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
47720         if( aHash[iKey]==i ) break;
47721       }
47722       assert( aHash[iKey]==i );
47723     }
47724   }
47725 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
47726 }
47727 
47728 
47729 /*
47730 ** Set an entry in the wal-index that will map database page number
47731 ** pPage into WAL frame iFrame.
47732 */
47733 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
47734   int rc;                         /* Return code */
47735   u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
47736   volatile u32 *aPgno = 0;        /* Page number array */
47737   volatile ht_slot *aHash = 0;    /* Hash table */
47738 
47739   rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
47740 
47741   /* Assuming the wal-index file was successfully mapped, populate the
47742   ** page number array and hash table entry.
47743   */
47744   if( rc==SQLITE_OK ){
47745     int iKey;                     /* Hash table key */
47746     int idx;                      /* Value to write to hash-table slot */
47747     int nCollide;                 /* Number of hash collisions */
47748 
47749     idx = iFrame - iZero;
47750     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
47751 
47752     /* If this is the first entry to be added to this hash-table, zero the
47753     ** entire hash table and aPgno[] array before proceding.
47754     */
47755     if( idx==1 ){
47756       int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
47757       memset((void*)&aPgno[1], 0, nByte);
47758     }
47759 
47760     /* If the entry in aPgno[] is already set, then the previous writer
47761     ** must have exited unexpectedly in the middle of a transaction (after
47762     ** writing one or more dirty pages to the WAL to free up memory).
47763     ** Remove the remnants of that writers uncommitted transaction from
47764     ** the hash-table before writing any new entries.
47765     */
47766     if( aPgno[idx] ){
47767       walCleanupHash(pWal);
47768       assert( !aPgno[idx] );
47769     }
47770 
47771     /* Write the aPgno[] array entry and the hash-table slot. */
47772     nCollide = idx;
47773     for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
47774       if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
47775     }
47776     aPgno[idx] = iPage;
47777     aHash[iKey] = (ht_slot)idx;
47778 
47779 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
47780     /* Verify that the number of entries in the hash table exactly equals
47781     ** the number of entries in the mapping region.
47782     */
47783     {
47784       int i;           /* Loop counter */
47785       int nEntry = 0;  /* Number of entries in the hash table */
47786       for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
47787       assert( nEntry==idx );
47788     }
47789 
47790     /* Verify that the every entry in the mapping region is reachable
47791     ** via the hash table.  This turns out to be a really, really expensive
47792     ** thing to check, so only do this occasionally - not on every
47793     ** iteration.
47794     */
47795     if( (idx&0x3ff)==0 ){
47796       int i;           /* Loop counter */
47797       for(i=1; i<=idx; i++){
47798         for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
47799           if( aHash[iKey]==i ) break;
47800         }
47801         assert( aHash[iKey]==i );
47802       }
47803     }
47804 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
47805   }
47806 
47807 
47808   return rc;
47809 }
47810 
47811 
47812 /*
47813 ** Recover the wal-index by reading the write-ahead log file.
47814 **
47815 ** This routine first tries to establish an exclusive lock on the
47816 ** wal-index to prevent other threads/processes from doing anything
47817 ** with the WAL or wal-index while recovery is running.  The
47818 ** WAL_RECOVER_LOCK is also held so that other threads will know
47819 ** that this thread is running recovery.  If unable to establish
47820 ** the necessary locks, this routine returns SQLITE_BUSY.
47821 */
47822 static int walIndexRecover(Wal *pWal){
47823   int rc;                         /* Return Code */
47824   i64 nSize;                      /* Size of log file */
47825   u32 aFrameCksum[2] = {0, 0};
47826   int iLock;                      /* Lock offset to lock for checkpoint */
47827   int nLock;                      /* Number of locks to hold */
47828 
47829   /* Obtain an exclusive lock on all byte in the locking range not already
47830   ** locked by the caller. The caller is guaranteed to have locked the
47831   ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
47832   ** If successful, the same bytes that are locked here are unlocked before
47833   ** this function returns.
47834   */
47835   assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
47836   assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
47837   assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
47838   assert( pWal->writeLock );
47839   iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
47840   nLock = SQLITE_SHM_NLOCK - iLock;
47841   rc = walLockExclusive(pWal, iLock, nLock);
47842   if( rc ){
47843     return rc;
47844   }
47845   WALTRACE(("WAL%p: recovery begin...\n", pWal));
47846 
47847   memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
47848 
47849   rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
47850   if( rc!=SQLITE_OK ){
47851     goto recovery_error;
47852   }
47853 
47854   if( nSize>WAL_HDRSIZE ){
47855     u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
47856     u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
47857     int szFrame;                  /* Number of bytes in buffer aFrame[] */
47858     u8 *aData;                    /* Pointer to data part of aFrame buffer */
47859     int iFrame;                   /* Index of last frame read */
47860     i64 iOffset;                  /* Next offset to read from log file */
47861     int szPage;                   /* Page size according to the log */
47862     u32 magic;                    /* Magic value read from WAL header */
47863     u32 version;                  /* Magic value read from WAL header */
47864     int isValid;                  /* True if this frame is valid */
47865 
47866     /* Read in the WAL header. */
47867     rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
47868     if( rc!=SQLITE_OK ){
47869       goto recovery_error;
47870     }
47871 
47872     /* If the database page size is not a power of two, or is greater than
47873     ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
47874     ** data. Similarly, if the 'magic' value is invalid, ignore the whole
47875     ** WAL file.
47876     */
47877     magic = sqlite3Get4byte(&aBuf[0]);
47878     szPage = sqlite3Get4byte(&aBuf[8]);
47879     if( (magic&0xFFFFFFFE)!=WAL_MAGIC
47880      || szPage&(szPage-1)
47881      || szPage>SQLITE_MAX_PAGE_SIZE
47882      || szPage<512
47883     ){
47884       goto finished;
47885     }
47886     pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
47887     pWal->szPage = szPage;
47888     pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
47889     memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
47890 
47891     /* Verify that the WAL header checksum is correct */
47892     walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
47893         aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
47894     );
47895     if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
47896      || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
47897     ){
47898       goto finished;
47899     }
47900 
47901     /* Verify that the version number on the WAL format is one that
47902     ** are able to understand */
47903     version = sqlite3Get4byte(&aBuf[4]);
47904     if( version!=WAL_MAX_VERSION ){
47905       rc = SQLITE_CANTOPEN_BKPT;
47906       goto finished;
47907     }
47908 
47909     /* Malloc a buffer to read frames into. */
47910     szFrame = szPage + WAL_FRAME_HDRSIZE;
47911     aFrame = (u8 *)sqlite3_malloc(szFrame);
47912     if( !aFrame ){
47913       rc = SQLITE_NOMEM;
47914       goto recovery_error;
47915     }
47916     aData = &aFrame[WAL_FRAME_HDRSIZE];
47917 
47918     /* Read all frames from the log file. */
47919     iFrame = 0;
47920     for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
47921       u32 pgno;                   /* Database page number for frame */
47922       u32 nTruncate;              /* dbsize field from frame header */
47923 
47924       /* Read and decode the next log frame. */
47925       iFrame++;
47926       rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
47927       if( rc!=SQLITE_OK ) break;
47928       isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
47929       if( !isValid ) break;
47930       rc = walIndexAppend(pWal, iFrame, pgno);
47931       if( rc!=SQLITE_OK ) break;
47932 
47933       /* If nTruncate is non-zero, this is a commit record. */
47934       if( nTruncate ){
47935         pWal->hdr.mxFrame = iFrame;
47936         pWal->hdr.nPage = nTruncate;
47937         pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
47938         testcase( szPage<=32768 );
47939         testcase( szPage>=65536 );
47940         aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
47941         aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
47942       }
47943     }
47944 
47945     sqlite3_free(aFrame);
47946   }
47947 
47948 finished:
47949   if( rc==SQLITE_OK ){
47950     volatile WalCkptInfo *pInfo;
47951     int i;
47952     pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
47953     pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
47954     walIndexWriteHdr(pWal);
47955 
47956     /* Reset the checkpoint-header. This is safe because this thread is
47957     ** currently holding locks that exclude all other readers, writers and
47958     ** checkpointers.
47959     */
47960     pInfo = walCkptInfo(pWal);
47961     pInfo->nBackfill = 0;
47962     pInfo->aReadMark[0] = 0;
47963     for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
47964     if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
47965 
47966     /* If more than one frame was recovered from the log file, report an
47967     ** event via sqlite3_log(). This is to help with identifying performance
47968     ** problems caused by applications routinely shutting down without
47969     ** checkpointing the log file.
47970     */
47971     if( pWal->hdr.nPage ){
47972       sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
47973           "recovered %d frames from WAL file %s",
47974           pWal->hdr.mxFrame, pWal->zWalName
47975       );
47976     }
47977   }
47978 
47979 recovery_error:
47980   WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
47981   walUnlockExclusive(pWal, iLock, nLock);
47982   return rc;
47983 }
47984 
47985 /*
47986 ** Close an open wal-index.
47987 */
47988 static void walIndexClose(Wal *pWal, int isDelete){
47989   if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
47990     int i;
47991     for(i=0; i<pWal->nWiData; i++){
47992       sqlite3_free((void *)pWal->apWiData[i]);
47993       pWal->apWiData[i] = 0;
47994     }
47995   }else{
47996     sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
47997   }
47998 }
47999 
48000 /*
48001 ** Open a connection to the WAL file zWalName. The database file must
48002 ** already be opened on connection pDbFd. The buffer that zWalName points
48003 ** to must remain valid for the lifetime of the returned Wal* handle.
48004 **
48005 ** A SHARED lock should be held on the database file when this function
48006 ** is called. The purpose of this SHARED lock is to prevent any other
48007 ** client from unlinking the WAL or wal-index file. If another process
48008 ** were to do this just after this client opened one of these files, the
48009 ** system would be badly broken.
48010 **
48011 ** If the log file is successfully opened, SQLITE_OK is returned and
48012 ** *ppWal is set to point to a new WAL handle. If an error occurs,
48013 ** an SQLite error code is returned and *ppWal is left unmodified.
48014 */
48015 SQLITE_PRIVATE int sqlite3WalOpen(
48016   sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
48017   sqlite3_file *pDbFd,            /* The open database file */
48018   const char *zWalName,           /* Name of the WAL file */
48019   int bNoShm,                     /* True to run in heap-memory mode */
48020   i64 mxWalSize,                  /* Truncate WAL to this size on reset */
48021   Wal **ppWal                     /* OUT: Allocated Wal handle */
48022 ){
48023   int rc;                         /* Return Code */
48024   Wal *pRet;                      /* Object to allocate and return */
48025   int flags;                      /* Flags passed to OsOpen() */
48026 
48027   assert( zWalName && zWalName[0] );
48028   assert( pDbFd );
48029 
48030   /* In the amalgamation, the os_unix.c and os_win.c source files come before
48031   ** this source file.  Verify that the #defines of the locking byte offsets
48032   ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
48033   */
48034 #ifdef WIN_SHM_BASE
48035   assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
48036 #endif
48037 #ifdef UNIX_SHM_BASE
48038   assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
48039 #endif
48040 
48041 
48042   /* Allocate an instance of struct Wal to return. */
48043   *ppWal = 0;
48044   pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
48045   if( !pRet ){
48046     return SQLITE_NOMEM;
48047   }
48048 
48049   pRet->pVfs = pVfs;
48050   pRet->pWalFd = (sqlite3_file *)&pRet[1];
48051   pRet->pDbFd = pDbFd;
48052   pRet->readLock = -1;
48053   pRet->mxWalSize = mxWalSize;
48054   pRet->zWalName = zWalName;
48055   pRet->syncHeader = 1;
48056   pRet->padToSectorBoundary = 1;
48057   pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
48058 
48059   /* Open file handle on the write-ahead log file. */
48060   flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
48061   rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
48062   if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
48063     pRet->readOnly = WAL_RDONLY;
48064   }
48065 
48066   if( rc!=SQLITE_OK ){
48067     walIndexClose(pRet, 0);
48068     sqlite3OsClose(pRet->pWalFd);
48069     sqlite3_free(pRet);
48070   }else{
48071     int iDC = sqlite3OsDeviceCharacteristics(pDbFd);
48072     if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
48073     if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
48074       pRet->padToSectorBoundary = 0;
48075     }
48076     *ppWal = pRet;
48077     WALTRACE(("WAL%d: opened\n", pRet));
48078   }
48079   return rc;
48080 }
48081 
48082 /*
48083 ** Change the size to which the WAL file is trucated on each reset.
48084 */
48085 SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
48086   if( pWal ) pWal->mxWalSize = iLimit;
48087 }
48088 
48089 /*
48090 ** Find the smallest page number out of all pages held in the WAL that
48091 ** has not been returned by any prior invocation of this method on the
48092 ** same WalIterator object.   Write into *piFrame the frame index where
48093 ** that page was last written into the WAL.  Write into *piPage the page
48094 ** number.
48095 **
48096 ** Return 0 on success.  If there are no pages in the WAL with a page
48097 ** number larger than *piPage, then return 1.
48098 */
48099 static int walIteratorNext(
48100   WalIterator *p,               /* Iterator */
48101   u32 *piPage,                  /* OUT: The page number of the next page */
48102   u32 *piFrame                  /* OUT: Wal frame index of next page */
48103 ){
48104   u32 iMin;                     /* Result pgno must be greater than iMin */
48105   u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
48106   int i;                        /* For looping through segments */
48107 
48108   iMin = p->iPrior;
48109   assert( iMin<0xffffffff );
48110   for(i=p->nSegment-1; i>=0; i--){
48111     struct WalSegment *pSegment = &p->aSegment[i];
48112     while( pSegment->iNext<pSegment->nEntry ){
48113       u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
48114       if( iPg>iMin ){
48115         if( iPg<iRet ){
48116           iRet = iPg;
48117           *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
48118         }
48119         break;
48120       }
48121       pSegment->iNext++;
48122     }
48123   }
48124 
48125   *piPage = p->iPrior = iRet;
48126   return (iRet==0xFFFFFFFF);
48127 }
48128 
48129 /*
48130 ** This function merges two sorted lists into a single sorted list.
48131 **
48132 ** aLeft[] and aRight[] are arrays of indices.  The sort key is
48133 ** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
48134 ** is guaranteed for all J<K:
48135 **
48136 **        aContent[aLeft[J]] < aContent[aLeft[K]]
48137 **        aContent[aRight[J]] < aContent[aRight[K]]
48138 **
48139 ** This routine overwrites aRight[] with a new (probably longer) sequence
48140 ** of indices such that the aRight[] contains every index that appears in
48141 ** either aLeft[] or the old aRight[] and such that the second condition
48142 ** above is still met.
48143 **
48144 ** The aContent[aLeft[X]] values will be unique for all X.  And the
48145 ** aContent[aRight[X]] values will be unique too.  But there might be
48146 ** one or more combinations of X and Y such that
48147 **
48148 **      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
48149 **
48150 ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
48151 */
48152 static void walMerge(
48153   const u32 *aContent,            /* Pages in wal - keys for the sort */
48154   ht_slot *aLeft,                 /* IN: Left hand input list */
48155   int nLeft,                      /* IN: Elements in array *paLeft */
48156   ht_slot **paRight,              /* IN/OUT: Right hand input list */
48157   int *pnRight,                   /* IN/OUT: Elements in *paRight */
48158   ht_slot *aTmp                   /* Temporary buffer */
48159 ){
48160   int iLeft = 0;                  /* Current index in aLeft */
48161   int iRight = 0;                 /* Current index in aRight */
48162   int iOut = 0;                   /* Current index in output buffer */
48163   int nRight = *pnRight;
48164   ht_slot *aRight = *paRight;
48165 
48166   assert( nLeft>0 && nRight>0 );
48167   while( iRight<nRight || iLeft<nLeft ){
48168     ht_slot logpage;
48169     Pgno dbpage;
48170 
48171     if( (iLeft<nLeft)
48172      && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
48173     ){
48174       logpage = aLeft[iLeft++];
48175     }else{
48176       logpage = aRight[iRight++];
48177     }
48178     dbpage = aContent[logpage];
48179 
48180     aTmp[iOut++] = logpage;
48181     if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
48182 
48183     assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
48184     assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
48185   }
48186 
48187   *paRight = aLeft;
48188   *pnRight = iOut;
48189   memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
48190 }
48191 
48192 /*
48193 ** Sort the elements in list aList using aContent[] as the sort key.
48194 ** Remove elements with duplicate keys, preferring to keep the
48195 ** larger aList[] values.
48196 **
48197 ** The aList[] entries are indices into aContent[].  The values in
48198 ** aList[] are to be sorted so that for all J<K:
48199 **
48200 **      aContent[aList[J]] < aContent[aList[K]]
48201 **
48202 ** For any X and Y such that
48203 **
48204 **      aContent[aList[X]] == aContent[aList[Y]]
48205 **
48206 ** Keep the larger of the two values aList[X] and aList[Y] and discard
48207 ** the smaller.
48208 */
48209 static void walMergesort(
48210   const u32 *aContent,            /* Pages in wal */
48211   ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
48212   ht_slot *aList,                 /* IN/OUT: List to sort */
48213   int *pnList                     /* IN/OUT: Number of elements in aList[] */
48214 ){
48215   struct Sublist {
48216     int nList;                    /* Number of elements in aList */
48217     ht_slot *aList;               /* Pointer to sub-list content */
48218   };
48219 
48220   const int nList = *pnList;      /* Size of input list */
48221   int nMerge = 0;                 /* Number of elements in list aMerge */
48222   ht_slot *aMerge = 0;            /* List to be merged */
48223   int iList;                      /* Index into input list */
48224   int iSub = 0;                   /* Index into aSub array */
48225   struct Sublist aSub[13];        /* Array of sub-lists */
48226 
48227   memset(aSub, 0, sizeof(aSub));
48228   assert( nList<=HASHTABLE_NPAGE && nList>0 );
48229   assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
48230 
48231   for(iList=0; iList<nList; iList++){
48232     nMerge = 1;
48233     aMerge = &aList[iList];
48234     for(iSub=0; iList & (1<<iSub); iSub++){
48235       struct Sublist *p = &aSub[iSub];
48236       assert( p->aList && p->nList<=(1<<iSub) );
48237       assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
48238       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
48239     }
48240     aSub[iSub].aList = aMerge;
48241     aSub[iSub].nList = nMerge;
48242   }
48243 
48244   for(iSub++; iSub<ArraySize(aSub); iSub++){
48245     if( nList & (1<<iSub) ){
48246       struct Sublist *p = &aSub[iSub];
48247       assert( p->nList<=(1<<iSub) );
48248       assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
48249       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
48250     }
48251   }
48252   assert( aMerge==aList );
48253   *pnList = nMerge;
48254 
48255 #ifdef SQLITE_DEBUG
48256   {
48257     int i;
48258     for(i=1; i<*pnList; i++){
48259       assert( aContent[aList[i]] > aContent[aList[i-1]] );
48260     }
48261   }
48262 #endif
48263 }
48264 
48265 /*
48266 ** Free an iterator allocated by walIteratorInit().
48267 */
48268 static void walIteratorFree(WalIterator *p){
48269   sqlite3ScratchFree(p);
48270 }
48271 
48272 /*
48273 ** Construct a WalInterator object that can be used to loop over all
48274 ** pages in the WAL in ascending order. The caller must hold the checkpoint
48275 ** lock.
48276 **
48277 ** On success, make *pp point to the newly allocated WalInterator object
48278 ** return SQLITE_OK. Otherwise, return an error code. If this routine
48279 ** returns an error, the value of *pp is undefined.
48280 **
48281 ** The calling routine should invoke walIteratorFree() to destroy the
48282 ** WalIterator object when it has finished with it.
48283 */
48284 static int walIteratorInit(Wal *pWal, WalIterator **pp){
48285   WalIterator *p;                 /* Return value */
48286   int nSegment;                   /* Number of segments to merge */
48287   u32 iLast;                      /* Last frame in log */
48288   int nByte;                      /* Number of bytes to allocate */
48289   int i;                          /* Iterator variable */
48290   ht_slot *aTmp;                  /* Temp space used by merge-sort */
48291   int rc = SQLITE_OK;             /* Return Code */
48292 
48293   /* This routine only runs while holding the checkpoint lock. And
48294   ** it only runs if there is actually content in the log (mxFrame>0).
48295   */
48296   assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
48297   iLast = pWal->hdr.mxFrame;
48298 
48299   /* Allocate space for the WalIterator object. */
48300   nSegment = walFramePage(iLast) + 1;
48301   nByte = sizeof(WalIterator)
48302         + (nSegment-1)*sizeof(struct WalSegment)
48303         + iLast*sizeof(ht_slot);
48304   p = (WalIterator *)sqlite3ScratchMalloc(nByte);
48305   if( !p ){
48306     return SQLITE_NOMEM;
48307   }
48308   memset(p, 0, nByte);
48309   p->nSegment = nSegment;
48310 
48311   /* Allocate temporary space used by the merge-sort routine. This block
48312   ** of memory will be freed before this function returns.
48313   */
48314   aTmp = (ht_slot *)sqlite3ScratchMalloc(
48315       sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
48316   );
48317   if( !aTmp ){
48318     rc = SQLITE_NOMEM;
48319   }
48320 
48321   for(i=0; rc==SQLITE_OK && i<nSegment; i++){
48322     volatile ht_slot *aHash;
48323     u32 iZero;
48324     volatile u32 *aPgno;
48325 
48326     rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
48327     if( rc==SQLITE_OK ){
48328       int j;                      /* Counter variable */
48329       int nEntry;                 /* Number of entries in this segment */
48330       ht_slot *aIndex;            /* Sorted index for this segment */
48331 
48332       aPgno++;
48333       if( (i+1)==nSegment ){
48334         nEntry = (int)(iLast - iZero);
48335       }else{
48336         nEntry = (int)((u32*)aHash - (u32*)aPgno);
48337       }
48338       aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
48339       iZero++;
48340 
48341       for(j=0; j<nEntry; j++){
48342         aIndex[j] = (ht_slot)j;
48343       }
48344       walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
48345       p->aSegment[i].iZero = iZero;
48346       p->aSegment[i].nEntry = nEntry;
48347       p->aSegment[i].aIndex = aIndex;
48348       p->aSegment[i].aPgno = (u32 *)aPgno;
48349     }
48350   }
48351   sqlite3ScratchFree(aTmp);
48352 
48353   if( rc!=SQLITE_OK ){
48354     walIteratorFree(p);
48355   }
48356   *pp = p;
48357   return rc;
48358 }
48359 
48360 /*
48361 ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
48362 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
48363 ** busy-handler function. Invoke it and retry the lock until either the
48364 ** lock is successfully obtained or the busy-handler returns 0.
48365 */
48366 static int walBusyLock(
48367   Wal *pWal,                      /* WAL connection */
48368   int (*xBusy)(void*),            /* Function to call when busy */
48369   void *pBusyArg,                 /* Context argument for xBusyHandler */
48370   int lockIdx,                    /* Offset of first byte to lock */
48371   int n                           /* Number of bytes to lock */
48372 ){
48373   int rc;
48374   do {
48375     rc = walLockExclusive(pWal, lockIdx, n);
48376   }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
48377   return rc;
48378 }
48379 
48380 /*
48381 ** The cache of the wal-index header must be valid to call this function.
48382 ** Return the page-size in bytes used by the database.
48383 */
48384 static int walPagesize(Wal *pWal){
48385   return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
48386 }
48387 
48388 /*
48389 ** Copy as much content as we can from the WAL back into the database file
48390 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
48391 **
48392 ** The amount of information copies from WAL to database might be limited
48393 ** by active readers.  This routine will never overwrite a database page
48394 ** that a concurrent reader might be using.
48395 **
48396 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
48397 ** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if
48398 ** checkpoints are always run by a background thread or background
48399 ** process, foreground threads will never block on a lengthy fsync call.
48400 **
48401 ** Fsync is called on the WAL before writing content out of the WAL and
48402 ** into the database.  This ensures that if the new content is persistent
48403 ** in the WAL and can be recovered following a power-loss or hard reset.
48404 **
48405 ** Fsync is also called on the database file if (and only if) the entire
48406 ** WAL content is copied into the database file.  This second fsync makes
48407 ** it safe to delete the WAL since the new content will persist in the
48408 ** database file.
48409 **
48410 ** This routine uses and updates the nBackfill field of the wal-index header.
48411 ** This is the only routine tha will increase the value of nBackfill.
48412 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
48413 ** its value.)
48414 **
48415 ** The caller must be holding sufficient locks to ensure that no other
48416 ** checkpoint is running (in any other thread or process) at the same
48417 ** time.
48418 */
48419 static int walCheckpoint(
48420   Wal *pWal,                      /* Wal connection */
48421   int eMode,                      /* One of PASSIVE, FULL or RESTART */
48422   int (*xBusyCall)(void*),        /* Function to call when busy */
48423   void *pBusyArg,                 /* Context argument for xBusyHandler */
48424   int sync_flags,                 /* Flags for OsSync() (or 0) */
48425   u8 *zBuf                        /* Temporary buffer to use */
48426 ){
48427   int rc;                         /* Return code */
48428   int szPage;                     /* Database page-size */
48429   WalIterator *pIter = 0;         /* Wal iterator context */
48430   u32 iDbpage = 0;                /* Next database page to write */
48431   u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
48432   u32 mxSafeFrame;                /* Max frame that can be backfilled */
48433   u32 mxPage;                     /* Max database page to write */
48434   int i;                          /* Loop counter */
48435   volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
48436   int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
48437 
48438   szPage = walPagesize(pWal);
48439   testcase( szPage<=32768 );
48440   testcase( szPage>=65536 );
48441   pInfo = walCkptInfo(pWal);
48442   if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
48443 
48444   /* Allocate the iterator */
48445   rc = walIteratorInit(pWal, &pIter);
48446   if( rc!=SQLITE_OK ){
48447     return rc;
48448   }
48449   assert( pIter );
48450 
48451   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
48452 
48453   /* Compute in mxSafeFrame the index of the last frame of the WAL that is
48454   ** safe to write into the database.  Frames beyond mxSafeFrame might
48455   ** overwrite database pages that are in use by active readers and thus
48456   ** cannot be backfilled from the WAL.
48457   */
48458   mxSafeFrame = pWal->hdr.mxFrame;
48459   mxPage = pWal->hdr.nPage;
48460   for(i=1; i<WAL_NREADER; i++){
48461     u32 y = pInfo->aReadMark[i];
48462     if( mxSafeFrame>y ){
48463       assert( y<=pWal->hdr.mxFrame );
48464       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
48465       if( rc==SQLITE_OK ){
48466         pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
48467         walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
48468       }else if( rc==SQLITE_BUSY ){
48469         mxSafeFrame = y;
48470         xBusy = 0;
48471       }else{
48472         goto walcheckpoint_out;
48473       }
48474     }
48475   }
48476 
48477   if( pInfo->nBackfill<mxSafeFrame
48478    && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
48479   ){
48480     i64 nSize;                    /* Current size of database file */
48481     u32 nBackfill = pInfo->nBackfill;
48482 
48483     /* Sync the WAL to disk */
48484     if( sync_flags ){
48485       rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
48486     }
48487 
48488     /* If the database may grow as a result of this checkpoint, hint
48489     ** about the eventual size of the db file to the VFS layer.
48490     */
48491     if( rc==SQLITE_OK ){
48492       i64 nReq = ((i64)mxPage * szPage);
48493       rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
48494       if( rc==SQLITE_OK && nSize<nReq ){
48495         sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
48496       }
48497     }
48498 
48499 
48500     /* Iterate through the contents of the WAL, copying data to the db file. */
48501     while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
48502       i64 iOffset;
48503       assert( walFramePgno(pWal, iFrame)==iDbpage );
48504       if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
48505       iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
48506       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
48507       rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
48508       if( rc!=SQLITE_OK ) break;
48509       iOffset = (iDbpage-1)*(i64)szPage;
48510       testcase( IS_BIG_INT(iOffset) );
48511       rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
48512       if( rc!=SQLITE_OK ) break;
48513     }
48514 
48515     /* If work was actually accomplished... */
48516     if( rc==SQLITE_OK ){
48517       if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
48518         i64 szDb = pWal->hdr.nPage*(i64)szPage;
48519         testcase( IS_BIG_INT(szDb) );
48520         rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
48521         if( rc==SQLITE_OK && sync_flags ){
48522           rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
48523         }
48524       }
48525       if( rc==SQLITE_OK ){
48526         pInfo->nBackfill = mxSafeFrame;
48527       }
48528     }
48529 
48530     /* Release the reader lock held while backfilling */
48531     walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
48532   }
48533 
48534   if( rc==SQLITE_BUSY ){
48535     /* Reset the return code so as not to report a checkpoint failure
48536     ** just because there are active readers.  */
48537     rc = SQLITE_OK;
48538   }
48539 
48540   /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
48541   ** file has been copied into the database file, then block until all
48542   ** readers have finished using the wal file. This ensures that the next
48543   ** process to write to the database restarts the wal file.
48544   */
48545   if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
48546     assert( pWal->writeLock );
48547     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
48548       rc = SQLITE_BUSY;
48549     }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
48550       assert( mxSafeFrame==pWal->hdr.mxFrame );
48551       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
48552       if( rc==SQLITE_OK ){
48553         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
48554       }
48555     }
48556   }
48557 
48558  walcheckpoint_out:
48559   walIteratorFree(pIter);
48560   return rc;
48561 }
48562 
48563 /*
48564 ** If the WAL file is currently larger than nMax bytes in size, truncate
48565 ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
48566 */
48567 static void walLimitSize(Wal *pWal, i64 nMax){
48568   i64 sz;
48569   int rx;
48570   sqlite3BeginBenignMalloc();
48571   rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
48572   if( rx==SQLITE_OK && (sz > nMax ) ){
48573     rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
48574   }
48575   sqlite3EndBenignMalloc();
48576   if( rx ){
48577     sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
48578   }
48579 }
48580 
48581 /*
48582 ** Close a connection to a log file.
48583 */
48584 SQLITE_PRIVATE int sqlite3WalClose(
48585   Wal *pWal,                      /* Wal to close */
48586   int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
48587   int nBuf,
48588   u8 *zBuf                        /* Buffer of at least nBuf bytes */
48589 ){
48590   int rc = SQLITE_OK;
48591   if( pWal ){
48592     int isDelete = 0;             /* True to unlink wal and wal-index files */
48593 
48594     /* If an EXCLUSIVE lock can be obtained on the database file (using the
48595     ** ordinary, rollback-mode locking methods, this guarantees that the
48596     ** connection associated with this log file is the only connection to
48597     ** the database. In this case checkpoint the database and unlink both
48598     ** the wal and wal-index files.
48599     **
48600     ** The EXCLUSIVE lock is not released before returning.
48601     */
48602     rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
48603     if( rc==SQLITE_OK ){
48604       if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
48605         pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
48606       }
48607       rc = sqlite3WalCheckpoint(
48608           pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
48609       );
48610       if( rc==SQLITE_OK ){
48611         int bPersist = -1;
48612         sqlite3OsFileControlHint(
48613             pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
48614         );
48615         if( bPersist!=1 ){
48616           /* Try to delete the WAL file if the checkpoint completed and
48617           ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
48618           ** mode (!bPersist) */
48619           isDelete = 1;
48620         }else if( pWal->mxWalSize>=0 ){
48621           /* Try to truncate the WAL file to zero bytes if the checkpoint
48622           ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
48623           ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
48624           ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
48625           ** to zero bytes as truncating to the journal_size_limit might
48626           ** leave a corrupt WAL file on disk. */
48627           walLimitSize(pWal, 0);
48628         }
48629       }
48630     }
48631 
48632     walIndexClose(pWal, isDelete);
48633     sqlite3OsClose(pWal->pWalFd);
48634     if( isDelete ){
48635       sqlite3BeginBenignMalloc();
48636       sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
48637       sqlite3EndBenignMalloc();
48638     }
48639     WALTRACE(("WAL%p: closed\n", pWal));
48640     sqlite3_free((void *)pWal->apWiData);
48641     sqlite3_free(pWal);
48642   }
48643   return rc;
48644 }
48645 
48646 /*
48647 ** Try to read the wal-index header.  Return 0 on success and 1 if
48648 ** there is a problem.
48649 **
48650 ** The wal-index is in shared memory.  Another thread or process might
48651 ** be writing the header at the same time this procedure is trying to
48652 ** read it, which might result in inconsistency.  A dirty read is detected
48653 ** by verifying that both copies of the header are the same and also by
48654 ** a checksum on the header.
48655 **
48656 ** If and only if the read is consistent and the header is different from
48657 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
48658 ** and *pChanged is set to 1.
48659 **
48660 ** If the checksum cannot be verified return non-zero. If the header
48661 ** is read successfully and the checksum verified, return zero.
48662 */
48663 static int walIndexTryHdr(Wal *pWal, int *pChanged){
48664   u32 aCksum[2];                  /* Checksum on the header content */
48665   WalIndexHdr h1, h2;             /* Two copies of the header content */
48666   WalIndexHdr volatile *aHdr;     /* Header in shared memory */
48667 
48668   /* The first page of the wal-index must be mapped at this point. */
48669   assert( pWal->nWiData>0 && pWal->apWiData[0] );
48670 
48671   /* Read the header. This might happen concurrently with a write to the
48672   ** same area of shared memory on a different CPU in a SMP,
48673   ** meaning it is possible that an inconsistent snapshot is read
48674   ** from the file. If this happens, return non-zero.
48675   **
48676   ** There are two copies of the header at the beginning of the wal-index.
48677   ** When reading, read [0] first then [1].  Writes are in the reverse order.
48678   ** Memory barriers are used to prevent the compiler or the hardware from
48679   ** reordering the reads and writes.
48680   */
48681   aHdr = walIndexHdr(pWal);
48682   memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
48683   walShmBarrier(pWal);
48684   memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
48685 
48686   if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
48687     return 1;   /* Dirty read */
48688   }
48689   if( h1.isInit==0 ){
48690     return 1;   /* Malformed header - probably all zeros */
48691   }
48692   walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
48693   if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
48694     return 1;   /* Checksum does not match */
48695   }
48696 
48697   if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
48698     *pChanged = 1;
48699     memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
48700     pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
48701     testcase( pWal->szPage<=32768 );
48702     testcase( pWal->szPage>=65536 );
48703   }
48704 
48705   /* The header was successfully read. Return zero. */
48706   return 0;
48707 }
48708 
48709 /*
48710 ** Read the wal-index header from the wal-index and into pWal->hdr.
48711 ** If the wal-header appears to be corrupt, try to reconstruct the
48712 ** wal-index from the WAL before returning.
48713 **
48714 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
48715 ** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
48716 ** to 0.
48717 **
48718 ** If the wal-index header is successfully read, return SQLITE_OK.
48719 ** Otherwise an SQLite error code.
48720 */
48721 static int walIndexReadHdr(Wal *pWal, int *pChanged){
48722   int rc;                         /* Return code */
48723   int badHdr;                     /* True if a header read failed */
48724   volatile u32 *page0;            /* Chunk of wal-index containing header */
48725 
48726   /* Ensure that page 0 of the wal-index (the page that contains the
48727   ** wal-index header) is mapped. Return early if an error occurs here.
48728   */
48729   assert( pChanged );
48730   rc = walIndexPage(pWal, 0, &page0);
48731   if( rc!=SQLITE_OK ){
48732     return rc;
48733   };
48734   assert( page0 || pWal->writeLock==0 );
48735 
48736   /* If the first page of the wal-index has been mapped, try to read the
48737   ** wal-index header immediately, without holding any lock. This usually
48738   ** works, but may fail if the wal-index header is corrupt or currently
48739   ** being modified by another thread or process.
48740   */
48741   badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
48742 
48743   /* If the first attempt failed, it might have been due to a race
48744   ** with a writer.  So get a WRITE lock and try again.
48745   */
48746   assert( badHdr==0 || pWal->writeLock==0 );
48747   if( badHdr ){
48748     if( pWal->readOnly & WAL_SHM_RDONLY ){
48749       if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
48750         walUnlockShared(pWal, WAL_WRITE_LOCK);
48751         rc = SQLITE_READONLY_RECOVERY;
48752       }
48753     }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
48754       pWal->writeLock = 1;
48755       if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
48756         badHdr = walIndexTryHdr(pWal, pChanged);
48757         if( badHdr ){
48758           /* If the wal-index header is still malformed even while holding
48759           ** a WRITE lock, it can only mean that the header is corrupted and
48760           ** needs to be reconstructed.  So run recovery to do exactly that.
48761           */
48762           rc = walIndexRecover(pWal);
48763           *pChanged = 1;
48764         }
48765       }
48766       pWal->writeLock = 0;
48767       walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
48768     }
48769   }
48770 
48771   /* If the header is read successfully, check the version number to make
48772   ** sure the wal-index was not constructed with some future format that
48773   ** this version of SQLite cannot understand.
48774   */
48775   if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
48776     rc = SQLITE_CANTOPEN_BKPT;
48777   }
48778 
48779   return rc;
48780 }
48781 
48782 /*
48783 ** This is the value that walTryBeginRead returns when it needs to
48784 ** be retried.
48785 */
48786 #define WAL_RETRY  (-1)
48787 
48788 /*
48789 ** Attempt to start a read transaction.  This might fail due to a race or
48790 ** other transient condition.  When that happens, it returns WAL_RETRY to
48791 ** indicate to the caller that it is safe to retry immediately.
48792 **
48793 ** On success return SQLITE_OK.  On a permanent failure (such an
48794 ** I/O error or an SQLITE_BUSY because another process is running
48795 ** recovery) return a positive error code.
48796 **
48797 ** The useWal parameter is true to force the use of the WAL and disable
48798 ** the case where the WAL is bypassed because it has been completely
48799 ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr()
48800 ** to make a copy of the wal-index header into pWal->hdr.  If the
48801 ** wal-index header has changed, *pChanged is set to 1 (as an indication
48802 ** to the caller that the local paget cache is obsolete and needs to be
48803 ** flushed.)  When useWal==1, the wal-index header is assumed to already
48804 ** be loaded and the pChanged parameter is unused.
48805 **
48806 ** The caller must set the cnt parameter to the number of prior calls to
48807 ** this routine during the current read attempt that returned WAL_RETRY.
48808 ** This routine will start taking more aggressive measures to clear the
48809 ** race conditions after multiple WAL_RETRY returns, and after an excessive
48810 ** number of errors will ultimately return SQLITE_PROTOCOL.  The
48811 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
48812 ** and is not honoring the locking protocol.  There is a vanishingly small
48813 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
48814 ** bad luck when there is lots of contention for the wal-index, but that
48815 ** possibility is so small that it can be safely neglected, we believe.
48816 **
48817 ** On success, this routine obtains a read lock on
48818 ** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
48819 ** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
48820 ** that means the Wal does not hold any read lock.  The reader must not
48821 ** access any database page that is modified by a WAL frame up to and
48822 ** including frame number aReadMark[pWal->readLock].  The reader will
48823 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
48824 ** Or if pWal->readLock==0, then the reader will ignore the WAL
48825 ** completely and get all content directly from the database file.
48826 ** If the useWal parameter is 1 then the WAL will never be ignored and
48827 ** this routine will always set pWal->readLock>0 on success.
48828 ** When the read transaction is completed, the caller must release the
48829 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
48830 **
48831 ** This routine uses the nBackfill and aReadMark[] fields of the header
48832 ** to select a particular WAL_READ_LOCK() that strives to let the
48833 ** checkpoint process do as much work as possible.  This routine might
48834 ** update values of the aReadMark[] array in the header, but if it does
48835 ** so it takes care to hold an exclusive lock on the corresponding
48836 ** WAL_READ_LOCK() while changing values.
48837 */
48838 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
48839   volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
48840   u32 mxReadMark;                 /* Largest aReadMark[] value */
48841   int mxI;                        /* Index of largest aReadMark[] value */
48842   int i;                          /* Loop counter */
48843   int rc = SQLITE_OK;             /* Return code  */
48844 
48845   assert( pWal->readLock<0 );     /* Not currently locked */
48846 
48847   /* Take steps to avoid spinning forever if there is a protocol error.
48848   **
48849   ** Circumstances that cause a RETRY should only last for the briefest
48850   ** instances of time.  No I/O or other system calls are done while the
48851   ** locks are held, so the locks should not be held for very long. But
48852   ** if we are unlucky, another process that is holding a lock might get
48853   ** paged out or take a page-fault that is time-consuming to resolve,
48854   ** during the few nanoseconds that it is holding the lock.  In that case,
48855   ** it might take longer than normal for the lock to free.
48856   **
48857   ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
48858   ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
48859   ** is more of a scheduler yield than an actual delay.  But on the 10th
48860   ** an subsequent retries, the delays start becoming longer and longer,
48861   ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
48862   ** The total delay time before giving up is less than 1 second.
48863   */
48864   if( cnt>5 ){
48865     int nDelay = 1;                      /* Pause time in microseconds */
48866     if( cnt>100 ){
48867       VVA_ONLY( pWal->lockError = 1; )
48868       return SQLITE_PROTOCOL;
48869     }
48870     if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
48871     sqlite3OsSleep(pWal->pVfs, nDelay);
48872   }
48873 
48874   if( !useWal ){
48875     rc = walIndexReadHdr(pWal, pChanged);
48876     if( rc==SQLITE_BUSY ){
48877       /* If there is not a recovery running in another thread or process
48878       ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
48879       ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
48880       ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
48881       ** would be technically correct.  But the race is benign since with
48882       ** WAL_RETRY this routine will be called again and will probably be
48883       ** right on the second iteration.
48884       */
48885       if( pWal->apWiData[0]==0 ){
48886         /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
48887         ** We assume this is a transient condition, so return WAL_RETRY. The
48888         ** xShmMap() implementation used by the default unix and win32 VFS
48889         ** modules may return SQLITE_BUSY due to a race condition in the
48890         ** code that determines whether or not the shared-memory region
48891         ** must be zeroed before the requested page is returned.
48892         */
48893         rc = WAL_RETRY;
48894       }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
48895         walUnlockShared(pWal, WAL_RECOVER_LOCK);
48896         rc = WAL_RETRY;
48897       }else if( rc==SQLITE_BUSY ){
48898         rc = SQLITE_BUSY_RECOVERY;
48899       }
48900     }
48901     if( rc!=SQLITE_OK ){
48902       return rc;
48903     }
48904   }
48905 
48906   pInfo = walCkptInfo(pWal);
48907   if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
48908     /* The WAL has been completely backfilled (or it is empty).
48909     ** and can be safely ignored.
48910     */
48911     rc = walLockShared(pWal, WAL_READ_LOCK(0));
48912     walShmBarrier(pWal);
48913     if( rc==SQLITE_OK ){
48914       if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
48915         /* It is not safe to allow the reader to continue here if frames
48916         ** may have been appended to the log before READ_LOCK(0) was obtained.
48917         ** When holding READ_LOCK(0), the reader ignores the entire log file,
48918         ** which implies that the database file contains a trustworthy
48919         ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
48920         ** happening, this is usually correct.
48921         **
48922         ** However, if frames have been appended to the log (or if the log
48923         ** is wrapped and written for that matter) before the READ_LOCK(0)
48924         ** is obtained, that is not necessarily true. A checkpointer may
48925         ** have started to backfill the appended frames but crashed before
48926         ** it finished. Leaving a corrupt image in the database file.
48927         */
48928         walUnlockShared(pWal, WAL_READ_LOCK(0));
48929         return WAL_RETRY;
48930       }
48931       pWal->readLock = 0;
48932       return SQLITE_OK;
48933     }else if( rc!=SQLITE_BUSY ){
48934       return rc;
48935     }
48936   }
48937 
48938   /* If we get this far, it means that the reader will want to use
48939   ** the WAL to get at content from recent commits.  The job now is
48940   ** to select one of the aReadMark[] entries that is closest to
48941   ** but not exceeding pWal->hdr.mxFrame and lock that entry.
48942   */
48943   mxReadMark = 0;
48944   mxI = 0;
48945   for(i=1; i<WAL_NREADER; i++){
48946     u32 thisMark = pInfo->aReadMark[i];
48947     if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
48948       assert( thisMark!=READMARK_NOT_USED );
48949       mxReadMark = thisMark;
48950       mxI = i;
48951     }
48952   }
48953   /* There was once an "if" here. The extra "{" is to preserve indentation. */
48954   {
48955     if( (pWal->readOnly & WAL_SHM_RDONLY)==0
48956      && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
48957     ){
48958       for(i=1; i<WAL_NREADER; i++){
48959         rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
48960         if( rc==SQLITE_OK ){
48961           mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
48962           mxI = i;
48963           walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
48964           break;
48965         }else if( rc!=SQLITE_BUSY ){
48966           return rc;
48967         }
48968       }
48969     }
48970     if( mxI==0 ){
48971       assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
48972       return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
48973     }
48974 
48975     rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
48976     if( rc ){
48977       return rc==SQLITE_BUSY ? WAL_RETRY : rc;
48978     }
48979     /* Now that the read-lock has been obtained, check that neither the
48980     ** value in the aReadMark[] array or the contents of the wal-index
48981     ** header have changed.
48982     **
48983     ** It is necessary to check that the wal-index header did not change
48984     ** between the time it was read and when the shared-lock was obtained
48985     ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
48986     ** that the log file may have been wrapped by a writer, or that frames
48987     ** that occur later in the log than pWal->hdr.mxFrame may have been
48988     ** copied into the database by a checkpointer. If either of these things
48989     ** happened, then reading the database with the current value of
48990     ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
48991     ** instead.
48992     **
48993     ** This does not guarantee that the copy of the wal-index header is up to
48994     ** date before proceeding. That would not be possible without somehow
48995     ** blocking writers. It only guarantees that a dangerous checkpoint or
48996     ** log-wrap (either of which would require an exclusive lock on
48997     ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
48998     */
48999     walShmBarrier(pWal);
49000     if( pInfo->aReadMark[mxI]!=mxReadMark
49001      || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
49002     ){
49003       walUnlockShared(pWal, WAL_READ_LOCK(mxI));
49004       return WAL_RETRY;
49005     }else{
49006       assert( mxReadMark<=pWal->hdr.mxFrame );
49007       pWal->readLock = (i16)mxI;
49008     }
49009   }
49010   return rc;
49011 }
49012 
49013 /*
49014 ** Begin a read transaction on the database.
49015 **
49016 ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
49017 ** it takes a snapshot of the state of the WAL and wal-index for the current
49018 ** instant in time.  The current thread will continue to use this snapshot.
49019 ** Other threads might append new content to the WAL and wal-index but
49020 ** that extra content is ignored by the current thread.
49021 **
49022 ** If the database contents have changes since the previous read
49023 ** transaction, then *pChanged is set to 1 before returning.  The
49024 ** Pager layer will use this to know that is cache is stale and
49025 ** needs to be flushed.
49026 */
49027 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
49028   int rc;                         /* Return code */
49029   int cnt = 0;                    /* Number of TryBeginRead attempts */
49030 
49031   do{
49032     rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
49033   }while( rc==WAL_RETRY );
49034   testcase( (rc&0xff)==SQLITE_BUSY );
49035   testcase( (rc&0xff)==SQLITE_IOERR );
49036   testcase( rc==SQLITE_PROTOCOL );
49037   testcase( rc==SQLITE_OK );
49038   return rc;
49039 }
49040 
49041 /*
49042 ** Finish with a read transaction.  All this does is release the
49043 ** read-lock.
49044 */
49045 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
49046   sqlite3WalEndWriteTransaction(pWal);
49047   if( pWal->readLock>=0 ){
49048     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
49049     pWal->readLock = -1;
49050   }
49051 }
49052 
49053 /*
49054 ** Search the wal file for page pgno. If found, set *piRead to the frame that
49055 ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
49056 ** to zero.
49057 **
49058 ** Return SQLITE_OK if successful, or an error code if an error occurs. If an
49059 ** error does occur, the final value of *piRead is undefined.
49060 */
49061 SQLITE_PRIVATE int sqlite3WalFindFrame(
49062   Wal *pWal,                      /* WAL handle */
49063   Pgno pgno,                      /* Database page number to read data for */
49064   u32 *piRead                     /* OUT: Frame number (or zero) */
49065 ){
49066   u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
49067   u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
49068   int iHash;                      /* Used to loop through N hash tables */
49069 
49070   /* This routine is only be called from within a read transaction. */
49071   assert( pWal->readLock>=0 || pWal->lockError );
49072 
49073   /* If the "last page" field of the wal-index header snapshot is 0, then
49074   ** no data will be read from the wal under any circumstances. Return early
49075   ** in this case as an optimization.  Likewise, if pWal->readLock==0,
49076   ** then the WAL is ignored by the reader so return early, as if the
49077   ** WAL were empty.
49078   */
49079   if( iLast==0 || pWal->readLock==0 ){
49080     *piRead = 0;
49081     return SQLITE_OK;
49082   }
49083 
49084   /* Search the hash table or tables for an entry matching page number
49085   ** pgno. Each iteration of the following for() loop searches one
49086   ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
49087   **
49088   ** This code might run concurrently to the code in walIndexAppend()
49089   ** that adds entries to the wal-index (and possibly to this hash
49090   ** table). This means the value just read from the hash
49091   ** slot (aHash[iKey]) may have been added before or after the
49092   ** current read transaction was opened. Values added after the
49093   ** read transaction was opened may have been written incorrectly -
49094   ** i.e. these slots may contain garbage data. However, we assume
49095   ** that any slots written before the current read transaction was
49096   ** opened remain unmodified.
49097   **
49098   ** For the reasons above, the if(...) condition featured in the inner
49099   ** loop of the following block is more stringent that would be required
49100   ** if we had exclusive access to the hash-table:
49101   **
49102   **   (aPgno[iFrame]==pgno):
49103   **     This condition filters out normal hash-table collisions.
49104   **
49105   **   (iFrame<=iLast):
49106   **     This condition filters out entries that were added to the hash
49107   **     table after the current read-transaction had started.
49108   */
49109   for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
49110     volatile ht_slot *aHash;      /* Pointer to hash table */
49111     volatile u32 *aPgno;          /* Pointer to array of page numbers */
49112     u32 iZero;                    /* Frame number corresponding to aPgno[0] */
49113     int iKey;                     /* Hash slot index */
49114     int nCollide;                 /* Number of hash collisions remaining */
49115     int rc;                       /* Error code */
49116 
49117     rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
49118     if( rc!=SQLITE_OK ){
49119       return rc;
49120     }
49121     nCollide = HASHTABLE_NSLOT;
49122     for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
49123       u32 iFrame = aHash[iKey] + iZero;
49124       if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
49125         /* assert( iFrame>iRead ); -- not true if there is corruption */
49126         iRead = iFrame;
49127       }
49128       if( (nCollide--)==0 ){
49129         return SQLITE_CORRUPT_BKPT;
49130       }
49131     }
49132   }
49133 
49134 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
49135   /* If expensive assert() statements are available, do a linear search
49136   ** of the wal-index file content. Make sure the results agree with the
49137   ** result obtained using the hash indexes above.  */
49138   {
49139     u32 iRead2 = 0;
49140     u32 iTest;
49141     for(iTest=iLast; iTest>0; iTest--){
49142       if( walFramePgno(pWal, iTest)==pgno ){
49143         iRead2 = iTest;
49144         break;
49145       }
49146     }
49147     assert( iRead==iRead2 );
49148   }
49149 #endif
49150 
49151   *piRead = iRead;
49152   return SQLITE_OK;
49153 }
49154 
49155 /*
49156 ** Read the contents of frame iRead from the wal file into buffer pOut
49157 ** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
49158 ** error code otherwise.
49159 */
49160 SQLITE_PRIVATE int sqlite3WalReadFrame(
49161   Wal *pWal,                      /* WAL handle */
49162   u32 iRead,                      /* Frame to read */
49163   int nOut,                       /* Size of buffer pOut in bytes */
49164   u8 *pOut                        /* Buffer to write page data to */
49165 ){
49166   int sz;
49167   i64 iOffset;
49168   sz = pWal->hdr.szPage;
49169   sz = (sz&0xfe00) + ((sz&0x0001)<<16);
49170   testcase( sz<=32768 );
49171   testcase( sz>=65536 );
49172   iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
49173   /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
49174   return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
49175 }
49176 
49177 /*
49178 ** Return the size of the database in pages (or zero, if unknown).
49179 */
49180 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
49181   if( pWal && ALWAYS(pWal->readLock>=0) ){
49182     return pWal->hdr.nPage;
49183   }
49184   return 0;
49185 }
49186 
49187 
49188 /*
49189 ** This function starts a write transaction on the WAL.
49190 **
49191 ** A read transaction must have already been started by a prior call
49192 ** to sqlite3WalBeginReadTransaction().
49193 **
49194 ** If another thread or process has written into the database since
49195 ** the read transaction was started, then it is not possible for this
49196 ** thread to write as doing so would cause a fork.  So this routine
49197 ** returns SQLITE_BUSY in that case and no write transaction is started.
49198 **
49199 ** There can only be a single writer active at a time.
49200 */
49201 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
49202   int rc;
49203 
49204   /* Cannot start a write transaction without first holding a read
49205   ** transaction. */
49206   assert( pWal->readLock>=0 );
49207 
49208   if( pWal->readOnly ){
49209     return SQLITE_READONLY;
49210   }
49211 
49212   /* Only one writer allowed at a time.  Get the write lock.  Return
49213   ** SQLITE_BUSY if unable.
49214   */
49215   rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
49216   if( rc ){
49217     return rc;
49218   }
49219   pWal->writeLock = 1;
49220 
49221   /* If another connection has written to the database file since the
49222   ** time the read transaction on this connection was started, then
49223   ** the write is disallowed.
49224   */
49225   if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
49226     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
49227     pWal->writeLock = 0;
49228     rc = SQLITE_BUSY_SNAPSHOT;
49229   }
49230 
49231   return rc;
49232 }
49233 
49234 /*
49235 ** End a write transaction.  The commit has already been done.  This
49236 ** routine merely releases the lock.
49237 */
49238 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
49239   if( pWal->writeLock ){
49240     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
49241     pWal->writeLock = 0;
49242     pWal->truncateOnCommit = 0;
49243   }
49244   return SQLITE_OK;
49245 }
49246 
49247 /*
49248 ** If any data has been written (but not committed) to the log file, this
49249 ** function moves the write-pointer back to the start of the transaction.
49250 **
49251 ** Additionally, the callback function is invoked for each frame written
49252 ** to the WAL since the start of the transaction. If the callback returns
49253 ** other than SQLITE_OK, it is not invoked again and the error code is
49254 ** returned to the caller.
49255 **
49256 ** Otherwise, if the callback function does not return an error, this
49257 ** function returns SQLITE_OK.
49258 */
49259 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
49260   int rc = SQLITE_OK;
49261   if( ALWAYS(pWal->writeLock) ){
49262     Pgno iMax = pWal->hdr.mxFrame;
49263     Pgno iFrame;
49264 
49265     /* Restore the clients cache of the wal-index header to the state it
49266     ** was in before the client began writing to the database.
49267     */
49268     memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
49269 
49270     for(iFrame=pWal->hdr.mxFrame+1;
49271         ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
49272         iFrame++
49273     ){
49274       /* This call cannot fail. Unless the page for which the page number
49275       ** is passed as the second argument is (a) in the cache and
49276       ** (b) has an outstanding reference, then xUndo is either a no-op
49277       ** (if (a) is false) or simply expels the page from the cache (if (b)
49278       ** is false).
49279       **
49280       ** If the upper layer is doing a rollback, it is guaranteed that there
49281       ** are no outstanding references to any page other than page 1. And
49282       ** page 1 is never written to the log until the transaction is
49283       ** committed. As a result, the call to xUndo may not fail.
49284       */
49285       assert( walFramePgno(pWal, iFrame)!=1 );
49286       rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
49287     }
49288     if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
49289   }
49290   assert( rc==SQLITE_OK );
49291   return rc;
49292 }
49293 
49294 /*
49295 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
49296 ** values. This function populates the array with values required to
49297 ** "rollback" the write position of the WAL handle back to the current
49298 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
49299 */
49300 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
49301   assert( pWal->writeLock );
49302   aWalData[0] = pWal->hdr.mxFrame;
49303   aWalData[1] = pWal->hdr.aFrameCksum[0];
49304   aWalData[2] = pWal->hdr.aFrameCksum[1];
49305   aWalData[3] = pWal->nCkpt;
49306 }
49307 
49308 /*
49309 ** Move the write position of the WAL back to the point identified by
49310 ** the values in the aWalData[] array. aWalData must point to an array
49311 ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
49312 ** by a call to WalSavepoint().
49313 */
49314 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
49315   int rc = SQLITE_OK;
49316 
49317   assert( pWal->writeLock );
49318   assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
49319 
49320   if( aWalData[3]!=pWal->nCkpt ){
49321     /* This savepoint was opened immediately after the write-transaction
49322     ** was started. Right after that, the writer decided to wrap around
49323     ** to the start of the log. Update the savepoint values to match.
49324     */
49325     aWalData[0] = 0;
49326     aWalData[3] = pWal->nCkpt;
49327   }
49328 
49329   if( aWalData[0]<pWal->hdr.mxFrame ){
49330     pWal->hdr.mxFrame = aWalData[0];
49331     pWal->hdr.aFrameCksum[0] = aWalData[1];
49332     pWal->hdr.aFrameCksum[1] = aWalData[2];
49333     walCleanupHash(pWal);
49334   }
49335 
49336   return rc;
49337 }
49338 
49339 
49340 /*
49341 ** This function is called just before writing a set of frames to the log
49342 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
49343 ** to the current log file, it is possible to overwrite the start of the
49344 ** existing log file with the new frames (i.e. "reset" the log). If so,
49345 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
49346 ** unchanged.
49347 **
49348 ** SQLITE_OK is returned if no error is encountered (regardless of whether
49349 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
49350 ** if an error occurs.
49351 */
49352 static int walRestartLog(Wal *pWal){
49353   int rc = SQLITE_OK;
49354   int cnt;
49355 
49356   if( pWal->readLock==0 ){
49357     volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
49358     assert( pInfo->nBackfill==pWal->hdr.mxFrame );
49359     if( pInfo->nBackfill>0 ){
49360       u32 salt1;
49361       sqlite3_randomness(4, &salt1);
49362       rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
49363       if( rc==SQLITE_OK ){
49364         /* If all readers are using WAL_READ_LOCK(0) (in other words if no
49365         ** readers are currently using the WAL), then the transactions
49366         ** frames will overwrite the start of the existing log. Update the
49367         ** wal-index header to reflect this.
49368         **
49369         ** In theory it would be Ok to update the cache of the header only
49370         ** at this point. But updating the actual wal-index header is also
49371         ** safe and means there is no special case for sqlite3WalUndo()
49372         ** to handle if this transaction is rolled back.
49373         */
49374         int i;                    /* Loop counter */
49375         u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
49376 
49377         pWal->nCkpt++;
49378         pWal->hdr.mxFrame = 0;
49379         sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
49380         aSalt[1] = salt1;
49381         walIndexWriteHdr(pWal);
49382         pInfo->nBackfill = 0;
49383         pInfo->aReadMark[1] = 0;
49384         for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
49385         assert( pInfo->aReadMark[0]==0 );
49386         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
49387       }else if( rc!=SQLITE_BUSY ){
49388         return rc;
49389       }
49390     }
49391     walUnlockShared(pWal, WAL_READ_LOCK(0));
49392     pWal->readLock = -1;
49393     cnt = 0;
49394     do{
49395       int notUsed;
49396       rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
49397     }while( rc==WAL_RETRY );
49398     assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
49399     testcase( (rc&0xff)==SQLITE_IOERR );
49400     testcase( rc==SQLITE_PROTOCOL );
49401     testcase( rc==SQLITE_OK );
49402   }
49403   return rc;
49404 }
49405 
49406 /*
49407 ** Information about the current state of the WAL file and where
49408 ** the next fsync should occur - passed from sqlite3WalFrames() into
49409 ** walWriteToLog().
49410 */
49411 typedef struct WalWriter {
49412   Wal *pWal;                   /* The complete WAL information */
49413   sqlite3_file *pFd;           /* The WAL file to which we write */
49414   sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
49415   int syncFlags;               /* Flags for the fsync */
49416   int szPage;                  /* Size of one page */
49417 } WalWriter;
49418 
49419 /*
49420 ** Write iAmt bytes of content into the WAL file beginning at iOffset.
49421 ** Do a sync when crossing the p->iSyncPoint boundary.
49422 **
49423 ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
49424 ** first write the part before iSyncPoint, then sync, then write the
49425 ** rest.
49426 */
49427 static int walWriteToLog(
49428   WalWriter *p,              /* WAL to write to */
49429   void *pContent,            /* Content to be written */
49430   int iAmt,                  /* Number of bytes to write */
49431   sqlite3_int64 iOffset      /* Start writing at this offset */
49432 ){
49433   int rc;
49434   if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
49435     int iFirstAmt = (int)(p->iSyncPoint - iOffset);
49436     rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
49437     if( rc ) return rc;
49438     iOffset += iFirstAmt;
49439     iAmt -= iFirstAmt;
49440     pContent = (void*)(iFirstAmt + (char*)pContent);
49441     assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
49442     rc = sqlite3OsSync(p->pFd, p->syncFlags & SQLITE_SYNC_MASK);
49443     if( iAmt==0 || rc ) return rc;
49444   }
49445   rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
49446   return rc;
49447 }
49448 
49449 /*
49450 ** Write out a single frame of the WAL
49451 */
49452 static int walWriteOneFrame(
49453   WalWriter *p,               /* Where to write the frame */
49454   PgHdr *pPage,               /* The page of the frame to be written */
49455   int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
49456   sqlite3_int64 iOffset       /* Byte offset at which to write */
49457 ){
49458   int rc;                         /* Result code from subfunctions */
49459   void *pData;                    /* Data actually written */
49460   u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
49461 #if defined(SQLITE_HAS_CODEC)
49462   if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
49463 #else
49464   pData = pPage->pData;
49465 #endif
49466   walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
49467   rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
49468   if( rc ) return rc;
49469   /* Write the page data */
49470   rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
49471   return rc;
49472 }
49473 
49474 /*
49475 ** Write a set of frames to the log. The caller must hold the write-lock
49476 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
49477 */
49478 SQLITE_PRIVATE int sqlite3WalFrames(
49479   Wal *pWal,                      /* Wal handle to write to */
49480   int szPage,                     /* Database page-size in bytes */
49481   PgHdr *pList,                   /* List of dirty pages to write */
49482   Pgno nTruncate,                 /* Database size after this commit */
49483   int isCommit,                   /* True if this is a commit */
49484   int sync_flags                  /* Flags to pass to OsSync() (or 0) */
49485 ){
49486   int rc;                         /* Used to catch return codes */
49487   u32 iFrame;                     /* Next frame address */
49488   PgHdr *p;                       /* Iterator to run through pList with. */
49489   PgHdr *pLast = 0;               /* Last frame in list */
49490   int nExtra = 0;                 /* Number of extra copies of last page */
49491   int szFrame;                    /* The size of a single frame */
49492   i64 iOffset;                    /* Next byte to write in WAL file */
49493   WalWriter w;                    /* The writer */
49494 
49495   assert( pList );
49496   assert( pWal->writeLock );
49497 
49498   /* If this frame set completes a transaction, then nTruncate>0.  If
49499   ** nTruncate==0 then this frame set does not complete the transaction. */
49500   assert( (isCommit!=0)==(nTruncate!=0) );
49501 
49502 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
49503   { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
49504     WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
49505               pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
49506   }
49507 #endif
49508 
49509   /* See if it is possible to write these frames into the start of the
49510   ** log file, instead of appending to it at pWal->hdr.mxFrame.
49511   */
49512   if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
49513     return rc;
49514   }
49515 
49516   /* If this is the first frame written into the log, write the WAL
49517   ** header to the start of the WAL file. See comments at the top of
49518   ** this source file for a description of the WAL header format.
49519   */
49520   iFrame = pWal->hdr.mxFrame;
49521   if( iFrame==0 ){
49522     u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
49523     u32 aCksum[2];                /* Checksum for wal-header */
49524 
49525     sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
49526     sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
49527     sqlite3Put4byte(&aWalHdr[8], szPage);
49528     sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
49529     if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
49530     memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
49531     walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
49532     sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
49533     sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
49534 
49535     pWal->szPage = szPage;
49536     pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
49537     pWal->hdr.aFrameCksum[0] = aCksum[0];
49538     pWal->hdr.aFrameCksum[1] = aCksum[1];
49539     pWal->truncateOnCommit = 1;
49540 
49541     rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
49542     WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
49543     if( rc!=SQLITE_OK ){
49544       return rc;
49545     }
49546 
49547     /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
49548     ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
49549     ** an out-of-order write following a WAL restart could result in
49550     ** database corruption.  See the ticket:
49551     **
49552     **     http://localhost:591/sqlite/info/ff5be73dee
49553     */
49554     if( pWal->syncHeader && sync_flags ){
49555       rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
49556       if( rc ) return rc;
49557     }
49558   }
49559   assert( (int)pWal->szPage==szPage );
49560 
49561   /* Setup information needed to write frames into the WAL */
49562   w.pWal = pWal;
49563   w.pFd = pWal->pWalFd;
49564   w.iSyncPoint = 0;
49565   w.syncFlags = sync_flags;
49566   w.szPage = szPage;
49567   iOffset = walFrameOffset(iFrame+1, szPage);
49568   szFrame = szPage + WAL_FRAME_HDRSIZE;
49569 
49570   /* Write all frames into the log file exactly once */
49571   for(p=pList; p; p=p->pDirty){
49572     int nDbSize;   /* 0 normally.  Positive == commit flag */
49573     iFrame++;
49574     assert( iOffset==walFrameOffset(iFrame, szPage) );
49575     nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
49576     rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
49577     if( rc ) return rc;
49578     pLast = p;
49579     iOffset += szFrame;
49580   }
49581 
49582   /* If this is the end of a transaction, then we might need to pad
49583   ** the transaction and/or sync the WAL file.
49584   **
49585   ** Padding and syncing only occur if this set of frames complete a
49586   ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
49587   ** or synchonous==OFF, then no padding or syncing are needed.
49588   **
49589   ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
49590   ** needed and only the sync is done.  If padding is needed, then the
49591   ** final frame is repeated (with its commit mark) until the next sector
49592   ** boundary is crossed.  Only the part of the WAL prior to the last
49593   ** sector boundary is synced; the part of the last frame that extends
49594   ** past the sector boundary is written after the sync.
49595   */
49596   if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
49597     if( pWal->padToSectorBoundary ){
49598       int sectorSize = sqlite3SectorSize(pWal->pWalFd);
49599       w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
49600       while( iOffset<w.iSyncPoint ){
49601         rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
49602         if( rc ) return rc;
49603         iOffset += szFrame;
49604         nExtra++;
49605       }
49606     }else{
49607       rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
49608     }
49609   }
49610 
49611   /* If this frame set completes the first transaction in the WAL and
49612   ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
49613   ** journal size limit, if possible.
49614   */
49615   if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
49616     i64 sz = pWal->mxWalSize;
49617     if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
49618       sz = walFrameOffset(iFrame+nExtra+1, szPage);
49619     }
49620     walLimitSize(pWal, sz);
49621     pWal->truncateOnCommit = 0;
49622   }
49623 
49624   /* Append data to the wal-index. It is not necessary to lock the
49625   ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
49626   ** guarantees that there are no other writers, and no data that may
49627   ** be in use by existing readers is being overwritten.
49628   */
49629   iFrame = pWal->hdr.mxFrame;
49630   for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
49631     iFrame++;
49632     rc = walIndexAppend(pWal, iFrame, p->pgno);
49633   }
49634   while( rc==SQLITE_OK && nExtra>0 ){
49635     iFrame++;
49636     nExtra--;
49637     rc = walIndexAppend(pWal, iFrame, pLast->pgno);
49638   }
49639 
49640   if( rc==SQLITE_OK ){
49641     /* Update the private copy of the header. */
49642     pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
49643     testcase( szPage<=32768 );
49644     testcase( szPage>=65536 );
49645     pWal->hdr.mxFrame = iFrame;
49646     if( isCommit ){
49647       pWal->hdr.iChange++;
49648       pWal->hdr.nPage = nTruncate;
49649     }
49650     /* If this is a commit, update the wal-index header too. */
49651     if( isCommit ){
49652       walIndexWriteHdr(pWal);
49653       pWal->iCallback = iFrame;
49654     }
49655   }
49656 
49657   WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
49658   return rc;
49659 }
49660 
49661 /*
49662 ** This routine is called to implement sqlite3_wal_checkpoint() and
49663 ** related interfaces.
49664 **
49665 ** Obtain a CHECKPOINT lock and then backfill as much information as
49666 ** we can from WAL into the database.
49667 **
49668 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
49669 ** callback. In this case this function runs a blocking checkpoint.
49670 */
49671 SQLITE_PRIVATE int sqlite3WalCheckpoint(
49672   Wal *pWal,                      /* Wal connection */
49673   int eMode,                      /* PASSIVE, FULL or RESTART */
49674   int (*xBusy)(void*),            /* Function to call when busy */
49675   void *pBusyArg,                 /* Context argument for xBusyHandler */
49676   int sync_flags,                 /* Flags to sync db file with (or 0) */
49677   int nBuf,                       /* Size of temporary buffer */
49678   u8 *zBuf,                       /* Temporary buffer to use */
49679   int *pnLog,                     /* OUT: Number of frames in WAL */
49680   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
49681 ){
49682   int rc;                         /* Return code */
49683   int isChanged = 0;              /* True if a new wal-index header is loaded */
49684   int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
49685 
49686   assert( pWal->ckptLock==0 );
49687   assert( pWal->writeLock==0 );
49688 
49689   if( pWal->readOnly ) return SQLITE_READONLY;
49690   WALTRACE(("WAL%p: checkpoint begins\n", pWal));
49691   rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
49692   if( rc ){
49693     /* Usually this is SQLITE_BUSY meaning that another thread or process
49694     ** is already running a checkpoint, or maybe a recovery.  But it might
49695     ** also be SQLITE_IOERR. */
49696     return rc;
49697   }
49698   pWal->ckptLock = 1;
49699 
49700   /* If this is a blocking-checkpoint, then obtain the write-lock as well
49701   ** to prevent any writers from running while the checkpoint is underway.
49702   ** This has to be done before the call to walIndexReadHdr() below.
49703   **
49704   ** If the writer lock cannot be obtained, then a passive checkpoint is
49705   ** run instead. Since the checkpointer is not holding the writer lock,
49706   ** there is no point in blocking waiting for any readers. Assuming no
49707   ** other error occurs, this function will return SQLITE_BUSY to the caller.
49708   */
49709   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
49710     rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
49711     if( rc==SQLITE_OK ){
49712       pWal->writeLock = 1;
49713     }else if( rc==SQLITE_BUSY ){
49714       eMode2 = SQLITE_CHECKPOINT_PASSIVE;
49715       rc = SQLITE_OK;
49716     }
49717   }
49718 
49719   /* Read the wal-index header. */
49720   if( rc==SQLITE_OK ){
49721     rc = walIndexReadHdr(pWal, &isChanged);
49722     if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
49723       sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
49724     }
49725   }
49726 
49727   /* Copy data from the log to the database file. */
49728   if( rc==SQLITE_OK ){
49729     if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
49730       rc = SQLITE_CORRUPT_BKPT;
49731     }else{
49732       rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
49733     }
49734 
49735     /* If no error occurred, set the output variables. */
49736     if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
49737       if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
49738       if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
49739     }
49740   }
49741 
49742   if( isChanged ){
49743     /* If a new wal-index header was loaded before the checkpoint was
49744     ** performed, then the pager-cache associated with pWal is now
49745     ** out of date. So zero the cached wal-index header to ensure that
49746     ** next time the pager opens a snapshot on this database it knows that
49747     ** the cache needs to be reset.
49748     */
49749     memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
49750   }
49751 
49752   /* Release the locks. */
49753   sqlite3WalEndWriteTransaction(pWal);
49754   walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
49755   pWal->ckptLock = 0;
49756   WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
49757   return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
49758 }
49759 
49760 /* Return the value to pass to a sqlite3_wal_hook callback, the
49761 ** number of frames in the WAL at the point of the last commit since
49762 ** sqlite3WalCallback() was called.  If no commits have occurred since
49763 ** the last call, then return 0.
49764 */
49765 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
49766   u32 ret = 0;
49767   if( pWal ){
49768     ret = pWal->iCallback;
49769     pWal->iCallback = 0;
49770   }
49771   return (int)ret;
49772 }
49773 
49774 /*
49775 ** This function is called to change the WAL subsystem into or out
49776 ** of locking_mode=EXCLUSIVE.
49777 **
49778 ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
49779 ** into locking_mode=NORMAL.  This means that we must acquire a lock
49780 ** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
49781 ** or if the acquisition of the lock fails, then return 0.  If the
49782 ** transition out of exclusive-mode is successful, return 1.  This
49783 ** operation must occur while the pager is still holding the exclusive
49784 ** lock on the main database file.
49785 **
49786 ** If op is one, then change from locking_mode=NORMAL into
49787 ** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
49788 ** be released.  Return 1 if the transition is made and 0 if the
49789 ** WAL is already in exclusive-locking mode - meaning that this
49790 ** routine is a no-op.  The pager must already hold the exclusive lock
49791 ** on the main database file before invoking this operation.
49792 **
49793 ** If op is negative, then do a dry-run of the op==1 case but do
49794 ** not actually change anything. The pager uses this to see if it
49795 ** should acquire the database exclusive lock prior to invoking
49796 ** the op==1 case.
49797 */
49798 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
49799   int rc;
49800   assert( pWal->writeLock==0 );
49801   assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
49802 
49803   /* pWal->readLock is usually set, but might be -1 if there was a
49804   ** prior error while attempting to acquire are read-lock. This cannot
49805   ** happen if the connection is actually in exclusive mode (as no xShmLock
49806   ** locks are taken in this case). Nor should the pager attempt to
49807   ** upgrade to exclusive-mode following such an error.
49808   */
49809   assert( pWal->readLock>=0 || pWal->lockError );
49810   assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
49811 
49812   if( op==0 ){
49813     if( pWal->exclusiveMode ){
49814       pWal->exclusiveMode = 0;
49815       if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
49816         pWal->exclusiveMode = 1;
49817       }
49818       rc = pWal->exclusiveMode==0;
49819     }else{
49820       /* Already in locking_mode=NORMAL */
49821       rc = 0;
49822     }
49823   }else if( op>0 ){
49824     assert( pWal->exclusiveMode==0 );
49825     assert( pWal->readLock>=0 );
49826     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
49827     pWal->exclusiveMode = 1;
49828     rc = 1;
49829   }else{
49830     rc = pWal->exclusiveMode==0;
49831   }
49832   return rc;
49833 }
49834 
49835 /*
49836 ** Return true if the argument is non-NULL and the WAL module is using
49837 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
49838 ** WAL module is using shared-memory, return false.
49839 */
49840 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
49841   return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
49842 }
49843 
49844 #ifdef SQLITE_ENABLE_ZIPVFS
49845 /*
49846 ** If the argument is not NULL, it points to a Wal object that holds a
49847 ** read-lock. This function returns the database page-size if it is known,
49848 ** or zero if it is not (or if pWal is NULL).
49849 */
49850 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
49851   assert( pWal==0 || pWal->readLock>=0 );
49852   return (pWal ? pWal->szPage : 0);
49853 }
49854 #endif
49855 
49856 #endif /* #ifndef SQLITE_OMIT_WAL */
49857 
49858 /************** End of wal.c *************************************************/
49859 /************** Begin file btmutex.c *****************************************/
49860 /*
49861 ** 2007 August 27
49862 **
49863 ** The author disclaims copyright to this source code.  In place of
49864 ** a legal notice, here is a blessing:
49865 **
49866 **    May you do good and not evil.
49867 **    May you find forgiveness for yourself and forgive others.
49868 **    May you share freely, never taking more than you give.
49869 **
49870 *************************************************************************
49871 **
49872 ** This file contains code used to implement mutexes on Btree objects.
49873 ** This code really belongs in btree.c.  But btree.c is getting too
49874 ** big and we want to break it down some.  This packaged seemed like
49875 ** a good breakout.
49876 */
49877 /************** Include btreeInt.h in the middle of btmutex.c ****************/
49878 /************** Begin file btreeInt.h ****************************************/
49879 /*
49880 ** 2004 April 6
49881 **
49882 ** The author disclaims copyright to this source code.  In place of
49883 ** a legal notice, here is a blessing:
49884 **
49885 **    May you do good and not evil.
49886 **    May you find forgiveness for yourself and forgive others.
49887 **    May you share freely, never taking more than you give.
49888 **
49889 *************************************************************************
49890 ** This file implements a external (disk-based) database using BTrees.
49891 ** For a detailed discussion of BTrees, refer to
49892 **
49893 **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
49894 **     "Sorting And Searching", pages 473-480. Addison-Wesley
49895 **     Publishing Company, Reading, Massachusetts.
49896 **
49897 ** The basic idea is that each page of the file contains N database
49898 ** entries and N+1 pointers to subpages.
49899 **
49900 **   ----------------------------------------------------------------
49901 **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
49902 **   ----------------------------------------------------------------
49903 **
49904 ** All of the keys on the page that Ptr(0) points to have values less
49905 ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
49906 ** values greater than Key(0) and less than Key(1).  All of the keys
49907 ** on Ptr(N) and its subpages have values greater than Key(N-1).  And
49908 ** so forth.
49909 **
49910 ** Finding a particular key requires reading O(log(M)) pages from the
49911 ** disk where M is the number of entries in the tree.
49912 **
49913 ** In this implementation, a single file can hold one or more separate
49914 ** BTrees.  Each BTree is identified by the index of its root page.  The
49915 ** key and data for any entry are combined to form the "payload".  A
49916 ** fixed amount of payload can be carried directly on the database
49917 ** page.  If the payload is larger than the preset amount then surplus
49918 ** bytes are stored on overflow pages.  The payload for an entry
49919 ** and the preceding pointer are combined to form a "Cell".  Each
49920 ** page has a small header which contains the Ptr(N) pointer and other
49921 ** information such as the size of key and data.
49922 **
49923 ** FORMAT DETAILS
49924 **
49925 ** The file is divided into pages.  The first page is called page 1,
49926 ** the second is page 2, and so forth.  A page number of zero indicates
49927 ** "no such page".  The page size can be any power of 2 between 512 and 65536.
49928 ** Each page can be either a btree page, a freelist page, an overflow
49929 ** page, or a pointer-map page.
49930 **
49931 ** The first page is always a btree page.  The first 100 bytes of the first
49932 ** page contain a special header (the "file header") that describes the file.
49933 ** The format of the file header is as follows:
49934 **
49935 **   OFFSET   SIZE    DESCRIPTION
49936 **      0      16     Header string: "SQLite format 3\000"
49937 **     16       2     Page size in bytes.  (1 means 65536)
49938 **     18       1     File format write version
49939 **     19       1     File format read version
49940 **     20       1     Bytes of unused space at the end of each page
49941 **     21       1     Max embedded payload fraction (must be 64)
49942 **     22       1     Min embedded payload fraction (must be 32)
49943 **     23       1     Min leaf payload fraction (must be 32)
49944 **     24       4     File change counter
49945 **     28       4     Reserved for future use
49946 **     32       4     First freelist page
49947 **     36       4     Number of freelist pages in the file
49948 **     40      60     15 4-byte meta values passed to higher layers
49949 **
49950 **     40       4     Schema cookie
49951 **     44       4     File format of schema layer
49952 **     48       4     Size of page cache
49953 **     52       4     Largest root-page (auto/incr_vacuum)
49954 **     56       4     1=UTF-8 2=UTF16le 3=UTF16be
49955 **     60       4     User version
49956 **     64       4     Incremental vacuum mode
49957 **     68       4     Application-ID
49958 **     72      20     unused
49959 **     92       4     The version-valid-for number
49960 **     96       4     SQLITE_VERSION_NUMBER
49961 **
49962 ** All of the integer values are big-endian (most significant byte first).
49963 **
49964 ** The file change counter is incremented when the database is changed
49965 ** This counter allows other processes to know when the file has changed
49966 ** and thus when they need to flush their cache.
49967 **
49968 ** The max embedded payload fraction is the amount of the total usable
49969 ** space in a page that can be consumed by a single cell for standard
49970 ** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
49971 ** is to limit the maximum cell size so that at least 4 cells will fit
49972 ** on one page.  Thus the default max embedded payload fraction is 64.
49973 **
49974 ** If the payload for a cell is larger than the max payload, then extra
49975 ** payload is spilled to overflow pages.  Once an overflow page is allocated,
49976 ** as many bytes as possible are moved into the overflow pages without letting
49977 ** the cell size drop below the min embedded payload fraction.
49978 **
49979 ** The min leaf payload fraction is like the min embedded payload fraction
49980 ** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
49981 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
49982 ** not specified in the header.
49983 **
49984 ** Each btree pages is divided into three sections:  The header, the
49985 ** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
49986 ** file header that occurs before the page header.
49987 **
49988 **      |----------------|
49989 **      | file header    |   100 bytes.  Page 1 only.
49990 **      |----------------|
49991 **      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
49992 **      |----------------|
49993 **      | cell pointer   |   |  2 bytes per cell.  Sorted order.
49994 **      | array          |   |  Grows downward
49995 **      |                |   v
49996 **      |----------------|
49997 **      | unallocated    |
49998 **      | space          |
49999 **      |----------------|   ^  Grows upwards
50000 **      | cell content   |   |  Arbitrary order interspersed with freeblocks.
50001 **      | area           |   |  and free space fragments.
50002 **      |----------------|
50003 **
50004 ** The page headers looks like this:
50005 **
50006 **   OFFSET   SIZE     DESCRIPTION
50007 **      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
50008 **      1       2      byte offset to the first freeblock
50009 **      3       2      number of cells on this page
50010 **      5       2      first byte of the cell content area
50011 **      7       1      number of fragmented free bytes
50012 **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
50013 **
50014 ** The flags define the format of this btree page.  The leaf flag means that
50015 ** this page has no children.  The zerodata flag means that this page carries
50016 ** only keys and no data.  The intkey flag means that the key is a integer
50017 ** which is stored in the key size entry of the cell header rather than in
50018 ** the payload area.
50019 **
50020 ** The cell pointer array begins on the first byte after the page header.
50021 ** The cell pointer array contains zero or more 2-byte numbers which are
50022 ** offsets from the beginning of the page to the cell content in the cell
50023 ** content area.  The cell pointers occur in sorted order.  The system strives
50024 ** to keep free space after the last cell pointer so that new cells can
50025 ** be easily added without having to defragment the page.
50026 **
50027 ** Cell content is stored at the very end of the page and grows toward the
50028 ** beginning of the page.
50029 **
50030 ** Unused space within the cell content area is collected into a linked list of
50031 ** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
50032 ** to the first freeblock is given in the header.  Freeblocks occur in
50033 ** increasing order.  Because a freeblock must be at least 4 bytes in size,
50034 ** any group of 3 or fewer unused bytes in the cell content area cannot
50035 ** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
50036 ** a fragment.  The total number of bytes in all fragments is recorded.
50037 ** in the page header at offset 7.
50038 **
50039 **    SIZE    DESCRIPTION
50040 **      2     Byte offset of the next freeblock
50041 **      2     Bytes in this freeblock
50042 **
50043 ** Cells are of variable length.  Cells are stored in the cell content area at
50044 ** the end of the page.  Pointers to the cells are in the cell pointer array
50045 ** that immediately follows the page header.  Cells is not necessarily
50046 ** contiguous or in order, but cell pointers are contiguous and in order.
50047 **
50048 ** Cell content makes use of variable length integers.  A variable
50049 ** length integer is 1 to 9 bytes where the lower 7 bits of each
50050 ** byte are used.  The integer consists of all bytes that have bit 8 set and
50051 ** the first byte with bit 8 clear.  The most significant byte of the integer
50052 ** appears first.  A variable-length integer may not be more than 9 bytes long.
50053 ** As a special case, all 8 bytes of the 9th byte are used as data.  This
50054 ** allows a 64-bit integer to be encoded in 9 bytes.
50055 **
50056 **    0x00                      becomes  0x00000000
50057 **    0x7f                      becomes  0x0000007f
50058 **    0x81 0x00                 becomes  0x00000080
50059 **    0x82 0x00                 becomes  0x00000100
50060 **    0x80 0x7f                 becomes  0x0000007f
50061 **    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
50062 **    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
50063 **
50064 ** Variable length integers are used for rowids and to hold the number of
50065 ** bytes of key and data in a btree cell.
50066 **
50067 ** The content of a cell looks like this:
50068 **
50069 **    SIZE    DESCRIPTION
50070 **      4     Page number of the left child. Omitted if leaf flag is set.
50071 **     var    Number of bytes of data. Omitted if the zerodata flag is set.
50072 **     var    Number of bytes of key. Or the key itself if intkey flag is set.
50073 **      *     Payload
50074 **      4     First page of the overflow chain.  Omitted if no overflow
50075 **
50076 ** Overflow pages form a linked list.  Each page except the last is completely
50077 ** filled with data (pagesize - 4 bytes).  The last page can have as little
50078 ** as 1 byte of data.
50079 **
50080 **    SIZE    DESCRIPTION
50081 **      4     Page number of next overflow page
50082 **      *     Data
50083 **
50084 ** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
50085 ** file header points to the first in a linked list of trunk page.  Each trunk
50086 ** page points to multiple leaf pages.  The content of a leaf page is
50087 ** unspecified.  A trunk page looks like this:
50088 **
50089 **    SIZE    DESCRIPTION
50090 **      4     Page number of next trunk page
50091 **      4     Number of leaf pointers on this page
50092 **      *     zero or more pages numbers of leaves
50093 */
50094 
50095 
50096 /* The following value is the maximum cell size assuming a maximum page
50097 ** size give above.
50098 */
50099 #define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
50100 
50101 /* The maximum number of cells on a single page of the database.  This
50102 ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
50103 ** plus 2 bytes for the index to the cell in the page header).  Such
50104 ** small cells will be rare, but they are possible.
50105 */
50106 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
50107 
50108 /* Forward declarations */
50109 typedef struct MemPage MemPage;
50110 typedef struct BtLock BtLock;
50111 
50112 /*
50113 ** This is a magic string that appears at the beginning of every
50114 ** SQLite database in order to identify the file as a real database.
50115 **
50116 ** You can change this value at compile-time by specifying a
50117 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
50118 ** header must be exactly 16 bytes including the zero-terminator so
50119 ** the string itself should be 15 characters long.  If you change
50120 ** the header, then your custom library will not be able to read
50121 ** databases generated by the standard tools and the standard tools
50122 ** will not be able to read databases created by your custom library.
50123 */
50124 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
50125 #  define SQLITE_FILE_HEADER "SQLite format 3"
50126 #endif
50127 
50128 /*
50129 ** Page type flags.  An ORed combination of these flags appear as the
50130 ** first byte of on-disk image of every BTree page.
50131 */
50132 #define PTF_INTKEY    0x01
50133 #define PTF_ZERODATA  0x02
50134 #define PTF_LEAFDATA  0x04
50135 #define PTF_LEAF      0x08
50136 
50137 /*
50138 ** As each page of the file is loaded into memory, an instance of the following
50139 ** structure is appended and initialized to zero.  This structure stores
50140 ** information about the page that is decoded from the raw file page.
50141 **
50142 ** The pParent field points back to the parent page.  This allows us to
50143 ** walk up the BTree from any leaf to the root.  Care must be taken to
50144 ** unref() the parent page pointer when this page is no longer referenced.
50145 ** The pageDestructor() routine handles that chore.
50146 **
50147 ** Access to all fields of this structure is controlled by the mutex
50148 ** stored in MemPage.pBt->mutex.
50149 */
50150 struct MemPage {
50151   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
50152   u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
50153   u8 intKey;           /* True if intkey flag is set */
50154   u8 leaf;             /* True if leaf flag is set */
50155   u8 hasData;          /* True if this page stores data */
50156   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
50157   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
50158   u8 max1bytePayload;  /* min(maxLocal,127) */
50159   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
50160   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
50161   u16 cellOffset;      /* Index in aData of first cell pointer */
50162   u16 nFree;           /* Number of free bytes on the page */
50163   u16 nCell;           /* Number of cells on this page, local and ovfl */
50164   u16 maskPage;        /* Mask for page offset */
50165   u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
50166                        ** non-overflow cell */
50167   u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
50168   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
50169   u8 *aData;           /* Pointer to disk image of the page data */
50170   u8 *aDataEnd;        /* One byte past the end of usable data */
50171   u8 *aCellIdx;        /* The cell index area */
50172   DbPage *pDbPage;     /* Pager page handle */
50173   Pgno pgno;           /* Page number for this page */
50174 };
50175 
50176 /*
50177 ** The in-memory image of a disk page has the auxiliary information appended
50178 ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
50179 ** that extra information.
50180 */
50181 #define EXTRA_SIZE sizeof(MemPage)
50182 
50183 /*
50184 ** A linked list of the following structures is stored at BtShared.pLock.
50185 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
50186 ** is opened on the table with root page BtShared.iTable. Locks are removed
50187 ** from this list when a transaction is committed or rolled back, or when
50188 ** a btree handle is closed.
50189 */
50190 struct BtLock {
50191   Btree *pBtree;        /* Btree handle holding this lock */
50192   Pgno iTable;          /* Root page of table */
50193   u8 eLock;             /* READ_LOCK or WRITE_LOCK */
50194   BtLock *pNext;        /* Next in BtShared.pLock list */
50195 };
50196 
50197 /* Candidate values for BtLock.eLock */
50198 #define READ_LOCK     1
50199 #define WRITE_LOCK    2
50200 
50201 /* A Btree handle
50202 **
50203 ** A database connection contains a pointer to an instance of
50204 ** this object for every database file that it has open.  This structure
50205 ** is opaque to the database connection.  The database connection cannot
50206 ** see the internals of this structure and only deals with pointers to
50207 ** this structure.
50208 **
50209 ** For some database files, the same underlying database cache might be
50210 ** shared between multiple connections.  In that case, each connection
50211 ** has it own instance of this object.  But each instance of this object
50212 ** points to the same BtShared object.  The database cache and the
50213 ** schema associated with the database file are all contained within
50214 ** the BtShared object.
50215 **
50216 ** All fields in this structure are accessed under sqlite3.mutex.
50217 ** The pBt pointer itself may not be changed while there exists cursors
50218 ** in the referenced BtShared that point back to this Btree since those
50219 ** cursors have to go through this Btree to find their BtShared and
50220 ** they often do so without holding sqlite3.mutex.
50221 */
50222 struct Btree {
50223   sqlite3 *db;       /* The database connection holding this btree */
50224   BtShared *pBt;     /* Sharable content of this btree */
50225   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
50226   u8 sharable;       /* True if we can share pBt with another db */
50227   u8 locked;         /* True if db currently has pBt locked */
50228   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
50229   int nBackup;       /* Number of backup operations reading this btree */
50230   Btree *pNext;      /* List of other sharable Btrees from the same db */
50231   Btree *pPrev;      /* Back pointer of the same list */
50232 #ifndef SQLITE_OMIT_SHARED_CACHE
50233   BtLock lock;       /* Object used to lock page 1 */
50234 #endif
50235 };
50236 
50237 /*
50238 ** Btree.inTrans may take one of the following values.
50239 **
50240 ** If the shared-data extension is enabled, there may be multiple users
50241 ** of the Btree structure. At most one of these may open a write transaction,
50242 ** but any number may have active read transactions.
50243 */
50244 #define TRANS_NONE  0
50245 #define TRANS_READ  1
50246 #define TRANS_WRITE 2
50247 
50248 /*
50249 ** An instance of this object represents a single database file.
50250 **
50251 ** A single database file can be in use at the same time by two
50252 ** or more database connections.  When two or more connections are
50253 ** sharing the same database file, each connection has it own
50254 ** private Btree object for the file and each of those Btrees points
50255 ** to this one BtShared object.  BtShared.nRef is the number of
50256 ** connections currently sharing this database file.
50257 **
50258 ** Fields in this structure are accessed under the BtShared.mutex
50259 ** mutex, except for nRef and pNext which are accessed under the
50260 ** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
50261 ** may not be modified once it is initially set as long as nRef>0.
50262 ** The pSchema field may be set once under BtShared.mutex and
50263 ** thereafter is unchanged as long as nRef>0.
50264 **
50265 ** isPending:
50266 **
50267 **   If a BtShared client fails to obtain a write-lock on a database
50268 **   table (because there exists one or more read-locks on the table),
50269 **   the shared-cache enters 'pending-lock' state and isPending is
50270 **   set to true.
50271 **
50272 **   The shared-cache leaves the 'pending lock' state when either of
50273 **   the following occur:
50274 **
50275 **     1) The current writer (BtShared.pWriter) concludes its transaction, OR
50276 **     2) The number of locks held by other connections drops to zero.
50277 **
50278 **   while in the 'pending-lock' state, no connection may start a new
50279 **   transaction.
50280 **
50281 **   This feature is included to help prevent writer-starvation.
50282 */
50283 struct BtShared {
50284   Pager *pPager;        /* The page cache */
50285   sqlite3 *db;          /* Database connection currently using this Btree */
50286   BtCursor *pCursor;    /* A list of all open cursors */
50287   MemPage *pPage1;      /* First page of the database */
50288   u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
50289 #ifndef SQLITE_OMIT_AUTOVACUUM
50290   u8 autoVacuum;        /* True if auto-vacuum is enabled */
50291   u8 incrVacuum;        /* True if incr-vacuum is enabled */
50292   u8 bDoTruncate;       /* True to truncate db on commit */
50293 #endif
50294   u8 inTransaction;     /* Transaction state */
50295   u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
50296   u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
50297   u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
50298   u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
50299   u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
50300   u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
50301   u32 pageSize;         /* Total number of bytes on a page */
50302   u32 usableSize;       /* Number of usable bytes on each page */
50303   int nTransaction;     /* Number of open transactions (read + write) */
50304   u32 nPage;            /* Number of pages in the database */
50305   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
50306   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
50307   sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
50308   Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
50309 #ifndef SQLITE_OMIT_SHARED_CACHE
50310   int nRef;             /* Number of references to this structure */
50311   BtShared *pNext;      /* Next on a list of sharable BtShared structs */
50312   BtLock *pLock;        /* List of locks held on this shared-btree struct */
50313   Btree *pWriter;       /* Btree with currently open write transaction */
50314 #endif
50315   u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
50316 };
50317 
50318 /*
50319 ** Allowed values for BtShared.btsFlags
50320 */
50321 #define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
50322 #define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
50323 #define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
50324 #define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
50325 #define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
50326 #define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
50327 #define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
50328 
50329 /*
50330 ** An instance of the following structure is used to hold information
50331 ** about a cell.  The parseCellPtr() function fills in this structure
50332 ** based on information extract from the raw disk page.
50333 */
50334 typedef struct CellInfo CellInfo;
50335 struct CellInfo {
50336   i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
50337   u8 *pCell;     /* Pointer to the start of cell content */
50338   u32 nData;     /* Number of bytes of data */
50339   u32 nPayload;  /* Total amount of payload */
50340   u16 nHeader;   /* Size of the cell content header in bytes */
50341   u16 nLocal;    /* Amount of payload held locally */
50342   u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
50343   u16 nSize;     /* Size of the cell content on the main b-tree page */
50344 };
50345 
50346 /*
50347 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
50348 ** this will be declared corrupt. This value is calculated based on a
50349 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
50350 ** root-node and 3 for all other internal nodes.
50351 **
50352 ** If a tree that appears to be taller than this is encountered, it is
50353 ** assumed that the database is corrupt.
50354 */
50355 #define BTCURSOR_MAX_DEPTH 20
50356 
50357 /*
50358 ** A cursor is a pointer to a particular entry within a particular
50359 ** b-tree within a database file.
50360 **
50361 ** The entry is identified by its MemPage and the index in
50362 ** MemPage.aCell[] of the entry.
50363 **
50364 ** A single database file can be shared by two more database connections,
50365 ** but cursors cannot be shared.  Each cursor is associated with a
50366 ** particular database connection identified BtCursor.pBtree.db.
50367 **
50368 ** Fields in this structure are accessed under the BtShared.mutex
50369 ** found at self->pBt->mutex.
50370 */
50371 struct BtCursor {
50372   Btree *pBtree;            /* The Btree to which this cursor belongs */
50373   BtShared *pBt;            /* The BtShared this cursor points to */
50374   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
50375   struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
50376 #ifndef SQLITE_OMIT_INCRBLOB
50377   Pgno *aOverflow;          /* Cache of overflow page locations */
50378 #endif
50379   Pgno pgnoRoot;            /* The root page of this tree */
50380   CellInfo info;            /* A parse of the cell we are pointing at */
50381   i64 nKey;        /* Size of pKey, or last integer key */
50382   void *pKey;      /* Saved key that was cursor's last known position */
50383   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
50384   u8 wrFlag;                /* True if writable */
50385   u8 atLast;                /* Cursor pointing to the last entry */
50386   u8 validNKey;             /* True if info.nKey is valid */
50387   u8 eState;                /* One of the CURSOR_XXX constants (see below) */
50388 #ifndef SQLITE_OMIT_INCRBLOB
50389   u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
50390 #endif
50391   u8 hints;                             /* As configured by CursorSetHints() */
50392   i16 iPage;                            /* Index of current page in apPage */
50393   u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
50394   MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
50395 };
50396 
50397 /*
50398 ** Potential values for BtCursor.eState.
50399 **
50400 ** CURSOR_INVALID:
50401 **   Cursor does not point to a valid entry. This can happen (for example)
50402 **   because the table is empty or because BtreeCursorFirst() has not been
50403 **   called.
50404 **
50405 ** CURSOR_VALID:
50406 **   Cursor points to a valid entry. getPayload() etc. may be called.
50407 **
50408 ** CURSOR_SKIPNEXT:
50409 **   Cursor is valid except that the Cursor.skipNext field is non-zero
50410 **   indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
50411 **   operation should be a no-op.
50412 **
50413 ** CURSOR_REQUIRESEEK:
50414 **   The table that this cursor was opened on still exists, but has been
50415 **   modified since the cursor was last used. The cursor position is saved
50416 **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
50417 **   this state, restoreCursorPosition() can be called to attempt to
50418 **   seek the cursor to the saved position.
50419 **
50420 ** CURSOR_FAULT:
50421 **   A unrecoverable error (an I/O error or a malloc failure) has occurred
50422 **   on a different connection that shares the BtShared cache with this
50423 **   cursor.  The error has left the cache in an inconsistent state.
50424 **   Do nothing else with this cursor.  Any attempt to use the cursor
50425 **   should return the error code stored in BtCursor.skip
50426 */
50427 #define CURSOR_INVALID           0
50428 #define CURSOR_VALID             1
50429 #define CURSOR_SKIPNEXT          2
50430 #define CURSOR_REQUIRESEEK       3
50431 #define CURSOR_FAULT             4
50432 
50433 /*
50434 ** The database page the PENDING_BYTE occupies. This page is never used.
50435 */
50436 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
50437 
50438 /*
50439 ** These macros define the location of the pointer-map entry for a
50440 ** database page. The first argument to each is the number of usable
50441 ** bytes on each page of the database (often 1024). The second is the
50442 ** page number to look up in the pointer map.
50443 **
50444 ** PTRMAP_PAGENO returns the database page number of the pointer-map
50445 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
50446 ** the offset of the requested map entry.
50447 **
50448 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
50449 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
50450 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
50451 ** this test.
50452 */
50453 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
50454 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
50455 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
50456 
50457 /*
50458 ** The pointer map is a lookup table that identifies the parent page for
50459 ** each child page in the database file.  The parent page is the page that
50460 ** contains a pointer to the child.  Every page in the database contains
50461 ** 0 or 1 parent pages.  (In this context 'database page' refers
50462 ** to any page that is not part of the pointer map itself.)  Each pointer map
50463 ** entry consists of a single byte 'type' and a 4 byte parent page number.
50464 ** The PTRMAP_XXX identifiers below are the valid types.
50465 **
50466 ** The purpose of the pointer map is to facility moving pages from one
50467 ** position in the file to another as part of autovacuum.  When a page
50468 ** is moved, the pointer in its parent must be updated to point to the
50469 ** new location.  The pointer map is used to locate the parent page quickly.
50470 **
50471 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
50472 **                  used in this case.
50473 **
50474 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
50475 **                  is not used in this case.
50476 **
50477 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
50478 **                   overflow pages. The page number identifies the page that
50479 **                   contains the cell with a pointer to this overflow page.
50480 **
50481 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
50482 **                   overflow pages. The page-number identifies the previous
50483 **                   page in the overflow page list.
50484 **
50485 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
50486 **               identifies the parent page in the btree.
50487 */
50488 #define PTRMAP_ROOTPAGE 1
50489 #define PTRMAP_FREEPAGE 2
50490 #define PTRMAP_OVERFLOW1 3
50491 #define PTRMAP_OVERFLOW2 4
50492 #define PTRMAP_BTREE 5
50493 
50494 /* A bunch of assert() statements to check the transaction state variables
50495 ** of handle p (type Btree*) are internally consistent.
50496 */
50497 #define btreeIntegrity(p) \
50498   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
50499   assert( p->pBt->inTransaction>=p->inTrans );
50500 
50501 
50502 /*
50503 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
50504 ** if the database supports auto-vacuum or not. Because it is used
50505 ** within an expression that is an argument to another macro
50506 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
50507 ** So, this macro is defined instead.
50508 */
50509 #ifndef SQLITE_OMIT_AUTOVACUUM
50510 #define ISAUTOVACUUM (pBt->autoVacuum)
50511 #else
50512 #define ISAUTOVACUUM 0
50513 #endif
50514 
50515 
50516 /*
50517 ** This structure is passed around through all the sanity checking routines
50518 ** in order to keep track of some global state information.
50519 **
50520 ** The aRef[] array is allocated so that there is 1 bit for each page in
50521 ** the database. As the integrity-check proceeds, for each page used in
50522 ** the database the corresponding bit is set. This allows integrity-check to
50523 ** detect pages that are used twice and orphaned pages (both of which
50524 ** indicate corruption).
50525 */
50526 typedef struct IntegrityCk IntegrityCk;
50527 struct IntegrityCk {
50528   BtShared *pBt;    /* The tree being checked out */
50529   Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
50530   u8 *aPgRef;       /* 1 bit per page in the db (see above) */
50531   Pgno nPage;       /* Number of pages in the database */
50532   int mxErr;        /* Stop accumulating errors when this reaches zero */
50533   int nErr;         /* Number of messages written to zErrMsg so far */
50534   int mallocFailed; /* A memory allocation error has occurred */
50535   StrAccum errMsg;  /* Accumulate the error message text here */
50536 };
50537 
50538 /*
50539 ** Routines to read or write a two- and four-byte big-endian integer values.
50540 */
50541 #define get2byte(x)   ((x)[0]<<8 | (x)[1])
50542 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
50543 #define get4byte sqlite3Get4byte
50544 #define put4byte sqlite3Put4byte
50545 
50546 /************** End of btreeInt.h ********************************************/
50547 /************** Continuing where we left off in btmutex.c ********************/
50548 #ifndef SQLITE_OMIT_SHARED_CACHE
50549 #if SQLITE_THREADSAFE
50550 
50551 /*
50552 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
50553 ** set BtShared.db to the database handle associated with p and the
50554 ** p->locked boolean to true.
50555 */
50556 static void lockBtreeMutex(Btree *p){
50557   assert( p->locked==0 );
50558   assert( sqlite3_mutex_notheld(p->pBt->mutex) );
50559   assert( sqlite3_mutex_held(p->db->mutex) );
50560 
50561   sqlite3_mutex_enter(p->pBt->mutex);
50562   p->pBt->db = p->db;
50563   p->locked = 1;
50564 }
50565 
50566 /*
50567 ** Release the BtShared mutex associated with B-Tree handle p and
50568 ** clear the p->locked boolean.
50569 */
50570 static void unlockBtreeMutex(Btree *p){
50571   BtShared *pBt = p->pBt;
50572   assert( p->locked==1 );
50573   assert( sqlite3_mutex_held(pBt->mutex) );
50574   assert( sqlite3_mutex_held(p->db->mutex) );
50575   assert( p->db==pBt->db );
50576 
50577   sqlite3_mutex_leave(pBt->mutex);
50578   p->locked = 0;
50579 }
50580 
50581 /*
50582 ** Enter a mutex on the given BTree object.
50583 **
50584 ** If the object is not sharable, then no mutex is ever required
50585 ** and this routine is a no-op.  The underlying mutex is non-recursive.
50586 ** But we keep a reference count in Btree.wantToLock so the behavior
50587 ** of this interface is recursive.
50588 **
50589 ** To avoid deadlocks, multiple Btrees are locked in the same order
50590 ** by all database connections.  The p->pNext is a list of other
50591 ** Btrees belonging to the same database connection as the p Btree
50592 ** which need to be locked after p.  If we cannot get a lock on
50593 ** p, then first unlock all of the others on p->pNext, then wait
50594 ** for the lock to become available on p, then relock all of the
50595 ** subsequent Btrees that desire a lock.
50596 */
50597 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
50598   Btree *pLater;
50599 
50600   /* Some basic sanity checking on the Btree.  The list of Btrees
50601   ** connected by pNext and pPrev should be in sorted order by
50602   ** Btree.pBt value. All elements of the list should belong to
50603   ** the same connection. Only shared Btrees are on the list. */
50604   assert( p->pNext==0 || p->pNext->pBt>p->pBt );
50605   assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
50606   assert( p->pNext==0 || p->pNext->db==p->db );
50607   assert( p->pPrev==0 || p->pPrev->db==p->db );
50608   assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
50609 
50610   /* Check for locking consistency */
50611   assert( !p->locked || p->wantToLock>0 );
50612   assert( p->sharable || p->wantToLock==0 );
50613 
50614   /* We should already hold a lock on the database connection */
50615   assert( sqlite3_mutex_held(p->db->mutex) );
50616 
50617   /* Unless the database is sharable and unlocked, then BtShared.db
50618   ** should already be set correctly. */
50619   assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
50620 
50621   if( !p->sharable ) return;
50622   p->wantToLock++;
50623   if( p->locked ) return;
50624 
50625   /* In most cases, we should be able to acquire the lock we
50626   ** want without having to go throught the ascending lock
50627   ** procedure that follows.  Just be sure not to block.
50628   */
50629   if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
50630     p->pBt->db = p->db;
50631     p->locked = 1;
50632     return;
50633   }
50634 
50635   /* To avoid deadlock, first release all locks with a larger
50636   ** BtShared address.  Then acquire our lock.  Then reacquire
50637   ** the other BtShared locks that we used to hold in ascending
50638   ** order.
50639   */
50640   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
50641     assert( pLater->sharable );
50642     assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
50643     assert( !pLater->locked || pLater->wantToLock>0 );
50644     if( pLater->locked ){
50645       unlockBtreeMutex(pLater);
50646     }
50647   }
50648   lockBtreeMutex(p);
50649   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
50650     if( pLater->wantToLock ){
50651       lockBtreeMutex(pLater);
50652     }
50653   }
50654 }
50655 
50656 /*
50657 ** Exit the recursive mutex on a Btree.
50658 */
50659 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
50660   if( p->sharable ){
50661     assert( p->wantToLock>0 );
50662     p->wantToLock--;
50663     if( p->wantToLock==0 ){
50664       unlockBtreeMutex(p);
50665     }
50666   }
50667 }
50668 
50669 #ifndef NDEBUG
50670 /*
50671 ** Return true if the BtShared mutex is held on the btree, or if the
50672 ** B-Tree is not marked as sharable.
50673 **
50674 ** This routine is used only from within assert() statements.
50675 */
50676 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
50677   assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
50678   assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
50679   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
50680   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
50681 
50682   return (p->sharable==0 || p->locked);
50683 }
50684 #endif
50685 
50686 
50687 #ifndef SQLITE_OMIT_INCRBLOB
50688 /*
50689 ** Enter and leave a mutex on a Btree given a cursor owned by that
50690 ** Btree.  These entry points are used by incremental I/O and can be
50691 ** omitted if that module is not used.
50692 */
50693 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
50694   sqlite3BtreeEnter(pCur->pBtree);
50695 }
50696 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
50697   sqlite3BtreeLeave(pCur->pBtree);
50698 }
50699 #endif /* SQLITE_OMIT_INCRBLOB */
50700 
50701 
50702 /*
50703 ** Enter the mutex on every Btree associated with a database
50704 ** connection.  This is needed (for example) prior to parsing
50705 ** a statement since we will be comparing table and column names
50706 ** against all schemas and we do not want those schemas being
50707 ** reset out from under us.
50708 **
50709 ** There is a corresponding leave-all procedures.
50710 **
50711 ** Enter the mutexes in accending order by BtShared pointer address
50712 ** to avoid the possibility of deadlock when two threads with
50713 ** two or more btrees in common both try to lock all their btrees
50714 ** at the same instant.
50715 */
50716 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
50717   int i;
50718   Btree *p;
50719   assert( sqlite3_mutex_held(db->mutex) );
50720   for(i=0; i<db->nDb; i++){
50721     p = db->aDb[i].pBt;
50722     if( p ) sqlite3BtreeEnter(p);
50723   }
50724 }
50725 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
50726   int i;
50727   Btree *p;
50728   assert( sqlite3_mutex_held(db->mutex) );
50729   for(i=0; i<db->nDb; i++){
50730     p = db->aDb[i].pBt;
50731     if( p ) sqlite3BtreeLeave(p);
50732   }
50733 }
50734 
50735 /*
50736 ** Return true if a particular Btree requires a lock.  Return FALSE if
50737 ** no lock is ever required since it is not sharable.
50738 */
50739 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
50740   return p->sharable;
50741 }
50742 
50743 #ifndef NDEBUG
50744 /*
50745 ** Return true if the current thread holds the database connection
50746 ** mutex and all required BtShared mutexes.
50747 **
50748 ** This routine is used inside assert() statements only.
50749 */
50750 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
50751   int i;
50752   if( !sqlite3_mutex_held(db->mutex) ){
50753     return 0;
50754   }
50755   for(i=0; i<db->nDb; i++){
50756     Btree *p;
50757     p = db->aDb[i].pBt;
50758     if( p && p->sharable &&
50759          (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
50760       return 0;
50761     }
50762   }
50763   return 1;
50764 }
50765 #endif /* NDEBUG */
50766 
50767 #ifndef NDEBUG
50768 /*
50769 ** Return true if the correct mutexes are held for accessing the
50770 ** db->aDb[iDb].pSchema structure.  The mutexes required for schema
50771 ** access are:
50772 **
50773 **   (1) The mutex on db
50774 **   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
50775 **
50776 ** If pSchema is not NULL, then iDb is computed from pSchema and
50777 ** db using sqlite3SchemaToIndex().
50778 */
50779 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
50780   Btree *p;
50781   assert( db!=0 );
50782   if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
50783   assert( iDb>=0 && iDb<db->nDb );
50784   if( !sqlite3_mutex_held(db->mutex) ) return 0;
50785   if( iDb==1 ) return 1;
50786   p = db->aDb[iDb].pBt;
50787   assert( p!=0 );
50788   return p->sharable==0 || p->locked==1;
50789 }
50790 #endif /* NDEBUG */
50791 
50792 #else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
50793 /*
50794 ** The following are special cases for mutex enter routines for use
50795 ** in single threaded applications that use shared cache.  Except for
50796 ** these two routines, all mutex operations are no-ops in that case and
50797 ** are null #defines in btree.h.
50798 **
50799 ** If shared cache is disabled, then all btree mutex routines, including
50800 ** the ones below, are no-ops and are null #defines in btree.h.
50801 */
50802 
50803 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
50804   p->pBt->db = p->db;
50805 }
50806 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
50807   int i;
50808   for(i=0; i<db->nDb; i++){
50809     Btree *p = db->aDb[i].pBt;
50810     if( p ){
50811       p->pBt->db = p->db;
50812     }
50813   }
50814 }
50815 #endif /* if SQLITE_THREADSAFE */
50816 #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
50817 
50818 /************** End of btmutex.c *********************************************/
50819 /************** Begin file btree.c *******************************************/
50820 /*
50821 ** 2004 April 6
50822 **
50823 ** The author disclaims copyright to this source code.  In place of
50824 ** a legal notice, here is a blessing:
50825 **
50826 **    May you do good and not evil.
50827 **    May you find forgiveness for yourself and forgive others.
50828 **    May you share freely, never taking more than you give.
50829 **
50830 *************************************************************************
50831 ** This file implements a external (disk-based) database using BTrees.
50832 ** See the header comment on "btreeInt.h" for additional information.
50833 ** Including a description of file format and an overview of operation.
50834 */
50835 
50836 /*
50837 ** The header string that appears at the beginning of every
50838 ** SQLite database.
50839 */
50840 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
50841 
50842 /*
50843 ** Set this global variable to 1 to enable tracing using the TRACE
50844 ** macro.
50845 */
50846 #if 0
50847 int sqlite3BtreeTrace=1;  /* True to enable tracing */
50848 # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
50849 #else
50850 # define TRACE(X)
50851 #endif
50852 
50853 /*
50854 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
50855 ** But if the value is zero, make it 65536.
50856 **
50857 ** This routine is used to extract the "offset to cell content area" value
50858 ** from the header of a btree page.  If the page size is 65536 and the page
50859 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
50860 ** This routine makes the necessary adjustment to 65536.
50861 */
50862 #define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
50863 
50864 /*
50865 ** Values passed as the 5th argument to allocateBtreePage()
50866 */
50867 #define BTALLOC_ANY   0           /* Allocate any page */
50868 #define BTALLOC_EXACT 1           /* Allocate exact page if possible */
50869 #define BTALLOC_LE    2           /* Allocate any page <= the parameter */
50870 
50871 /*
50872 ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
50873 ** defined, or 0 if it is. For example:
50874 **
50875 **   bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
50876 */
50877 #ifndef SQLITE_OMIT_AUTOVACUUM
50878 #define IfNotOmitAV(expr) (expr)
50879 #else
50880 #define IfNotOmitAV(expr) 0
50881 #endif
50882 
50883 #ifndef SQLITE_OMIT_SHARED_CACHE
50884 /*
50885 ** A list of BtShared objects that are eligible for participation
50886 ** in shared cache.  This variable has file scope during normal builds,
50887 ** but the test harness needs to access it so we make it global for
50888 ** test builds.
50889 **
50890 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
50891 */
50892 #ifdef SQLITE_TEST
50893 SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
50894 #else
50895 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
50896 #endif
50897 #endif /* SQLITE_OMIT_SHARED_CACHE */
50898 
50899 #ifndef SQLITE_OMIT_SHARED_CACHE
50900 /*
50901 ** Enable or disable the shared pager and schema features.
50902 **
50903 ** This routine has no effect on existing database connections.
50904 ** The shared cache setting effects only future calls to
50905 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
50906 */
50907 SQLITE_API int sqlite3_enable_shared_cache(int enable){
50908   sqlite3GlobalConfig.sharedCacheEnabled = enable;
50909   return SQLITE_OK;
50910 }
50911 #endif
50912 
50913 
50914 
50915 #ifdef SQLITE_OMIT_SHARED_CACHE
50916   /*
50917   ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
50918   ** and clearAllSharedCacheTableLocks()
50919   ** manipulate entries in the BtShared.pLock linked list used to store
50920   ** shared-cache table level locks. If the library is compiled with the
50921   ** shared-cache feature disabled, then there is only ever one user
50922   ** of each BtShared structure and so this locking is not necessary.
50923   ** So define the lock related functions as no-ops.
50924   */
50925   #define querySharedCacheTableLock(a,b,c) SQLITE_OK
50926   #define setSharedCacheTableLock(a,b,c) SQLITE_OK
50927   #define clearAllSharedCacheTableLocks(a)
50928   #define downgradeAllSharedCacheTableLocks(a)
50929   #define hasSharedCacheTableLock(a,b,c,d) 1
50930   #define hasReadConflicts(a, b) 0
50931 #endif
50932 
50933 #ifndef SQLITE_OMIT_SHARED_CACHE
50934 
50935 #ifdef SQLITE_DEBUG
50936 /*
50937 **** This function is only used as part of an assert() statement. ***
50938 **
50939 ** Check to see if pBtree holds the required locks to read or write to the
50940 ** table with root page iRoot.   Return 1 if it does and 0 if not.
50941 **
50942 ** For example, when writing to a table with root-page iRoot via
50943 ** Btree connection pBtree:
50944 **
50945 **    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
50946 **
50947 ** When writing to an index that resides in a sharable database, the
50948 ** caller should have first obtained a lock specifying the root page of
50949 ** the corresponding table. This makes things a bit more complicated,
50950 ** as this module treats each table as a separate structure. To determine
50951 ** the table corresponding to the index being written, this
50952 ** function has to search through the database schema.
50953 **
50954 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
50955 ** hold a write-lock on the schema table (root page 1). This is also
50956 ** acceptable.
50957 */
50958 static int hasSharedCacheTableLock(
50959   Btree *pBtree,         /* Handle that must hold lock */
50960   Pgno iRoot,            /* Root page of b-tree */
50961   int isIndex,           /* True if iRoot is the root of an index b-tree */
50962   int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
50963 ){
50964   Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
50965   Pgno iTab = 0;
50966   BtLock *pLock;
50967 
50968   /* If this database is not shareable, or if the client is reading
50969   ** and has the read-uncommitted flag set, then no lock is required.
50970   ** Return true immediately.
50971   */
50972   if( (pBtree->sharable==0)
50973    || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
50974   ){
50975     return 1;
50976   }
50977 
50978   /* If the client is reading  or writing an index and the schema is
50979   ** not loaded, then it is too difficult to actually check to see if
50980   ** the correct locks are held.  So do not bother - just return true.
50981   ** This case does not come up very often anyhow.
50982   */
50983   if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
50984     return 1;
50985   }
50986 
50987   /* Figure out the root-page that the lock should be held on. For table
50988   ** b-trees, this is just the root page of the b-tree being read or
50989   ** written. For index b-trees, it is the root page of the associated
50990   ** table.  */
50991   if( isIndex ){
50992     HashElem *p;
50993     for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
50994       Index *pIdx = (Index *)sqliteHashData(p);
50995       if( pIdx->tnum==(int)iRoot ){
50996         iTab = pIdx->pTable->tnum;
50997       }
50998     }
50999   }else{
51000     iTab = iRoot;
51001   }
51002 
51003   /* Search for the required lock. Either a write-lock on root-page iTab, a
51004   ** write-lock on the schema table, or (if the client is reading) a
51005   ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
51006   for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
51007     if( pLock->pBtree==pBtree
51008      && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
51009      && pLock->eLock>=eLockType
51010     ){
51011       return 1;
51012     }
51013   }
51014 
51015   /* Failed to find the required lock. */
51016   return 0;
51017 }
51018 #endif /* SQLITE_DEBUG */
51019 
51020 #ifdef SQLITE_DEBUG
51021 /*
51022 **** This function may be used as part of assert() statements only. ****
51023 **
51024 ** Return true if it would be illegal for pBtree to write into the
51025 ** table or index rooted at iRoot because other shared connections are
51026 ** simultaneously reading that same table or index.
51027 **
51028 ** It is illegal for pBtree to write if some other Btree object that
51029 ** shares the same BtShared object is currently reading or writing
51030 ** the iRoot table.  Except, if the other Btree object has the
51031 ** read-uncommitted flag set, then it is OK for the other object to
51032 ** have a read cursor.
51033 **
51034 ** For example, before writing to any part of the table or index
51035 ** rooted at page iRoot, one should call:
51036 **
51037 **    assert( !hasReadConflicts(pBtree, iRoot) );
51038 */
51039 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
51040   BtCursor *p;
51041   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
51042     if( p->pgnoRoot==iRoot
51043      && p->pBtree!=pBtree
51044      && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
51045     ){
51046       return 1;
51047     }
51048   }
51049   return 0;
51050 }
51051 #endif    /* #ifdef SQLITE_DEBUG */
51052 
51053 /*
51054 ** Query to see if Btree handle p may obtain a lock of type eLock
51055 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
51056 ** SQLITE_OK if the lock may be obtained (by calling
51057 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
51058 */
51059 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
51060   BtShared *pBt = p->pBt;
51061   BtLock *pIter;
51062 
51063   assert( sqlite3BtreeHoldsMutex(p) );
51064   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
51065   assert( p->db!=0 );
51066   assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
51067 
51068   /* If requesting a write-lock, then the Btree must have an open write
51069   ** transaction on this file. And, obviously, for this to be so there
51070   ** must be an open write transaction on the file itself.
51071   */
51072   assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
51073   assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
51074 
51075   /* This routine is a no-op if the shared-cache is not enabled */
51076   if( !p->sharable ){
51077     return SQLITE_OK;
51078   }
51079 
51080   /* If some other connection is holding an exclusive lock, the
51081   ** requested lock may not be obtained.
51082   */
51083   if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
51084     sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
51085     return SQLITE_LOCKED_SHAREDCACHE;
51086   }
51087 
51088   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
51089     /* The condition (pIter->eLock!=eLock) in the following if(...)
51090     ** statement is a simplification of:
51091     **
51092     **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
51093     **
51094     ** since we know that if eLock==WRITE_LOCK, then no other connection
51095     ** may hold a WRITE_LOCK on any table in this file (since there can
51096     ** only be a single writer).
51097     */
51098     assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
51099     assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
51100     if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
51101       sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
51102       if( eLock==WRITE_LOCK ){
51103         assert( p==pBt->pWriter );
51104         pBt->btsFlags |= BTS_PENDING;
51105       }
51106       return SQLITE_LOCKED_SHAREDCACHE;
51107     }
51108   }
51109   return SQLITE_OK;
51110 }
51111 #endif /* !SQLITE_OMIT_SHARED_CACHE */
51112 
51113 #ifndef SQLITE_OMIT_SHARED_CACHE
51114 /*
51115 ** Add a lock on the table with root-page iTable to the shared-btree used
51116 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
51117 ** WRITE_LOCK.
51118 **
51119 ** This function assumes the following:
51120 **
51121 **   (a) The specified Btree object p is connected to a sharable
51122 **       database (one with the BtShared.sharable flag set), and
51123 **
51124 **   (b) No other Btree objects hold a lock that conflicts
51125 **       with the requested lock (i.e. querySharedCacheTableLock() has
51126 **       already been called and returned SQLITE_OK).
51127 **
51128 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
51129 ** is returned if a malloc attempt fails.
51130 */
51131 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
51132   BtShared *pBt = p->pBt;
51133   BtLock *pLock = 0;
51134   BtLock *pIter;
51135 
51136   assert( sqlite3BtreeHoldsMutex(p) );
51137   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
51138   assert( p->db!=0 );
51139 
51140   /* A connection with the read-uncommitted flag set will never try to
51141   ** obtain a read-lock using this function. The only read-lock obtained
51142   ** by a connection in read-uncommitted mode is on the sqlite_master
51143   ** table, and that lock is obtained in BtreeBeginTrans().  */
51144   assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
51145 
51146   /* This function should only be called on a sharable b-tree after it
51147   ** has been determined that no other b-tree holds a conflicting lock.  */
51148   assert( p->sharable );
51149   assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
51150 
51151   /* First search the list for an existing lock on this table. */
51152   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
51153     if( pIter->iTable==iTable && pIter->pBtree==p ){
51154       pLock = pIter;
51155       break;
51156     }
51157   }
51158 
51159   /* If the above search did not find a BtLock struct associating Btree p
51160   ** with table iTable, allocate one and link it into the list.
51161   */
51162   if( !pLock ){
51163     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
51164     if( !pLock ){
51165       return SQLITE_NOMEM;
51166     }
51167     pLock->iTable = iTable;
51168     pLock->pBtree = p;
51169     pLock->pNext = pBt->pLock;
51170     pBt->pLock = pLock;
51171   }
51172 
51173   /* Set the BtLock.eLock variable to the maximum of the current lock
51174   ** and the requested lock. This means if a write-lock was already held
51175   ** and a read-lock requested, we don't incorrectly downgrade the lock.
51176   */
51177   assert( WRITE_LOCK>READ_LOCK );
51178   if( eLock>pLock->eLock ){
51179     pLock->eLock = eLock;
51180   }
51181 
51182   return SQLITE_OK;
51183 }
51184 #endif /* !SQLITE_OMIT_SHARED_CACHE */
51185 
51186 #ifndef SQLITE_OMIT_SHARED_CACHE
51187 /*
51188 ** Release all the table locks (locks obtained via calls to
51189 ** the setSharedCacheTableLock() procedure) held by Btree object p.
51190 **
51191 ** This function assumes that Btree p has an open read or write
51192 ** transaction. If it does not, then the BTS_PENDING flag
51193 ** may be incorrectly cleared.
51194 */
51195 static void clearAllSharedCacheTableLocks(Btree *p){
51196   BtShared *pBt = p->pBt;
51197   BtLock **ppIter = &pBt->pLock;
51198 
51199   assert( sqlite3BtreeHoldsMutex(p) );
51200   assert( p->sharable || 0==*ppIter );
51201   assert( p->inTrans>0 );
51202 
51203   while( *ppIter ){
51204     BtLock *pLock = *ppIter;
51205     assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
51206     assert( pLock->pBtree->inTrans>=pLock->eLock );
51207     if( pLock->pBtree==p ){
51208       *ppIter = pLock->pNext;
51209       assert( pLock->iTable!=1 || pLock==&p->lock );
51210       if( pLock->iTable!=1 ){
51211         sqlite3_free(pLock);
51212       }
51213     }else{
51214       ppIter = &pLock->pNext;
51215     }
51216   }
51217 
51218   assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
51219   if( pBt->pWriter==p ){
51220     pBt->pWriter = 0;
51221     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
51222   }else if( pBt->nTransaction==2 ){
51223     /* This function is called when Btree p is concluding its
51224     ** transaction. If there currently exists a writer, and p is not
51225     ** that writer, then the number of locks held by connections other
51226     ** than the writer must be about to drop to zero. In this case
51227     ** set the BTS_PENDING flag to 0.
51228     **
51229     ** If there is not currently a writer, then BTS_PENDING must
51230     ** be zero already. So this next line is harmless in that case.
51231     */
51232     pBt->btsFlags &= ~BTS_PENDING;
51233   }
51234 }
51235 
51236 /*
51237 ** This function changes all write-locks held by Btree p into read-locks.
51238 */
51239 static void downgradeAllSharedCacheTableLocks(Btree *p){
51240   BtShared *pBt = p->pBt;
51241   if( pBt->pWriter==p ){
51242     BtLock *pLock;
51243     pBt->pWriter = 0;
51244     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
51245     for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
51246       assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
51247       pLock->eLock = READ_LOCK;
51248     }
51249   }
51250 }
51251 
51252 #endif /* SQLITE_OMIT_SHARED_CACHE */
51253 
51254 static void releasePage(MemPage *pPage);  /* Forward reference */
51255 
51256 /*
51257 ***** This routine is used inside of assert() only ****
51258 **
51259 ** Verify that the cursor holds the mutex on its BtShared
51260 */
51261 #ifdef SQLITE_DEBUG
51262 static int cursorHoldsMutex(BtCursor *p){
51263   return sqlite3_mutex_held(p->pBt->mutex);
51264 }
51265 #endif
51266 
51267 
51268 #ifndef SQLITE_OMIT_INCRBLOB
51269 /*
51270 ** Invalidate the overflow page-list cache for cursor pCur, if any.
51271 */
51272 static void invalidateOverflowCache(BtCursor *pCur){
51273   assert( cursorHoldsMutex(pCur) );
51274   sqlite3_free(pCur->aOverflow);
51275   pCur->aOverflow = 0;
51276 }
51277 
51278 /*
51279 ** Invalidate the overflow page-list cache for all cursors opened
51280 ** on the shared btree structure pBt.
51281 */
51282 static void invalidateAllOverflowCache(BtShared *pBt){
51283   BtCursor *p;
51284   assert( sqlite3_mutex_held(pBt->mutex) );
51285   for(p=pBt->pCursor; p; p=p->pNext){
51286     invalidateOverflowCache(p);
51287   }
51288 }
51289 
51290 /*
51291 ** This function is called before modifying the contents of a table
51292 ** to invalidate any incrblob cursors that are open on the
51293 ** row or one of the rows being modified.
51294 **
51295 ** If argument isClearTable is true, then the entire contents of the
51296 ** table is about to be deleted. In this case invalidate all incrblob
51297 ** cursors open on any row within the table with root-page pgnoRoot.
51298 **
51299 ** Otherwise, if argument isClearTable is false, then the row with
51300 ** rowid iRow is being replaced or deleted. In this case invalidate
51301 ** only those incrblob cursors open on that specific row.
51302 */
51303 static void invalidateIncrblobCursors(
51304   Btree *pBtree,          /* The database file to check */
51305   i64 iRow,               /* The rowid that might be changing */
51306   int isClearTable        /* True if all rows are being deleted */
51307 ){
51308   BtCursor *p;
51309   BtShared *pBt = pBtree->pBt;
51310   assert( sqlite3BtreeHoldsMutex(pBtree) );
51311   for(p=pBt->pCursor; p; p=p->pNext){
51312     if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
51313       p->eState = CURSOR_INVALID;
51314     }
51315   }
51316 }
51317 
51318 #else
51319   /* Stub functions when INCRBLOB is omitted */
51320   #define invalidateOverflowCache(x)
51321   #define invalidateAllOverflowCache(x)
51322   #define invalidateIncrblobCursors(x,y,z)
51323 #endif /* SQLITE_OMIT_INCRBLOB */
51324 
51325 /*
51326 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
51327 ** when a page that previously contained data becomes a free-list leaf
51328 ** page.
51329 **
51330 ** The BtShared.pHasContent bitvec exists to work around an obscure
51331 ** bug caused by the interaction of two useful IO optimizations surrounding
51332 ** free-list leaf pages:
51333 **
51334 **   1) When all data is deleted from a page and the page becomes
51335 **      a free-list leaf page, the page is not written to the database
51336 **      (as free-list leaf pages contain no meaningful data). Sometimes
51337 **      such a page is not even journalled (as it will not be modified,
51338 **      why bother journalling it?).
51339 **
51340 **   2) When a free-list leaf page is reused, its content is not read
51341 **      from the database or written to the journal file (why should it
51342 **      be, if it is not at all meaningful?).
51343 **
51344 ** By themselves, these optimizations work fine and provide a handy
51345 ** performance boost to bulk delete or insert operations. However, if
51346 ** a page is moved to the free-list and then reused within the same
51347 ** transaction, a problem comes up. If the page is not journalled when
51348 ** it is moved to the free-list and it is also not journalled when it
51349 ** is extracted from the free-list and reused, then the original data
51350 ** may be lost. In the event of a rollback, it may not be possible
51351 ** to restore the database to its original configuration.
51352 **
51353 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
51354 ** moved to become a free-list leaf page, the corresponding bit is
51355 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
51356 ** optimization 2 above is omitted if the corresponding bit is already
51357 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
51358 ** at the end of every transaction.
51359 */
51360 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
51361   int rc = SQLITE_OK;
51362   if( !pBt->pHasContent ){
51363     assert( pgno<=pBt->nPage );
51364     pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
51365     if( !pBt->pHasContent ){
51366       rc = SQLITE_NOMEM;
51367     }
51368   }
51369   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
51370     rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
51371   }
51372   return rc;
51373 }
51374 
51375 /*
51376 ** Query the BtShared.pHasContent vector.
51377 **
51378 ** This function is called when a free-list leaf page is removed from the
51379 ** free-list for reuse. It returns false if it is safe to retrieve the
51380 ** page from the pager layer with the 'no-content' flag set. True otherwise.
51381 */
51382 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
51383   Bitvec *p = pBt->pHasContent;
51384   return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
51385 }
51386 
51387 /*
51388 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
51389 ** invoked at the conclusion of each write-transaction.
51390 */
51391 static void btreeClearHasContent(BtShared *pBt){
51392   sqlite3BitvecDestroy(pBt->pHasContent);
51393   pBt->pHasContent = 0;
51394 }
51395 
51396 /*
51397 ** Release all of the apPage[] pages for a cursor.
51398 */
51399 static void btreeReleaseAllCursorPages(BtCursor *pCur){
51400   int i;
51401   for(i=0; i<=pCur->iPage; i++){
51402     releasePage(pCur->apPage[i]);
51403     pCur->apPage[i] = 0;
51404   }
51405   pCur->iPage = -1;
51406 }
51407 
51408 
51409 /*
51410 ** Save the current cursor position in the variables BtCursor.nKey
51411 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
51412 **
51413 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
51414 ** prior to calling this routine.
51415 */
51416 static int saveCursorPosition(BtCursor *pCur){
51417   int rc;
51418 
51419   assert( CURSOR_VALID==pCur->eState );
51420   assert( 0==pCur->pKey );
51421   assert( cursorHoldsMutex(pCur) );
51422 
51423   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
51424   assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
51425 
51426   /* If this is an intKey table, then the above call to BtreeKeySize()
51427   ** stores the integer key in pCur->nKey. In this case this value is
51428   ** all that is required. Otherwise, if pCur is not open on an intKey
51429   ** table, then malloc space for and store the pCur->nKey bytes of key
51430   ** data.
51431   */
51432   if( 0==pCur->apPage[0]->intKey ){
51433     void *pKey = sqlite3Malloc( (int)pCur->nKey );
51434     if( pKey ){
51435       rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
51436       if( rc==SQLITE_OK ){
51437         pCur->pKey = pKey;
51438       }else{
51439         sqlite3_free(pKey);
51440       }
51441     }else{
51442       rc = SQLITE_NOMEM;
51443     }
51444   }
51445   assert( !pCur->apPage[0]->intKey || !pCur->pKey );
51446 
51447   if( rc==SQLITE_OK ){
51448     btreeReleaseAllCursorPages(pCur);
51449     pCur->eState = CURSOR_REQUIRESEEK;
51450   }
51451 
51452   invalidateOverflowCache(pCur);
51453   return rc;
51454 }
51455 
51456 /*
51457 ** Save the positions of all cursors (except pExcept) that are open on
51458 ** the table  with root-page iRoot. Usually, this is called just before cursor
51459 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
51460 */
51461 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
51462   BtCursor *p;
51463   assert( sqlite3_mutex_held(pBt->mutex) );
51464   assert( pExcept==0 || pExcept->pBt==pBt );
51465   for(p=pBt->pCursor; p; p=p->pNext){
51466     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
51467       if( p->eState==CURSOR_VALID ){
51468         int rc = saveCursorPosition(p);
51469         if( SQLITE_OK!=rc ){
51470           return rc;
51471         }
51472       }else{
51473         testcase( p->iPage>0 );
51474         btreeReleaseAllCursorPages(p);
51475       }
51476     }
51477   }
51478   return SQLITE_OK;
51479 }
51480 
51481 /*
51482 ** Clear the current cursor position.
51483 */
51484 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
51485   assert( cursorHoldsMutex(pCur) );
51486   sqlite3_free(pCur->pKey);
51487   pCur->pKey = 0;
51488   pCur->eState = CURSOR_INVALID;
51489 }
51490 
51491 /*
51492 ** In this version of BtreeMoveto, pKey is a packed index record
51493 ** such as is generated by the OP_MakeRecord opcode.  Unpack the
51494 ** record and then call BtreeMovetoUnpacked() to do the work.
51495 */
51496 static int btreeMoveto(
51497   BtCursor *pCur,     /* Cursor open on the btree to be searched */
51498   const void *pKey,   /* Packed key if the btree is an index */
51499   i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
51500   int bias,           /* Bias search to the high end */
51501   int *pRes           /* Write search results here */
51502 ){
51503   int rc;                    /* Status code */
51504   UnpackedRecord *pIdxKey;   /* Unpacked index key */
51505   char aSpace[200];          /* Temp space for pIdxKey - to avoid a malloc */
51506   char *pFree = 0;
51507 
51508   if( pKey ){
51509     assert( nKey==(i64)(int)nKey );
51510     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
51511         pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
51512     );
51513     if( pIdxKey==0 ) return SQLITE_NOMEM;
51514     sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
51515     if( pIdxKey->nField==0 ){
51516       sqlite3DbFree(pCur->pKeyInfo->db, pFree);
51517       return SQLITE_CORRUPT_BKPT;
51518     }
51519   }else{
51520     pIdxKey = 0;
51521   }
51522   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
51523   if( pFree ){
51524     sqlite3DbFree(pCur->pKeyInfo->db, pFree);
51525   }
51526   return rc;
51527 }
51528 
51529 /*
51530 ** Restore the cursor to the position it was in (or as close to as possible)
51531 ** when saveCursorPosition() was called. Note that this call deletes the
51532 ** saved position info stored by saveCursorPosition(), so there can be
51533 ** at most one effective restoreCursorPosition() call after each
51534 ** saveCursorPosition().
51535 */
51536 static int btreeRestoreCursorPosition(BtCursor *pCur){
51537   int rc;
51538   assert( cursorHoldsMutex(pCur) );
51539   assert( pCur->eState>=CURSOR_REQUIRESEEK );
51540   if( pCur->eState==CURSOR_FAULT ){
51541     return pCur->skipNext;
51542   }
51543   pCur->eState = CURSOR_INVALID;
51544   rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
51545   if( rc==SQLITE_OK ){
51546     sqlite3_free(pCur->pKey);
51547     pCur->pKey = 0;
51548     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
51549     if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
51550       pCur->eState = CURSOR_SKIPNEXT;
51551     }
51552   }
51553   return rc;
51554 }
51555 
51556 #define restoreCursorPosition(p) \
51557   (p->eState>=CURSOR_REQUIRESEEK ? \
51558          btreeRestoreCursorPosition(p) : \
51559          SQLITE_OK)
51560 
51561 /*
51562 ** Determine whether or not a cursor has moved from the position it
51563 ** was last placed at.  Cursors can move when the row they are pointing
51564 ** at is deleted out from under them.
51565 **
51566 ** This routine returns an error code if something goes wrong.  The
51567 ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
51568 */
51569 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
51570   int rc;
51571 
51572   rc = restoreCursorPosition(pCur);
51573   if( rc ){
51574     *pHasMoved = 1;
51575     return rc;
51576   }
51577   if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
51578     *pHasMoved = 1;
51579   }else{
51580     *pHasMoved = 0;
51581   }
51582   return SQLITE_OK;
51583 }
51584 
51585 #ifndef SQLITE_OMIT_AUTOVACUUM
51586 /*
51587 ** Given a page number of a regular database page, return the page
51588 ** number for the pointer-map page that contains the entry for the
51589 ** input page number.
51590 **
51591 ** Return 0 (not a valid page) for pgno==1 since there is
51592 ** no pointer map associated with page 1.  The integrity_check logic
51593 ** requires that ptrmapPageno(*,1)!=1.
51594 */
51595 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
51596   int nPagesPerMapPage;
51597   Pgno iPtrMap, ret;
51598   assert( sqlite3_mutex_held(pBt->mutex) );
51599   if( pgno<2 ) return 0;
51600   nPagesPerMapPage = (pBt->usableSize/5)+1;
51601   iPtrMap = (pgno-2)/nPagesPerMapPage;
51602   ret = (iPtrMap*nPagesPerMapPage) + 2;
51603   if( ret==PENDING_BYTE_PAGE(pBt) ){
51604     ret++;
51605   }
51606   return ret;
51607 }
51608 
51609 /*
51610 ** Write an entry into the pointer map.
51611 **
51612 ** This routine updates the pointer map entry for page number 'key'
51613 ** so that it maps to type 'eType' and parent page number 'pgno'.
51614 **
51615 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
51616 ** a no-op.  If an error occurs, the appropriate error code is written
51617 ** into *pRC.
51618 */
51619 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
51620   DbPage *pDbPage;  /* The pointer map page */
51621   u8 *pPtrmap;      /* The pointer map data */
51622   Pgno iPtrmap;     /* The pointer map page number */
51623   int offset;       /* Offset in pointer map page */
51624   int rc;           /* Return code from subfunctions */
51625 
51626   if( *pRC ) return;
51627 
51628   assert( sqlite3_mutex_held(pBt->mutex) );
51629   /* The master-journal page number must never be used as a pointer map page */
51630   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
51631 
51632   assert( pBt->autoVacuum );
51633   if( key==0 ){
51634     *pRC = SQLITE_CORRUPT_BKPT;
51635     return;
51636   }
51637   iPtrmap = PTRMAP_PAGENO(pBt, key);
51638   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
51639   if( rc!=SQLITE_OK ){
51640     *pRC = rc;
51641     return;
51642   }
51643   offset = PTRMAP_PTROFFSET(iPtrmap, key);
51644   if( offset<0 ){
51645     *pRC = SQLITE_CORRUPT_BKPT;
51646     goto ptrmap_exit;
51647   }
51648   assert( offset <= (int)pBt->usableSize-5 );
51649   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
51650 
51651   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
51652     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
51653     *pRC= rc = sqlite3PagerWrite(pDbPage);
51654     if( rc==SQLITE_OK ){
51655       pPtrmap[offset] = eType;
51656       put4byte(&pPtrmap[offset+1], parent);
51657     }
51658   }
51659 
51660 ptrmap_exit:
51661   sqlite3PagerUnref(pDbPage);
51662 }
51663 
51664 /*
51665 ** Read an entry from the pointer map.
51666 **
51667 ** This routine retrieves the pointer map entry for page 'key', writing
51668 ** the type and parent page number to *pEType and *pPgno respectively.
51669 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
51670 */
51671 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
51672   DbPage *pDbPage;   /* The pointer map page */
51673   int iPtrmap;       /* Pointer map page index */
51674   u8 *pPtrmap;       /* Pointer map page data */
51675   int offset;        /* Offset of entry in pointer map */
51676   int rc;
51677 
51678   assert( sqlite3_mutex_held(pBt->mutex) );
51679 
51680   iPtrmap = PTRMAP_PAGENO(pBt, key);
51681   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
51682   if( rc!=0 ){
51683     return rc;
51684   }
51685   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
51686 
51687   offset = PTRMAP_PTROFFSET(iPtrmap, key);
51688   if( offset<0 ){
51689     sqlite3PagerUnref(pDbPage);
51690     return SQLITE_CORRUPT_BKPT;
51691   }
51692   assert( offset <= (int)pBt->usableSize-5 );
51693   assert( pEType!=0 );
51694   *pEType = pPtrmap[offset];
51695   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
51696 
51697   sqlite3PagerUnref(pDbPage);
51698   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
51699   return SQLITE_OK;
51700 }
51701 
51702 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
51703   #define ptrmapPut(w,x,y,z,rc)
51704   #define ptrmapGet(w,x,y,z) SQLITE_OK
51705   #define ptrmapPutOvflPtr(x, y, rc)
51706 #endif
51707 
51708 /*
51709 ** Given a btree page and a cell index (0 means the first cell on
51710 ** the page, 1 means the second cell, and so forth) return a pointer
51711 ** to the cell content.
51712 **
51713 ** This routine works only for pages that do not contain overflow cells.
51714 */
51715 #define findCell(P,I) \
51716   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
51717 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
51718 
51719 
51720 /*
51721 ** This a more complex version of findCell() that works for
51722 ** pages that do contain overflow cells.
51723 */
51724 static u8 *findOverflowCell(MemPage *pPage, int iCell){
51725   int i;
51726   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
51727   for(i=pPage->nOverflow-1; i>=0; i--){
51728     int k;
51729     k = pPage->aiOvfl[i];
51730     if( k<=iCell ){
51731       if( k==iCell ){
51732         return pPage->apOvfl[i];
51733       }
51734       iCell--;
51735     }
51736   }
51737   return findCell(pPage, iCell);
51738 }
51739 
51740 /*
51741 ** Parse a cell content block and fill in the CellInfo structure.  There
51742 ** are two versions of this function.  btreeParseCell() takes a
51743 ** cell index as the second argument and btreeParseCellPtr()
51744 ** takes a pointer to the body of the cell as its second argument.
51745 **
51746 ** Within this file, the parseCell() macro can be called instead of
51747 ** btreeParseCellPtr(). Using some compilers, this will be faster.
51748 */
51749 static void btreeParseCellPtr(
51750   MemPage *pPage,         /* Page containing the cell */
51751   u8 *pCell,              /* Pointer to the cell text. */
51752   CellInfo *pInfo         /* Fill in this structure */
51753 ){
51754   u16 n;                  /* Number bytes in cell content header */
51755   u32 nPayload;           /* Number of bytes of cell payload */
51756 
51757   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
51758 
51759   pInfo->pCell = pCell;
51760   assert( pPage->leaf==0 || pPage->leaf==1 );
51761   n = pPage->childPtrSize;
51762   assert( n==4-4*pPage->leaf );
51763   if( pPage->intKey ){
51764     if( pPage->hasData ){
51765       assert( n==0 );
51766       n = getVarint32(pCell, nPayload);
51767     }else{
51768       nPayload = 0;
51769     }
51770     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
51771     pInfo->nData = nPayload;
51772   }else{
51773     pInfo->nData = 0;
51774     n += getVarint32(&pCell[n], nPayload);
51775     pInfo->nKey = nPayload;
51776   }
51777   pInfo->nPayload = nPayload;
51778   pInfo->nHeader = n;
51779   testcase( nPayload==pPage->maxLocal );
51780   testcase( nPayload==pPage->maxLocal+1 );
51781   if( likely(nPayload<=pPage->maxLocal) ){
51782     /* This is the (easy) common case where the entire payload fits
51783     ** on the local page.  No overflow is required.
51784     */
51785     if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
51786     pInfo->nLocal = (u16)nPayload;
51787     pInfo->iOverflow = 0;
51788   }else{
51789     /* If the payload will not fit completely on the local page, we have
51790     ** to decide how much to store locally and how much to spill onto
51791     ** overflow pages.  The strategy is to minimize the amount of unused
51792     ** space on overflow pages while keeping the amount of local storage
51793     ** in between minLocal and maxLocal.
51794     **
51795     ** Warning:  changing the way overflow payload is distributed in any
51796     ** way will result in an incompatible file format.
51797     */
51798     int minLocal;  /* Minimum amount of payload held locally */
51799     int maxLocal;  /* Maximum amount of payload held locally */
51800     int surplus;   /* Overflow payload available for local storage */
51801 
51802     minLocal = pPage->minLocal;
51803     maxLocal = pPage->maxLocal;
51804     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
51805     testcase( surplus==maxLocal );
51806     testcase( surplus==maxLocal+1 );
51807     if( surplus <= maxLocal ){
51808       pInfo->nLocal = (u16)surplus;
51809     }else{
51810       pInfo->nLocal = (u16)minLocal;
51811     }
51812     pInfo->iOverflow = (u16)(pInfo->nLocal + n);
51813     pInfo->nSize = pInfo->iOverflow + 4;
51814   }
51815 }
51816 #define parseCell(pPage, iCell, pInfo) \
51817   btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
51818 static void btreeParseCell(
51819   MemPage *pPage,         /* Page containing the cell */
51820   int iCell,              /* The cell index.  First cell is 0 */
51821   CellInfo *pInfo         /* Fill in this structure */
51822 ){
51823   parseCell(pPage, iCell, pInfo);
51824 }
51825 
51826 /*
51827 ** Compute the total number of bytes that a Cell needs in the cell
51828 ** data area of the btree-page.  The return number includes the cell
51829 ** data header and the local payload, but not any overflow page or
51830 ** the space used by the cell pointer.
51831 */
51832 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
51833   u8 *pIter = &pCell[pPage->childPtrSize];
51834   u32 nSize;
51835 
51836 #ifdef SQLITE_DEBUG
51837   /* The value returned by this function should always be the same as
51838   ** the (CellInfo.nSize) value found by doing a full parse of the
51839   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
51840   ** this function verifies that this invariant is not violated. */
51841   CellInfo debuginfo;
51842   btreeParseCellPtr(pPage, pCell, &debuginfo);
51843 #endif
51844 
51845   if( pPage->intKey ){
51846     u8 *pEnd;
51847     if( pPage->hasData ){
51848       pIter += getVarint32(pIter, nSize);
51849     }else{
51850       nSize = 0;
51851     }
51852 
51853     /* pIter now points at the 64-bit integer key value, a variable length
51854     ** integer. The following block moves pIter to point at the first byte
51855     ** past the end of the key value. */
51856     pEnd = &pIter[9];
51857     while( (*pIter++)&0x80 && pIter<pEnd );
51858   }else{
51859     pIter += getVarint32(pIter, nSize);
51860   }
51861 
51862   testcase( nSize==pPage->maxLocal );
51863   testcase( nSize==pPage->maxLocal+1 );
51864   if( nSize>pPage->maxLocal ){
51865     int minLocal = pPage->minLocal;
51866     nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
51867     testcase( nSize==pPage->maxLocal );
51868     testcase( nSize==pPage->maxLocal+1 );
51869     if( nSize>pPage->maxLocal ){
51870       nSize = minLocal;
51871     }
51872     nSize += 4;
51873   }
51874   nSize += (u32)(pIter - pCell);
51875 
51876   /* The minimum size of any cell is 4 bytes. */
51877   if( nSize<4 ){
51878     nSize = 4;
51879   }
51880 
51881   assert( nSize==debuginfo.nSize );
51882   return (u16)nSize;
51883 }
51884 
51885 #ifdef SQLITE_DEBUG
51886 /* This variation on cellSizePtr() is used inside of assert() statements
51887 ** only. */
51888 static u16 cellSize(MemPage *pPage, int iCell){
51889   return cellSizePtr(pPage, findCell(pPage, iCell));
51890 }
51891 #endif
51892 
51893 #ifndef SQLITE_OMIT_AUTOVACUUM
51894 /*
51895 ** If the cell pCell, part of page pPage contains a pointer
51896 ** to an overflow page, insert an entry into the pointer-map
51897 ** for the overflow page.
51898 */
51899 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
51900   CellInfo info;
51901   if( *pRC ) return;
51902   assert( pCell!=0 );
51903   btreeParseCellPtr(pPage, pCell, &info);
51904   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
51905   if( info.iOverflow ){
51906     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
51907     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
51908   }
51909 }
51910 #endif
51911 
51912 
51913 /*
51914 ** Defragment the page given.  All Cells are moved to the
51915 ** end of the page and all free space is collected into one
51916 ** big FreeBlk that occurs in between the header and cell
51917 ** pointer array and the cell content area.
51918 */
51919 static int defragmentPage(MemPage *pPage){
51920   int i;                     /* Loop counter */
51921   int pc;                    /* Address of a i-th cell */
51922   int hdr;                   /* Offset to the page header */
51923   int size;                  /* Size of a cell */
51924   int usableSize;            /* Number of usable bytes on a page */
51925   int cellOffset;            /* Offset to the cell pointer array */
51926   int cbrk;                  /* Offset to the cell content area */
51927   int nCell;                 /* Number of cells on the page */
51928   unsigned char *data;       /* The page data */
51929   unsigned char *temp;       /* Temp area for cell content */
51930   int iCellFirst;            /* First allowable cell index */
51931   int iCellLast;             /* Last possible cell index */
51932 
51933 
51934   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
51935   assert( pPage->pBt!=0 );
51936   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
51937   assert( pPage->nOverflow==0 );
51938   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
51939   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
51940   data = pPage->aData;
51941   hdr = pPage->hdrOffset;
51942   cellOffset = pPage->cellOffset;
51943   nCell = pPage->nCell;
51944   assert( nCell==get2byte(&data[hdr+3]) );
51945   usableSize = pPage->pBt->usableSize;
51946   cbrk = get2byte(&data[hdr+5]);
51947   memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
51948   cbrk = usableSize;
51949   iCellFirst = cellOffset + 2*nCell;
51950   iCellLast = usableSize - 4;
51951   for(i=0; i<nCell; i++){
51952     u8 *pAddr;     /* The i-th cell pointer */
51953     pAddr = &data[cellOffset + i*2];
51954     pc = get2byte(pAddr);
51955     testcase( pc==iCellFirst );
51956     testcase( pc==iCellLast );
51957 #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
51958     /* These conditions have already been verified in btreeInitPage()
51959     ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
51960     */
51961     if( pc<iCellFirst || pc>iCellLast ){
51962       return SQLITE_CORRUPT_BKPT;
51963     }
51964 #endif
51965     assert( pc>=iCellFirst && pc<=iCellLast );
51966     size = cellSizePtr(pPage, &temp[pc]);
51967     cbrk -= size;
51968 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
51969     if( cbrk<iCellFirst ){
51970       return SQLITE_CORRUPT_BKPT;
51971     }
51972 #else
51973     if( cbrk<iCellFirst || pc+size>usableSize ){
51974       return SQLITE_CORRUPT_BKPT;
51975     }
51976 #endif
51977     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
51978     testcase( cbrk+size==usableSize );
51979     testcase( pc+size==usableSize );
51980     memcpy(&data[cbrk], &temp[pc], size);
51981     put2byte(pAddr, cbrk);
51982   }
51983   assert( cbrk>=iCellFirst );
51984   put2byte(&data[hdr+5], cbrk);
51985   data[hdr+1] = 0;
51986   data[hdr+2] = 0;
51987   data[hdr+7] = 0;
51988   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
51989   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
51990   if( cbrk-iCellFirst!=pPage->nFree ){
51991     return SQLITE_CORRUPT_BKPT;
51992   }
51993   return SQLITE_OK;
51994 }
51995 
51996 /*
51997 ** Allocate nByte bytes of space from within the B-Tree page passed
51998 ** as the first argument. Write into *pIdx the index into pPage->aData[]
51999 ** of the first byte of allocated space. Return either SQLITE_OK or
52000 ** an error code (usually SQLITE_CORRUPT).
52001 **
52002 ** The caller guarantees that there is sufficient space to make the
52003 ** allocation.  This routine might need to defragment in order to bring
52004 ** all the space together, however.  This routine will avoid using
52005 ** the first two bytes past the cell pointer area since presumably this
52006 ** allocation is being made in order to insert a new cell, so we will
52007 ** also end up needing a new cell pointer.
52008 */
52009 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
52010   const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
52011   u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
52012   int nFrag;                           /* Number of fragmented bytes on pPage */
52013   int top;                             /* First byte of cell content area */
52014   int gap;        /* First byte of gap between cell pointers and cell content */
52015   int rc;         /* Integer return code */
52016   int usableSize; /* Usable size of the page */
52017 
52018   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52019   assert( pPage->pBt );
52020   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52021   assert( nByte>=0 );  /* Minimum cell size is 4 */
52022   assert( pPage->nFree>=nByte );
52023   assert( pPage->nOverflow==0 );
52024   usableSize = pPage->pBt->usableSize;
52025   assert( nByte < usableSize-8 );
52026 
52027   nFrag = data[hdr+7];
52028   assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
52029   gap = pPage->cellOffset + 2*pPage->nCell;
52030   top = get2byteNotZero(&data[hdr+5]);
52031   if( gap>top ) return SQLITE_CORRUPT_BKPT;
52032   testcase( gap+2==top );
52033   testcase( gap+1==top );
52034   testcase( gap==top );
52035 
52036   if( nFrag>=60 ){
52037     /* Always defragment highly fragmented pages */
52038     rc = defragmentPage(pPage);
52039     if( rc ) return rc;
52040     top = get2byteNotZero(&data[hdr+5]);
52041   }else if( gap+2<=top ){
52042     /* Search the freelist looking for a free slot big enough to satisfy
52043     ** the request. The allocation is made from the first free slot in
52044     ** the list that is large enough to accommodate it.
52045     */
52046     int pc, addr;
52047     for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
52048       int size;            /* Size of the free slot */
52049       if( pc>usableSize-4 || pc<addr+4 ){
52050         return SQLITE_CORRUPT_BKPT;
52051       }
52052       size = get2byte(&data[pc+2]);
52053       if( size>=nByte ){
52054         int x = size - nByte;
52055         testcase( x==4 );
52056         testcase( x==3 );
52057         if( x<4 ){
52058           /* Remove the slot from the free-list. Update the number of
52059           ** fragmented bytes within the page. */
52060           memcpy(&data[addr], &data[pc], 2);
52061           data[hdr+7] = (u8)(nFrag + x);
52062         }else if( size+pc > usableSize ){
52063           return SQLITE_CORRUPT_BKPT;
52064         }else{
52065           /* The slot remains on the free-list. Reduce its size to account
52066           ** for the portion used by the new allocation. */
52067           put2byte(&data[pc+2], x);
52068         }
52069         *pIdx = pc + x;
52070         return SQLITE_OK;
52071       }
52072     }
52073   }
52074 
52075   /* Check to make sure there is enough space in the gap to satisfy
52076   ** the allocation.  If not, defragment.
52077   */
52078   testcase( gap+2+nByte==top );
52079   if( gap+2+nByte>top ){
52080     rc = defragmentPage(pPage);
52081     if( rc ) return rc;
52082     top = get2byteNotZero(&data[hdr+5]);
52083     assert( gap+nByte<=top );
52084   }
52085 
52086 
52087   /* Allocate memory from the gap in between the cell pointer array
52088   ** and the cell content area.  The btreeInitPage() call has already
52089   ** validated the freelist.  Given that the freelist is valid, there
52090   ** is no way that the allocation can extend off the end of the page.
52091   ** The assert() below verifies the previous sentence.
52092   */
52093   top -= nByte;
52094   put2byte(&data[hdr+5], top);
52095   assert( top+nByte <= (int)pPage->pBt->usableSize );
52096   *pIdx = top;
52097   return SQLITE_OK;
52098 }
52099 
52100 /*
52101 ** Return a section of the pPage->aData to the freelist.
52102 ** The first byte of the new free block is pPage->aDisk[start]
52103 ** and the size of the block is "size" bytes.
52104 **
52105 ** Most of the effort here is involved in coalesing adjacent
52106 ** free blocks into a single big free block.
52107 */
52108 static int freeSpace(MemPage *pPage, int start, int size){
52109   int addr, pbegin, hdr;
52110   int iLast;                        /* Largest possible freeblock offset */
52111   unsigned char *data = pPage->aData;
52112 
52113   assert( pPage->pBt!=0 );
52114   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52115   assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
52116   assert( (start + size) <= (int)pPage->pBt->usableSize );
52117   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52118   assert( size>=0 );   /* Minimum cell size is 4 */
52119 
52120   if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
52121     /* Overwrite deleted information with zeros when the secure_delete
52122     ** option is enabled */
52123     memset(&data[start], 0, size);
52124   }
52125 
52126   /* Add the space back into the linked list of freeblocks.  Note that
52127   ** even though the freeblock list was checked by btreeInitPage(),
52128   ** btreeInitPage() did not detect overlapping cells or
52129   ** freeblocks that overlapped cells.   Nor does it detect when the
52130   ** cell content area exceeds the value in the page header.  If these
52131   ** situations arise, then subsequent insert operations might corrupt
52132   ** the freelist.  So we do need to check for corruption while scanning
52133   ** the freelist.
52134   */
52135   hdr = pPage->hdrOffset;
52136   addr = hdr + 1;
52137   iLast = pPage->pBt->usableSize - 4;
52138   assert( start<=iLast );
52139   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
52140     if( pbegin<addr+4 ){
52141       return SQLITE_CORRUPT_BKPT;
52142     }
52143     addr = pbegin;
52144   }
52145   if( pbegin>iLast ){
52146     return SQLITE_CORRUPT_BKPT;
52147   }
52148   assert( pbegin>addr || pbegin==0 );
52149   put2byte(&data[addr], start);
52150   put2byte(&data[start], pbegin);
52151   put2byte(&data[start+2], size);
52152   pPage->nFree = pPage->nFree + (u16)size;
52153 
52154   /* Coalesce adjacent free blocks */
52155   addr = hdr + 1;
52156   while( (pbegin = get2byte(&data[addr]))>0 ){
52157     int pnext, psize, x;
52158     assert( pbegin>addr );
52159     assert( pbegin <= (int)pPage->pBt->usableSize-4 );
52160     pnext = get2byte(&data[pbegin]);
52161     psize = get2byte(&data[pbegin+2]);
52162     if( pbegin + psize + 3 >= pnext && pnext>0 ){
52163       int frag = pnext - (pbegin+psize);
52164       if( (frag<0) || (frag>(int)data[hdr+7]) ){
52165         return SQLITE_CORRUPT_BKPT;
52166       }
52167       data[hdr+7] -= (u8)frag;
52168       x = get2byte(&data[pnext]);
52169       put2byte(&data[pbegin], x);
52170       x = pnext + get2byte(&data[pnext+2]) - pbegin;
52171       put2byte(&data[pbegin+2], x);
52172     }else{
52173       addr = pbegin;
52174     }
52175   }
52176 
52177   /* If the cell content area begins with a freeblock, remove it. */
52178   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
52179     int top;
52180     pbegin = get2byte(&data[hdr+1]);
52181     memcpy(&data[hdr+1], &data[pbegin], 2);
52182     top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
52183     put2byte(&data[hdr+5], top);
52184   }
52185   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52186   return SQLITE_OK;
52187 }
52188 
52189 /*
52190 ** Decode the flags byte (the first byte of the header) for a page
52191 ** and initialize fields of the MemPage structure accordingly.
52192 **
52193 ** Only the following combinations are supported.  Anything different
52194 ** indicates a corrupt database files:
52195 **
52196 **         PTF_ZERODATA
52197 **         PTF_ZERODATA | PTF_LEAF
52198 **         PTF_LEAFDATA | PTF_INTKEY
52199 **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
52200 */
52201 static int decodeFlags(MemPage *pPage, int flagByte){
52202   BtShared *pBt;     /* A copy of pPage->pBt */
52203 
52204   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
52205   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52206   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
52207   flagByte &= ~PTF_LEAF;
52208   pPage->childPtrSize = 4-4*pPage->leaf;
52209   pBt = pPage->pBt;
52210   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
52211     pPage->intKey = 1;
52212     pPage->hasData = pPage->leaf;
52213     pPage->maxLocal = pBt->maxLeaf;
52214     pPage->minLocal = pBt->minLeaf;
52215   }else if( flagByte==PTF_ZERODATA ){
52216     pPage->intKey = 0;
52217     pPage->hasData = 0;
52218     pPage->maxLocal = pBt->maxLocal;
52219     pPage->minLocal = pBt->minLocal;
52220   }else{
52221     return SQLITE_CORRUPT_BKPT;
52222   }
52223   pPage->max1bytePayload = pBt->max1bytePayload;
52224   return SQLITE_OK;
52225 }
52226 
52227 /*
52228 ** Initialize the auxiliary information for a disk block.
52229 **
52230 ** Return SQLITE_OK on success.  If we see that the page does
52231 ** not contain a well-formed database page, then return
52232 ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
52233 ** guarantee that the page is well-formed.  It only shows that
52234 ** we failed to detect any corruption.
52235 */
52236 static int btreeInitPage(MemPage *pPage){
52237 
52238   assert( pPage->pBt!=0 );
52239   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52240   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
52241   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
52242   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
52243 
52244   if( !pPage->isInit ){
52245     u16 pc;            /* Address of a freeblock within pPage->aData[] */
52246     u8 hdr;            /* Offset to beginning of page header */
52247     u8 *data;          /* Equal to pPage->aData */
52248     BtShared *pBt;        /* The main btree structure */
52249     int usableSize;    /* Amount of usable space on each page */
52250     u16 cellOffset;    /* Offset from start of page to first cell pointer */
52251     int nFree;         /* Number of unused bytes on the page */
52252     int top;           /* First byte of the cell content area */
52253     int iCellFirst;    /* First allowable cell or freeblock offset */
52254     int iCellLast;     /* Last possible cell or freeblock offset */
52255 
52256     pBt = pPage->pBt;
52257 
52258     hdr = pPage->hdrOffset;
52259     data = pPage->aData;
52260     if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
52261     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
52262     pPage->maskPage = (u16)(pBt->pageSize - 1);
52263     pPage->nOverflow = 0;
52264     usableSize = pBt->usableSize;
52265     pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
52266     pPage->aDataEnd = &data[usableSize];
52267     pPage->aCellIdx = &data[cellOffset];
52268     top = get2byteNotZero(&data[hdr+5]);
52269     pPage->nCell = get2byte(&data[hdr+3]);
52270     if( pPage->nCell>MX_CELL(pBt) ){
52271       /* To many cells for a single page.  The page must be corrupt */
52272       return SQLITE_CORRUPT_BKPT;
52273     }
52274     testcase( pPage->nCell==MX_CELL(pBt) );
52275 
52276     /* A malformed database page might cause us to read past the end
52277     ** of page when parsing a cell.
52278     **
52279     ** The following block of code checks early to see if a cell extends
52280     ** past the end of a page boundary and causes SQLITE_CORRUPT to be
52281     ** returned if it does.
52282     */
52283     iCellFirst = cellOffset + 2*pPage->nCell;
52284     iCellLast = usableSize - 4;
52285 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
52286     {
52287       int i;            /* Index into the cell pointer array */
52288       int sz;           /* Size of a cell */
52289 
52290       if( !pPage->leaf ) iCellLast--;
52291       for(i=0; i<pPage->nCell; i++){
52292         pc = get2byte(&data[cellOffset+i*2]);
52293         testcase( pc==iCellFirst );
52294         testcase( pc==iCellLast );
52295         if( pc<iCellFirst || pc>iCellLast ){
52296           return SQLITE_CORRUPT_BKPT;
52297         }
52298         sz = cellSizePtr(pPage, &data[pc]);
52299         testcase( pc+sz==usableSize );
52300         if( pc+sz>usableSize ){
52301           return SQLITE_CORRUPT_BKPT;
52302         }
52303       }
52304       if( !pPage->leaf ) iCellLast++;
52305     }
52306 #endif
52307 
52308     /* Compute the total free space on the page */
52309     pc = get2byte(&data[hdr+1]);
52310     nFree = data[hdr+7] + top;
52311     while( pc>0 ){
52312       u16 next, size;
52313       if( pc<iCellFirst || pc>iCellLast ){
52314         /* Start of free block is off the page */
52315         return SQLITE_CORRUPT_BKPT;
52316       }
52317       next = get2byte(&data[pc]);
52318       size = get2byte(&data[pc+2]);
52319       if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
52320         /* Free blocks must be in ascending order. And the last byte of
52321         ** the free-block must lie on the database page.  */
52322         return SQLITE_CORRUPT_BKPT;
52323       }
52324       nFree = nFree + size;
52325       pc = next;
52326     }
52327 
52328     /* At this point, nFree contains the sum of the offset to the start
52329     ** of the cell-content area plus the number of free bytes within
52330     ** the cell-content area. If this is greater than the usable-size
52331     ** of the page, then the page must be corrupted. This check also
52332     ** serves to verify that the offset to the start of the cell-content
52333     ** area, according to the page header, lies within the page.
52334     */
52335     if( nFree>usableSize ){
52336       return SQLITE_CORRUPT_BKPT;
52337     }
52338     pPage->nFree = (u16)(nFree - iCellFirst);
52339     pPage->isInit = 1;
52340   }
52341   return SQLITE_OK;
52342 }
52343 
52344 /*
52345 ** Set up a raw page so that it looks like a database page holding
52346 ** no entries.
52347 */
52348 static void zeroPage(MemPage *pPage, int flags){
52349   unsigned char *data = pPage->aData;
52350   BtShared *pBt = pPage->pBt;
52351   u8 hdr = pPage->hdrOffset;
52352   u16 first;
52353 
52354   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
52355   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
52356   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
52357   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52358   assert( sqlite3_mutex_held(pBt->mutex) );
52359   if( pBt->btsFlags & BTS_SECURE_DELETE ){
52360     memset(&data[hdr], 0, pBt->usableSize - hdr);
52361   }
52362   data[hdr] = (char)flags;
52363   first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8);
52364   memset(&data[hdr+1], 0, 4);
52365   data[hdr+7] = 0;
52366   put2byte(&data[hdr+5], pBt->usableSize);
52367   pPage->nFree = (u16)(pBt->usableSize - first);
52368   decodeFlags(pPage, flags);
52369   pPage->cellOffset = first;
52370   pPage->aDataEnd = &data[pBt->usableSize];
52371   pPage->aCellIdx = &data[first];
52372   pPage->nOverflow = 0;
52373   assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
52374   pPage->maskPage = (u16)(pBt->pageSize - 1);
52375   pPage->nCell = 0;
52376   pPage->isInit = 1;
52377 }
52378 
52379 
52380 /*
52381 ** Convert a DbPage obtained from the pager into a MemPage used by
52382 ** the btree layer.
52383 */
52384 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
52385   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
52386   pPage->aData = sqlite3PagerGetData(pDbPage);
52387   pPage->pDbPage = pDbPage;
52388   pPage->pBt = pBt;
52389   pPage->pgno = pgno;
52390   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
52391   return pPage;
52392 }
52393 
52394 /*
52395 ** Get a page from the pager.  Initialize the MemPage.pBt and
52396 ** MemPage.aData elements if needed.
52397 **
52398 ** If the noContent flag is set, it means that we do not care about
52399 ** the content of the page at this time.  So do not go to the disk
52400 ** to fetch the content.  Just fill in the content with zeros for now.
52401 ** If in the future we call sqlite3PagerWrite() on this page, that
52402 ** means we have started to be concerned about content and the disk
52403 ** read should occur at that point.
52404 */
52405 static int btreeGetPage(
52406   BtShared *pBt,       /* The btree */
52407   Pgno pgno,           /* Number of the page to fetch */
52408   MemPage **ppPage,    /* Return the page in this parameter */
52409   int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
52410 ){
52411   int rc;
52412   DbPage *pDbPage;
52413 
52414   assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
52415   assert( sqlite3_mutex_held(pBt->mutex) );
52416   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
52417   if( rc ) return rc;
52418   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
52419   return SQLITE_OK;
52420 }
52421 
52422 /*
52423 ** Retrieve a page from the pager cache. If the requested page is not
52424 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
52425 ** MemPage.aData elements if needed.
52426 */
52427 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
52428   DbPage *pDbPage;
52429   assert( sqlite3_mutex_held(pBt->mutex) );
52430   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
52431   if( pDbPage ){
52432     return btreePageFromDbPage(pDbPage, pgno, pBt);
52433   }
52434   return 0;
52435 }
52436 
52437 /*
52438 ** Return the size of the database file in pages. If there is any kind of
52439 ** error, return ((unsigned int)-1).
52440 */
52441 static Pgno btreePagecount(BtShared *pBt){
52442   return pBt->nPage;
52443 }
52444 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
52445   assert( sqlite3BtreeHoldsMutex(p) );
52446   assert( ((p->pBt->nPage)&0x8000000)==0 );
52447   return (int)btreePagecount(p->pBt);
52448 }
52449 
52450 /*
52451 ** Get a page from the pager and initialize it.  This routine is just a
52452 ** convenience wrapper around separate calls to btreeGetPage() and
52453 ** btreeInitPage().
52454 **
52455 ** If an error occurs, then the value *ppPage is set to is undefined. It
52456 ** may remain unchanged, or it may be set to an invalid value.
52457 */
52458 static int getAndInitPage(
52459   BtShared *pBt,                  /* The database file */
52460   Pgno pgno,                      /* Number of the page to get */
52461   MemPage **ppPage,               /* Write the page pointer here */
52462   int bReadonly                   /* PAGER_GET_READONLY or 0 */
52463 ){
52464   int rc;
52465   assert( sqlite3_mutex_held(pBt->mutex) );
52466   assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
52467 
52468   if( pgno>btreePagecount(pBt) ){
52469     rc = SQLITE_CORRUPT_BKPT;
52470   }else{
52471     rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
52472     if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
52473       rc = btreeInitPage(*ppPage);
52474       if( rc!=SQLITE_OK ){
52475         releasePage(*ppPage);
52476       }
52477     }
52478   }
52479 
52480   testcase( pgno==0 );
52481   assert( pgno!=0 || rc==SQLITE_CORRUPT );
52482   return rc;
52483 }
52484 
52485 /*
52486 ** Release a MemPage.  This should be called once for each prior
52487 ** call to btreeGetPage.
52488 */
52489 static void releasePage(MemPage *pPage){
52490   if( pPage ){
52491     assert( pPage->aData );
52492     assert( pPage->pBt );
52493     assert( pPage->pDbPage!=0 );
52494     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
52495     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
52496     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52497     sqlite3PagerUnrefNotNull(pPage->pDbPage);
52498   }
52499 }
52500 
52501 /*
52502 ** During a rollback, when the pager reloads information into the cache
52503 ** so that the cache is restored to its original state at the start of
52504 ** the transaction, for each page restored this routine is called.
52505 **
52506 ** This routine needs to reset the extra data section at the end of the
52507 ** page to agree with the restored data.
52508 */
52509 static void pageReinit(DbPage *pData){
52510   MemPage *pPage;
52511   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
52512   assert( sqlite3PagerPageRefcount(pData)>0 );
52513   if( pPage->isInit ){
52514     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52515     pPage->isInit = 0;
52516     if( sqlite3PagerPageRefcount(pData)>1 ){
52517       /* pPage might not be a btree page;  it might be an overflow page
52518       ** or ptrmap page or a free page.  In those cases, the following
52519       ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
52520       ** But no harm is done by this.  And it is very important that
52521       ** btreeInitPage() be called on every btree page so we make
52522       ** the call for every page that comes in for re-initing. */
52523       btreeInitPage(pPage);
52524     }
52525   }
52526 }
52527 
52528 /*
52529 ** Invoke the busy handler for a btree.
52530 */
52531 static int btreeInvokeBusyHandler(void *pArg){
52532   BtShared *pBt = (BtShared*)pArg;
52533   assert( pBt->db );
52534   assert( sqlite3_mutex_held(pBt->db->mutex) );
52535   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
52536 }
52537 
52538 /*
52539 ** Open a database file.
52540 **
52541 ** zFilename is the name of the database file.  If zFilename is NULL
52542 ** then an ephemeral database is created.  The ephemeral database might
52543 ** be exclusively in memory, or it might use a disk-based memory cache.
52544 ** Either way, the ephemeral database will be automatically deleted
52545 ** when sqlite3BtreeClose() is called.
52546 **
52547 ** If zFilename is ":memory:" then an in-memory database is created
52548 ** that is automatically destroyed when it is closed.
52549 **
52550 ** The "flags" parameter is a bitmask that might contain bits like
52551 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
52552 **
52553 ** If the database is already opened in the same database connection
52554 ** and we are in shared cache mode, then the open will fail with an
52555 ** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
52556 ** objects in the same database connection since doing so will lead
52557 ** to problems with locking.
52558 */
52559 SQLITE_PRIVATE int sqlite3BtreeOpen(
52560   sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
52561   const char *zFilename,  /* Name of the file containing the BTree database */
52562   sqlite3 *db,            /* Associated database handle */
52563   Btree **ppBtree,        /* Pointer to new Btree object written here */
52564   int flags,              /* Options */
52565   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
52566 ){
52567   BtShared *pBt = 0;             /* Shared part of btree structure */
52568   Btree *p;                      /* Handle to return */
52569   sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
52570   int rc = SQLITE_OK;            /* Result code from this function */
52571   u8 nReserve;                   /* Byte of unused space on each page */
52572   unsigned char zDbHeader[100];  /* Database header content */
52573 
52574   /* True if opening an ephemeral, temporary database */
52575   const int isTempDb = zFilename==0 || zFilename[0]==0;
52576 
52577   /* Set the variable isMemdb to true for an in-memory database, or
52578   ** false for a file-based database.
52579   */
52580 #ifdef SQLITE_OMIT_MEMORYDB
52581   const int isMemdb = 0;
52582 #else
52583   const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
52584                        || (isTempDb && sqlite3TempInMemory(db))
52585                        || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
52586 #endif
52587 
52588   assert( db!=0 );
52589   assert( pVfs!=0 );
52590   assert( sqlite3_mutex_held(db->mutex) );
52591   assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
52592 
52593   /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
52594   assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
52595 
52596   /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
52597   assert( (flags & BTREE_SINGLE)==0 || isTempDb );
52598 
52599   if( isMemdb ){
52600     flags |= BTREE_MEMORY;
52601   }
52602   if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
52603     vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
52604   }
52605   p = sqlite3MallocZero(sizeof(Btree));
52606   if( !p ){
52607     return SQLITE_NOMEM;
52608   }
52609   p->inTrans = TRANS_NONE;
52610   p->db = db;
52611 #ifndef SQLITE_OMIT_SHARED_CACHE
52612   p->lock.pBtree = p;
52613   p->lock.iTable = 1;
52614 #endif
52615 
52616 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
52617   /*
52618   ** If this Btree is a candidate for shared cache, try to find an
52619   ** existing BtShared object that we can share with
52620   */
52621   if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
52622     if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
52623       int nFullPathname = pVfs->mxPathname+1;
52624       char *zFullPathname = sqlite3Malloc(nFullPathname);
52625       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
52626       p->sharable = 1;
52627       if( !zFullPathname ){
52628         sqlite3_free(p);
52629         return SQLITE_NOMEM;
52630       }
52631       if( isMemdb ){
52632         memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
52633       }else{
52634         rc = sqlite3OsFullPathname(pVfs, zFilename,
52635                                    nFullPathname, zFullPathname);
52636         if( rc ){
52637           sqlite3_free(zFullPathname);
52638           sqlite3_free(p);
52639           return rc;
52640         }
52641       }
52642 #if SQLITE_THREADSAFE
52643       mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
52644       sqlite3_mutex_enter(mutexOpen);
52645       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
52646       sqlite3_mutex_enter(mutexShared);
52647 #endif
52648       for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
52649         assert( pBt->nRef>0 );
52650         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
52651                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
52652           int iDb;
52653           for(iDb=db->nDb-1; iDb>=0; iDb--){
52654             Btree *pExisting = db->aDb[iDb].pBt;
52655             if( pExisting && pExisting->pBt==pBt ){
52656               sqlite3_mutex_leave(mutexShared);
52657               sqlite3_mutex_leave(mutexOpen);
52658               sqlite3_free(zFullPathname);
52659               sqlite3_free(p);
52660               return SQLITE_CONSTRAINT;
52661             }
52662           }
52663           p->pBt = pBt;
52664           pBt->nRef++;
52665           break;
52666         }
52667       }
52668       sqlite3_mutex_leave(mutexShared);
52669       sqlite3_free(zFullPathname);
52670     }
52671 #ifdef SQLITE_DEBUG
52672     else{
52673       /* In debug mode, we mark all persistent databases as sharable
52674       ** even when they are not.  This exercises the locking code and
52675       ** gives more opportunity for asserts(sqlite3_mutex_held())
52676       ** statements to find locking problems.
52677       */
52678       p->sharable = 1;
52679     }
52680 #endif
52681   }
52682 #endif
52683   if( pBt==0 ){
52684     /*
52685     ** The following asserts make sure that structures used by the btree are
52686     ** the right size.  This is to guard against size changes that result
52687     ** when compiling on a different architecture.
52688     */
52689     assert( sizeof(i64)==8 || sizeof(i64)==4 );
52690     assert( sizeof(u64)==8 || sizeof(u64)==4 );
52691     assert( sizeof(u32)==4 );
52692     assert( sizeof(u16)==2 );
52693     assert( sizeof(Pgno)==4 );
52694 
52695     pBt = sqlite3MallocZero( sizeof(*pBt) );
52696     if( pBt==0 ){
52697       rc = SQLITE_NOMEM;
52698       goto btree_open_out;
52699     }
52700     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
52701                           EXTRA_SIZE, flags, vfsFlags, pageReinit);
52702     if( rc==SQLITE_OK ){
52703       sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
52704       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
52705     }
52706     if( rc!=SQLITE_OK ){
52707       goto btree_open_out;
52708     }
52709     pBt->openFlags = (u8)flags;
52710     pBt->db = db;
52711     sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
52712     p->pBt = pBt;
52713 
52714     pBt->pCursor = 0;
52715     pBt->pPage1 = 0;
52716     if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
52717 #ifdef SQLITE_SECURE_DELETE
52718     pBt->btsFlags |= BTS_SECURE_DELETE;
52719 #endif
52720     pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
52721     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
52722          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
52723       pBt->pageSize = 0;
52724 #ifndef SQLITE_OMIT_AUTOVACUUM
52725       /* If the magic name ":memory:" will create an in-memory database, then
52726       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
52727       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
52728       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
52729       ** regular file-name. In this case the auto-vacuum applies as per normal.
52730       */
52731       if( zFilename && !isMemdb ){
52732         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
52733         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
52734       }
52735 #endif
52736       nReserve = 0;
52737     }else{
52738       nReserve = zDbHeader[20];
52739       pBt->btsFlags |= BTS_PAGESIZE_FIXED;
52740 #ifndef SQLITE_OMIT_AUTOVACUUM
52741       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
52742       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
52743 #endif
52744     }
52745     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
52746     if( rc ) goto btree_open_out;
52747     pBt->usableSize = pBt->pageSize - nReserve;
52748     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
52749 
52750 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
52751     /* Add the new BtShared object to the linked list sharable BtShareds.
52752     */
52753     if( p->sharable ){
52754       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
52755       pBt->nRef = 1;
52756       MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
52757       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
52758         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
52759         if( pBt->mutex==0 ){
52760           rc = SQLITE_NOMEM;
52761           db->mallocFailed = 0;
52762           goto btree_open_out;
52763         }
52764       }
52765       sqlite3_mutex_enter(mutexShared);
52766       pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
52767       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
52768       sqlite3_mutex_leave(mutexShared);
52769     }
52770 #endif
52771   }
52772 
52773 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
52774   /* If the new Btree uses a sharable pBtShared, then link the new
52775   ** Btree into the list of all sharable Btrees for the same connection.
52776   ** The list is kept in ascending order by pBt address.
52777   */
52778   if( p->sharable ){
52779     int i;
52780     Btree *pSib;
52781     for(i=0; i<db->nDb; i++){
52782       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
52783         while( pSib->pPrev ){ pSib = pSib->pPrev; }
52784         if( p->pBt<pSib->pBt ){
52785           p->pNext = pSib;
52786           p->pPrev = 0;
52787           pSib->pPrev = p;
52788         }else{
52789           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
52790             pSib = pSib->pNext;
52791           }
52792           p->pNext = pSib->pNext;
52793           p->pPrev = pSib;
52794           if( p->pNext ){
52795             p->pNext->pPrev = p;
52796           }
52797           pSib->pNext = p;
52798         }
52799         break;
52800       }
52801     }
52802   }
52803 #endif
52804   *ppBtree = p;
52805 
52806 btree_open_out:
52807   if( rc!=SQLITE_OK ){
52808     if( pBt && pBt->pPager ){
52809       sqlite3PagerClose(pBt->pPager);
52810     }
52811     sqlite3_free(pBt);
52812     sqlite3_free(p);
52813     *ppBtree = 0;
52814   }else{
52815     /* If the B-Tree was successfully opened, set the pager-cache size to the
52816     ** default value. Except, when opening on an existing shared pager-cache,
52817     ** do not change the pager-cache size.
52818     */
52819     if( sqlite3BtreeSchema(p, 0, 0)==0 ){
52820       sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
52821     }
52822   }
52823   if( mutexOpen ){
52824     assert( sqlite3_mutex_held(mutexOpen) );
52825     sqlite3_mutex_leave(mutexOpen);
52826   }
52827   return rc;
52828 }
52829 
52830 /*
52831 ** Decrement the BtShared.nRef counter.  When it reaches zero,
52832 ** remove the BtShared structure from the sharing list.  Return
52833 ** true if the BtShared.nRef counter reaches zero and return
52834 ** false if it is still positive.
52835 */
52836 static int removeFromSharingList(BtShared *pBt){
52837 #ifndef SQLITE_OMIT_SHARED_CACHE
52838   MUTEX_LOGIC( sqlite3_mutex *pMaster; )
52839   BtShared *pList;
52840   int removed = 0;
52841 
52842   assert( sqlite3_mutex_notheld(pBt->mutex) );
52843   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
52844   sqlite3_mutex_enter(pMaster);
52845   pBt->nRef--;
52846   if( pBt->nRef<=0 ){
52847     if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
52848       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
52849     }else{
52850       pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
52851       while( ALWAYS(pList) && pList->pNext!=pBt ){
52852         pList=pList->pNext;
52853       }
52854       if( ALWAYS(pList) ){
52855         pList->pNext = pBt->pNext;
52856       }
52857     }
52858     if( SQLITE_THREADSAFE ){
52859       sqlite3_mutex_free(pBt->mutex);
52860     }
52861     removed = 1;
52862   }
52863   sqlite3_mutex_leave(pMaster);
52864   return removed;
52865 #else
52866   return 1;
52867 #endif
52868 }
52869 
52870 /*
52871 ** Make sure pBt->pTmpSpace points to an allocation of
52872 ** MX_CELL_SIZE(pBt) bytes.
52873 */
52874 static void allocateTempSpace(BtShared *pBt){
52875   if( !pBt->pTmpSpace ){
52876     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
52877 
52878     /* One of the uses of pBt->pTmpSpace is to format cells before
52879     ** inserting them into a leaf page (function fillInCell()). If
52880     ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
52881     ** by the various routines that manipulate binary cells. Which
52882     ** can mean that fillInCell() only initializes the first 2 or 3
52883     ** bytes of pTmpSpace, but that the first 4 bytes are copied from
52884     ** it into a database page. This is not actually a problem, but it
52885     ** does cause a valgrind error when the 1 or 2 bytes of unitialized
52886     ** data is passed to system call write(). So to avoid this error,
52887     ** zero the first 4 bytes of temp space here.  */
52888     if( pBt->pTmpSpace ) memset(pBt->pTmpSpace, 0, 4);
52889   }
52890 }
52891 
52892 /*
52893 ** Free the pBt->pTmpSpace allocation
52894 */
52895 static void freeTempSpace(BtShared *pBt){
52896   sqlite3PageFree( pBt->pTmpSpace);
52897   pBt->pTmpSpace = 0;
52898 }
52899 
52900 /*
52901 ** Close an open database and invalidate all cursors.
52902 */
52903 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
52904   BtShared *pBt = p->pBt;
52905   BtCursor *pCur;
52906 
52907   /* Close all cursors opened via this handle.  */
52908   assert( sqlite3_mutex_held(p->db->mutex) );
52909   sqlite3BtreeEnter(p);
52910   pCur = pBt->pCursor;
52911   while( pCur ){
52912     BtCursor *pTmp = pCur;
52913     pCur = pCur->pNext;
52914     if( pTmp->pBtree==p ){
52915       sqlite3BtreeCloseCursor(pTmp);
52916     }
52917   }
52918 
52919   /* Rollback any active transaction and free the handle structure.
52920   ** The call to sqlite3BtreeRollback() drops any table-locks held by
52921   ** this handle.
52922   */
52923   sqlite3BtreeRollback(p, SQLITE_OK);
52924   sqlite3BtreeLeave(p);
52925 
52926   /* If there are still other outstanding references to the shared-btree
52927   ** structure, return now. The remainder of this procedure cleans
52928   ** up the shared-btree.
52929   */
52930   assert( p->wantToLock==0 && p->locked==0 );
52931   if( !p->sharable || removeFromSharingList(pBt) ){
52932     /* The pBt is no longer on the sharing list, so we can access
52933     ** it without having to hold the mutex.
52934     **
52935     ** Clean out and delete the BtShared object.
52936     */
52937     assert( !pBt->pCursor );
52938     sqlite3PagerClose(pBt->pPager);
52939     if( pBt->xFreeSchema && pBt->pSchema ){
52940       pBt->xFreeSchema(pBt->pSchema);
52941     }
52942     sqlite3DbFree(0, pBt->pSchema);
52943     freeTempSpace(pBt);
52944     sqlite3_free(pBt);
52945   }
52946 
52947 #ifndef SQLITE_OMIT_SHARED_CACHE
52948   assert( p->wantToLock==0 );
52949   assert( p->locked==0 );
52950   if( p->pPrev ) p->pPrev->pNext = p->pNext;
52951   if( p->pNext ) p->pNext->pPrev = p->pPrev;
52952 #endif
52953 
52954   sqlite3_free(p);
52955   return SQLITE_OK;
52956 }
52957 
52958 /*
52959 ** Change the limit on the number of pages allowed in the cache.
52960 **
52961 ** The maximum number of cache pages is set to the absolute
52962 ** value of mxPage.  If mxPage is negative, the pager will
52963 ** operate asynchronously - it will not stop to do fsync()s
52964 ** to insure data is written to the disk surface before
52965 ** continuing.  Transactions still work if synchronous is off,
52966 ** and the database cannot be corrupted if this program
52967 ** crashes.  But if the operating system crashes or there is
52968 ** an abrupt power failure when synchronous is off, the database
52969 ** could be left in an inconsistent and unrecoverable state.
52970 ** Synchronous is on by default so database corruption is not
52971 ** normally a worry.
52972 */
52973 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
52974   BtShared *pBt = p->pBt;
52975   assert( sqlite3_mutex_held(p->db->mutex) );
52976   sqlite3BtreeEnter(p);
52977   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
52978   sqlite3BtreeLeave(p);
52979   return SQLITE_OK;
52980 }
52981 
52982 /*
52983 ** Change the limit on the amount of the database file that may be
52984 ** memory mapped.
52985 */
52986 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
52987   BtShared *pBt = p->pBt;
52988   assert( sqlite3_mutex_held(p->db->mutex) );
52989   sqlite3BtreeEnter(p);
52990   sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
52991   sqlite3BtreeLeave(p);
52992   return SQLITE_OK;
52993 }
52994 
52995 /*
52996 ** Change the way data is synced to disk in order to increase or decrease
52997 ** how well the database resists damage due to OS crashes and power
52998 ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
52999 ** there is a high probability of damage)  Level 2 is the default.  There
53000 ** is a very low but non-zero probability of damage.  Level 3 reduces the
53001 ** probability of damage to near zero but with a write performance reduction.
53002 */
53003 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
53004 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
53005   Btree *p,              /* The btree to set the safety level on */
53006   unsigned pgFlags       /* Various PAGER_* flags */
53007 ){
53008   BtShared *pBt = p->pBt;
53009   assert( sqlite3_mutex_held(p->db->mutex) );
53010   sqlite3BtreeEnter(p);
53011   sqlite3PagerSetFlags(pBt->pPager, pgFlags);
53012   sqlite3BtreeLeave(p);
53013   return SQLITE_OK;
53014 }
53015 #endif
53016 
53017 /*
53018 ** Return TRUE if the given btree is set to safety level 1.  In other
53019 ** words, return TRUE if no sync() occurs on the disk files.
53020 */
53021 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
53022   BtShared *pBt = p->pBt;
53023   int rc;
53024   assert( sqlite3_mutex_held(p->db->mutex) );
53025   sqlite3BtreeEnter(p);
53026   assert( pBt && pBt->pPager );
53027   rc = sqlite3PagerNosync(pBt->pPager);
53028   sqlite3BtreeLeave(p);
53029   return rc;
53030 }
53031 
53032 /*
53033 ** Change the default pages size and the number of reserved bytes per page.
53034 ** Or, if the page size has already been fixed, return SQLITE_READONLY
53035 ** without changing anything.
53036 **
53037 ** The page size must be a power of 2 between 512 and 65536.  If the page
53038 ** size supplied does not meet this constraint then the page size is not
53039 ** changed.
53040 **
53041 ** Page sizes are constrained to be a power of two so that the region
53042 ** of the database file used for locking (beginning at PENDING_BYTE,
53043 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
53044 ** at the beginning of a page.
53045 **
53046 ** If parameter nReserve is less than zero, then the number of reserved
53047 ** bytes per page is left unchanged.
53048 **
53049 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
53050 ** and autovacuum mode can no longer be changed.
53051 */
53052 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
53053   int rc = SQLITE_OK;
53054   BtShared *pBt = p->pBt;
53055   assert( nReserve>=-1 && nReserve<=255 );
53056   sqlite3BtreeEnter(p);
53057   if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
53058     sqlite3BtreeLeave(p);
53059     return SQLITE_READONLY;
53060   }
53061   if( nReserve<0 ){
53062     nReserve = pBt->pageSize - pBt->usableSize;
53063   }
53064   assert( nReserve>=0 && nReserve<=255 );
53065   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
53066         ((pageSize-1)&pageSize)==0 ){
53067     assert( (pageSize & 7)==0 );
53068     assert( !pBt->pPage1 && !pBt->pCursor );
53069     pBt->pageSize = (u32)pageSize;
53070     freeTempSpace(pBt);
53071   }
53072   rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
53073   pBt->usableSize = pBt->pageSize - (u16)nReserve;
53074   if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
53075   sqlite3BtreeLeave(p);
53076   return rc;
53077 }
53078 
53079 /*
53080 ** Return the currently defined page size
53081 */
53082 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
53083   return p->pBt->pageSize;
53084 }
53085 
53086 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
53087 /*
53088 ** This function is similar to sqlite3BtreeGetReserve(), except that it
53089 ** may only be called if it is guaranteed that the b-tree mutex is already
53090 ** held.
53091 **
53092 ** This is useful in one special case in the backup API code where it is
53093 ** known that the shared b-tree mutex is held, but the mutex on the
53094 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
53095 ** were to be called, it might collide with some other operation on the
53096 ** database handle that owns *p, causing undefined behavior.
53097 */
53098 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
53099   assert( sqlite3_mutex_held(p->pBt->mutex) );
53100   return p->pBt->pageSize - p->pBt->usableSize;
53101 }
53102 #endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
53103 
53104 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
53105 /*
53106 ** Return the number of bytes of space at the end of every page that
53107 ** are intentually left unused.  This is the "reserved" space that is
53108 ** sometimes used by extensions.
53109 */
53110 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
53111   int n;
53112   sqlite3BtreeEnter(p);
53113   n = p->pBt->pageSize - p->pBt->usableSize;
53114   sqlite3BtreeLeave(p);
53115   return n;
53116 }
53117 
53118 /*
53119 ** Set the maximum page count for a database if mxPage is positive.
53120 ** No changes are made if mxPage is 0 or negative.
53121 ** Regardless of the value of mxPage, return the maximum page count.
53122 */
53123 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
53124   int n;
53125   sqlite3BtreeEnter(p);
53126   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
53127   sqlite3BtreeLeave(p);
53128   return n;
53129 }
53130 
53131 /*
53132 ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
53133 ** then make no changes.  Always return the value of the BTS_SECURE_DELETE
53134 ** setting after the change.
53135 */
53136 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
53137   int b;
53138   if( p==0 ) return 0;
53139   sqlite3BtreeEnter(p);
53140   if( newFlag>=0 ){
53141     p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
53142     if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
53143   }
53144   b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
53145   sqlite3BtreeLeave(p);
53146   return b;
53147 }
53148 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
53149 
53150 /*
53151 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
53152 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
53153 ** is disabled. The default value for the auto-vacuum property is
53154 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
53155 */
53156 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
53157 #ifdef SQLITE_OMIT_AUTOVACUUM
53158   return SQLITE_READONLY;
53159 #else
53160   BtShared *pBt = p->pBt;
53161   int rc = SQLITE_OK;
53162   u8 av = (u8)autoVacuum;
53163 
53164   sqlite3BtreeEnter(p);
53165   if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
53166     rc = SQLITE_READONLY;
53167   }else{
53168     pBt->autoVacuum = av ?1:0;
53169     pBt->incrVacuum = av==2 ?1:0;
53170   }
53171   sqlite3BtreeLeave(p);
53172   return rc;
53173 #endif
53174 }
53175 
53176 /*
53177 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
53178 ** enabled 1 is returned. Otherwise 0.
53179 */
53180 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
53181 #ifdef SQLITE_OMIT_AUTOVACUUM
53182   return BTREE_AUTOVACUUM_NONE;
53183 #else
53184   int rc;
53185   sqlite3BtreeEnter(p);
53186   rc = (
53187     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
53188     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
53189     BTREE_AUTOVACUUM_INCR
53190   );
53191   sqlite3BtreeLeave(p);
53192   return rc;
53193 #endif
53194 }
53195 
53196 
53197 /*
53198 ** Get a reference to pPage1 of the database file.  This will
53199 ** also acquire a readlock on that file.
53200 **
53201 ** SQLITE_OK is returned on success.  If the file is not a
53202 ** well-formed database file, then SQLITE_CORRUPT is returned.
53203 ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
53204 ** is returned if we run out of memory.
53205 */
53206 static int lockBtree(BtShared *pBt){
53207   int rc;              /* Result code from subfunctions */
53208   MemPage *pPage1;     /* Page 1 of the database file */
53209   int nPage;           /* Number of pages in the database */
53210   int nPageFile = 0;   /* Number of pages in the database file */
53211   int nPageHeader;     /* Number of pages in the database according to hdr */
53212 
53213   assert( sqlite3_mutex_held(pBt->mutex) );
53214   assert( pBt->pPage1==0 );
53215   rc = sqlite3PagerSharedLock(pBt->pPager);
53216   if( rc!=SQLITE_OK ) return rc;
53217   rc = btreeGetPage(pBt, 1, &pPage1, 0);
53218   if( rc!=SQLITE_OK ) return rc;
53219 
53220   /* Do some checking to help insure the file we opened really is
53221   ** a valid database file.
53222   */
53223   nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
53224   sqlite3PagerPagecount(pBt->pPager, &nPageFile);
53225   if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
53226     nPage = nPageFile;
53227   }
53228   if( nPage>0 ){
53229     u32 pageSize;
53230     u32 usableSize;
53231     u8 *page1 = pPage1->aData;
53232     rc = SQLITE_NOTADB;
53233     if( memcmp(page1, zMagicHeader, 16)!=0 ){
53234       goto page1_init_failed;
53235     }
53236 
53237 #ifdef SQLITE_OMIT_WAL
53238     if( page1[18]>1 ){
53239       pBt->btsFlags |= BTS_READ_ONLY;
53240     }
53241     if( page1[19]>1 ){
53242       goto page1_init_failed;
53243     }
53244 #else
53245     if( page1[18]>2 ){
53246       pBt->btsFlags |= BTS_READ_ONLY;
53247     }
53248     if( page1[19]>2 ){
53249       goto page1_init_failed;
53250     }
53251 
53252     /* If the write version is set to 2, this database should be accessed
53253     ** in WAL mode. If the log is not already open, open it now. Then
53254     ** return SQLITE_OK and return without populating BtShared.pPage1.
53255     ** The caller detects this and calls this function again. This is
53256     ** required as the version of page 1 currently in the page1 buffer
53257     ** may not be the latest version - there may be a newer one in the log
53258     ** file.
53259     */
53260     if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
53261       int isOpen = 0;
53262       rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
53263       if( rc!=SQLITE_OK ){
53264         goto page1_init_failed;
53265       }else if( isOpen==0 ){
53266         releasePage(pPage1);
53267         return SQLITE_OK;
53268       }
53269       rc = SQLITE_NOTADB;
53270     }
53271 #endif
53272 
53273     /* The maximum embedded fraction must be exactly 25%.  And the minimum
53274     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
53275     ** The original design allowed these amounts to vary, but as of
53276     ** version 3.6.0, we require them to be fixed.
53277     */
53278     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
53279       goto page1_init_failed;
53280     }
53281     pageSize = (page1[16]<<8) | (page1[17]<<16);
53282     if( ((pageSize-1)&pageSize)!=0
53283      || pageSize>SQLITE_MAX_PAGE_SIZE
53284      || pageSize<=256
53285     ){
53286       goto page1_init_failed;
53287     }
53288     assert( (pageSize & 7)==0 );
53289     usableSize = pageSize - page1[20];
53290     if( (u32)pageSize!=pBt->pageSize ){
53291       /* After reading the first page of the database assuming a page size
53292       ** of BtShared.pageSize, we have discovered that the page-size is
53293       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
53294       ** zero and return SQLITE_OK. The caller will call this function
53295       ** again with the correct page-size.
53296       */
53297       releasePage(pPage1);
53298       pBt->usableSize = usableSize;
53299       pBt->pageSize = pageSize;
53300       freeTempSpace(pBt);
53301       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
53302                                    pageSize-usableSize);
53303       return rc;
53304     }
53305     if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
53306       rc = SQLITE_CORRUPT_BKPT;
53307       goto page1_init_failed;
53308     }
53309     if( usableSize<480 ){
53310       goto page1_init_failed;
53311     }
53312     pBt->pageSize = pageSize;
53313     pBt->usableSize = usableSize;
53314 #ifndef SQLITE_OMIT_AUTOVACUUM
53315     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
53316     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
53317 #endif
53318   }
53319 
53320   /* maxLocal is the maximum amount of payload to store locally for
53321   ** a cell.  Make sure it is small enough so that at least minFanout
53322   ** cells can will fit on one page.  We assume a 10-byte page header.
53323   ** Besides the payload, the cell must store:
53324   **     2-byte pointer to the cell
53325   **     4-byte child pointer
53326   **     9-byte nKey value
53327   **     4-byte nData value
53328   **     4-byte overflow page pointer
53329   ** So a cell consists of a 2-byte pointer, a header which is as much as
53330   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
53331   ** page pointer.
53332   */
53333   pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
53334   pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
53335   pBt->maxLeaf = (u16)(pBt->usableSize - 35);
53336   pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
53337   if( pBt->maxLocal>127 ){
53338     pBt->max1bytePayload = 127;
53339   }else{
53340     pBt->max1bytePayload = (u8)pBt->maxLocal;
53341   }
53342   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
53343   pBt->pPage1 = pPage1;
53344   pBt->nPage = nPage;
53345   return SQLITE_OK;
53346 
53347 page1_init_failed:
53348   releasePage(pPage1);
53349   pBt->pPage1 = 0;
53350   return rc;
53351 }
53352 
53353 #ifndef NDEBUG
53354 /*
53355 ** Return the number of cursors open on pBt. This is for use
53356 ** in assert() expressions, so it is only compiled if NDEBUG is not
53357 ** defined.
53358 **
53359 ** Only write cursors are counted if wrOnly is true.  If wrOnly is
53360 ** false then all cursors are counted.
53361 **
53362 ** For the purposes of this routine, a cursor is any cursor that
53363 ** is capable of reading or writing to the databse.  Cursors that
53364 ** have been tripped into the CURSOR_FAULT state are not counted.
53365 */
53366 static int countValidCursors(BtShared *pBt, int wrOnly){
53367   BtCursor *pCur;
53368   int r = 0;
53369   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
53370     if( (wrOnly==0 || pCur->wrFlag) && pCur->eState!=CURSOR_FAULT ) r++;
53371   }
53372   return r;
53373 }
53374 #endif
53375 
53376 /*
53377 ** If there are no outstanding cursors and we are not in the middle
53378 ** of a transaction but there is a read lock on the database, then
53379 ** this routine unrefs the first page of the database file which
53380 ** has the effect of releasing the read lock.
53381 **
53382 ** If there is a transaction in progress, this routine is a no-op.
53383 */
53384 static void unlockBtreeIfUnused(BtShared *pBt){
53385   assert( sqlite3_mutex_held(pBt->mutex) );
53386   assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
53387   if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
53388     assert( pBt->pPage1->aData );
53389     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
53390     assert( pBt->pPage1->aData );
53391     releasePage(pBt->pPage1);
53392     pBt->pPage1 = 0;
53393   }
53394 }
53395 
53396 /*
53397 ** If pBt points to an empty file then convert that empty file
53398 ** into a new empty database by initializing the first page of
53399 ** the database.
53400 */
53401 static int newDatabase(BtShared *pBt){
53402   MemPage *pP1;
53403   unsigned char *data;
53404   int rc;
53405 
53406   assert( sqlite3_mutex_held(pBt->mutex) );
53407   if( pBt->nPage>0 ){
53408     return SQLITE_OK;
53409   }
53410   pP1 = pBt->pPage1;
53411   assert( pP1!=0 );
53412   data = pP1->aData;
53413   rc = sqlite3PagerWrite(pP1->pDbPage);
53414   if( rc ) return rc;
53415   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
53416   assert( sizeof(zMagicHeader)==16 );
53417   data[16] = (u8)((pBt->pageSize>>8)&0xff);
53418   data[17] = (u8)((pBt->pageSize>>16)&0xff);
53419   data[18] = 1;
53420   data[19] = 1;
53421   assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
53422   data[20] = (u8)(pBt->pageSize - pBt->usableSize);
53423   data[21] = 64;
53424   data[22] = 32;
53425   data[23] = 32;
53426   memset(&data[24], 0, 100-24);
53427   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
53428   pBt->btsFlags |= BTS_PAGESIZE_FIXED;
53429 #ifndef SQLITE_OMIT_AUTOVACUUM
53430   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
53431   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
53432   put4byte(&data[36 + 4*4], pBt->autoVacuum);
53433   put4byte(&data[36 + 7*4], pBt->incrVacuum);
53434 #endif
53435   pBt->nPage = 1;
53436   data[31] = 1;
53437   return SQLITE_OK;
53438 }
53439 
53440 /*
53441 ** Initialize the first page of the database file (creating a database
53442 ** consisting of a single page and no schema objects). Return SQLITE_OK
53443 ** if successful, or an SQLite error code otherwise.
53444 */
53445 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
53446   int rc;
53447   sqlite3BtreeEnter(p);
53448   p->pBt->nPage = 0;
53449   rc = newDatabase(p->pBt);
53450   sqlite3BtreeLeave(p);
53451   return rc;
53452 }
53453 
53454 /*
53455 ** Attempt to start a new transaction. A write-transaction
53456 ** is started if the second argument is nonzero, otherwise a read-
53457 ** transaction.  If the second argument is 2 or more and exclusive
53458 ** transaction is started, meaning that no other process is allowed
53459 ** to access the database.  A preexisting transaction may not be
53460 ** upgraded to exclusive by calling this routine a second time - the
53461 ** exclusivity flag only works for a new transaction.
53462 **
53463 ** A write-transaction must be started before attempting any
53464 ** changes to the database.  None of the following routines
53465 ** will work unless a transaction is started first:
53466 **
53467 **      sqlite3BtreeCreateTable()
53468 **      sqlite3BtreeCreateIndex()
53469 **      sqlite3BtreeClearTable()
53470 **      sqlite3BtreeDropTable()
53471 **      sqlite3BtreeInsert()
53472 **      sqlite3BtreeDelete()
53473 **      sqlite3BtreeUpdateMeta()
53474 **
53475 ** If an initial attempt to acquire the lock fails because of lock contention
53476 ** and the database was previously unlocked, then invoke the busy handler
53477 ** if there is one.  But if there was previously a read-lock, do not
53478 ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is
53479 ** returned when there is already a read-lock in order to avoid a deadlock.
53480 **
53481 ** Suppose there are two processes A and B.  A has a read lock and B has
53482 ** a reserved lock.  B tries to promote to exclusive but is blocked because
53483 ** of A's read lock.  A tries to promote to reserved but is blocked by B.
53484 ** One or the other of the two processes must give way or there can be
53485 ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
53486 ** when A already has a read lock, we encourage A to give up and let B
53487 ** proceed.
53488 */
53489 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
53490   sqlite3 *pBlock = 0;
53491   BtShared *pBt = p->pBt;
53492   int rc = SQLITE_OK;
53493 
53494   sqlite3BtreeEnter(p);
53495   btreeIntegrity(p);
53496 
53497   /* If the btree is already in a write-transaction, or it
53498   ** is already in a read-transaction and a read-transaction
53499   ** is requested, this is a no-op.
53500   */
53501   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
53502     goto trans_begun;
53503   }
53504   assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
53505 
53506   /* Write transactions are not possible on a read-only database */
53507   if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
53508     rc = SQLITE_READONLY;
53509     goto trans_begun;
53510   }
53511 
53512 #ifndef SQLITE_OMIT_SHARED_CACHE
53513   /* If another database handle has already opened a write transaction
53514   ** on this shared-btree structure and a second write transaction is
53515   ** requested, return SQLITE_LOCKED.
53516   */
53517   if( (wrflag && pBt->inTransaction==TRANS_WRITE)
53518    || (pBt->btsFlags & BTS_PENDING)!=0
53519   ){
53520     pBlock = pBt->pWriter->db;
53521   }else if( wrflag>1 ){
53522     BtLock *pIter;
53523     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
53524       if( pIter->pBtree!=p ){
53525         pBlock = pIter->pBtree->db;
53526         break;
53527       }
53528     }
53529   }
53530   if( pBlock ){
53531     sqlite3ConnectionBlocked(p->db, pBlock);
53532     rc = SQLITE_LOCKED_SHAREDCACHE;
53533     goto trans_begun;
53534   }
53535 #endif
53536 
53537   /* Any read-only or read-write transaction implies a read-lock on
53538   ** page 1. So if some other shared-cache client already has a write-lock
53539   ** on page 1, the transaction cannot be opened. */
53540   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
53541   if( SQLITE_OK!=rc ) goto trans_begun;
53542 
53543   pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
53544   if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
53545   do {
53546     /* Call lockBtree() until either pBt->pPage1 is populated or
53547     ** lockBtree() returns something other than SQLITE_OK. lockBtree()
53548     ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
53549     ** reading page 1 it discovers that the page-size of the database
53550     ** file is not pBt->pageSize. In this case lockBtree() will update
53551     ** pBt->pageSize to the page-size of the file on disk.
53552     */
53553     while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
53554 
53555     if( rc==SQLITE_OK && wrflag ){
53556       if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
53557         rc = SQLITE_READONLY;
53558       }else{
53559         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
53560         if( rc==SQLITE_OK ){
53561           rc = newDatabase(pBt);
53562         }
53563       }
53564     }
53565 
53566     if( rc!=SQLITE_OK ){
53567       unlockBtreeIfUnused(pBt);
53568     }
53569   }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
53570           btreeInvokeBusyHandler(pBt) );
53571 
53572   if( rc==SQLITE_OK ){
53573     if( p->inTrans==TRANS_NONE ){
53574       pBt->nTransaction++;
53575 #ifndef SQLITE_OMIT_SHARED_CACHE
53576       if( p->sharable ){
53577         assert( p->lock.pBtree==p && p->lock.iTable==1 );
53578         p->lock.eLock = READ_LOCK;
53579         p->lock.pNext = pBt->pLock;
53580         pBt->pLock = &p->lock;
53581       }
53582 #endif
53583     }
53584     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
53585     if( p->inTrans>pBt->inTransaction ){
53586       pBt->inTransaction = p->inTrans;
53587     }
53588     if( wrflag ){
53589       MemPage *pPage1 = pBt->pPage1;
53590 #ifndef SQLITE_OMIT_SHARED_CACHE
53591       assert( !pBt->pWriter );
53592       pBt->pWriter = p;
53593       pBt->btsFlags &= ~BTS_EXCLUSIVE;
53594       if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
53595 #endif
53596 
53597       /* If the db-size header field is incorrect (as it may be if an old
53598       ** client has been writing the database file), update it now. Doing
53599       ** this sooner rather than later means the database size can safely
53600       ** re-read the database size from page 1 if a savepoint or transaction
53601       ** rollback occurs within the transaction.
53602       */
53603       if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
53604         rc = sqlite3PagerWrite(pPage1->pDbPage);
53605         if( rc==SQLITE_OK ){
53606           put4byte(&pPage1->aData[28], pBt->nPage);
53607         }
53608       }
53609     }
53610   }
53611 
53612 
53613 trans_begun:
53614   if( rc==SQLITE_OK && wrflag ){
53615     /* This call makes sure that the pager has the correct number of
53616     ** open savepoints. If the second parameter is greater than 0 and
53617     ** the sub-journal is not already open, then it will be opened here.
53618     */
53619     rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
53620   }
53621 
53622   btreeIntegrity(p);
53623   sqlite3BtreeLeave(p);
53624   return rc;
53625 }
53626 
53627 #ifndef SQLITE_OMIT_AUTOVACUUM
53628 
53629 /*
53630 ** Set the pointer-map entries for all children of page pPage. Also, if
53631 ** pPage contains cells that point to overflow pages, set the pointer
53632 ** map entries for the overflow pages as well.
53633 */
53634 static int setChildPtrmaps(MemPage *pPage){
53635   int i;                             /* Counter variable */
53636   int nCell;                         /* Number of cells in page pPage */
53637   int rc;                            /* Return code */
53638   BtShared *pBt = pPage->pBt;
53639   u8 isInitOrig = pPage->isInit;
53640   Pgno pgno = pPage->pgno;
53641 
53642   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53643   rc = btreeInitPage(pPage);
53644   if( rc!=SQLITE_OK ){
53645     goto set_child_ptrmaps_out;
53646   }
53647   nCell = pPage->nCell;
53648 
53649   for(i=0; i<nCell; i++){
53650     u8 *pCell = findCell(pPage, i);
53651 
53652     ptrmapPutOvflPtr(pPage, pCell, &rc);
53653 
53654     if( !pPage->leaf ){
53655       Pgno childPgno = get4byte(pCell);
53656       ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
53657     }
53658   }
53659 
53660   if( !pPage->leaf ){
53661     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
53662     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
53663   }
53664 
53665 set_child_ptrmaps_out:
53666   pPage->isInit = isInitOrig;
53667   return rc;
53668 }
53669 
53670 /*
53671 ** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
53672 ** that it points to iTo. Parameter eType describes the type of pointer to
53673 ** be modified, as  follows:
53674 **
53675 ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child
53676 **                   page of pPage.
53677 **
53678 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
53679 **                   page pointed to by one of the cells on pPage.
53680 **
53681 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
53682 **                   overflow page in the list.
53683 */
53684 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
53685   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53686   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53687   if( eType==PTRMAP_OVERFLOW2 ){
53688     /* The pointer is always the first 4 bytes of the page in this case.  */
53689     if( get4byte(pPage->aData)!=iFrom ){
53690       return SQLITE_CORRUPT_BKPT;
53691     }
53692     put4byte(pPage->aData, iTo);
53693   }else{
53694     u8 isInitOrig = pPage->isInit;
53695     int i;
53696     int nCell;
53697 
53698     btreeInitPage(pPage);
53699     nCell = pPage->nCell;
53700 
53701     for(i=0; i<nCell; i++){
53702       u8 *pCell = findCell(pPage, i);
53703       if( eType==PTRMAP_OVERFLOW1 ){
53704         CellInfo info;
53705         btreeParseCellPtr(pPage, pCell, &info);
53706         if( info.iOverflow
53707          && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
53708          && iFrom==get4byte(&pCell[info.iOverflow])
53709         ){
53710           put4byte(&pCell[info.iOverflow], iTo);
53711           break;
53712         }
53713       }else{
53714         if( get4byte(pCell)==iFrom ){
53715           put4byte(pCell, iTo);
53716           break;
53717         }
53718       }
53719     }
53720 
53721     if( i==nCell ){
53722       if( eType!=PTRMAP_BTREE ||
53723           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
53724         return SQLITE_CORRUPT_BKPT;
53725       }
53726       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
53727     }
53728 
53729     pPage->isInit = isInitOrig;
53730   }
53731   return SQLITE_OK;
53732 }
53733 
53734 
53735 /*
53736 ** Move the open database page pDbPage to location iFreePage in the
53737 ** database. The pDbPage reference remains valid.
53738 **
53739 ** The isCommit flag indicates that there is no need to remember that
53740 ** the journal needs to be sync()ed before database page pDbPage->pgno
53741 ** can be written to. The caller has already promised not to write to that
53742 ** page.
53743 */
53744 static int relocatePage(
53745   BtShared *pBt,           /* Btree */
53746   MemPage *pDbPage,        /* Open page to move */
53747   u8 eType,                /* Pointer map 'type' entry for pDbPage */
53748   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
53749   Pgno iFreePage,          /* The location to move pDbPage to */
53750   int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
53751 ){
53752   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
53753   Pgno iDbPage = pDbPage->pgno;
53754   Pager *pPager = pBt->pPager;
53755   int rc;
53756 
53757   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
53758       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
53759   assert( sqlite3_mutex_held(pBt->mutex) );
53760   assert( pDbPage->pBt==pBt );
53761 
53762   /* Move page iDbPage from its current location to page number iFreePage */
53763   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
53764       iDbPage, iFreePage, iPtrPage, eType));
53765   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
53766   if( rc!=SQLITE_OK ){
53767     return rc;
53768   }
53769   pDbPage->pgno = iFreePage;
53770 
53771   /* If pDbPage was a btree-page, then it may have child pages and/or cells
53772   ** that point to overflow pages. The pointer map entries for all these
53773   ** pages need to be changed.
53774   **
53775   ** If pDbPage is an overflow page, then the first 4 bytes may store a
53776   ** pointer to a subsequent overflow page. If this is the case, then
53777   ** the pointer map needs to be updated for the subsequent overflow page.
53778   */
53779   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
53780     rc = setChildPtrmaps(pDbPage);
53781     if( rc!=SQLITE_OK ){
53782       return rc;
53783     }
53784   }else{
53785     Pgno nextOvfl = get4byte(pDbPage->aData);
53786     if( nextOvfl!=0 ){
53787       ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
53788       if( rc!=SQLITE_OK ){
53789         return rc;
53790       }
53791     }
53792   }
53793 
53794   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
53795   ** that it points at iFreePage. Also fix the pointer map entry for
53796   ** iPtrPage.
53797   */
53798   if( eType!=PTRMAP_ROOTPAGE ){
53799     rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
53800     if( rc!=SQLITE_OK ){
53801       return rc;
53802     }
53803     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
53804     if( rc!=SQLITE_OK ){
53805       releasePage(pPtrPage);
53806       return rc;
53807     }
53808     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
53809     releasePage(pPtrPage);
53810     if( rc==SQLITE_OK ){
53811       ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
53812     }
53813   }
53814   return rc;
53815 }
53816 
53817 /* Forward declaration required by incrVacuumStep(). */
53818 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
53819 
53820 /*
53821 ** Perform a single step of an incremental-vacuum. If successful, return
53822 ** SQLITE_OK. If there is no work to do (and therefore no point in
53823 ** calling this function again), return SQLITE_DONE. Or, if an error
53824 ** occurs, return some other error code.
53825 **
53826 ** More specificly, this function attempts to re-organize the database so
53827 ** that the last page of the file currently in use is no longer in use.
53828 **
53829 ** Parameter nFin is the number of pages that this database would contain
53830 ** were this function called until it returns SQLITE_DONE.
53831 **
53832 ** If the bCommit parameter is non-zero, this function assumes that the
53833 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
53834 ** or an error. bCommit is passed true for an auto-vacuum-on-commmit
53835 ** operation, or false for an incremental vacuum.
53836 */
53837 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
53838   Pgno nFreeList;           /* Number of pages still on the free-list */
53839   int rc;
53840 
53841   assert( sqlite3_mutex_held(pBt->mutex) );
53842   assert( iLastPg>nFin );
53843 
53844   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
53845     u8 eType;
53846     Pgno iPtrPage;
53847 
53848     nFreeList = get4byte(&pBt->pPage1->aData[36]);
53849     if( nFreeList==0 ){
53850       return SQLITE_DONE;
53851     }
53852 
53853     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
53854     if( rc!=SQLITE_OK ){
53855       return rc;
53856     }
53857     if( eType==PTRMAP_ROOTPAGE ){
53858       return SQLITE_CORRUPT_BKPT;
53859     }
53860 
53861     if( eType==PTRMAP_FREEPAGE ){
53862       if( bCommit==0 ){
53863         /* Remove the page from the files free-list. This is not required
53864         ** if bCommit is non-zero. In that case, the free-list will be
53865         ** truncated to zero after this function returns, so it doesn't
53866         ** matter if it still contains some garbage entries.
53867         */
53868         Pgno iFreePg;
53869         MemPage *pFreePg;
53870         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
53871         if( rc!=SQLITE_OK ){
53872           return rc;
53873         }
53874         assert( iFreePg==iLastPg );
53875         releasePage(pFreePg);
53876       }
53877     } else {
53878       Pgno iFreePg;             /* Index of free page to move pLastPg to */
53879       MemPage *pLastPg;
53880       u8 eMode = BTALLOC_ANY;   /* Mode parameter for allocateBtreePage() */
53881       Pgno iNear = 0;           /* nearby parameter for allocateBtreePage() */
53882 
53883       rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
53884       if( rc!=SQLITE_OK ){
53885         return rc;
53886       }
53887 
53888       /* If bCommit is zero, this loop runs exactly once and page pLastPg
53889       ** is swapped with the first free page pulled off the free list.
53890       **
53891       ** On the other hand, if bCommit is greater than zero, then keep
53892       ** looping until a free-page located within the first nFin pages
53893       ** of the file is found.
53894       */
53895       if( bCommit==0 ){
53896         eMode = BTALLOC_LE;
53897         iNear = nFin;
53898       }
53899       do {
53900         MemPage *pFreePg;
53901         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
53902         if( rc!=SQLITE_OK ){
53903           releasePage(pLastPg);
53904           return rc;
53905         }
53906         releasePage(pFreePg);
53907       }while( bCommit && iFreePg>nFin );
53908       assert( iFreePg<iLastPg );
53909 
53910       rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
53911       releasePage(pLastPg);
53912       if( rc!=SQLITE_OK ){
53913         return rc;
53914       }
53915     }
53916   }
53917 
53918   if( bCommit==0 ){
53919     do {
53920       iLastPg--;
53921     }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
53922     pBt->bDoTruncate = 1;
53923     pBt->nPage = iLastPg;
53924   }
53925   return SQLITE_OK;
53926 }
53927 
53928 /*
53929 ** The database opened by the first argument is an auto-vacuum database
53930 ** nOrig pages in size containing nFree free pages. Return the expected
53931 ** size of the database in pages following an auto-vacuum operation.
53932 */
53933 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
53934   int nEntry;                     /* Number of entries on one ptrmap page */
53935   Pgno nPtrmap;                   /* Number of PtrMap pages to be freed */
53936   Pgno nFin;                      /* Return value */
53937 
53938   nEntry = pBt->usableSize/5;
53939   nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
53940   nFin = nOrig - nFree - nPtrmap;
53941   if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
53942     nFin--;
53943   }
53944   while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
53945     nFin--;
53946   }
53947 
53948   return nFin;
53949 }
53950 
53951 /*
53952 ** A write-transaction must be opened before calling this function.
53953 ** It performs a single unit of work towards an incremental vacuum.
53954 **
53955 ** If the incremental vacuum is finished after this function has run,
53956 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
53957 ** SQLITE_OK is returned. Otherwise an SQLite error code.
53958 */
53959 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
53960   int rc;
53961   BtShared *pBt = p->pBt;
53962 
53963   sqlite3BtreeEnter(p);
53964   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
53965   if( !pBt->autoVacuum ){
53966     rc = SQLITE_DONE;
53967   }else{
53968     Pgno nOrig = btreePagecount(pBt);
53969     Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
53970     Pgno nFin = finalDbSize(pBt, nOrig, nFree);
53971 
53972     if( nOrig<nFin ){
53973       rc = SQLITE_CORRUPT_BKPT;
53974     }else if( nFree>0 ){
53975       rc = saveAllCursors(pBt, 0, 0);
53976       if( rc==SQLITE_OK ){
53977         invalidateAllOverflowCache(pBt);
53978         rc = incrVacuumStep(pBt, nFin, nOrig, 0);
53979       }
53980       if( rc==SQLITE_OK ){
53981         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
53982         put4byte(&pBt->pPage1->aData[28], pBt->nPage);
53983       }
53984     }else{
53985       rc = SQLITE_DONE;
53986     }
53987   }
53988   sqlite3BtreeLeave(p);
53989   return rc;
53990 }
53991 
53992 /*
53993 ** This routine is called prior to sqlite3PagerCommit when a transaction
53994 ** is committed for an auto-vacuum database.
53995 **
53996 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
53997 ** the database file should be truncated to during the commit process.
53998 ** i.e. the database has been reorganized so that only the first *pnTrunc
53999 ** pages are in use.
54000 */
54001 static int autoVacuumCommit(BtShared *pBt){
54002   int rc = SQLITE_OK;
54003   Pager *pPager = pBt->pPager;
54004   VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
54005 
54006   assert( sqlite3_mutex_held(pBt->mutex) );
54007   invalidateAllOverflowCache(pBt);
54008   assert(pBt->autoVacuum);
54009   if( !pBt->incrVacuum ){
54010     Pgno nFin;         /* Number of pages in database after autovacuuming */
54011     Pgno nFree;        /* Number of pages on the freelist initially */
54012     Pgno iFree;        /* The next page to be freed */
54013     Pgno nOrig;        /* Database size before freeing */
54014 
54015     nOrig = btreePagecount(pBt);
54016     if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
54017       /* It is not possible to create a database for which the final page
54018       ** is either a pointer-map page or the pending-byte page. If one
54019       ** is encountered, this indicates corruption.
54020       */
54021       return SQLITE_CORRUPT_BKPT;
54022     }
54023 
54024     nFree = get4byte(&pBt->pPage1->aData[36]);
54025     nFin = finalDbSize(pBt, nOrig, nFree);
54026     if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
54027     if( nFin<nOrig ){
54028       rc = saveAllCursors(pBt, 0, 0);
54029     }
54030     for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
54031       rc = incrVacuumStep(pBt, nFin, iFree, 1);
54032     }
54033     if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
54034       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
54035       put4byte(&pBt->pPage1->aData[32], 0);
54036       put4byte(&pBt->pPage1->aData[36], 0);
54037       put4byte(&pBt->pPage1->aData[28], nFin);
54038       pBt->bDoTruncate = 1;
54039       pBt->nPage = nFin;
54040     }
54041     if( rc!=SQLITE_OK ){
54042       sqlite3PagerRollback(pPager);
54043     }
54044   }
54045 
54046   assert( nRef>=sqlite3PagerRefcount(pPager) );
54047   return rc;
54048 }
54049 
54050 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
54051 # define setChildPtrmaps(x) SQLITE_OK
54052 #endif
54053 
54054 /*
54055 ** This routine does the first phase of a two-phase commit.  This routine
54056 ** causes a rollback journal to be created (if it does not already exist)
54057 ** and populated with enough information so that if a power loss occurs
54058 ** the database can be restored to its original state by playing back
54059 ** the journal.  Then the contents of the journal are flushed out to
54060 ** the disk.  After the journal is safely on oxide, the changes to the
54061 ** database are written into the database file and flushed to oxide.
54062 ** At the end of this call, the rollback journal still exists on the
54063 ** disk and we are still holding all locks, so the transaction has not
54064 ** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
54065 ** commit process.
54066 **
54067 ** This call is a no-op if no write-transaction is currently active on pBt.
54068 **
54069 ** Otherwise, sync the database file for the btree pBt. zMaster points to
54070 ** the name of a master journal file that should be written into the
54071 ** individual journal file, or is NULL, indicating no master journal file
54072 ** (single database transaction).
54073 **
54074 ** When this is called, the master journal should already have been
54075 ** created, populated with this journal pointer and synced to disk.
54076 **
54077 ** Once this is routine has returned, the only thing required to commit
54078 ** the write-transaction for this database file is to delete the journal.
54079 */
54080 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
54081   int rc = SQLITE_OK;
54082   if( p->inTrans==TRANS_WRITE ){
54083     BtShared *pBt = p->pBt;
54084     sqlite3BtreeEnter(p);
54085 #ifndef SQLITE_OMIT_AUTOVACUUM
54086     if( pBt->autoVacuum ){
54087       rc = autoVacuumCommit(pBt);
54088       if( rc!=SQLITE_OK ){
54089         sqlite3BtreeLeave(p);
54090         return rc;
54091       }
54092     }
54093     if( pBt->bDoTruncate ){
54094       sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
54095     }
54096 #endif
54097     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
54098     sqlite3BtreeLeave(p);
54099   }
54100   return rc;
54101 }
54102 
54103 /*
54104 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
54105 ** at the conclusion of a transaction.
54106 */
54107 static void btreeEndTransaction(Btree *p){
54108   BtShared *pBt = p->pBt;
54109   sqlite3 *db = p->db;
54110   assert( sqlite3BtreeHoldsMutex(p) );
54111 
54112 #ifndef SQLITE_OMIT_AUTOVACUUM
54113   pBt->bDoTruncate = 0;
54114 #endif
54115   if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
54116     /* If there are other active statements that belong to this database
54117     ** handle, downgrade to a read-only transaction. The other statements
54118     ** may still be reading from the database.  */
54119     downgradeAllSharedCacheTableLocks(p);
54120     p->inTrans = TRANS_READ;
54121   }else{
54122     /* If the handle had any kind of transaction open, decrement the
54123     ** transaction count of the shared btree. If the transaction count
54124     ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
54125     ** call below will unlock the pager.  */
54126     if( p->inTrans!=TRANS_NONE ){
54127       clearAllSharedCacheTableLocks(p);
54128       pBt->nTransaction--;
54129       if( 0==pBt->nTransaction ){
54130         pBt->inTransaction = TRANS_NONE;
54131       }
54132     }
54133 
54134     /* Set the current transaction state to TRANS_NONE and unlock the
54135     ** pager if this call closed the only read or write transaction.  */
54136     p->inTrans = TRANS_NONE;
54137     unlockBtreeIfUnused(pBt);
54138   }
54139 
54140   btreeIntegrity(p);
54141 }
54142 
54143 /*
54144 ** Commit the transaction currently in progress.
54145 **
54146 ** This routine implements the second phase of a 2-phase commit.  The
54147 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
54148 ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
54149 ** routine did all the work of writing information out to disk and flushing the
54150 ** contents so that they are written onto the disk platter.  All this
54151 ** routine has to do is delete or truncate or zero the header in the
54152 ** the rollback journal (which causes the transaction to commit) and
54153 ** drop locks.
54154 **
54155 ** Normally, if an error occurs while the pager layer is attempting to
54156 ** finalize the underlying journal file, this function returns an error and
54157 ** the upper layer will attempt a rollback. However, if the second argument
54158 ** is non-zero then this b-tree transaction is part of a multi-file
54159 ** transaction. In this case, the transaction has already been committed
54160 ** (by deleting a master journal file) and the caller will ignore this
54161 ** functions return code. So, even if an error occurs in the pager layer,
54162 ** reset the b-tree objects internal state to indicate that the write
54163 ** transaction has been closed. This is quite safe, as the pager will have
54164 ** transitioned to the error state.
54165 **
54166 ** This will release the write lock on the database file.  If there
54167 ** are no active cursors, it also releases the read lock.
54168 */
54169 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
54170 
54171   if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
54172   sqlite3BtreeEnter(p);
54173   btreeIntegrity(p);
54174 
54175   /* If the handle has a write-transaction open, commit the shared-btrees
54176   ** transaction and set the shared state to TRANS_READ.
54177   */
54178   if( p->inTrans==TRANS_WRITE ){
54179     int rc;
54180     BtShared *pBt = p->pBt;
54181     assert( pBt->inTransaction==TRANS_WRITE );
54182     assert( pBt->nTransaction>0 );
54183     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
54184     if( rc!=SQLITE_OK && bCleanup==0 ){
54185       sqlite3BtreeLeave(p);
54186       return rc;
54187     }
54188     pBt->inTransaction = TRANS_READ;
54189     btreeClearHasContent(pBt);
54190   }
54191 
54192   btreeEndTransaction(p);
54193   sqlite3BtreeLeave(p);
54194   return SQLITE_OK;
54195 }
54196 
54197 /*
54198 ** Do both phases of a commit.
54199 */
54200 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
54201   int rc;
54202   sqlite3BtreeEnter(p);
54203   rc = sqlite3BtreeCommitPhaseOne(p, 0);
54204   if( rc==SQLITE_OK ){
54205     rc = sqlite3BtreeCommitPhaseTwo(p, 0);
54206   }
54207   sqlite3BtreeLeave(p);
54208   return rc;
54209 }
54210 
54211 /*
54212 ** This routine sets the state to CURSOR_FAULT and the error
54213 ** code to errCode for every cursor on BtShared that pBtree
54214 ** references.
54215 **
54216 ** Every cursor is tripped, including cursors that belong
54217 ** to other database connections that happen to be sharing
54218 ** the cache with pBtree.
54219 **
54220 ** This routine gets called when a rollback occurs.
54221 ** All cursors using the same cache must be tripped
54222 ** to prevent them from trying to use the btree after
54223 ** the rollback.  The rollback may have deleted tables
54224 ** or moved root pages, so it is not sufficient to
54225 ** save the state of the cursor.  The cursor must be
54226 ** invalidated.
54227 */
54228 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
54229   BtCursor *p;
54230   if( pBtree==0 ) return;
54231   sqlite3BtreeEnter(pBtree);
54232   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
54233     int i;
54234     sqlite3BtreeClearCursor(p);
54235     p->eState = CURSOR_FAULT;
54236     p->skipNext = errCode;
54237     for(i=0; i<=p->iPage; i++){
54238       releasePage(p->apPage[i]);
54239       p->apPage[i] = 0;
54240     }
54241   }
54242   sqlite3BtreeLeave(pBtree);
54243 }
54244 
54245 /*
54246 ** Rollback the transaction in progress.  All cursors will be
54247 ** invalided by this operation.  Any attempt to use a cursor
54248 ** that was open at the beginning of this operation will result
54249 ** in an error.
54250 **
54251 ** This will release the write lock on the database file.  If there
54252 ** are no active cursors, it also releases the read lock.
54253 */
54254 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){
54255   int rc;
54256   BtShared *pBt = p->pBt;
54257   MemPage *pPage1;
54258 
54259   sqlite3BtreeEnter(p);
54260   if( tripCode==SQLITE_OK ){
54261     rc = tripCode = saveAllCursors(pBt, 0, 0);
54262   }else{
54263     rc = SQLITE_OK;
54264   }
54265   if( tripCode ){
54266     sqlite3BtreeTripAllCursors(p, tripCode);
54267   }
54268   btreeIntegrity(p);
54269 
54270   if( p->inTrans==TRANS_WRITE ){
54271     int rc2;
54272 
54273     assert( TRANS_WRITE==pBt->inTransaction );
54274     rc2 = sqlite3PagerRollback(pBt->pPager);
54275     if( rc2!=SQLITE_OK ){
54276       rc = rc2;
54277     }
54278 
54279     /* The rollback may have destroyed the pPage1->aData value.  So
54280     ** call btreeGetPage() on page 1 again to make
54281     ** sure pPage1->aData is set correctly. */
54282     if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
54283       int nPage = get4byte(28+(u8*)pPage1->aData);
54284       testcase( nPage==0 );
54285       if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
54286       testcase( pBt->nPage!=nPage );
54287       pBt->nPage = nPage;
54288       releasePage(pPage1);
54289     }
54290     assert( countValidCursors(pBt, 1)==0 );
54291     pBt->inTransaction = TRANS_READ;
54292     btreeClearHasContent(pBt);
54293   }
54294 
54295   btreeEndTransaction(p);
54296   sqlite3BtreeLeave(p);
54297   return rc;
54298 }
54299 
54300 /*
54301 ** Start a statement subtransaction. The subtransaction can can be rolled
54302 ** back independently of the main transaction. You must start a transaction
54303 ** before starting a subtransaction. The subtransaction is ended automatically
54304 ** if the main transaction commits or rolls back.
54305 **
54306 ** Statement subtransactions are used around individual SQL statements
54307 ** that are contained within a BEGIN...COMMIT block.  If a constraint
54308 ** error occurs within the statement, the effect of that one statement
54309 ** can be rolled back without having to rollback the entire transaction.
54310 **
54311 ** A statement sub-transaction is implemented as an anonymous savepoint. The
54312 ** value passed as the second parameter is the total number of savepoints,
54313 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
54314 ** are no active savepoints and no other statement-transactions open,
54315 ** iStatement is 1. This anonymous savepoint can be released or rolled back
54316 ** using the sqlite3BtreeSavepoint() function.
54317 */
54318 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
54319   int rc;
54320   BtShared *pBt = p->pBt;
54321   sqlite3BtreeEnter(p);
54322   assert( p->inTrans==TRANS_WRITE );
54323   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
54324   assert( iStatement>0 );
54325   assert( iStatement>p->db->nSavepoint );
54326   assert( pBt->inTransaction==TRANS_WRITE );
54327   /* At the pager level, a statement transaction is a savepoint with
54328   ** an index greater than all savepoints created explicitly using
54329   ** SQL statements. It is illegal to open, release or rollback any
54330   ** such savepoints while the statement transaction savepoint is active.
54331   */
54332   rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
54333   sqlite3BtreeLeave(p);
54334   return rc;
54335 }
54336 
54337 /*
54338 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
54339 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
54340 ** savepoint identified by parameter iSavepoint, depending on the value
54341 ** of op.
54342 **
54343 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
54344 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
54345 ** contents of the entire transaction are rolled back. This is different
54346 ** from a normal transaction rollback, as no locks are released and the
54347 ** transaction remains open.
54348 */
54349 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
54350   int rc = SQLITE_OK;
54351   if( p && p->inTrans==TRANS_WRITE ){
54352     BtShared *pBt = p->pBt;
54353     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
54354     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
54355     sqlite3BtreeEnter(p);
54356     rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
54357     if( rc==SQLITE_OK ){
54358       if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
54359         pBt->nPage = 0;
54360       }
54361       rc = newDatabase(pBt);
54362       pBt->nPage = get4byte(28 + pBt->pPage1->aData);
54363 
54364       /* The database size was written into the offset 28 of the header
54365       ** when the transaction started, so we know that the value at offset
54366       ** 28 is nonzero. */
54367       assert( pBt->nPage>0 );
54368     }
54369     sqlite3BtreeLeave(p);
54370   }
54371   return rc;
54372 }
54373 
54374 /*
54375 ** Create a new cursor for the BTree whose root is on the page
54376 ** iTable. If a read-only cursor is requested, it is assumed that
54377 ** the caller already has at least a read-only transaction open
54378 ** on the database already. If a write-cursor is requested, then
54379 ** the caller is assumed to have an open write transaction.
54380 **
54381 ** If wrFlag==0, then the cursor can only be used for reading.
54382 ** If wrFlag==1, then the cursor can be used for reading or for
54383 ** writing if other conditions for writing are also met.  These
54384 ** are the conditions that must be met in order for writing to
54385 ** be allowed:
54386 **
54387 ** 1:  The cursor must have been opened with wrFlag==1
54388 **
54389 ** 2:  Other database connections that share the same pager cache
54390 **     but which are not in the READ_UNCOMMITTED state may not have
54391 **     cursors open with wrFlag==0 on the same table.  Otherwise
54392 **     the changes made by this write cursor would be visible to
54393 **     the read cursors in the other database connection.
54394 **
54395 ** 3:  The database must be writable (not on read-only media)
54396 **
54397 ** 4:  There must be an active transaction.
54398 **
54399 ** No checking is done to make sure that page iTable really is the
54400 ** root page of a b-tree.  If it is not, then the cursor acquired
54401 ** will not work correctly.
54402 **
54403 ** It is assumed that the sqlite3BtreeCursorZero() has been called
54404 ** on pCur to initialize the memory space prior to invoking this routine.
54405 */
54406 static int btreeCursor(
54407   Btree *p,                              /* The btree */
54408   int iTable,                            /* Root page of table to open */
54409   int wrFlag,                            /* 1 to write. 0 read-only */
54410   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
54411   BtCursor *pCur                         /* Space for new cursor */
54412 ){
54413   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
54414 
54415   assert( sqlite3BtreeHoldsMutex(p) );
54416   assert( wrFlag==0 || wrFlag==1 );
54417 
54418   /* The following assert statements verify that if this is a sharable
54419   ** b-tree database, the connection is holding the required table locks,
54420   ** and that no other connection has any open cursor that conflicts with
54421   ** this lock.  */
54422   assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
54423   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
54424 
54425   /* Assert that the caller has opened the required transaction. */
54426   assert( p->inTrans>TRANS_NONE );
54427   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
54428   assert( pBt->pPage1 && pBt->pPage1->aData );
54429 
54430   if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
54431     return SQLITE_READONLY;
54432   }
54433   if( iTable==1 && btreePagecount(pBt)==0 ){
54434     assert( wrFlag==0 );
54435     iTable = 0;
54436   }
54437 
54438   /* Now that no other errors can occur, finish filling in the BtCursor
54439   ** variables and link the cursor into the BtShared list.  */
54440   pCur->pgnoRoot = (Pgno)iTable;
54441   pCur->iPage = -1;
54442   pCur->pKeyInfo = pKeyInfo;
54443   pCur->pBtree = p;
54444   pCur->pBt = pBt;
54445   pCur->wrFlag = (u8)wrFlag;
54446   pCur->pNext = pBt->pCursor;
54447   if( pCur->pNext ){
54448     pCur->pNext->pPrev = pCur;
54449   }
54450   pBt->pCursor = pCur;
54451   pCur->eState = CURSOR_INVALID;
54452   return SQLITE_OK;
54453 }
54454 SQLITE_PRIVATE int sqlite3BtreeCursor(
54455   Btree *p,                                   /* The btree */
54456   int iTable,                                 /* Root page of table to open */
54457   int wrFlag,                                 /* 1 to write. 0 read-only */
54458   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
54459   BtCursor *pCur                              /* Write new cursor here */
54460 ){
54461   int rc;
54462   sqlite3BtreeEnter(p);
54463   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
54464   sqlite3BtreeLeave(p);
54465   return rc;
54466 }
54467 
54468 /*
54469 ** Return the size of a BtCursor object in bytes.
54470 **
54471 ** This interfaces is needed so that users of cursors can preallocate
54472 ** sufficient storage to hold a cursor.  The BtCursor object is opaque
54473 ** to users so they cannot do the sizeof() themselves - they must call
54474 ** this routine.
54475 */
54476 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
54477   return ROUND8(sizeof(BtCursor));
54478 }
54479 
54480 /*
54481 ** Initialize memory that will be converted into a BtCursor object.
54482 **
54483 ** The simple approach here would be to memset() the entire object
54484 ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
54485 ** do not need to be zeroed and they are large, so we can save a lot
54486 ** of run-time by skipping the initialization of those elements.
54487 */
54488 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
54489   memset(p, 0, offsetof(BtCursor, iPage));
54490 }
54491 
54492 /*
54493 ** Close a cursor.  The read lock on the database file is released
54494 ** when the last cursor is closed.
54495 */
54496 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
54497   Btree *pBtree = pCur->pBtree;
54498   if( pBtree ){
54499     int i;
54500     BtShared *pBt = pCur->pBt;
54501     sqlite3BtreeEnter(pBtree);
54502     sqlite3BtreeClearCursor(pCur);
54503     if( pCur->pPrev ){
54504       pCur->pPrev->pNext = pCur->pNext;
54505     }else{
54506       pBt->pCursor = pCur->pNext;
54507     }
54508     if( pCur->pNext ){
54509       pCur->pNext->pPrev = pCur->pPrev;
54510     }
54511     for(i=0; i<=pCur->iPage; i++){
54512       releasePage(pCur->apPage[i]);
54513     }
54514     unlockBtreeIfUnused(pBt);
54515     invalidateOverflowCache(pCur);
54516     /* sqlite3_free(pCur); */
54517     sqlite3BtreeLeave(pBtree);
54518   }
54519   return SQLITE_OK;
54520 }
54521 
54522 /*
54523 ** Make sure the BtCursor* given in the argument has a valid
54524 ** BtCursor.info structure.  If it is not already valid, call
54525 ** btreeParseCell() to fill it in.
54526 **
54527 ** BtCursor.info is a cache of the information in the current cell.
54528 ** Using this cache reduces the number of calls to btreeParseCell().
54529 **
54530 ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
54531 ** compiler to crash when getCellInfo() is implemented as a macro.
54532 ** But there is a measureable speed advantage to using the macro on gcc
54533 ** (when less compiler optimizations like -Os or -O0 are used and the
54534 ** compiler is not doing agressive inlining.)  So we use a real function
54535 ** for MSVC and a macro for everything else.  Ticket #2457.
54536 */
54537 #ifndef NDEBUG
54538   static void assertCellInfo(BtCursor *pCur){
54539     CellInfo info;
54540     int iPage = pCur->iPage;
54541     memset(&info, 0, sizeof(info));
54542     btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
54543     assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
54544   }
54545 #else
54546   #define assertCellInfo(x)
54547 #endif
54548 #ifdef _MSC_VER
54549   /* Use a real function in MSVC to work around bugs in that compiler. */
54550   static void getCellInfo(BtCursor *pCur){
54551     if( pCur->info.nSize==0 ){
54552       int iPage = pCur->iPage;
54553       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
54554       pCur->validNKey = 1;
54555     }else{
54556       assertCellInfo(pCur);
54557     }
54558   }
54559 #else /* if not _MSC_VER */
54560   /* Use a macro in all other compilers so that the function is inlined */
54561 #define getCellInfo(pCur)                                                      \
54562   if( pCur->info.nSize==0 ){                                                   \
54563     int iPage = pCur->iPage;                                                   \
54564     btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
54565     pCur->validNKey = 1;                                                       \
54566   }else{                                                                       \
54567     assertCellInfo(pCur);                                                      \
54568   }
54569 #endif /* _MSC_VER */
54570 
54571 #ifndef NDEBUG  /* The next routine used only within assert() statements */
54572 /*
54573 ** Return true if the given BtCursor is valid.  A valid cursor is one
54574 ** that is currently pointing to a row in a (non-empty) table.
54575 ** This is a verification routine is used only within assert() statements.
54576 */
54577 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
54578   return pCur && pCur->eState==CURSOR_VALID;
54579 }
54580 #endif /* NDEBUG */
54581 
54582 /*
54583 ** Set *pSize to the size of the buffer needed to hold the value of
54584 ** the key for the current entry.  If the cursor is not pointing
54585 ** to a valid entry, *pSize is set to 0.
54586 **
54587 ** For a table with the INTKEY flag set, this routine returns the key
54588 ** itself, not the number of bytes in the key.
54589 **
54590 ** The caller must position the cursor prior to invoking this routine.
54591 **
54592 ** This routine cannot fail.  It always returns SQLITE_OK.
54593 */
54594 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
54595   assert( cursorHoldsMutex(pCur) );
54596   assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
54597   if( pCur->eState!=CURSOR_VALID ){
54598     *pSize = 0;
54599   }else{
54600     getCellInfo(pCur);
54601     *pSize = pCur->info.nKey;
54602   }
54603   return SQLITE_OK;
54604 }
54605 
54606 /*
54607 ** Set *pSize to the number of bytes of data in the entry the
54608 ** cursor currently points to.
54609 **
54610 ** The caller must guarantee that the cursor is pointing to a non-NULL
54611 ** valid entry.  In other words, the calling procedure must guarantee
54612 ** that the cursor has Cursor.eState==CURSOR_VALID.
54613 **
54614 ** Failure is not possible.  This function always returns SQLITE_OK.
54615 ** It might just as well be a procedure (returning void) but we continue
54616 ** to return an integer result code for historical reasons.
54617 */
54618 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
54619   assert( cursorHoldsMutex(pCur) );
54620   assert( pCur->eState==CURSOR_VALID );
54621   getCellInfo(pCur);
54622   *pSize = pCur->info.nData;
54623   return SQLITE_OK;
54624 }
54625 
54626 /*
54627 ** Given the page number of an overflow page in the database (parameter
54628 ** ovfl), this function finds the page number of the next page in the
54629 ** linked list of overflow pages. If possible, it uses the auto-vacuum
54630 ** pointer-map data instead of reading the content of page ovfl to do so.
54631 **
54632 ** If an error occurs an SQLite error code is returned. Otherwise:
54633 **
54634 ** The page number of the next overflow page in the linked list is
54635 ** written to *pPgnoNext. If page ovfl is the last page in its linked
54636 ** list, *pPgnoNext is set to zero.
54637 **
54638 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
54639 ** to page number pOvfl was obtained, then *ppPage is set to point to that
54640 ** reference. It is the responsibility of the caller to call releasePage()
54641 ** on *ppPage to free the reference. In no reference was obtained (because
54642 ** the pointer-map was used to obtain the value for *pPgnoNext), then
54643 ** *ppPage is set to zero.
54644 */
54645 static int getOverflowPage(
54646   BtShared *pBt,               /* The database file */
54647   Pgno ovfl,                   /* Current overflow page number */
54648   MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
54649   Pgno *pPgnoNext              /* OUT: Next overflow page number */
54650 ){
54651   Pgno next = 0;
54652   MemPage *pPage = 0;
54653   int rc = SQLITE_OK;
54654 
54655   assert( sqlite3_mutex_held(pBt->mutex) );
54656   assert(pPgnoNext);
54657 
54658 #ifndef SQLITE_OMIT_AUTOVACUUM
54659   /* Try to find the next page in the overflow list using the
54660   ** autovacuum pointer-map pages. Guess that the next page in
54661   ** the overflow list is page number (ovfl+1). If that guess turns
54662   ** out to be wrong, fall back to loading the data of page
54663   ** number ovfl to determine the next page number.
54664   */
54665   if( pBt->autoVacuum ){
54666     Pgno pgno;
54667     Pgno iGuess = ovfl+1;
54668     u8 eType;
54669 
54670     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
54671       iGuess++;
54672     }
54673 
54674     if( iGuess<=btreePagecount(pBt) ){
54675       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
54676       if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
54677         next = iGuess;
54678         rc = SQLITE_DONE;
54679       }
54680     }
54681   }
54682 #endif
54683 
54684   assert( next==0 || rc==SQLITE_DONE );
54685   if( rc==SQLITE_OK ){
54686     rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
54687     assert( rc==SQLITE_OK || pPage==0 );
54688     if( rc==SQLITE_OK ){
54689       next = get4byte(pPage->aData);
54690     }
54691   }
54692 
54693   *pPgnoNext = next;
54694   if( ppPage ){
54695     *ppPage = pPage;
54696   }else{
54697     releasePage(pPage);
54698   }
54699   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
54700 }
54701 
54702 /*
54703 ** Copy data from a buffer to a page, or from a page to a buffer.
54704 **
54705 ** pPayload is a pointer to data stored on database page pDbPage.
54706 ** If argument eOp is false, then nByte bytes of data are copied
54707 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
54708 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
54709 ** of data are copied from the buffer pBuf to pPayload.
54710 **
54711 ** SQLITE_OK is returned on success, otherwise an error code.
54712 */
54713 static int copyPayload(
54714   void *pPayload,           /* Pointer to page data */
54715   void *pBuf,               /* Pointer to buffer */
54716   int nByte,                /* Number of bytes to copy */
54717   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
54718   DbPage *pDbPage           /* Page containing pPayload */
54719 ){
54720   if( eOp ){
54721     /* Copy data from buffer to page (a write operation) */
54722     int rc = sqlite3PagerWrite(pDbPage);
54723     if( rc!=SQLITE_OK ){
54724       return rc;
54725     }
54726     memcpy(pPayload, pBuf, nByte);
54727   }else{
54728     /* Copy data from page to buffer (a read operation) */
54729     memcpy(pBuf, pPayload, nByte);
54730   }
54731   return SQLITE_OK;
54732 }
54733 
54734 /*
54735 ** This function is used to read or overwrite payload information
54736 ** for the entry that the pCur cursor is pointing to. If the eOp
54737 ** parameter is 0, this is a read operation (data copied into
54738 ** buffer pBuf). If it is non-zero, a write (data copied from
54739 ** buffer pBuf).
54740 **
54741 ** A total of "amt" bytes are read or written beginning at "offset".
54742 ** Data is read to or from the buffer pBuf.
54743 **
54744 ** The content being read or written might appear on the main page
54745 ** or be scattered out on multiple overflow pages.
54746 **
54747 ** If the BtCursor.isIncrblobHandle flag is set, and the current
54748 ** cursor entry uses one or more overflow pages, this function
54749 ** allocates space for and lazily popluates the overflow page-list
54750 ** cache array (BtCursor.aOverflow). Subsequent calls use this
54751 ** cache to make seeking to the supplied offset more efficient.
54752 **
54753 ** Once an overflow page-list cache has been allocated, it may be
54754 ** invalidated if some other cursor writes to the same table, or if
54755 ** the cursor is moved to a different row. Additionally, in auto-vacuum
54756 ** mode, the following events may invalidate an overflow page-list cache.
54757 **
54758 **   * An incremental vacuum,
54759 **   * A commit in auto_vacuum="full" mode,
54760 **   * Creating a table (may require moving an overflow page).
54761 */
54762 static int accessPayload(
54763   BtCursor *pCur,      /* Cursor pointing to entry to read from */
54764   u32 offset,          /* Begin reading this far into payload */
54765   u32 amt,             /* Read this many bytes */
54766   unsigned char *pBuf, /* Write the bytes into this buffer */
54767   int eOp              /* zero to read. non-zero to write. */
54768 ){
54769   unsigned char *aPayload;
54770   int rc = SQLITE_OK;
54771   u32 nKey;
54772   int iIdx = 0;
54773   MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
54774   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
54775 
54776   assert( pPage );
54777   assert( pCur->eState==CURSOR_VALID );
54778   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
54779   assert( cursorHoldsMutex(pCur) );
54780 
54781   getCellInfo(pCur);
54782   aPayload = pCur->info.pCell + pCur->info.nHeader;
54783   nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
54784 
54785   if( NEVER(offset+amt > nKey+pCur->info.nData)
54786    || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
54787   ){
54788     /* Trying to read or write past the end of the data is an error */
54789     return SQLITE_CORRUPT_BKPT;
54790   }
54791 
54792   /* Check if data must be read/written to/from the btree page itself. */
54793   if( offset<pCur->info.nLocal ){
54794     int a = amt;
54795     if( a+offset>pCur->info.nLocal ){
54796       a = pCur->info.nLocal - offset;
54797     }
54798     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
54799     offset = 0;
54800     pBuf += a;
54801     amt -= a;
54802   }else{
54803     offset -= pCur->info.nLocal;
54804   }
54805 
54806   if( rc==SQLITE_OK && amt>0 ){
54807     const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
54808     Pgno nextPage;
54809 
54810     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
54811 
54812 #ifndef SQLITE_OMIT_INCRBLOB
54813     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
54814     ** has not been allocated, allocate it now. The array is sized at
54815     ** one entry for each overflow page in the overflow chain. The
54816     ** page number of the first overflow page is stored in aOverflow[0],
54817     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
54818     ** (the cache is lazily populated).
54819     */
54820     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
54821       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
54822       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
54823       /* nOvfl is always positive.  If it were zero, fetchPayload would have
54824       ** been used instead of this routine. */
54825       if( ALWAYS(nOvfl) && !pCur->aOverflow ){
54826         rc = SQLITE_NOMEM;
54827       }
54828     }
54829 
54830     /* If the overflow page-list cache has been allocated and the
54831     ** entry for the first required overflow page is valid, skip
54832     ** directly to it.
54833     */
54834     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
54835       iIdx = (offset/ovflSize);
54836       nextPage = pCur->aOverflow[iIdx];
54837       offset = (offset%ovflSize);
54838     }
54839 #endif
54840 
54841     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
54842 
54843 #ifndef SQLITE_OMIT_INCRBLOB
54844       /* If required, populate the overflow page-list cache. */
54845       if( pCur->aOverflow ){
54846         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
54847         pCur->aOverflow[iIdx] = nextPage;
54848       }
54849 #endif
54850 
54851       if( offset>=ovflSize ){
54852         /* The only reason to read this page is to obtain the page
54853         ** number for the next page in the overflow chain. The page
54854         ** data is not required. So first try to lookup the overflow
54855         ** page-list cache, if any, then fall back to the getOverflowPage()
54856         ** function.
54857         */
54858 #ifndef SQLITE_OMIT_INCRBLOB
54859         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
54860           nextPage = pCur->aOverflow[iIdx+1];
54861         } else
54862 #endif
54863           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
54864         offset -= ovflSize;
54865       }else{
54866         /* Need to read this page properly. It contains some of the
54867         ** range of data that is being read (eOp==0) or written (eOp!=0).
54868         */
54869 #ifdef SQLITE_DIRECT_OVERFLOW_READ
54870         sqlite3_file *fd;
54871 #endif
54872         int a = amt;
54873         if( a + offset > ovflSize ){
54874           a = ovflSize - offset;
54875         }
54876 
54877 #ifdef SQLITE_DIRECT_OVERFLOW_READ
54878         /* If all the following are true:
54879         **
54880         **   1) this is a read operation, and
54881         **   2) data is required from the start of this overflow page, and
54882         **   3) the database is file-backed, and
54883         **   4) there is no open write-transaction, and
54884         **   5) the database is not a WAL database,
54885         **
54886         ** then data can be read directly from the database file into the
54887         ** output buffer, bypassing the page-cache altogether. This speeds
54888         ** up loading large records that span many overflow pages.
54889         */
54890         if( eOp==0                                             /* (1) */
54891          && offset==0                                          /* (2) */
54892          && pBt->inTransaction==TRANS_READ                     /* (4) */
54893          && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
54894          && pBt->pPage1->aData[19]==0x01                       /* (5) */
54895         ){
54896           u8 aSave[4];
54897           u8 *aWrite = &pBuf[-4];
54898           memcpy(aSave, aWrite, 4);
54899           rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
54900           nextPage = get4byte(aWrite);
54901           memcpy(aWrite, aSave, 4);
54902         }else
54903 #endif
54904 
54905         {
54906           DbPage *pDbPage;
54907           rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
54908               (eOp==0 ? PAGER_GET_READONLY : 0)
54909           );
54910           if( rc==SQLITE_OK ){
54911             aPayload = sqlite3PagerGetData(pDbPage);
54912             nextPage = get4byte(aPayload);
54913             rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
54914             sqlite3PagerUnref(pDbPage);
54915             offset = 0;
54916           }
54917         }
54918         amt -= a;
54919         pBuf += a;
54920       }
54921     }
54922   }
54923 
54924   if( rc==SQLITE_OK && amt>0 ){
54925     return SQLITE_CORRUPT_BKPT;
54926   }
54927   return rc;
54928 }
54929 
54930 /*
54931 ** Read part of the key associated with cursor pCur.  Exactly
54932 ** "amt" bytes will be transfered into pBuf[].  The transfer
54933 ** begins at "offset".
54934 **
54935 ** The caller must ensure that pCur is pointing to a valid row
54936 ** in the table.
54937 **
54938 ** Return SQLITE_OK on success or an error code if anything goes
54939 ** wrong.  An error is returned if "offset+amt" is larger than
54940 ** the available payload.
54941 */
54942 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
54943   assert( cursorHoldsMutex(pCur) );
54944   assert( pCur->eState==CURSOR_VALID );
54945   assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
54946   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
54947   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
54948 }
54949 
54950 /*
54951 ** Read part of the data associated with cursor pCur.  Exactly
54952 ** "amt" bytes will be transfered into pBuf[].  The transfer
54953 ** begins at "offset".
54954 **
54955 ** Return SQLITE_OK on success or an error code if anything goes
54956 ** wrong.  An error is returned if "offset+amt" is larger than
54957 ** the available payload.
54958 */
54959 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
54960   int rc;
54961 
54962 #ifndef SQLITE_OMIT_INCRBLOB
54963   if ( pCur->eState==CURSOR_INVALID ){
54964     return SQLITE_ABORT;
54965   }
54966 #endif
54967 
54968   assert( cursorHoldsMutex(pCur) );
54969   rc = restoreCursorPosition(pCur);
54970   if( rc==SQLITE_OK ){
54971     assert( pCur->eState==CURSOR_VALID );
54972     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
54973     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
54974     rc = accessPayload(pCur, offset, amt, pBuf, 0);
54975   }
54976   return rc;
54977 }
54978 
54979 /*
54980 ** Return a pointer to payload information from the entry that the
54981 ** pCur cursor is pointing to.  The pointer is to the beginning of
54982 ** the key if index btrees (pPage->intKey==0) and is the data for
54983 ** table btrees (pPage->intKey==1). The number of bytes of available
54984 ** key/data is written into *pAmt.  If *pAmt==0, then the value
54985 ** returned will not be a valid pointer.
54986 **
54987 ** This routine is an optimization.  It is common for the entire key
54988 ** and data to fit on the local page and for there to be no overflow
54989 ** pages.  When that is so, this routine can be used to access the
54990 ** key and data without making a copy.  If the key and/or data spills
54991 ** onto overflow pages, then accessPayload() must be used to reassemble
54992 ** the key/data and copy it into a preallocated buffer.
54993 **
54994 ** The pointer returned by this routine looks directly into the cached
54995 ** page of the database.  The data might change or move the next time
54996 ** any btree routine is called.
54997 */
54998 static const void *fetchPayload(
54999   BtCursor *pCur,      /* Cursor pointing to entry to read from */
55000   u32 *pAmt            /* Write the number of available bytes here */
55001 ){
55002   assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
55003   assert( pCur->eState==CURSOR_VALID );
55004   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55005   assert( cursorHoldsMutex(pCur) );
55006   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
55007   if( pCur->info.nSize==0 ){
55008     btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
55009                    &pCur->info);
55010   }
55011   *pAmt = pCur->info.nLocal;
55012   return (void*)(pCur->info.pCell + pCur->info.nHeader);
55013 }
55014 
55015 
55016 /*
55017 ** For the entry that cursor pCur is point to, return as
55018 ** many bytes of the key or data as are available on the local
55019 ** b-tree page.  Write the number of available bytes into *pAmt.
55020 **
55021 ** The pointer returned is ephemeral.  The key/data may move
55022 ** or be destroyed on the next call to any Btree routine,
55023 ** including calls from other threads against the same cache.
55024 ** Hence, a mutex on the BtShared should be held prior to calling
55025 ** this routine.
55026 **
55027 ** These routines is used to get quick access to key and data
55028 ** in the common case where no overflow pages are used.
55029 */
55030 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
55031   return fetchPayload(pCur, pAmt);
55032 }
55033 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
55034   return fetchPayload(pCur, pAmt);
55035 }
55036 
55037 
55038 /*
55039 ** Move the cursor down to a new child page.  The newPgno argument is the
55040 ** page number of the child page to move to.
55041 **
55042 ** This function returns SQLITE_CORRUPT if the page-header flags field of
55043 ** the new child page does not match the flags field of the parent (i.e.
55044 ** if an intkey page appears to be the parent of a non-intkey page, or
55045 ** vice-versa).
55046 */
55047 static int moveToChild(BtCursor *pCur, u32 newPgno){
55048   int rc;
55049   int i = pCur->iPage;
55050   MemPage *pNewPage;
55051   BtShared *pBt = pCur->pBt;
55052 
55053   assert( cursorHoldsMutex(pCur) );
55054   assert( pCur->eState==CURSOR_VALID );
55055   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
55056   assert( pCur->iPage>=0 );
55057   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
55058     return SQLITE_CORRUPT_BKPT;
55059   }
55060   rc = getAndInitPage(pBt, newPgno, &pNewPage,
55061                pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
55062   if( rc ) return rc;
55063   pCur->apPage[i+1] = pNewPage;
55064   pCur->aiIdx[i+1] = 0;
55065   pCur->iPage++;
55066 
55067   pCur->info.nSize = 0;
55068   pCur->validNKey = 0;
55069   if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
55070     return SQLITE_CORRUPT_BKPT;
55071   }
55072   return SQLITE_OK;
55073 }
55074 
55075 #if 0
55076 /*
55077 ** Page pParent is an internal (non-leaf) tree page. This function
55078 ** asserts that page number iChild is the left-child if the iIdx'th
55079 ** cell in page pParent. Or, if iIdx is equal to the total number of
55080 ** cells in pParent, that page number iChild is the right-child of
55081 ** the page.
55082 */
55083 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
55084   assert( iIdx<=pParent->nCell );
55085   if( iIdx==pParent->nCell ){
55086     assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
55087   }else{
55088     assert( get4byte(findCell(pParent, iIdx))==iChild );
55089   }
55090 }
55091 #else
55092 #  define assertParentIndex(x,y,z)
55093 #endif
55094 
55095 /*
55096 ** Move the cursor up to the parent page.
55097 **
55098 ** pCur->idx is set to the cell index that contains the pointer
55099 ** to the page we are coming from.  If we are coming from the
55100 ** right-most child page then pCur->idx is set to one more than
55101 ** the largest cell index.
55102 */
55103 static void moveToParent(BtCursor *pCur){
55104   assert( cursorHoldsMutex(pCur) );
55105   assert( pCur->eState==CURSOR_VALID );
55106   assert( pCur->iPage>0 );
55107   assert( pCur->apPage[pCur->iPage] );
55108 
55109   /* UPDATE: It is actually possible for the condition tested by the assert
55110   ** below to be untrue if the database file is corrupt. This can occur if
55111   ** one cursor has modified page pParent while a reference to it is held
55112   ** by a second cursor. Which can only happen if a single page is linked
55113   ** into more than one b-tree structure in a corrupt database.  */
55114 #if 0
55115   assertParentIndex(
55116     pCur->apPage[pCur->iPage-1],
55117     pCur->aiIdx[pCur->iPage-1],
55118     pCur->apPage[pCur->iPage]->pgno
55119   );
55120 #endif
55121   testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
55122 
55123   releasePage(pCur->apPage[pCur->iPage]);
55124   pCur->iPage--;
55125   pCur->info.nSize = 0;
55126   pCur->validNKey = 0;
55127 }
55128 
55129 /*
55130 ** Move the cursor to point to the root page of its b-tree structure.
55131 **
55132 ** If the table has a virtual root page, then the cursor is moved to point
55133 ** to the virtual root page instead of the actual root page. A table has a
55134 ** virtual root page when the actual root page contains no cells and a
55135 ** single child page. This can only happen with the table rooted at page 1.
55136 **
55137 ** If the b-tree structure is empty, the cursor state is set to
55138 ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
55139 ** cell located on the root (or virtual root) page and the cursor state
55140 ** is set to CURSOR_VALID.
55141 **
55142 ** If this function returns successfully, it may be assumed that the
55143 ** page-header flags indicate that the [virtual] root-page is the expected
55144 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
55145 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
55146 ** indicating a table b-tree, or if the caller did specify a KeyInfo
55147 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
55148 ** b-tree).
55149 */
55150 static int moveToRoot(BtCursor *pCur){
55151   MemPage *pRoot;
55152   int rc = SQLITE_OK;
55153 
55154   assert( cursorHoldsMutex(pCur) );
55155   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
55156   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
55157   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
55158   if( pCur->eState>=CURSOR_REQUIRESEEK ){
55159     if( pCur->eState==CURSOR_FAULT ){
55160       assert( pCur->skipNext!=SQLITE_OK );
55161       return pCur->skipNext;
55162     }
55163     sqlite3BtreeClearCursor(pCur);
55164   }
55165 
55166   if( pCur->iPage>=0 ){
55167     while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
55168   }else if( pCur->pgnoRoot==0 ){
55169     pCur->eState = CURSOR_INVALID;
55170     return SQLITE_OK;
55171   }else{
55172     rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
55173                         pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
55174     if( rc!=SQLITE_OK ){
55175       pCur->eState = CURSOR_INVALID;
55176       return rc;
55177     }
55178     pCur->iPage = 0;
55179   }
55180   pRoot = pCur->apPage[0];
55181   assert( pRoot->pgno==pCur->pgnoRoot );
55182 
55183   /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
55184   ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
55185   ** NULL, the caller expects a table b-tree. If this is not the case,
55186   ** return an SQLITE_CORRUPT error.
55187   **
55188   ** Earlier versions of SQLite assumed that this test could not fail
55189   ** if the root page was already loaded when this function was called (i.e.
55190   ** if pCur->iPage>=0). But this is not so if the database is corrupted
55191   ** in such a way that page pRoot is linked into a second b-tree table
55192   ** (or the freelist).  */
55193   assert( pRoot->intKey==1 || pRoot->intKey==0 );
55194   if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
55195     return SQLITE_CORRUPT_BKPT;
55196   }
55197 
55198   pCur->aiIdx[0] = 0;
55199   pCur->info.nSize = 0;
55200   pCur->atLast = 0;
55201   pCur->validNKey = 0;
55202 
55203   if( pRoot->nCell>0 ){
55204     pCur->eState = CURSOR_VALID;
55205   }else if( !pRoot->leaf ){
55206     Pgno subpage;
55207     if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
55208     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
55209     pCur->eState = CURSOR_VALID;
55210     rc = moveToChild(pCur, subpage);
55211   }else{
55212     pCur->eState = CURSOR_INVALID;
55213   }
55214   return rc;
55215 }
55216 
55217 /*
55218 ** Move the cursor down to the left-most leaf entry beneath the
55219 ** entry to which it is currently pointing.
55220 **
55221 ** The left-most leaf is the one with the smallest key - the first
55222 ** in ascending order.
55223 */
55224 static int moveToLeftmost(BtCursor *pCur){
55225   Pgno pgno;
55226   int rc = SQLITE_OK;
55227   MemPage *pPage;
55228 
55229   assert( cursorHoldsMutex(pCur) );
55230   assert( pCur->eState==CURSOR_VALID );
55231   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
55232     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
55233     pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
55234     rc = moveToChild(pCur, pgno);
55235   }
55236   return rc;
55237 }
55238 
55239 /*
55240 ** Move the cursor down to the right-most leaf entry beneath the
55241 ** page to which it is currently pointing.  Notice the difference
55242 ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
55243 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
55244 ** finds the right-most entry beneath the *page*.
55245 **
55246 ** The right-most entry is the one with the largest key - the last
55247 ** key in ascending order.
55248 */
55249 static int moveToRightmost(BtCursor *pCur){
55250   Pgno pgno;
55251   int rc = SQLITE_OK;
55252   MemPage *pPage = 0;
55253 
55254   assert( cursorHoldsMutex(pCur) );
55255   assert( pCur->eState==CURSOR_VALID );
55256   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
55257     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
55258     pCur->aiIdx[pCur->iPage] = pPage->nCell;
55259     rc = moveToChild(pCur, pgno);
55260   }
55261   if( rc==SQLITE_OK ){
55262     pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
55263     pCur->info.nSize = 0;
55264     pCur->validNKey = 0;
55265   }
55266   return rc;
55267 }
55268 
55269 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
55270 ** on success.  Set *pRes to 0 if the cursor actually points to something
55271 ** or set *pRes to 1 if the table is empty.
55272 */
55273 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
55274   int rc;
55275 
55276   assert( cursorHoldsMutex(pCur) );
55277   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55278   rc = moveToRoot(pCur);
55279   if( rc==SQLITE_OK ){
55280     if( pCur->eState==CURSOR_INVALID ){
55281       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
55282       *pRes = 1;
55283     }else{
55284       assert( pCur->apPage[pCur->iPage]->nCell>0 );
55285       *pRes = 0;
55286       rc = moveToLeftmost(pCur);
55287     }
55288   }
55289   return rc;
55290 }
55291 
55292 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
55293 ** on success.  Set *pRes to 0 if the cursor actually points to something
55294 ** or set *pRes to 1 if the table is empty.
55295 */
55296 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
55297   int rc;
55298 
55299   assert( cursorHoldsMutex(pCur) );
55300   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55301 
55302   /* If the cursor already points to the last entry, this is a no-op. */
55303   if( CURSOR_VALID==pCur->eState && pCur->atLast ){
55304 #ifdef SQLITE_DEBUG
55305     /* This block serves to assert() that the cursor really does point
55306     ** to the last entry in the b-tree. */
55307     int ii;
55308     for(ii=0; ii<pCur->iPage; ii++){
55309       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
55310     }
55311     assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
55312     assert( pCur->apPage[pCur->iPage]->leaf );
55313 #endif
55314     return SQLITE_OK;
55315   }
55316 
55317   rc = moveToRoot(pCur);
55318   if( rc==SQLITE_OK ){
55319     if( CURSOR_INVALID==pCur->eState ){
55320       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
55321       *pRes = 1;
55322     }else{
55323       assert( pCur->eState==CURSOR_VALID );
55324       *pRes = 0;
55325       rc = moveToRightmost(pCur);
55326       pCur->atLast = rc==SQLITE_OK ?1:0;
55327     }
55328   }
55329   return rc;
55330 }
55331 
55332 /* Move the cursor so that it points to an entry near the key
55333 ** specified by pIdxKey or intKey.   Return a success code.
55334 **
55335 ** For INTKEY tables, the intKey parameter is used.  pIdxKey
55336 ** must be NULL.  For index tables, pIdxKey is used and intKey
55337 ** is ignored.
55338 **
55339 ** If an exact match is not found, then the cursor is always
55340 ** left pointing at a leaf page which would hold the entry if it
55341 ** were present.  The cursor might point to an entry that comes
55342 ** before or after the key.
55343 **
55344 ** An integer is written into *pRes which is the result of
55345 ** comparing the key with the entry to which the cursor is
55346 ** pointing.  The meaning of the integer written into
55347 ** *pRes is as follows:
55348 **
55349 **     *pRes<0      The cursor is left pointing at an entry that
55350 **                  is smaller than intKey/pIdxKey or if the table is empty
55351 **                  and the cursor is therefore left point to nothing.
55352 **
55353 **     *pRes==0     The cursor is left pointing at an entry that
55354 **                  exactly matches intKey/pIdxKey.
55355 **
55356 **     *pRes>0      The cursor is left pointing at an entry that
55357 **                  is larger than intKey/pIdxKey.
55358 **
55359 */
55360 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
55361   BtCursor *pCur,          /* The cursor to be moved */
55362   UnpackedRecord *pIdxKey, /* Unpacked index key */
55363   i64 intKey,              /* The table key */
55364   int biasRight,           /* If true, bias the search to the high end */
55365   int *pRes                /* Write search results here */
55366 ){
55367   int rc;
55368   RecordCompare xRecordCompare;
55369 
55370   assert( cursorHoldsMutex(pCur) );
55371   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55372   assert( pRes );
55373   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
55374 
55375   /* If the cursor is already positioned at the point we are trying
55376   ** to move to, then just return without doing any work */
55377   if( pCur->eState==CURSOR_VALID && pCur->validNKey
55378    && pCur->apPage[0]->intKey
55379   ){
55380     if( pCur->info.nKey==intKey ){
55381       *pRes = 0;
55382       return SQLITE_OK;
55383     }
55384     if( pCur->atLast && pCur->info.nKey<intKey ){
55385       *pRes = -1;
55386       return SQLITE_OK;
55387     }
55388   }
55389 
55390   if( pIdxKey ){
55391     xRecordCompare = sqlite3VdbeFindCompare(pIdxKey);
55392     assert( pIdxKey->default_rc==1
55393          || pIdxKey->default_rc==0
55394          || pIdxKey->default_rc==-1
55395     );
55396   }else{
55397     xRecordCompare = 0; /* All keys are integers */
55398   }
55399 
55400   rc = moveToRoot(pCur);
55401   if( rc ){
55402     return rc;
55403   }
55404   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
55405   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
55406   assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
55407   if( pCur->eState==CURSOR_INVALID ){
55408     *pRes = -1;
55409     assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
55410     return SQLITE_OK;
55411   }
55412   assert( pCur->apPage[0]->intKey || pIdxKey );
55413   for(;;){
55414     int lwr, upr, idx, c;
55415     Pgno chldPg;
55416     MemPage *pPage = pCur->apPage[pCur->iPage];
55417     u8 *pCell;                          /* Pointer to current cell in pPage */
55418 
55419     /* pPage->nCell must be greater than zero. If this is the root-page
55420     ** the cursor would have been INVALID above and this for(;;) loop
55421     ** not run. If this is not the root-page, then the moveToChild() routine
55422     ** would have already detected db corruption. Similarly, pPage must
55423     ** be the right kind (index or table) of b-tree page. Otherwise
55424     ** a moveToChild() or moveToRoot() call would have detected corruption.  */
55425     assert( pPage->nCell>0 );
55426     assert( pPage->intKey==(pIdxKey==0) );
55427     lwr = 0;
55428     upr = pPage->nCell-1;
55429     assert( biasRight==0 || biasRight==1 );
55430     idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
55431     pCur->aiIdx[pCur->iPage] = (u16)idx;
55432     if( xRecordCompare==0 ){
55433       for(;;){
55434         i64 nCellKey;
55435         pCell = findCell(pPage, idx) + pPage->childPtrSize;
55436         if( pPage->hasData ){
55437           while( 0x80 <= *(pCell++) ){
55438             if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
55439           }
55440         }
55441         getVarint(pCell, (u64*)&nCellKey);
55442         if( nCellKey<intKey ){
55443           lwr = idx+1;
55444           if( lwr>upr ){ c = -1; break; }
55445         }else if( nCellKey>intKey ){
55446           upr = idx-1;
55447           if( lwr>upr ){ c = +1; break; }
55448         }else{
55449           assert( nCellKey==intKey );
55450           pCur->validNKey = 1;
55451           pCur->info.nKey = nCellKey;
55452           pCur->aiIdx[pCur->iPage] = (u16)idx;
55453           if( !pPage->leaf ){
55454             lwr = idx;
55455             goto moveto_next_layer;
55456           }else{
55457             *pRes = 0;
55458             rc = SQLITE_OK;
55459             goto moveto_finish;
55460           }
55461         }
55462         assert( lwr+upr>=0 );
55463         idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2; */
55464       }
55465     }else{
55466       for(;;){
55467         int nCell;
55468         pCell = findCell(pPage, idx) + pPage->childPtrSize;
55469 
55470         /* The maximum supported page-size is 65536 bytes. This means that
55471         ** the maximum number of record bytes stored on an index B-Tree
55472         ** page is less than 16384 bytes and may be stored as a 2-byte
55473         ** varint. This information is used to attempt to avoid parsing
55474         ** the entire cell by checking for the cases where the record is
55475         ** stored entirely within the b-tree page by inspecting the first
55476         ** 2 bytes of the cell.
55477         */
55478         nCell = pCell[0];
55479         if( nCell<=pPage->max1bytePayload ){
55480           /* This branch runs if the record-size field of the cell is a
55481           ** single byte varint and the record fits entirely on the main
55482           ** b-tree page.  */
55483           testcase( pCell+nCell+1==pPage->aDataEnd );
55484           c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0);
55485         }else if( !(pCell[1] & 0x80)
55486           && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
55487         ){
55488           /* The record-size field is a 2 byte varint and the record
55489           ** fits entirely on the main b-tree page.  */
55490           testcase( pCell+nCell+2==pPage->aDataEnd );
55491           c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0);
55492         }else{
55493           /* The record flows over onto one or more overflow pages. In
55494           ** this case the whole cell needs to be parsed, a buffer allocated
55495           ** and accessPayload() used to retrieve the record into the
55496           ** buffer before VdbeRecordCompare() can be called. */
55497           void *pCellKey;
55498           u8 * const pCellBody = pCell - pPage->childPtrSize;
55499           btreeParseCellPtr(pPage, pCellBody, &pCur->info);
55500           nCell = (int)pCur->info.nKey;
55501           pCellKey = sqlite3Malloc( nCell );
55502           if( pCellKey==0 ){
55503             rc = SQLITE_NOMEM;
55504             goto moveto_finish;
55505           }
55506           pCur->aiIdx[pCur->iPage] = (u16)idx;
55507           rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
55508           if( rc ){
55509             sqlite3_free(pCellKey);
55510             goto moveto_finish;
55511           }
55512           c = xRecordCompare(nCell, pCellKey, pIdxKey, 0);
55513           sqlite3_free(pCellKey);
55514         }
55515         if( c<0 ){
55516           lwr = idx+1;
55517         }else if( c>0 ){
55518           upr = idx-1;
55519         }else{
55520           assert( c==0 );
55521           *pRes = 0;
55522           rc = SQLITE_OK;
55523           pCur->aiIdx[pCur->iPage] = (u16)idx;
55524           goto moveto_finish;
55525         }
55526         if( lwr>upr ) break;
55527         assert( lwr+upr>=0 );
55528         idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
55529       }
55530     }
55531     assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
55532     assert( pPage->isInit );
55533     if( pPage->leaf ){
55534       assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
55535       pCur->aiIdx[pCur->iPage] = (u16)idx;
55536       *pRes = c;
55537       rc = SQLITE_OK;
55538       goto moveto_finish;
55539     }
55540 moveto_next_layer:
55541     if( lwr>=pPage->nCell ){
55542       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
55543     }else{
55544       chldPg = get4byte(findCell(pPage, lwr));
55545     }
55546     pCur->aiIdx[pCur->iPage] = (u16)lwr;
55547     rc = moveToChild(pCur, chldPg);
55548     if( rc ) break;
55549   }
55550 moveto_finish:
55551   pCur->info.nSize = 0;
55552   pCur->validNKey = 0;
55553   return rc;
55554 }
55555 
55556 
55557 /*
55558 ** Return TRUE if the cursor is not pointing at an entry of the table.
55559 **
55560 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
55561 ** past the last entry in the table or sqlite3BtreePrev() moves past
55562 ** the first entry.  TRUE is also returned if the table is empty.
55563 */
55564 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
55565   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
55566   ** have been deleted? This API will need to change to return an error code
55567   ** as well as the boolean result value.
55568   */
55569   return (CURSOR_VALID!=pCur->eState);
55570 }
55571 
55572 /*
55573 ** Advance the cursor to the next entry in the database.  If
55574 ** successful then set *pRes=0.  If the cursor
55575 ** was already pointing to the last entry in the database before
55576 ** this routine was called, then set *pRes=1.
55577 **
55578 ** The calling function will set *pRes to 0 or 1.  The initial *pRes value
55579 ** will be 1 if the cursor being stepped corresponds to an SQL index and
55580 ** if this routine could have been skipped if that SQL index had been
55581 ** a unique index.  Otherwise the caller will have set *pRes to zero.
55582 ** Zero is the common case. The btree implementation is free to use the
55583 ** initial *pRes value as a hint to improve performance, but the current
55584 ** SQLite btree implementation does not. (Note that the comdb2 btree
55585 ** implementation does use this hint, however.)
55586 */
55587 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
55588   int rc;
55589   int idx;
55590   MemPage *pPage;
55591 
55592   assert( cursorHoldsMutex(pCur) );
55593   assert( pRes!=0 );
55594   assert( *pRes==0 || *pRes==1 );
55595   assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
55596   if( pCur->eState!=CURSOR_VALID ){
55597     rc = restoreCursorPosition(pCur);
55598     if( rc!=SQLITE_OK ){
55599       *pRes = 0;
55600       return rc;
55601     }
55602     if( CURSOR_INVALID==pCur->eState ){
55603       *pRes = 1;
55604       return SQLITE_OK;
55605     }
55606     if( pCur->skipNext ){
55607       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
55608       pCur->eState = CURSOR_VALID;
55609       if( pCur->skipNext>0 ){
55610         pCur->skipNext = 0;
55611         *pRes = 0;
55612         return SQLITE_OK;
55613       }
55614       pCur->skipNext = 0;
55615     }
55616   }
55617 
55618   pPage = pCur->apPage[pCur->iPage];
55619   idx = ++pCur->aiIdx[pCur->iPage];
55620   assert( pPage->isInit );
55621 
55622   /* If the database file is corrupt, it is possible for the value of idx
55623   ** to be invalid here. This can only occur if a second cursor modifies
55624   ** the page while cursor pCur is holding a reference to it. Which can
55625   ** only happen if the database is corrupt in such a way as to link the
55626   ** page into more than one b-tree structure. */
55627   testcase( idx>pPage->nCell );
55628 
55629   pCur->info.nSize = 0;
55630   pCur->validNKey = 0;
55631   if( idx>=pPage->nCell ){
55632     if( !pPage->leaf ){
55633       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
55634       if( rc ){
55635         *pRes = 0;
55636         return rc;
55637       }
55638       rc = moveToLeftmost(pCur);
55639       *pRes = 0;
55640       return rc;
55641     }
55642     do{
55643       if( pCur->iPage==0 ){
55644         *pRes = 1;
55645         pCur->eState = CURSOR_INVALID;
55646         return SQLITE_OK;
55647       }
55648       moveToParent(pCur);
55649       pPage = pCur->apPage[pCur->iPage];
55650     }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
55651     *pRes = 0;
55652     if( pPage->intKey ){
55653       rc = sqlite3BtreeNext(pCur, pRes);
55654     }else{
55655       rc = SQLITE_OK;
55656     }
55657     return rc;
55658   }
55659   *pRes = 0;
55660   if( pPage->leaf ){
55661     return SQLITE_OK;
55662   }
55663   rc = moveToLeftmost(pCur);
55664   return rc;
55665 }
55666 
55667 
55668 /*
55669 ** Step the cursor to the back to the previous entry in the database.  If
55670 ** successful then set *pRes=0.  If the cursor
55671 ** was already pointing to the first entry in the database before
55672 ** this routine was called, then set *pRes=1.
55673 **
55674 ** The calling function will set *pRes to 0 or 1.  The initial *pRes value
55675 ** will be 1 if the cursor being stepped corresponds to an SQL index and
55676 ** if this routine could have been skipped if that SQL index had been
55677 ** a unique index.  Otherwise the caller will have set *pRes to zero.
55678 ** Zero is the common case. The btree implementation is free to use the
55679 ** initial *pRes value as a hint to improve performance, but the current
55680 ** SQLite btree implementation does not. (Note that the comdb2 btree
55681 ** implementation does use this hint, however.)
55682 */
55683 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
55684   int rc;
55685   MemPage *pPage;
55686 
55687   assert( cursorHoldsMutex(pCur) );
55688   assert( pRes!=0 );
55689   assert( *pRes==0 || *pRes==1 );
55690   assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
55691   pCur->atLast = 0;
55692   if( pCur->eState!=CURSOR_VALID ){
55693     if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
55694       rc = btreeRestoreCursorPosition(pCur);
55695       if( rc!=SQLITE_OK ){
55696         *pRes = 0;
55697         return rc;
55698       }
55699     }
55700     if( CURSOR_INVALID==pCur->eState ){
55701       *pRes = 1;
55702       return SQLITE_OK;
55703     }
55704     if( pCur->skipNext ){
55705       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
55706       pCur->eState = CURSOR_VALID;
55707       if( pCur->skipNext<0 ){
55708         pCur->skipNext = 0;
55709         *pRes = 0;
55710         return SQLITE_OK;
55711       }
55712       pCur->skipNext = 0;
55713     }
55714   }
55715 
55716   pPage = pCur->apPage[pCur->iPage];
55717   assert( pPage->isInit );
55718   if( !pPage->leaf ){
55719     int idx = pCur->aiIdx[pCur->iPage];
55720     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
55721     if( rc ){
55722       *pRes = 0;
55723       return rc;
55724     }
55725     rc = moveToRightmost(pCur);
55726   }else{
55727     while( pCur->aiIdx[pCur->iPage]==0 ){
55728       if( pCur->iPage==0 ){
55729         pCur->eState = CURSOR_INVALID;
55730         *pRes = 1;
55731         return SQLITE_OK;
55732       }
55733       moveToParent(pCur);
55734     }
55735     pCur->info.nSize = 0;
55736     pCur->validNKey = 0;
55737 
55738     pCur->aiIdx[pCur->iPage]--;
55739     pPage = pCur->apPage[pCur->iPage];
55740     if( pPage->intKey && !pPage->leaf ){
55741       rc = sqlite3BtreePrevious(pCur, pRes);
55742     }else{
55743       rc = SQLITE_OK;
55744     }
55745   }
55746   *pRes = 0;
55747   return rc;
55748 }
55749 
55750 /*
55751 ** Allocate a new page from the database file.
55752 **
55753 ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
55754 ** has already been called on the new page.)  The new page has also
55755 ** been referenced and the calling routine is responsible for calling
55756 ** sqlite3PagerUnref() on the new page when it is done.
55757 **
55758 ** SQLITE_OK is returned on success.  Any other return value indicates
55759 ** an error.  *ppPage and *pPgno are undefined in the event of an error.
55760 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
55761 **
55762 ** If the "nearby" parameter is not 0, then an effort is made to
55763 ** locate a page close to the page number "nearby".  This can be used in an
55764 ** attempt to keep related pages close to each other in the database file,
55765 ** which in turn can make database access faster.
55766 **
55767 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
55768 ** anywhere on the free-list, then it is guaranteed to be returned.  If
55769 ** eMode is BTALLOC_LT then the page returned will be less than or equal
55770 ** to nearby if any such page exists.  If eMode is BTALLOC_ANY then there
55771 ** are no restrictions on which page is returned.
55772 */
55773 static int allocateBtreePage(
55774   BtShared *pBt,         /* The btree */
55775   MemPage **ppPage,      /* Store pointer to the allocated page here */
55776   Pgno *pPgno,           /* Store the page number here */
55777   Pgno nearby,           /* Search for a page near this one */
55778   u8 eMode               /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
55779 ){
55780   MemPage *pPage1;
55781   int rc;
55782   u32 n;     /* Number of pages on the freelist */
55783   u32 k;     /* Number of leaves on the trunk of the freelist */
55784   MemPage *pTrunk = 0;
55785   MemPage *pPrevTrunk = 0;
55786   Pgno mxPage;     /* Total size of the database file */
55787 
55788   assert( sqlite3_mutex_held(pBt->mutex) );
55789   assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
55790   pPage1 = pBt->pPage1;
55791   mxPage = btreePagecount(pBt);
55792   n = get4byte(&pPage1->aData[36]);
55793   testcase( n==mxPage-1 );
55794   if( n>=mxPage ){
55795     return SQLITE_CORRUPT_BKPT;
55796   }
55797   if( n>0 ){
55798     /* There are pages on the freelist.  Reuse one of those pages. */
55799     Pgno iTrunk;
55800     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
55801 
55802     /* If eMode==BTALLOC_EXACT and a query of the pointer-map
55803     ** shows that the page 'nearby' is somewhere on the free-list, then
55804     ** the entire-list will be searched for that page.
55805     */
55806 #ifndef SQLITE_OMIT_AUTOVACUUM
55807     if( eMode==BTALLOC_EXACT ){
55808       if( nearby<=mxPage ){
55809         u8 eType;
55810         assert( nearby>0 );
55811         assert( pBt->autoVacuum );
55812         rc = ptrmapGet(pBt, nearby, &eType, 0);
55813         if( rc ) return rc;
55814         if( eType==PTRMAP_FREEPAGE ){
55815           searchList = 1;
55816         }
55817       }
55818     }else if( eMode==BTALLOC_LE ){
55819       searchList = 1;
55820     }
55821 #endif
55822 
55823     /* Decrement the free-list count by 1. Set iTrunk to the index of the
55824     ** first free-list trunk page. iPrevTrunk is initially 1.
55825     */
55826     rc = sqlite3PagerWrite(pPage1->pDbPage);
55827     if( rc ) return rc;
55828     put4byte(&pPage1->aData[36], n-1);
55829 
55830     /* The code within this loop is run only once if the 'searchList' variable
55831     ** is not true. Otherwise, it runs once for each trunk-page on the
55832     ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
55833     ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
55834     */
55835     do {
55836       pPrevTrunk = pTrunk;
55837       if( pPrevTrunk ){
55838         iTrunk = get4byte(&pPrevTrunk->aData[0]);
55839       }else{
55840         iTrunk = get4byte(&pPage1->aData[32]);
55841       }
55842       testcase( iTrunk==mxPage );
55843       if( iTrunk>mxPage ){
55844         rc = SQLITE_CORRUPT_BKPT;
55845       }else{
55846         rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
55847       }
55848       if( rc ){
55849         pTrunk = 0;
55850         goto end_allocate_page;
55851       }
55852       assert( pTrunk!=0 );
55853       assert( pTrunk->aData!=0 );
55854 
55855       k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
55856       if( k==0 && !searchList ){
55857         /* The trunk has no leaves and the list is not being searched.
55858         ** So extract the trunk page itself and use it as the newly
55859         ** allocated page */
55860         assert( pPrevTrunk==0 );
55861         rc = sqlite3PagerWrite(pTrunk->pDbPage);
55862         if( rc ){
55863           goto end_allocate_page;
55864         }
55865         *pPgno = iTrunk;
55866         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
55867         *ppPage = pTrunk;
55868         pTrunk = 0;
55869         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
55870       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
55871         /* Value of k is out of range.  Database corruption */
55872         rc = SQLITE_CORRUPT_BKPT;
55873         goto end_allocate_page;
55874 #ifndef SQLITE_OMIT_AUTOVACUUM
55875       }else if( searchList
55876             && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
55877       ){
55878         /* The list is being searched and this trunk page is the page
55879         ** to allocate, regardless of whether it has leaves.
55880         */
55881         *pPgno = iTrunk;
55882         *ppPage = pTrunk;
55883         searchList = 0;
55884         rc = sqlite3PagerWrite(pTrunk->pDbPage);
55885         if( rc ){
55886           goto end_allocate_page;
55887         }
55888         if( k==0 ){
55889           if( !pPrevTrunk ){
55890             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
55891           }else{
55892             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
55893             if( rc!=SQLITE_OK ){
55894               goto end_allocate_page;
55895             }
55896             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
55897           }
55898         }else{
55899           /* The trunk page is required by the caller but it contains
55900           ** pointers to free-list leaves. The first leaf becomes a trunk
55901           ** page in this case.
55902           */
55903           MemPage *pNewTrunk;
55904           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
55905           if( iNewTrunk>mxPage ){
55906             rc = SQLITE_CORRUPT_BKPT;
55907             goto end_allocate_page;
55908           }
55909           testcase( iNewTrunk==mxPage );
55910           rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
55911           if( rc!=SQLITE_OK ){
55912             goto end_allocate_page;
55913           }
55914           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
55915           if( rc!=SQLITE_OK ){
55916             releasePage(pNewTrunk);
55917             goto end_allocate_page;
55918           }
55919           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
55920           put4byte(&pNewTrunk->aData[4], k-1);
55921           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
55922           releasePage(pNewTrunk);
55923           if( !pPrevTrunk ){
55924             assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
55925             put4byte(&pPage1->aData[32], iNewTrunk);
55926           }else{
55927             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
55928             if( rc ){
55929               goto end_allocate_page;
55930             }
55931             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
55932           }
55933         }
55934         pTrunk = 0;
55935         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
55936 #endif
55937       }else if( k>0 ){
55938         /* Extract a leaf from the trunk */
55939         u32 closest;
55940         Pgno iPage;
55941         unsigned char *aData = pTrunk->aData;
55942         if( nearby>0 ){
55943           u32 i;
55944           closest = 0;
55945           if( eMode==BTALLOC_LE ){
55946             for(i=0; i<k; i++){
55947               iPage = get4byte(&aData[8+i*4]);
55948               if( iPage<=nearby ){
55949                 closest = i;
55950                 break;
55951               }
55952             }
55953           }else{
55954             int dist;
55955             dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
55956             for(i=1; i<k; i++){
55957               int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
55958               if( d2<dist ){
55959                 closest = i;
55960                 dist = d2;
55961               }
55962             }
55963           }
55964         }else{
55965           closest = 0;
55966         }
55967 
55968         iPage = get4byte(&aData[8+closest*4]);
55969         testcase( iPage==mxPage );
55970         if( iPage>mxPage ){
55971           rc = SQLITE_CORRUPT_BKPT;
55972           goto end_allocate_page;
55973         }
55974         testcase( iPage==mxPage );
55975         if( !searchList
55976          || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
55977         ){
55978           int noContent;
55979           *pPgno = iPage;
55980           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
55981                  ": %d more free pages\n",
55982                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
55983           rc = sqlite3PagerWrite(pTrunk->pDbPage);
55984           if( rc ) goto end_allocate_page;
55985           if( closest<k-1 ){
55986             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
55987           }
55988           put4byte(&aData[4], k-1);
55989           noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
55990           rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
55991           if( rc==SQLITE_OK ){
55992             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
55993             if( rc!=SQLITE_OK ){
55994               releasePage(*ppPage);
55995             }
55996           }
55997           searchList = 0;
55998         }
55999       }
56000       releasePage(pPrevTrunk);
56001       pPrevTrunk = 0;
56002     }while( searchList );
56003   }else{
56004     /* There are no pages on the freelist, so append a new page to the
56005     ** database image.
56006     **
56007     ** Normally, new pages allocated by this block can be requested from the
56008     ** pager layer with the 'no-content' flag set. This prevents the pager
56009     ** from trying to read the pages content from disk. However, if the
56010     ** current transaction has already run one or more incremental-vacuum
56011     ** steps, then the page we are about to allocate may contain content
56012     ** that is required in the event of a rollback. In this case, do
56013     ** not set the no-content flag. This causes the pager to load and journal
56014     ** the current page content before overwriting it.
56015     **
56016     ** Note that the pager will not actually attempt to load or journal
56017     ** content for any page that really does lie past the end of the database
56018     ** file on disk. So the effects of disabling the no-content optimization
56019     ** here are confined to those pages that lie between the end of the
56020     ** database image and the end of the database file.
56021     */
56022     int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
56023 
56024     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
56025     if( rc ) return rc;
56026     pBt->nPage++;
56027     if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
56028 
56029 #ifndef SQLITE_OMIT_AUTOVACUUM
56030     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
56031       /* If *pPgno refers to a pointer-map page, allocate two new pages
56032       ** at the end of the file instead of one. The first allocated page
56033       ** becomes a new pointer-map page, the second is used by the caller.
56034       */
56035       MemPage *pPg = 0;
56036       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
56037       assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
56038       rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
56039       if( rc==SQLITE_OK ){
56040         rc = sqlite3PagerWrite(pPg->pDbPage);
56041         releasePage(pPg);
56042       }
56043       if( rc ) return rc;
56044       pBt->nPage++;
56045       if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
56046     }
56047 #endif
56048     put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
56049     *pPgno = pBt->nPage;
56050 
56051     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
56052     rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
56053     if( rc ) return rc;
56054     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
56055     if( rc!=SQLITE_OK ){
56056       releasePage(*ppPage);
56057     }
56058     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
56059   }
56060 
56061   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
56062 
56063 end_allocate_page:
56064   releasePage(pTrunk);
56065   releasePage(pPrevTrunk);
56066   if( rc==SQLITE_OK ){
56067     if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
56068       releasePage(*ppPage);
56069       *ppPage = 0;
56070       return SQLITE_CORRUPT_BKPT;
56071     }
56072     (*ppPage)->isInit = 0;
56073   }else{
56074     *ppPage = 0;
56075   }
56076   assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
56077   return rc;
56078 }
56079 
56080 /*
56081 ** This function is used to add page iPage to the database file free-list.
56082 ** It is assumed that the page is not already a part of the free-list.
56083 **
56084 ** The value passed as the second argument to this function is optional.
56085 ** If the caller happens to have a pointer to the MemPage object
56086 ** corresponding to page iPage handy, it may pass it as the second value.
56087 ** Otherwise, it may pass NULL.
56088 **
56089 ** If a pointer to a MemPage object is passed as the second argument,
56090 ** its reference count is not altered by this function.
56091 */
56092 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
56093   MemPage *pTrunk = 0;                /* Free-list trunk page */
56094   Pgno iTrunk = 0;                    /* Page number of free-list trunk page */
56095   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
56096   MemPage *pPage;                     /* Page being freed. May be NULL. */
56097   int rc;                             /* Return Code */
56098   int nFree;                          /* Initial number of pages on free-list */
56099 
56100   assert( sqlite3_mutex_held(pBt->mutex) );
56101   assert( iPage>1 );
56102   assert( !pMemPage || pMemPage->pgno==iPage );
56103 
56104   if( pMemPage ){
56105     pPage = pMemPage;
56106     sqlite3PagerRef(pPage->pDbPage);
56107   }else{
56108     pPage = btreePageLookup(pBt, iPage);
56109   }
56110 
56111   /* Increment the free page count on pPage1 */
56112   rc = sqlite3PagerWrite(pPage1->pDbPage);
56113   if( rc ) goto freepage_out;
56114   nFree = get4byte(&pPage1->aData[36]);
56115   put4byte(&pPage1->aData[36], nFree+1);
56116 
56117   if( pBt->btsFlags & BTS_SECURE_DELETE ){
56118     /* If the secure_delete option is enabled, then
56119     ** always fully overwrite deleted information with zeros.
56120     */
56121     if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
56122      ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
56123     ){
56124       goto freepage_out;
56125     }
56126     memset(pPage->aData, 0, pPage->pBt->pageSize);
56127   }
56128 
56129   /* If the database supports auto-vacuum, write an entry in the pointer-map
56130   ** to indicate that the page is free.
56131   */
56132   if( ISAUTOVACUUM ){
56133     ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
56134     if( rc ) goto freepage_out;
56135   }
56136 
56137   /* Now manipulate the actual database free-list structure. There are two
56138   ** possibilities. If the free-list is currently empty, or if the first
56139   ** trunk page in the free-list is full, then this page will become a
56140   ** new free-list trunk page. Otherwise, it will become a leaf of the
56141   ** first trunk page in the current free-list. This block tests if it
56142   ** is possible to add the page as a new free-list leaf.
56143   */
56144   if( nFree!=0 ){
56145     u32 nLeaf;                /* Initial number of leaf cells on trunk page */
56146 
56147     iTrunk = get4byte(&pPage1->aData[32]);
56148     rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
56149     if( rc!=SQLITE_OK ){
56150       goto freepage_out;
56151     }
56152 
56153     nLeaf = get4byte(&pTrunk->aData[4]);
56154     assert( pBt->usableSize>32 );
56155     if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
56156       rc = SQLITE_CORRUPT_BKPT;
56157       goto freepage_out;
56158     }
56159     if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
56160       /* In this case there is room on the trunk page to insert the page
56161       ** being freed as a new leaf.
56162       **
56163       ** Note that the trunk page is not really full until it contains
56164       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
56165       ** coded.  But due to a coding error in versions of SQLite prior to
56166       ** 3.6.0, databases with freelist trunk pages holding more than
56167       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
56168       ** to maintain backwards compatibility with older versions of SQLite,
56169       ** we will continue to restrict the number of entries to usableSize/4 - 8
56170       ** for now.  At some point in the future (once everyone has upgraded
56171       ** to 3.6.0 or later) we should consider fixing the conditional above
56172       ** to read "usableSize/4-2" instead of "usableSize/4-8".
56173       */
56174       rc = sqlite3PagerWrite(pTrunk->pDbPage);
56175       if( rc==SQLITE_OK ){
56176         put4byte(&pTrunk->aData[4], nLeaf+1);
56177         put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
56178         if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
56179           sqlite3PagerDontWrite(pPage->pDbPage);
56180         }
56181         rc = btreeSetHasContent(pBt, iPage);
56182       }
56183       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
56184       goto freepage_out;
56185     }
56186   }
56187 
56188   /* If control flows to this point, then it was not possible to add the
56189   ** the page being freed as a leaf page of the first trunk in the free-list.
56190   ** Possibly because the free-list is empty, or possibly because the
56191   ** first trunk in the free-list is full. Either way, the page being freed
56192   ** will become the new first trunk page in the free-list.
56193   */
56194   if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
56195     goto freepage_out;
56196   }
56197   rc = sqlite3PagerWrite(pPage->pDbPage);
56198   if( rc!=SQLITE_OK ){
56199     goto freepage_out;
56200   }
56201   put4byte(pPage->aData, iTrunk);
56202   put4byte(&pPage->aData[4], 0);
56203   put4byte(&pPage1->aData[32], iPage);
56204   TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
56205 
56206 freepage_out:
56207   if( pPage ){
56208     pPage->isInit = 0;
56209   }
56210   releasePage(pPage);
56211   releasePage(pTrunk);
56212   return rc;
56213 }
56214 static void freePage(MemPage *pPage, int *pRC){
56215   if( (*pRC)==SQLITE_OK ){
56216     *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
56217   }
56218 }
56219 
56220 /*
56221 ** Free any overflow pages associated with the given Cell.
56222 */
56223 static int clearCell(MemPage *pPage, unsigned char *pCell){
56224   BtShared *pBt = pPage->pBt;
56225   CellInfo info;
56226   Pgno ovflPgno;
56227   int rc;
56228   int nOvfl;
56229   u32 ovflPageSize;
56230 
56231   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56232   btreeParseCellPtr(pPage, pCell, &info);
56233   if( info.iOverflow==0 ){
56234     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
56235   }
56236   if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
56237     return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
56238   }
56239   ovflPgno = get4byte(&pCell[info.iOverflow]);
56240   assert( pBt->usableSize > 4 );
56241   ovflPageSize = pBt->usableSize - 4;
56242   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
56243   assert( ovflPgno==0 || nOvfl>0 );
56244   while( nOvfl-- ){
56245     Pgno iNext = 0;
56246     MemPage *pOvfl = 0;
56247     if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
56248       /* 0 is not a legal page number and page 1 cannot be an
56249       ** overflow page. Therefore if ovflPgno<2 or past the end of the
56250       ** file the database must be corrupt. */
56251       return SQLITE_CORRUPT_BKPT;
56252     }
56253     if( nOvfl ){
56254       rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
56255       if( rc ) return rc;
56256     }
56257 
56258     if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
56259      && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
56260     ){
56261       /* There is no reason any cursor should have an outstanding reference
56262       ** to an overflow page belonging to a cell that is being deleted/updated.
56263       ** So if there exists more than one reference to this page, then it
56264       ** must not really be an overflow page and the database must be corrupt.
56265       ** It is helpful to detect this before calling freePage2(), as
56266       ** freePage2() may zero the page contents if secure-delete mode is
56267       ** enabled. If this 'overflow' page happens to be a page that the
56268       ** caller is iterating through or using in some other way, this
56269       ** can be problematic.
56270       */
56271       rc = SQLITE_CORRUPT_BKPT;
56272     }else{
56273       rc = freePage2(pBt, pOvfl, ovflPgno);
56274     }
56275 
56276     if( pOvfl ){
56277       sqlite3PagerUnref(pOvfl->pDbPage);
56278     }
56279     if( rc ) return rc;
56280     ovflPgno = iNext;
56281   }
56282   return SQLITE_OK;
56283 }
56284 
56285 /*
56286 ** Create the byte sequence used to represent a cell on page pPage
56287 ** and write that byte sequence into pCell[].  Overflow pages are
56288 ** allocated and filled in as necessary.  The calling procedure
56289 ** is responsible for making sure sufficient space has been allocated
56290 ** for pCell[].
56291 **
56292 ** Note that pCell does not necessary need to point to the pPage->aData
56293 ** area.  pCell might point to some temporary storage.  The cell will
56294 ** be constructed in this temporary area then copied into pPage->aData
56295 ** later.
56296 */
56297 static int fillInCell(
56298   MemPage *pPage,                /* The page that contains the cell */
56299   unsigned char *pCell,          /* Complete text of the cell */
56300   const void *pKey, i64 nKey,    /* The key */
56301   const void *pData,int nData,   /* The data */
56302   int nZero,                     /* Extra zero bytes to append to pData */
56303   int *pnSize                    /* Write cell size here */
56304 ){
56305   int nPayload;
56306   const u8 *pSrc;
56307   int nSrc, n, rc;
56308   int spaceLeft;
56309   MemPage *pOvfl = 0;
56310   MemPage *pToRelease = 0;
56311   unsigned char *pPrior;
56312   unsigned char *pPayload;
56313   BtShared *pBt = pPage->pBt;
56314   Pgno pgnoOvfl = 0;
56315   int nHeader;
56316   CellInfo info;
56317 
56318   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56319 
56320   /* pPage is not necessarily writeable since pCell might be auxiliary
56321   ** buffer space that is separate from the pPage buffer area */
56322   assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
56323             || sqlite3PagerIswriteable(pPage->pDbPage) );
56324 
56325   /* Fill in the header. */
56326   nHeader = 0;
56327   if( !pPage->leaf ){
56328     nHeader += 4;
56329   }
56330   if( pPage->hasData ){
56331     nHeader += putVarint32(&pCell[nHeader], nData+nZero);
56332   }else{
56333     nData = nZero = 0;
56334   }
56335   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
56336   btreeParseCellPtr(pPage, pCell, &info);
56337   assert( info.nHeader==nHeader );
56338   assert( info.nKey==nKey );
56339   assert( info.nData==(u32)(nData+nZero) );
56340 
56341   /* Fill in the payload */
56342   nPayload = nData + nZero;
56343   if( pPage->intKey ){
56344     pSrc = pData;
56345     nSrc = nData;
56346     nData = 0;
56347   }else{
56348     if( NEVER(nKey>0x7fffffff || pKey==0) ){
56349       return SQLITE_CORRUPT_BKPT;
56350     }
56351     nPayload += (int)nKey;
56352     pSrc = pKey;
56353     nSrc = (int)nKey;
56354   }
56355   *pnSize = info.nSize;
56356   spaceLeft = info.nLocal;
56357   pPayload = &pCell[nHeader];
56358   pPrior = &pCell[info.iOverflow];
56359 
56360   while( nPayload>0 ){
56361     if( spaceLeft==0 ){
56362 #ifndef SQLITE_OMIT_AUTOVACUUM
56363       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
56364       if( pBt->autoVacuum ){
56365         do{
56366           pgnoOvfl++;
56367         } while(
56368           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
56369         );
56370       }
56371 #endif
56372       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
56373 #ifndef SQLITE_OMIT_AUTOVACUUM
56374       /* If the database supports auto-vacuum, and the second or subsequent
56375       ** overflow page is being allocated, add an entry to the pointer-map
56376       ** for that page now.
56377       **
56378       ** If this is the first overflow page, then write a partial entry
56379       ** to the pointer-map. If we write nothing to this pointer-map slot,
56380       ** then the optimistic overflow chain processing in clearCell()
56381       ** may misinterpret the uninitialized values and delete the
56382       ** wrong pages from the database.
56383       */
56384       if( pBt->autoVacuum && rc==SQLITE_OK ){
56385         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
56386         ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
56387         if( rc ){
56388           releasePage(pOvfl);
56389         }
56390       }
56391 #endif
56392       if( rc ){
56393         releasePage(pToRelease);
56394         return rc;
56395       }
56396 
56397       /* If pToRelease is not zero than pPrior points into the data area
56398       ** of pToRelease.  Make sure pToRelease is still writeable. */
56399       assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
56400 
56401       /* If pPrior is part of the data area of pPage, then make sure pPage
56402       ** is still writeable */
56403       assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
56404             || sqlite3PagerIswriteable(pPage->pDbPage) );
56405 
56406       put4byte(pPrior, pgnoOvfl);
56407       releasePage(pToRelease);
56408       pToRelease = pOvfl;
56409       pPrior = pOvfl->aData;
56410       put4byte(pPrior, 0);
56411       pPayload = &pOvfl->aData[4];
56412       spaceLeft = pBt->usableSize - 4;
56413     }
56414     n = nPayload;
56415     if( n>spaceLeft ) n = spaceLeft;
56416 
56417     /* If pToRelease is not zero than pPayload points into the data area
56418     ** of pToRelease.  Make sure pToRelease is still writeable. */
56419     assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
56420 
56421     /* If pPayload is part of the data area of pPage, then make sure pPage
56422     ** is still writeable */
56423     assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
56424             || sqlite3PagerIswriteable(pPage->pDbPage) );
56425 
56426     if( nSrc>0 ){
56427       if( n>nSrc ) n = nSrc;
56428       assert( pSrc );
56429       memcpy(pPayload, pSrc, n);
56430     }else{
56431       memset(pPayload, 0, n);
56432     }
56433     nPayload -= n;
56434     pPayload += n;
56435     pSrc += n;
56436     nSrc -= n;
56437     spaceLeft -= n;
56438     if( nSrc==0 ){
56439       nSrc = nData;
56440       pSrc = pData;
56441     }
56442   }
56443   releasePage(pToRelease);
56444   return SQLITE_OK;
56445 }
56446 
56447 /*
56448 ** Remove the i-th cell from pPage.  This routine effects pPage only.
56449 ** The cell content is not freed or deallocated.  It is assumed that
56450 ** the cell content has been copied someplace else.  This routine just
56451 ** removes the reference to the cell from pPage.
56452 **
56453 ** "sz" must be the number of bytes in the cell.
56454 */
56455 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
56456   u32 pc;         /* Offset to cell content of cell being deleted */
56457   u8 *data;       /* pPage->aData */
56458   u8 *ptr;        /* Used to move bytes around within data[] */
56459   int rc;         /* The return code */
56460   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
56461 
56462   if( *pRC ) return;
56463 
56464   assert( idx>=0 && idx<pPage->nCell );
56465   assert( sz==cellSize(pPage, idx) );
56466   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
56467   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56468   data = pPage->aData;
56469   ptr = &pPage->aCellIdx[2*idx];
56470   pc = get2byte(ptr);
56471   hdr = pPage->hdrOffset;
56472   testcase( pc==get2byte(&data[hdr+5]) );
56473   testcase( pc+sz==pPage->pBt->usableSize );
56474   if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
56475     *pRC = SQLITE_CORRUPT_BKPT;
56476     return;
56477   }
56478   rc = freeSpace(pPage, pc, sz);
56479   if( rc ){
56480     *pRC = rc;
56481     return;
56482   }
56483   pPage->nCell--;
56484   memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
56485   put2byte(&data[hdr+3], pPage->nCell);
56486   pPage->nFree += 2;
56487 }
56488 
56489 /*
56490 ** Insert a new cell on pPage at cell index "i".  pCell points to the
56491 ** content of the cell.
56492 **
56493 ** If the cell content will fit on the page, then put it there.  If it
56494 ** will not fit, then make a copy of the cell content into pTemp if
56495 ** pTemp is not null.  Regardless of pTemp, allocate a new entry
56496 ** in pPage->apOvfl[] and make it point to the cell content (either
56497 ** in pTemp or the original pCell) and also record its index.
56498 ** Allocating a new entry in pPage->aCell[] implies that
56499 ** pPage->nOverflow is incremented.
56500 **
56501 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
56502 ** cell. The caller will overwrite them after this function returns. If
56503 ** nSkip is non-zero, then pCell may not point to an invalid memory location
56504 ** (but pCell+nSkip is always valid).
56505 */
56506 static void insertCell(
56507   MemPage *pPage,   /* Page into which we are copying */
56508   int i,            /* New cell becomes the i-th cell of the page */
56509   u8 *pCell,        /* Content of the new cell */
56510   int sz,           /* Bytes of content in pCell */
56511   u8 *pTemp,        /* Temp storage space for pCell, if needed */
56512   Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
56513   int *pRC          /* Read and write return code from here */
56514 ){
56515   int idx = 0;      /* Where to write new cell content in data[] */
56516   int j;            /* Loop counter */
56517   int end;          /* First byte past the last cell pointer in data[] */
56518   int ins;          /* Index in data[] where new cell pointer is inserted */
56519   int cellOffset;   /* Address of first cell pointer in data[] */
56520   u8 *data;         /* The content of the whole page */
56521   int nSkip = (iChild ? 4 : 0);
56522 
56523   if( *pRC ) return;
56524 
56525   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
56526   assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
56527   assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
56528   assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
56529   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56530   /* The cell should normally be sized correctly.  However, when moving a
56531   ** malformed cell from a leaf page to an interior page, if the cell size
56532   ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
56533   ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
56534   ** the term after the || in the following assert(). */
56535   assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
56536   if( pPage->nOverflow || sz+2>pPage->nFree ){
56537     if( pTemp ){
56538       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
56539       pCell = pTemp;
56540     }
56541     if( iChild ){
56542       put4byte(pCell, iChild);
56543     }
56544     j = pPage->nOverflow++;
56545     assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
56546     pPage->apOvfl[j] = pCell;
56547     pPage->aiOvfl[j] = (u16)i;
56548   }else{
56549     int rc = sqlite3PagerWrite(pPage->pDbPage);
56550     if( rc!=SQLITE_OK ){
56551       *pRC = rc;
56552       return;
56553     }
56554     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
56555     data = pPage->aData;
56556     cellOffset = pPage->cellOffset;
56557     end = cellOffset + 2*pPage->nCell;
56558     ins = cellOffset + 2*i;
56559     rc = allocateSpace(pPage, sz, &idx);
56560     if( rc ){ *pRC = rc; return; }
56561     /* The allocateSpace() routine guarantees the following two properties
56562     ** if it returns success */
56563     assert( idx >= end+2 );
56564     assert( idx+sz <= (int)pPage->pBt->usableSize );
56565     pPage->nCell++;
56566     pPage->nFree -= (u16)(2 + sz);
56567     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
56568     if( iChild ){
56569       put4byte(&data[idx], iChild);
56570     }
56571     memmove(&data[ins+2], &data[ins], end-ins);
56572     put2byte(&data[ins], idx);
56573     put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
56574 #ifndef SQLITE_OMIT_AUTOVACUUM
56575     if( pPage->pBt->autoVacuum ){
56576       /* The cell may contain a pointer to an overflow page. If so, write
56577       ** the entry for the overflow page into the pointer map.
56578       */
56579       ptrmapPutOvflPtr(pPage, pCell, pRC);
56580     }
56581 #endif
56582   }
56583 }
56584 
56585 /*
56586 ** Add a list of cells to a page.  The page should be initially empty.
56587 ** The cells are guaranteed to fit on the page.
56588 */
56589 static void assemblePage(
56590   MemPage *pPage,   /* The page to be assemblied */
56591   int nCell,        /* The number of cells to add to this page */
56592   u8 **apCell,      /* Pointers to cell bodies */
56593   u16 *aSize        /* Sizes of the cells */
56594 ){
56595   int i;            /* Loop counter */
56596   u8 *pCellptr;     /* Address of next cell pointer */
56597   int cellbody;     /* Address of next cell body */
56598   u8 * const data = pPage->aData;             /* Pointer to data for pPage */
56599   const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
56600   const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
56601 
56602   assert( pPage->nOverflow==0 );
56603   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56604   assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
56605             && (int)MX_CELL(pPage->pBt)<=10921);
56606   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
56607 
56608   /* Check that the page has just been zeroed by zeroPage() */
56609   assert( pPage->nCell==0 );
56610   assert( get2byteNotZero(&data[hdr+5])==nUsable );
56611 
56612   pCellptr = &pPage->aCellIdx[nCell*2];
56613   cellbody = nUsable;
56614   for(i=nCell-1; i>=0; i--){
56615     u16 sz = aSize[i];
56616     pCellptr -= 2;
56617     cellbody -= sz;
56618     put2byte(pCellptr, cellbody);
56619     memcpy(&data[cellbody], apCell[i], sz);
56620   }
56621   put2byte(&data[hdr+3], nCell);
56622   put2byte(&data[hdr+5], cellbody);
56623   pPage->nFree -= (nCell*2 + nUsable - cellbody);
56624   pPage->nCell = (u16)nCell;
56625 }
56626 
56627 /*
56628 ** The following parameters determine how many adjacent pages get involved
56629 ** in a balancing operation.  NN is the number of neighbors on either side
56630 ** of the page that participate in the balancing operation.  NB is the
56631 ** total number of pages that participate, including the target page and
56632 ** NN neighbors on either side.
56633 **
56634 ** The minimum value of NN is 1 (of course).  Increasing NN above 1
56635 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
56636 ** in exchange for a larger degradation in INSERT and UPDATE performance.
56637 ** The value of NN appears to give the best results overall.
56638 */
56639 #define NN 1             /* Number of neighbors on either side of pPage */
56640 #define NB (NN*2+1)      /* Total pages involved in the balance */
56641 
56642 
56643 #ifndef SQLITE_OMIT_QUICKBALANCE
56644 /*
56645 ** This version of balance() handles the common special case where
56646 ** a new entry is being inserted on the extreme right-end of the
56647 ** tree, in other words, when the new entry will become the largest
56648 ** entry in the tree.
56649 **
56650 ** Instead of trying to balance the 3 right-most leaf pages, just add
56651 ** a new page to the right-hand side and put the one new entry in
56652 ** that page.  This leaves the right side of the tree somewhat
56653 ** unbalanced.  But odds are that we will be inserting new entries
56654 ** at the end soon afterwards so the nearly empty page will quickly
56655 ** fill up.  On average.
56656 **
56657 ** pPage is the leaf page which is the right-most page in the tree.
56658 ** pParent is its parent.  pPage must have a single overflow entry
56659 ** which is also the right-most entry on the page.
56660 **
56661 ** The pSpace buffer is used to store a temporary copy of the divider
56662 ** cell that will be inserted into pParent. Such a cell consists of a 4
56663 ** byte page number followed by a variable length integer. In other
56664 ** words, at most 13 bytes. Hence the pSpace buffer must be at
56665 ** least 13 bytes in size.
56666 */
56667 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
56668   BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
56669   MemPage *pNew;                       /* Newly allocated page */
56670   int rc;                              /* Return Code */
56671   Pgno pgnoNew;                        /* Page number of pNew */
56672 
56673   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56674   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
56675   assert( pPage->nOverflow==1 );
56676 
56677   /* This error condition is now caught prior to reaching this function */
56678   if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
56679 
56680   /* Allocate a new page. This page will become the right-sibling of
56681   ** pPage. Make the parent page writable, so that the new divider cell
56682   ** may be inserted. If both these operations are successful, proceed.
56683   */
56684   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
56685 
56686   if( rc==SQLITE_OK ){
56687 
56688     u8 *pOut = &pSpace[4];
56689     u8 *pCell = pPage->apOvfl[0];
56690     u16 szCell = cellSizePtr(pPage, pCell);
56691     u8 *pStop;
56692 
56693     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
56694     assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
56695     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
56696     assemblePage(pNew, 1, &pCell, &szCell);
56697 
56698     /* If this is an auto-vacuum database, update the pointer map
56699     ** with entries for the new page, and any pointer from the
56700     ** cell on the page to an overflow page. If either of these
56701     ** operations fails, the return code is set, but the contents
56702     ** of the parent page are still manipulated by thh code below.
56703     ** That is Ok, at this point the parent page is guaranteed to
56704     ** be marked as dirty. Returning an error code will cause a
56705     ** rollback, undoing any changes made to the parent page.
56706     */
56707     if( ISAUTOVACUUM ){
56708       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
56709       if( szCell>pNew->minLocal ){
56710         ptrmapPutOvflPtr(pNew, pCell, &rc);
56711       }
56712     }
56713 
56714     /* Create a divider cell to insert into pParent. The divider cell
56715     ** consists of a 4-byte page number (the page number of pPage) and
56716     ** a variable length key value (which must be the same value as the
56717     ** largest key on pPage).
56718     **
56719     ** To find the largest key value on pPage, first find the right-most
56720     ** cell on pPage. The first two fields of this cell are the
56721     ** record-length (a variable length integer at most 32-bits in size)
56722     ** and the key value (a variable length integer, may have any value).
56723     ** The first of the while(...) loops below skips over the record-length
56724     ** field. The second while(...) loop copies the key value from the
56725     ** cell on pPage into the pSpace buffer.
56726     */
56727     pCell = findCell(pPage, pPage->nCell-1);
56728     pStop = &pCell[9];
56729     while( (*(pCell++)&0x80) && pCell<pStop );
56730     pStop = &pCell[9];
56731     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
56732 
56733     /* Insert the new divider cell into pParent. */
56734     insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
56735                0, pPage->pgno, &rc);
56736 
56737     /* Set the right-child pointer of pParent to point to the new page. */
56738     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
56739 
56740     /* Release the reference to the new page. */
56741     releasePage(pNew);
56742   }
56743 
56744   return rc;
56745 }
56746 #endif /* SQLITE_OMIT_QUICKBALANCE */
56747 
56748 #if 0
56749 /*
56750 ** This function does not contribute anything to the operation of SQLite.
56751 ** it is sometimes activated temporarily while debugging code responsible
56752 ** for setting pointer-map entries.
56753 */
56754 static int ptrmapCheckPages(MemPage **apPage, int nPage){
56755   int i, j;
56756   for(i=0; i<nPage; i++){
56757     Pgno n;
56758     u8 e;
56759     MemPage *pPage = apPage[i];
56760     BtShared *pBt = pPage->pBt;
56761     assert( pPage->isInit );
56762 
56763     for(j=0; j<pPage->nCell; j++){
56764       CellInfo info;
56765       u8 *z;
56766 
56767       z = findCell(pPage, j);
56768       btreeParseCellPtr(pPage, z, &info);
56769       if( info.iOverflow ){
56770         Pgno ovfl = get4byte(&z[info.iOverflow]);
56771         ptrmapGet(pBt, ovfl, &e, &n);
56772         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
56773       }
56774       if( !pPage->leaf ){
56775         Pgno child = get4byte(z);
56776         ptrmapGet(pBt, child, &e, &n);
56777         assert( n==pPage->pgno && e==PTRMAP_BTREE );
56778       }
56779     }
56780     if( !pPage->leaf ){
56781       Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
56782       ptrmapGet(pBt, child, &e, &n);
56783       assert( n==pPage->pgno && e==PTRMAP_BTREE );
56784     }
56785   }
56786   return 1;
56787 }
56788 #endif
56789 
56790 /*
56791 ** This function is used to copy the contents of the b-tree node stored
56792 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
56793 ** the pointer-map entries for each child page are updated so that the
56794 ** parent page stored in the pointer map is page pTo. If pFrom contained
56795 ** any cells with overflow page pointers, then the corresponding pointer
56796 ** map entries are also updated so that the parent page is page pTo.
56797 **
56798 ** If pFrom is currently carrying any overflow cells (entries in the
56799 ** MemPage.apOvfl[] array), they are not copied to pTo.
56800 **
56801 ** Before returning, page pTo is reinitialized using btreeInitPage().
56802 **
56803 ** The performance of this function is not critical. It is only used by
56804 ** the balance_shallower() and balance_deeper() procedures, neither of
56805 ** which are called often under normal circumstances.
56806 */
56807 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
56808   if( (*pRC)==SQLITE_OK ){
56809     BtShared * const pBt = pFrom->pBt;
56810     u8 * const aFrom = pFrom->aData;
56811     u8 * const aTo = pTo->aData;
56812     int const iFromHdr = pFrom->hdrOffset;
56813     int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
56814     int rc;
56815     int iData;
56816 
56817 
56818     assert( pFrom->isInit );
56819     assert( pFrom->nFree>=iToHdr );
56820     assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
56821 
56822     /* Copy the b-tree node content from page pFrom to page pTo. */
56823     iData = get2byte(&aFrom[iFromHdr+5]);
56824     memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
56825     memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
56826 
56827     /* Reinitialize page pTo so that the contents of the MemPage structure
56828     ** match the new data. The initialization of pTo can actually fail under
56829     ** fairly obscure circumstances, even though it is a copy of initialized
56830     ** page pFrom.
56831     */
56832     pTo->isInit = 0;
56833     rc = btreeInitPage(pTo);
56834     if( rc!=SQLITE_OK ){
56835       *pRC = rc;
56836       return;
56837     }
56838 
56839     /* If this is an auto-vacuum database, update the pointer-map entries
56840     ** for any b-tree or overflow pages that pTo now contains the pointers to.
56841     */
56842     if( ISAUTOVACUUM ){
56843       *pRC = setChildPtrmaps(pTo);
56844     }
56845   }
56846 }
56847 
56848 /*
56849 ** This routine redistributes cells on the iParentIdx'th child of pParent
56850 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
56851 ** same amount of free space. Usually a single sibling on either side of the
56852 ** page are used in the balancing, though both siblings might come from one
56853 ** side if the page is the first or last child of its parent. If the page
56854 ** has fewer than 2 siblings (something which can only happen if the page
56855 ** is a root page or a child of a root page) then all available siblings
56856 ** participate in the balancing.
56857 **
56858 ** The number of siblings of the page might be increased or decreased by
56859 ** one or two in an effort to keep pages nearly full but not over full.
56860 **
56861 ** Note that when this routine is called, some of the cells on the page
56862 ** might not actually be stored in MemPage.aData[]. This can happen
56863 ** if the page is overfull. This routine ensures that all cells allocated
56864 ** to the page and its siblings fit into MemPage.aData[] before returning.
56865 **
56866 ** In the course of balancing the page and its siblings, cells may be
56867 ** inserted into or removed from the parent page (pParent). Doing so
56868 ** may cause the parent page to become overfull or underfull. If this
56869 ** happens, it is the responsibility of the caller to invoke the correct
56870 ** balancing routine to fix this problem (see the balance() routine).
56871 **
56872 ** If this routine fails for any reason, it might leave the database
56873 ** in a corrupted state. So if this routine fails, the database should
56874 ** be rolled back.
56875 **
56876 ** The third argument to this function, aOvflSpace, is a pointer to a
56877 ** buffer big enough to hold one page. If while inserting cells into the parent
56878 ** page (pParent) the parent page becomes overfull, this buffer is
56879 ** used to store the parent's overflow cells. Because this function inserts
56880 ** a maximum of four divider cells into the parent page, and the maximum
56881 ** size of a cell stored within an internal node is always less than 1/4
56882 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
56883 ** enough for all overflow cells.
56884 **
56885 ** If aOvflSpace is set to a null pointer, this function returns
56886 ** SQLITE_NOMEM.
56887 */
56888 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
56889 #pragma optimize("", off)
56890 #endif
56891 static int balance_nonroot(
56892   MemPage *pParent,               /* Parent page of siblings being balanced */
56893   int iParentIdx,                 /* Index of "the page" in pParent */
56894   u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
56895   int isRoot,                     /* True if pParent is a root-page */
56896   int bBulk                       /* True if this call is part of a bulk load */
56897 ){
56898   BtShared *pBt;               /* The whole database */
56899   int nCell = 0;               /* Number of cells in apCell[] */
56900   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
56901   int nNew = 0;                /* Number of pages in apNew[] */
56902   int nOld;                    /* Number of pages in apOld[] */
56903   int i, j, k;                 /* Loop counters */
56904   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
56905   int rc = SQLITE_OK;          /* The return code */
56906   u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
56907   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
56908   int usableSpace;             /* Bytes in pPage beyond the header */
56909   int pageFlags;               /* Value of pPage->aData[0] */
56910   int subtotal;                /* Subtotal of bytes in cells on one page */
56911   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
56912   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
56913   int szScratch;               /* Size of scratch memory requested */
56914   MemPage *apOld[NB];          /* pPage and up to two siblings */
56915   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
56916   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
56917   u8 *pRight;                  /* Location in parent of right-sibling pointer */
56918   u8 *apDiv[NB-1];             /* Divider cells in pParent */
56919   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
56920   int szNew[NB+2];             /* Combined size of cells place on i-th page */
56921   u8 **apCell = 0;             /* All cells begin balanced */
56922   u16 *szCell;                 /* Local size of all cells in apCell[] */
56923   u8 *aSpace1;                 /* Space for copies of dividers cells */
56924   Pgno pgno;                   /* Temp var to store a page number in */
56925 
56926   pBt = pParent->pBt;
56927   assert( sqlite3_mutex_held(pBt->mutex) );
56928   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
56929 
56930 #if 0
56931   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
56932 #endif
56933 
56934   /* At this point pParent may have at most one overflow cell. And if
56935   ** this overflow cell is present, it must be the cell with
56936   ** index iParentIdx. This scenario comes about when this function
56937   ** is called (indirectly) from sqlite3BtreeDelete().
56938   */
56939   assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
56940   assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
56941 
56942   if( !aOvflSpace ){
56943     return SQLITE_NOMEM;
56944   }
56945 
56946   /* Find the sibling pages to balance. Also locate the cells in pParent
56947   ** that divide the siblings. An attempt is made to find NN siblings on
56948   ** either side of pPage. More siblings are taken from one side, however,
56949   ** if there are fewer than NN siblings on the other side. If pParent
56950   ** has NB or fewer children then all children of pParent are taken.
56951   **
56952   ** This loop also drops the divider cells from the parent page. This
56953   ** way, the remainder of the function does not have to deal with any
56954   ** overflow cells in the parent page, since if any existed they will
56955   ** have already been removed.
56956   */
56957   i = pParent->nOverflow + pParent->nCell;
56958   if( i<2 ){
56959     nxDiv = 0;
56960   }else{
56961     assert( bBulk==0 || bBulk==1 );
56962     if( iParentIdx==0 ){
56963       nxDiv = 0;
56964     }else if( iParentIdx==i ){
56965       nxDiv = i-2+bBulk;
56966     }else{
56967       assert( bBulk==0 );
56968       nxDiv = iParentIdx-1;
56969     }
56970     i = 2-bBulk;
56971   }
56972   nOld = i+1;
56973   if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
56974     pRight = &pParent->aData[pParent->hdrOffset+8];
56975   }else{
56976     pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
56977   }
56978   pgno = get4byte(pRight);
56979   while( 1 ){
56980     rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
56981     if( rc ){
56982       memset(apOld, 0, (i+1)*sizeof(MemPage*));
56983       goto balance_cleanup;
56984     }
56985     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
56986     if( (i--)==0 ) break;
56987 
56988     if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
56989       apDiv[i] = pParent->apOvfl[0];
56990       pgno = get4byte(apDiv[i]);
56991       szNew[i] = cellSizePtr(pParent, apDiv[i]);
56992       pParent->nOverflow = 0;
56993     }else{
56994       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
56995       pgno = get4byte(apDiv[i]);
56996       szNew[i] = cellSizePtr(pParent, apDiv[i]);
56997 
56998       /* Drop the cell from the parent page. apDiv[i] still points to
56999       ** the cell within the parent, even though it has been dropped.
57000       ** This is safe because dropping a cell only overwrites the first
57001       ** four bytes of it, and this function does not need the first
57002       ** four bytes of the divider cell. So the pointer is safe to use
57003       ** later on.
57004       **
57005       ** But not if we are in secure-delete mode. In secure-delete mode,
57006       ** the dropCell() routine will overwrite the entire cell with zeroes.
57007       ** In this case, temporarily copy the cell into the aOvflSpace[]
57008       ** buffer. It will be copied out again as soon as the aSpace[] buffer
57009       ** is allocated.  */
57010       if( pBt->btsFlags & BTS_SECURE_DELETE ){
57011         int iOff;
57012 
57013         iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
57014         if( (iOff+szNew[i])>(int)pBt->usableSize ){
57015           rc = SQLITE_CORRUPT_BKPT;
57016           memset(apOld, 0, (i+1)*sizeof(MemPage*));
57017           goto balance_cleanup;
57018         }else{
57019           memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
57020           apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
57021         }
57022       }
57023       dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
57024     }
57025   }
57026 
57027   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
57028   ** alignment */
57029   nMaxCells = (nMaxCells + 3)&~3;
57030 
57031   /*
57032   ** Allocate space for memory structures
57033   */
57034   k = pBt->pageSize + ROUND8(sizeof(MemPage));
57035   szScratch =
57036        nMaxCells*sizeof(u8*)                       /* apCell */
57037      + nMaxCells*sizeof(u16)                       /* szCell */
57038      + pBt->pageSize                               /* aSpace1 */
57039      + k*nOld;                                     /* Page copies (apCopy) */
57040   apCell = sqlite3ScratchMalloc( szScratch );
57041   if( apCell==0 ){
57042     rc = SQLITE_NOMEM;
57043     goto balance_cleanup;
57044   }
57045   szCell = (u16*)&apCell[nMaxCells];
57046   aSpace1 = (u8*)&szCell[nMaxCells];
57047   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
57048 
57049   /*
57050   ** Load pointers to all cells on sibling pages and the divider cells
57051   ** into the local apCell[] array.  Make copies of the divider cells
57052   ** into space obtained from aSpace1[] and remove the divider cells
57053   ** from pParent.
57054   **
57055   ** If the siblings are on leaf pages, then the child pointers of the
57056   ** divider cells are stripped from the cells before they are copied
57057   ** into aSpace1[].  In this way, all cells in apCell[] are without
57058   ** child pointers.  If siblings are not leaves, then all cell in
57059   ** apCell[] include child pointers.  Either way, all cells in apCell[]
57060   ** are alike.
57061   **
57062   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
57063   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
57064   */
57065   leafCorrection = apOld[0]->leaf*4;
57066   leafData = apOld[0]->hasData;
57067   for(i=0; i<nOld; i++){
57068     int limit;
57069 
57070     /* Before doing anything else, take a copy of the i'th original sibling
57071     ** The rest of this function will use data from the copies rather
57072     ** that the original pages since the original pages will be in the
57073     ** process of being overwritten.  */
57074     MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
57075     memcpy(pOld, apOld[i], sizeof(MemPage));
57076     pOld->aData = (void*)&pOld[1];
57077     memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
57078 
57079     limit = pOld->nCell+pOld->nOverflow;
57080     if( pOld->nOverflow>0 ){
57081       for(j=0; j<limit; j++){
57082         assert( nCell<nMaxCells );
57083         apCell[nCell] = findOverflowCell(pOld, j);
57084         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
57085         nCell++;
57086       }
57087     }else{
57088       u8 *aData = pOld->aData;
57089       u16 maskPage = pOld->maskPage;
57090       u16 cellOffset = pOld->cellOffset;
57091       for(j=0; j<limit; j++){
57092         assert( nCell<nMaxCells );
57093         apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
57094         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
57095         nCell++;
57096       }
57097     }
57098     if( i<nOld-1 && !leafData){
57099       u16 sz = (u16)szNew[i];
57100       u8 *pTemp;
57101       assert( nCell<nMaxCells );
57102       szCell[nCell] = sz;
57103       pTemp = &aSpace1[iSpace1];
57104       iSpace1 += sz;
57105       assert( sz<=pBt->maxLocal+23 );
57106       assert( iSpace1 <= (int)pBt->pageSize );
57107       memcpy(pTemp, apDiv[i], sz);
57108       apCell[nCell] = pTemp+leafCorrection;
57109       assert( leafCorrection==0 || leafCorrection==4 );
57110       szCell[nCell] = szCell[nCell] - leafCorrection;
57111       if( !pOld->leaf ){
57112         assert( leafCorrection==0 );
57113         assert( pOld->hdrOffset==0 );
57114         /* The right pointer of the child page pOld becomes the left
57115         ** pointer of the divider cell */
57116         memcpy(apCell[nCell], &pOld->aData[8], 4);
57117       }else{
57118         assert( leafCorrection==4 );
57119         if( szCell[nCell]<4 ){
57120           /* Do not allow any cells smaller than 4 bytes. */
57121           szCell[nCell] = 4;
57122         }
57123       }
57124       nCell++;
57125     }
57126   }
57127 
57128   /*
57129   ** Figure out the number of pages needed to hold all nCell cells.
57130   ** Store this number in "k".  Also compute szNew[] which is the total
57131   ** size of all cells on the i-th page and cntNew[] which is the index
57132   ** in apCell[] of the cell that divides page i from page i+1.
57133   ** cntNew[k] should equal nCell.
57134   **
57135   ** Values computed by this block:
57136   **
57137   **           k: The total number of sibling pages
57138   **    szNew[i]: Spaced used on the i-th sibling page.
57139   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
57140   **              the right of the i-th sibling page.
57141   ** usableSpace: Number of bytes of space available on each sibling.
57142   **
57143   */
57144   usableSpace = pBt->usableSize - 12 + leafCorrection;
57145   for(subtotal=k=i=0; i<nCell; i++){
57146     assert( i<nMaxCells );
57147     subtotal += szCell[i] + 2;
57148     if( subtotal > usableSpace ){
57149       szNew[k] = subtotal - szCell[i];
57150       cntNew[k] = i;
57151       if( leafData ){ i--; }
57152       subtotal = 0;
57153       k++;
57154       if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
57155     }
57156   }
57157   szNew[k] = subtotal;
57158   cntNew[k] = nCell;
57159   k++;
57160 
57161   /*
57162   ** The packing computed by the previous block is biased toward the siblings
57163   ** on the left side.  The left siblings are always nearly full, while the
57164   ** right-most sibling might be nearly empty.  This block of code attempts
57165   ** to adjust the packing of siblings to get a better balance.
57166   **
57167   ** This adjustment is more than an optimization.  The packing above might
57168   ** be so out of balance as to be illegal.  For example, the right-most
57169   ** sibling might be completely empty.  This adjustment is not optional.
57170   */
57171   for(i=k-1; i>0; i--){
57172     int szRight = szNew[i];  /* Size of sibling on the right */
57173     int szLeft = szNew[i-1]; /* Size of sibling on the left */
57174     int r;              /* Index of right-most cell in left sibling */
57175     int d;              /* Index of first cell to the left of right sibling */
57176 
57177     r = cntNew[i-1] - 1;
57178     d = r + 1 - leafData;
57179     assert( d<nMaxCells );
57180     assert( r<nMaxCells );
57181     while( szRight==0
57182        || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2))
57183     ){
57184       szRight += szCell[d] + 2;
57185       szLeft -= szCell[r] + 2;
57186       cntNew[i-1]--;
57187       r = cntNew[i-1] - 1;
57188       d = r + 1 - leafData;
57189     }
57190     szNew[i] = szRight;
57191     szNew[i-1] = szLeft;
57192   }
57193 
57194   /* Either we found one or more cells (cntnew[0])>0) or pPage is
57195   ** a virtual root page.  A virtual root page is when the real root
57196   ** page is page 1 and we are the only child of that page.
57197   **
57198   ** UPDATE:  The assert() below is not necessarily true if the database
57199   ** file is corrupt.  The corruption will be detected and reported later
57200   ** in this procedure so there is no need to act upon it now.
57201   */
57202 #if 0
57203   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
57204 #endif
57205 
57206   TRACE(("BALANCE: old: %d %d %d  ",
57207     apOld[0]->pgno,
57208     nOld>=2 ? apOld[1]->pgno : 0,
57209     nOld>=3 ? apOld[2]->pgno : 0
57210   ));
57211 
57212   /*
57213   ** Allocate k new pages.  Reuse old pages where possible.
57214   */
57215   if( apOld[0]->pgno<=1 ){
57216     rc = SQLITE_CORRUPT_BKPT;
57217     goto balance_cleanup;
57218   }
57219   pageFlags = apOld[0]->aData[0];
57220   for(i=0; i<k; i++){
57221     MemPage *pNew;
57222     if( i<nOld ){
57223       pNew = apNew[i] = apOld[i];
57224       apOld[i] = 0;
57225       rc = sqlite3PagerWrite(pNew->pDbPage);
57226       nNew++;
57227       if( rc ) goto balance_cleanup;
57228     }else{
57229       assert( i>0 );
57230       rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
57231       if( rc ) goto balance_cleanup;
57232       apNew[i] = pNew;
57233       nNew++;
57234 
57235       /* Set the pointer-map entry for the new sibling page. */
57236       if( ISAUTOVACUUM ){
57237         ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
57238         if( rc!=SQLITE_OK ){
57239           goto balance_cleanup;
57240         }
57241       }
57242     }
57243   }
57244 
57245   /* Free any old pages that were not reused as new pages.
57246   */
57247   while( i<nOld ){
57248     freePage(apOld[i], &rc);
57249     if( rc ) goto balance_cleanup;
57250     releasePage(apOld[i]);
57251     apOld[i] = 0;
57252     i++;
57253   }
57254 
57255   /*
57256   ** Put the new pages in accending order.  This helps to
57257   ** keep entries in the disk file in order so that a scan
57258   ** of the table is a linear scan through the file.  That
57259   ** in turn helps the operating system to deliver pages
57260   ** from the disk more rapidly.
57261   **
57262   ** An O(n^2) insertion sort algorithm is used, but since
57263   ** n is never more than NB (a small constant), that should
57264   ** not be a problem.
57265   **
57266   ** When NB==3, this one optimization makes the database
57267   ** about 25% faster for large insertions and deletions.
57268   */
57269   for(i=0; i<k-1; i++){
57270     int minV = apNew[i]->pgno;
57271     int minI = i;
57272     for(j=i+1; j<k; j++){
57273       if( apNew[j]->pgno<(unsigned)minV ){
57274         minI = j;
57275         minV = apNew[j]->pgno;
57276       }
57277     }
57278     if( minI>i ){
57279       MemPage *pT;
57280       pT = apNew[i];
57281       apNew[i] = apNew[minI];
57282       apNew[minI] = pT;
57283     }
57284   }
57285   TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
57286     apNew[0]->pgno, szNew[0],
57287     nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
57288     nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
57289     nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
57290     nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
57291 
57292   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
57293   put4byte(pRight, apNew[nNew-1]->pgno);
57294 
57295   /*
57296   ** Evenly distribute the data in apCell[] across the new pages.
57297   ** Insert divider cells into pParent as necessary.
57298   */
57299   j = 0;
57300   for(i=0; i<nNew; i++){
57301     /* Assemble the new sibling page. */
57302     MemPage *pNew = apNew[i];
57303     assert( j<nMaxCells );
57304     zeroPage(pNew, pageFlags);
57305     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
57306     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
57307     assert( pNew->nOverflow==0 );
57308 
57309     j = cntNew[i];
57310 
57311     /* If the sibling page assembled above was not the right-most sibling,
57312     ** insert a divider cell into the parent page.
57313     */
57314     assert( i<nNew-1 || j==nCell );
57315     if( j<nCell ){
57316       u8 *pCell;
57317       u8 *pTemp;
57318       int sz;
57319 
57320       assert( j<nMaxCells );
57321       pCell = apCell[j];
57322       sz = szCell[j] + leafCorrection;
57323       pTemp = &aOvflSpace[iOvflSpace];
57324       if( !pNew->leaf ){
57325         memcpy(&pNew->aData[8], pCell, 4);
57326       }else if( leafData ){
57327         /* If the tree is a leaf-data tree, and the siblings are leaves,
57328         ** then there is no divider cell in apCell[]. Instead, the divider
57329         ** cell consists of the integer key for the right-most cell of
57330         ** the sibling-page assembled above only.
57331         */
57332         CellInfo info;
57333         j--;
57334         btreeParseCellPtr(pNew, apCell[j], &info);
57335         pCell = pTemp;
57336         sz = 4 + putVarint(&pCell[4], info.nKey);
57337         pTemp = 0;
57338       }else{
57339         pCell -= 4;
57340         /* Obscure case for non-leaf-data trees: If the cell at pCell was
57341         ** previously stored on a leaf node, and its reported size was 4
57342         ** bytes, then it may actually be smaller than this
57343         ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
57344         ** any cell). But it is important to pass the correct size to
57345         ** insertCell(), so reparse the cell now.
57346         **
57347         ** Note that this can never happen in an SQLite data file, as all
57348         ** cells are at least 4 bytes. It only happens in b-trees used
57349         ** to evaluate "IN (SELECT ...)" and similar clauses.
57350         */
57351         if( szCell[j]==4 ){
57352           assert(leafCorrection==4);
57353           sz = cellSizePtr(pParent, pCell);
57354         }
57355       }
57356       iOvflSpace += sz;
57357       assert( sz<=pBt->maxLocal+23 );
57358       assert( iOvflSpace <= (int)pBt->pageSize );
57359       insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
57360       if( rc!=SQLITE_OK ) goto balance_cleanup;
57361       assert( sqlite3PagerIswriteable(pParent->pDbPage) );
57362 
57363       j++;
57364       nxDiv++;
57365     }
57366   }
57367   assert( j==nCell );
57368   assert( nOld>0 );
57369   assert( nNew>0 );
57370   if( (pageFlags & PTF_LEAF)==0 ){
57371     u8 *zChild = &apCopy[nOld-1]->aData[8];
57372     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
57373   }
57374 
57375   if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
57376     /* The root page of the b-tree now contains no cells. The only sibling
57377     ** page is the right-child of the parent. Copy the contents of the
57378     ** child page into the parent, decreasing the overall height of the
57379     ** b-tree structure by one. This is described as the "balance-shallower"
57380     ** sub-algorithm in some documentation.
57381     **
57382     ** If this is an auto-vacuum database, the call to copyNodeContent()
57383     ** sets all pointer-map entries corresponding to database image pages
57384     ** for which the pointer is stored within the content being copied.
57385     **
57386     ** The second assert below verifies that the child page is defragmented
57387     ** (it must be, as it was just reconstructed using assemblePage()). This
57388     ** is important if the parent page happens to be page 1 of the database
57389     ** image.  */
57390     assert( nNew==1 );
57391     assert( apNew[0]->nFree ==
57392         (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
57393     );
57394     copyNodeContent(apNew[0], pParent, &rc);
57395     freePage(apNew[0], &rc);
57396   }else if( ISAUTOVACUUM ){
57397     /* Fix the pointer-map entries for all the cells that were shifted around.
57398     ** There are several different types of pointer-map entries that need to
57399     ** be dealt with by this routine. Some of these have been set already, but
57400     ** many have not. The following is a summary:
57401     **
57402     **   1) The entries associated with new sibling pages that were not
57403     **      siblings when this function was called. These have already
57404     **      been set. We don't need to worry about old siblings that were
57405     **      moved to the free-list - the freePage() code has taken care
57406     **      of those.
57407     **
57408     **   2) The pointer-map entries associated with the first overflow
57409     **      page in any overflow chains used by new divider cells. These
57410     **      have also already been taken care of by the insertCell() code.
57411     **
57412     **   3) If the sibling pages are not leaves, then the child pages of
57413     **      cells stored on the sibling pages may need to be updated.
57414     **
57415     **   4) If the sibling pages are not internal intkey nodes, then any
57416     **      overflow pages used by these cells may need to be updated
57417     **      (internal intkey nodes never contain pointers to overflow pages).
57418     **
57419     **   5) If the sibling pages are not leaves, then the pointer-map
57420     **      entries for the right-child pages of each sibling may need
57421     **      to be updated.
57422     **
57423     ** Cases 1 and 2 are dealt with above by other code. The next
57424     ** block deals with cases 3 and 4 and the one after that, case 5. Since
57425     ** setting a pointer map entry is a relatively expensive operation, this
57426     ** code only sets pointer map entries for child or overflow pages that have
57427     ** actually moved between pages.  */
57428     MemPage *pNew = apNew[0];
57429     MemPage *pOld = apCopy[0];
57430     int nOverflow = pOld->nOverflow;
57431     int iNextOld = pOld->nCell + nOverflow;
57432     int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1);
57433     j = 0;                             /* Current 'old' sibling page */
57434     k = 0;                             /* Current 'new' sibling page */
57435     for(i=0; i<nCell; i++){
57436       int isDivider = 0;
57437       while( i==iNextOld ){
57438         /* Cell i is the cell immediately following the last cell on old
57439         ** sibling page j. If the siblings are not leaf pages of an
57440         ** intkey b-tree, then cell i was a divider cell. */
57441         assert( j+1 < ArraySize(apCopy) );
57442         assert( j+1 < nOld );
57443         pOld = apCopy[++j];
57444         iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
57445         if( pOld->nOverflow ){
57446           nOverflow = pOld->nOverflow;
57447           iOverflow = i + !leafData + pOld->aiOvfl[0];
57448         }
57449         isDivider = !leafData;
57450       }
57451 
57452       assert(nOverflow>0 || iOverflow<i );
57453       assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1);
57454       assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1);
57455       if( i==iOverflow ){
57456         isDivider = 1;
57457         if( (--nOverflow)>0 ){
57458           iOverflow++;
57459         }
57460       }
57461 
57462       if( i==cntNew[k] ){
57463         /* Cell i is the cell immediately following the last cell on new
57464         ** sibling page k. If the siblings are not leaf pages of an
57465         ** intkey b-tree, then cell i is a divider cell.  */
57466         pNew = apNew[++k];
57467         if( !leafData ) continue;
57468       }
57469       assert( j<nOld );
57470       assert( k<nNew );
57471 
57472       /* If the cell was originally divider cell (and is not now) or
57473       ** an overflow cell, or if the cell was located on a different sibling
57474       ** page before the balancing, then the pointer map entries associated
57475       ** with any child or overflow pages need to be updated.  */
57476       if( isDivider || pOld->pgno!=pNew->pgno ){
57477         if( !leafCorrection ){
57478           ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
57479         }
57480         if( szCell[i]>pNew->minLocal ){
57481           ptrmapPutOvflPtr(pNew, apCell[i], &rc);
57482         }
57483       }
57484     }
57485 
57486     if( !leafCorrection ){
57487       for(i=0; i<nNew; i++){
57488         u32 key = get4byte(&apNew[i]->aData[8]);
57489         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
57490       }
57491     }
57492 
57493 #if 0
57494     /* The ptrmapCheckPages() contains assert() statements that verify that
57495     ** all pointer map pages are set correctly. This is helpful while
57496     ** debugging. This is usually disabled because a corrupt database may
57497     ** cause an assert() statement to fail.  */
57498     ptrmapCheckPages(apNew, nNew);
57499     ptrmapCheckPages(&pParent, 1);
57500 #endif
57501   }
57502 
57503   assert( pParent->isInit );
57504   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
57505           nOld, nNew, nCell));
57506 
57507   /*
57508   ** Cleanup before returning.
57509   */
57510 balance_cleanup:
57511   sqlite3ScratchFree(apCell);
57512   for(i=0; i<nOld; i++){
57513     releasePage(apOld[i]);
57514   }
57515   for(i=0; i<nNew; i++){
57516     releasePage(apNew[i]);
57517   }
57518 
57519   return rc;
57520 }
57521 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
57522 #pragma optimize("", on)
57523 #endif
57524 
57525 
57526 /*
57527 ** This function is called when the root page of a b-tree structure is
57528 ** overfull (has one or more overflow pages).
57529 **
57530 ** A new child page is allocated and the contents of the current root
57531 ** page, including overflow cells, are copied into the child. The root
57532 ** page is then overwritten to make it an empty page with the right-child
57533 ** pointer pointing to the new page.
57534 **
57535 ** Before returning, all pointer-map entries corresponding to pages
57536 ** that the new child-page now contains pointers to are updated. The
57537 ** entry corresponding to the new right-child pointer of the root
57538 ** page is also updated.
57539 **
57540 ** If successful, *ppChild is set to contain a reference to the child
57541 ** page and SQLITE_OK is returned. In this case the caller is required
57542 ** to call releasePage() on *ppChild exactly once. If an error occurs,
57543 ** an error code is returned and *ppChild is set to 0.
57544 */
57545 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
57546   int rc;                        /* Return value from subprocedures */
57547   MemPage *pChild = 0;           /* Pointer to a new child page */
57548   Pgno pgnoChild = 0;            /* Page number of the new child page */
57549   BtShared *pBt = pRoot->pBt;    /* The BTree */
57550 
57551   assert( pRoot->nOverflow>0 );
57552   assert( sqlite3_mutex_held(pBt->mutex) );
57553 
57554   /* Make pRoot, the root page of the b-tree, writable. Allocate a new
57555   ** page that will become the new right-child of pPage. Copy the contents
57556   ** of the node stored on pRoot into the new child page.
57557   */
57558   rc = sqlite3PagerWrite(pRoot->pDbPage);
57559   if( rc==SQLITE_OK ){
57560     rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
57561     copyNodeContent(pRoot, pChild, &rc);
57562     if( ISAUTOVACUUM ){
57563       ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
57564     }
57565   }
57566   if( rc ){
57567     *ppChild = 0;
57568     releasePage(pChild);
57569     return rc;
57570   }
57571   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
57572   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
57573   assert( pChild->nCell==pRoot->nCell );
57574 
57575   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
57576 
57577   /* Copy the overflow cells from pRoot to pChild */
57578   memcpy(pChild->aiOvfl, pRoot->aiOvfl,
57579          pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
57580   memcpy(pChild->apOvfl, pRoot->apOvfl,
57581          pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
57582   pChild->nOverflow = pRoot->nOverflow;
57583 
57584   /* Zero the contents of pRoot. Then install pChild as the right-child. */
57585   zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
57586   put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
57587 
57588   *ppChild = pChild;
57589   return SQLITE_OK;
57590 }
57591 
57592 /*
57593 ** The page that pCur currently points to has just been modified in
57594 ** some way. This function figures out if this modification means the
57595 ** tree needs to be balanced, and if so calls the appropriate balancing
57596 ** routine. Balancing routines are:
57597 **
57598 **   balance_quick()
57599 **   balance_deeper()
57600 **   balance_nonroot()
57601 */
57602 static int balance(BtCursor *pCur){
57603   int rc = SQLITE_OK;
57604   const int nMin = pCur->pBt->usableSize * 2 / 3;
57605   u8 aBalanceQuickSpace[13];
57606   u8 *pFree = 0;
57607 
57608   TESTONLY( int balance_quick_called = 0 );
57609   TESTONLY( int balance_deeper_called = 0 );
57610 
57611   do {
57612     int iPage = pCur->iPage;
57613     MemPage *pPage = pCur->apPage[iPage];
57614 
57615     if( iPage==0 ){
57616       if( pPage->nOverflow ){
57617         /* The root page of the b-tree is overfull. In this case call the
57618         ** balance_deeper() function to create a new child for the root-page
57619         ** and copy the current contents of the root-page to it. The
57620         ** next iteration of the do-loop will balance the child page.
57621         */
57622         assert( (balance_deeper_called++)==0 );
57623         rc = balance_deeper(pPage, &pCur->apPage[1]);
57624         if( rc==SQLITE_OK ){
57625           pCur->iPage = 1;
57626           pCur->aiIdx[0] = 0;
57627           pCur->aiIdx[1] = 0;
57628           assert( pCur->apPage[1]->nOverflow );
57629         }
57630       }else{
57631         break;
57632       }
57633     }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
57634       break;
57635     }else{
57636       MemPage * const pParent = pCur->apPage[iPage-1];
57637       int const iIdx = pCur->aiIdx[iPage-1];
57638 
57639       rc = sqlite3PagerWrite(pParent->pDbPage);
57640       if( rc==SQLITE_OK ){
57641 #ifndef SQLITE_OMIT_QUICKBALANCE
57642         if( pPage->hasData
57643          && pPage->nOverflow==1
57644          && pPage->aiOvfl[0]==pPage->nCell
57645          && pParent->pgno!=1
57646          && pParent->nCell==iIdx
57647         ){
57648           /* Call balance_quick() to create a new sibling of pPage on which
57649           ** to store the overflow cell. balance_quick() inserts a new cell
57650           ** into pParent, which may cause pParent overflow. If this
57651           ** happens, the next interation of the do-loop will balance pParent
57652           ** use either balance_nonroot() or balance_deeper(). Until this
57653           ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
57654           ** buffer.
57655           **
57656           ** The purpose of the following assert() is to check that only a
57657           ** single call to balance_quick() is made for each call to this
57658           ** function. If this were not verified, a subtle bug involving reuse
57659           ** of the aBalanceQuickSpace[] might sneak in.
57660           */
57661           assert( (balance_quick_called++)==0 );
57662           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
57663         }else
57664 #endif
57665         {
57666           /* In this case, call balance_nonroot() to redistribute cells
57667           ** between pPage and up to 2 of its sibling pages. This involves
57668           ** modifying the contents of pParent, which may cause pParent to
57669           ** become overfull or underfull. The next iteration of the do-loop
57670           ** will balance the parent page to correct this.
57671           **
57672           ** If the parent page becomes overfull, the overflow cell or cells
57673           ** are stored in the pSpace buffer allocated immediately below.
57674           ** A subsequent iteration of the do-loop will deal with this by
57675           ** calling balance_nonroot() (balance_deeper() may be called first,
57676           ** but it doesn't deal with overflow cells - just moves them to a
57677           ** different page). Once this subsequent call to balance_nonroot()
57678           ** has completed, it is safe to release the pSpace buffer used by
57679           ** the previous call, as the overflow cell data will have been
57680           ** copied either into the body of a database page or into the new
57681           ** pSpace buffer passed to the latter call to balance_nonroot().
57682           */
57683           u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
57684           rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
57685           if( pFree ){
57686             /* If pFree is not NULL, it points to the pSpace buffer used
57687             ** by a previous call to balance_nonroot(). Its contents are
57688             ** now stored either on real database pages or within the
57689             ** new pSpace buffer, so it may be safely freed here. */
57690             sqlite3PageFree(pFree);
57691           }
57692 
57693           /* The pSpace buffer will be freed after the next call to
57694           ** balance_nonroot(), or just before this function returns, whichever
57695           ** comes first. */
57696           pFree = pSpace;
57697         }
57698       }
57699 
57700       pPage->nOverflow = 0;
57701 
57702       /* The next iteration of the do-loop balances the parent page. */
57703       releasePage(pPage);
57704       pCur->iPage--;
57705     }
57706   }while( rc==SQLITE_OK );
57707 
57708   if( pFree ){
57709     sqlite3PageFree(pFree);
57710   }
57711   return rc;
57712 }
57713 
57714 
57715 /*
57716 ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
57717 ** and the data is given by (pData,nData).  The cursor is used only to
57718 ** define what table the record should be inserted into.  The cursor
57719 ** is left pointing at a random location.
57720 **
57721 ** For an INTKEY table, only the nKey value of the key is used.  pKey is
57722 ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
57723 **
57724 ** If the seekResult parameter is non-zero, then a successful call to
57725 ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
57726 ** been performed. seekResult is the search result returned (a negative
57727 ** number if pCur points at an entry that is smaller than (pKey, nKey), or
57728 ** a positive value if pCur points at an etry that is larger than
57729 ** (pKey, nKey)).
57730 **
57731 ** If the seekResult parameter is non-zero, then the caller guarantees that
57732 ** cursor pCur is pointing at the existing copy of a row that is to be
57733 ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
57734 ** point to any entry or to no entry at all and so this function has to seek
57735 ** the cursor before the new key can be inserted.
57736 */
57737 SQLITE_PRIVATE int sqlite3BtreeInsert(
57738   BtCursor *pCur,                /* Insert data into the table of this cursor */
57739   const void *pKey, i64 nKey,    /* The key of the new record */
57740   const void *pData, int nData,  /* The data of the new record */
57741   int nZero,                     /* Number of extra 0 bytes to append to data */
57742   int appendBias,                /* True if this is likely an append */
57743   int seekResult                 /* Result of prior MovetoUnpacked() call */
57744 ){
57745   int rc;
57746   int loc = seekResult;          /* -1: before desired location  +1: after */
57747   int szNew = 0;
57748   int idx;
57749   MemPage *pPage;
57750   Btree *p = pCur->pBtree;
57751   BtShared *pBt = p->pBt;
57752   unsigned char *oldCell;
57753   unsigned char *newCell = 0;
57754 
57755   if( pCur->eState==CURSOR_FAULT ){
57756     assert( pCur->skipNext!=SQLITE_OK );
57757     return pCur->skipNext;
57758   }
57759 
57760   assert( cursorHoldsMutex(pCur) );
57761   assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
57762               && (pBt->btsFlags & BTS_READ_ONLY)==0 );
57763   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
57764 
57765   /* Assert that the caller has been consistent. If this cursor was opened
57766   ** expecting an index b-tree, then the caller should be inserting blob
57767   ** keys with no associated data. If the cursor was opened expecting an
57768   ** intkey table, the caller should be inserting integer keys with a
57769   ** blob of associated data.  */
57770   assert( (pKey==0)==(pCur->pKeyInfo==0) );
57771 
57772   /* Save the positions of any other cursors open on this table.
57773   **
57774   ** In some cases, the call to btreeMoveto() below is a no-op. For
57775   ** example, when inserting data into a table with auto-generated integer
57776   ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
57777   ** integer key to use. It then calls this function to actually insert the
57778   ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
57779   ** that the cursor is already where it needs to be and returns without
57780   ** doing any work. To avoid thwarting these optimizations, it is important
57781   ** not to clear the cursor here.
57782   */
57783   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
57784   if( rc ) return rc;
57785 
57786   if( pCur->pKeyInfo==0 ){
57787     /* If this is an insert into a table b-tree, invalidate any incrblob
57788     ** cursors open on the row being replaced */
57789     invalidateIncrblobCursors(p, nKey, 0);
57790 
57791     /* If the cursor is currently on the last row and we are appending a
57792     ** new row onto the end, set the "loc" to avoid an unnecessary btreeMoveto()
57793     ** call */
57794     if( pCur->validNKey && nKey>0 && pCur->info.nKey==nKey-1 ){
57795       loc = -1;
57796     }
57797   }
57798 
57799   if( !loc ){
57800     rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
57801     if( rc ) return rc;
57802   }
57803   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
57804 
57805   pPage = pCur->apPage[pCur->iPage];
57806   assert( pPage->intKey || nKey>=0 );
57807   assert( pPage->leaf || !pPage->intKey );
57808 
57809   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
57810           pCur->pgnoRoot, nKey, nData, pPage->pgno,
57811           loc==0 ? "overwrite" : "new entry"));
57812   assert( pPage->isInit );
57813   allocateTempSpace(pBt);
57814   newCell = pBt->pTmpSpace;
57815   if( newCell==0 ) return SQLITE_NOMEM;
57816   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
57817   if( rc ) goto end_insert;
57818   assert( szNew==cellSizePtr(pPage, newCell) );
57819   assert( szNew <= MX_CELL_SIZE(pBt) );
57820   idx = pCur->aiIdx[pCur->iPage];
57821   if( loc==0 ){
57822     u16 szOld;
57823     assert( idx<pPage->nCell );
57824     rc = sqlite3PagerWrite(pPage->pDbPage);
57825     if( rc ){
57826       goto end_insert;
57827     }
57828     oldCell = findCell(pPage, idx);
57829     if( !pPage->leaf ){
57830       memcpy(newCell, oldCell, 4);
57831     }
57832     szOld = cellSizePtr(pPage, oldCell);
57833     rc = clearCell(pPage, oldCell);
57834     dropCell(pPage, idx, szOld, &rc);
57835     if( rc ) goto end_insert;
57836   }else if( loc<0 && pPage->nCell>0 ){
57837     assert( pPage->leaf );
57838     idx = ++pCur->aiIdx[pCur->iPage];
57839   }else{
57840     assert( pPage->leaf );
57841   }
57842   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
57843   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
57844 
57845   /* If no error has occurred and pPage has an overflow cell, call balance()
57846   ** to redistribute the cells within the tree. Since balance() may move
57847   ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
57848   ** variables.
57849   **
57850   ** Previous versions of SQLite called moveToRoot() to move the cursor
57851   ** back to the root page as balance() used to invalidate the contents
57852   ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
57853   ** set the cursor state to "invalid". This makes common insert operations
57854   ** slightly faster.
57855   **
57856   ** There is a subtle but important optimization here too. When inserting
57857   ** multiple records into an intkey b-tree using a single cursor (as can
57858   ** happen while processing an "INSERT INTO ... SELECT" statement), it
57859   ** is advantageous to leave the cursor pointing to the last entry in
57860   ** the b-tree if possible. If the cursor is left pointing to the last
57861   ** entry in the table, and the next row inserted has an integer key
57862   ** larger than the largest existing key, it is possible to insert the
57863   ** row without seeking the cursor. This can be a big performance boost.
57864   */
57865   pCur->info.nSize = 0;
57866   if( rc==SQLITE_OK && pPage->nOverflow ){
57867     pCur->validNKey = 0;
57868     rc = balance(pCur);
57869 
57870     /* Must make sure nOverflow is reset to zero even if the balance()
57871     ** fails. Internal data structure corruption will result otherwise.
57872     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
57873     ** from trying to save the current position of the cursor.  */
57874     pCur->apPage[pCur->iPage]->nOverflow = 0;
57875     pCur->eState = CURSOR_INVALID;
57876   }
57877   assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
57878 
57879 end_insert:
57880   return rc;
57881 }
57882 
57883 /*
57884 ** Delete the entry that the cursor is pointing to.  The cursor
57885 ** is left pointing at a arbitrary location.
57886 */
57887 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
57888   Btree *p = pCur->pBtree;
57889   BtShared *pBt = p->pBt;
57890   int rc;                              /* Return code */
57891   MemPage *pPage;                      /* Page to delete cell from */
57892   unsigned char *pCell;                /* Pointer to cell to delete */
57893   int iCellIdx;                        /* Index of cell to delete */
57894   int iCellDepth;                      /* Depth of node containing pCell */
57895 
57896   assert( cursorHoldsMutex(pCur) );
57897   assert( pBt->inTransaction==TRANS_WRITE );
57898   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
57899   assert( pCur->wrFlag );
57900   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
57901   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
57902 
57903   if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
57904    || NEVER(pCur->eState!=CURSOR_VALID)
57905   ){
57906     return SQLITE_ERROR;  /* Something has gone awry. */
57907   }
57908 
57909   iCellDepth = pCur->iPage;
57910   iCellIdx = pCur->aiIdx[iCellDepth];
57911   pPage = pCur->apPage[iCellDepth];
57912   pCell = findCell(pPage, iCellIdx);
57913 
57914   /* If the page containing the entry to delete is not a leaf page, move
57915   ** the cursor to the largest entry in the tree that is smaller than
57916   ** the entry being deleted. This cell will replace the cell being deleted
57917   ** from the internal node. The 'previous' entry is used for this instead
57918   ** of the 'next' entry, as the previous entry is always a part of the
57919   ** sub-tree headed by the child page of the cell being deleted. This makes
57920   ** balancing the tree following the delete operation easier.  */
57921   if( !pPage->leaf ){
57922     int notUsed = 0;
57923     rc = sqlite3BtreePrevious(pCur, &notUsed);
57924     if( rc ) return rc;
57925   }
57926 
57927   /* Save the positions of any other cursors open on this table before
57928   ** making any modifications. Make the page containing the entry to be
57929   ** deleted writable. Then free any overflow pages associated with the
57930   ** entry and finally remove the cell itself from within the page.
57931   */
57932   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
57933   if( rc ) return rc;
57934 
57935   /* If this is a delete operation to remove a row from a table b-tree,
57936   ** invalidate any incrblob cursors open on the row being deleted.  */
57937   if( pCur->pKeyInfo==0 ){
57938     invalidateIncrblobCursors(p, pCur->info.nKey, 0);
57939   }
57940 
57941   rc = sqlite3PagerWrite(pPage->pDbPage);
57942   if( rc ) return rc;
57943   rc = clearCell(pPage, pCell);
57944   dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
57945   if( rc ) return rc;
57946 
57947   /* If the cell deleted was not located on a leaf page, then the cursor
57948   ** is currently pointing to the largest entry in the sub-tree headed
57949   ** by the child-page of the cell that was just deleted from an internal
57950   ** node. The cell from the leaf node needs to be moved to the internal
57951   ** node to replace the deleted cell.  */
57952   if( !pPage->leaf ){
57953     MemPage *pLeaf = pCur->apPage[pCur->iPage];
57954     int nCell;
57955     Pgno n = pCur->apPage[iCellDepth+1]->pgno;
57956     unsigned char *pTmp;
57957 
57958     pCell = findCell(pLeaf, pLeaf->nCell-1);
57959     nCell = cellSizePtr(pLeaf, pCell);
57960     assert( MX_CELL_SIZE(pBt) >= nCell );
57961 
57962     allocateTempSpace(pBt);
57963     pTmp = pBt->pTmpSpace;
57964 
57965     rc = sqlite3PagerWrite(pLeaf->pDbPage);
57966     insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
57967     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
57968     if( rc ) return rc;
57969   }
57970 
57971   /* Balance the tree. If the entry deleted was located on a leaf page,
57972   ** then the cursor still points to that page. In this case the first
57973   ** call to balance() repairs the tree, and the if(...) condition is
57974   ** never true.
57975   **
57976   ** Otherwise, if the entry deleted was on an internal node page, then
57977   ** pCur is pointing to the leaf page from which a cell was removed to
57978   ** replace the cell deleted from the internal node. This is slightly
57979   ** tricky as the leaf node may be underfull, and the internal node may
57980   ** be either under or overfull. In this case run the balancing algorithm
57981   ** on the leaf node first. If the balance proceeds far enough up the
57982   ** tree that we can be sure that any problem in the internal node has
57983   ** been corrected, so be it. Otherwise, after balancing the leaf node,
57984   ** walk the cursor up the tree to the internal node and balance it as
57985   ** well.  */
57986   rc = balance(pCur);
57987   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
57988     while( pCur->iPage>iCellDepth ){
57989       releasePage(pCur->apPage[pCur->iPage--]);
57990     }
57991     rc = balance(pCur);
57992   }
57993 
57994   if( rc==SQLITE_OK ){
57995     moveToRoot(pCur);
57996   }
57997   return rc;
57998 }
57999 
58000 /*
58001 ** Create a new BTree table.  Write into *piTable the page
58002 ** number for the root page of the new table.
58003 **
58004 ** The type of type is determined by the flags parameter.  Only the
58005 ** following values of flags are currently in use.  Other values for
58006 ** flags might not work:
58007 **
58008 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
58009 **     BTREE_ZERODATA                  Used for SQL indices
58010 */
58011 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
58012   BtShared *pBt = p->pBt;
58013   MemPage *pRoot;
58014   Pgno pgnoRoot;
58015   int rc;
58016   int ptfFlags;          /* Page-type flage for the root page of new table */
58017 
58018   assert( sqlite3BtreeHoldsMutex(p) );
58019   assert( pBt->inTransaction==TRANS_WRITE );
58020   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
58021 
58022 #ifdef SQLITE_OMIT_AUTOVACUUM
58023   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
58024   if( rc ){
58025     return rc;
58026   }
58027 #else
58028   if( pBt->autoVacuum ){
58029     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
58030     MemPage *pPageMove; /* The page to move to. */
58031 
58032     /* Creating a new table may probably require moving an existing database
58033     ** to make room for the new tables root page. In case this page turns
58034     ** out to be an overflow page, delete all overflow page-map caches
58035     ** held by open cursors.
58036     */
58037     invalidateAllOverflowCache(pBt);
58038 
58039     /* Read the value of meta[3] from the database to determine where the
58040     ** root page of the new table should go. meta[3] is the largest root-page
58041     ** created so far, so the new root-page is (meta[3]+1).
58042     */
58043     sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
58044     pgnoRoot++;
58045 
58046     /* The new root-page may not be allocated on a pointer-map page, or the
58047     ** PENDING_BYTE page.
58048     */
58049     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
58050         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
58051       pgnoRoot++;
58052     }
58053     assert( pgnoRoot>=3 );
58054 
58055     /* Allocate a page. The page that currently resides at pgnoRoot will
58056     ** be moved to the allocated page (unless the allocated page happens
58057     ** to reside at pgnoRoot).
58058     */
58059     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
58060     if( rc!=SQLITE_OK ){
58061       return rc;
58062     }
58063 
58064     if( pgnoMove!=pgnoRoot ){
58065       /* pgnoRoot is the page that will be used for the root-page of
58066       ** the new table (assuming an error did not occur). But we were
58067       ** allocated pgnoMove. If required (i.e. if it was not allocated
58068       ** by extending the file), the current page at position pgnoMove
58069       ** is already journaled.
58070       */
58071       u8 eType = 0;
58072       Pgno iPtrPage = 0;
58073 
58074       /* Save the positions of any open cursors. This is required in
58075       ** case they are holding a reference to an xFetch reference
58076       ** corresponding to page pgnoRoot.  */
58077       rc = saveAllCursors(pBt, 0, 0);
58078       releasePage(pPageMove);
58079       if( rc!=SQLITE_OK ){
58080         return rc;
58081       }
58082 
58083       /* Move the page currently at pgnoRoot to pgnoMove. */
58084       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
58085       if( rc!=SQLITE_OK ){
58086         return rc;
58087       }
58088       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
58089       if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
58090         rc = SQLITE_CORRUPT_BKPT;
58091       }
58092       if( rc!=SQLITE_OK ){
58093         releasePage(pRoot);
58094         return rc;
58095       }
58096       assert( eType!=PTRMAP_ROOTPAGE );
58097       assert( eType!=PTRMAP_FREEPAGE );
58098       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
58099       releasePage(pRoot);
58100 
58101       /* Obtain the page at pgnoRoot */
58102       if( rc!=SQLITE_OK ){
58103         return rc;
58104       }
58105       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
58106       if( rc!=SQLITE_OK ){
58107         return rc;
58108       }
58109       rc = sqlite3PagerWrite(pRoot->pDbPage);
58110       if( rc!=SQLITE_OK ){
58111         releasePage(pRoot);
58112         return rc;
58113       }
58114     }else{
58115       pRoot = pPageMove;
58116     }
58117 
58118     /* Update the pointer-map and meta-data with the new root-page number. */
58119     ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
58120     if( rc ){
58121       releasePage(pRoot);
58122       return rc;
58123     }
58124 
58125     /* When the new root page was allocated, page 1 was made writable in
58126     ** order either to increase the database filesize, or to decrement the
58127     ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
58128     */
58129     assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
58130     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
58131     if( NEVER(rc) ){
58132       releasePage(pRoot);
58133       return rc;
58134     }
58135 
58136   }else{
58137     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
58138     if( rc ) return rc;
58139   }
58140 #endif
58141   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
58142   if( createTabFlags & BTREE_INTKEY ){
58143     ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
58144   }else{
58145     ptfFlags = PTF_ZERODATA | PTF_LEAF;
58146   }
58147   zeroPage(pRoot, ptfFlags);
58148   sqlite3PagerUnref(pRoot->pDbPage);
58149   assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
58150   *piTable = (int)pgnoRoot;
58151   return SQLITE_OK;
58152 }
58153 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
58154   int rc;
58155   sqlite3BtreeEnter(p);
58156   rc = btreeCreateTable(p, piTable, flags);
58157   sqlite3BtreeLeave(p);
58158   return rc;
58159 }
58160 
58161 /*
58162 ** Erase the given database page and all its children.  Return
58163 ** the page to the freelist.
58164 */
58165 static int clearDatabasePage(
58166   BtShared *pBt,           /* The BTree that contains the table */
58167   Pgno pgno,               /* Page number to clear */
58168   int freePageFlag,        /* Deallocate page if true */
58169   int *pnChange            /* Add number of Cells freed to this counter */
58170 ){
58171   MemPage *pPage;
58172   int rc;
58173   unsigned char *pCell;
58174   int i;
58175   int hdr;
58176 
58177   assert( sqlite3_mutex_held(pBt->mutex) );
58178   if( pgno>btreePagecount(pBt) ){
58179     return SQLITE_CORRUPT_BKPT;
58180   }
58181 
58182   rc = getAndInitPage(pBt, pgno, &pPage, 0);
58183   if( rc ) return rc;
58184   hdr = pPage->hdrOffset;
58185   for(i=0; i<pPage->nCell; i++){
58186     pCell = findCell(pPage, i);
58187     if( !pPage->leaf ){
58188       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
58189       if( rc ) goto cleardatabasepage_out;
58190     }
58191     rc = clearCell(pPage, pCell);
58192     if( rc ) goto cleardatabasepage_out;
58193   }
58194   if( !pPage->leaf ){
58195     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
58196     if( rc ) goto cleardatabasepage_out;
58197   }else if( pnChange ){
58198     assert( pPage->intKey );
58199     *pnChange += pPage->nCell;
58200   }
58201   if( freePageFlag ){
58202     freePage(pPage, &rc);
58203   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
58204     zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
58205   }
58206 
58207 cleardatabasepage_out:
58208   releasePage(pPage);
58209   return rc;
58210 }
58211 
58212 /*
58213 ** Delete all information from a single table in the database.  iTable is
58214 ** the page number of the root of the table.  After this routine returns,
58215 ** the root page is empty, but still exists.
58216 **
58217 ** This routine will fail with SQLITE_LOCKED if there are any open
58218 ** read cursors on the table.  Open write cursors are moved to the
58219 ** root of the table.
58220 **
58221 ** If pnChange is not NULL, then table iTable must be an intkey table. The
58222 ** integer value pointed to by pnChange is incremented by the number of
58223 ** entries in the table.
58224 */
58225 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
58226   int rc;
58227   BtShared *pBt = p->pBt;
58228   sqlite3BtreeEnter(p);
58229   assert( p->inTrans==TRANS_WRITE );
58230 
58231   rc = saveAllCursors(pBt, (Pgno)iTable, 0);
58232 
58233   if( SQLITE_OK==rc ){
58234     /* Invalidate all incrblob cursors open on table iTable (assuming iTable
58235     ** is the root of a table b-tree - if it is not, the following call is
58236     ** a no-op).  */
58237     invalidateIncrblobCursors(p, 0, 1);
58238     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
58239   }
58240   sqlite3BtreeLeave(p);
58241   return rc;
58242 }
58243 
58244 /*
58245 ** Erase all information in a table and add the root of the table to
58246 ** the freelist.  Except, the root of the principle table (the one on
58247 ** page 1) is never added to the freelist.
58248 **
58249 ** This routine will fail with SQLITE_LOCKED if there are any open
58250 ** cursors on the table.
58251 **
58252 ** If AUTOVACUUM is enabled and the page at iTable is not the last
58253 ** root page in the database file, then the last root page
58254 ** in the database file is moved into the slot formerly occupied by
58255 ** iTable and that last slot formerly occupied by the last root page
58256 ** is added to the freelist instead of iTable.  In this say, all
58257 ** root pages are kept at the beginning of the database file, which
58258 ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the
58259 ** page number that used to be the last root page in the file before
58260 ** the move.  If no page gets moved, *piMoved is set to 0.
58261 ** The last root page is recorded in meta[3] and the value of
58262 ** meta[3] is updated by this procedure.
58263 */
58264 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
58265   int rc;
58266   MemPage *pPage = 0;
58267   BtShared *pBt = p->pBt;
58268 
58269   assert( sqlite3BtreeHoldsMutex(p) );
58270   assert( p->inTrans==TRANS_WRITE );
58271 
58272   /* It is illegal to drop a table if any cursors are open on the
58273   ** database. This is because in auto-vacuum mode the backend may
58274   ** need to move another root-page to fill a gap left by the deleted
58275   ** root page. If an open cursor was using this page a problem would
58276   ** occur.
58277   **
58278   ** This error is caught long before control reaches this point.
58279   */
58280   if( NEVER(pBt->pCursor) ){
58281     sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
58282     return SQLITE_LOCKED_SHAREDCACHE;
58283   }
58284 
58285   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
58286   if( rc ) return rc;
58287   rc = sqlite3BtreeClearTable(p, iTable, 0);
58288   if( rc ){
58289     releasePage(pPage);
58290     return rc;
58291   }
58292 
58293   *piMoved = 0;
58294 
58295   if( iTable>1 ){
58296 #ifdef SQLITE_OMIT_AUTOVACUUM
58297     freePage(pPage, &rc);
58298     releasePage(pPage);
58299 #else
58300     if( pBt->autoVacuum ){
58301       Pgno maxRootPgno;
58302       sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
58303 
58304       if( iTable==maxRootPgno ){
58305         /* If the table being dropped is the table with the largest root-page
58306         ** number in the database, put the root page on the free list.
58307         */
58308         freePage(pPage, &rc);
58309         releasePage(pPage);
58310         if( rc!=SQLITE_OK ){
58311           return rc;
58312         }
58313       }else{
58314         /* The table being dropped does not have the largest root-page
58315         ** number in the database. So move the page that does into the
58316         ** gap left by the deleted root-page.
58317         */
58318         MemPage *pMove;
58319         releasePage(pPage);
58320         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
58321         if( rc!=SQLITE_OK ){
58322           return rc;
58323         }
58324         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
58325         releasePage(pMove);
58326         if( rc!=SQLITE_OK ){
58327           return rc;
58328         }
58329         pMove = 0;
58330         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
58331         freePage(pMove, &rc);
58332         releasePage(pMove);
58333         if( rc!=SQLITE_OK ){
58334           return rc;
58335         }
58336         *piMoved = maxRootPgno;
58337       }
58338 
58339       /* Set the new 'max-root-page' value in the database header. This
58340       ** is the old value less one, less one more if that happens to
58341       ** be a root-page number, less one again if that is the
58342       ** PENDING_BYTE_PAGE.
58343       */
58344       maxRootPgno--;
58345       while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
58346              || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
58347         maxRootPgno--;
58348       }
58349       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
58350 
58351       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
58352     }else{
58353       freePage(pPage, &rc);
58354       releasePage(pPage);
58355     }
58356 #endif
58357   }else{
58358     /* If sqlite3BtreeDropTable was called on page 1.
58359     ** This really never should happen except in a corrupt
58360     ** database.
58361     */
58362     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
58363     releasePage(pPage);
58364   }
58365   return rc;
58366 }
58367 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
58368   int rc;
58369   sqlite3BtreeEnter(p);
58370   rc = btreeDropTable(p, iTable, piMoved);
58371   sqlite3BtreeLeave(p);
58372   return rc;
58373 }
58374 
58375 
58376 /*
58377 ** This function may only be called if the b-tree connection already
58378 ** has a read or write transaction open on the database.
58379 **
58380 ** Read the meta-information out of a database file.  Meta[0]
58381 ** is the number of free pages currently in the database.  Meta[1]
58382 ** through meta[15] are available for use by higher layers.  Meta[0]
58383 ** is read-only, the others are read/write.
58384 **
58385 ** The schema layer numbers meta values differently.  At the schema
58386 ** layer (and the SetCookie and ReadCookie opcodes) the number of
58387 ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
58388 */
58389 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
58390   BtShared *pBt = p->pBt;
58391 
58392   sqlite3BtreeEnter(p);
58393   assert( p->inTrans>TRANS_NONE );
58394   assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
58395   assert( pBt->pPage1 );
58396   assert( idx>=0 && idx<=15 );
58397 
58398   *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
58399 
58400   /* If auto-vacuum is disabled in this build and this is an auto-vacuum
58401   ** database, mark the database as read-only.  */
58402 #ifdef SQLITE_OMIT_AUTOVACUUM
58403   if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
58404     pBt->btsFlags |= BTS_READ_ONLY;
58405   }
58406 #endif
58407 
58408   sqlite3BtreeLeave(p);
58409 }
58410 
58411 /*
58412 ** Write meta-information back into the database.  Meta[0] is
58413 ** read-only and may not be written.
58414 */
58415 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
58416   BtShared *pBt = p->pBt;
58417   unsigned char *pP1;
58418   int rc;
58419   assert( idx>=1 && idx<=15 );
58420   sqlite3BtreeEnter(p);
58421   assert( p->inTrans==TRANS_WRITE );
58422   assert( pBt->pPage1!=0 );
58423   pP1 = pBt->pPage1->aData;
58424   rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
58425   if( rc==SQLITE_OK ){
58426     put4byte(&pP1[36 + idx*4], iMeta);
58427 #ifndef SQLITE_OMIT_AUTOVACUUM
58428     if( idx==BTREE_INCR_VACUUM ){
58429       assert( pBt->autoVacuum || iMeta==0 );
58430       assert( iMeta==0 || iMeta==1 );
58431       pBt->incrVacuum = (u8)iMeta;
58432     }
58433 #endif
58434   }
58435   sqlite3BtreeLeave(p);
58436   return rc;
58437 }
58438 
58439 #ifndef SQLITE_OMIT_BTREECOUNT
58440 /*
58441 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
58442 ** number of entries in the b-tree and write the result to *pnEntry.
58443 **
58444 ** SQLITE_OK is returned if the operation is successfully executed.
58445 ** Otherwise, if an error is encountered (i.e. an IO error or database
58446 ** corruption) an SQLite error code is returned.
58447 */
58448 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
58449   i64 nEntry = 0;                      /* Value to return in *pnEntry */
58450   int rc;                              /* Return code */
58451 
58452   if( pCur->pgnoRoot==0 ){
58453     *pnEntry = 0;
58454     return SQLITE_OK;
58455   }
58456   rc = moveToRoot(pCur);
58457 
58458   /* Unless an error occurs, the following loop runs one iteration for each
58459   ** page in the B-Tree structure (not including overflow pages).
58460   */
58461   while( rc==SQLITE_OK ){
58462     int iIdx;                          /* Index of child node in parent */
58463     MemPage *pPage;                    /* Current page of the b-tree */
58464 
58465     /* If this is a leaf page or the tree is not an int-key tree, then
58466     ** this page contains countable entries. Increment the entry counter
58467     ** accordingly.
58468     */
58469     pPage = pCur->apPage[pCur->iPage];
58470     if( pPage->leaf || !pPage->intKey ){
58471       nEntry += pPage->nCell;
58472     }
58473 
58474     /* pPage is a leaf node. This loop navigates the cursor so that it
58475     ** points to the first interior cell that it points to the parent of
58476     ** the next page in the tree that has not yet been visited. The
58477     ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
58478     ** of the page, or to the number of cells in the page if the next page
58479     ** to visit is the right-child of its parent.
58480     **
58481     ** If all pages in the tree have been visited, return SQLITE_OK to the
58482     ** caller.
58483     */
58484     if( pPage->leaf ){
58485       do {
58486         if( pCur->iPage==0 ){
58487           /* All pages of the b-tree have been visited. Return successfully. */
58488           *pnEntry = nEntry;
58489           return SQLITE_OK;
58490         }
58491         moveToParent(pCur);
58492       }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
58493 
58494       pCur->aiIdx[pCur->iPage]++;
58495       pPage = pCur->apPage[pCur->iPage];
58496     }
58497 
58498     /* Descend to the child node of the cell that the cursor currently
58499     ** points at. This is the right-child if (iIdx==pPage->nCell).
58500     */
58501     iIdx = pCur->aiIdx[pCur->iPage];
58502     if( iIdx==pPage->nCell ){
58503       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
58504     }else{
58505       rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
58506     }
58507   }
58508 
58509   /* An error has occurred. Return an error code. */
58510   return rc;
58511 }
58512 #endif
58513 
58514 /*
58515 ** Return the pager associated with a BTree.  This routine is used for
58516 ** testing and debugging only.
58517 */
58518 SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
58519   return p->pBt->pPager;
58520 }
58521 
58522 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58523 /*
58524 ** Append a message to the error message string.
58525 */
58526 static void checkAppendMsg(
58527   IntegrityCk *pCheck,
58528   char *zMsg1,
58529   const char *zFormat,
58530   ...
58531 ){
58532   va_list ap;
58533   if( !pCheck->mxErr ) return;
58534   pCheck->mxErr--;
58535   pCheck->nErr++;
58536   va_start(ap, zFormat);
58537   if( pCheck->errMsg.nChar ){
58538     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
58539   }
58540   if( zMsg1 ){
58541     sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
58542   }
58543   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
58544   va_end(ap);
58545   if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
58546     pCheck->mallocFailed = 1;
58547   }
58548 }
58549 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
58550 
58551 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58552 
58553 /*
58554 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
58555 ** corresponds to page iPg is already set.
58556 */
58557 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
58558   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
58559   return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
58560 }
58561 
58562 /*
58563 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
58564 */
58565 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
58566   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
58567   pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
58568 }
58569 
58570 
58571 /*
58572 ** Add 1 to the reference count for page iPage.  If this is the second
58573 ** reference to the page, add an error message to pCheck->zErrMsg.
58574 ** Return 1 if there are 2 ore more references to the page and 0 if
58575 ** if this is the first reference to the page.
58576 **
58577 ** Also check that the page number is in bounds.
58578 */
58579 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
58580   if( iPage==0 ) return 1;
58581   if( iPage>pCheck->nPage ){
58582     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
58583     return 1;
58584   }
58585   if( getPageReferenced(pCheck, iPage) ){
58586     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
58587     return 1;
58588   }
58589   setPageReferenced(pCheck, iPage);
58590   return 0;
58591 }
58592 
58593 #ifndef SQLITE_OMIT_AUTOVACUUM
58594 /*
58595 ** Check that the entry in the pointer-map for page iChild maps to
58596 ** page iParent, pointer type ptrType. If not, append an error message
58597 ** to pCheck.
58598 */
58599 static void checkPtrmap(
58600   IntegrityCk *pCheck,   /* Integrity check context */
58601   Pgno iChild,           /* Child page number */
58602   u8 eType,              /* Expected pointer map type */
58603   Pgno iParent,          /* Expected pointer map parent page number */
58604   char *zContext         /* Context description (used for error msg) */
58605 ){
58606   int rc;
58607   u8 ePtrmapType;
58608   Pgno iPtrmapParent;
58609 
58610   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
58611   if( rc!=SQLITE_OK ){
58612     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
58613     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
58614     return;
58615   }
58616 
58617   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
58618     checkAppendMsg(pCheck, zContext,
58619       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
58620       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
58621   }
58622 }
58623 #endif
58624 
58625 /*
58626 ** Check the integrity of the freelist or of an overflow page list.
58627 ** Verify that the number of pages on the list is N.
58628 */
58629 static void checkList(
58630   IntegrityCk *pCheck,  /* Integrity checking context */
58631   int isFreeList,       /* True for a freelist.  False for overflow page list */
58632   int iPage,            /* Page number for first page in the list */
58633   int N,                /* Expected number of pages in the list */
58634   char *zContext        /* Context for error messages */
58635 ){
58636   int i;
58637   int expected = N;
58638   int iFirst = iPage;
58639   while( N-- > 0 && pCheck->mxErr ){
58640     DbPage *pOvflPage;
58641     unsigned char *pOvflData;
58642     if( iPage<1 ){
58643       checkAppendMsg(pCheck, zContext,
58644          "%d of %d pages missing from overflow list starting at %d",
58645           N+1, expected, iFirst);
58646       break;
58647     }
58648     if( checkRef(pCheck, iPage, zContext) ) break;
58649     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
58650       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
58651       break;
58652     }
58653     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
58654     if( isFreeList ){
58655       int n = get4byte(&pOvflData[4]);
58656 #ifndef SQLITE_OMIT_AUTOVACUUM
58657       if( pCheck->pBt->autoVacuum ){
58658         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
58659       }
58660 #endif
58661       if( n>(int)pCheck->pBt->usableSize/4-2 ){
58662         checkAppendMsg(pCheck, zContext,
58663            "freelist leaf count too big on page %d", iPage);
58664         N--;
58665       }else{
58666         for(i=0; i<n; i++){
58667           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
58668 #ifndef SQLITE_OMIT_AUTOVACUUM
58669           if( pCheck->pBt->autoVacuum ){
58670             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
58671           }
58672 #endif
58673           checkRef(pCheck, iFreePage, zContext);
58674         }
58675         N -= n;
58676       }
58677     }
58678 #ifndef SQLITE_OMIT_AUTOVACUUM
58679     else{
58680       /* If this database supports auto-vacuum and iPage is not the last
58681       ** page in this overflow list, check that the pointer-map entry for
58682       ** the following page matches iPage.
58683       */
58684       if( pCheck->pBt->autoVacuum && N>0 ){
58685         i = get4byte(pOvflData);
58686         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
58687       }
58688     }
58689 #endif
58690     iPage = get4byte(pOvflData);
58691     sqlite3PagerUnref(pOvflPage);
58692   }
58693 }
58694 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
58695 
58696 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58697 /*
58698 ** Do various sanity checks on a single page of a tree.  Return
58699 ** the tree depth.  Root pages return 0.  Parents of root pages
58700 ** return 1, and so forth.
58701 **
58702 ** These checks are done:
58703 **
58704 **      1.  Make sure that cells and freeblocks do not overlap
58705 **          but combine to completely cover the page.
58706 **  NO  2.  Make sure cell keys are in order.
58707 **  NO  3.  Make sure no key is less than or equal to zLowerBound.
58708 **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
58709 **      5.  Check the integrity of overflow pages.
58710 **      6.  Recursively call checkTreePage on all children.
58711 **      7.  Verify that the depth of all children is the same.
58712 **      8.  Make sure this page is at least 33% full or else it is
58713 **          the root of the tree.
58714 */
58715 static int checkTreePage(
58716   IntegrityCk *pCheck,  /* Context for the sanity check */
58717   int iPage,            /* Page number of the page to check */
58718   char *zParentContext, /* Parent context */
58719   i64 *pnParentMinKey,
58720   i64 *pnParentMaxKey
58721 ){
58722   MemPage *pPage;
58723   int i, rc, depth, d2, pgno, cnt;
58724   int hdr, cellStart;
58725   int nCell;
58726   u8 *data;
58727   BtShared *pBt;
58728   int usableSize;
58729   char zContext[100];
58730   char *hit = 0;
58731   i64 nMinKey = 0;
58732   i64 nMaxKey = 0;
58733 
58734   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
58735 
58736   /* Check that the page exists
58737   */
58738   pBt = pCheck->pBt;
58739   usableSize = pBt->usableSize;
58740   if( iPage==0 ) return 0;
58741   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
58742   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
58743     checkAppendMsg(pCheck, zContext,
58744        "unable to get the page. error code=%d", rc);
58745     return 0;
58746   }
58747 
58748   /* Clear MemPage.isInit to make sure the corruption detection code in
58749   ** btreeInitPage() is executed.  */
58750   pPage->isInit = 0;
58751   if( (rc = btreeInitPage(pPage))!=0 ){
58752     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
58753     checkAppendMsg(pCheck, zContext,
58754                    "btreeInitPage() returns error code %d", rc);
58755     releasePage(pPage);
58756     return 0;
58757   }
58758 
58759   /* Check out all the cells.
58760   */
58761   depth = 0;
58762   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
58763     u8 *pCell;
58764     u32 sz;
58765     CellInfo info;
58766 
58767     /* Check payload overflow pages
58768     */
58769     sqlite3_snprintf(sizeof(zContext), zContext,
58770              "On tree page %d cell %d: ", iPage, i);
58771     pCell = findCell(pPage,i);
58772     btreeParseCellPtr(pPage, pCell, &info);
58773     sz = info.nData;
58774     if( !pPage->intKey ) sz += (int)info.nKey;
58775     /* For intKey pages, check that the keys are in order.
58776     */
58777     else if( i==0 ) nMinKey = nMaxKey = info.nKey;
58778     else{
58779       if( info.nKey <= nMaxKey ){
58780         checkAppendMsg(pCheck, zContext,
58781             "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
58782       }
58783       nMaxKey = info.nKey;
58784     }
58785     assert( sz==info.nPayload );
58786     if( (sz>info.nLocal)
58787      && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
58788     ){
58789       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
58790       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
58791 #ifndef SQLITE_OMIT_AUTOVACUUM
58792       if( pBt->autoVacuum ){
58793         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
58794       }
58795 #endif
58796       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
58797     }
58798 
58799     /* Check sanity of left child page.
58800     */
58801     if( !pPage->leaf ){
58802       pgno = get4byte(pCell);
58803 #ifndef SQLITE_OMIT_AUTOVACUUM
58804       if( pBt->autoVacuum ){
58805         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
58806       }
58807 #endif
58808       d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
58809       if( i>0 && d2!=depth ){
58810         checkAppendMsg(pCheck, zContext, "Child page depth differs");
58811       }
58812       depth = d2;
58813     }
58814   }
58815 
58816   if( !pPage->leaf ){
58817     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
58818     sqlite3_snprintf(sizeof(zContext), zContext,
58819                      "On page %d at right child: ", iPage);
58820 #ifndef SQLITE_OMIT_AUTOVACUUM
58821     if( pBt->autoVacuum ){
58822       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
58823     }
58824 #endif
58825     checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
58826   }
58827 
58828   /* For intKey leaf pages, check that the min/max keys are in order
58829   ** with any left/parent/right pages.
58830   */
58831   if( pPage->leaf && pPage->intKey ){
58832     /* if we are a left child page */
58833     if( pnParentMinKey ){
58834       /* if we are the left most child page */
58835       if( !pnParentMaxKey ){
58836         if( nMaxKey > *pnParentMinKey ){
58837           checkAppendMsg(pCheck, zContext,
58838               "Rowid %lld out of order (max larger than parent min of %lld)",
58839               nMaxKey, *pnParentMinKey);
58840         }
58841       }else{
58842         if( nMinKey <= *pnParentMinKey ){
58843           checkAppendMsg(pCheck, zContext,
58844               "Rowid %lld out of order (min less than parent min of %lld)",
58845               nMinKey, *pnParentMinKey);
58846         }
58847         if( nMaxKey > *pnParentMaxKey ){
58848           checkAppendMsg(pCheck, zContext,
58849               "Rowid %lld out of order (max larger than parent max of %lld)",
58850               nMaxKey, *pnParentMaxKey);
58851         }
58852         *pnParentMinKey = nMaxKey;
58853       }
58854     /* else if we're a right child page */
58855     } else if( pnParentMaxKey ){
58856       if( nMinKey <= *pnParentMaxKey ){
58857         checkAppendMsg(pCheck, zContext,
58858             "Rowid %lld out of order (min less than parent max of %lld)",
58859             nMinKey, *pnParentMaxKey);
58860       }
58861     }
58862   }
58863 
58864   /* Check for complete coverage of the page
58865   */
58866   data = pPage->aData;
58867   hdr = pPage->hdrOffset;
58868   hit = sqlite3PageMalloc( pBt->pageSize );
58869   if( hit==0 ){
58870     pCheck->mallocFailed = 1;
58871   }else{
58872     int contentOffset = get2byteNotZero(&data[hdr+5]);
58873     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
58874     memset(hit+contentOffset, 0, usableSize-contentOffset);
58875     memset(hit, 1, contentOffset);
58876     nCell = get2byte(&data[hdr+3]);
58877     cellStart = hdr + 12 - 4*pPage->leaf;
58878     for(i=0; i<nCell; i++){
58879       int pc = get2byte(&data[cellStart+i*2]);
58880       u32 size = 65536;
58881       int j;
58882       if( pc<=usableSize-4 ){
58883         size = cellSizePtr(pPage, &data[pc]);
58884       }
58885       if( (int)(pc+size-1)>=usableSize ){
58886         checkAppendMsg(pCheck, 0,
58887             "Corruption detected in cell %d on page %d",i,iPage);
58888       }else{
58889         for(j=pc+size-1; j>=pc; j--) hit[j]++;
58890       }
58891     }
58892     i = get2byte(&data[hdr+1]);
58893     while( i>0 ){
58894       int size, j;
58895       assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
58896       size = get2byte(&data[i+2]);
58897       assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
58898       for(j=i+size-1; j>=i; j--) hit[j]++;
58899       j = get2byte(&data[i]);
58900       assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
58901       assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
58902       i = j;
58903     }
58904     for(i=cnt=0; i<usableSize; i++){
58905       if( hit[i]==0 ){
58906         cnt++;
58907       }else if( hit[i]>1 ){
58908         checkAppendMsg(pCheck, 0,
58909           "Multiple uses for byte %d of page %d", i, iPage);
58910         break;
58911       }
58912     }
58913     if( cnt!=data[hdr+7] ){
58914       checkAppendMsg(pCheck, 0,
58915           "Fragmentation of %d bytes reported as %d on page %d",
58916           cnt, data[hdr+7], iPage);
58917     }
58918   }
58919   sqlite3PageFree(hit);
58920   releasePage(pPage);
58921   return depth+1;
58922 }
58923 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
58924 
58925 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58926 /*
58927 ** This routine does a complete check of the given BTree file.  aRoot[] is
58928 ** an array of pages numbers were each page number is the root page of
58929 ** a table.  nRoot is the number of entries in aRoot.
58930 **
58931 ** A read-only or read-write transaction must be opened before calling
58932 ** this function.
58933 **
58934 ** Write the number of error seen in *pnErr.  Except for some memory
58935 ** allocation errors,  an error message held in memory obtained from
58936 ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
58937 ** returned.  If a memory allocation error occurs, NULL is returned.
58938 */
58939 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
58940   Btree *p,     /* The btree to be checked */
58941   int *aRoot,   /* An array of root pages numbers for individual trees */
58942   int nRoot,    /* Number of entries in aRoot[] */
58943   int mxErr,    /* Stop reporting errors after this many */
58944   int *pnErr    /* Write number of errors seen to this variable */
58945 ){
58946   Pgno i;
58947   int nRef;
58948   IntegrityCk sCheck;
58949   BtShared *pBt = p->pBt;
58950   char zErr[100];
58951 
58952   sqlite3BtreeEnter(p);
58953   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
58954   nRef = sqlite3PagerRefcount(pBt->pPager);
58955   sCheck.pBt = pBt;
58956   sCheck.pPager = pBt->pPager;
58957   sCheck.nPage = btreePagecount(sCheck.pBt);
58958   sCheck.mxErr = mxErr;
58959   sCheck.nErr = 0;
58960   sCheck.mallocFailed = 0;
58961   *pnErr = 0;
58962   if( sCheck.nPage==0 ){
58963     sqlite3BtreeLeave(p);
58964     return 0;
58965   }
58966 
58967   sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
58968   if( !sCheck.aPgRef ){
58969     *pnErr = 1;
58970     sqlite3BtreeLeave(p);
58971     return 0;
58972   }
58973   i = PENDING_BYTE_PAGE(pBt);
58974   if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
58975   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
58976   sCheck.errMsg.useMalloc = 2;
58977 
58978   /* Check the integrity of the freelist
58979   */
58980   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
58981             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
58982 
58983   /* Check all the tables.
58984   */
58985   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
58986     if( aRoot[i]==0 ) continue;
58987 #ifndef SQLITE_OMIT_AUTOVACUUM
58988     if( pBt->autoVacuum && aRoot[i]>1 ){
58989       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
58990     }
58991 #endif
58992     checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
58993   }
58994 
58995   /* Make sure every page in the file is referenced
58996   */
58997   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
58998 #ifdef SQLITE_OMIT_AUTOVACUUM
58999     if( getPageReferenced(&sCheck, i)==0 ){
59000       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
59001     }
59002 #else
59003     /* If the database supports auto-vacuum, make sure no tables contain
59004     ** references to pointer-map pages.
59005     */
59006     if( getPageReferenced(&sCheck, i)==0 &&
59007        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
59008       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
59009     }
59010     if( getPageReferenced(&sCheck, i)!=0 &&
59011        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
59012       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
59013     }
59014 #endif
59015   }
59016 
59017   /* Make sure this analysis did not leave any unref() pages.
59018   ** This is an internal consistency check; an integrity check
59019   ** of the integrity check.
59020   */
59021   if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
59022     checkAppendMsg(&sCheck, 0,
59023       "Outstanding page count goes from %d to %d during this analysis",
59024       nRef, sqlite3PagerRefcount(pBt->pPager)
59025     );
59026   }
59027 
59028   /* Clean  up and report errors.
59029   */
59030   sqlite3BtreeLeave(p);
59031   sqlite3_free(sCheck.aPgRef);
59032   if( sCheck.mallocFailed ){
59033     sqlite3StrAccumReset(&sCheck.errMsg);
59034     *pnErr = sCheck.nErr+1;
59035     return 0;
59036   }
59037   *pnErr = sCheck.nErr;
59038   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
59039   return sqlite3StrAccumFinish(&sCheck.errMsg);
59040 }
59041 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
59042 
59043 /*
59044 ** Return the full pathname of the underlying database file.  Return
59045 ** an empty string if the database is in-memory or a TEMP database.
59046 **
59047 ** The pager filename is invariant as long as the pager is
59048 ** open so it is safe to access without the BtShared mutex.
59049 */
59050 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
59051   assert( p->pBt->pPager!=0 );
59052   return sqlite3PagerFilename(p->pBt->pPager, 1);
59053 }
59054 
59055 /*
59056 ** Return the pathname of the journal file for this database. The return
59057 ** value of this routine is the same regardless of whether the journal file
59058 ** has been created or not.
59059 **
59060 ** The pager journal filename is invariant as long as the pager is
59061 ** open so it is safe to access without the BtShared mutex.
59062 */
59063 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
59064   assert( p->pBt->pPager!=0 );
59065   return sqlite3PagerJournalname(p->pBt->pPager);
59066 }
59067 
59068 /*
59069 ** Return non-zero if a transaction is active.
59070 */
59071 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
59072   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
59073   return (p && (p->inTrans==TRANS_WRITE));
59074 }
59075 
59076 #ifndef SQLITE_OMIT_WAL
59077 /*
59078 ** Run a checkpoint on the Btree passed as the first argument.
59079 **
59080 ** Return SQLITE_LOCKED if this or any other connection has an open
59081 ** transaction on the shared-cache the argument Btree is connected to.
59082 **
59083 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
59084 */
59085 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
59086   int rc = SQLITE_OK;
59087   if( p ){
59088     BtShared *pBt = p->pBt;
59089     sqlite3BtreeEnter(p);
59090     if( pBt->inTransaction!=TRANS_NONE ){
59091       rc = SQLITE_LOCKED;
59092     }else{
59093       rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
59094     }
59095     sqlite3BtreeLeave(p);
59096   }
59097   return rc;
59098 }
59099 #endif
59100 
59101 /*
59102 ** Return non-zero if a read (or write) transaction is active.
59103 */
59104 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
59105   assert( p );
59106   assert( sqlite3_mutex_held(p->db->mutex) );
59107   return p->inTrans!=TRANS_NONE;
59108 }
59109 
59110 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
59111   assert( p );
59112   assert( sqlite3_mutex_held(p->db->mutex) );
59113   return p->nBackup!=0;
59114 }
59115 
59116 /*
59117 ** This function returns a pointer to a blob of memory associated with
59118 ** a single shared-btree. The memory is used by client code for its own
59119 ** purposes (for example, to store a high-level schema associated with
59120 ** the shared-btree). The btree layer manages reference counting issues.
59121 **
59122 ** The first time this is called on a shared-btree, nBytes bytes of memory
59123 ** are allocated, zeroed, and returned to the caller. For each subsequent
59124 ** call the nBytes parameter is ignored and a pointer to the same blob
59125 ** of memory returned.
59126 **
59127 ** If the nBytes parameter is 0 and the blob of memory has not yet been
59128 ** allocated, a null pointer is returned. If the blob has already been
59129 ** allocated, it is returned as normal.
59130 **
59131 ** Just before the shared-btree is closed, the function passed as the
59132 ** xFree argument when the memory allocation was made is invoked on the
59133 ** blob of allocated memory. The xFree function should not call sqlite3_free()
59134 ** on the memory, the btree layer does that.
59135 */
59136 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
59137   BtShared *pBt = p->pBt;
59138   sqlite3BtreeEnter(p);
59139   if( !pBt->pSchema && nBytes ){
59140     pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
59141     pBt->xFreeSchema = xFree;
59142   }
59143   sqlite3BtreeLeave(p);
59144   return pBt->pSchema;
59145 }
59146 
59147 /*
59148 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
59149 ** btree as the argument handle holds an exclusive lock on the
59150 ** sqlite_master table. Otherwise SQLITE_OK.
59151 */
59152 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
59153   int rc;
59154   assert( sqlite3_mutex_held(p->db->mutex) );
59155   sqlite3BtreeEnter(p);
59156   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
59157   assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
59158   sqlite3BtreeLeave(p);
59159   return rc;
59160 }
59161 
59162 
59163 #ifndef SQLITE_OMIT_SHARED_CACHE
59164 /*
59165 ** Obtain a lock on the table whose root page is iTab.  The
59166 ** lock is a write lock if isWritelock is true or a read lock
59167 ** if it is false.
59168 */
59169 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
59170   int rc = SQLITE_OK;
59171   assert( p->inTrans!=TRANS_NONE );
59172   if( p->sharable ){
59173     u8 lockType = READ_LOCK + isWriteLock;
59174     assert( READ_LOCK+1==WRITE_LOCK );
59175     assert( isWriteLock==0 || isWriteLock==1 );
59176 
59177     sqlite3BtreeEnter(p);
59178     rc = querySharedCacheTableLock(p, iTab, lockType);
59179     if( rc==SQLITE_OK ){
59180       rc = setSharedCacheTableLock(p, iTab, lockType);
59181     }
59182     sqlite3BtreeLeave(p);
59183   }
59184   return rc;
59185 }
59186 #endif
59187 
59188 #ifndef SQLITE_OMIT_INCRBLOB
59189 /*
59190 ** Argument pCsr must be a cursor opened for writing on an
59191 ** INTKEY table currently pointing at a valid table entry.
59192 ** This function modifies the data stored as part of that entry.
59193 **
59194 ** Only the data content may only be modified, it is not possible to
59195 ** change the length of the data stored. If this function is called with
59196 ** parameters that attempt to write past the end of the existing data,
59197 ** no modifications are made and SQLITE_CORRUPT is returned.
59198 */
59199 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
59200   int rc;
59201   assert( cursorHoldsMutex(pCsr) );
59202   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
59203   assert( pCsr->isIncrblobHandle );
59204 
59205   rc = restoreCursorPosition(pCsr);
59206   if( rc!=SQLITE_OK ){
59207     return rc;
59208   }
59209   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
59210   if( pCsr->eState!=CURSOR_VALID ){
59211     return SQLITE_ABORT;
59212   }
59213 
59214   /* Save the positions of all other cursors open on this table. This is
59215   ** required in case any of them are holding references to an xFetch
59216   ** version of the b-tree page modified by the accessPayload call below.
59217   **
59218   ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
59219   ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
59220   ** saveAllCursors can only return SQLITE_OK.
59221   */
59222   VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
59223   assert( rc==SQLITE_OK );
59224 
59225   /* Check some assumptions:
59226   **   (a) the cursor is open for writing,
59227   **   (b) there is a read/write transaction open,
59228   **   (c) the connection holds a write-lock on the table (if required),
59229   **   (d) there are no conflicting read-locks, and
59230   **   (e) the cursor points at a valid row of an intKey table.
59231   */
59232   if( !pCsr->wrFlag ){
59233     return SQLITE_READONLY;
59234   }
59235   assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
59236               && pCsr->pBt->inTransaction==TRANS_WRITE );
59237   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
59238   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
59239   assert( pCsr->apPage[pCsr->iPage]->intKey );
59240 
59241   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
59242 }
59243 
59244 /*
59245 ** Set a flag on this cursor to cache the locations of pages from the
59246 ** overflow list for the current row. This is used by cursors opened
59247 ** for incremental blob IO only.
59248 **
59249 ** This function sets a flag only. The actual page location cache
59250 ** (stored in BtCursor.aOverflow[]) is allocated and used by function
59251 ** accessPayload() (the worker function for sqlite3BtreeData() and
59252 ** sqlite3BtreePutData()).
59253 */
59254 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
59255   assert( cursorHoldsMutex(pCur) );
59256   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
59257   invalidateOverflowCache(pCur);
59258   pCur->isIncrblobHandle = 1;
59259 }
59260 #endif
59261 
59262 /*
59263 ** Set both the "read version" (single byte at byte offset 18) and
59264 ** "write version" (single byte at byte offset 19) fields in the database
59265 ** header to iVersion.
59266 */
59267 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
59268   BtShared *pBt = pBtree->pBt;
59269   int rc;                         /* Return code */
59270 
59271   assert( iVersion==1 || iVersion==2 );
59272 
59273   /* If setting the version fields to 1, do not automatically open the
59274   ** WAL connection, even if the version fields are currently set to 2.
59275   */
59276   pBt->btsFlags &= ~BTS_NO_WAL;
59277   if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
59278 
59279   rc = sqlite3BtreeBeginTrans(pBtree, 0);
59280   if( rc==SQLITE_OK ){
59281     u8 *aData = pBt->pPage1->aData;
59282     if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
59283       rc = sqlite3BtreeBeginTrans(pBtree, 2);
59284       if( rc==SQLITE_OK ){
59285         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
59286         if( rc==SQLITE_OK ){
59287           aData[18] = (u8)iVersion;
59288           aData[19] = (u8)iVersion;
59289         }
59290       }
59291     }
59292   }
59293 
59294   pBt->btsFlags &= ~BTS_NO_WAL;
59295   return rc;
59296 }
59297 
59298 /*
59299 ** set the mask of hint flags for cursor pCsr. Currently the only valid
59300 ** values are 0 and BTREE_BULKLOAD.
59301 */
59302 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
59303   assert( mask==BTREE_BULKLOAD || mask==0 );
59304   pCsr->hints = mask;
59305 }
59306 
59307 /************** End of btree.c ***********************************************/
59308 /************** Begin file backup.c ******************************************/
59309 /*
59310 ** 2009 January 28
59311 **
59312 ** The author disclaims copyright to this source code.  In place of
59313 ** a legal notice, here is a blessing:
59314 **
59315 **    May you do good and not evil.
59316 **    May you find forgiveness for yourself and forgive others.
59317 **    May you share freely, never taking more than you give.
59318 **
59319 *************************************************************************
59320 ** This file contains the implementation of the sqlite3_backup_XXX()
59321 ** API functions and the related features.
59322 */
59323 
59324 /*
59325 ** Structure allocated for each backup operation.
59326 */
59327 struct sqlite3_backup {
59328   sqlite3* pDestDb;        /* Destination database handle */
59329   Btree *pDest;            /* Destination b-tree file */
59330   u32 iDestSchema;         /* Original schema cookie in destination */
59331   int bDestLocked;         /* True once a write-transaction is open on pDest */
59332 
59333   Pgno iNext;              /* Page number of the next source page to copy */
59334   sqlite3* pSrcDb;         /* Source database handle */
59335   Btree *pSrc;             /* Source b-tree file */
59336 
59337   int rc;                  /* Backup process error code */
59338 
59339   /* These two variables are set by every call to backup_step(). They are
59340   ** read by calls to backup_remaining() and backup_pagecount().
59341   */
59342   Pgno nRemaining;         /* Number of pages left to copy */
59343   Pgno nPagecount;         /* Total number of pages to copy */
59344 
59345   int isAttached;          /* True once backup has been registered with pager */
59346   sqlite3_backup *pNext;   /* Next backup associated with source pager */
59347 };
59348 
59349 /*
59350 ** THREAD SAFETY NOTES:
59351 **
59352 **   Once it has been created using backup_init(), a single sqlite3_backup
59353 **   structure may be accessed via two groups of thread-safe entry points:
59354 **
59355 **     * Via the sqlite3_backup_XXX() API function backup_step() and
59356 **       backup_finish(). Both these functions obtain the source database
59357 **       handle mutex and the mutex associated with the source BtShared
59358 **       structure, in that order.
59359 **
59360 **     * Via the BackupUpdate() and BackupRestart() functions, which are
59361 **       invoked by the pager layer to report various state changes in
59362 **       the page cache associated with the source database. The mutex
59363 **       associated with the source database BtShared structure will always
59364 **       be held when either of these functions are invoked.
59365 **
59366 **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
59367 **   backup_pagecount() are not thread-safe functions. If they are called
59368 **   while some other thread is calling backup_step() or backup_finish(),
59369 **   the values returned may be invalid. There is no way for a call to
59370 **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
59371 **   or backup_pagecount().
59372 **
59373 **   Depending on the SQLite configuration, the database handles and/or
59374 **   the Btree objects may have their own mutexes that require locking.
59375 **   Non-sharable Btrees (in-memory databases for example), do not have
59376 **   associated mutexes.
59377 */
59378 
59379 /*
59380 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
59381 ** in connection handle pDb. If such a database cannot be found, return
59382 ** a NULL pointer and write an error message to pErrorDb.
59383 **
59384 ** If the "temp" database is requested, it may need to be opened by this
59385 ** function. If an error occurs while doing so, return 0 and write an
59386 ** error message to pErrorDb.
59387 */
59388 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
59389   int i = sqlite3FindDbName(pDb, zDb);
59390 
59391   if( i==1 ){
59392     Parse *pParse;
59393     int rc = 0;
59394     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
59395     if( pParse==0 ){
59396       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
59397       rc = SQLITE_NOMEM;
59398     }else{
59399       pParse->db = pDb;
59400       if( sqlite3OpenTempDatabase(pParse) ){
59401         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
59402         rc = SQLITE_ERROR;
59403       }
59404       sqlite3DbFree(pErrorDb, pParse->zErrMsg);
59405       sqlite3ParserReset(pParse);
59406       sqlite3StackFree(pErrorDb, pParse);
59407     }
59408     if( rc ){
59409       return 0;
59410     }
59411   }
59412 
59413   if( i<0 ){
59414     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
59415     return 0;
59416   }
59417 
59418   return pDb->aDb[i].pBt;
59419 }
59420 
59421 /*
59422 ** Attempt to set the page size of the destination to match the page size
59423 ** of the source.
59424 */
59425 static int setDestPgsz(sqlite3_backup *p){
59426   int rc;
59427   rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
59428   return rc;
59429 }
59430 
59431 /*
59432 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
59433 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
59434 ** a pointer to the new sqlite3_backup object.
59435 **
59436 ** If an error occurs, NULL is returned and an error code and error message
59437 ** stored in database handle pDestDb.
59438 */
59439 SQLITE_API sqlite3_backup *sqlite3_backup_init(
59440   sqlite3* pDestDb,                     /* Database to write to */
59441   const char *zDestDb,                  /* Name of database within pDestDb */
59442   sqlite3* pSrcDb,                      /* Database connection to read from */
59443   const char *zSrcDb                    /* Name of database within pSrcDb */
59444 ){
59445   sqlite3_backup *p;                    /* Value to return */
59446 
59447   /* Lock the source database handle. The destination database
59448   ** handle is not locked in this routine, but it is locked in
59449   ** sqlite3_backup_step(). The user is required to ensure that no
59450   ** other thread accesses the destination handle for the duration
59451   ** of the backup operation.  Any attempt to use the destination
59452   ** database connection while a backup is in progress may cause
59453   ** a malfunction or a deadlock.
59454   */
59455   sqlite3_mutex_enter(pSrcDb->mutex);
59456   sqlite3_mutex_enter(pDestDb->mutex);
59457 
59458   if( pSrcDb==pDestDb ){
59459     sqlite3Error(
59460         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
59461     );
59462     p = 0;
59463   }else {
59464     /* Allocate space for a new sqlite3_backup object...
59465     ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
59466     ** call to sqlite3_backup_init() and is destroyed by a call to
59467     ** sqlite3_backup_finish(). */
59468     p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
59469     if( !p ){
59470       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
59471     }
59472   }
59473 
59474   /* If the allocation succeeded, populate the new object. */
59475   if( p ){
59476     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
59477     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
59478     p->pDestDb = pDestDb;
59479     p->pSrcDb = pSrcDb;
59480     p->iNext = 1;
59481     p->isAttached = 0;
59482 
59483     if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
59484       /* One (or both) of the named databases did not exist or an OOM
59485       ** error was hit.  The error has already been written into the
59486       ** pDestDb handle.  All that is left to do here is free the
59487       ** sqlite3_backup structure.
59488       */
59489       sqlite3_free(p);
59490       p = 0;
59491     }
59492   }
59493   if( p ){
59494     p->pSrc->nBackup++;
59495   }
59496 
59497   sqlite3_mutex_leave(pDestDb->mutex);
59498   sqlite3_mutex_leave(pSrcDb->mutex);
59499   return p;
59500 }
59501 
59502 /*
59503 ** Argument rc is an SQLite error code. Return true if this error is
59504 ** considered fatal if encountered during a backup operation. All errors
59505 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
59506 */
59507 static int isFatalError(int rc){
59508   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
59509 }
59510 
59511 /*
59512 ** Parameter zSrcData points to a buffer containing the data for
59513 ** page iSrcPg from the source database. Copy this data into the
59514 ** destination database.
59515 */
59516 static int backupOnePage(
59517   sqlite3_backup *p,              /* Backup handle */
59518   Pgno iSrcPg,                    /* Source database page to backup */
59519   const u8 *zSrcData,             /* Source database page data */
59520   int bUpdate                     /* True for an update, false otherwise */
59521 ){
59522   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
59523   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
59524   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
59525   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
59526   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
59527 #ifdef SQLITE_HAS_CODEC
59528   /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
59529   ** guaranteed that the shared-mutex is held by this thread, handle
59530   ** p->pSrc may not actually be the owner.  */
59531   int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
59532   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
59533 #endif
59534   int rc = SQLITE_OK;
59535   i64 iOff;
59536 
59537   assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
59538   assert( p->bDestLocked );
59539   assert( !isFatalError(p->rc) );
59540   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
59541   assert( zSrcData );
59542 
59543   /* Catch the case where the destination is an in-memory database and the
59544   ** page sizes of the source and destination differ.
59545   */
59546   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
59547     rc = SQLITE_READONLY;
59548   }
59549 
59550 #ifdef SQLITE_HAS_CODEC
59551   /* Backup is not possible if the page size of the destination is changing
59552   ** and a codec is in use.
59553   */
59554   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
59555     rc = SQLITE_READONLY;
59556   }
59557 
59558   /* Backup is not possible if the number of bytes of reserve space differ
59559   ** between source and destination.  If there is a difference, try to
59560   ** fix the destination to agree with the source.  If that is not possible,
59561   ** then the backup cannot proceed.
59562   */
59563   if( nSrcReserve!=nDestReserve ){
59564     u32 newPgsz = nSrcPgsz;
59565     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
59566     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
59567   }
59568 #endif
59569 
59570   /* This loop runs once for each destination page spanned by the source
59571   ** page. For each iteration, variable iOff is set to the byte offset
59572   ** of the destination page.
59573   */
59574   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
59575     DbPage *pDestPg = 0;
59576     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
59577     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
59578     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
59579      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
59580     ){
59581       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
59582       u8 *zDestData = sqlite3PagerGetData(pDestPg);
59583       u8 *zOut = &zDestData[iOff%nDestPgsz];
59584 
59585       /* Copy the data from the source page into the destination page.
59586       ** Then clear the Btree layer MemPage.isInit flag. Both this module
59587       ** and the pager code use this trick (clearing the first byte
59588       ** of the page 'extra' space to invalidate the Btree layers
59589       ** cached parse of the page). MemPage.isInit is marked
59590       ** "MUST BE FIRST" for this purpose.
59591       */
59592       memcpy(zOut, zIn, nCopy);
59593       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
59594       if( iOff==0 && bUpdate==0 ){
59595         sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
59596       }
59597     }
59598     sqlite3PagerUnref(pDestPg);
59599   }
59600 
59601   return rc;
59602 }
59603 
59604 /*
59605 ** If pFile is currently larger than iSize bytes, then truncate it to
59606 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
59607 ** this function is a no-op.
59608 **
59609 ** Return SQLITE_OK if everything is successful, or an SQLite error
59610 ** code if an error occurs.
59611 */
59612 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
59613   i64 iCurrent;
59614   int rc = sqlite3OsFileSize(pFile, &iCurrent);
59615   if( rc==SQLITE_OK && iCurrent>iSize ){
59616     rc = sqlite3OsTruncate(pFile, iSize);
59617   }
59618   return rc;
59619 }
59620 
59621 /*
59622 ** Register this backup object with the associated source pager for
59623 ** callbacks when pages are changed or the cache invalidated.
59624 */
59625 static void attachBackupObject(sqlite3_backup *p){
59626   sqlite3_backup **pp;
59627   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
59628   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
59629   p->pNext = *pp;
59630   *pp = p;
59631   p->isAttached = 1;
59632 }
59633 
59634 /*
59635 ** Copy nPage pages from the source b-tree to the destination.
59636 */
59637 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
59638   int rc;
59639   int destMode;       /* Destination journal mode */
59640   int pgszSrc = 0;    /* Source page size */
59641   int pgszDest = 0;   /* Destination page size */
59642 
59643   sqlite3_mutex_enter(p->pSrcDb->mutex);
59644   sqlite3BtreeEnter(p->pSrc);
59645   if( p->pDestDb ){
59646     sqlite3_mutex_enter(p->pDestDb->mutex);
59647   }
59648 
59649   rc = p->rc;
59650   if( !isFatalError(rc) ){
59651     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
59652     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
59653     int ii;                            /* Iterator variable */
59654     int nSrcPage = -1;                 /* Size of source db in pages */
59655     int bCloseTrans = 0;               /* True if src db requires unlocking */
59656 
59657     /* If the source pager is currently in a write-transaction, return
59658     ** SQLITE_BUSY immediately.
59659     */
59660     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
59661       rc = SQLITE_BUSY;
59662     }else{
59663       rc = SQLITE_OK;
59664     }
59665 
59666     /* Lock the destination database, if it is not locked already. */
59667     if( SQLITE_OK==rc && p->bDestLocked==0
59668      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
59669     ){
59670       p->bDestLocked = 1;
59671       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
59672     }
59673 
59674     /* If there is no open read-transaction on the source database, open
59675     ** one now. If a transaction is opened here, then it will be closed
59676     ** before this function exits.
59677     */
59678     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
59679       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
59680       bCloseTrans = 1;
59681     }
59682 
59683     /* Do not allow backup if the destination database is in WAL mode
59684     ** and the page sizes are different between source and destination */
59685     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
59686     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
59687     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
59688     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
59689       rc = SQLITE_READONLY;
59690     }
59691 
59692     /* Now that there is a read-lock on the source database, query the
59693     ** source pager for the number of pages in the database.
59694     */
59695     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
59696     assert( nSrcPage>=0 );
59697     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
59698       const Pgno iSrcPg = p->iNext;                 /* Source page number */
59699       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
59700         DbPage *pSrcPg;                             /* Source page object */
59701         rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
59702                                  PAGER_GET_READONLY);
59703         if( rc==SQLITE_OK ){
59704           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
59705           sqlite3PagerUnref(pSrcPg);
59706         }
59707       }
59708       p->iNext++;
59709     }
59710     if( rc==SQLITE_OK ){
59711       p->nPagecount = nSrcPage;
59712       p->nRemaining = nSrcPage+1-p->iNext;
59713       if( p->iNext>(Pgno)nSrcPage ){
59714         rc = SQLITE_DONE;
59715       }else if( !p->isAttached ){
59716         attachBackupObject(p);
59717       }
59718     }
59719 
59720     /* Update the schema version field in the destination database. This
59721     ** is to make sure that the schema-version really does change in
59722     ** the case where the source and destination databases have the
59723     ** same schema version.
59724     */
59725     if( rc==SQLITE_DONE ){
59726       if( nSrcPage==0 ){
59727         rc = sqlite3BtreeNewDb(p->pDest);
59728         nSrcPage = 1;
59729       }
59730       if( rc==SQLITE_OK || rc==SQLITE_DONE ){
59731         rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
59732       }
59733       if( rc==SQLITE_OK ){
59734         if( p->pDestDb ){
59735           sqlite3ResetAllSchemasOfConnection(p->pDestDb);
59736         }
59737         if( destMode==PAGER_JOURNALMODE_WAL ){
59738           rc = sqlite3BtreeSetVersion(p->pDest, 2);
59739         }
59740       }
59741       if( rc==SQLITE_OK ){
59742         int nDestTruncate;
59743         /* Set nDestTruncate to the final number of pages in the destination
59744         ** database. The complication here is that the destination page
59745         ** size may be different to the source page size.
59746         **
59747         ** If the source page size is smaller than the destination page size,
59748         ** round up. In this case the call to sqlite3OsTruncate() below will
59749         ** fix the size of the file. However it is important to call
59750         ** sqlite3PagerTruncateImage() here so that any pages in the
59751         ** destination file that lie beyond the nDestTruncate page mark are
59752         ** journalled by PagerCommitPhaseOne() before they are destroyed
59753         ** by the file truncation.
59754         */
59755         assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
59756         assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
59757         if( pgszSrc<pgszDest ){
59758           int ratio = pgszDest/pgszSrc;
59759           nDestTruncate = (nSrcPage+ratio-1)/ratio;
59760           if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
59761             nDestTruncate--;
59762           }
59763         }else{
59764           nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
59765         }
59766         assert( nDestTruncate>0 );
59767 
59768         if( pgszSrc<pgszDest ){
59769           /* If the source page-size is smaller than the destination page-size,
59770           ** two extra things may need to happen:
59771           **
59772           **   * The destination may need to be truncated, and
59773           **
59774           **   * Data stored on the pages immediately following the
59775           **     pending-byte page in the source database may need to be
59776           **     copied into the destination database.
59777           */
59778           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
59779           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
59780           Pgno iPg;
59781           int nDstPage;
59782           i64 iOff;
59783           i64 iEnd;
59784 
59785           assert( pFile );
59786           assert( nDestTruncate==0
59787               || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
59788                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
59789              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
59790           ));
59791 
59792           /* This block ensures that all data required to recreate the original
59793           ** database has been stored in the journal for pDestPager and the
59794           ** journal synced to disk. So at this point we may safely modify
59795           ** the database file in any way, knowing that if a power failure
59796           ** occurs, the original database will be reconstructed from the
59797           ** journal file.  */
59798           sqlite3PagerPagecount(pDestPager, &nDstPage);
59799           for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
59800             if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
59801               DbPage *pPg;
59802               rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
59803               if( rc==SQLITE_OK ){
59804                 rc = sqlite3PagerWrite(pPg);
59805                 sqlite3PagerUnref(pPg);
59806               }
59807             }
59808           }
59809           if( rc==SQLITE_OK ){
59810             rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
59811           }
59812 
59813           /* Write the extra pages and truncate the database file as required */
59814           iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
59815           for(
59816             iOff=PENDING_BYTE+pgszSrc;
59817             rc==SQLITE_OK && iOff<iEnd;
59818             iOff+=pgszSrc
59819           ){
59820             PgHdr *pSrcPg = 0;
59821             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
59822             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
59823             if( rc==SQLITE_OK ){
59824               u8 *zData = sqlite3PagerGetData(pSrcPg);
59825               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
59826             }
59827             sqlite3PagerUnref(pSrcPg);
59828           }
59829           if( rc==SQLITE_OK ){
59830             rc = backupTruncateFile(pFile, iSize);
59831           }
59832 
59833           /* Sync the database file to disk. */
59834           if( rc==SQLITE_OK ){
59835             rc = sqlite3PagerSync(pDestPager, 0);
59836           }
59837         }else{
59838           sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
59839           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
59840         }
59841 
59842         /* Finish committing the transaction to the destination database. */
59843         if( SQLITE_OK==rc
59844          && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
59845         ){
59846           rc = SQLITE_DONE;
59847         }
59848       }
59849     }
59850 
59851     /* If bCloseTrans is true, then this function opened a read transaction
59852     ** on the source database. Close the read transaction here. There is
59853     ** no need to check the return values of the btree methods here, as
59854     ** "committing" a read-only transaction cannot fail.
59855     */
59856     if( bCloseTrans ){
59857       TESTONLY( int rc2 );
59858       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
59859       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
59860       assert( rc2==SQLITE_OK );
59861     }
59862 
59863     if( rc==SQLITE_IOERR_NOMEM ){
59864       rc = SQLITE_NOMEM;
59865     }
59866     p->rc = rc;
59867   }
59868   if( p->pDestDb ){
59869     sqlite3_mutex_leave(p->pDestDb->mutex);
59870   }
59871   sqlite3BtreeLeave(p->pSrc);
59872   sqlite3_mutex_leave(p->pSrcDb->mutex);
59873   return rc;
59874 }
59875 
59876 /*
59877 ** Release all resources associated with an sqlite3_backup* handle.
59878 */
59879 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
59880   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
59881   sqlite3 *pSrcDb;                     /* Source database connection */
59882   int rc;                              /* Value to return */
59883 
59884   /* Enter the mutexes */
59885   if( p==0 ) return SQLITE_OK;
59886   pSrcDb = p->pSrcDb;
59887   sqlite3_mutex_enter(pSrcDb->mutex);
59888   sqlite3BtreeEnter(p->pSrc);
59889   if( p->pDestDb ){
59890     sqlite3_mutex_enter(p->pDestDb->mutex);
59891   }
59892 
59893   /* Detach this backup from the source pager. */
59894   if( p->pDestDb ){
59895     p->pSrc->nBackup--;
59896   }
59897   if( p->isAttached ){
59898     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
59899     while( *pp!=p ){
59900       pp = &(*pp)->pNext;
59901     }
59902     *pp = p->pNext;
59903   }
59904 
59905   /* If a transaction is still open on the Btree, roll it back. */
59906   sqlite3BtreeRollback(p->pDest, SQLITE_OK);
59907 
59908   /* Set the error code of the destination database handle. */
59909   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
59910   if( p->pDestDb ){
59911     sqlite3Error(p->pDestDb, rc, 0);
59912 
59913     /* Exit the mutexes and free the backup context structure. */
59914     sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
59915   }
59916   sqlite3BtreeLeave(p->pSrc);
59917   if( p->pDestDb ){
59918     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
59919     ** call to sqlite3_backup_init() and is destroyed by a call to
59920     ** sqlite3_backup_finish(). */
59921     sqlite3_free(p);
59922   }
59923   sqlite3LeaveMutexAndCloseZombie(pSrcDb);
59924   return rc;
59925 }
59926 
59927 /*
59928 ** Return the number of pages still to be backed up as of the most recent
59929 ** call to sqlite3_backup_step().
59930 */
59931 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
59932   return p->nRemaining;
59933 }
59934 
59935 /*
59936 ** Return the total number of pages in the source database as of the most
59937 ** recent call to sqlite3_backup_step().
59938 */
59939 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
59940   return p->nPagecount;
59941 }
59942 
59943 /*
59944 ** This function is called after the contents of page iPage of the
59945 ** source database have been modified. If page iPage has already been
59946 ** copied into the destination database, then the data written to the
59947 ** destination is now invalidated. The destination copy of iPage needs
59948 ** to be updated with the new data before the backup operation is
59949 ** complete.
59950 **
59951 ** It is assumed that the mutex associated with the BtShared object
59952 ** corresponding to the source database is held when this function is
59953 ** called.
59954 */
59955 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
59956   sqlite3_backup *p;                   /* Iterator variable */
59957   for(p=pBackup; p; p=p->pNext){
59958     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
59959     if( !isFatalError(p->rc) && iPage<p->iNext ){
59960       /* The backup process p has already copied page iPage. But now it
59961       ** has been modified by a transaction on the source pager. Copy
59962       ** the new data into the backup.
59963       */
59964       int rc;
59965       assert( p->pDestDb );
59966       sqlite3_mutex_enter(p->pDestDb->mutex);
59967       rc = backupOnePage(p, iPage, aData, 1);
59968       sqlite3_mutex_leave(p->pDestDb->mutex);
59969       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
59970       if( rc!=SQLITE_OK ){
59971         p->rc = rc;
59972       }
59973     }
59974   }
59975 }
59976 
59977 /*
59978 ** Restart the backup process. This is called when the pager layer
59979 ** detects that the database has been modified by an external database
59980 ** connection. In this case there is no way of knowing which of the
59981 ** pages that have been copied into the destination database are still
59982 ** valid and which are not, so the entire process needs to be restarted.
59983 **
59984 ** It is assumed that the mutex associated with the BtShared object
59985 ** corresponding to the source database is held when this function is
59986 ** called.
59987 */
59988 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
59989   sqlite3_backup *p;                   /* Iterator variable */
59990   for(p=pBackup; p; p=p->pNext){
59991     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
59992     p->iNext = 1;
59993   }
59994 }
59995 
59996 #ifndef SQLITE_OMIT_VACUUM
59997 /*
59998 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
59999 ** must be active for both files.
60000 **
60001 ** The size of file pTo may be reduced by this operation. If anything
60002 ** goes wrong, the transaction on pTo is rolled back. If successful, the
60003 ** transaction is committed before returning.
60004 */
60005 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
60006   int rc;
60007   sqlite3_file *pFd;              /* File descriptor for database pTo */
60008   sqlite3_backup b;
60009   sqlite3BtreeEnter(pTo);
60010   sqlite3BtreeEnter(pFrom);
60011 
60012   assert( sqlite3BtreeIsInTrans(pTo) );
60013   pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
60014   if( pFd->pMethods ){
60015     i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
60016     rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
60017     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
60018     if( rc ) goto copy_finished;
60019   }
60020 
60021   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
60022   ** to 0. This is used by the implementations of sqlite3_backup_step()
60023   ** and sqlite3_backup_finish() to detect that they are being called
60024   ** from this function, not directly by the user.
60025   */
60026   memset(&b, 0, sizeof(b));
60027   b.pSrcDb = pFrom->db;
60028   b.pSrc = pFrom;
60029   b.pDest = pTo;
60030   b.iNext = 1;
60031 
60032   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
60033   ** file. By passing this as the number of pages to copy to
60034   ** sqlite3_backup_step(), we can guarantee that the copy finishes
60035   ** within a single call (unless an error occurs). The assert() statement
60036   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
60037   ** or an error code.
60038   */
60039   sqlite3_backup_step(&b, 0x7FFFFFFF);
60040   assert( b.rc!=SQLITE_OK );
60041   rc = sqlite3_backup_finish(&b);
60042   if( rc==SQLITE_OK ){
60043     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
60044   }else{
60045     sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
60046   }
60047 
60048   assert( sqlite3BtreeIsInTrans(pTo)==0 );
60049 copy_finished:
60050   sqlite3BtreeLeave(pFrom);
60051   sqlite3BtreeLeave(pTo);
60052   return rc;
60053 }
60054 #endif /* SQLITE_OMIT_VACUUM */
60055 
60056 /************** End of backup.c **********************************************/
60057 /************** Begin file vdbemem.c *****************************************/
60058 /*
60059 ** 2004 May 26
60060 **
60061 ** The author disclaims copyright to this source code.  In place of
60062 ** a legal notice, here is a blessing:
60063 **
60064 **    May you do good and not evil.
60065 **    May you find forgiveness for yourself and forgive others.
60066 **    May you share freely, never taking more than you give.
60067 **
60068 *************************************************************************
60069 **
60070 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
60071 ** stores a single value in the VDBE.  Mem is an opaque structure visible
60072 ** only within the VDBE.  Interface routines refer to a Mem using the
60073 ** name sqlite_value
60074 */
60075 
60076 #ifdef SQLITE_DEBUG
60077 /*
60078 ** Check invariants on a Mem object.
60079 **
60080 ** This routine is intended for use inside of assert() statements, like
60081 ** this:    assert( sqlite3VdbeCheckMemInvariants(pMem) );
60082 */
60083 SQLITE_PRIVATE int sqlite3VdbeCheckMemInvariants(Mem *p){
60084   /* The MEM_Dyn bit is set if and only if Mem.xDel is a non-NULL destructor
60085   ** function for Mem.z
60086   */
60087   assert( (p->flags & MEM_Dyn)==0 || p->xDel!=0 );
60088   assert( (p->flags & MEM_Dyn)!=0 || p->xDel==0 );
60089 
60090   /* If p holds a string or blob, the Mem.z must point to exactly
60091   ** one of the following:
60092   **
60093   **   (1) Memory in Mem.zMalloc and managed by the Mem object
60094   **   (2) Memory to be freed using Mem.xDel
60095   **   (3) An ephermal string or blob
60096   **   (4) A static string or blob
60097   */
60098   if( (p->flags & (MEM_Str|MEM_Blob)) && p->z!=0 ){
60099     assert(
60100       ((p->z==p->zMalloc)? 1 : 0) +
60101       ((p->flags&MEM_Dyn)!=0 ? 1 : 0) +
60102       ((p->flags&MEM_Ephem)!=0 ? 1 : 0) +
60103       ((p->flags&MEM_Static)!=0 ? 1 : 0) == 1
60104     );
60105   }
60106 
60107   return 1;
60108 }
60109 #endif
60110 
60111 
60112 /*
60113 ** If pMem is an object with a valid string representation, this routine
60114 ** ensures the internal encoding for the string representation is
60115 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
60116 **
60117 ** If pMem is not a string object, or the encoding of the string
60118 ** representation is already stored using the requested encoding, then this
60119 ** routine is a no-op.
60120 **
60121 ** SQLITE_OK is returned if the conversion is successful (or not required).
60122 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
60123 ** between formats.
60124 */
60125 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
60126 #ifndef SQLITE_OMIT_UTF16
60127   int rc;
60128 #endif
60129   assert( (pMem->flags&MEM_RowSet)==0 );
60130   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
60131            || desiredEnc==SQLITE_UTF16BE );
60132   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
60133     return SQLITE_OK;
60134   }
60135   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60136 #ifdef SQLITE_OMIT_UTF16
60137   return SQLITE_ERROR;
60138 #else
60139 
60140   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
60141   ** then the encoding of the value may not have changed.
60142   */
60143   rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
60144   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
60145   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
60146   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
60147   return rc;
60148 #endif
60149 }
60150 
60151 /*
60152 ** Make sure pMem->z points to a writable allocation of at least
60153 ** min(n,32) bytes.
60154 **
60155 ** If the bPreserve argument is true, then copy of the content of
60156 ** pMem->z into the new allocation.  pMem must be either a string or
60157 ** blob if bPreserve is true.  If bPreserve is false, any prior content
60158 ** in pMem->z is discarded.
60159 */
60160 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
60161   assert( sqlite3VdbeCheckMemInvariants(pMem) );
60162   assert( (pMem->flags&MEM_RowSet)==0 );
60163 
60164   /* If the bPreserve flag is set to true, then the memory cell must already
60165   ** contain a valid string or blob value.  */
60166   assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
60167   testcase( bPreserve && pMem->z==0 );
60168 
60169   if( pMem->zMalloc==0 || sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
60170     if( n<32 ) n = 32;
60171     if( bPreserve && pMem->z==pMem->zMalloc ){
60172       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
60173       bPreserve = 0;
60174     }else{
60175       sqlite3DbFree(pMem->db, pMem->zMalloc);
60176       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
60177     }
60178     if( pMem->zMalloc==0 ){
60179       VdbeMemRelease(pMem);
60180       pMem->z = 0;
60181       pMem->flags = MEM_Null;
60182       return SQLITE_NOMEM;
60183     }
60184   }
60185 
60186   if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
60187     memcpy(pMem->zMalloc, pMem->z, pMem->n);
60188   }
60189   if( (pMem->flags&MEM_Dyn)!=0 ){
60190     assert( pMem->xDel!=0 && pMem->xDel!=SQLITE_DYNAMIC );
60191     pMem->xDel((void *)(pMem->z));
60192   }
60193 
60194   pMem->z = pMem->zMalloc;
60195   pMem->flags &= ~(MEM_Dyn|MEM_Ephem|MEM_Static);
60196   pMem->xDel = 0;
60197   return SQLITE_OK;
60198 }
60199 
60200 /*
60201 ** Make the given Mem object MEM_Dyn.  In other words, make it so
60202 ** that any TEXT or BLOB content is stored in memory obtained from
60203 ** malloc().  In this way, we know that the memory is safe to be
60204 ** overwritten or altered.
60205 **
60206 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
60207 */
60208 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
60209   int f;
60210   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60211   assert( (pMem->flags&MEM_RowSet)==0 );
60212   ExpandBlob(pMem);
60213   f = pMem->flags;
60214   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
60215     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
60216       return SQLITE_NOMEM;
60217     }
60218     pMem->z[pMem->n] = 0;
60219     pMem->z[pMem->n+1] = 0;
60220     pMem->flags |= MEM_Term;
60221 #ifdef SQLITE_DEBUG
60222     pMem->pScopyFrom = 0;
60223 #endif
60224   }
60225 
60226   return SQLITE_OK;
60227 }
60228 
60229 /*
60230 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
60231 ** blob stored in dynamically allocated space.
60232 */
60233 #ifndef SQLITE_OMIT_INCRBLOB
60234 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
60235   if( pMem->flags & MEM_Zero ){
60236     int nByte;
60237     assert( pMem->flags&MEM_Blob );
60238     assert( (pMem->flags&MEM_RowSet)==0 );
60239     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60240 
60241     /* Set nByte to the number of bytes required to store the expanded blob. */
60242     nByte = pMem->n + pMem->u.nZero;
60243     if( nByte<=0 ){
60244       nByte = 1;
60245     }
60246     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
60247       return SQLITE_NOMEM;
60248     }
60249 
60250     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
60251     pMem->n += pMem->u.nZero;
60252     pMem->flags &= ~(MEM_Zero|MEM_Term);
60253   }
60254   return SQLITE_OK;
60255 }
60256 #endif
60257 
60258 
60259 /*
60260 ** Make sure the given Mem is \u0000 terminated.
60261 */
60262 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
60263   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60264   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
60265     return SQLITE_OK;   /* Nothing to do */
60266   }
60267   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
60268     return SQLITE_NOMEM;
60269   }
60270   pMem->z[pMem->n] = 0;
60271   pMem->z[pMem->n+1] = 0;
60272   pMem->flags |= MEM_Term;
60273   return SQLITE_OK;
60274 }
60275 
60276 /*
60277 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
60278 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
60279 ** is a no-op.
60280 **
60281 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
60282 **
60283 ** A MEM_Null value will never be passed to this function. This function is
60284 ** used for converting values to text for returning to the user (i.e. via
60285 ** sqlite3_value_text()), or for ensuring that values to be used as btree
60286 ** keys are strings. In the former case a NULL pointer is returned the
60287 ** user and the later is an internal programming error.
60288 */
60289 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
60290   int rc = SQLITE_OK;
60291   int fg = pMem->flags;
60292   const int nByte = 32;
60293 
60294   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60295   assert( !(fg&MEM_Zero) );
60296   assert( !(fg&(MEM_Str|MEM_Blob)) );
60297   assert( fg&(MEM_Int|MEM_Real) );
60298   assert( (pMem->flags&MEM_RowSet)==0 );
60299   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60300 
60301 
60302   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
60303     return SQLITE_NOMEM;
60304   }
60305 
60306   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
60307   ** string representation of the value. Then, if the required encoding
60308   ** is UTF-16le or UTF-16be do a translation.
60309   **
60310   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
60311   */
60312   if( fg & MEM_Int ){
60313     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
60314   }else{
60315     assert( fg & MEM_Real );
60316     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
60317   }
60318   pMem->n = sqlite3Strlen30(pMem->z);
60319   pMem->enc = SQLITE_UTF8;
60320   pMem->flags |= MEM_Str|MEM_Term;
60321   sqlite3VdbeChangeEncoding(pMem, enc);
60322   return rc;
60323 }
60324 
60325 /*
60326 ** Memory cell pMem contains the context of an aggregate function.
60327 ** This routine calls the finalize method for that function.  The
60328 ** result of the aggregate is stored back into pMem.
60329 **
60330 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
60331 ** otherwise.
60332 */
60333 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
60334   int rc = SQLITE_OK;
60335   if( ALWAYS(pFunc && pFunc->xFinalize) ){
60336     sqlite3_context ctx;
60337     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
60338     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60339     memset(&ctx, 0, sizeof(ctx));
60340     ctx.s.flags = MEM_Null;
60341     ctx.s.db = pMem->db;
60342     ctx.pMem = pMem;
60343     ctx.pFunc = pFunc;
60344     pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
60345     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
60346     sqlite3DbFree(pMem->db, pMem->zMalloc);
60347     memcpy(pMem, &ctx.s, sizeof(ctx.s));
60348     rc = ctx.isError;
60349   }
60350   return rc;
60351 }
60352 
60353 /*
60354 ** If the memory cell contains a string value that must be freed by
60355 ** invoking an external callback, free it now. Calling this function
60356 ** does not free any Mem.zMalloc buffer.
60357 */
60358 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
60359   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
60360   if( p->flags&MEM_Agg ){
60361     sqlite3VdbeMemFinalize(p, p->u.pDef);
60362     assert( (p->flags & MEM_Agg)==0 );
60363     sqlite3VdbeMemRelease(p);
60364   }else if( p->flags&MEM_Dyn ){
60365     assert( (p->flags&MEM_RowSet)==0 );
60366     assert( p->xDel!=SQLITE_DYNAMIC && p->xDel!=0 );
60367     p->xDel((void *)p->z);
60368     p->xDel = 0;
60369   }else if( p->flags&MEM_RowSet ){
60370     sqlite3RowSetClear(p->u.pRowSet);
60371   }else if( p->flags&MEM_Frame ){
60372     sqlite3VdbeMemSetNull(p);
60373   }
60374 }
60375 
60376 /*
60377 ** Release any memory held by the Mem. This may leave the Mem in an
60378 ** inconsistent state, for example with (Mem.z==0) and
60379 ** (Mem.flags==MEM_Str).
60380 */
60381 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
60382   assert( sqlite3VdbeCheckMemInvariants(p) );
60383   VdbeMemRelease(p);
60384   if( p->zMalloc ){
60385     sqlite3DbFree(p->db, p->zMalloc);
60386     p->zMalloc = 0;
60387   }
60388   p->z = 0;
60389   assert( p->xDel==0 );  /* Zeroed by VdbeMemRelease() above */
60390 }
60391 
60392 /*
60393 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
60394 ** If the double is out of range of a 64-bit signed integer then
60395 ** return the closest available 64-bit signed integer.
60396 */
60397 static i64 doubleToInt64(double r){
60398 #ifdef SQLITE_OMIT_FLOATING_POINT
60399   /* When floating-point is omitted, double and int64 are the same thing */
60400   return r;
60401 #else
60402   /*
60403   ** Many compilers we encounter do not define constants for the
60404   ** minimum and maximum 64-bit integers, or they define them
60405   ** inconsistently.  And many do not understand the "LL" notation.
60406   ** So we define our own static constants here using nothing
60407   ** larger than a 32-bit integer constant.
60408   */
60409   static const i64 maxInt = LARGEST_INT64;
60410   static const i64 minInt = SMALLEST_INT64;
60411 
60412   if( r<=(double)minInt ){
60413     return minInt;
60414   }else if( r>=(double)maxInt ){
60415     return maxInt;
60416   }else{
60417     return (i64)r;
60418   }
60419 #endif
60420 }
60421 
60422 /*
60423 ** Return some kind of integer value which is the best we can do
60424 ** at representing the value that *pMem describes as an integer.
60425 ** If pMem is an integer, then the value is exact.  If pMem is
60426 ** a floating-point then the value returned is the integer part.
60427 ** If pMem is a string or blob, then we make an attempt to convert
60428 ** it into a integer and return that.  If pMem represents an
60429 ** an SQL-NULL value, return 0.
60430 **
60431 ** If pMem represents a string value, its encoding might be changed.
60432 */
60433 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
60434   int flags;
60435   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60436   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60437   flags = pMem->flags;
60438   if( flags & MEM_Int ){
60439     return pMem->u.i;
60440   }else if( flags & MEM_Real ){
60441     return doubleToInt64(pMem->r);
60442   }else if( flags & (MEM_Str|MEM_Blob) ){
60443     i64 value = 0;
60444     assert( pMem->z || pMem->n==0 );
60445     testcase( pMem->z==0 );
60446     sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
60447     return value;
60448   }else{
60449     return 0;
60450   }
60451 }
60452 
60453 /*
60454 ** Return the best representation of pMem that we can get into a
60455 ** double.  If pMem is already a double or an integer, return its
60456 ** value.  If it is a string or blob, try to convert it to a double.
60457 ** If it is a NULL, return 0.0.
60458 */
60459 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
60460   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60461   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60462   if( pMem->flags & MEM_Real ){
60463     return pMem->r;
60464   }else if( pMem->flags & MEM_Int ){
60465     return (double)pMem->u.i;
60466   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
60467     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
60468     double val = (double)0;
60469     sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
60470     return val;
60471   }else{
60472     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
60473     return (double)0;
60474   }
60475 }
60476 
60477 /*
60478 ** The MEM structure is already a MEM_Real.  Try to also make it a
60479 ** MEM_Int if we can.
60480 */
60481 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
60482   assert( pMem->flags & MEM_Real );
60483   assert( (pMem->flags & MEM_RowSet)==0 );
60484   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60485   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60486 
60487   pMem->u.i = doubleToInt64(pMem->r);
60488 
60489   /* Only mark the value as an integer if
60490   **
60491   **    (1) the round-trip conversion real->int->real is a no-op, and
60492   **    (2) The integer is neither the largest nor the smallest
60493   **        possible integer (ticket #3922)
60494   **
60495   ** The second and third terms in the following conditional enforces
60496   ** the second condition under the assumption that addition overflow causes
60497   ** values to wrap around.
60498   */
60499   if( pMem->r==(double)pMem->u.i
60500    && pMem->u.i>SMALLEST_INT64
60501    && pMem->u.i<LARGEST_INT64
60502   ){
60503     pMem->flags |= MEM_Int;
60504   }
60505 }
60506 
60507 /*
60508 ** Convert pMem to type integer.  Invalidate any prior representations.
60509 */
60510 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
60511   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60512   assert( (pMem->flags & MEM_RowSet)==0 );
60513   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60514 
60515   pMem->u.i = sqlite3VdbeIntValue(pMem);
60516   MemSetTypeFlag(pMem, MEM_Int);
60517   return SQLITE_OK;
60518 }
60519 
60520 /*
60521 ** Convert pMem so that it is of type MEM_Real.
60522 ** Invalidate any prior representations.
60523 */
60524 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
60525   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60526   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60527 
60528   pMem->r = sqlite3VdbeRealValue(pMem);
60529   MemSetTypeFlag(pMem, MEM_Real);
60530   return SQLITE_OK;
60531 }
60532 
60533 /*
60534 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
60535 ** Invalidate any prior representations.
60536 **
60537 ** Every effort is made to force the conversion, even if the input
60538 ** is a string that does not look completely like a number.  Convert
60539 ** as much of the string as we can and ignore the rest.
60540 */
60541 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
60542   if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
60543     assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
60544     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60545     if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
60546       MemSetTypeFlag(pMem, MEM_Int);
60547     }else{
60548       pMem->r = sqlite3VdbeRealValue(pMem);
60549       MemSetTypeFlag(pMem, MEM_Real);
60550       sqlite3VdbeIntegerAffinity(pMem);
60551     }
60552   }
60553   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
60554   pMem->flags &= ~(MEM_Str|MEM_Blob);
60555   return SQLITE_OK;
60556 }
60557 
60558 /*
60559 ** Delete any previous value and set the value stored in *pMem to NULL.
60560 */
60561 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
60562   if( pMem->flags & MEM_Frame ){
60563     VdbeFrame *pFrame = pMem->u.pFrame;
60564     pFrame->pParent = pFrame->v->pDelFrame;
60565     pFrame->v->pDelFrame = pFrame;
60566   }
60567   if( pMem->flags & MEM_RowSet ){
60568     sqlite3RowSetClear(pMem->u.pRowSet);
60569   }
60570   MemSetTypeFlag(pMem, MEM_Null);
60571 }
60572 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
60573   sqlite3VdbeMemSetNull((Mem*)p);
60574 }
60575 
60576 /*
60577 ** Delete any previous value and set the value to be a BLOB of length
60578 ** n containing all zeros.
60579 */
60580 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
60581   sqlite3VdbeMemRelease(pMem);
60582   pMem->flags = MEM_Blob|MEM_Zero;
60583   pMem->n = 0;
60584   if( n<0 ) n = 0;
60585   pMem->u.nZero = n;
60586   pMem->enc = SQLITE_UTF8;
60587 
60588 #ifdef SQLITE_OMIT_INCRBLOB
60589   sqlite3VdbeMemGrow(pMem, n, 0);
60590   if( pMem->z ){
60591     pMem->n = n;
60592     memset(pMem->z, 0, n);
60593   }
60594 #endif
60595 }
60596 
60597 /*
60598 ** Delete any previous value and set the value stored in *pMem to val,
60599 ** manifest type INTEGER.
60600 */
60601 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
60602   sqlite3VdbeMemRelease(pMem);
60603   pMem->u.i = val;
60604   pMem->flags = MEM_Int;
60605 }
60606 
60607 #ifndef SQLITE_OMIT_FLOATING_POINT
60608 /*
60609 ** Delete any previous value and set the value stored in *pMem to val,
60610 ** manifest type REAL.
60611 */
60612 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
60613   if( sqlite3IsNaN(val) ){
60614     sqlite3VdbeMemSetNull(pMem);
60615   }else{
60616     sqlite3VdbeMemRelease(pMem);
60617     pMem->r = val;
60618     pMem->flags = MEM_Real;
60619   }
60620 }
60621 #endif
60622 
60623 /*
60624 ** Delete any previous value and set the value of pMem to be an
60625 ** empty boolean index.
60626 */
60627 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
60628   sqlite3 *db = pMem->db;
60629   assert( db!=0 );
60630   assert( (pMem->flags & MEM_RowSet)==0 );
60631   sqlite3VdbeMemRelease(pMem);
60632   pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
60633   if( db->mallocFailed ){
60634     pMem->flags = MEM_Null;
60635   }else{
60636     assert( pMem->zMalloc );
60637     pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
60638                                        sqlite3DbMallocSize(db, pMem->zMalloc));
60639     assert( pMem->u.pRowSet!=0 );
60640     pMem->flags = MEM_RowSet;
60641   }
60642 }
60643 
60644 /*
60645 ** Return true if the Mem object contains a TEXT or BLOB that is
60646 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
60647 */
60648 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
60649   assert( p->db!=0 );
60650   if( p->flags & (MEM_Str|MEM_Blob) ){
60651     int n = p->n;
60652     if( p->flags & MEM_Zero ){
60653       n += p->u.nZero;
60654     }
60655     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
60656   }
60657   return 0;
60658 }
60659 
60660 #ifdef SQLITE_DEBUG
60661 /*
60662 ** This routine prepares a memory cell for modication by breaking
60663 ** its link to a shallow copy and by marking any current shallow
60664 ** copies of this cell as invalid.
60665 **
60666 ** This is used for testing and debugging only - to make sure shallow
60667 ** copies are not misused.
60668 */
60669 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
60670   int i;
60671   Mem *pX;
60672   for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
60673     if( pX->pScopyFrom==pMem ){
60674       pX->flags |= MEM_Undefined;
60675       pX->pScopyFrom = 0;
60676     }
60677   }
60678   pMem->pScopyFrom = 0;
60679 }
60680 #endif /* SQLITE_DEBUG */
60681 
60682 /*
60683 ** Size of struct Mem not including the Mem.zMalloc member.
60684 */
60685 #define MEMCELLSIZE offsetof(Mem,zMalloc)
60686 
60687 /*
60688 ** Make an shallow copy of pFrom into pTo.  Prior contents of
60689 ** pTo are freed.  The pFrom->z field is not duplicated.  If
60690 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
60691 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
60692 */
60693 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
60694   assert( (pFrom->flags & MEM_RowSet)==0 );
60695   VdbeMemRelease(pTo);
60696   memcpy(pTo, pFrom, MEMCELLSIZE);
60697   pTo->xDel = 0;
60698   if( (pFrom->flags&MEM_Static)==0 ){
60699     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
60700     assert( srcType==MEM_Ephem || srcType==MEM_Static );
60701     pTo->flags |= srcType;
60702   }
60703 }
60704 
60705 /*
60706 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
60707 ** freed before the copy is made.
60708 */
60709 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
60710   int rc = SQLITE_OK;
60711 
60712   assert( (pFrom->flags & MEM_RowSet)==0 );
60713   VdbeMemRelease(pTo);
60714   memcpy(pTo, pFrom, MEMCELLSIZE);
60715   pTo->flags &= ~MEM_Dyn;
60716   pTo->xDel = 0;
60717 
60718   if( pTo->flags&(MEM_Str|MEM_Blob) ){
60719     if( 0==(pFrom->flags&MEM_Static) ){
60720       pTo->flags |= MEM_Ephem;
60721       rc = sqlite3VdbeMemMakeWriteable(pTo);
60722     }
60723   }
60724 
60725   return rc;
60726 }
60727 
60728 /*
60729 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
60730 ** freed. If pFrom contains ephemeral data, a copy is made.
60731 **
60732 ** pFrom contains an SQL NULL when this routine returns.
60733 */
60734 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
60735   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
60736   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
60737   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
60738 
60739   sqlite3VdbeMemRelease(pTo);
60740   memcpy(pTo, pFrom, sizeof(Mem));
60741   pFrom->flags = MEM_Null;
60742   pFrom->xDel = 0;
60743   pFrom->zMalloc = 0;
60744 }
60745 
60746 /*
60747 ** Change the value of a Mem to be a string or a BLOB.
60748 **
60749 ** The memory management strategy depends on the value of the xDel
60750 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
60751 ** string is copied into a (possibly existing) buffer managed by the
60752 ** Mem structure. Otherwise, any existing buffer is freed and the
60753 ** pointer copied.
60754 **
60755 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
60756 ** size limit) then no memory allocation occurs.  If the string can be
60757 ** stored without allocating memory, then it is.  If a memory allocation
60758 ** is required to store the string, then value of pMem is unchanged.  In
60759 ** either case, SQLITE_TOOBIG is returned.
60760 */
60761 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
60762   Mem *pMem,          /* Memory cell to set to string value */
60763   const char *z,      /* String pointer */
60764   int n,              /* Bytes in string, or negative */
60765   u8 enc,             /* Encoding of z.  0 for BLOBs */
60766   void (*xDel)(void*) /* Destructor function */
60767 ){
60768   int nByte = n;      /* New value for pMem->n */
60769   int iLimit;         /* Maximum allowed string or blob size */
60770   u16 flags = 0;      /* New value for pMem->flags */
60771 
60772   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60773   assert( (pMem->flags & MEM_RowSet)==0 );
60774 
60775   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
60776   if( !z ){
60777     sqlite3VdbeMemSetNull(pMem);
60778     return SQLITE_OK;
60779   }
60780 
60781   if( pMem->db ){
60782     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
60783   }else{
60784     iLimit = SQLITE_MAX_LENGTH;
60785   }
60786   flags = (enc==0?MEM_Blob:MEM_Str);
60787   if( nByte<0 ){
60788     assert( enc!=0 );
60789     if( enc==SQLITE_UTF8 ){
60790       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
60791     }else{
60792       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
60793     }
60794     flags |= MEM_Term;
60795   }
60796 
60797   /* The following block sets the new values of Mem.z and Mem.xDel. It
60798   ** also sets a flag in local variable "flags" to indicate the memory
60799   ** management (one of MEM_Dyn or MEM_Static).
60800   */
60801   if( xDel==SQLITE_TRANSIENT ){
60802     int nAlloc = nByte;
60803     if( flags&MEM_Term ){
60804       nAlloc += (enc==SQLITE_UTF8?1:2);
60805     }
60806     if( nByte>iLimit ){
60807       return SQLITE_TOOBIG;
60808     }
60809     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
60810       return SQLITE_NOMEM;
60811     }
60812     memcpy(pMem->z, z, nAlloc);
60813   }else if( xDel==SQLITE_DYNAMIC ){
60814     sqlite3VdbeMemRelease(pMem);
60815     pMem->zMalloc = pMem->z = (char *)z;
60816     pMem->xDel = 0;
60817   }else{
60818     sqlite3VdbeMemRelease(pMem);
60819     pMem->z = (char *)z;
60820     pMem->xDel = xDel;
60821     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
60822   }
60823 
60824   pMem->n = nByte;
60825   pMem->flags = flags;
60826   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
60827 
60828 #ifndef SQLITE_OMIT_UTF16
60829   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
60830     return SQLITE_NOMEM;
60831   }
60832 #endif
60833 
60834   if( nByte>iLimit ){
60835     return SQLITE_TOOBIG;
60836   }
60837 
60838   return SQLITE_OK;
60839 }
60840 
60841 /*
60842 ** Move data out of a btree key or data field and into a Mem structure.
60843 ** The data or key is taken from the entry that pCur is currently pointing
60844 ** to.  offset and amt determine what portion of the data or key to retrieve.
60845 ** key is true to get the key or false to get data.  The result is written
60846 ** into the pMem element.
60847 **
60848 ** The pMem structure is assumed to be uninitialized.  Any prior content
60849 ** is overwritten without being freed.
60850 **
60851 ** If this routine fails for any reason (malloc returns NULL or unable
60852 ** to read from the disk) then the pMem is left in an inconsistent state.
60853 */
60854 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
60855   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
60856   u32 offset,       /* Offset from the start of data to return bytes from. */
60857   u32 amt,          /* Number of bytes to return. */
60858   int key,          /* If true, retrieve from the btree key, not data. */
60859   Mem *pMem         /* OUT: Return data in this Mem structure. */
60860 ){
60861   char *zData;        /* Data from the btree layer */
60862   u32 available = 0;  /* Number of bytes available on the local btree page */
60863   int rc = SQLITE_OK; /* Return code */
60864 
60865   assert( sqlite3BtreeCursorIsValid(pCur) );
60866 
60867   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
60868   ** that both the BtShared and database handle mutexes are held. */
60869   assert( (pMem->flags & MEM_RowSet)==0 );
60870   if( key ){
60871     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
60872   }else{
60873     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
60874   }
60875   assert( zData!=0 );
60876 
60877   if( offset+amt<=available ){
60878     sqlite3VdbeMemRelease(pMem);
60879     pMem->z = &zData[offset];
60880     pMem->flags = MEM_Blob|MEM_Ephem;
60881     pMem->n = (int)amt;
60882   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
60883     if( key ){
60884       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
60885     }else{
60886       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
60887     }
60888     if( rc==SQLITE_OK ){
60889       pMem->z[amt] = 0;
60890       pMem->z[amt+1] = 0;
60891       pMem->flags = MEM_Blob|MEM_Term;
60892       pMem->n = (int)amt;
60893     }else{
60894       sqlite3VdbeMemRelease(pMem);
60895     }
60896   }
60897 
60898   return rc;
60899 }
60900 
60901 /* This function is only available internally, it is not part of the
60902 ** external API. It works in a similar way to sqlite3_value_text(),
60903 ** except the data returned is in the encoding specified by the second
60904 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
60905 ** SQLITE_UTF8.
60906 **
60907 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
60908 ** If that is the case, then the result must be aligned on an even byte
60909 ** boundary.
60910 */
60911 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
60912   if( !pVal ) return 0;
60913 
60914   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
60915   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
60916   assert( (pVal->flags & MEM_RowSet)==0 );
60917 
60918   if( pVal->flags&MEM_Null ){
60919     return 0;
60920   }
60921   assert( (MEM_Blob>>3) == MEM_Str );
60922   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
60923   ExpandBlob(pVal);
60924   if( pVal->flags&MEM_Str ){
60925     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
60926     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
60927       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
60928       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
60929         return 0;
60930       }
60931     }
60932     sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
60933   }else{
60934     assert( (pVal->flags&MEM_Blob)==0 );
60935     sqlite3VdbeMemStringify(pVal, enc);
60936     assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
60937   }
60938   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
60939               || pVal->db->mallocFailed );
60940   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
60941     return pVal->z;
60942   }else{
60943     return 0;
60944   }
60945 }
60946 
60947 /*
60948 ** Create a new sqlite3_value object.
60949 */
60950 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
60951   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
60952   if( p ){
60953     p->flags = MEM_Null;
60954     p->db = db;
60955   }
60956   return p;
60957 }
60958 
60959 /*
60960 ** Context object passed by sqlite3Stat4ProbeSetValue() through to
60961 ** valueNew(). See comments above valueNew() for details.
60962 */
60963 struct ValueNewStat4Ctx {
60964   Parse *pParse;
60965   Index *pIdx;
60966   UnpackedRecord **ppRec;
60967   int iVal;
60968 };
60969 
60970 /*
60971 ** Allocate and return a pointer to a new sqlite3_value object. If
60972 ** the second argument to this function is NULL, the object is allocated
60973 ** by calling sqlite3ValueNew().
60974 **
60975 ** Otherwise, if the second argument is non-zero, then this function is
60976 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
60977 ** already been allocated, allocate the UnpackedRecord structure that
60978 ** that function will return to its caller here. Then return a pointer
60979 ** an sqlite3_value within the UnpackedRecord.a[] array.
60980 */
60981 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
60982 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60983   if( p ){
60984     UnpackedRecord *pRec = p->ppRec[0];
60985 
60986     if( pRec==0 ){
60987       Index *pIdx = p->pIdx;      /* Index being probed */
60988       int nByte;                  /* Bytes of space to allocate */
60989       int i;                      /* Counter variable */
60990       int nCol = pIdx->nColumn;   /* Number of index columns including rowid */
60991 
60992       nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
60993       pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
60994       if( pRec ){
60995         pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
60996         if( pRec->pKeyInfo ){
60997           assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
60998           assert( pRec->pKeyInfo->enc==ENC(db) );
60999           pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
61000           for(i=0; i<nCol; i++){
61001             pRec->aMem[i].flags = MEM_Null;
61002             pRec->aMem[i].db = db;
61003           }
61004         }else{
61005           sqlite3DbFree(db, pRec);
61006           pRec = 0;
61007         }
61008       }
61009       if( pRec==0 ) return 0;
61010       p->ppRec[0] = pRec;
61011     }
61012 
61013     pRec->nField = p->iVal+1;
61014     return &pRec->aMem[p->iVal];
61015   }
61016 #else
61017   UNUSED_PARAMETER(p);
61018 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
61019   return sqlite3ValueNew(db);
61020 }
61021 
61022 /*
61023 ** Extract a value from the supplied expression in the manner described
61024 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
61025 ** using valueNew().
61026 **
61027 ** If pCtx is NULL and an error occurs after the sqlite3_value object
61028 ** has been allocated, it is freed before returning. Or, if pCtx is not
61029 ** NULL, it is assumed that the caller will free any allocated object
61030 ** in all cases.
61031 */
61032 static int valueFromExpr(
61033   sqlite3 *db,                    /* The database connection */
61034   Expr *pExpr,                    /* The expression to evaluate */
61035   u8 enc,                         /* Encoding to use */
61036   u8 affinity,                    /* Affinity to use */
61037   sqlite3_value **ppVal,          /* Write the new value here */
61038   struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
61039 ){
61040   int op;
61041   char *zVal = 0;
61042   sqlite3_value *pVal = 0;
61043   int negInt = 1;
61044   const char *zNeg = "";
61045   int rc = SQLITE_OK;
61046 
61047   if( !pExpr ){
61048     *ppVal = 0;
61049     return SQLITE_OK;
61050   }
61051   op = pExpr->op;
61052   if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
61053 
61054   /* Handle negative integers in a single step.  This is needed in the
61055   ** case when the value is -9223372036854775808.
61056   */
61057   if( op==TK_UMINUS
61058    && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
61059     pExpr = pExpr->pLeft;
61060     op = pExpr->op;
61061     negInt = -1;
61062     zNeg = "-";
61063   }
61064 
61065   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
61066     pVal = valueNew(db, pCtx);
61067     if( pVal==0 ) goto no_mem;
61068     if( ExprHasProperty(pExpr, EP_IntValue) ){
61069       sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
61070     }else{
61071       zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
61072       if( zVal==0 ) goto no_mem;
61073       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
61074     }
61075     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
61076       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
61077     }else{
61078       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
61079     }
61080     if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
61081     if( enc!=SQLITE_UTF8 ){
61082       rc = sqlite3VdbeChangeEncoding(pVal, enc);
61083     }
61084   }else if( op==TK_UMINUS ) {
61085     /* This branch happens for multiple negative signs.  Ex: -(-5) */
61086     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
61087      && pVal!=0
61088     ){
61089       sqlite3VdbeMemNumerify(pVal);
61090       if( pVal->u.i==SMALLEST_INT64 ){
61091         pVal->flags &= ~MEM_Int;
61092         pVal->flags |= MEM_Real;
61093         pVal->r = (double)SMALLEST_INT64;
61094       }else{
61095         pVal->u.i = -pVal->u.i;
61096       }
61097       pVal->r = -pVal->r;
61098       sqlite3ValueApplyAffinity(pVal, affinity, enc);
61099     }
61100   }else if( op==TK_NULL ){
61101     pVal = valueNew(db, pCtx);
61102     if( pVal==0 ) goto no_mem;
61103   }
61104 #ifndef SQLITE_OMIT_BLOB_LITERAL
61105   else if( op==TK_BLOB ){
61106     int nVal;
61107     assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
61108     assert( pExpr->u.zToken[1]=='\'' );
61109     pVal = valueNew(db, pCtx);
61110     if( !pVal ) goto no_mem;
61111     zVal = &pExpr->u.zToken[2];
61112     nVal = sqlite3Strlen30(zVal)-1;
61113     assert( zVal[nVal]=='\'' );
61114     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
61115                          0, SQLITE_DYNAMIC);
61116   }
61117 #endif
61118 
61119   *ppVal = pVal;
61120   return rc;
61121 
61122 no_mem:
61123   db->mallocFailed = 1;
61124   sqlite3DbFree(db, zVal);
61125   assert( *ppVal==0 );
61126 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
61127   if( pCtx==0 ) sqlite3ValueFree(pVal);
61128 #else
61129   assert( pCtx==0 ); sqlite3ValueFree(pVal);
61130 #endif
61131   return SQLITE_NOMEM;
61132 }
61133 
61134 /*
61135 ** Create a new sqlite3_value object, containing the value of pExpr.
61136 **
61137 ** This only works for very simple expressions that consist of one constant
61138 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
61139 ** be converted directly into a value, then the value is allocated and
61140 ** a pointer written to *ppVal. The caller is responsible for deallocating
61141 ** the value by passing it to sqlite3ValueFree() later on. If the expression
61142 ** cannot be converted to a value, then *ppVal is set to NULL.
61143 */
61144 SQLITE_PRIVATE int sqlite3ValueFromExpr(
61145   sqlite3 *db,              /* The database connection */
61146   Expr *pExpr,              /* The expression to evaluate */
61147   u8 enc,                   /* Encoding to use */
61148   u8 affinity,              /* Affinity to use */
61149   sqlite3_value **ppVal     /* Write the new value here */
61150 ){
61151   return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
61152 }
61153 
61154 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
61155 /*
61156 ** The implementation of the sqlite_record() function. This function accepts
61157 ** a single argument of any type. The return value is a formatted database
61158 ** record (a blob) containing the argument value.
61159 **
61160 ** This is used to convert the value stored in the 'sample' column of the
61161 ** sqlite_stat3 table to the record format SQLite uses internally.
61162 */
61163 static void recordFunc(
61164   sqlite3_context *context,
61165   int argc,
61166   sqlite3_value **argv
61167 ){
61168   const int file_format = 1;
61169   int iSerial;                    /* Serial type */
61170   int nSerial;                    /* Bytes of space for iSerial as varint */
61171   int nVal;                       /* Bytes of space required for argv[0] */
61172   int nRet;
61173   sqlite3 *db;
61174   u8 *aRet;
61175 
61176   UNUSED_PARAMETER( argc );
61177   iSerial = sqlite3VdbeSerialType(argv[0], file_format);
61178   nSerial = sqlite3VarintLen(iSerial);
61179   nVal = sqlite3VdbeSerialTypeLen(iSerial);
61180   db = sqlite3_context_db_handle(context);
61181 
61182   nRet = 1 + nSerial + nVal;
61183   aRet = sqlite3DbMallocRaw(db, nRet);
61184   if( aRet==0 ){
61185     sqlite3_result_error_nomem(context);
61186   }else{
61187     aRet[0] = nSerial+1;
61188     sqlite3PutVarint(&aRet[1], iSerial);
61189     sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
61190     sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
61191     sqlite3DbFree(db, aRet);
61192   }
61193 }
61194 
61195 /*
61196 ** Register built-in functions used to help read ANALYZE data.
61197 */
61198 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
61199   static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
61200     FUNCTION(sqlite_record,   1, 0, 0, recordFunc),
61201   };
61202   int i;
61203   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
61204   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
61205   for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
61206     sqlite3FuncDefInsert(pHash, &aFunc[i]);
61207   }
61208 }
61209 
61210 /*
61211 ** This function is used to allocate and populate UnpackedRecord
61212 ** structures intended to be compared against sample index keys stored
61213 ** in the sqlite_stat4 table.
61214 **
61215 ** A single call to this function attempts to populates field iVal (leftmost
61216 ** is 0 etc.) of the unpacked record with a value extracted from expression
61217 ** pExpr. Extraction of values is possible if:
61218 **
61219 **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
61220 **
61221 **  * The expression is a bound variable, and this is a reprepare, or
61222 **
61223 **  * The sqlite3ValueFromExpr() function is able to extract a value
61224 **    from the expression (i.e. the expression is a literal value).
61225 **
61226 ** If a value can be extracted, the affinity passed as the 5th argument
61227 ** is applied to it before it is copied into the UnpackedRecord. Output
61228 ** parameter *pbOk is set to true if a value is extracted, or false
61229 ** otherwise.
61230 **
61231 ** When this function is called, *ppRec must either point to an object
61232 ** allocated by an earlier call to this function, or must be NULL. If it
61233 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
61234 ** is allocated (and *ppRec set to point to it) before returning.
61235 **
61236 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
61237 ** error if a value cannot be extracted from pExpr. If an error does
61238 ** occur, an SQLite error code is returned.
61239 */
61240 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
61241   Parse *pParse,                  /* Parse context */
61242   Index *pIdx,                    /* Index being probed */
61243   UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
61244   Expr *pExpr,                    /* The expression to extract a value from */
61245   u8 affinity,                    /* Affinity to use */
61246   int iVal,                       /* Array element to populate */
61247   int *pbOk                       /* OUT: True if value was extracted */
61248 ){
61249   int rc = SQLITE_OK;
61250   sqlite3_value *pVal = 0;
61251   sqlite3 *db = pParse->db;
61252 
61253 
61254   struct ValueNewStat4Ctx alloc;
61255   alloc.pParse = pParse;
61256   alloc.pIdx = pIdx;
61257   alloc.ppRec = ppRec;
61258   alloc.iVal = iVal;
61259 
61260   /* Skip over any TK_COLLATE nodes */
61261   pExpr = sqlite3ExprSkipCollate(pExpr);
61262 
61263   if( !pExpr ){
61264     pVal = valueNew(db, &alloc);
61265     if( pVal ){
61266       sqlite3VdbeMemSetNull((Mem*)pVal);
61267     }
61268   }else if( pExpr->op==TK_VARIABLE
61269         || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
61270   ){
61271     Vdbe *v;
61272     int iBindVar = pExpr->iColumn;
61273     sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
61274     if( (v = pParse->pReprepare)!=0 ){
61275       pVal = valueNew(db, &alloc);
61276       if( pVal ){
61277         rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
61278         if( rc==SQLITE_OK ){
61279           sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
61280         }
61281         pVal->db = pParse->db;
61282       }
61283     }
61284   }else{
61285     rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
61286   }
61287   *pbOk = (pVal!=0);
61288 
61289   assert( pVal==0 || pVal->db==db );
61290   return rc;
61291 }
61292 
61293 /*
61294 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
61295 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
61296 ** the object.
61297 */
61298 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
61299   if( pRec ){
61300     int i;
61301     int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
61302     Mem *aMem = pRec->aMem;
61303     sqlite3 *db = aMem[0].db;
61304     for(i=0; i<nCol; i++){
61305       sqlite3DbFree(db, aMem[i].zMalloc);
61306     }
61307     sqlite3KeyInfoUnref(pRec->pKeyInfo);
61308     sqlite3DbFree(db, pRec);
61309   }
61310 }
61311 #endif /* ifdef SQLITE_ENABLE_STAT4 */
61312 
61313 /*
61314 ** Change the string value of an sqlite3_value object
61315 */
61316 SQLITE_PRIVATE void sqlite3ValueSetStr(
61317   sqlite3_value *v,     /* Value to be set */
61318   int n,                /* Length of string z */
61319   const void *z,        /* Text of the new string */
61320   u8 enc,               /* Encoding to use */
61321   void (*xDel)(void*)   /* Destructor for the string */
61322 ){
61323   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
61324 }
61325 
61326 /*
61327 ** Free an sqlite3_value object
61328 */
61329 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
61330   if( !v ) return;
61331   sqlite3VdbeMemRelease((Mem *)v);
61332   sqlite3DbFree(((Mem*)v)->db, v);
61333 }
61334 
61335 /*
61336 ** Return the number of bytes in the sqlite3_value object assuming
61337 ** that it uses the encoding "enc"
61338 */
61339 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
61340   Mem *p = (Mem*)pVal;
61341   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
61342     if( p->flags & MEM_Zero ){
61343       return p->n + p->u.nZero;
61344     }else{
61345       return p->n;
61346     }
61347   }
61348   return 0;
61349 }
61350 
61351 /************** End of vdbemem.c *********************************************/
61352 /************** Begin file vdbeaux.c *****************************************/
61353 /*
61354 ** 2003 September 6
61355 **
61356 ** The author disclaims copyright to this source code.  In place of
61357 ** a legal notice, here is a blessing:
61358 **
61359 **    May you do good and not evil.
61360 **    May you find forgiveness for yourself and forgive others.
61361 **    May you share freely, never taking more than you give.
61362 **
61363 *************************************************************************
61364 ** This file contains code used for creating, destroying, and populating
61365 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
61366 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
61367 ** But that file was getting too big so this subroutines were split out.
61368 */
61369 
61370 /*
61371 ** Create a new virtual database engine.
61372 */
61373 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
61374   sqlite3 *db = pParse->db;
61375   Vdbe *p;
61376   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
61377   if( p==0 ) return 0;
61378   p->db = db;
61379   if( db->pVdbe ){
61380     db->pVdbe->pPrev = p;
61381   }
61382   p->pNext = db->pVdbe;
61383   p->pPrev = 0;
61384   db->pVdbe = p;
61385   p->magic = VDBE_MAGIC_INIT;
61386   p->pParse = pParse;
61387   assert( pParse->aLabel==0 );
61388   assert( pParse->nLabel==0 );
61389   assert( pParse->nOpAlloc==0 );
61390   return p;
61391 }
61392 
61393 /*
61394 ** Remember the SQL string for a prepared statement.
61395 */
61396 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
61397   assert( isPrepareV2==1 || isPrepareV2==0 );
61398   if( p==0 ) return;
61399 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
61400   if( !isPrepareV2 ) return;
61401 #endif
61402   assert( p->zSql==0 );
61403   p->zSql = sqlite3DbStrNDup(p->db, z, n);
61404   p->isPrepareV2 = (u8)isPrepareV2;
61405 }
61406 
61407 /*
61408 ** Return the SQL associated with a prepared statement
61409 */
61410 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
61411   Vdbe *p = (Vdbe *)pStmt;
61412   return (p && p->isPrepareV2) ? p->zSql : 0;
61413 }
61414 
61415 /*
61416 ** Swap all content between two VDBE structures.
61417 */
61418 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
61419   Vdbe tmp, *pTmp;
61420   char *zTmp;
61421   tmp = *pA;
61422   *pA = *pB;
61423   *pB = tmp;
61424   pTmp = pA->pNext;
61425   pA->pNext = pB->pNext;
61426   pB->pNext = pTmp;
61427   pTmp = pA->pPrev;
61428   pA->pPrev = pB->pPrev;
61429   pB->pPrev = pTmp;
61430   zTmp = pA->zSql;
61431   pA->zSql = pB->zSql;
61432   pB->zSql = zTmp;
61433   pB->isPrepareV2 = pA->isPrepareV2;
61434 }
61435 
61436 /*
61437 ** Resize the Vdbe.aOp array so that it is at least one op larger than
61438 ** it was.
61439 **
61440 ** If an out-of-memory error occurs while resizing the array, return
61441 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
61442 ** unchanged (this is so that any opcodes already allocated can be
61443 ** correctly deallocated along with the rest of the Vdbe).
61444 */
61445 static int growOpArray(Vdbe *v){
61446   VdbeOp *pNew;
61447   Parse *p = v->pParse;
61448   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
61449   pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
61450   if( pNew ){
61451     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
61452     v->aOp = pNew;
61453   }
61454   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
61455 }
61456 
61457 #ifdef SQLITE_DEBUG
61458 /* This routine is just a convenient place to set a breakpoint that will
61459 ** fire after each opcode is inserted and displayed using
61460 ** "PRAGMA vdbe_addoptrace=on".
61461 */
61462 static void test_addop_breakpoint(void){
61463   static int n = 0;
61464   n++;
61465 }
61466 #endif
61467 
61468 /*
61469 ** Add a new instruction to the list of instructions current in the
61470 ** VDBE.  Return the address of the new instruction.
61471 **
61472 ** Parameters:
61473 **
61474 **    p               Pointer to the VDBE
61475 **
61476 **    op              The opcode for this instruction
61477 **
61478 **    p1, p2, p3      Operands
61479 **
61480 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
61481 ** the sqlite3VdbeChangeP4() function to change the value of the P4
61482 ** operand.
61483 */
61484 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
61485   int i;
61486   VdbeOp *pOp;
61487 
61488   i = p->nOp;
61489   assert( p->magic==VDBE_MAGIC_INIT );
61490   assert( op>0 && op<0xff );
61491   if( p->pParse->nOpAlloc<=i ){
61492     if( growOpArray(p) ){
61493       return 1;
61494     }
61495   }
61496   p->nOp++;
61497   pOp = &p->aOp[i];
61498   pOp->opcode = (u8)op;
61499   pOp->p5 = 0;
61500   pOp->p1 = p1;
61501   pOp->p2 = p2;
61502   pOp->p3 = p3;
61503   pOp->p4.p = 0;
61504   pOp->p4type = P4_NOTUSED;
61505 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
61506   pOp->zComment = 0;
61507 #endif
61508 #ifdef SQLITE_DEBUG
61509   if( p->db->flags & SQLITE_VdbeAddopTrace ){
61510     int jj, kk;
61511     Parse *pParse = p->pParse;
61512     for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
61513       struct yColCache *x = pParse->aColCache + jj;
61514       if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
61515       printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
61516       kk++;
61517     }
61518     if( kk ) printf("\n");
61519     sqlite3VdbePrintOp(0, i, &p->aOp[i]);
61520     test_addop_breakpoint();
61521   }
61522 #endif
61523 #ifdef VDBE_PROFILE
61524   pOp->cycles = 0;
61525   pOp->cnt = 0;
61526 #endif
61527 #ifdef SQLITE_VDBE_COVERAGE
61528   pOp->iSrcLine = 0;
61529 #endif
61530   return i;
61531 }
61532 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
61533   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
61534 }
61535 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
61536   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
61537 }
61538 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
61539   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
61540 }
61541 
61542 
61543 /*
61544 ** Add an opcode that includes the p4 value as a pointer.
61545 */
61546 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
61547   Vdbe *p,            /* Add the opcode to this VM */
61548   int op,             /* The new opcode */
61549   int p1,             /* The P1 operand */
61550   int p2,             /* The P2 operand */
61551   int p3,             /* The P3 operand */
61552   const char *zP4,    /* The P4 operand */
61553   int p4type          /* P4 operand type */
61554 ){
61555   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
61556   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
61557   return addr;
61558 }
61559 
61560 /*
61561 ** Add an OP_ParseSchema opcode.  This routine is broken out from
61562 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
61563 ** as having been used.
61564 **
61565 ** The zWhere string must have been obtained from sqlite3_malloc().
61566 ** This routine will take ownership of the allocated memory.
61567 */
61568 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
61569   int j;
61570   int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
61571   sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
61572   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
61573 }
61574 
61575 /*
61576 ** Add an opcode that includes the p4 value as an integer.
61577 */
61578 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
61579   Vdbe *p,            /* Add the opcode to this VM */
61580   int op,             /* The new opcode */
61581   int p1,             /* The P1 operand */
61582   int p2,             /* The P2 operand */
61583   int p3,             /* The P3 operand */
61584   int p4              /* The P4 operand as an integer */
61585 ){
61586   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
61587   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
61588   return addr;
61589 }
61590 
61591 /*
61592 ** Create a new symbolic label for an instruction that has yet to be
61593 ** coded.  The symbolic label is really just a negative number.  The
61594 ** label can be used as the P2 value of an operation.  Later, when
61595 ** the label is resolved to a specific address, the VDBE will scan
61596 ** through its operation list and change all values of P2 which match
61597 ** the label into the resolved address.
61598 **
61599 ** The VDBE knows that a P2 value is a label because labels are
61600 ** always negative and P2 values are suppose to be non-negative.
61601 ** Hence, a negative P2 value is a label that has yet to be resolved.
61602 **
61603 ** Zero is returned if a malloc() fails.
61604 */
61605 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
61606   Parse *p = v->pParse;
61607   int i = p->nLabel++;
61608   assert( v->magic==VDBE_MAGIC_INIT );
61609   if( (i & (i-1))==0 ){
61610     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
61611                                        (i*2+1)*sizeof(p->aLabel[0]));
61612   }
61613   if( p->aLabel ){
61614     p->aLabel[i] = -1;
61615   }
61616   return -1-i;
61617 }
61618 
61619 /*
61620 ** Resolve label "x" to be the address of the next instruction to
61621 ** be inserted.  The parameter "x" must have been obtained from
61622 ** a prior call to sqlite3VdbeMakeLabel().
61623 */
61624 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
61625   Parse *p = v->pParse;
61626   int j = -1-x;
61627   assert( v->magic==VDBE_MAGIC_INIT );
61628   assert( j<p->nLabel );
61629   if( j>=0 && p->aLabel ){
61630     p->aLabel[j] = v->nOp;
61631   }
61632   p->iFixedOp = v->nOp - 1;
61633 }
61634 
61635 /*
61636 ** Mark the VDBE as one that can only be run one time.
61637 */
61638 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
61639   p->runOnlyOnce = 1;
61640 }
61641 
61642 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
61643 
61644 /*
61645 ** The following type and function are used to iterate through all opcodes
61646 ** in a Vdbe main program and each of the sub-programs (triggers) it may
61647 ** invoke directly or indirectly. It should be used as follows:
61648 **
61649 **   Op *pOp;
61650 **   VdbeOpIter sIter;
61651 **
61652 **   memset(&sIter, 0, sizeof(sIter));
61653 **   sIter.v = v;                            // v is of type Vdbe*
61654 **   while( (pOp = opIterNext(&sIter)) ){
61655 **     // Do something with pOp
61656 **   }
61657 **   sqlite3DbFree(v->db, sIter.apSub);
61658 **
61659 */
61660 typedef struct VdbeOpIter VdbeOpIter;
61661 struct VdbeOpIter {
61662   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
61663   SubProgram **apSub;        /* Array of subprograms */
61664   int nSub;                  /* Number of entries in apSub */
61665   int iAddr;                 /* Address of next instruction to return */
61666   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
61667 };
61668 static Op *opIterNext(VdbeOpIter *p){
61669   Vdbe *v = p->v;
61670   Op *pRet = 0;
61671   Op *aOp;
61672   int nOp;
61673 
61674   if( p->iSub<=p->nSub ){
61675 
61676     if( p->iSub==0 ){
61677       aOp = v->aOp;
61678       nOp = v->nOp;
61679     }else{
61680       aOp = p->apSub[p->iSub-1]->aOp;
61681       nOp = p->apSub[p->iSub-1]->nOp;
61682     }
61683     assert( p->iAddr<nOp );
61684 
61685     pRet = &aOp[p->iAddr];
61686     p->iAddr++;
61687     if( p->iAddr==nOp ){
61688       p->iSub++;
61689       p->iAddr = 0;
61690     }
61691 
61692     if( pRet->p4type==P4_SUBPROGRAM ){
61693       int nByte = (p->nSub+1)*sizeof(SubProgram*);
61694       int j;
61695       for(j=0; j<p->nSub; j++){
61696         if( p->apSub[j]==pRet->p4.pProgram ) break;
61697       }
61698       if( j==p->nSub ){
61699         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
61700         if( !p->apSub ){
61701           pRet = 0;
61702         }else{
61703           p->apSub[p->nSub++] = pRet->p4.pProgram;
61704         }
61705       }
61706     }
61707   }
61708 
61709   return pRet;
61710 }
61711 
61712 /*
61713 ** Check if the program stored in the VM associated with pParse may
61714 ** throw an ABORT exception (causing the statement, but not entire transaction
61715 ** to be rolled back). This condition is true if the main program or any
61716 ** sub-programs contains any of the following:
61717 **
61718 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
61719 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
61720 **   *  OP_Destroy
61721 **   *  OP_VUpdate
61722 **   *  OP_VRename
61723 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
61724 **
61725 ** Then check that the value of Parse.mayAbort is true if an
61726 ** ABORT may be thrown, or false otherwise. Return true if it does
61727 ** match, or false otherwise. This function is intended to be used as
61728 ** part of an assert statement in the compiler. Similar to:
61729 **
61730 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
61731 */
61732 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
61733   int hasAbort = 0;
61734   Op *pOp;
61735   VdbeOpIter sIter;
61736   memset(&sIter, 0, sizeof(sIter));
61737   sIter.v = v;
61738 
61739   while( (pOp = opIterNext(&sIter))!=0 ){
61740     int opcode = pOp->opcode;
61741     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
61742 #ifndef SQLITE_OMIT_FOREIGN_KEY
61743      || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
61744 #endif
61745      || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
61746       && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
61747     ){
61748       hasAbort = 1;
61749       break;
61750     }
61751   }
61752   sqlite3DbFree(v->db, sIter.apSub);
61753 
61754   /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
61755   ** If malloc failed, then the while() loop above may not have iterated
61756   ** through all opcodes and hasAbort may be set incorrectly. Return
61757   ** true for this case to prevent the assert() in the callers frame
61758   ** from failing.  */
61759   return ( v->db->mallocFailed || hasAbort==mayAbort );
61760 }
61761 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
61762 
61763 /*
61764 ** Loop through the program looking for P2 values that are negative
61765 ** on jump instructions.  Each such value is a label.  Resolve the
61766 ** label by setting the P2 value to its correct non-zero value.
61767 **
61768 ** This routine is called once after all opcodes have been inserted.
61769 **
61770 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
61771 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
61772 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
61773 **
61774 ** The Op.opflags field is set on all opcodes.
61775 */
61776 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
61777   int i;
61778   int nMaxArgs = *pMaxFuncArgs;
61779   Op *pOp;
61780   Parse *pParse = p->pParse;
61781   int *aLabel = pParse->aLabel;
61782   p->readOnly = 1;
61783   p->bIsReader = 0;
61784   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
61785     u8 opcode = pOp->opcode;
61786 
61787     /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
61788     ** cases from this switch! */
61789     switch( opcode ){
61790       case OP_Function:
61791       case OP_AggStep: {
61792         if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
61793         break;
61794       }
61795       case OP_Transaction: {
61796         if( pOp->p2!=0 ) p->readOnly = 0;
61797         /* fall thru */
61798       }
61799       case OP_AutoCommit:
61800       case OP_Savepoint: {
61801         p->bIsReader = 1;
61802         break;
61803       }
61804 #ifndef SQLITE_OMIT_WAL
61805       case OP_Checkpoint:
61806 #endif
61807       case OP_Vacuum:
61808       case OP_JournalMode: {
61809         p->readOnly = 0;
61810         p->bIsReader = 1;
61811         break;
61812       }
61813 #ifndef SQLITE_OMIT_VIRTUALTABLE
61814       case OP_VUpdate: {
61815         if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
61816         break;
61817       }
61818       case OP_VFilter: {
61819         int n;
61820         assert( p->nOp - i >= 3 );
61821         assert( pOp[-1].opcode==OP_Integer );
61822         n = pOp[-1].p1;
61823         if( n>nMaxArgs ) nMaxArgs = n;
61824         break;
61825       }
61826 #endif
61827       case OP_Next:
61828       case OP_NextIfOpen:
61829       case OP_SorterNext: {
61830         pOp->p4.xAdvance = sqlite3BtreeNext;
61831         pOp->p4type = P4_ADVANCE;
61832         break;
61833       }
61834       case OP_Prev:
61835       case OP_PrevIfOpen: {
61836         pOp->p4.xAdvance = sqlite3BtreePrevious;
61837         pOp->p4type = P4_ADVANCE;
61838         break;
61839       }
61840     }
61841 
61842     pOp->opflags = sqlite3OpcodeProperty[opcode];
61843     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
61844       assert( -1-pOp->p2<pParse->nLabel );
61845       pOp->p2 = aLabel[-1-pOp->p2];
61846     }
61847   }
61848   sqlite3DbFree(p->db, pParse->aLabel);
61849   pParse->aLabel = 0;
61850   pParse->nLabel = 0;
61851   *pMaxFuncArgs = nMaxArgs;
61852   assert( p->bIsReader!=0 || p->btreeMask==0 );
61853 }
61854 
61855 /*
61856 ** Return the address of the next instruction to be inserted.
61857 */
61858 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
61859   assert( p->magic==VDBE_MAGIC_INIT );
61860   return p->nOp;
61861 }
61862 
61863 /*
61864 ** This function returns a pointer to the array of opcodes associated with
61865 ** the Vdbe passed as the first argument. It is the callers responsibility
61866 ** to arrange for the returned array to be eventually freed using the
61867 ** vdbeFreeOpArray() function.
61868 **
61869 ** Before returning, *pnOp is set to the number of entries in the returned
61870 ** array. Also, *pnMaxArg is set to the larger of its current value and
61871 ** the number of entries in the Vdbe.apArg[] array required to execute the
61872 ** returned program.
61873 */
61874 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
61875   VdbeOp *aOp = p->aOp;
61876   assert( aOp && !p->db->mallocFailed );
61877 
61878   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
61879   assert( p->btreeMask==0 );
61880 
61881   resolveP2Values(p, pnMaxArg);
61882   *pnOp = p->nOp;
61883   p->aOp = 0;
61884   return aOp;
61885 }
61886 
61887 /*
61888 ** Add a whole list of operations to the operation stack.  Return the
61889 ** address of the first operation added.
61890 */
61891 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp, int iLineno){
61892   int addr;
61893   assert( p->magic==VDBE_MAGIC_INIT );
61894   if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
61895     return 0;
61896   }
61897   addr = p->nOp;
61898   if( ALWAYS(nOp>0) ){
61899     int i;
61900     VdbeOpList const *pIn = aOp;
61901     for(i=0; i<nOp; i++, pIn++){
61902       int p2 = pIn->p2;
61903       VdbeOp *pOut = &p->aOp[i+addr];
61904       pOut->opcode = pIn->opcode;
61905       pOut->p1 = pIn->p1;
61906       if( p2<0 ){
61907         assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
61908         pOut->p2 = addr + ADDR(p2);
61909       }else{
61910         pOut->p2 = p2;
61911       }
61912       pOut->p3 = pIn->p3;
61913       pOut->p4type = P4_NOTUSED;
61914       pOut->p4.p = 0;
61915       pOut->p5 = 0;
61916 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
61917       pOut->zComment = 0;
61918 #endif
61919 #ifdef SQLITE_VDBE_COVERAGE
61920       pOut->iSrcLine = iLineno+i;
61921 #else
61922       (void)iLineno;
61923 #endif
61924 #ifdef SQLITE_DEBUG
61925       if( p->db->flags & SQLITE_VdbeAddopTrace ){
61926         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
61927       }
61928 #endif
61929     }
61930     p->nOp += nOp;
61931   }
61932   return addr;
61933 }
61934 
61935 /*
61936 ** Change the value of the P1 operand for a specific instruction.
61937 ** This routine is useful when a large program is loaded from a
61938 ** static array using sqlite3VdbeAddOpList but we want to make a
61939 ** few minor changes to the program.
61940 */
61941 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
61942   assert( p!=0 );
61943   if( ((u32)p->nOp)>addr ){
61944     p->aOp[addr].p1 = val;
61945   }
61946 }
61947 
61948 /*
61949 ** Change the value of the P2 operand for a specific instruction.
61950 ** This routine is useful for setting a jump destination.
61951 */
61952 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
61953   assert( p!=0 );
61954   if( ((u32)p->nOp)>addr ){
61955     p->aOp[addr].p2 = val;
61956   }
61957 }
61958 
61959 /*
61960 ** Change the value of the P3 operand for a specific instruction.
61961 */
61962 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
61963   assert( p!=0 );
61964   if( ((u32)p->nOp)>addr ){
61965     p->aOp[addr].p3 = val;
61966   }
61967 }
61968 
61969 /*
61970 ** Change the value of the P5 operand for the most recently
61971 ** added operation.
61972 */
61973 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
61974   assert( p!=0 );
61975   if( p->aOp ){
61976     assert( p->nOp>0 );
61977     p->aOp[p->nOp-1].p5 = val;
61978   }
61979 }
61980 
61981 /*
61982 ** Change the P2 operand of instruction addr so that it points to
61983 ** the address of the next instruction to be coded.
61984 */
61985 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
61986   sqlite3VdbeChangeP2(p, addr, p->nOp);
61987   p->pParse->iFixedOp = p->nOp - 1;
61988 }
61989 
61990 
61991 /*
61992 ** If the input FuncDef structure is ephemeral, then free it.  If
61993 ** the FuncDef is not ephermal, then do nothing.
61994 */
61995 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
61996   if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
61997     sqlite3DbFree(db, pDef);
61998   }
61999 }
62000 
62001 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
62002 
62003 /*
62004 ** Delete a P4 value if necessary.
62005 */
62006 static void freeP4(sqlite3 *db, int p4type, void *p4){
62007   if( p4 ){
62008     assert( db );
62009     switch( p4type ){
62010       case P4_REAL:
62011       case P4_INT64:
62012       case P4_DYNAMIC:
62013       case P4_INTARRAY: {
62014         sqlite3DbFree(db, p4);
62015         break;
62016       }
62017       case P4_KEYINFO: {
62018         if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
62019         break;
62020       }
62021       case P4_MPRINTF: {
62022         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
62023         break;
62024       }
62025       case P4_FUNCDEF: {
62026         freeEphemeralFunction(db, (FuncDef*)p4);
62027         break;
62028       }
62029       case P4_MEM: {
62030         if( db->pnBytesFreed==0 ){
62031           sqlite3ValueFree((sqlite3_value*)p4);
62032         }else{
62033           Mem *p = (Mem*)p4;
62034           sqlite3DbFree(db, p->zMalloc);
62035           sqlite3DbFree(db, p);
62036         }
62037         break;
62038       }
62039       case P4_VTAB : {
62040         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
62041         break;
62042       }
62043     }
62044   }
62045 }
62046 
62047 /*
62048 ** Free the space allocated for aOp and any p4 values allocated for the
62049 ** opcodes contained within. If aOp is not NULL it is assumed to contain
62050 ** nOp entries.
62051 */
62052 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
62053   if( aOp ){
62054     Op *pOp;
62055     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
62056       freeP4(db, pOp->p4type, pOp->p4.p);
62057 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62058       sqlite3DbFree(db, pOp->zComment);
62059 #endif
62060     }
62061   }
62062   sqlite3DbFree(db, aOp);
62063 }
62064 
62065 /*
62066 ** Link the SubProgram object passed as the second argument into the linked
62067 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
62068 ** objects when the VM is no longer required.
62069 */
62070 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
62071   p->pNext = pVdbe->pProgram;
62072   pVdbe->pProgram = p;
62073 }
62074 
62075 /*
62076 ** Change the opcode at addr into OP_Noop
62077 */
62078 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
62079   if( p->aOp ){
62080     VdbeOp *pOp = &p->aOp[addr];
62081     sqlite3 *db = p->db;
62082     freeP4(db, pOp->p4type, pOp->p4.p);
62083     memset(pOp, 0, sizeof(pOp[0]));
62084     pOp->opcode = OP_Noop;
62085     if( addr==p->nOp-1 ) p->nOp--;
62086   }
62087 }
62088 
62089 /*
62090 ** Remove the last opcode inserted
62091 */
62092 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
62093   if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
62094     sqlite3VdbeChangeToNoop(p, p->nOp-1);
62095     return 1;
62096   }else{
62097     return 0;
62098   }
62099 }
62100 
62101 /*
62102 ** Change the value of the P4 operand for a specific instruction.
62103 ** This routine is useful when a large program is loaded from a
62104 ** static array using sqlite3VdbeAddOpList but we want to make a
62105 ** few minor changes to the program.
62106 **
62107 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
62108 ** the string is made into memory obtained from sqlite3_malloc().
62109 ** A value of n==0 means copy bytes of zP4 up to and including the
62110 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
62111 **
62112 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
62113 ** to a string or structure that is guaranteed to exist for the lifetime of
62114 ** the Vdbe. In these cases we can just copy the pointer.
62115 **
62116 ** If addr<0 then change P4 on the most recently inserted instruction.
62117 */
62118 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
62119   Op *pOp;
62120   sqlite3 *db;
62121   assert( p!=0 );
62122   db = p->db;
62123   assert( p->magic==VDBE_MAGIC_INIT );
62124   if( p->aOp==0 || db->mallocFailed ){
62125     if( n!=P4_VTAB ){
62126       freeP4(db, n, (void*)*(char**)&zP4);
62127     }
62128     return;
62129   }
62130   assert( p->nOp>0 );
62131   assert( addr<p->nOp );
62132   if( addr<0 ){
62133     addr = p->nOp - 1;
62134   }
62135   pOp = &p->aOp[addr];
62136   assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
62137   freeP4(db, pOp->p4type, pOp->p4.p);
62138   pOp->p4.p = 0;
62139   if( n==P4_INT32 ){
62140     /* Note: this cast is safe, because the origin data point was an int
62141     ** that was cast to a (const char *). */
62142     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
62143     pOp->p4type = P4_INT32;
62144   }else if( zP4==0 ){
62145     pOp->p4.p = 0;
62146     pOp->p4type = P4_NOTUSED;
62147   }else if( n==P4_KEYINFO ){
62148     pOp->p4.p = (void*)zP4;
62149     pOp->p4type = P4_KEYINFO;
62150   }else if( n==P4_VTAB ){
62151     pOp->p4.p = (void*)zP4;
62152     pOp->p4type = P4_VTAB;
62153     sqlite3VtabLock((VTable *)zP4);
62154     assert( ((VTable *)zP4)->db==p->db );
62155   }else if( n<0 ){
62156     pOp->p4.p = (void*)zP4;
62157     pOp->p4type = (signed char)n;
62158   }else{
62159     if( n==0 ) n = sqlite3Strlen30(zP4);
62160     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
62161     pOp->p4type = P4_DYNAMIC;
62162   }
62163 }
62164 
62165 /*
62166 ** Set the P4 on the most recently added opcode to the KeyInfo for the
62167 ** index given.
62168 */
62169 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
62170   Vdbe *v = pParse->pVdbe;
62171   assert( v!=0 );
62172   assert( pIdx!=0 );
62173   sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
62174                       P4_KEYINFO);
62175 }
62176 
62177 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62178 /*
62179 ** Change the comment on the most recently coded instruction.  Or
62180 ** insert a No-op and add the comment to that new instruction.  This
62181 ** makes the code easier to read during debugging.  None of this happens
62182 ** in a production build.
62183 */
62184 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
62185   assert( p->nOp>0 || p->aOp==0 );
62186   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
62187   if( p->nOp ){
62188     assert( p->aOp );
62189     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
62190     p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
62191   }
62192 }
62193 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
62194   va_list ap;
62195   if( p ){
62196     va_start(ap, zFormat);
62197     vdbeVComment(p, zFormat, ap);
62198     va_end(ap);
62199   }
62200 }
62201 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
62202   va_list ap;
62203   if( p ){
62204     sqlite3VdbeAddOp0(p, OP_Noop);
62205     va_start(ap, zFormat);
62206     vdbeVComment(p, zFormat, ap);
62207     va_end(ap);
62208   }
62209 }
62210 #endif  /* NDEBUG */
62211 
62212 #ifdef SQLITE_VDBE_COVERAGE
62213 /*
62214 ** Set the value if the iSrcLine field for the previously coded instruction.
62215 */
62216 SQLITE_PRIVATE void sqlite3VdbeSetLineNumber(Vdbe *v, int iLine){
62217   sqlite3VdbeGetOp(v,-1)->iSrcLine = iLine;
62218 }
62219 #endif /* SQLITE_VDBE_COVERAGE */
62220 
62221 /*
62222 ** Return the opcode for a given address.  If the address is -1, then
62223 ** return the most recently inserted opcode.
62224 **
62225 ** If a memory allocation error has occurred prior to the calling of this
62226 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
62227 ** is readable but not writable, though it is cast to a writable value.
62228 ** The return of a dummy opcode allows the call to continue functioning
62229 ** after a OOM fault without having to check to see if the return from
62230 ** this routine is a valid pointer.  But because the dummy.opcode is 0,
62231 ** dummy will never be written to.  This is verified by code inspection and
62232 ** by running with Valgrind.
62233 */
62234 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
62235   /* C89 specifies that the constant "dummy" will be initialized to all
62236   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
62237   static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
62238   assert( p->magic==VDBE_MAGIC_INIT );
62239   if( addr<0 ){
62240     addr = p->nOp - 1;
62241   }
62242   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
62243   if( p->db->mallocFailed ){
62244     return (VdbeOp*)&dummy;
62245   }else{
62246     return &p->aOp[addr];
62247   }
62248 }
62249 
62250 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
62251 /*
62252 ** Return an integer value for one of the parameters to the opcode pOp
62253 ** determined by character c.
62254 */
62255 static int translateP(char c, const Op *pOp){
62256   if( c=='1' ) return pOp->p1;
62257   if( c=='2' ) return pOp->p2;
62258   if( c=='3' ) return pOp->p3;
62259   if( c=='4' ) return pOp->p4.i;
62260   return pOp->p5;
62261 }
62262 
62263 /*
62264 ** Compute a string for the "comment" field of a VDBE opcode listing.
62265 **
62266 ** The Synopsis: field in comments in the vdbe.c source file gets converted
62267 ** to an extra string that is appended to the sqlite3OpcodeName().  In the
62268 ** absence of other comments, this synopsis becomes the comment on the opcode.
62269 ** Some translation occurs:
62270 **
62271 **       "PX"      ->  "r[X]"
62272 **       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
62273 **       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
62274 **       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
62275 */
62276 static int displayComment(
62277   const Op *pOp,     /* The opcode to be commented */
62278   const char *zP4,   /* Previously obtained value for P4 */
62279   char *zTemp,       /* Write result here */
62280   int nTemp          /* Space available in zTemp[] */
62281 ){
62282   const char *zOpName;
62283   const char *zSynopsis;
62284   int nOpName;
62285   int ii, jj;
62286   zOpName = sqlite3OpcodeName(pOp->opcode);
62287   nOpName = sqlite3Strlen30(zOpName);
62288   if( zOpName[nOpName+1] ){
62289     int seenCom = 0;
62290     char c;
62291     zSynopsis = zOpName += nOpName + 1;
62292     for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
62293       if( c=='P' ){
62294         c = zSynopsis[++ii];
62295         if( c=='4' ){
62296           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
62297         }else if( c=='X' ){
62298           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
62299           seenCom = 1;
62300         }else{
62301           int v1 = translateP(c, pOp);
62302           int v2;
62303           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
62304           if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
62305             ii += 3;
62306             jj += sqlite3Strlen30(zTemp+jj);
62307             v2 = translateP(zSynopsis[ii], pOp);
62308             if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
62309               ii += 2;
62310               v2++;
62311             }
62312             if( v2>1 ){
62313               sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
62314             }
62315           }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
62316             ii += 4;
62317           }
62318         }
62319         jj += sqlite3Strlen30(zTemp+jj);
62320       }else{
62321         zTemp[jj++] = c;
62322       }
62323     }
62324     if( !seenCom && jj<nTemp-5 && pOp->zComment ){
62325       sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
62326       jj += sqlite3Strlen30(zTemp+jj);
62327     }
62328     if( jj<nTemp ) zTemp[jj] = 0;
62329   }else if( pOp->zComment ){
62330     sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
62331     jj = sqlite3Strlen30(zTemp);
62332   }else{
62333     zTemp[0] = 0;
62334     jj = 0;
62335   }
62336   return jj;
62337 }
62338 #endif /* SQLITE_DEBUG */
62339 
62340 
62341 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
62342      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
62343 /*
62344 ** Compute a string that describes the P4 parameter for an opcode.
62345 ** Use zTemp for any required temporary buffer space.
62346 */
62347 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
62348   char *zP4 = zTemp;
62349   assert( nTemp>=20 );
62350   switch( pOp->p4type ){
62351     case P4_KEYINFO: {
62352       int i, j;
62353       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
62354       assert( pKeyInfo->aSortOrder!=0 );
62355       sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
62356       i = sqlite3Strlen30(zTemp);
62357       for(j=0; j<pKeyInfo->nField; j++){
62358         CollSeq *pColl = pKeyInfo->aColl[j];
62359         const char *zColl = pColl ? pColl->zName : "nil";
62360         int n = sqlite3Strlen30(zColl);
62361         if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
62362           zColl = "B";
62363           n = 1;
62364         }
62365         if( i+n>nTemp-6 ){
62366           memcpy(&zTemp[i],",...",4);
62367           break;
62368         }
62369         zTemp[i++] = ',';
62370         if( pKeyInfo->aSortOrder[j] ){
62371           zTemp[i++] = '-';
62372         }
62373         memcpy(&zTemp[i], zColl, n+1);
62374         i += n;
62375       }
62376       zTemp[i++] = ')';
62377       zTemp[i] = 0;
62378       assert( i<nTemp );
62379       break;
62380     }
62381     case P4_COLLSEQ: {
62382       CollSeq *pColl = pOp->p4.pColl;
62383       sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
62384       break;
62385     }
62386     case P4_FUNCDEF: {
62387       FuncDef *pDef = pOp->p4.pFunc;
62388       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
62389       break;
62390     }
62391     case P4_INT64: {
62392       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
62393       break;
62394     }
62395     case P4_INT32: {
62396       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
62397       break;
62398     }
62399     case P4_REAL: {
62400       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
62401       break;
62402     }
62403     case P4_MEM: {
62404       Mem *pMem = pOp->p4.pMem;
62405       if( pMem->flags & MEM_Str ){
62406         zP4 = pMem->z;
62407       }else if( pMem->flags & MEM_Int ){
62408         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
62409       }else if( pMem->flags & MEM_Real ){
62410         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
62411       }else if( pMem->flags & MEM_Null ){
62412         sqlite3_snprintf(nTemp, zTemp, "NULL");
62413       }else{
62414         assert( pMem->flags & MEM_Blob );
62415         zP4 = "(blob)";
62416       }
62417       break;
62418     }
62419 #ifndef SQLITE_OMIT_VIRTUALTABLE
62420     case P4_VTAB: {
62421       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
62422       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
62423       break;
62424     }
62425 #endif
62426     case P4_INTARRAY: {
62427       sqlite3_snprintf(nTemp, zTemp, "intarray");
62428       break;
62429     }
62430     case P4_SUBPROGRAM: {
62431       sqlite3_snprintf(nTemp, zTemp, "program");
62432       break;
62433     }
62434     case P4_ADVANCE: {
62435       zTemp[0] = 0;
62436       break;
62437     }
62438     default: {
62439       zP4 = pOp->p4.z;
62440       if( zP4==0 ){
62441         zP4 = zTemp;
62442         zTemp[0] = 0;
62443       }
62444     }
62445   }
62446   assert( zP4!=0 );
62447   return zP4;
62448 }
62449 #endif
62450 
62451 /*
62452 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
62453 **
62454 ** The prepared statements need to know in advance the complete set of
62455 ** attached databases that will be use.  A mask of these databases
62456 ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
62457 ** p->btreeMask of databases that will require a lock.
62458 */
62459 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
62460   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
62461   assert( i<(int)sizeof(p->btreeMask)*8 );
62462   p->btreeMask |= ((yDbMask)1)<<i;
62463   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
62464     p->lockMask |= ((yDbMask)1)<<i;
62465   }
62466 }
62467 
62468 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
62469 /*
62470 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
62471 ** this routine obtains the mutex associated with each BtShared structure
62472 ** that may be accessed by the VM passed as an argument. In doing so it also
62473 ** sets the BtShared.db member of each of the BtShared structures, ensuring
62474 ** that the correct busy-handler callback is invoked if required.
62475 **
62476 ** If SQLite is not threadsafe but does support shared-cache mode, then
62477 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
62478 ** of all of BtShared structures accessible via the database handle
62479 ** associated with the VM.
62480 **
62481 ** If SQLite is not threadsafe and does not support shared-cache mode, this
62482 ** function is a no-op.
62483 **
62484 ** The p->btreeMask field is a bitmask of all btrees that the prepared
62485 ** statement p will ever use.  Let N be the number of bits in p->btreeMask
62486 ** corresponding to btrees that use shared cache.  Then the runtime of
62487 ** this routine is N*N.  But as N is rarely more than 1, this should not
62488 ** be a problem.
62489 */
62490 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
62491   int i;
62492   yDbMask mask;
62493   sqlite3 *db;
62494   Db *aDb;
62495   int nDb;
62496   if( p->lockMask==0 ) return;  /* The common case */
62497   db = p->db;
62498   aDb = db->aDb;
62499   nDb = db->nDb;
62500   for(i=0, mask=1; i<nDb; i++, mask += mask){
62501     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
62502       sqlite3BtreeEnter(aDb[i].pBt);
62503     }
62504   }
62505 }
62506 #endif
62507 
62508 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
62509 /*
62510 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
62511 */
62512 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
62513   int i;
62514   yDbMask mask;
62515   sqlite3 *db;
62516   Db *aDb;
62517   int nDb;
62518   if( p->lockMask==0 ) return;  /* The common case */
62519   db = p->db;
62520   aDb = db->aDb;
62521   nDb = db->nDb;
62522   for(i=0, mask=1; i<nDb; i++, mask += mask){
62523     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
62524       sqlite3BtreeLeave(aDb[i].pBt);
62525     }
62526   }
62527 }
62528 #endif
62529 
62530 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
62531 /*
62532 ** Print a single opcode.  This routine is used for debugging only.
62533 */
62534 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
62535   char *zP4;
62536   char zPtr[50];
62537   char zCom[100];
62538   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
62539   if( pOut==0 ) pOut = stdout;
62540   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
62541 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62542   displayComment(pOp, zP4, zCom, sizeof(zCom));
62543 #else
62544   zCom[0] = 0;
62545 #endif
62546   /* NB:  The sqlite3OpcodeName() function is implemented by code created
62547   ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
62548   ** information from the vdbe.c source text */
62549   fprintf(pOut, zFormat1, pc,
62550       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
62551       zCom
62552   );
62553   fflush(pOut);
62554 }
62555 #endif
62556 
62557 /*
62558 ** Release an array of N Mem elements
62559 */
62560 static void releaseMemArray(Mem *p, int N){
62561   if( p && N ){
62562     Mem *pEnd;
62563     sqlite3 *db = p->db;
62564     u8 malloc_failed = db->mallocFailed;
62565     if( db->pnBytesFreed ){
62566       for(pEnd=&p[N]; p<pEnd; p++){
62567         sqlite3DbFree(db, p->zMalloc);
62568       }
62569       return;
62570     }
62571     for(pEnd=&p[N]; p<pEnd; p++){
62572       assert( (&p[1])==pEnd || p[0].db==p[1].db );
62573       assert( sqlite3VdbeCheckMemInvariants(p) );
62574 
62575       /* This block is really an inlined version of sqlite3VdbeMemRelease()
62576       ** that takes advantage of the fact that the memory cell value is
62577       ** being set to NULL after releasing any dynamic resources.
62578       **
62579       ** The justification for duplicating code is that according to
62580       ** callgrind, this causes a certain test case to hit the CPU 4.7
62581       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
62582       ** sqlite3MemRelease() were called from here. With -O2, this jumps
62583       ** to 6.6 percent. The test case is inserting 1000 rows into a table
62584       ** with no indexes using a single prepared INSERT statement, bind()
62585       ** and reset(). Inserts are grouped into a transaction.
62586       */
62587       testcase( p->flags & MEM_Agg );
62588       testcase( p->flags & MEM_Dyn );
62589       testcase( p->flags & MEM_Frame );
62590       testcase( p->flags & MEM_RowSet );
62591       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
62592         sqlite3VdbeMemRelease(p);
62593       }else if( p->zMalloc ){
62594         sqlite3DbFree(db, p->zMalloc);
62595         p->zMalloc = 0;
62596       }
62597 
62598       p->flags = MEM_Undefined;
62599     }
62600     db->mallocFailed = malloc_failed;
62601   }
62602 }
62603 
62604 /*
62605 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
62606 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
62607 */
62608 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
62609   int i;
62610   Mem *aMem = VdbeFrameMem(p);
62611   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
62612   for(i=0; i<p->nChildCsr; i++){
62613     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
62614   }
62615   releaseMemArray(aMem, p->nChildMem);
62616   sqlite3DbFree(p->v->db, p);
62617 }
62618 
62619 #ifndef SQLITE_OMIT_EXPLAIN
62620 /*
62621 ** Give a listing of the program in the virtual machine.
62622 **
62623 ** The interface is the same as sqlite3VdbeExec().  But instead of
62624 ** running the code, it invokes the callback once for each instruction.
62625 ** This feature is used to implement "EXPLAIN".
62626 **
62627 ** When p->explain==1, each instruction is listed.  When
62628 ** p->explain==2, only OP_Explain instructions are listed and these
62629 ** are shown in a different format.  p->explain==2 is used to implement
62630 ** EXPLAIN QUERY PLAN.
62631 **
62632 ** When p->explain==1, first the main program is listed, then each of
62633 ** the trigger subprograms are listed one by one.
62634 */
62635 SQLITE_PRIVATE int sqlite3VdbeList(
62636   Vdbe *p                   /* The VDBE */
62637 ){
62638   int nRow;                            /* Stop when row count reaches this */
62639   int nSub = 0;                        /* Number of sub-vdbes seen so far */
62640   SubProgram **apSub = 0;              /* Array of sub-vdbes */
62641   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
62642   sqlite3 *db = p->db;                 /* The database connection */
62643   int i;                               /* Loop counter */
62644   int rc = SQLITE_OK;                  /* Return code */
62645   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
62646 
62647   assert( p->explain );
62648   assert( p->magic==VDBE_MAGIC_RUN );
62649   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
62650 
62651   /* Even though this opcode does not use dynamic strings for
62652   ** the result, result columns may become dynamic if the user calls
62653   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
62654   */
62655   releaseMemArray(pMem, 8);
62656   p->pResultSet = 0;
62657 
62658   if( p->rc==SQLITE_NOMEM ){
62659     /* This happens if a malloc() inside a call to sqlite3_column_text() or
62660     ** sqlite3_column_text16() failed.  */
62661     db->mallocFailed = 1;
62662     return SQLITE_ERROR;
62663   }
62664 
62665   /* When the number of output rows reaches nRow, that means the
62666   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
62667   ** nRow is the sum of the number of rows in the main program, plus
62668   ** the sum of the number of rows in all trigger subprograms encountered
62669   ** so far.  The nRow value will increase as new trigger subprograms are
62670   ** encountered, but p->pc will eventually catch up to nRow.
62671   */
62672   nRow = p->nOp;
62673   if( p->explain==1 ){
62674     /* The first 8 memory cells are used for the result set.  So we will
62675     ** commandeer the 9th cell to use as storage for an array of pointers
62676     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
62677     ** cells.  */
62678     assert( p->nMem>9 );
62679     pSub = &p->aMem[9];
62680     if( pSub->flags&MEM_Blob ){
62681       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
62682       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
62683       nSub = pSub->n/sizeof(Vdbe*);
62684       apSub = (SubProgram **)pSub->z;
62685     }
62686     for(i=0; i<nSub; i++){
62687       nRow += apSub[i]->nOp;
62688     }
62689   }
62690 
62691   do{
62692     i = p->pc++;
62693   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
62694   if( i>=nRow ){
62695     p->rc = SQLITE_OK;
62696     rc = SQLITE_DONE;
62697   }else if( db->u1.isInterrupted ){
62698     p->rc = SQLITE_INTERRUPT;
62699     rc = SQLITE_ERROR;
62700     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
62701   }else{
62702     char *zP4;
62703     Op *pOp;
62704     if( i<p->nOp ){
62705       /* The output line number is small enough that we are still in the
62706       ** main program. */
62707       pOp = &p->aOp[i];
62708     }else{
62709       /* We are currently listing subprograms.  Figure out which one and
62710       ** pick up the appropriate opcode. */
62711       int j;
62712       i -= p->nOp;
62713       for(j=0; i>=apSub[j]->nOp; j++){
62714         i -= apSub[j]->nOp;
62715       }
62716       pOp = &apSub[j]->aOp[i];
62717     }
62718     if( p->explain==1 ){
62719       pMem->flags = MEM_Int;
62720       pMem->u.i = i;                                /* Program counter */
62721       pMem++;
62722 
62723       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
62724       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
62725       assert( pMem->z!=0 );
62726       pMem->n = sqlite3Strlen30(pMem->z);
62727       pMem->enc = SQLITE_UTF8;
62728       pMem++;
62729 
62730       /* When an OP_Program opcode is encounter (the only opcode that has
62731       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
62732       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
62733       ** has not already been seen.
62734       */
62735       if( pOp->p4type==P4_SUBPROGRAM ){
62736         int nByte = (nSub+1)*sizeof(SubProgram*);
62737         int j;
62738         for(j=0; j<nSub; j++){
62739           if( apSub[j]==pOp->p4.pProgram ) break;
62740         }
62741         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
62742           apSub = (SubProgram **)pSub->z;
62743           apSub[nSub++] = pOp->p4.pProgram;
62744           pSub->flags |= MEM_Blob;
62745           pSub->n = nSub*sizeof(SubProgram*);
62746         }
62747       }
62748     }
62749 
62750     pMem->flags = MEM_Int;
62751     pMem->u.i = pOp->p1;                          /* P1 */
62752     pMem++;
62753 
62754     pMem->flags = MEM_Int;
62755     pMem->u.i = pOp->p2;                          /* P2 */
62756     pMem++;
62757 
62758     pMem->flags = MEM_Int;
62759     pMem->u.i = pOp->p3;                          /* P3 */
62760     pMem++;
62761 
62762     if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
62763       assert( p->db->mallocFailed );
62764       return SQLITE_ERROR;
62765     }
62766     pMem->flags = MEM_Str|MEM_Term;
62767     zP4 = displayP4(pOp, pMem->z, 32);
62768     if( zP4!=pMem->z ){
62769       sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
62770     }else{
62771       assert( pMem->z!=0 );
62772       pMem->n = sqlite3Strlen30(pMem->z);
62773       pMem->enc = SQLITE_UTF8;
62774     }
62775     pMem++;
62776 
62777     if( p->explain==1 ){
62778       if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
62779         assert( p->db->mallocFailed );
62780         return SQLITE_ERROR;
62781       }
62782       pMem->flags = MEM_Str|MEM_Term;
62783       pMem->n = 2;
62784       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
62785       pMem->enc = SQLITE_UTF8;
62786       pMem++;
62787 
62788 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62789       if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
62790         assert( p->db->mallocFailed );
62791         return SQLITE_ERROR;
62792       }
62793       pMem->flags = MEM_Str|MEM_Term;
62794       pMem->n = displayComment(pOp, zP4, pMem->z, 500);
62795       pMem->enc = SQLITE_UTF8;
62796 #else
62797       pMem->flags = MEM_Null;                       /* Comment */
62798 #endif
62799     }
62800 
62801     p->nResColumn = 8 - 4*(p->explain-1);
62802     p->pResultSet = &p->aMem[1];
62803     p->rc = SQLITE_OK;
62804     rc = SQLITE_ROW;
62805   }
62806   return rc;
62807 }
62808 #endif /* SQLITE_OMIT_EXPLAIN */
62809 
62810 #ifdef SQLITE_DEBUG
62811 /*
62812 ** Print the SQL that was used to generate a VDBE program.
62813 */
62814 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
62815   const char *z = 0;
62816   if( p->zSql ){
62817     z = p->zSql;
62818   }else if( p->nOp>=1 ){
62819     const VdbeOp *pOp = &p->aOp[0];
62820     if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
62821       z = pOp->p4.z;
62822       while( sqlite3Isspace(*z) ) z++;
62823     }
62824   }
62825   if( z ) printf("SQL: [%s]\n", z);
62826 }
62827 #endif
62828 
62829 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
62830 /*
62831 ** Print an IOTRACE message showing SQL content.
62832 */
62833 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
62834   int nOp = p->nOp;
62835   VdbeOp *pOp;
62836   if( sqlite3IoTrace==0 ) return;
62837   if( nOp<1 ) return;
62838   pOp = &p->aOp[0];
62839   if( pOp->opcode==OP_Init && pOp->p4.z!=0 ){
62840     int i, j;
62841     char z[1000];
62842     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
62843     for(i=0; sqlite3Isspace(z[i]); i++){}
62844     for(j=0; z[i]; i++){
62845       if( sqlite3Isspace(z[i]) ){
62846         if( z[i-1]!=' ' ){
62847           z[j++] = ' ';
62848         }
62849       }else{
62850         z[j++] = z[i];
62851       }
62852     }
62853     z[j] = 0;
62854     sqlite3IoTrace("SQL %s\n", z);
62855   }
62856 }
62857 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
62858 
62859 /*
62860 ** Allocate space from a fixed size buffer and return a pointer to
62861 ** that space.  If insufficient space is available, return NULL.
62862 **
62863 ** The pBuf parameter is the initial value of a pointer which will
62864 ** receive the new memory.  pBuf is normally NULL.  If pBuf is not
62865 ** NULL, it means that memory space has already been allocated and that
62866 ** this routine should not allocate any new memory.  When pBuf is not
62867 ** NULL simply return pBuf.  Only allocate new memory space when pBuf
62868 ** is NULL.
62869 **
62870 ** nByte is the number of bytes of space needed.
62871 **
62872 ** *ppFrom points to available space and pEnd points to the end of the
62873 ** available space.  When space is allocated, *ppFrom is advanced past
62874 ** the end of the allocated space.
62875 **
62876 ** *pnByte is a counter of the number of bytes of space that have failed
62877 ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
62878 ** request, then increment *pnByte by the amount of the request.
62879 */
62880 static void *allocSpace(
62881   void *pBuf,          /* Where return pointer will be stored */
62882   int nByte,           /* Number of bytes to allocate */
62883   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
62884   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
62885   int *pnByte          /* If allocation cannot be made, increment *pnByte */
62886 ){
62887   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
62888   if( pBuf ) return pBuf;
62889   nByte = ROUND8(nByte);
62890   if( &(*ppFrom)[nByte] <= pEnd ){
62891     pBuf = (void*)*ppFrom;
62892     *ppFrom += nByte;
62893   }else{
62894     *pnByte += nByte;
62895   }
62896   return pBuf;
62897 }
62898 
62899 /*
62900 ** Rewind the VDBE back to the beginning in preparation for
62901 ** running it.
62902 */
62903 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
62904 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
62905   int i;
62906 #endif
62907   assert( p!=0 );
62908   assert( p->magic==VDBE_MAGIC_INIT );
62909 
62910   /* There should be at least one opcode.
62911   */
62912   assert( p->nOp>0 );
62913 
62914   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
62915   p->magic = VDBE_MAGIC_RUN;
62916 
62917 #ifdef SQLITE_DEBUG
62918   for(i=1; i<p->nMem; i++){
62919     assert( p->aMem[i].db==p->db );
62920   }
62921 #endif
62922   p->pc = -1;
62923   p->rc = SQLITE_OK;
62924   p->errorAction = OE_Abort;
62925   p->magic = VDBE_MAGIC_RUN;
62926   p->nChange = 0;
62927   p->cacheCtr = 1;
62928   p->minWriteFileFormat = 255;
62929   p->iStatement = 0;
62930   p->nFkConstraint = 0;
62931 #ifdef VDBE_PROFILE
62932   for(i=0; i<p->nOp; i++){
62933     p->aOp[i].cnt = 0;
62934     p->aOp[i].cycles = 0;
62935   }
62936 #endif
62937 }
62938 
62939 /*
62940 ** Prepare a virtual machine for execution for the first time after
62941 ** creating the virtual machine.  This involves things such
62942 ** as allocating stack space and initializing the program counter.
62943 ** After the VDBE has be prepped, it can be executed by one or more
62944 ** calls to sqlite3VdbeExec().
62945 **
62946 ** This function may be called exact once on a each virtual machine.
62947 ** After this routine is called the VM has been "packaged" and is ready
62948 ** to run.  After this routine is called, futher calls to
62949 ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
62950 ** the Vdbe from the Parse object that helped generate it so that the
62951 ** the Vdbe becomes an independent entity and the Parse object can be
62952 ** destroyed.
62953 **
62954 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
62955 ** to its initial state after it has been run.
62956 */
62957 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
62958   Vdbe *p,                       /* The VDBE */
62959   Parse *pParse                  /* Parsing context */
62960 ){
62961   sqlite3 *db;                   /* The database connection */
62962   int nVar;                      /* Number of parameters */
62963   int nMem;                      /* Number of VM memory registers */
62964   int nCursor;                   /* Number of cursors required */
62965   int nArg;                      /* Number of arguments in subprograms */
62966   int nOnce;                     /* Number of OP_Once instructions */
62967   int n;                         /* Loop counter */
62968   u8 *zCsr;                      /* Memory available for allocation */
62969   u8 *zEnd;                      /* First byte past allocated memory */
62970   int nByte;                     /* How much extra memory is needed */
62971 
62972   assert( p!=0 );
62973   assert( p->nOp>0 );
62974   assert( pParse!=0 );
62975   assert( p->magic==VDBE_MAGIC_INIT );
62976   assert( pParse==p->pParse );
62977   db = p->db;
62978   assert( db->mallocFailed==0 );
62979   nVar = pParse->nVar;
62980   nMem = pParse->nMem;
62981   nCursor = pParse->nTab;
62982   nArg = pParse->nMaxArg;
62983   nOnce = pParse->nOnce;
62984   if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
62985 
62986   /* For each cursor required, also allocate a memory cell. Memory
62987   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
62988   ** the vdbe program. Instead they are used to allocate space for
62989   ** VdbeCursor/BtCursor structures. The blob of memory associated with
62990   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
62991   ** stores the blob of memory associated with cursor 1, etc.
62992   **
62993   ** See also: allocateCursor().
62994   */
62995   nMem += nCursor;
62996 
62997   /* Allocate space for memory registers, SQL variables, VDBE cursors and
62998   ** an array to marshal SQL function arguments in.
62999   */
63000   zCsr = (u8*)&p->aOp[p->nOp];            /* Memory avaliable for allocation */
63001   zEnd = (u8*)&p->aOp[pParse->nOpAlloc];  /* First byte past end of zCsr[] */
63002 
63003   resolveP2Values(p, &nArg);
63004   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
63005   if( pParse->explain && nMem<10 ){
63006     nMem = 10;
63007   }
63008   memset(zCsr, 0, zEnd-zCsr);
63009   zCsr += (zCsr - (u8*)0)&7;
63010   assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
63011   p->expired = 0;
63012 
63013   /* Memory for registers, parameters, cursor, etc, is allocated in two
63014   ** passes.  On the first pass, we try to reuse unused space at the
63015   ** end of the opcode array.  If we are unable to satisfy all memory
63016   ** requirements by reusing the opcode array tail, then the second
63017   ** pass will fill in the rest using a fresh allocation.
63018   **
63019   ** This two-pass approach that reuses as much memory as possible from
63020   ** the leftover space at the end of the opcode array can significantly
63021   ** reduce the amount of memory held by a prepared statement.
63022   */
63023   do {
63024     nByte = 0;
63025     p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
63026     p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
63027     p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
63028     p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
63029     p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
63030                           &zCsr, zEnd, &nByte);
63031     p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
63032     if( nByte ){
63033       p->pFree = sqlite3DbMallocZero(db, nByte);
63034     }
63035     zCsr = p->pFree;
63036     zEnd = &zCsr[nByte];
63037   }while( nByte && !db->mallocFailed );
63038 
63039   p->nCursor = nCursor;
63040   p->nOnceFlag = nOnce;
63041   if( p->aVar ){
63042     p->nVar = (ynVar)nVar;
63043     for(n=0; n<nVar; n++){
63044       p->aVar[n].flags = MEM_Null;
63045       p->aVar[n].db = db;
63046     }
63047   }
63048   if( p->azVar ){
63049     p->nzVar = pParse->nzVar;
63050     memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
63051     memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
63052   }
63053   if( p->aMem ){
63054     p->aMem--;                      /* aMem[] goes from 1..nMem */
63055     p->nMem = nMem;                 /*       not from 0..nMem-1 */
63056     for(n=1; n<=nMem; n++){
63057       p->aMem[n].flags = MEM_Undefined;
63058       p->aMem[n].db = db;
63059     }
63060   }
63061   p->explain = pParse->explain;
63062   sqlite3VdbeRewind(p);
63063 }
63064 
63065 /*
63066 ** Close a VDBE cursor and release all the resources that cursor
63067 ** happens to hold.
63068 */
63069 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
63070   if( pCx==0 ){
63071     return;
63072   }
63073   sqlite3VdbeSorterClose(p->db, pCx);
63074   if( pCx->pBt ){
63075     sqlite3BtreeClose(pCx->pBt);
63076     /* The pCx->pCursor will be close automatically, if it exists, by
63077     ** the call above. */
63078   }else if( pCx->pCursor ){
63079     sqlite3BtreeCloseCursor(pCx->pCursor);
63080   }
63081 #ifndef SQLITE_OMIT_VIRTUALTABLE
63082   if( pCx->pVtabCursor ){
63083     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
63084     const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
63085     p->inVtabMethod = 1;
63086     pModule->xClose(pVtabCursor);
63087     p->inVtabMethod = 0;
63088   }
63089 #endif
63090 }
63091 
63092 /*
63093 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
63094 ** is used, for example, when a trigger sub-program is halted to restore
63095 ** control to the main program.
63096 */
63097 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
63098   Vdbe *v = pFrame->v;
63099   v->aOnceFlag = pFrame->aOnceFlag;
63100   v->nOnceFlag = pFrame->nOnceFlag;
63101   v->aOp = pFrame->aOp;
63102   v->nOp = pFrame->nOp;
63103   v->aMem = pFrame->aMem;
63104   v->nMem = pFrame->nMem;
63105   v->apCsr = pFrame->apCsr;
63106   v->nCursor = pFrame->nCursor;
63107   v->db->lastRowid = pFrame->lastRowid;
63108   v->nChange = pFrame->nChange;
63109   return pFrame->pc;
63110 }
63111 
63112 /*
63113 ** Close all cursors.
63114 **
63115 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
63116 ** cell array. This is necessary as the memory cell array may contain
63117 ** pointers to VdbeFrame objects, which may in turn contain pointers to
63118 ** open cursors.
63119 */
63120 static void closeAllCursors(Vdbe *p){
63121   if( p->pFrame ){
63122     VdbeFrame *pFrame;
63123     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
63124     sqlite3VdbeFrameRestore(pFrame);
63125   }
63126   p->pFrame = 0;
63127   p->nFrame = 0;
63128 
63129   if( p->apCsr ){
63130     int i;
63131     for(i=0; i<p->nCursor; i++){
63132       VdbeCursor *pC = p->apCsr[i];
63133       if( pC ){
63134         sqlite3VdbeFreeCursor(p, pC);
63135         p->apCsr[i] = 0;
63136       }
63137     }
63138   }
63139   if( p->aMem ){
63140     releaseMemArray(&p->aMem[1], p->nMem);
63141   }
63142   while( p->pDelFrame ){
63143     VdbeFrame *pDel = p->pDelFrame;
63144     p->pDelFrame = pDel->pParent;
63145     sqlite3VdbeFrameDelete(pDel);
63146   }
63147 
63148   /* Delete any auxdata allocations made by the VM */
63149   sqlite3VdbeDeleteAuxData(p, -1, 0);
63150   assert( p->pAuxData==0 );
63151 }
63152 
63153 /*
63154 ** Clean up the VM after execution.
63155 **
63156 ** This routine will automatically close any cursors, lists, and/or
63157 ** sorters that were left open.  It also deletes the values of
63158 ** variables in the aVar[] array.
63159 */
63160 static void Cleanup(Vdbe *p){
63161   sqlite3 *db = p->db;
63162 
63163 #ifdef SQLITE_DEBUG
63164   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
63165   ** Vdbe.aMem[] arrays have already been cleaned up.  */
63166   int i;
63167   if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
63168   if( p->aMem ){
63169     for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Undefined );
63170   }
63171 #endif
63172 
63173   sqlite3DbFree(db, p->zErrMsg);
63174   p->zErrMsg = 0;
63175   p->pResultSet = 0;
63176 }
63177 
63178 /*
63179 ** Set the number of result columns that will be returned by this SQL
63180 ** statement. This is now set at compile time, rather than during
63181 ** execution of the vdbe program so that sqlite3_column_count() can
63182 ** be called on an SQL statement before sqlite3_step().
63183 */
63184 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
63185   Mem *pColName;
63186   int n;
63187   sqlite3 *db = p->db;
63188 
63189   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
63190   sqlite3DbFree(db, p->aColName);
63191   n = nResColumn*COLNAME_N;
63192   p->nResColumn = (u16)nResColumn;
63193   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
63194   if( p->aColName==0 ) return;
63195   while( n-- > 0 ){
63196     pColName->flags = MEM_Null;
63197     pColName->db = p->db;
63198     pColName++;
63199   }
63200 }
63201 
63202 /*
63203 ** Set the name of the idx'th column to be returned by the SQL statement.
63204 ** zName must be a pointer to a nul terminated string.
63205 **
63206 ** This call must be made after a call to sqlite3VdbeSetNumCols().
63207 **
63208 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
63209 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
63210 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
63211 */
63212 SQLITE_PRIVATE int sqlite3VdbeSetColName(
63213   Vdbe *p,                         /* Vdbe being configured */
63214   int idx,                         /* Index of column zName applies to */
63215   int var,                         /* One of the COLNAME_* constants */
63216   const char *zName,               /* Pointer to buffer containing name */
63217   void (*xDel)(void*)              /* Memory management strategy for zName */
63218 ){
63219   int rc;
63220   Mem *pColName;
63221   assert( idx<p->nResColumn );
63222   assert( var<COLNAME_N );
63223   if( p->db->mallocFailed ){
63224     assert( !zName || xDel!=SQLITE_DYNAMIC );
63225     return SQLITE_NOMEM;
63226   }
63227   assert( p->aColName!=0 );
63228   pColName = &(p->aColName[idx+var*p->nResColumn]);
63229   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
63230   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
63231   return rc;
63232 }
63233 
63234 /*
63235 ** A read or write transaction may or may not be active on database handle
63236 ** db. If a transaction is active, commit it. If there is a
63237 ** write-transaction spanning more than one database file, this routine
63238 ** takes care of the master journal trickery.
63239 */
63240 static int vdbeCommit(sqlite3 *db, Vdbe *p){
63241   int i;
63242   int nTrans = 0;  /* Number of databases with an active write-transaction */
63243   int rc = SQLITE_OK;
63244   int needXcommit = 0;
63245 
63246 #ifdef SQLITE_OMIT_VIRTUALTABLE
63247   /* With this option, sqlite3VtabSync() is defined to be simply
63248   ** SQLITE_OK so p is not used.
63249   */
63250   UNUSED_PARAMETER(p);
63251 #endif
63252 
63253   /* Before doing anything else, call the xSync() callback for any
63254   ** virtual module tables written in this transaction. This has to
63255   ** be done before determining whether a master journal file is
63256   ** required, as an xSync() callback may add an attached database
63257   ** to the transaction.
63258   */
63259   rc = sqlite3VtabSync(db, p);
63260 
63261   /* This loop determines (a) if the commit hook should be invoked and
63262   ** (b) how many database files have open write transactions, not
63263   ** including the temp database. (b) is important because if more than
63264   ** one database file has an open write transaction, a master journal
63265   ** file is required for an atomic commit.
63266   */
63267   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63268     Btree *pBt = db->aDb[i].pBt;
63269     if( sqlite3BtreeIsInTrans(pBt) ){
63270       needXcommit = 1;
63271       if( i!=1 ) nTrans++;
63272       sqlite3BtreeEnter(pBt);
63273       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
63274       sqlite3BtreeLeave(pBt);
63275     }
63276   }
63277   if( rc!=SQLITE_OK ){
63278     return rc;
63279   }
63280 
63281   /* If there are any write-transactions at all, invoke the commit hook */
63282   if( needXcommit && db->xCommitCallback ){
63283     rc = db->xCommitCallback(db->pCommitArg);
63284     if( rc ){
63285       return SQLITE_CONSTRAINT_COMMITHOOK;
63286     }
63287   }
63288 
63289   /* The simple case - no more than one database file (not counting the
63290   ** TEMP database) has a transaction active.   There is no need for the
63291   ** master-journal.
63292   **
63293   ** If the return value of sqlite3BtreeGetFilename() is a zero length
63294   ** string, it means the main database is :memory: or a temp file.  In
63295   ** that case we do not support atomic multi-file commits, so use the
63296   ** simple case then too.
63297   */
63298   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
63299    || nTrans<=1
63300   ){
63301     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63302       Btree *pBt = db->aDb[i].pBt;
63303       if( pBt ){
63304         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
63305       }
63306     }
63307 
63308     /* Do the commit only if all databases successfully complete phase 1.
63309     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
63310     ** IO error while deleting or truncating a journal file. It is unlikely,
63311     ** but could happen. In this case abandon processing and return the error.
63312     */
63313     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63314       Btree *pBt = db->aDb[i].pBt;
63315       if( pBt ){
63316         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
63317       }
63318     }
63319     if( rc==SQLITE_OK ){
63320       sqlite3VtabCommit(db);
63321     }
63322   }
63323 
63324   /* The complex case - There is a multi-file write-transaction active.
63325   ** This requires a master journal file to ensure the transaction is
63326   ** committed atomicly.
63327   */
63328 #ifndef SQLITE_OMIT_DISKIO
63329   else{
63330     sqlite3_vfs *pVfs = db->pVfs;
63331     int needSync = 0;
63332     char *zMaster = 0;   /* File-name for the master journal */
63333     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
63334     sqlite3_file *pMaster = 0;
63335     i64 offset = 0;
63336     int res;
63337     int retryCount = 0;
63338     int nMainFile;
63339 
63340     /* Select a master journal file name */
63341     nMainFile = sqlite3Strlen30(zMainFile);
63342     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
63343     if( zMaster==0 ) return SQLITE_NOMEM;
63344     do {
63345       u32 iRandom;
63346       if( retryCount ){
63347         if( retryCount>100 ){
63348           sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
63349           sqlite3OsDelete(pVfs, zMaster, 0);
63350           break;
63351         }else if( retryCount==1 ){
63352           sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
63353         }
63354       }
63355       retryCount++;
63356       sqlite3_randomness(sizeof(iRandom), &iRandom);
63357       sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
63358                                (iRandom>>8)&0xffffff, iRandom&0xff);
63359       /* The antipenultimate character of the master journal name must
63360       ** be "9" to avoid name collisions when using 8+3 filenames. */
63361       assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
63362       sqlite3FileSuffix3(zMainFile, zMaster);
63363       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
63364     }while( rc==SQLITE_OK && res );
63365     if( rc==SQLITE_OK ){
63366       /* Open the master journal. */
63367       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
63368           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
63369           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
63370       );
63371     }
63372     if( rc!=SQLITE_OK ){
63373       sqlite3DbFree(db, zMaster);
63374       return rc;
63375     }
63376 
63377     /* Write the name of each database file in the transaction into the new
63378     ** master journal file. If an error occurs at this point close
63379     ** and delete the master journal file. All the individual journal files
63380     ** still have 'null' as the master journal pointer, so they will roll
63381     ** back independently if a failure occurs.
63382     */
63383     for(i=0; i<db->nDb; i++){
63384       Btree *pBt = db->aDb[i].pBt;
63385       if( sqlite3BtreeIsInTrans(pBt) ){
63386         char const *zFile = sqlite3BtreeGetJournalname(pBt);
63387         if( zFile==0 ){
63388           continue;  /* Ignore TEMP and :memory: databases */
63389         }
63390         assert( zFile[0]!=0 );
63391         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
63392           needSync = 1;
63393         }
63394         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
63395         offset += sqlite3Strlen30(zFile)+1;
63396         if( rc!=SQLITE_OK ){
63397           sqlite3OsCloseFree(pMaster);
63398           sqlite3OsDelete(pVfs, zMaster, 0);
63399           sqlite3DbFree(db, zMaster);
63400           return rc;
63401         }
63402       }
63403     }
63404 
63405     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
63406     ** flag is set this is not required.
63407     */
63408     if( needSync
63409      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
63410      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
63411     ){
63412       sqlite3OsCloseFree(pMaster);
63413       sqlite3OsDelete(pVfs, zMaster, 0);
63414       sqlite3DbFree(db, zMaster);
63415       return rc;
63416     }
63417 
63418     /* Sync all the db files involved in the transaction. The same call
63419     ** sets the master journal pointer in each individual journal. If
63420     ** an error occurs here, do not delete the master journal file.
63421     **
63422     ** If the error occurs during the first call to
63423     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
63424     ** master journal file will be orphaned. But we cannot delete it,
63425     ** in case the master journal file name was written into the journal
63426     ** file before the failure occurred.
63427     */
63428     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63429       Btree *pBt = db->aDb[i].pBt;
63430       if( pBt ){
63431         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
63432       }
63433     }
63434     sqlite3OsCloseFree(pMaster);
63435     assert( rc!=SQLITE_BUSY );
63436     if( rc!=SQLITE_OK ){
63437       sqlite3DbFree(db, zMaster);
63438       return rc;
63439     }
63440 
63441     /* Delete the master journal file. This commits the transaction. After
63442     ** doing this the directory is synced again before any individual
63443     ** transaction files are deleted.
63444     */
63445     rc = sqlite3OsDelete(pVfs, zMaster, 1);
63446     sqlite3DbFree(db, zMaster);
63447     zMaster = 0;
63448     if( rc ){
63449       return rc;
63450     }
63451 
63452     /* All files and directories have already been synced, so the following
63453     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
63454     ** deleting or truncating journals. If something goes wrong while
63455     ** this is happening we don't really care. The integrity of the
63456     ** transaction is already guaranteed, but some stray 'cold' journals
63457     ** may be lying around. Returning an error code won't help matters.
63458     */
63459     disable_simulated_io_errors();
63460     sqlite3BeginBenignMalloc();
63461     for(i=0; i<db->nDb; i++){
63462       Btree *pBt = db->aDb[i].pBt;
63463       if( pBt ){
63464         sqlite3BtreeCommitPhaseTwo(pBt, 1);
63465       }
63466     }
63467     sqlite3EndBenignMalloc();
63468     enable_simulated_io_errors();
63469 
63470     sqlite3VtabCommit(db);
63471   }
63472 #endif
63473 
63474   return rc;
63475 }
63476 
63477 /*
63478 ** This routine checks that the sqlite3.nVdbeActive count variable
63479 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
63480 ** currently active. An assertion fails if the two counts do not match.
63481 ** This is an internal self-check only - it is not an essential processing
63482 ** step.
63483 **
63484 ** This is a no-op if NDEBUG is defined.
63485 */
63486 #ifndef NDEBUG
63487 static void checkActiveVdbeCnt(sqlite3 *db){
63488   Vdbe *p;
63489   int cnt = 0;
63490   int nWrite = 0;
63491   int nRead = 0;
63492   p = db->pVdbe;
63493   while( p ){
63494     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
63495       cnt++;
63496       if( p->readOnly==0 ) nWrite++;
63497       if( p->bIsReader ) nRead++;
63498     }
63499     p = p->pNext;
63500   }
63501   assert( cnt==db->nVdbeActive );
63502   assert( nWrite==db->nVdbeWrite );
63503   assert( nRead==db->nVdbeRead );
63504 }
63505 #else
63506 #define checkActiveVdbeCnt(x)
63507 #endif
63508 
63509 /*
63510 ** If the Vdbe passed as the first argument opened a statement-transaction,
63511 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
63512 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
63513 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
63514 ** statement transaction is committed.
63515 **
63516 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
63517 ** Otherwise SQLITE_OK.
63518 */
63519 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
63520   sqlite3 *const db = p->db;
63521   int rc = SQLITE_OK;
63522 
63523   /* If p->iStatement is greater than zero, then this Vdbe opened a
63524   ** statement transaction that should be closed here. The only exception
63525   ** is that an IO error may have occurred, causing an emergency rollback.
63526   ** In this case (db->nStatement==0), and there is nothing to do.
63527   */
63528   if( db->nStatement && p->iStatement ){
63529     int i;
63530     const int iSavepoint = p->iStatement-1;
63531 
63532     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
63533     assert( db->nStatement>0 );
63534     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
63535 
63536     for(i=0; i<db->nDb; i++){
63537       int rc2 = SQLITE_OK;
63538       Btree *pBt = db->aDb[i].pBt;
63539       if( pBt ){
63540         if( eOp==SAVEPOINT_ROLLBACK ){
63541           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
63542         }
63543         if( rc2==SQLITE_OK ){
63544           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
63545         }
63546         if( rc==SQLITE_OK ){
63547           rc = rc2;
63548         }
63549       }
63550     }
63551     db->nStatement--;
63552     p->iStatement = 0;
63553 
63554     if( rc==SQLITE_OK ){
63555       if( eOp==SAVEPOINT_ROLLBACK ){
63556         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
63557       }
63558       if( rc==SQLITE_OK ){
63559         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
63560       }
63561     }
63562 
63563     /* If the statement transaction is being rolled back, also restore the
63564     ** database handles deferred constraint counter to the value it had when
63565     ** the statement transaction was opened.  */
63566     if( eOp==SAVEPOINT_ROLLBACK ){
63567       db->nDeferredCons = p->nStmtDefCons;
63568       db->nDeferredImmCons = p->nStmtDefImmCons;
63569     }
63570   }
63571   return rc;
63572 }
63573 
63574 /*
63575 ** This function is called when a transaction opened by the database
63576 ** handle associated with the VM passed as an argument is about to be
63577 ** committed. If there are outstanding deferred foreign key constraint
63578 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
63579 **
63580 ** If there are outstanding FK violations and this function returns
63581 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
63582 ** and write an error message to it. Then return SQLITE_ERROR.
63583 */
63584 #ifndef SQLITE_OMIT_FOREIGN_KEY
63585 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
63586   sqlite3 *db = p->db;
63587   if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
63588    || (!deferred && p->nFkConstraint>0)
63589   ){
63590     p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
63591     p->errorAction = OE_Abort;
63592     sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
63593     return SQLITE_ERROR;
63594   }
63595   return SQLITE_OK;
63596 }
63597 #endif
63598 
63599 /*
63600 ** This routine is called the when a VDBE tries to halt.  If the VDBE
63601 ** has made changes and is in autocommit mode, then commit those
63602 ** changes.  If a rollback is needed, then do the rollback.
63603 **
63604 ** This routine is the only way to move the state of a VM from
63605 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
63606 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
63607 **
63608 ** Return an error code.  If the commit could not complete because of
63609 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
63610 ** means the close did not happen and needs to be repeated.
63611 */
63612 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
63613   int rc;                         /* Used to store transient return codes */
63614   sqlite3 *db = p->db;
63615 
63616   /* This function contains the logic that determines if a statement or
63617   ** transaction will be committed or rolled back as a result of the
63618   ** execution of this virtual machine.
63619   **
63620   ** If any of the following errors occur:
63621   **
63622   **     SQLITE_NOMEM
63623   **     SQLITE_IOERR
63624   **     SQLITE_FULL
63625   **     SQLITE_INTERRUPT
63626   **
63627   ** Then the internal cache might have been left in an inconsistent
63628   ** state.  We need to rollback the statement transaction, if there is
63629   ** one, or the complete transaction if there is no statement transaction.
63630   */
63631 
63632   if( p->db->mallocFailed ){
63633     p->rc = SQLITE_NOMEM;
63634   }
63635   if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
63636   closeAllCursors(p);
63637   if( p->magic!=VDBE_MAGIC_RUN ){
63638     return SQLITE_OK;
63639   }
63640   checkActiveVdbeCnt(db);
63641 
63642   /* No commit or rollback needed if the program never started or if the
63643   ** SQL statement does not read or write a database file.  */
63644   if( p->pc>=0 && p->bIsReader ){
63645     int mrc;   /* Primary error code from p->rc */
63646     int eStatementOp = 0;
63647     int isSpecialError;            /* Set to true if a 'special' error */
63648 
63649     /* Lock all btrees used by the statement */
63650     sqlite3VdbeEnter(p);
63651 
63652     /* Check for one of the special errors */
63653     mrc = p->rc & 0xff;
63654     assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
63655     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
63656                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
63657     if( isSpecialError ){
63658       /* If the query was read-only and the error code is SQLITE_INTERRUPT,
63659       ** no rollback is necessary. Otherwise, at least a savepoint
63660       ** transaction must be rolled back to restore the database to a
63661       ** consistent state.
63662       **
63663       ** Even if the statement is read-only, it is important to perform
63664       ** a statement or transaction rollback operation. If the error
63665       ** occurred while writing to the journal, sub-journal or database
63666       ** file as part of an effort to free up cache space (see function
63667       ** pagerStress() in pager.c), the rollback is required to restore
63668       ** the pager to a consistent state.
63669       */
63670       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
63671         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
63672           eStatementOp = SAVEPOINT_ROLLBACK;
63673         }else{
63674           /* We are forced to roll back the active transaction. Before doing
63675           ** so, abort any other statements this handle currently has active.
63676           */
63677           sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
63678           sqlite3CloseSavepoints(db);
63679           db->autoCommit = 1;
63680         }
63681       }
63682     }
63683 
63684     /* Check for immediate foreign key violations. */
63685     if( p->rc==SQLITE_OK ){
63686       sqlite3VdbeCheckFk(p, 0);
63687     }
63688 
63689     /* If the auto-commit flag is set and this is the only active writer
63690     ** VM, then we do either a commit or rollback of the current transaction.
63691     **
63692     ** Note: This block also runs if one of the special errors handled
63693     ** above has occurred.
63694     */
63695     if( !sqlite3VtabInSync(db)
63696      && db->autoCommit
63697      && db->nVdbeWrite==(p->readOnly==0)
63698     ){
63699       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
63700         rc = sqlite3VdbeCheckFk(p, 1);
63701         if( rc!=SQLITE_OK ){
63702           if( NEVER(p->readOnly) ){
63703             sqlite3VdbeLeave(p);
63704             return SQLITE_ERROR;
63705           }
63706           rc = SQLITE_CONSTRAINT_FOREIGNKEY;
63707         }else{
63708           /* The auto-commit flag is true, the vdbe program was successful
63709           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
63710           ** key constraints to hold up the transaction. This means a commit
63711           ** is required. */
63712           rc = vdbeCommit(db, p);
63713         }
63714         if( rc==SQLITE_BUSY && p->readOnly ){
63715           sqlite3VdbeLeave(p);
63716           return SQLITE_BUSY;
63717         }else if( rc!=SQLITE_OK ){
63718           p->rc = rc;
63719           sqlite3RollbackAll(db, SQLITE_OK);
63720         }else{
63721           db->nDeferredCons = 0;
63722           db->nDeferredImmCons = 0;
63723           db->flags &= ~SQLITE_DeferFKs;
63724           sqlite3CommitInternalChanges(db);
63725         }
63726       }else{
63727         sqlite3RollbackAll(db, SQLITE_OK);
63728       }
63729       db->nStatement = 0;
63730     }else if( eStatementOp==0 ){
63731       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
63732         eStatementOp = SAVEPOINT_RELEASE;
63733       }else if( p->errorAction==OE_Abort ){
63734         eStatementOp = SAVEPOINT_ROLLBACK;
63735       }else{
63736         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
63737         sqlite3CloseSavepoints(db);
63738         db->autoCommit = 1;
63739       }
63740     }
63741 
63742     /* If eStatementOp is non-zero, then a statement transaction needs to
63743     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
63744     ** do so. If this operation returns an error, and the current statement
63745     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
63746     ** current statement error code.
63747     */
63748     if( eStatementOp ){
63749       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
63750       if( rc ){
63751         if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
63752           p->rc = rc;
63753           sqlite3DbFree(db, p->zErrMsg);
63754           p->zErrMsg = 0;
63755         }
63756         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
63757         sqlite3CloseSavepoints(db);
63758         db->autoCommit = 1;
63759       }
63760     }
63761 
63762     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
63763     ** has been rolled back, update the database connection change-counter.
63764     */
63765     if( p->changeCntOn ){
63766       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
63767         sqlite3VdbeSetChanges(db, p->nChange);
63768       }else{
63769         sqlite3VdbeSetChanges(db, 0);
63770       }
63771       p->nChange = 0;
63772     }
63773 
63774     /* Release the locks */
63775     sqlite3VdbeLeave(p);
63776   }
63777 
63778   /* We have successfully halted and closed the VM.  Record this fact. */
63779   if( p->pc>=0 ){
63780     db->nVdbeActive--;
63781     if( !p->readOnly ) db->nVdbeWrite--;
63782     if( p->bIsReader ) db->nVdbeRead--;
63783     assert( db->nVdbeActive>=db->nVdbeRead );
63784     assert( db->nVdbeRead>=db->nVdbeWrite );
63785     assert( db->nVdbeWrite>=0 );
63786   }
63787   p->magic = VDBE_MAGIC_HALT;
63788   checkActiveVdbeCnt(db);
63789   if( p->db->mallocFailed ){
63790     p->rc = SQLITE_NOMEM;
63791   }
63792 
63793   /* If the auto-commit flag is set to true, then any locks that were held
63794   ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
63795   ** to invoke any required unlock-notify callbacks.
63796   */
63797   if( db->autoCommit ){
63798     sqlite3ConnectionUnlocked(db);
63799   }
63800 
63801   assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
63802   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
63803 }
63804 
63805 
63806 /*
63807 ** Each VDBE holds the result of the most recent sqlite3_step() call
63808 ** in p->rc.  This routine sets that result back to SQLITE_OK.
63809 */
63810 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
63811   p->rc = SQLITE_OK;
63812 }
63813 
63814 /*
63815 ** Copy the error code and error message belonging to the VDBE passed
63816 ** as the first argument to its database handle (so that they will be
63817 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
63818 **
63819 ** This function does not clear the VDBE error code or message, just
63820 ** copies them to the database handle.
63821 */
63822 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
63823   sqlite3 *db = p->db;
63824   int rc = p->rc;
63825   if( p->zErrMsg ){
63826     u8 mallocFailed = db->mallocFailed;
63827     sqlite3BeginBenignMalloc();
63828     if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
63829     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
63830     sqlite3EndBenignMalloc();
63831     db->mallocFailed = mallocFailed;
63832     db->errCode = rc;
63833   }else{
63834     sqlite3Error(db, rc, 0);
63835   }
63836   return rc;
63837 }
63838 
63839 #ifdef SQLITE_ENABLE_SQLLOG
63840 /*
63841 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
63842 ** invoke it.
63843 */
63844 static void vdbeInvokeSqllog(Vdbe *v){
63845   if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
63846     char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
63847     assert( v->db->init.busy==0 );
63848     if( zExpanded ){
63849       sqlite3GlobalConfig.xSqllog(
63850           sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
63851       );
63852       sqlite3DbFree(v->db, zExpanded);
63853     }
63854   }
63855 }
63856 #else
63857 # define vdbeInvokeSqllog(x)
63858 #endif
63859 
63860 /*
63861 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
63862 ** Write any error messages into *pzErrMsg.  Return the result code.
63863 **
63864 ** After this routine is run, the VDBE should be ready to be executed
63865 ** again.
63866 **
63867 ** To look at it another way, this routine resets the state of the
63868 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
63869 ** VDBE_MAGIC_INIT.
63870 */
63871 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
63872   sqlite3 *db;
63873   db = p->db;
63874 
63875   /* If the VM did not run to completion or if it encountered an
63876   ** error, then it might not have been halted properly.  So halt
63877   ** it now.
63878   */
63879   sqlite3VdbeHalt(p);
63880 
63881   /* If the VDBE has be run even partially, then transfer the error code
63882   ** and error message from the VDBE into the main database structure.  But
63883   ** if the VDBE has just been set to run but has not actually executed any
63884   ** instructions yet, leave the main database error information unchanged.
63885   */
63886   if( p->pc>=0 ){
63887     vdbeInvokeSqllog(p);
63888     sqlite3VdbeTransferError(p);
63889     sqlite3DbFree(db, p->zErrMsg);
63890     p->zErrMsg = 0;
63891     if( p->runOnlyOnce ) p->expired = 1;
63892   }else if( p->rc && p->expired ){
63893     /* The expired flag was set on the VDBE before the first call
63894     ** to sqlite3_step(). For consistency (since sqlite3_step() was
63895     ** called), set the database error in this case as well.
63896     */
63897     sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
63898     sqlite3DbFree(db, p->zErrMsg);
63899     p->zErrMsg = 0;
63900   }
63901 
63902   /* Reclaim all memory used by the VDBE
63903   */
63904   Cleanup(p);
63905 
63906   /* Save profiling information from this VDBE run.
63907   */
63908 #ifdef VDBE_PROFILE
63909   {
63910     FILE *out = fopen("vdbe_profile.out", "a");
63911     if( out ){
63912       int i;
63913       fprintf(out, "---- ");
63914       for(i=0; i<p->nOp; i++){
63915         fprintf(out, "%02x", p->aOp[i].opcode);
63916       }
63917       fprintf(out, "\n");
63918       if( p->zSql ){
63919         char c, pc = 0;
63920         fprintf(out, "-- ");
63921         for(i=0; (c = p->zSql[i])!=0; i++){
63922           if( pc=='\n' ) fprintf(out, "-- ");
63923           putc(c, out);
63924           pc = c;
63925         }
63926         if( pc!='\n' ) fprintf(out, "\n");
63927       }
63928       for(i=0; i<p->nOp; i++){
63929         char zHdr[100];
63930         sqlite3_snprintf(sizeof(zHdr), zHdr, "%6u %12llu %8llu ",
63931            p->aOp[i].cnt,
63932            p->aOp[i].cycles,
63933            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
63934         );
63935         fprintf(out, "%s", zHdr);
63936         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
63937       }
63938       fclose(out);
63939     }
63940   }
63941 #endif
63942   p->iCurrentTime = 0;
63943   p->magic = VDBE_MAGIC_INIT;
63944   return p->rc & db->errMask;
63945 }
63946 
63947 /*
63948 ** Clean up and delete a VDBE after execution.  Return an integer which is
63949 ** the result code.  Write any error message text into *pzErrMsg.
63950 */
63951 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
63952   int rc = SQLITE_OK;
63953   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
63954     rc = sqlite3VdbeReset(p);
63955     assert( (rc & p->db->errMask)==rc );
63956   }
63957   sqlite3VdbeDelete(p);
63958   return rc;
63959 }
63960 
63961 /*
63962 ** If parameter iOp is less than zero, then invoke the destructor for
63963 ** all auxiliary data pointers currently cached by the VM passed as
63964 ** the first argument.
63965 **
63966 ** Or, if iOp is greater than or equal to zero, then the destructor is
63967 ** only invoked for those auxiliary data pointers created by the user
63968 ** function invoked by the OP_Function opcode at instruction iOp of
63969 ** VM pVdbe, and only then if:
63970 **
63971 **    * the associated function parameter is the 32nd or later (counting
63972 **      from left to right), or
63973 **
63974 **    * the corresponding bit in argument mask is clear (where the first
63975 **      function parameter corrsponds to bit 0 etc.).
63976 */
63977 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
63978   AuxData **pp = &pVdbe->pAuxData;
63979   while( *pp ){
63980     AuxData *pAux = *pp;
63981     if( (iOp<0)
63982      || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
63983     ){
63984       testcase( pAux->iArg==31 );
63985       if( pAux->xDelete ){
63986         pAux->xDelete(pAux->pAux);
63987       }
63988       *pp = pAux->pNext;
63989       sqlite3DbFree(pVdbe->db, pAux);
63990     }else{
63991       pp= &pAux->pNext;
63992     }
63993   }
63994 }
63995 
63996 /*
63997 ** Free all memory associated with the Vdbe passed as the second argument,
63998 ** except for object itself, which is preserved.
63999 **
64000 ** The difference between this function and sqlite3VdbeDelete() is that
64001 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
64002 ** the database connection and frees the object itself.
64003 */
64004 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
64005   SubProgram *pSub, *pNext;
64006   int i;
64007   assert( p->db==0 || p->db==db );
64008   releaseMemArray(p->aVar, p->nVar);
64009   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
64010   for(pSub=p->pProgram; pSub; pSub=pNext){
64011     pNext = pSub->pNext;
64012     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
64013     sqlite3DbFree(db, pSub);
64014   }
64015   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
64016   vdbeFreeOpArray(db, p->aOp, p->nOp);
64017   sqlite3DbFree(db, p->aColName);
64018   sqlite3DbFree(db, p->zSql);
64019   sqlite3DbFree(db, p->pFree);
64020 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
64021   sqlite3DbFree(db, p->zExplain);
64022   sqlite3DbFree(db, p->pExplain);
64023 #endif
64024 }
64025 
64026 /*
64027 ** Delete an entire VDBE.
64028 */
64029 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
64030   sqlite3 *db;
64031 
64032   if( NEVER(p==0) ) return;
64033   db = p->db;
64034   assert( sqlite3_mutex_held(db->mutex) );
64035   sqlite3VdbeClearObject(db, p);
64036   if( p->pPrev ){
64037     p->pPrev->pNext = p->pNext;
64038   }else{
64039     assert( db->pVdbe==p );
64040     db->pVdbe = p->pNext;
64041   }
64042   if( p->pNext ){
64043     p->pNext->pPrev = p->pPrev;
64044   }
64045   p->magic = VDBE_MAGIC_DEAD;
64046   p->db = 0;
64047   sqlite3DbFree(db, p);
64048 }
64049 
64050 /*
64051 ** Make sure the cursor p is ready to read or write the row to which it
64052 ** was last positioned.  Return an error code if an OOM fault or I/O error
64053 ** prevents us from positioning the cursor to its correct position.
64054 **
64055 ** If a MoveTo operation is pending on the given cursor, then do that
64056 ** MoveTo now.  If no move is pending, check to see if the row has been
64057 ** deleted out from under the cursor and if it has, mark the row as
64058 ** a NULL row.
64059 **
64060 ** If the cursor is already pointing to the correct row and that row has
64061 ** not been deleted out from under the cursor, then this routine is a no-op.
64062 */
64063 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
64064   if( p->deferredMoveto ){
64065     int res, rc;
64066 #ifdef SQLITE_TEST
64067     extern int sqlite3_search_count;
64068 #endif
64069     assert( p->isTable );
64070     rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
64071     if( rc ) return rc;
64072     p->lastRowid = p->movetoTarget;
64073     if( res!=0 ) return SQLITE_CORRUPT_BKPT;
64074     p->rowidIsValid = 1;
64075 #ifdef SQLITE_TEST
64076     sqlite3_search_count++;
64077 #endif
64078     p->deferredMoveto = 0;
64079     p->cacheStatus = CACHE_STALE;
64080   }else if( p->pCursor ){
64081     int hasMoved;
64082     int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
64083     if( rc ) return rc;
64084     if( hasMoved ){
64085       p->cacheStatus = CACHE_STALE;
64086       p->nullRow = 1;
64087     }
64088   }
64089   return SQLITE_OK;
64090 }
64091 
64092 /*
64093 ** The following functions:
64094 **
64095 ** sqlite3VdbeSerialType()
64096 ** sqlite3VdbeSerialTypeLen()
64097 ** sqlite3VdbeSerialLen()
64098 ** sqlite3VdbeSerialPut()
64099 ** sqlite3VdbeSerialGet()
64100 **
64101 ** encapsulate the code that serializes values for storage in SQLite
64102 ** data and index records. Each serialized value consists of a
64103 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
64104 ** integer, stored as a varint.
64105 **
64106 ** In an SQLite index record, the serial type is stored directly before
64107 ** the blob of data that it corresponds to. In a table record, all serial
64108 ** types are stored at the start of the record, and the blobs of data at
64109 ** the end. Hence these functions allow the caller to handle the
64110 ** serial-type and data blob separately.
64111 **
64112 ** The following table describes the various storage classes for data:
64113 **
64114 **   serial type        bytes of data      type
64115 **   --------------     ---------------    ---------------
64116 **      0                     0            NULL
64117 **      1                     1            signed integer
64118 **      2                     2            signed integer
64119 **      3                     3            signed integer
64120 **      4                     4            signed integer
64121 **      5                     6            signed integer
64122 **      6                     8            signed integer
64123 **      7                     8            IEEE float
64124 **      8                     0            Integer constant 0
64125 **      9                     0            Integer constant 1
64126 **     10,11                               reserved for expansion
64127 **    N>=12 and even       (N-12)/2        BLOB
64128 **    N>=13 and odd        (N-13)/2        text
64129 **
64130 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
64131 ** of SQLite will not understand those serial types.
64132 */
64133 
64134 /*
64135 ** Return the serial-type for the value stored in pMem.
64136 */
64137 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
64138   int flags = pMem->flags;
64139   int n;
64140 
64141   if( flags&MEM_Null ){
64142     return 0;
64143   }
64144   if( flags&MEM_Int ){
64145     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
64146 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
64147     i64 i = pMem->u.i;
64148     u64 u;
64149     if( i<0 ){
64150       if( i<(-MAX_6BYTE) ) return 6;
64151       /* Previous test prevents:  u = -(-9223372036854775808) */
64152       u = -i;
64153     }else{
64154       u = i;
64155     }
64156     if( u<=127 ){
64157       return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
64158     }
64159     if( u<=32767 ) return 2;
64160     if( u<=8388607 ) return 3;
64161     if( u<=2147483647 ) return 4;
64162     if( u<=MAX_6BYTE ) return 5;
64163     return 6;
64164   }
64165   if( flags&MEM_Real ){
64166     return 7;
64167   }
64168   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
64169   n = pMem->n;
64170   if( flags & MEM_Zero ){
64171     n += pMem->u.nZero;
64172   }
64173   assert( n>=0 );
64174   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
64175 }
64176 
64177 /*
64178 ** Return the length of the data corresponding to the supplied serial-type.
64179 */
64180 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
64181   if( serial_type>=12 ){
64182     return (serial_type-12)/2;
64183   }else{
64184     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
64185     return aSize[serial_type];
64186   }
64187 }
64188 
64189 /*
64190 ** If we are on an architecture with mixed-endian floating
64191 ** points (ex: ARM7) then swap the lower 4 bytes with the
64192 ** upper 4 bytes.  Return the result.
64193 **
64194 ** For most architectures, this is a no-op.
64195 **
64196 ** (later):  It is reported to me that the mixed-endian problem
64197 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
64198 ** that early versions of GCC stored the two words of a 64-bit
64199 ** float in the wrong order.  And that error has been propagated
64200 ** ever since.  The blame is not necessarily with GCC, though.
64201 ** GCC might have just copying the problem from a prior compiler.
64202 ** I am also told that newer versions of GCC that follow a different
64203 ** ABI get the byte order right.
64204 **
64205 ** Developers using SQLite on an ARM7 should compile and run their
64206 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
64207 ** enabled, some asserts below will ensure that the byte order of
64208 ** floating point values is correct.
64209 **
64210 ** (2007-08-30)  Frank van Vugt has studied this problem closely
64211 ** and has send his findings to the SQLite developers.  Frank
64212 ** writes that some Linux kernels offer floating point hardware
64213 ** emulation that uses only 32-bit mantissas instead of a full
64214 ** 48-bits as required by the IEEE standard.  (This is the
64215 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
64216 ** byte swapping becomes very complicated.  To avoid problems,
64217 ** the necessary byte swapping is carried out using a 64-bit integer
64218 ** rather than a 64-bit float.  Frank assures us that the code here
64219 ** works for him.  We, the developers, have no way to independently
64220 ** verify this, but Frank seems to know what he is talking about
64221 ** so we trust him.
64222 */
64223 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
64224 static u64 floatSwap(u64 in){
64225   union {
64226     u64 r;
64227     u32 i[2];
64228   } u;
64229   u32 t;
64230 
64231   u.r = in;
64232   t = u.i[0];
64233   u.i[0] = u.i[1];
64234   u.i[1] = t;
64235   return u.r;
64236 }
64237 # define swapMixedEndianFloat(X)  X = floatSwap(X)
64238 #else
64239 # define swapMixedEndianFloat(X)
64240 #endif
64241 
64242 /*
64243 ** Write the serialized data blob for the value stored in pMem into
64244 ** buf. It is assumed that the caller has allocated sufficient space.
64245 ** Return the number of bytes written.
64246 **
64247 ** nBuf is the amount of space left in buf[].  The caller is responsible
64248 ** for allocating enough space to buf[] to hold the entire field, exclusive
64249 ** of the pMem->u.nZero bytes for a MEM_Zero value.
64250 **
64251 ** Return the number of bytes actually written into buf[].  The number
64252 ** of bytes in the zero-filled tail is included in the return value only
64253 ** if those bytes were zeroed in buf[].
64254 */
64255 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
64256   u32 len;
64257 
64258   /* Integer and Real */
64259   if( serial_type<=7 && serial_type>0 ){
64260     u64 v;
64261     u32 i;
64262     if( serial_type==7 ){
64263       assert( sizeof(v)==sizeof(pMem->r) );
64264       memcpy(&v, &pMem->r, sizeof(v));
64265       swapMixedEndianFloat(v);
64266     }else{
64267       v = pMem->u.i;
64268     }
64269     len = i = sqlite3VdbeSerialTypeLen(serial_type);
64270     while( i-- ){
64271       buf[i] = (u8)(v&0xFF);
64272       v >>= 8;
64273     }
64274     return len;
64275   }
64276 
64277   /* String or blob */
64278   if( serial_type>=12 ){
64279     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
64280              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
64281     len = pMem->n;
64282     memcpy(buf, pMem->z, len);
64283     return len;
64284   }
64285 
64286   /* NULL or constants 0 or 1 */
64287   return 0;
64288 }
64289 
64290 /* Input "x" is a sequence of unsigned characters that represent a
64291 ** big-endian integer.  Return the equivalent native integer
64292 */
64293 #define ONE_BYTE_INT(x)    ((i8)(x)[0])
64294 #define TWO_BYTE_INT(x)    (256*(i8)((x)[0])|(x)[1])
64295 #define THREE_BYTE_INT(x)  (65536*(i8)((x)[0])|((x)[1]<<8)|(x)[2])
64296 #define FOUR_BYTE_UINT(x)  (((u32)(x)[0]<<24)|((x)[1]<<16)|((x)[2]<<8)|(x)[3])
64297 
64298 /*
64299 ** Deserialize the data blob pointed to by buf as serial type serial_type
64300 ** and store the result in pMem.  Return the number of bytes read.
64301 */
64302 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
64303   const unsigned char *buf,     /* Buffer to deserialize from */
64304   u32 serial_type,              /* Serial type to deserialize */
64305   Mem *pMem                     /* Memory cell to write value into */
64306 ){
64307   u64 x;
64308   u32 y;
64309   switch( serial_type ){
64310     case 10:   /* Reserved for future use */
64311     case 11:   /* Reserved for future use */
64312     case 0: {  /* NULL */
64313       pMem->flags = MEM_Null;
64314       break;
64315     }
64316     case 1: { /* 1-byte signed integer */
64317       pMem->u.i = ONE_BYTE_INT(buf);
64318       pMem->flags = MEM_Int;
64319       testcase( pMem->u.i<0 );
64320       return 1;
64321     }
64322     case 2: { /* 2-byte signed integer */
64323       pMem->u.i = TWO_BYTE_INT(buf);
64324       pMem->flags = MEM_Int;
64325       testcase( pMem->u.i<0 );
64326       return 2;
64327     }
64328     case 3: { /* 3-byte signed integer */
64329       pMem->u.i = THREE_BYTE_INT(buf);
64330       pMem->flags = MEM_Int;
64331       testcase( pMem->u.i<0 );
64332       return 3;
64333     }
64334     case 4: { /* 4-byte signed integer */
64335       y = FOUR_BYTE_UINT(buf);
64336       pMem->u.i = (i64)*(int*)&y;
64337       pMem->flags = MEM_Int;
64338       testcase( pMem->u.i<0 );
64339       return 4;
64340     }
64341     case 5: { /* 6-byte signed integer */
64342       pMem->u.i = FOUR_BYTE_UINT(buf+2) + (((i64)1)<<32)*TWO_BYTE_INT(buf);
64343       pMem->flags = MEM_Int;
64344       testcase( pMem->u.i<0 );
64345       return 6;
64346     }
64347     case 6:   /* 8-byte signed integer */
64348     case 7: { /* IEEE floating point */
64349 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
64350       /* Verify that integers and floating point values use the same
64351       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
64352       ** defined that 64-bit floating point values really are mixed
64353       ** endian.
64354       */
64355       static const u64 t1 = ((u64)0x3ff00000)<<32;
64356       static const double r1 = 1.0;
64357       u64 t2 = t1;
64358       swapMixedEndianFloat(t2);
64359       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
64360 #endif
64361       x = FOUR_BYTE_UINT(buf);
64362       y = FOUR_BYTE_UINT(buf+4);
64363       x = (x<<32) | y;
64364       if( serial_type==6 ){
64365         pMem->u.i = *(i64*)&x;
64366         pMem->flags = MEM_Int;
64367         testcase( pMem->u.i<0 );
64368       }else{
64369         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
64370         swapMixedEndianFloat(x);
64371         memcpy(&pMem->r, &x, sizeof(x));
64372         pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
64373       }
64374       return 8;
64375     }
64376     case 8:    /* Integer 0 */
64377     case 9: {  /* Integer 1 */
64378       pMem->u.i = serial_type-8;
64379       pMem->flags = MEM_Int;
64380       return 0;
64381     }
64382     default: {
64383       static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
64384       u32 len = (serial_type-12)/2;
64385       pMem->z = (char *)buf;
64386       pMem->n = len;
64387       pMem->xDel = 0;
64388       pMem->flags = aFlag[serial_type&1];
64389       return len;
64390     }
64391   }
64392   return 0;
64393 }
64394 
64395 /*
64396 ** This routine is used to allocate sufficient space for an UnpackedRecord
64397 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
64398 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
64399 **
64400 ** The space is either allocated using sqlite3DbMallocRaw() or from within
64401 ** the unaligned buffer passed via the second and third arguments (presumably
64402 ** stack space). If the former, then *ppFree is set to a pointer that should
64403 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
64404 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
64405 ** before returning.
64406 **
64407 ** If an OOM error occurs, NULL is returned.
64408 */
64409 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
64410   KeyInfo *pKeyInfo,              /* Description of the record */
64411   char *pSpace,                   /* Unaligned space available */
64412   int szSpace,                    /* Size of pSpace[] in bytes */
64413   char **ppFree                   /* OUT: Caller should free this pointer */
64414 ){
64415   UnpackedRecord *p;              /* Unpacked record to return */
64416   int nOff;                       /* Increment pSpace by nOff to align it */
64417   int nByte;                      /* Number of bytes required for *p */
64418 
64419   /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
64420   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
64421   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
64422   */
64423   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
64424   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
64425   if( nByte>szSpace+nOff ){
64426     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
64427     *ppFree = (char *)p;
64428     if( !p ) return 0;
64429   }else{
64430     p = (UnpackedRecord*)&pSpace[nOff];
64431     *ppFree = 0;
64432   }
64433 
64434   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
64435   assert( pKeyInfo->aSortOrder!=0 );
64436   p->pKeyInfo = pKeyInfo;
64437   p->nField = pKeyInfo->nField + 1;
64438   return p;
64439 }
64440 
64441 /*
64442 ** Given the nKey-byte encoding of a record in pKey[], populate the
64443 ** UnpackedRecord structure indicated by the fourth argument with the
64444 ** contents of the decoded record.
64445 */
64446 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
64447   KeyInfo *pKeyInfo,     /* Information about the record format */
64448   int nKey,              /* Size of the binary record */
64449   const void *pKey,      /* The binary record */
64450   UnpackedRecord *p      /* Populate this structure before returning. */
64451 ){
64452   const unsigned char *aKey = (const unsigned char *)pKey;
64453   int d;
64454   u32 idx;                        /* Offset in aKey[] to read from */
64455   u16 u;                          /* Unsigned loop counter */
64456   u32 szHdr;
64457   Mem *pMem = p->aMem;
64458 
64459   p->default_rc = 0;
64460   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64461   idx = getVarint32(aKey, szHdr);
64462   d = szHdr;
64463   u = 0;
64464   while( idx<szHdr && u<p->nField && d<=nKey ){
64465     u32 serial_type;
64466 
64467     idx += getVarint32(&aKey[idx], serial_type);
64468     pMem->enc = pKeyInfo->enc;
64469     pMem->db = pKeyInfo->db;
64470     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
64471     pMem->zMalloc = 0;
64472     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
64473     pMem++;
64474     u++;
64475   }
64476   assert( u<=pKeyInfo->nField + 1 );
64477   p->nField = u;
64478 }
64479 
64480 #if SQLITE_DEBUG
64481 /*
64482 ** This function compares two index or table record keys in the same way
64483 ** as the sqlite3VdbeRecordCompare() routine. Unlike VdbeRecordCompare(),
64484 ** this function deserializes and compares values using the
64485 ** sqlite3VdbeSerialGet() and sqlite3MemCompare() functions. It is used
64486 ** in assert() statements to ensure that the optimized code in
64487 ** sqlite3VdbeRecordCompare() returns results with these two primitives.
64488 */
64489 static int vdbeRecordCompareDebug(
64490   int nKey1, const void *pKey1, /* Left key */
64491   const UnpackedRecord *pPKey2  /* Right key */
64492 ){
64493   u32 d1;            /* Offset into aKey[] of next data element */
64494   u32 idx1;          /* Offset into aKey[] of next header element */
64495   u32 szHdr1;        /* Number of bytes in header */
64496   int i = 0;
64497   int rc = 0;
64498   const unsigned char *aKey1 = (const unsigned char *)pKey1;
64499   KeyInfo *pKeyInfo;
64500   Mem mem1;
64501 
64502   pKeyInfo = pPKey2->pKeyInfo;
64503   mem1.enc = pKeyInfo->enc;
64504   mem1.db = pKeyInfo->db;
64505   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
64506   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
64507 
64508   /* Compilers may complain that mem1.u.i is potentially uninitialized.
64509   ** We could initialize it, as shown here, to silence those complaints.
64510   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
64511   ** the unnecessary initialization has a measurable negative performance
64512   ** impact, since this routine is a very high runner.  And so, we choose
64513   ** to ignore the compiler warnings and leave this variable uninitialized.
64514   */
64515   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
64516 
64517   idx1 = getVarint32(aKey1, szHdr1);
64518   d1 = szHdr1;
64519   assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
64520   assert( pKeyInfo->aSortOrder!=0 );
64521   assert( pKeyInfo->nField>0 );
64522   assert( idx1<=szHdr1 || CORRUPT_DB );
64523   do{
64524     u32 serial_type1;
64525 
64526     /* Read the serial types for the next element in each key. */
64527     idx1 += getVarint32( aKey1+idx1, serial_type1 );
64528 
64529     /* Verify that there is enough key space remaining to avoid
64530     ** a buffer overread.  The "d1+serial_type1+2" subexpression will
64531     ** always be greater than or equal to the amount of required key space.
64532     ** Use that approximation to avoid the more expensive call to
64533     ** sqlite3VdbeSerialTypeLen() in the common case.
64534     */
64535     if( d1+serial_type1+2>(u32)nKey1
64536      && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
64537     ){
64538       break;
64539     }
64540 
64541     /* Extract the values to be compared.
64542     */
64543     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
64544 
64545     /* Do the comparison
64546     */
64547     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
64548     if( rc!=0 ){
64549       assert( mem1.zMalloc==0 );  /* See comment below */
64550       if( pKeyInfo->aSortOrder[i] ){
64551         rc = -rc;  /* Invert the result for DESC sort order. */
64552       }
64553       return rc;
64554     }
64555     i++;
64556   }while( idx1<szHdr1 && i<pPKey2->nField );
64557 
64558   /* No memory allocation is ever used on mem1.  Prove this using
64559   ** the following assert().  If the assert() fails, it indicates a
64560   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
64561   */
64562   assert( mem1.zMalloc==0 );
64563 
64564   /* rc==0 here means that one of the keys ran out of fields and
64565   ** all the fields up to that point were equal. Return the the default_rc
64566   ** value.  */
64567   return pPKey2->default_rc;
64568 }
64569 #endif
64570 
64571 /*
64572 ** Both *pMem1 and *pMem2 contain string values. Compare the two values
64573 ** using the collation sequence pColl. As usual, return a negative , zero
64574 ** or positive value if *pMem1 is less than, equal to or greater than
64575 ** *pMem2, respectively. Similar in spirit to "rc = (*pMem1) - (*pMem2);".
64576 */
64577 static int vdbeCompareMemString(
64578   const Mem *pMem1,
64579   const Mem *pMem2,
64580   const CollSeq *pColl
64581 ){
64582   if( pMem1->enc==pColl->enc ){
64583     /* The strings are already in the correct encoding.  Call the
64584      ** comparison function directly */
64585     return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
64586   }else{
64587     int rc;
64588     const void *v1, *v2;
64589     int n1, n2;
64590     Mem c1;
64591     Mem c2;
64592     memset(&c1, 0, sizeof(c1));
64593     memset(&c2, 0, sizeof(c2));
64594     sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
64595     sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
64596     v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
64597     n1 = v1==0 ? 0 : c1.n;
64598     v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
64599     n2 = v2==0 ? 0 : c2.n;
64600     rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
64601     sqlite3VdbeMemRelease(&c1);
64602     sqlite3VdbeMemRelease(&c2);
64603     return rc;
64604   }
64605 }
64606 
64607 /*
64608 ** Compare the values contained by the two memory cells, returning
64609 ** negative, zero or positive if pMem1 is less than, equal to, or greater
64610 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
64611 ** and reals) sorted numerically, followed by text ordered by the collating
64612 ** sequence pColl and finally blob's ordered by memcmp().
64613 **
64614 ** Two NULL values are considered equal by this function.
64615 */
64616 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
64617   int rc;
64618   int f1, f2;
64619   int combined_flags;
64620 
64621   f1 = pMem1->flags;
64622   f2 = pMem2->flags;
64623   combined_flags = f1|f2;
64624   assert( (combined_flags & MEM_RowSet)==0 );
64625 
64626   /* If one value is NULL, it is less than the other. If both values
64627   ** are NULL, return 0.
64628   */
64629   if( combined_flags&MEM_Null ){
64630     return (f2&MEM_Null) - (f1&MEM_Null);
64631   }
64632 
64633   /* If one value is a number and the other is not, the number is less.
64634   ** If both are numbers, compare as reals if one is a real, or as integers
64635   ** if both values are integers.
64636   */
64637   if( combined_flags&(MEM_Int|MEM_Real) ){
64638     double r1, r2;
64639     if( (f1 & f2 & MEM_Int)!=0 ){
64640       if( pMem1->u.i < pMem2->u.i ) return -1;
64641       if( pMem1->u.i > pMem2->u.i ) return 1;
64642       return 0;
64643     }
64644     if( (f1&MEM_Real)!=0 ){
64645       r1 = pMem1->r;
64646     }else if( (f1&MEM_Int)!=0 ){
64647       r1 = (double)pMem1->u.i;
64648     }else{
64649       return 1;
64650     }
64651     if( (f2&MEM_Real)!=0 ){
64652       r2 = pMem2->r;
64653     }else if( (f2&MEM_Int)!=0 ){
64654       r2 = (double)pMem2->u.i;
64655     }else{
64656       return -1;
64657     }
64658     if( r1<r2 ) return -1;
64659     if( r1>r2 ) return 1;
64660     return 0;
64661   }
64662 
64663   /* If one value is a string and the other is a blob, the string is less.
64664   ** If both are strings, compare using the collating functions.
64665   */
64666   if( combined_flags&MEM_Str ){
64667     if( (f1 & MEM_Str)==0 ){
64668       return 1;
64669     }
64670     if( (f2 & MEM_Str)==0 ){
64671       return -1;
64672     }
64673 
64674     assert( pMem1->enc==pMem2->enc );
64675     assert( pMem1->enc==SQLITE_UTF8 ||
64676             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
64677 
64678     /* The collation sequence must be defined at this point, even if
64679     ** the user deletes the collation sequence after the vdbe program is
64680     ** compiled (this was not always the case).
64681     */
64682     assert( !pColl || pColl->xCmp );
64683 
64684     if( pColl ){
64685       return vdbeCompareMemString(pMem1, pMem2, pColl);
64686     }
64687     /* If a NULL pointer was passed as the collate function, fall through
64688     ** to the blob case and use memcmp().  */
64689   }
64690 
64691   /* Both values must be blobs.  Compare using memcmp().  */
64692   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
64693   if( rc==0 ){
64694     rc = pMem1->n - pMem2->n;
64695   }
64696   return rc;
64697 }
64698 
64699 
64700 /*
64701 ** The first argument passed to this function is a serial-type that
64702 ** corresponds to an integer - all values between 1 and 9 inclusive
64703 ** except 7. The second points to a buffer containing an integer value
64704 ** serialized according to serial_type. This function deserializes
64705 ** and returns the value.
64706 */
64707 static i64 vdbeRecordDecodeInt(u32 serial_type, const u8 *aKey){
64708   u32 y;
64709   assert( CORRUPT_DB || (serial_type>=1 && serial_type<=9 && serial_type!=7) );
64710   switch( serial_type ){
64711     case 0:
64712     case 1:
64713       testcase( aKey[0]&0x80 );
64714       return ONE_BYTE_INT(aKey);
64715     case 2:
64716       testcase( aKey[0]&0x80 );
64717       return TWO_BYTE_INT(aKey);
64718     case 3:
64719       testcase( aKey[0]&0x80 );
64720       return THREE_BYTE_INT(aKey);
64721     case 4: {
64722       testcase( aKey[0]&0x80 );
64723       y = FOUR_BYTE_UINT(aKey);
64724       return (i64)*(int*)&y;
64725     }
64726     case 5: {
64727       testcase( aKey[0]&0x80 );
64728       return FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64729     }
64730     case 6: {
64731       u64 x = FOUR_BYTE_UINT(aKey);
64732       testcase( aKey[0]&0x80 );
64733       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64734       return (i64)*(i64*)&x;
64735     }
64736   }
64737 
64738   return (serial_type - 8);
64739 }
64740 
64741 /*
64742 ** This function compares the two table rows or index records
64743 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
64744 ** or positive integer if key1 is less than, equal to or
64745 ** greater than key2.  The {nKey1, pKey1} key must be a blob
64746 ** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
64747 ** key must be a parsed key such as obtained from
64748 ** sqlite3VdbeParseRecord.
64749 **
64750 ** If argument bSkip is non-zero, it is assumed that the caller has already
64751 ** determined that the first fields of the keys are equal.
64752 **
64753 ** Key1 and Key2 do not have to contain the same number of fields. If all
64754 ** fields that appear in both keys are equal, then pPKey2->default_rc is
64755 ** returned.
64756 */
64757 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64758   int nKey1, const void *pKey1,   /* Left key */
64759   const UnpackedRecord *pPKey2,   /* Right key */
64760   int bSkip                       /* If true, skip the first field */
64761 ){
64762   u32 d1;                         /* Offset into aKey[] of next data element */
64763   int i;                          /* Index of next field to compare */
64764   u32 szHdr1;                     /* Size of record header in bytes */
64765   u32 idx1;                       /* Offset of first type in header */
64766   int rc = 0;                     /* Return value */
64767   Mem *pRhs = pPKey2->aMem;       /* Next field of pPKey2 to compare */
64768   KeyInfo *pKeyInfo = pPKey2->pKeyInfo;
64769   const unsigned char *aKey1 = (const unsigned char *)pKey1;
64770   Mem mem1;
64771 
64772   /* If bSkip is true, then the caller has already determined that the first
64773   ** two elements in the keys are equal. Fix the various stack variables so
64774   ** that this routine begins comparing at the second field. */
64775   if( bSkip ){
64776     u32 s1;
64777     idx1 = 1 + getVarint32(&aKey1[1], s1);
64778     szHdr1 = aKey1[0];
64779     d1 = szHdr1 + sqlite3VdbeSerialTypeLen(s1);
64780     i = 1;
64781     pRhs++;
64782   }else{
64783     idx1 = getVarint32(aKey1, szHdr1);
64784     d1 = szHdr1;
64785     i = 0;
64786   }
64787 
64788   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
64789   assert( pPKey2->pKeyInfo->nField+pPKey2->pKeyInfo->nXField>=pPKey2->nField
64790        || CORRUPT_DB );
64791   assert( pPKey2->pKeyInfo->aSortOrder!=0 );
64792   assert( pPKey2->pKeyInfo->nField>0 );
64793   assert( idx1<=szHdr1 || CORRUPT_DB );
64794   do{
64795     u32 serial_type;
64796 
64797     /* RHS is an integer */
64798     if( pRhs->flags & MEM_Int ){
64799       serial_type = aKey1[idx1];
64800       testcase( serial_type==12 );
64801       if( serial_type>=12 ){
64802         rc = +1;
64803       }else if( serial_type==0 ){
64804         rc = -1;
64805       }else if( serial_type==7 ){
64806         double rhs = (double)pRhs->u.i;
64807         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64808         if( mem1.r<rhs ){
64809           rc = -1;
64810         }else if( mem1.r>rhs ){
64811           rc = +1;
64812         }
64813       }else{
64814         i64 lhs = vdbeRecordDecodeInt(serial_type, &aKey1[d1]);
64815         i64 rhs = pRhs->u.i;
64816         if( lhs<rhs ){
64817           rc = -1;
64818         }else if( lhs>rhs ){
64819           rc = +1;
64820         }
64821       }
64822     }
64823 
64824     /* RHS is real */
64825     else if( pRhs->flags & MEM_Real ){
64826       serial_type = aKey1[idx1];
64827       if( serial_type>=12 ){
64828         rc = +1;
64829       }else if( serial_type==0 ){
64830         rc = -1;
64831       }else{
64832         double rhs = pRhs->r;
64833         double lhs;
64834         sqlite3VdbeSerialGet(&aKey1[d1], serial_type, &mem1);
64835         if( serial_type==7 ){
64836           lhs = mem1.r;
64837         }else{
64838           lhs = (double)mem1.u.i;
64839         }
64840         if( lhs<rhs ){
64841           rc = -1;
64842         }else if( lhs>rhs ){
64843           rc = +1;
64844         }
64845       }
64846     }
64847 
64848     /* RHS is a string */
64849     else if( pRhs->flags & MEM_Str ){
64850       getVarint32(&aKey1[idx1], serial_type);
64851       testcase( serial_type==12 );
64852       if( serial_type<12 ){
64853         rc = -1;
64854       }else if( !(serial_type & 0x01) ){
64855         rc = +1;
64856       }else{
64857         mem1.n = (serial_type - 12) / 2;
64858         testcase( (d1+mem1.n)==(unsigned)nKey1 );
64859         testcase( (d1+mem1.n+1)==(unsigned)nKey1 );
64860         if( (d1+mem1.n) > (unsigned)nKey1 ){
64861           rc = 1;                /* Corruption */
64862         }else if( pKeyInfo->aColl[i] ){
64863           mem1.enc = pKeyInfo->enc;
64864           mem1.db = pKeyInfo->db;
64865           mem1.flags = MEM_Str;
64866           mem1.z = (char*)&aKey1[d1];
64867           rc = vdbeCompareMemString(&mem1, pRhs, pKeyInfo->aColl[i]);
64868         }else{
64869           int nCmp = MIN(mem1.n, pRhs->n);
64870           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64871           if( rc==0 ) rc = mem1.n - pRhs->n;
64872         }
64873       }
64874     }
64875 
64876     /* RHS is a blob */
64877     else if( pRhs->flags & MEM_Blob ){
64878       getVarint32(&aKey1[idx1], serial_type);
64879       testcase( serial_type==12 );
64880       if( serial_type<12 || (serial_type & 0x01) ){
64881         rc = -1;
64882       }else{
64883         int nStr = (serial_type - 12) / 2;
64884         testcase( (d1+nStr)==(unsigned)nKey1 );
64885         testcase( (d1+nStr+1)==(unsigned)nKey1 );
64886         if( (d1+nStr) > (unsigned)nKey1 ){
64887           rc = 1;                /* Corruption */
64888         }else{
64889           int nCmp = MIN(nStr, pRhs->n);
64890           rc = memcmp(&aKey1[d1], pRhs->z, nCmp);
64891           if( rc==0 ) rc = nStr - pRhs->n;
64892         }
64893       }
64894     }
64895 
64896     /* RHS is null */
64897     else{
64898       serial_type = aKey1[idx1];
64899       rc = (serial_type!=0);
64900     }
64901 
64902     if( rc!=0 ){
64903       if( pKeyInfo->aSortOrder[i] ){
64904         rc = -rc;
64905       }
64906       assert( CORRUPT_DB
64907           || (rc<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
64908           || (rc>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
64909           || pKeyInfo->db->mallocFailed
64910       );
64911       assert( mem1.zMalloc==0 );  /* See comment below */
64912       return rc;
64913     }
64914 
64915     i++;
64916     pRhs++;
64917     d1 += sqlite3VdbeSerialTypeLen(serial_type);
64918     idx1 += sqlite3VarintLen(serial_type);
64919   }while( idx1<(unsigned)szHdr1 && i<pPKey2->nField && d1<=(unsigned)nKey1 );
64920 
64921   /* No memory allocation is ever used on mem1.  Prove this using
64922   ** the following assert().  If the assert() fails, it indicates a
64923   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).  */
64924   assert( mem1.zMalloc==0 );
64925 
64926   /* rc==0 here means that one or both of the keys ran out of fields and
64927   ** all the fields up to that point were equal. Return the the default_rc
64928   ** value.  */
64929   assert( CORRUPT_DB
64930        || pPKey2->default_rc==vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)
64931   );
64932   return pPKey2->default_rc;
64933 }
64934 
64935 /*
64936 ** This function is an optimized version of sqlite3VdbeRecordCompare()
64937 ** that (a) the first field of pPKey2 is an integer, and (b) the
64938 ** size-of-header varint at the start of (pKey1/nKey1) fits in a single
64939 ** byte (i.e. is less than 128).
64940 */
64941 static int vdbeRecordCompareInt(
64942   int nKey1, const void *pKey1, /* Left key */
64943   const UnpackedRecord *pPKey2, /* Right key */
64944   int bSkip                     /* Ignored */
64945 ){
64946   const u8 *aKey = &((const u8*)pKey1)[*(const u8*)pKey1 & 0x3F];
64947   int serial_type = ((const u8*)pKey1)[1];
64948   int res;
64949   u32 y;
64950   u64 x;
64951   i64 v = pPKey2->aMem[0].u.i;
64952   i64 lhs;
64953   UNUSED_PARAMETER(bSkip);
64954 
64955   assert( bSkip==0 );
64956   switch( serial_type ){
64957     case 1: { /* 1-byte signed integer */
64958       lhs = ONE_BYTE_INT(aKey);
64959       testcase( lhs<0 );
64960       break;
64961     }
64962     case 2: { /* 2-byte signed integer */
64963       lhs = TWO_BYTE_INT(aKey);
64964       testcase( lhs<0 );
64965       break;
64966     }
64967     case 3: { /* 3-byte signed integer */
64968       lhs = THREE_BYTE_INT(aKey);
64969       testcase( lhs<0 );
64970       break;
64971     }
64972     case 4: { /* 4-byte signed integer */
64973       y = FOUR_BYTE_UINT(aKey);
64974       lhs = (i64)*(int*)&y;
64975       testcase( lhs<0 );
64976       break;
64977     }
64978     case 5: { /* 6-byte signed integer */
64979       lhs = FOUR_BYTE_UINT(aKey+2) + (((i64)1)<<32)*TWO_BYTE_INT(aKey);
64980       testcase( lhs<0 );
64981       break;
64982     }
64983     case 6: { /* 8-byte signed integer */
64984       x = FOUR_BYTE_UINT(aKey);
64985       x = (x<<32) | FOUR_BYTE_UINT(aKey+4);
64986       lhs = *(i64*)&x;
64987       testcase( lhs<0 );
64988       break;
64989     }
64990     case 8:
64991       lhs = 0;
64992       break;
64993     case 9:
64994       lhs = 1;
64995       break;
64996 
64997     /* This case could be removed without changing the results of running
64998     ** this code. Including it causes gcc to generate a faster switch
64999     ** statement (since the range of switch targets now starts at zero and
65000     ** is contiguous) but does not cause any duplicate code to be generated
65001     ** (as gcc is clever enough to combine the two like cases). Other
65002     ** compilers might be similar.  */
65003     case 0: case 7:
65004       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
65005 
65006     default:
65007       return sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 0);
65008   }
65009 
65010   if( v>lhs ){
65011     res = pPKey2->r1;
65012   }else if( v<lhs ){
65013     res = pPKey2->r2;
65014   }else if( pPKey2->nField>1 ){
65015     /* The first fields of the two keys are equal. Compare the trailing
65016     ** fields.  */
65017     res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
65018   }else{
65019     /* The first fields of the two keys are equal and there are no trailing
65020     ** fields. Return pPKey2->default_rc in this case. */
65021     res = pPKey2->default_rc;
65022   }
65023 
65024   assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65025        || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65026        || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65027        || CORRUPT_DB
65028   );
65029   return res;
65030 }
65031 
65032 /*
65033 ** This function is an optimized version of sqlite3VdbeRecordCompare()
65034 ** that (a) the first field of pPKey2 is a string, that (b) the first field
65035 ** uses the collation sequence BINARY and (c) that the size-of-header varint
65036 ** at the start of (pKey1/nKey1) fits in a single byte.
65037 */
65038 static int vdbeRecordCompareString(
65039   int nKey1, const void *pKey1, /* Left key */
65040   const UnpackedRecord *pPKey2, /* Right key */
65041   int bSkip
65042 ){
65043   const u8 *aKey1 = (const u8*)pKey1;
65044   int serial_type;
65045   int res;
65046   UNUSED_PARAMETER(bSkip);
65047 
65048   assert( bSkip==0 );
65049   getVarint32(&aKey1[1], serial_type);
65050 
65051   if( serial_type<12 ){
65052     res = pPKey2->r1;      /* (pKey1/nKey1) is a number or a null */
65053   }else if( !(serial_type & 0x01) ){
65054     res = pPKey2->r2;      /* (pKey1/nKey1) is a blob */
65055   }else{
65056     int nCmp;
65057     int nStr;
65058     int szHdr = aKey1[0];
65059 
65060     nStr = (serial_type-12) / 2;
65061     if( (szHdr + nStr) > nKey1 ) return 0;    /* Corruption */
65062     nCmp = MIN( pPKey2->aMem[0].n, nStr );
65063     res = memcmp(&aKey1[szHdr], pPKey2->aMem[0].z, nCmp);
65064 
65065     if( res==0 ){
65066       res = nStr - pPKey2->aMem[0].n;
65067       if( res==0 ){
65068         if( pPKey2->nField>1 ){
65069           res = sqlite3VdbeRecordCompare(nKey1, pKey1, pPKey2, 1);
65070         }else{
65071           res = pPKey2->default_rc;
65072         }
65073       }else if( res>0 ){
65074         res = pPKey2->r2;
65075       }else{
65076         res = pPKey2->r1;
65077       }
65078     }else if( res>0 ){
65079       res = pPKey2->r2;
65080     }else{
65081       res = pPKey2->r1;
65082     }
65083   }
65084 
65085   assert( (res==0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)==0)
65086        || (res<0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)<0)
65087        || (res>0 && vdbeRecordCompareDebug(nKey1, pKey1, pPKey2)>0)
65088        || CORRUPT_DB
65089   );
65090   return res;
65091 }
65092 
65093 /*
65094 ** Return a pointer to an sqlite3VdbeRecordCompare() compatible function
65095 ** suitable for comparing serialized records to the unpacked record passed
65096 ** as the only argument.
65097 */
65098 SQLITE_PRIVATE RecordCompare sqlite3VdbeFindCompare(UnpackedRecord *p){
65099   /* varintRecordCompareInt() and varintRecordCompareString() both assume
65100   ** that the size-of-header varint that occurs at the start of each record
65101   ** fits in a single byte (i.e. is 127 or less). varintRecordCompareInt()
65102   ** also assumes that it is safe to overread a buffer by at least the
65103   ** maximum possible legal header size plus 8 bytes. Because there is
65104   ** guaranteed to be at least 74 (but not 136) bytes of padding following each
65105   ** buffer passed to varintRecordCompareInt() this makes it convenient to
65106   ** limit the size of the header to 64 bytes in cases where the first field
65107   ** is an integer.
65108   **
65109   ** The easiest way to enforce this limit is to consider only records with
65110   ** 13 fields or less. If the first field is an integer, the maximum legal
65111   ** header size is (12*5 + 1 + 1) bytes.  */
65112   if( (p->pKeyInfo->nField + p->pKeyInfo->nXField)<=13 ){
65113     int flags = p->aMem[0].flags;
65114     if( p->pKeyInfo->aSortOrder[0] ){
65115       p->r1 = 1;
65116       p->r2 = -1;
65117     }else{
65118       p->r1 = -1;
65119       p->r2 = 1;
65120     }
65121     if( (flags & MEM_Int) ){
65122       return vdbeRecordCompareInt;
65123     }
65124     testcase( flags & MEM_Real );
65125     testcase( flags & MEM_Null );
65126     testcase( flags & MEM_Blob );
65127     if( (flags & (MEM_Real|MEM_Null|MEM_Blob))==0 && p->pKeyInfo->aColl[0]==0 ){
65128       assert( flags & MEM_Str );
65129       return vdbeRecordCompareString;
65130     }
65131   }
65132 
65133   return sqlite3VdbeRecordCompare;
65134 }
65135 
65136 /*
65137 ** pCur points at an index entry created using the OP_MakeRecord opcode.
65138 ** Read the rowid (the last field in the record) and store it in *rowid.
65139 ** Return SQLITE_OK if everything works, or an error code otherwise.
65140 **
65141 ** pCur might be pointing to text obtained from a corrupt database file.
65142 ** So the content cannot be trusted.  Do appropriate checks on the content.
65143 */
65144 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
65145   i64 nCellKey = 0;
65146   int rc;
65147   u32 szHdr;        /* Size of the header */
65148   u32 typeRowid;    /* Serial type of the rowid */
65149   u32 lenRowid;     /* Size of the rowid */
65150   Mem m, v;
65151 
65152   UNUSED_PARAMETER(db);
65153 
65154   /* Get the size of the index entry.  Only indices entries of less
65155   ** than 2GiB are support - anything large must be database corruption.
65156   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
65157   ** this code can safely assume that nCellKey is 32-bits
65158   */
65159   assert( sqlite3BtreeCursorIsValid(pCur) );
65160   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
65161   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
65162   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
65163 
65164   /* Read in the complete content of the index entry */
65165   memset(&m, 0, sizeof(m));
65166   rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
65167   if( rc ){
65168     return rc;
65169   }
65170 
65171   /* The index entry must begin with a header size */
65172   (void)getVarint32((u8*)m.z, szHdr);
65173   testcase( szHdr==3 );
65174   testcase( szHdr==m.n );
65175   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
65176     goto idx_rowid_corruption;
65177   }
65178 
65179   /* The last field of the index should be an integer - the ROWID.
65180   ** Verify that the last entry really is an integer. */
65181   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
65182   testcase( typeRowid==1 );
65183   testcase( typeRowid==2 );
65184   testcase( typeRowid==3 );
65185   testcase( typeRowid==4 );
65186   testcase( typeRowid==5 );
65187   testcase( typeRowid==6 );
65188   testcase( typeRowid==8 );
65189   testcase( typeRowid==9 );
65190   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
65191     goto idx_rowid_corruption;
65192   }
65193   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
65194   testcase( (u32)m.n==szHdr+lenRowid );
65195   if( unlikely((u32)m.n<szHdr+lenRowid) ){
65196     goto idx_rowid_corruption;
65197   }
65198 
65199   /* Fetch the integer off the end of the index record */
65200   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
65201   *rowid = v.u.i;
65202   sqlite3VdbeMemRelease(&m);
65203   return SQLITE_OK;
65204 
65205   /* Jump here if database corruption is detected after m has been
65206   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
65207 idx_rowid_corruption:
65208   testcase( m.zMalloc!=0 );
65209   sqlite3VdbeMemRelease(&m);
65210   return SQLITE_CORRUPT_BKPT;
65211 }
65212 
65213 /*
65214 ** Compare the key of the index entry that cursor pC is pointing to against
65215 ** the key string in pUnpacked.  Write into *pRes a number
65216 ** that is negative, zero, or positive if pC is less than, equal to,
65217 ** or greater than pUnpacked.  Return SQLITE_OK on success.
65218 **
65219 ** pUnpacked is either created without a rowid or is truncated so that it
65220 ** omits the rowid at the end.  The rowid at the end of the index entry
65221 ** is ignored as well.  Hence, this routine only compares the prefixes
65222 ** of the keys prior to the final rowid, not the entire key.
65223 */
65224 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
65225   VdbeCursor *pC,                  /* The cursor to compare against */
65226   const UnpackedRecord *pUnpacked, /* Unpacked version of key */
65227   int *res                         /* Write the comparison result here */
65228 ){
65229   i64 nCellKey = 0;
65230   int rc;
65231   BtCursor *pCur = pC->pCursor;
65232   Mem m;
65233 
65234   assert( sqlite3BtreeCursorIsValid(pCur) );
65235   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
65236   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
65237   /* nCellKey will always be between 0 and 0xffffffff because of the way
65238   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
65239   if( nCellKey<=0 || nCellKey>0x7fffffff ){
65240     *res = 0;
65241     return SQLITE_CORRUPT_BKPT;
65242   }
65243   memset(&m, 0, sizeof(m));
65244   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
65245   if( rc ){
65246     return rc;
65247   }
65248   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked, 0);
65249   sqlite3VdbeMemRelease(&m);
65250   return SQLITE_OK;
65251 }
65252 
65253 /*
65254 ** This routine sets the value to be returned by subsequent calls to
65255 ** sqlite3_changes() on the database handle 'db'.
65256 */
65257 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
65258   assert( sqlite3_mutex_held(db->mutex) );
65259   db->nChange = nChange;
65260   db->nTotalChange += nChange;
65261 }
65262 
65263 /*
65264 ** Set a flag in the vdbe to update the change counter when it is finalised
65265 ** or reset.
65266 */
65267 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
65268   v->changeCntOn = 1;
65269 }
65270 
65271 /*
65272 ** Mark every prepared statement associated with a database connection
65273 ** as expired.
65274 **
65275 ** An expired statement means that recompilation of the statement is
65276 ** recommend.  Statements expire when things happen that make their
65277 ** programs obsolete.  Removing user-defined functions or collating
65278 ** sequences, or changing an authorization function are the types of
65279 ** things that make prepared statements obsolete.
65280 */
65281 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
65282   Vdbe *p;
65283   for(p = db->pVdbe; p; p=p->pNext){
65284     p->expired = 1;
65285   }
65286 }
65287 
65288 /*
65289 ** Return the database associated with the Vdbe.
65290 */
65291 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
65292   return v->db;
65293 }
65294 
65295 /*
65296 ** Return a pointer to an sqlite3_value structure containing the value bound
65297 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
65298 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
65299 ** constants) to the value before returning it.
65300 **
65301 ** The returned value must be freed by the caller using sqlite3ValueFree().
65302 */
65303 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
65304   assert( iVar>0 );
65305   if( v ){
65306     Mem *pMem = &v->aVar[iVar-1];
65307     if( 0==(pMem->flags & MEM_Null) ){
65308       sqlite3_value *pRet = sqlite3ValueNew(v->db);
65309       if( pRet ){
65310         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
65311         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
65312       }
65313       return pRet;
65314     }
65315   }
65316   return 0;
65317 }
65318 
65319 /*
65320 ** Configure SQL variable iVar so that binding a new value to it signals
65321 ** to sqlite3_reoptimize() that re-preparing the statement may result
65322 ** in a better query plan.
65323 */
65324 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
65325   assert( iVar>0 );
65326   if( iVar>32 ){
65327     v->expmask = 0xffffffff;
65328   }else{
65329     v->expmask |= ((u32)1 << (iVar-1));
65330   }
65331 }
65332 
65333 #ifndef SQLITE_OMIT_VIRTUALTABLE
65334 /*
65335 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
65336 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
65337 ** in memory obtained from sqlite3DbMalloc).
65338 */
65339 SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
65340   sqlite3 *db = p->db;
65341   sqlite3DbFree(db, p->zErrMsg);
65342   p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
65343   sqlite3_free(pVtab->zErrMsg);
65344   pVtab->zErrMsg = 0;
65345 }
65346 #endif /* SQLITE_OMIT_VIRTUALTABLE */
65347 
65348 /************** End of vdbeaux.c *********************************************/
65349 /************** Begin file vdbeapi.c *****************************************/
65350 /*
65351 ** 2004 May 26
65352 **
65353 ** The author disclaims copyright to this source code.  In place of
65354 ** a legal notice, here is a blessing:
65355 **
65356 **    May you do good and not evil.
65357 **    May you find forgiveness for yourself and forgive others.
65358 **    May you share freely, never taking more than you give.
65359 **
65360 *************************************************************************
65361 **
65362 ** This file contains code use to implement APIs that are part of the
65363 ** VDBE.
65364 */
65365 
65366 #ifndef SQLITE_OMIT_DEPRECATED
65367 /*
65368 ** Return TRUE (non-zero) of the statement supplied as an argument needs
65369 ** to be recompiled.  A statement needs to be recompiled whenever the
65370 ** execution environment changes in a way that would alter the program
65371 ** that sqlite3_prepare() generates.  For example, if new functions or
65372 ** collating sequences are registered or if an authorizer function is
65373 ** added or changed.
65374 */
65375 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
65376   Vdbe *p = (Vdbe*)pStmt;
65377   return p==0 || p->expired;
65378 }
65379 #endif
65380 
65381 /*
65382 ** Check on a Vdbe to make sure it has not been finalized.  Log
65383 ** an error and return true if it has been finalized (or is otherwise
65384 ** invalid).  Return false if it is ok.
65385 */
65386 static int vdbeSafety(Vdbe *p){
65387   if( p->db==0 ){
65388     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
65389     return 1;
65390   }else{
65391     return 0;
65392   }
65393 }
65394 static int vdbeSafetyNotNull(Vdbe *p){
65395   if( p==0 ){
65396     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
65397     return 1;
65398   }else{
65399     return vdbeSafety(p);
65400   }
65401 }
65402 
65403 /*
65404 ** The following routine destroys a virtual machine that is created by
65405 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
65406 ** success/failure code that describes the result of executing the virtual
65407 ** machine.
65408 **
65409 ** This routine sets the error code and string returned by
65410 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
65411 */
65412 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
65413   int rc;
65414   if( pStmt==0 ){
65415     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
65416     ** pointer is a harmless no-op. */
65417     rc = SQLITE_OK;
65418   }else{
65419     Vdbe *v = (Vdbe*)pStmt;
65420     sqlite3 *db = v->db;
65421     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
65422     sqlite3_mutex_enter(db->mutex);
65423     rc = sqlite3VdbeFinalize(v);
65424     rc = sqlite3ApiExit(db, rc);
65425     sqlite3LeaveMutexAndCloseZombie(db);
65426   }
65427   return rc;
65428 }
65429 
65430 /*
65431 ** Terminate the current execution of an SQL statement and reset it
65432 ** back to its starting state so that it can be reused. A success code from
65433 ** the prior execution is returned.
65434 **
65435 ** This routine sets the error code and string returned by
65436 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
65437 */
65438 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
65439   int rc;
65440   if( pStmt==0 ){
65441     rc = SQLITE_OK;
65442   }else{
65443     Vdbe *v = (Vdbe*)pStmt;
65444     sqlite3_mutex_enter(v->db->mutex);
65445     rc = sqlite3VdbeReset(v);
65446     sqlite3VdbeRewind(v);
65447     assert( (rc & (v->db->errMask))==rc );
65448     rc = sqlite3ApiExit(v->db, rc);
65449     sqlite3_mutex_leave(v->db->mutex);
65450   }
65451   return rc;
65452 }
65453 
65454 /*
65455 ** Set all the parameters in the compiled SQL statement to NULL.
65456 */
65457 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
65458   int i;
65459   int rc = SQLITE_OK;
65460   Vdbe *p = (Vdbe*)pStmt;
65461 #if SQLITE_THREADSAFE
65462   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
65463 #endif
65464   sqlite3_mutex_enter(mutex);
65465   for(i=0; i<p->nVar; i++){
65466     sqlite3VdbeMemRelease(&p->aVar[i]);
65467     p->aVar[i].flags = MEM_Null;
65468   }
65469   if( p->isPrepareV2 && p->expmask ){
65470     p->expired = 1;
65471   }
65472   sqlite3_mutex_leave(mutex);
65473   return rc;
65474 }
65475 
65476 
65477 /**************************** sqlite3_value_  *******************************
65478 ** The following routines extract information from a Mem or sqlite3_value
65479 ** structure.
65480 */
65481 SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
65482   Mem *p = (Mem*)pVal;
65483   if( p->flags & (MEM_Blob|MEM_Str) ){
65484     sqlite3VdbeMemExpandBlob(p);
65485     p->flags |= MEM_Blob;
65486     return p->n ? p->z : 0;
65487   }else{
65488     return sqlite3_value_text(pVal);
65489   }
65490 }
65491 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
65492   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
65493 }
65494 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
65495   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
65496 }
65497 SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
65498   return sqlite3VdbeRealValue((Mem*)pVal);
65499 }
65500 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
65501   return (int)sqlite3VdbeIntValue((Mem*)pVal);
65502 }
65503 SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
65504   return sqlite3VdbeIntValue((Mem*)pVal);
65505 }
65506 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
65507   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
65508 }
65509 #ifndef SQLITE_OMIT_UTF16
65510 SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
65511   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
65512 }
65513 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
65514   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
65515 }
65516 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
65517   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
65518 }
65519 #endif /* SQLITE_OMIT_UTF16 */
65520 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
65521   static const u8 aType[] = {
65522      SQLITE_BLOB,     /* 0x00 */
65523      SQLITE_NULL,     /* 0x01 */
65524      SQLITE_TEXT,     /* 0x02 */
65525      SQLITE_NULL,     /* 0x03 */
65526      SQLITE_INTEGER,  /* 0x04 */
65527      SQLITE_NULL,     /* 0x05 */
65528      SQLITE_INTEGER,  /* 0x06 */
65529      SQLITE_NULL,     /* 0x07 */
65530      SQLITE_FLOAT,    /* 0x08 */
65531      SQLITE_NULL,     /* 0x09 */
65532      SQLITE_FLOAT,    /* 0x0a */
65533      SQLITE_NULL,     /* 0x0b */
65534      SQLITE_INTEGER,  /* 0x0c */
65535      SQLITE_NULL,     /* 0x0d */
65536      SQLITE_INTEGER,  /* 0x0e */
65537      SQLITE_NULL,     /* 0x0f */
65538      SQLITE_BLOB,     /* 0x10 */
65539      SQLITE_NULL,     /* 0x11 */
65540      SQLITE_TEXT,     /* 0x12 */
65541      SQLITE_NULL,     /* 0x13 */
65542      SQLITE_INTEGER,  /* 0x14 */
65543      SQLITE_NULL,     /* 0x15 */
65544      SQLITE_INTEGER,  /* 0x16 */
65545      SQLITE_NULL,     /* 0x17 */
65546      SQLITE_FLOAT,    /* 0x18 */
65547      SQLITE_NULL,     /* 0x19 */
65548      SQLITE_FLOAT,    /* 0x1a */
65549      SQLITE_NULL,     /* 0x1b */
65550      SQLITE_INTEGER,  /* 0x1c */
65551      SQLITE_NULL,     /* 0x1d */
65552      SQLITE_INTEGER,  /* 0x1e */
65553      SQLITE_NULL,     /* 0x1f */
65554   };
65555   return aType[pVal->flags&MEM_AffMask];
65556 }
65557 
65558 /**************************** sqlite3_result_  *******************************
65559 ** The following routines are used by user-defined functions to specify
65560 ** the function result.
65561 **
65562 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
65563 ** result as a string or blob but if the string or blob is too large, it
65564 ** then sets the error code to SQLITE_TOOBIG
65565 */
65566 static void setResultStrOrError(
65567   sqlite3_context *pCtx,  /* Function context */
65568   const char *z,          /* String pointer */
65569   int n,                  /* Bytes in string, or negative */
65570   u8 enc,                 /* Encoding of z.  0 for BLOBs */
65571   void (*xDel)(void*)     /* Destructor function */
65572 ){
65573   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
65574     sqlite3_result_error_toobig(pCtx);
65575   }
65576 }
65577 SQLITE_API void sqlite3_result_blob(
65578   sqlite3_context *pCtx,
65579   const void *z,
65580   int n,
65581   void (*xDel)(void *)
65582 ){
65583   assert( n>=0 );
65584   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65585   setResultStrOrError(pCtx, z, n, 0, xDel);
65586 }
65587 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
65588   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65589   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
65590 }
65591 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
65592   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65593   pCtx->isError = SQLITE_ERROR;
65594   pCtx->fErrorOrAux = 1;
65595   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
65596 }
65597 #ifndef SQLITE_OMIT_UTF16
65598 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
65599   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65600   pCtx->isError = SQLITE_ERROR;
65601   pCtx->fErrorOrAux = 1;
65602   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
65603 }
65604 #endif
65605 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
65606   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65607   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
65608 }
65609 SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
65610   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65611   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
65612 }
65613 SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
65614   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65615   sqlite3VdbeMemSetNull(&pCtx->s);
65616 }
65617 SQLITE_API void sqlite3_result_text(
65618   sqlite3_context *pCtx,
65619   const char *z,
65620   int n,
65621   void (*xDel)(void *)
65622 ){
65623   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65624   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
65625 }
65626 #ifndef SQLITE_OMIT_UTF16
65627 SQLITE_API void sqlite3_result_text16(
65628   sqlite3_context *pCtx,
65629   const void *z,
65630   int n,
65631   void (*xDel)(void *)
65632 ){
65633   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65634   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
65635 }
65636 SQLITE_API void sqlite3_result_text16be(
65637   sqlite3_context *pCtx,
65638   const void *z,
65639   int n,
65640   void (*xDel)(void *)
65641 ){
65642   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65643   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
65644 }
65645 SQLITE_API void sqlite3_result_text16le(
65646   sqlite3_context *pCtx,
65647   const void *z,
65648   int n,
65649   void (*xDel)(void *)
65650 ){
65651   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65652   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
65653 }
65654 #endif /* SQLITE_OMIT_UTF16 */
65655 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
65656   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65657   sqlite3VdbeMemCopy(&pCtx->s, pValue);
65658 }
65659 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
65660   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65661   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
65662 }
65663 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
65664   pCtx->isError = errCode;
65665   pCtx->fErrorOrAux = 1;
65666   if( pCtx->s.flags & MEM_Null ){
65667     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
65668                          SQLITE_UTF8, SQLITE_STATIC);
65669   }
65670 }
65671 
65672 /* Force an SQLITE_TOOBIG error. */
65673 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
65674   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65675   pCtx->isError = SQLITE_TOOBIG;
65676   pCtx->fErrorOrAux = 1;
65677   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
65678                        SQLITE_UTF8, SQLITE_STATIC);
65679 }
65680 
65681 /* An SQLITE_NOMEM error. */
65682 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
65683   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65684   sqlite3VdbeMemSetNull(&pCtx->s);
65685   pCtx->isError = SQLITE_NOMEM;
65686   pCtx->fErrorOrAux = 1;
65687   pCtx->s.db->mallocFailed = 1;
65688 }
65689 
65690 /*
65691 ** This function is called after a transaction has been committed. It
65692 ** invokes callbacks registered with sqlite3_wal_hook() as required.
65693 */
65694 static int doWalCallbacks(sqlite3 *db){
65695   int rc = SQLITE_OK;
65696 #ifndef SQLITE_OMIT_WAL
65697   int i;
65698   for(i=0; i<db->nDb; i++){
65699     Btree *pBt = db->aDb[i].pBt;
65700     if( pBt ){
65701       int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
65702       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
65703         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
65704       }
65705     }
65706   }
65707 #endif
65708   return rc;
65709 }
65710 
65711 /*
65712 ** Execute the statement pStmt, either until a row of data is ready, the
65713 ** statement is completely executed or an error occurs.
65714 **
65715 ** This routine implements the bulk of the logic behind the sqlite_step()
65716 ** API.  The only thing omitted is the automatic recompile if a
65717 ** schema change has occurred.  That detail is handled by the
65718 ** outer sqlite3_step() wrapper procedure.
65719 */
65720 static int sqlite3Step(Vdbe *p){
65721   sqlite3 *db;
65722   int rc;
65723 
65724   assert(p);
65725   if( p->magic!=VDBE_MAGIC_RUN ){
65726     /* We used to require that sqlite3_reset() be called before retrying
65727     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
65728     ** with version 3.7.0, we changed this so that sqlite3_reset() would
65729     ** be called automatically instead of throwing the SQLITE_MISUSE error.
65730     ** This "automatic-reset" change is not technically an incompatibility,
65731     ** since any application that receives an SQLITE_MISUSE is broken by
65732     ** definition.
65733     **
65734     ** Nevertheless, some published applications that were originally written
65735     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
65736     ** returns, and those were broken by the automatic-reset change.  As a
65737     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
65738     ** legacy behavior of returning SQLITE_MISUSE for cases where the
65739     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
65740     ** or SQLITE_BUSY error.
65741     */
65742 #ifdef SQLITE_OMIT_AUTORESET
65743     if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
65744       sqlite3_reset((sqlite3_stmt*)p);
65745     }else{
65746       return SQLITE_MISUSE_BKPT;
65747     }
65748 #else
65749     sqlite3_reset((sqlite3_stmt*)p);
65750 #endif
65751   }
65752 
65753   /* Check that malloc() has not failed. If it has, return early. */
65754   db = p->db;
65755   if( db->mallocFailed ){
65756     p->rc = SQLITE_NOMEM;
65757     return SQLITE_NOMEM;
65758   }
65759 
65760   if( p->pc<=0 && p->expired ){
65761     p->rc = SQLITE_SCHEMA;
65762     rc = SQLITE_ERROR;
65763     goto end_of_step;
65764   }
65765   if( p->pc<0 ){
65766     /* If there are no other statements currently running, then
65767     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
65768     ** from interrupting a statement that has not yet started.
65769     */
65770     if( db->nVdbeActive==0 ){
65771       db->u1.isInterrupted = 0;
65772     }
65773 
65774     assert( db->nVdbeWrite>0 || db->autoCommit==0
65775         || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
65776     );
65777 
65778 #ifndef SQLITE_OMIT_TRACE
65779     if( db->xProfile && !db->init.busy ){
65780       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
65781     }
65782 #endif
65783 
65784     db->nVdbeActive++;
65785     if( p->readOnly==0 ) db->nVdbeWrite++;
65786     if( p->bIsReader ) db->nVdbeRead++;
65787     p->pc = 0;
65788   }
65789 #ifndef SQLITE_OMIT_EXPLAIN
65790   if( p->explain ){
65791     rc = sqlite3VdbeList(p);
65792   }else
65793 #endif /* SQLITE_OMIT_EXPLAIN */
65794   {
65795     db->nVdbeExec++;
65796     rc = sqlite3VdbeExec(p);
65797     db->nVdbeExec--;
65798   }
65799 
65800 #ifndef SQLITE_OMIT_TRACE
65801   /* Invoke the profile callback if there is one
65802   */
65803   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
65804     sqlite3_int64 iNow;
65805     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
65806     db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
65807   }
65808 #endif
65809 
65810   if( rc==SQLITE_DONE ){
65811     assert( p->rc==SQLITE_OK );
65812     p->rc = doWalCallbacks(db);
65813     if( p->rc!=SQLITE_OK ){
65814       rc = SQLITE_ERROR;
65815     }
65816   }
65817 
65818   db->errCode = rc;
65819   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
65820     p->rc = SQLITE_NOMEM;
65821   }
65822 end_of_step:
65823   /* At this point local variable rc holds the value that should be
65824   ** returned if this statement was compiled using the legacy
65825   ** sqlite3_prepare() interface. According to the docs, this can only
65826   ** be one of the values in the first assert() below. Variable p->rc
65827   ** contains the value that would be returned if sqlite3_finalize()
65828   ** were called on statement p.
65829   */
65830   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
65831        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
65832   );
65833   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
65834   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
65835     /* If this statement was prepared using sqlite3_prepare_v2(), and an
65836     ** error has occurred, then return the error code in p->rc to the
65837     ** caller. Set the error code in the database handle to the same value.
65838     */
65839     rc = sqlite3VdbeTransferError(p);
65840   }
65841   return (rc&db->errMask);
65842 }
65843 
65844 /*
65845 ** This is the top-level implementation of sqlite3_step().  Call
65846 ** sqlite3Step() to do most of the work.  If a schema error occurs,
65847 ** call sqlite3Reprepare() and try again.
65848 */
65849 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
65850   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
65851   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
65852   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
65853   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
65854   sqlite3 *db;             /* The database connection */
65855 
65856   if( vdbeSafetyNotNull(v) ){
65857     return SQLITE_MISUSE_BKPT;
65858   }
65859   db = v->db;
65860   sqlite3_mutex_enter(db->mutex);
65861   v->doingRerun = 0;
65862   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
65863          && cnt++ < SQLITE_MAX_SCHEMA_RETRY
65864          && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
65865     sqlite3_reset(pStmt);
65866     v->doingRerun = 1;
65867     assert( v->expired==0 );
65868   }
65869   if( rc2!=SQLITE_OK ){
65870     /* This case occurs after failing to recompile an sql statement.
65871     ** The error message from the SQL compiler has already been loaded
65872     ** into the database handle. This block copies the error message
65873     ** from the database handle into the statement and sets the statement
65874     ** program counter to 0 to ensure that when the statement is
65875     ** finalized or reset the parser error message is available via
65876     ** sqlite3_errmsg() and sqlite3_errcode().
65877     */
65878     const char *zErr = (const char *)sqlite3_value_text(db->pErr);
65879     assert( zErr!=0 || db->mallocFailed );
65880     sqlite3DbFree(db, v->zErrMsg);
65881     if( !db->mallocFailed ){
65882       v->zErrMsg = sqlite3DbStrDup(db, zErr);
65883       v->rc = rc2;
65884     } else {
65885       v->zErrMsg = 0;
65886       v->rc = rc = SQLITE_NOMEM;
65887     }
65888   }
65889   rc = sqlite3ApiExit(db, rc);
65890   sqlite3_mutex_leave(db->mutex);
65891   return rc;
65892 }
65893 
65894 
65895 /*
65896 ** Extract the user data from a sqlite3_context structure and return a
65897 ** pointer to it.
65898 */
65899 SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
65900   assert( p && p->pFunc );
65901   return p->pFunc->pUserData;
65902 }
65903 
65904 /*
65905 ** Extract the user data from a sqlite3_context structure and return a
65906 ** pointer to it.
65907 **
65908 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
65909 ** returns a copy of the pointer to the database connection (the 1st
65910 ** parameter) of the sqlite3_create_function() and
65911 ** sqlite3_create_function16() routines that originally registered the
65912 ** application defined function.
65913 */
65914 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
65915   assert( p && p->pFunc );
65916   return p->s.db;
65917 }
65918 
65919 /*
65920 ** Return the current time for a statement
65921 */
65922 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
65923   Vdbe *v = p->pVdbe;
65924   int rc;
65925   if( v->iCurrentTime==0 ){
65926     rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime);
65927     if( rc ) v->iCurrentTime = 0;
65928   }
65929   return v->iCurrentTime;
65930 }
65931 
65932 /*
65933 ** The following is the implementation of an SQL function that always
65934 ** fails with an error message stating that the function is used in the
65935 ** wrong context.  The sqlite3_overload_function() API might construct
65936 ** SQL function that use this routine so that the functions will exist
65937 ** for name resolution but are actually overloaded by the xFindFunction
65938 ** method of virtual tables.
65939 */
65940 SQLITE_PRIVATE void sqlite3InvalidFunction(
65941   sqlite3_context *context,  /* The function calling context */
65942   int NotUsed,               /* Number of arguments to the function */
65943   sqlite3_value **NotUsed2   /* Value of each argument */
65944 ){
65945   const char *zName = context->pFunc->zName;
65946   char *zErr;
65947   UNUSED_PARAMETER2(NotUsed, NotUsed2);
65948   zErr = sqlite3_mprintf(
65949       "unable to use function %s in the requested context", zName);
65950   sqlite3_result_error(context, zErr, -1);
65951   sqlite3_free(zErr);
65952 }
65953 
65954 /*
65955 ** Allocate or return the aggregate context for a user function.  A new
65956 ** context is allocated on the first call.  Subsequent calls return the
65957 ** same context that was returned on prior calls.
65958 */
65959 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
65960   Mem *pMem;
65961   assert( p && p->pFunc && p->pFunc->xStep );
65962   assert( sqlite3_mutex_held(p->s.db->mutex) );
65963   pMem = p->pMem;
65964   testcase( nByte<0 );
65965   if( (pMem->flags & MEM_Agg)==0 ){
65966     if( nByte<=0 ){
65967       sqlite3VdbeMemReleaseExternal(pMem);
65968       pMem->flags = MEM_Null;
65969       pMem->z = 0;
65970     }else{
65971       sqlite3VdbeMemGrow(pMem, nByte, 0);
65972       pMem->flags = MEM_Agg;
65973       pMem->u.pDef = p->pFunc;
65974       if( pMem->z ){
65975         memset(pMem->z, 0, nByte);
65976       }
65977     }
65978   }
65979   return (void*)pMem->z;
65980 }
65981 
65982 /*
65983 ** Return the auxilary data pointer, if any, for the iArg'th argument to
65984 ** the user-function defined by pCtx.
65985 */
65986 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
65987   AuxData *pAuxData;
65988 
65989   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65990   for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
65991     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
65992   }
65993 
65994   return (pAuxData ? pAuxData->pAux : 0);
65995 }
65996 
65997 /*
65998 ** Set the auxilary data pointer and delete function, for the iArg'th
65999 ** argument to the user-function defined by pCtx. Any previous value is
66000 ** deleted by calling the delete function specified when it was set.
66001 */
66002 SQLITE_API void sqlite3_set_auxdata(
66003   sqlite3_context *pCtx,
66004   int iArg,
66005   void *pAux,
66006   void (*xDelete)(void*)
66007 ){
66008   AuxData *pAuxData;
66009   Vdbe *pVdbe = pCtx->pVdbe;
66010 
66011   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
66012   if( iArg<0 ) goto failed;
66013 
66014   for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
66015     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
66016   }
66017   if( pAuxData==0 ){
66018     pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
66019     if( !pAuxData ) goto failed;
66020     pAuxData->iOp = pCtx->iOp;
66021     pAuxData->iArg = iArg;
66022     pAuxData->pNext = pVdbe->pAuxData;
66023     pVdbe->pAuxData = pAuxData;
66024     if( pCtx->fErrorOrAux==0 ){
66025       pCtx->isError = 0;
66026       pCtx->fErrorOrAux = 1;
66027     }
66028   }else if( pAuxData->xDelete ){
66029     pAuxData->xDelete(pAuxData->pAux);
66030   }
66031 
66032   pAuxData->pAux = pAux;
66033   pAuxData->xDelete = xDelete;
66034   return;
66035 
66036 failed:
66037   if( xDelete ){
66038     xDelete(pAux);
66039   }
66040 }
66041 
66042 #ifndef SQLITE_OMIT_DEPRECATED
66043 /*
66044 ** Return the number of times the Step function of a aggregate has been
66045 ** called.
66046 **
66047 ** This function is deprecated.  Do not use it for new code.  It is
66048 ** provide only to avoid breaking legacy code.  New aggregate function
66049 ** implementations should keep their own counts within their aggregate
66050 ** context.
66051 */
66052 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
66053   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
66054   return p->pMem->n;
66055 }
66056 #endif
66057 
66058 /*
66059 ** Return the number of columns in the result set for the statement pStmt.
66060 */
66061 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
66062   Vdbe *pVm = (Vdbe *)pStmt;
66063   return pVm ? pVm->nResColumn : 0;
66064 }
66065 
66066 /*
66067 ** Return the number of values available from the current row of the
66068 ** currently executing statement pStmt.
66069 */
66070 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
66071   Vdbe *pVm = (Vdbe *)pStmt;
66072   if( pVm==0 || pVm->pResultSet==0 ) return 0;
66073   return pVm->nResColumn;
66074 }
66075 
66076 /*
66077 ** Return a pointer to static memory containing an SQL NULL value.
66078 */
66079 static const Mem *columnNullValue(void){
66080   /* Even though the Mem structure contains an element
66081   ** of type i64, on certain architectures (x86) with certain compiler
66082   ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
66083   ** instead of an 8-byte one. This all works fine, except that when
66084   ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
66085   ** that a Mem structure is located on an 8-byte boundary. To prevent
66086   ** these assert()s from failing, when building with SQLITE_DEBUG defined
66087   ** using gcc, we force nullMem to be 8-byte aligned using the magical
66088   ** __attribute__((aligned(8))) macro.  */
66089   static const Mem nullMem
66090 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
66091     __attribute__((aligned(8)))
66092 #endif
66093     = {0, "", (double)0, {0}, 0, MEM_Null, 0,
66094 #ifdef SQLITE_DEBUG
66095        0, 0,  /* pScopyFrom, pFiller */
66096 #endif
66097        0, 0 };
66098   return &nullMem;
66099 }
66100 
66101 /*
66102 ** Check to see if column iCol of the given statement is valid.  If
66103 ** it is, return a pointer to the Mem for the value of that column.
66104 ** If iCol is not valid, return a pointer to a Mem which has a value
66105 ** of NULL.
66106 */
66107 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
66108   Vdbe *pVm;
66109   Mem *pOut;
66110 
66111   pVm = (Vdbe *)pStmt;
66112   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
66113     sqlite3_mutex_enter(pVm->db->mutex);
66114     pOut = &pVm->pResultSet[i];
66115   }else{
66116     if( pVm && ALWAYS(pVm->db) ){
66117       sqlite3_mutex_enter(pVm->db->mutex);
66118       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
66119     }
66120     pOut = (Mem*)columnNullValue();
66121   }
66122   return pOut;
66123 }
66124 
66125 /*
66126 ** This function is called after invoking an sqlite3_value_XXX function on a
66127 ** column value (i.e. a value returned by evaluating an SQL expression in the
66128 ** select list of a SELECT statement) that may cause a malloc() failure. If
66129 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
66130 ** code of statement pStmt set to SQLITE_NOMEM.
66131 **
66132 ** Specifically, this is called from within:
66133 **
66134 **     sqlite3_column_int()
66135 **     sqlite3_column_int64()
66136 **     sqlite3_column_text()
66137 **     sqlite3_column_text16()
66138 **     sqlite3_column_real()
66139 **     sqlite3_column_bytes()
66140 **     sqlite3_column_bytes16()
66141 **     sqiite3_column_blob()
66142 */
66143 static void columnMallocFailure(sqlite3_stmt *pStmt)
66144 {
66145   /* If malloc() failed during an encoding conversion within an
66146   ** sqlite3_column_XXX API, then set the return code of the statement to
66147   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
66148   ** and _finalize() will return NOMEM.
66149   */
66150   Vdbe *p = (Vdbe *)pStmt;
66151   if( p ){
66152     p->rc = sqlite3ApiExit(p->db, p->rc);
66153     sqlite3_mutex_leave(p->db->mutex);
66154   }
66155 }
66156 
66157 /**************************** sqlite3_column_  *******************************
66158 ** The following routines are used to access elements of the current row
66159 ** in the result set.
66160 */
66161 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
66162   const void *val;
66163   val = sqlite3_value_blob( columnMem(pStmt,i) );
66164   /* Even though there is no encoding conversion, value_blob() might
66165   ** need to call malloc() to expand the result of a zeroblob()
66166   ** expression.
66167   */
66168   columnMallocFailure(pStmt);
66169   return val;
66170 }
66171 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
66172   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
66173   columnMallocFailure(pStmt);
66174   return val;
66175 }
66176 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
66177   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
66178   columnMallocFailure(pStmt);
66179   return val;
66180 }
66181 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
66182   double val = sqlite3_value_double( columnMem(pStmt,i) );
66183   columnMallocFailure(pStmt);
66184   return val;
66185 }
66186 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
66187   int val = sqlite3_value_int( columnMem(pStmt,i) );
66188   columnMallocFailure(pStmt);
66189   return val;
66190 }
66191 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
66192   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
66193   columnMallocFailure(pStmt);
66194   return val;
66195 }
66196 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
66197   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
66198   columnMallocFailure(pStmt);
66199   return val;
66200 }
66201 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
66202   Mem *pOut = columnMem(pStmt, i);
66203   if( pOut->flags&MEM_Static ){
66204     pOut->flags &= ~MEM_Static;
66205     pOut->flags |= MEM_Ephem;
66206   }
66207   columnMallocFailure(pStmt);
66208   return (sqlite3_value *)pOut;
66209 }
66210 #ifndef SQLITE_OMIT_UTF16
66211 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
66212   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
66213   columnMallocFailure(pStmt);
66214   return val;
66215 }
66216 #endif /* SQLITE_OMIT_UTF16 */
66217 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
66218   int iType = sqlite3_value_type( columnMem(pStmt,i) );
66219   columnMallocFailure(pStmt);
66220   return iType;
66221 }
66222 
66223 /*
66224 ** Convert the N-th element of pStmt->pColName[] into a string using
66225 ** xFunc() then return that string.  If N is out of range, return 0.
66226 **
66227 ** There are up to 5 names for each column.  useType determines which
66228 ** name is returned.  Here are the names:
66229 **
66230 **    0      The column name as it should be displayed for output
66231 **    1      The datatype name for the column
66232 **    2      The name of the database that the column derives from
66233 **    3      The name of the table that the column derives from
66234 **    4      The name of the table column that the result column derives from
66235 **
66236 ** If the result is not a simple column reference (if it is an expression
66237 ** or a constant) then useTypes 2, 3, and 4 return NULL.
66238 */
66239 static const void *columnName(
66240   sqlite3_stmt *pStmt,
66241   int N,
66242   const void *(*xFunc)(Mem*),
66243   int useType
66244 ){
66245   const void *ret = 0;
66246   Vdbe *p = (Vdbe *)pStmt;
66247   int n;
66248   sqlite3 *db = p->db;
66249 
66250   assert( db!=0 );
66251   n = sqlite3_column_count(pStmt);
66252   if( N<n && N>=0 ){
66253     N += useType*n;
66254     sqlite3_mutex_enter(db->mutex);
66255     assert( db->mallocFailed==0 );
66256     ret = xFunc(&p->aColName[N]);
66257      /* A malloc may have failed inside of the xFunc() call. If this
66258     ** is the case, clear the mallocFailed flag and return NULL.
66259     */
66260     if( db->mallocFailed ){
66261       db->mallocFailed = 0;
66262       ret = 0;
66263     }
66264     sqlite3_mutex_leave(db->mutex);
66265   }
66266   return ret;
66267 }
66268 
66269 /*
66270 ** Return the name of the Nth column of the result set returned by SQL
66271 ** statement pStmt.
66272 */
66273 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
66274   return columnName(
66275       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
66276 }
66277 #ifndef SQLITE_OMIT_UTF16
66278 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
66279   return columnName(
66280       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
66281 }
66282 #endif
66283 
66284 /*
66285 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
66286 ** not define OMIT_DECLTYPE.
66287 */
66288 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
66289 # error "Must not define both SQLITE_OMIT_DECLTYPE \
66290          and SQLITE_ENABLE_COLUMN_METADATA"
66291 #endif
66292 
66293 #ifndef SQLITE_OMIT_DECLTYPE
66294 /*
66295 ** Return the column declaration type (if applicable) of the 'i'th column
66296 ** of the result set of SQL statement pStmt.
66297 */
66298 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
66299   return columnName(
66300       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
66301 }
66302 #ifndef SQLITE_OMIT_UTF16
66303 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
66304   return columnName(
66305       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
66306 }
66307 #endif /* SQLITE_OMIT_UTF16 */
66308 #endif /* SQLITE_OMIT_DECLTYPE */
66309 
66310 #ifdef SQLITE_ENABLE_COLUMN_METADATA
66311 /*
66312 ** Return the name of the database from which a result column derives.
66313 ** NULL is returned if the result column is an expression or constant or
66314 ** anything else which is not an unabiguous reference to a database column.
66315 */
66316 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
66317   return columnName(
66318       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
66319 }
66320 #ifndef SQLITE_OMIT_UTF16
66321 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
66322   return columnName(
66323       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
66324 }
66325 #endif /* SQLITE_OMIT_UTF16 */
66326 
66327 /*
66328 ** Return the name of the table from which a result column derives.
66329 ** NULL is returned if the result column is an expression or constant or
66330 ** anything else which is not an unabiguous reference to a database column.
66331 */
66332 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
66333   return columnName(
66334       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
66335 }
66336 #ifndef SQLITE_OMIT_UTF16
66337 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
66338   return columnName(
66339       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
66340 }
66341 #endif /* SQLITE_OMIT_UTF16 */
66342 
66343 /*
66344 ** Return the name of the table column from which a result column derives.
66345 ** NULL is returned if the result column is an expression or constant or
66346 ** anything else which is not an unabiguous reference to a database column.
66347 */
66348 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
66349   return columnName(
66350       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
66351 }
66352 #ifndef SQLITE_OMIT_UTF16
66353 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
66354   return columnName(
66355       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
66356 }
66357 #endif /* SQLITE_OMIT_UTF16 */
66358 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
66359 
66360 
66361 /******************************* sqlite3_bind_  ***************************
66362 **
66363 ** Routines used to attach values to wildcards in a compiled SQL statement.
66364 */
66365 /*
66366 ** Unbind the value bound to variable i in virtual machine p. This is the
66367 ** the same as binding a NULL value to the column. If the "i" parameter is
66368 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
66369 **
66370 ** A successful evaluation of this routine acquires the mutex on p.
66371 ** the mutex is released if any kind of error occurs.
66372 **
66373 ** The error code stored in database p->db is overwritten with the return
66374 ** value in any case.
66375 */
66376 static int vdbeUnbind(Vdbe *p, int i){
66377   Mem *pVar;
66378   if( vdbeSafetyNotNull(p) ){
66379     return SQLITE_MISUSE_BKPT;
66380   }
66381   sqlite3_mutex_enter(p->db->mutex);
66382   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
66383     sqlite3Error(p->db, SQLITE_MISUSE, 0);
66384     sqlite3_mutex_leave(p->db->mutex);
66385     sqlite3_log(SQLITE_MISUSE,
66386         "bind on a busy prepared statement: [%s]", p->zSql);
66387     return SQLITE_MISUSE_BKPT;
66388   }
66389   if( i<1 || i>p->nVar ){
66390     sqlite3Error(p->db, SQLITE_RANGE, 0);
66391     sqlite3_mutex_leave(p->db->mutex);
66392     return SQLITE_RANGE;
66393   }
66394   i--;
66395   pVar = &p->aVar[i];
66396   sqlite3VdbeMemRelease(pVar);
66397   pVar->flags = MEM_Null;
66398   sqlite3Error(p->db, SQLITE_OK, 0);
66399 
66400   /* If the bit corresponding to this variable in Vdbe.expmask is set, then
66401   ** binding a new value to this variable invalidates the current query plan.
66402   **
66403   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
66404   ** parameter in the WHERE clause might influence the choice of query plan
66405   ** for a statement, then the statement will be automatically recompiled,
66406   ** as if there had been a schema change, on the first sqlite3_step() call
66407   ** following any change to the bindings of that parameter.
66408   */
66409   if( p->isPrepareV2 &&
66410      ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
66411   ){
66412     p->expired = 1;
66413   }
66414   return SQLITE_OK;
66415 }
66416 
66417 /*
66418 ** Bind a text or BLOB value.
66419 */
66420 static int bindText(
66421   sqlite3_stmt *pStmt,   /* The statement to bind against */
66422   int i,                 /* Index of the parameter to bind */
66423   const void *zData,     /* Pointer to the data to be bound */
66424   int nData,             /* Number of bytes of data to be bound */
66425   void (*xDel)(void*),   /* Destructor for the data */
66426   u8 encoding            /* Encoding for the data */
66427 ){
66428   Vdbe *p = (Vdbe *)pStmt;
66429   Mem *pVar;
66430   int rc;
66431 
66432   rc = vdbeUnbind(p, i);
66433   if( rc==SQLITE_OK ){
66434     if( zData!=0 ){
66435       pVar = &p->aVar[i-1];
66436       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
66437       if( rc==SQLITE_OK && encoding!=0 ){
66438         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
66439       }
66440       sqlite3Error(p->db, rc, 0);
66441       rc = sqlite3ApiExit(p->db, rc);
66442     }
66443     sqlite3_mutex_leave(p->db->mutex);
66444   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
66445     xDel((void*)zData);
66446   }
66447   return rc;
66448 }
66449 
66450 
66451 /*
66452 ** Bind a blob value to an SQL statement variable.
66453 */
66454 SQLITE_API int sqlite3_bind_blob(
66455   sqlite3_stmt *pStmt,
66456   int i,
66457   const void *zData,
66458   int nData,
66459   void (*xDel)(void*)
66460 ){
66461   return bindText(pStmt, i, zData, nData, xDel, 0);
66462 }
66463 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
66464   int rc;
66465   Vdbe *p = (Vdbe *)pStmt;
66466   rc = vdbeUnbind(p, i);
66467   if( rc==SQLITE_OK ){
66468     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
66469     sqlite3_mutex_leave(p->db->mutex);
66470   }
66471   return rc;
66472 }
66473 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
66474   return sqlite3_bind_int64(p, i, (i64)iValue);
66475 }
66476 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
66477   int rc;
66478   Vdbe *p = (Vdbe *)pStmt;
66479   rc = vdbeUnbind(p, i);
66480   if( rc==SQLITE_OK ){
66481     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
66482     sqlite3_mutex_leave(p->db->mutex);
66483   }
66484   return rc;
66485 }
66486 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
66487   int rc;
66488   Vdbe *p = (Vdbe*)pStmt;
66489   rc = vdbeUnbind(p, i);
66490   if( rc==SQLITE_OK ){
66491     sqlite3_mutex_leave(p->db->mutex);
66492   }
66493   return rc;
66494 }
66495 SQLITE_API int sqlite3_bind_text(
66496   sqlite3_stmt *pStmt,
66497   int i,
66498   const char *zData,
66499   int nData,
66500   void (*xDel)(void*)
66501 ){
66502   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
66503 }
66504 #ifndef SQLITE_OMIT_UTF16
66505 SQLITE_API int sqlite3_bind_text16(
66506   sqlite3_stmt *pStmt,
66507   int i,
66508   const void *zData,
66509   int nData,
66510   void (*xDel)(void*)
66511 ){
66512   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
66513 }
66514 #endif /* SQLITE_OMIT_UTF16 */
66515 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
66516   int rc;
66517   switch( sqlite3_value_type((sqlite3_value*)pValue) ){
66518     case SQLITE_INTEGER: {
66519       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
66520       break;
66521     }
66522     case SQLITE_FLOAT: {
66523       rc = sqlite3_bind_double(pStmt, i, pValue->r);
66524       break;
66525     }
66526     case SQLITE_BLOB: {
66527       if( pValue->flags & MEM_Zero ){
66528         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
66529       }else{
66530         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
66531       }
66532       break;
66533     }
66534     case SQLITE_TEXT: {
66535       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
66536                               pValue->enc);
66537       break;
66538     }
66539     default: {
66540       rc = sqlite3_bind_null(pStmt, i);
66541       break;
66542     }
66543   }
66544   return rc;
66545 }
66546 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
66547   int rc;
66548   Vdbe *p = (Vdbe *)pStmt;
66549   rc = vdbeUnbind(p, i);
66550   if( rc==SQLITE_OK ){
66551     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
66552     sqlite3_mutex_leave(p->db->mutex);
66553   }
66554   return rc;
66555 }
66556 
66557 /*
66558 ** Return the number of wildcards that can be potentially bound to.
66559 ** This routine is added to support DBD::SQLite.
66560 */
66561 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
66562   Vdbe *p = (Vdbe*)pStmt;
66563   return p ? p->nVar : 0;
66564 }
66565 
66566 /*
66567 ** Return the name of a wildcard parameter.  Return NULL if the index
66568 ** is out of range or if the wildcard is unnamed.
66569 **
66570 ** The result is always UTF-8.
66571 */
66572 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
66573   Vdbe *p = (Vdbe*)pStmt;
66574   if( p==0 || i<1 || i>p->nzVar ){
66575     return 0;
66576   }
66577   return p->azVar[i-1];
66578 }
66579 
66580 /*
66581 ** Given a wildcard parameter name, return the index of the variable
66582 ** with that name.  If there is no variable with the given name,
66583 ** return 0.
66584 */
66585 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
66586   int i;
66587   if( p==0 ){
66588     return 0;
66589   }
66590   if( zName ){
66591     for(i=0; i<p->nzVar; i++){
66592       const char *z = p->azVar[i];
66593       if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
66594         return i+1;
66595       }
66596     }
66597   }
66598   return 0;
66599 }
66600 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
66601   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
66602 }
66603 
66604 /*
66605 ** Transfer all bindings from the first statement over to the second.
66606 */
66607 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
66608   Vdbe *pFrom = (Vdbe*)pFromStmt;
66609   Vdbe *pTo = (Vdbe*)pToStmt;
66610   int i;
66611   assert( pTo->db==pFrom->db );
66612   assert( pTo->nVar==pFrom->nVar );
66613   sqlite3_mutex_enter(pTo->db->mutex);
66614   for(i=0; i<pFrom->nVar; i++){
66615     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
66616   }
66617   sqlite3_mutex_leave(pTo->db->mutex);
66618   return SQLITE_OK;
66619 }
66620 
66621 #ifndef SQLITE_OMIT_DEPRECATED
66622 /*
66623 ** Deprecated external interface.  Internal/core SQLite code
66624 ** should call sqlite3TransferBindings.
66625 **
66626 ** Is is misuse to call this routine with statements from different
66627 ** database connections.  But as this is a deprecated interface, we
66628 ** will not bother to check for that condition.
66629 **
66630 ** If the two statements contain a different number of bindings, then
66631 ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
66632 ** SQLITE_OK is returned.
66633 */
66634 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
66635   Vdbe *pFrom = (Vdbe*)pFromStmt;
66636   Vdbe *pTo = (Vdbe*)pToStmt;
66637   if( pFrom->nVar!=pTo->nVar ){
66638     return SQLITE_ERROR;
66639   }
66640   if( pTo->isPrepareV2 && pTo->expmask ){
66641     pTo->expired = 1;
66642   }
66643   if( pFrom->isPrepareV2 && pFrom->expmask ){
66644     pFrom->expired = 1;
66645   }
66646   return sqlite3TransferBindings(pFromStmt, pToStmt);
66647 }
66648 #endif
66649 
66650 /*
66651 ** Return the sqlite3* database handle to which the prepared statement given
66652 ** in the argument belongs.  This is the same database handle that was
66653 ** the first argument to the sqlite3_prepare() that was used to create
66654 ** the statement in the first place.
66655 */
66656 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
66657   return pStmt ? ((Vdbe*)pStmt)->db : 0;
66658 }
66659 
66660 /*
66661 ** Return true if the prepared statement is guaranteed to not modify the
66662 ** database.
66663 */
66664 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
66665   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
66666 }
66667 
66668 /*
66669 ** Return true if the prepared statement is in need of being reset.
66670 */
66671 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
66672   Vdbe *v = (Vdbe*)pStmt;
66673   return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
66674 }
66675 
66676 /*
66677 ** Return a pointer to the next prepared statement after pStmt associated
66678 ** with database connection pDb.  If pStmt is NULL, return the first
66679 ** prepared statement for the database connection.  Return NULL if there
66680 ** are no more.
66681 */
66682 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
66683   sqlite3_stmt *pNext;
66684   sqlite3_mutex_enter(pDb->mutex);
66685   if( pStmt==0 ){
66686     pNext = (sqlite3_stmt*)pDb->pVdbe;
66687   }else{
66688     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
66689   }
66690   sqlite3_mutex_leave(pDb->mutex);
66691   return pNext;
66692 }
66693 
66694 /*
66695 ** Return the value of a status counter for a prepared statement
66696 */
66697 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
66698   Vdbe *pVdbe = (Vdbe*)pStmt;
66699   u32 v = pVdbe->aCounter[op];
66700   if( resetFlag ) pVdbe->aCounter[op] = 0;
66701   return (int)v;
66702 }
66703 
66704 /************** End of vdbeapi.c *********************************************/
66705 /************** Begin file vdbetrace.c ***************************************/
66706 /*
66707 ** 2009 November 25
66708 **
66709 ** The author disclaims copyright to this source code.  In place of
66710 ** a legal notice, here is a blessing:
66711 **
66712 **    May you do good and not evil.
66713 **    May you find forgiveness for yourself and forgive others.
66714 **    May you share freely, never taking more than you give.
66715 **
66716 *************************************************************************
66717 **
66718 ** This file contains code used to insert the values of host parameters
66719 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
66720 **
66721 ** The Vdbe parse-tree explainer is also found here.
66722 */
66723 
66724 #ifndef SQLITE_OMIT_TRACE
66725 
66726 /*
66727 ** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
66728 ** bytes in this text up to but excluding the first character in
66729 ** a host parameter.  If the text contains no host parameters, return
66730 ** the total number of bytes in the text.
66731 */
66732 static int findNextHostParameter(const char *zSql, int *pnToken){
66733   int tokenType;
66734   int nTotal = 0;
66735   int n;
66736 
66737   *pnToken = 0;
66738   while( zSql[0] ){
66739     n = sqlite3GetToken((u8*)zSql, &tokenType);
66740     assert( n>0 && tokenType!=TK_ILLEGAL );
66741     if( tokenType==TK_VARIABLE ){
66742       *pnToken = n;
66743       break;
66744     }
66745     nTotal += n;
66746     zSql += n;
66747   }
66748   return nTotal;
66749 }
66750 
66751 /*
66752 ** This function returns a pointer to a nul-terminated string in memory
66753 ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
66754 ** string contains a copy of zRawSql but with host parameters expanded to
66755 ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
66756 ** then the returned string holds a copy of zRawSql with "-- " prepended
66757 ** to each line of text.
66758 **
66759 ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
66760 ** then long strings and blobs are truncated to that many bytes.  This
66761 ** can be used to prevent unreasonably large trace strings when dealing
66762 ** with large (multi-megabyte) strings and blobs.
66763 **
66764 ** The calling function is responsible for making sure the memory returned
66765 ** is eventually freed.
66766 **
66767 ** ALGORITHM:  Scan the input string looking for host parameters in any of
66768 ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
66769 ** string literals, quoted identifier names, and comments.  For text forms,
66770 ** the host parameter index is found by scanning the perpared
66771 ** statement for the corresponding OP_Variable opcode.  Once the host
66772 ** parameter index is known, locate the value in p->aVar[].  Then render
66773 ** the value as a literal in place of the host parameter name.
66774 */
66775 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
66776   Vdbe *p,                 /* The prepared statement being evaluated */
66777   const char *zRawSql      /* Raw text of the SQL statement */
66778 ){
66779   sqlite3 *db;             /* The database connection */
66780   int idx = 0;             /* Index of a host parameter */
66781   int nextIndex = 1;       /* Index of next ? host parameter */
66782   int n;                   /* Length of a token prefix */
66783   int nToken;              /* Length of the parameter token */
66784   int i;                   /* Loop counter */
66785   Mem *pVar;               /* Value of a host parameter */
66786   StrAccum out;            /* Accumulate the output here */
66787   char zBase[100];         /* Initial working space */
66788 
66789   db = p->db;
66790   sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
66791                       db->aLimit[SQLITE_LIMIT_LENGTH]);
66792   out.db = db;
66793   if( db->nVdbeExec>1 ){
66794     while( *zRawSql ){
66795       const char *zStart = zRawSql;
66796       while( *(zRawSql++)!='\n' && *zRawSql );
66797       sqlite3StrAccumAppend(&out, "-- ", 3);
66798       assert( (zRawSql - zStart) > 0 );
66799       sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
66800     }
66801   }else{
66802     while( zRawSql[0] ){
66803       n = findNextHostParameter(zRawSql, &nToken);
66804       assert( n>0 );
66805       sqlite3StrAccumAppend(&out, zRawSql, n);
66806       zRawSql += n;
66807       assert( zRawSql[0] || nToken==0 );
66808       if( nToken==0 ) break;
66809       if( zRawSql[0]=='?' ){
66810         if( nToken>1 ){
66811           assert( sqlite3Isdigit(zRawSql[1]) );
66812           sqlite3GetInt32(&zRawSql[1], &idx);
66813         }else{
66814           idx = nextIndex;
66815         }
66816       }else{
66817         assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
66818         testcase( zRawSql[0]==':' );
66819         testcase( zRawSql[0]=='$' );
66820         testcase( zRawSql[0]=='@' );
66821         idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
66822         assert( idx>0 );
66823       }
66824       zRawSql += nToken;
66825       nextIndex = idx + 1;
66826       assert( idx>0 && idx<=p->nVar );
66827       pVar = &p->aVar[idx-1];
66828       if( pVar->flags & MEM_Null ){
66829         sqlite3StrAccumAppend(&out, "NULL", 4);
66830       }else if( pVar->flags & MEM_Int ){
66831         sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
66832       }else if( pVar->flags & MEM_Real ){
66833         sqlite3XPrintf(&out, 0, "%!.15g", pVar->r);
66834       }else if( pVar->flags & MEM_Str ){
66835         int nOut;  /* Number of bytes of the string text to include in output */
66836 #ifndef SQLITE_OMIT_UTF16
66837         u8 enc = ENC(db);
66838         Mem utf8;
66839         if( enc!=SQLITE_UTF8 ){
66840           memset(&utf8, 0, sizeof(utf8));
66841           utf8.db = db;
66842           sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
66843           sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
66844           pVar = &utf8;
66845         }
66846 #endif
66847         nOut = pVar->n;
66848 #ifdef SQLITE_TRACE_SIZE_LIMIT
66849         if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
66850           nOut = SQLITE_TRACE_SIZE_LIMIT;
66851           while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
66852         }
66853 #endif
66854         sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
66855 #ifdef SQLITE_TRACE_SIZE_LIMIT
66856         if( nOut<pVar->n ){
66857           sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
66858         }
66859 #endif
66860 #ifndef SQLITE_OMIT_UTF16
66861         if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
66862 #endif
66863       }else if( pVar->flags & MEM_Zero ){
66864         sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
66865       }else{
66866         int nOut;  /* Number of bytes of the blob to include in output */
66867         assert( pVar->flags & MEM_Blob );
66868         sqlite3StrAccumAppend(&out, "x'", 2);
66869         nOut = pVar->n;
66870 #ifdef SQLITE_TRACE_SIZE_LIMIT
66871         if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
66872 #endif
66873         for(i=0; i<nOut; i++){
66874           sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
66875         }
66876         sqlite3StrAccumAppend(&out, "'", 1);
66877 #ifdef SQLITE_TRACE_SIZE_LIMIT
66878         if( nOut<pVar->n ){
66879           sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
66880         }
66881 #endif
66882       }
66883     }
66884   }
66885   return sqlite3StrAccumFinish(&out);
66886 }
66887 
66888 #endif /* #ifndef SQLITE_OMIT_TRACE */
66889 
66890 /*****************************************************************************
66891 ** The following code implements the data-structure explaining logic
66892 ** for the Vdbe.
66893 */
66894 
66895 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
66896 
66897 /*
66898 ** Allocate a new Explain object
66899 */
66900 SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
66901   if( pVdbe ){
66902     Explain *p;
66903     sqlite3BeginBenignMalloc();
66904     p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
66905     if( p ){
66906       p->pVdbe = pVdbe;
66907       sqlite3_free(pVdbe->pExplain);
66908       pVdbe->pExplain = p;
66909       sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
66910                           SQLITE_MAX_LENGTH);
66911       p->str.useMalloc = 2;
66912     }else{
66913       sqlite3EndBenignMalloc();
66914     }
66915   }
66916 }
66917 
66918 /*
66919 ** Return true if the Explain ends with a new-line.
66920 */
66921 static int endsWithNL(Explain *p){
66922   return p && p->str.zText && p->str.nChar
66923            && p->str.zText[p->str.nChar-1]=='\n';
66924 }
66925 
66926 /*
66927 ** Append text to the indentation
66928 */
66929 SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
66930   Explain *p;
66931   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
66932     va_list ap;
66933     if( p->nIndent && endsWithNL(p) ){
66934       int n = p->nIndent;
66935       if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
66936       sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
66937     }
66938     va_start(ap, zFormat);
66939     sqlite3VXPrintf(&p->str, SQLITE_PRINTF_INTERNAL, zFormat, ap);
66940     va_end(ap);
66941   }
66942 }
66943 
66944 /*
66945 ** Append a '\n' if there is not already one.
66946 */
66947 SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
66948   Explain *p;
66949   if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
66950     sqlite3StrAccumAppend(&p->str, "\n", 1);
66951   }
66952 }
66953 
66954 /*
66955 ** Push a new indentation level.  Subsequent lines will be indented
66956 ** so that they begin at the current cursor position.
66957 */
66958 SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
66959   Explain *p;
66960   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
66961     if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
66962       const char *z = p->str.zText;
66963       int i = p->str.nChar-1;
66964       int x;
66965       while( i>=0 && z[i]!='\n' ){ i--; }
66966       x = (p->str.nChar - 1) - i;
66967       if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
66968         x = p->aIndent[p->nIndent-1];
66969       }
66970       p->aIndent[p->nIndent] = x;
66971     }
66972     p->nIndent++;
66973   }
66974 }
66975 
66976 /*
66977 ** Pop the indentation stack by one level.
66978 */
66979 SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
66980   if( p && p->pExplain ) p->pExplain->nIndent--;
66981 }
66982 
66983 /*
66984 ** Free the indentation structure
66985 */
66986 SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
66987   if( pVdbe && pVdbe->pExplain ){
66988     sqlite3_free(pVdbe->zExplain);
66989     sqlite3ExplainNL(pVdbe);
66990     pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
66991     sqlite3_free(pVdbe->pExplain);
66992     pVdbe->pExplain = 0;
66993     sqlite3EndBenignMalloc();
66994   }
66995 }
66996 
66997 /*
66998 ** Return the explanation of a virtual machine.
66999 */
67000 SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
67001   return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
67002 }
67003 #endif /* defined(SQLITE_DEBUG) */
67004 
67005 /************** End of vdbetrace.c *******************************************/
67006 /************** Begin file vdbe.c ********************************************/
67007 /*
67008 ** 2001 September 15
67009 **
67010 ** The author disclaims copyright to this source code.  In place of
67011 ** a legal notice, here is a blessing:
67012 **
67013 **    May you do good and not evil.
67014 **    May you find forgiveness for yourself and forgive others.
67015 **    May you share freely, never taking more than you give.
67016 **
67017 *************************************************************************
67018 ** The code in this file implements the function that runs the
67019 ** bytecode of a prepared statement.
67020 **
67021 ** Various scripts scan this source file in order to generate HTML
67022 ** documentation, headers files, or other derived files.  The formatting
67023 ** of the code in this file is, therefore, important.  See other comments
67024 ** in this file for details.  If in doubt, do not deviate from existing
67025 ** commenting and indentation practices when changing or adding code.
67026 */
67027 
67028 /*
67029 ** Invoke this macro on memory cells just prior to changing the
67030 ** value of the cell.  This macro verifies that shallow copies are
67031 ** not misused.  A shallow copy of a string or blob just copies a
67032 ** pointer to the string or blob, not the content.  If the original
67033 ** is changed while the copy is still in use, the string or blob might
67034 ** be changed out from under the copy.  This macro verifies that nothing
67035 ** like that ever happens.
67036 */
67037 #ifdef SQLITE_DEBUG
67038 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
67039 #else
67040 # define memAboutToChange(P,M)
67041 #endif
67042 
67043 /*
67044 ** The following global variable is incremented every time a cursor
67045 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
67046 ** procedures use this information to make sure that indices are
67047 ** working correctly.  This variable has no function other than to
67048 ** help verify the correct operation of the library.
67049 */
67050 #ifdef SQLITE_TEST
67051 SQLITE_API int sqlite3_search_count = 0;
67052 #endif
67053 
67054 /*
67055 ** When this global variable is positive, it gets decremented once before
67056 ** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
67057 ** field of the sqlite3 structure is set in order to simulate an interrupt.
67058 **
67059 ** This facility is used for testing purposes only.  It does not function
67060 ** in an ordinary build.
67061 */
67062 #ifdef SQLITE_TEST
67063 SQLITE_API int sqlite3_interrupt_count = 0;
67064 #endif
67065 
67066 /*
67067 ** The next global variable is incremented each type the OP_Sort opcode
67068 ** is executed.  The test procedures use this information to make sure that
67069 ** sorting is occurring or not occurring at appropriate times.   This variable
67070 ** has no function other than to help verify the correct operation of the
67071 ** library.
67072 */
67073 #ifdef SQLITE_TEST
67074 SQLITE_API int sqlite3_sort_count = 0;
67075 #endif
67076 
67077 /*
67078 ** The next global variable records the size of the largest MEM_Blob
67079 ** or MEM_Str that has been used by a VDBE opcode.  The test procedures
67080 ** use this information to make sure that the zero-blob functionality
67081 ** is working correctly.   This variable has no function other than to
67082 ** help verify the correct operation of the library.
67083 */
67084 #ifdef SQLITE_TEST
67085 SQLITE_API int sqlite3_max_blobsize = 0;
67086 static void updateMaxBlobsize(Mem *p){
67087   if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
67088     sqlite3_max_blobsize = p->n;
67089   }
67090 }
67091 #endif
67092 
67093 /*
67094 ** The next global variable is incremented each time the OP_Found opcode
67095 ** is executed. This is used to test whether or not the foreign key
67096 ** operation implemented using OP_FkIsZero is working. This variable
67097 ** has no function other than to help verify the correct operation of the
67098 ** library.
67099 */
67100 #ifdef SQLITE_TEST
67101 SQLITE_API int sqlite3_found_count = 0;
67102 #endif
67103 
67104 /*
67105 ** Test a register to see if it exceeds the current maximum blob size.
67106 ** If it does, record the new maximum blob size.
67107 */
67108 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
67109 # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
67110 #else
67111 # define UPDATE_MAX_BLOBSIZE(P)
67112 #endif
67113 
67114 /*
67115 ** Invoke the VDBE coverage callback, if that callback is defined.  This
67116 ** feature is used for test suite validation only and does not appear an
67117 ** production builds.
67118 **
67119 ** M is an integer, 2 or 3, that indices how many different ways the
67120 ** branch can go.  It is usually 2.  "I" is the direction the branch
67121 ** goes.  0 means falls through.  1 means branch is taken.  2 means the
67122 ** second alternative branch is taken.
67123 */
67124 #if !defined(SQLITE_VDBE_COVERAGE)
67125 # define VdbeBranchTaken(I,M)
67126 #else
67127 # define VdbeBranchTaken(I,M) vdbeTakeBranch(pOp->iSrcLine,I,M)
67128   static void vdbeTakeBranch(int iSrcLine, u8 I, u8 M){
67129     if( iSrcLine<=2 && ALWAYS(iSrcLine>0) ){
67130       M = iSrcLine;
67131       /* Assert the truth of VdbeCoverageAlwaysTaken() and
67132       ** VdbeCoverageNeverTaken() */
67133       assert( (M & I)==I );
67134     }else{
67135       if( sqlite3GlobalConfig.xVdbeBranch==0 ) return;  /*NO_TEST*/
67136       sqlite3GlobalConfig.xVdbeBranch(sqlite3GlobalConfig.pVdbeBranchArg,
67137                                       iSrcLine,I,M);
67138     }
67139   }
67140 #endif
67141 
67142 /*
67143 ** Convert the given register into a string if it isn't one
67144 ** already. Return non-zero if a malloc() fails.
67145 */
67146 #define Stringify(P, enc) \
67147    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
67148      { goto no_mem; }
67149 
67150 /*
67151 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
67152 ** a pointer to a dynamically allocated string where some other entity
67153 ** is responsible for deallocating that string.  Because the register
67154 ** does not control the string, it might be deleted without the register
67155 ** knowing it.
67156 **
67157 ** This routine converts an ephemeral string into a dynamically allocated
67158 ** string that the register itself controls.  In other words, it
67159 ** converts an MEM_Ephem string into a string with P.z==P.zMalloc.
67160 */
67161 #define Deephemeralize(P) \
67162    if( ((P)->flags&MEM_Ephem)!=0 \
67163        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
67164 
67165 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
67166 #define isSorter(x) ((x)->pSorter!=0)
67167 
67168 /*
67169 ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
67170 ** if we run out of memory.
67171 */
67172 static VdbeCursor *allocateCursor(
67173   Vdbe *p,              /* The virtual machine */
67174   int iCur,             /* Index of the new VdbeCursor */
67175   int nField,           /* Number of fields in the table or index */
67176   int iDb,              /* Database the cursor belongs to, or -1 */
67177   int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
67178 ){
67179   /* Find the memory cell that will be used to store the blob of memory
67180   ** required for this VdbeCursor structure. It is convenient to use a
67181   ** vdbe memory cell to manage the memory allocation required for a
67182   ** VdbeCursor structure for the following reasons:
67183   **
67184   **   * Sometimes cursor numbers are used for a couple of different
67185   **     purposes in a vdbe program. The different uses might require
67186   **     different sized allocations. Memory cells provide growable
67187   **     allocations.
67188   **
67189   **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
67190   **     be freed lazily via the sqlite3_release_memory() API. This
67191   **     minimizes the number of malloc calls made by the system.
67192   **
67193   ** Memory cells for cursors are allocated at the top of the address
67194   ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
67195   ** cursor 1 is managed by memory cell (p->nMem-1), etc.
67196   */
67197   Mem *pMem = &p->aMem[p->nMem-iCur];
67198 
67199   int nByte;
67200   VdbeCursor *pCx = 0;
67201   nByte =
67202       ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
67203       (isBtreeCursor?sqlite3BtreeCursorSize():0);
67204 
67205   assert( iCur<p->nCursor );
67206   if( p->apCsr[iCur] ){
67207     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
67208     p->apCsr[iCur] = 0;
67209   }
67210   if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
67211     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
67212     memset(pCx, 0, sizeof(VdbeCursor));
67213     pCx->iDb = iDb;
67214     pCx->nField = nField;
67215     if( isBtreeCursor ){
67216       pCx->pCursor = (BtCursor*)
67217           &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
67218       sqlite3BtreeCursorZero(pCx->pCursor);
67219     }
67220   }
67221   return pCx;
67222 }
67223 
67224 /*
67225 ** Try to convert a value into a numeric representation if we can
67226 ** do so without loss of information.  In other words, if the string
67227 ** looks like a number, convert it into a number.  If it does not
67228 ** look like a number, leave it alone.
67229 */
67230 static void applyNumericAffinity(Mem *pRec){
67231   if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
67232     double rValue;
67233     i64 iValue;
67234     u8 enc = pRec->enc;
67235     if( (pRec->flags&MEM_Str)==0 ) return;
67236     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
67237     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
67238       pRec->u.i = iValue;
67239       pRec->flags |= MEM_Int;
67240     }else{
67241       pRec->r = rValue;
67242       pRec->flags |= MEM_Real;
67243     }
67244   }
67245 }
67246 
67247 /*
67248 ** Processing is determine by the affinity parameter:
67249 **
67250 ** SQLITE_AFF_INTEGER:
67251 ** SQLITE_AFF_REAL:
67252 ** SQLITE_AFF_NUMERIC:
67253 **    Try to convert pRec to an integer representation or a
67254 **    floating-point representation if an integer representation
67255 **    is not possible.  Note that the integer representation is
67256 **    always preferred, even if the affinity is REAL, because
67257 **    an integer representation is more space efficient on disk.
67258 **
67259 ** SQLITE_AFF_TEXT:
67260 **    Convert pRec to a text representation.
67261 **
67262 ** SQLITE_AFF_NONE:
67263 **    No-op.  pRec is unchanged.
67264 */
67265 static void applyAffinity(
67266   Mem *pRec,          /* The value to apply affinity to */
67267   char affinity,      /* The affinity to be applied */
67268   u8 enc              /* Use this text encoding */
67269 ){
67270   if( affinity==SQLITE_AFF_TEXT ){
67271     /* Only attempt the conversion to TEXT if there is an integer or real
67272     ** representation (blob and NULL do not get converted) but no string
67273     ** representation.
67274     */
67275     if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
67276       sqlite3VdbeMemStringify(pRec, enc);
67277     }
67278     pRec->flags &= ~(MEM_Real|MEM_Int);
67279   }else if( affinity!=SQLITE_AFF_NONE ){
67280     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
67281              || affinity==SQLITE_AFF_NUMERIC );
67282     applyNumericAffinity(pRec);
67283     if( pRec->flags & MEM_Real ){
67284       sqlite3VdbeIntegerAffinity(pRec);
67285     }
67286   }
67287 }
67288 
67289 /*
67290 ** Try to convert the type of a function argument or a result column
67291 ** into a numeric representation.  Use either INTEGER or REAL whichever
67292 ** is appropriate.  But only do the conversion if it is possible without
67293 ** loss of information and return the revised type of the argument.
67294 */
67295 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
67296   int eType = sqlite3_value_type(pVal);
67297   if( eType==SQLITE_TEXT ){
67298     Mem *pMem = (Mem*)pVal;
67299     applyNumericAffinity(pMem);
67300     eType = sqlite3_value_type(pVal);
67301   }
67302   return eType;
67303 }
67304 
67305 /*
67306 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
67307 ** not the internal Mem* type.
67308 */
67309 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
67310   sqlite3_value *pVal,
67311   u8 affinity,
67312   u8 enc
67313 ){
67314   applyAffinity((Mem *)pVal, affinity, enc);
67315 }
67316 
67317 #ifdef SQLITE_DEBUG
67318 /*
67319 ** Write a nice string representation of the contents of cell pMem
67320 ** into buffer zBuf, length nBuf.
67321 */
67322 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
67323   char *zCsr = zBuf;
67324   int f = pMem->flags;
67325 
67326   static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
67327 
67328   if( f&MEM_Blob ){
67329     int i;
67330     char c;
67331     if( f & MEM_Dyn ){
67332       c = 'z';
67333       assert( (f & (MEM_Static|MEM_Ephem))==0 );
67334     }else if( f & MEM_Static ){
67335       c = 't';
67336       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
67337     }else if( f & MEM_Ephem ){
67338       c = 'e';
67339       assert( (f & (MEM_Static|MEM_Dyn))==0 );
67340     }else{
67341       c = 's';
67342     }
67343 
67344     sqlite3_snprintf(100, zCsr, "%c", c);
67345     zCsr += sqlite3Strlen30(zCsr);
67346     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
67347     zCsr += sqlite3Strlen30(zCsr);
67348     for(i=0; i<16 && i<pMem->n; i++){
67349       sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
67350       zCsr += sqlite3Strlen30(zCsr);
67351     }
67352     for(i=0; i<16 && i<pMem->n; i++){
67353       char z = pMem->z[i];
67354       if( z<32 || z>126 ) *zCsr++ = '.';
67355       else *zCsr++ = z;
67356     }
67357 
67358     sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
67359     zCsr += sqlite3Strlen30(zCsr);
67360     if( f & MEM_Zero ){
67361       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
67362       zCsr += sqlite3Strlen30(zCsr);
67363     }
67364     *zCsr = '\0';
67365   }else if( f & MEM_Str ){
67366     int j, k;
67367     zBuf[0] = ' ';
67368     if( f & MEM_Dyn ){
67369       zBuf[1] = 'z';
67370       assert( (f & (MEM_Static|MEM_Ephem))==0 );
67371     }else if( f & MEM_Static ){
67372       zBuf[1] = 't';
67373       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
67374     }else if( f & MEM_Ephem ){
67375       zBuf[1] = 'e';
67376       assert( (f & (MEM_Static|MEM_Dyn))==0 );
67377     }else{
67378       zBuf[1] = 's';
67379     }
67380     k = 2;
67381     sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
67382     k += sqlite3Strlen30(&zBuf[k]);
67383     zBuf[k++] = '[';
67384     for(j=0; j<15 && j<pMem->n; j++){
67385       u8 c = pMem->z[j];
67386       if( c>=0x20 && c<0x7f ){
67387         zBuf[k++] = c;
67388       }else{
67389         zBuf[k++] = '.';
67390       }
67391     }
67392     zBuf[k++] = ']';
67393     sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
67394     k += sqlite3Strlen30(&zBuf[k]);
67395     zBuf[k++] = 0;
67396   }
67397 }
67398 #endif
67399 
67400 #ifdef SQLITE_DEBUG
67401 /*
67402 ** Print the value of a register for tracing purposes:
67403 */
67404 static void memTracePrint(Mem *p){
67405   if( p->flags & MEM_Undefined ){
67406     printf(" undefined");
67407   }else if( p->flags & MEM_Null ){
67408     printf(" NULL");
67409   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
67410     printf(" si:%lld", p->u.i);
67411   }else if( p->flags & MEM_Int ){
67412     printf(" i:%lld", p->u.i);
67413 #ifndef SQLITE_OMIT_FLOATING_POINT
67414   }else if( p->flags & MEM_Real ){
67415     printf(" r:%g", p->r);
67416 #endif
67417   }else if( p->flags & MEM_RowSet ){
67418     printf(" (rowset)");
67419   }else{
67420     char zBuf[200];
67421     sqlite3VdbeMemPrettyPrint(p, zBuf);
67422     printf(" %s", zBuf);
67423   }
67424 }
67425 static void registerTrace(int iReg, Mem *p){
67426   printf("REG[%d] = ", iReg);
67427   memTracePrint(p);
67428   printf("\n");
67429 }
67430 #endif
67431 
67432 #ifdef SQLITE_DEBUG
67433 #  define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
67434 #else
67435 #  define REGISTER_TRACE(R,M)
67436 #endif
67437 
67438 
67439 #ifdef VDBE_PROFILE
67440 
67441 /*
67442 ** hwtime.h contains inline assembler code for implementing
67443 ** high-performance timing routines.
67444 */
67445 /************** Include hwtime.h in the middle of vdbe.c *********************/
67446 /************** Begin file hwtime.h ******************************************/
67447 /*
67448 ** 2008 May 27
67449 **
67450 ** The author disclaims copyright to this source code.  In place of
67451 ** a legal notice, here is a blessing:
67452 **
67453 **    May you do good and not evil.
67454 **    May you find forgiveness for yourself and forgive others.
67455 **    May you share freely, never taking more than you give.
67456 **
67457 ******************************************************************************
67458 **
67459 ** This file contains inline asm code for retrieving "high-performance"
67460 ** counters for x86 class CPUs.
67461 */
67462 #ifndef _HWTIME_H_
67463 #define _HWTIME_H_
67464 
67465 /*
67466 ** The following routine only works on pentium-class (or newer) processors.
67467 ** It uses the RDTSC opcode to read the cycle count value out of the
67468 ** processor and returns that value.  This can be used for high-res
67469 ** profiling.
67470 */
67471 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
67472       (defined(i386) || defined(__i386__) || defined(_M_IX86))
67473 
67474   #if defined(__GNUC__)
67475 
67476   __inline__ sqlite_uint64 sqlite3Hwtime(void){
67477      unsigned int lo, hi;
67478      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
67479      return (sqlite_uint64)hi << 32 | lo;
67480   }
67481 
67482   #elif defined(_MSC_VER)
67483 
67484   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
67485      __asm {
67486         rdtsc
67487         ret       ; return value at EDX:EAX
67488      }
67489   }
67490 
67491   #endif
67492 
67493 #elif (defined(__GNUC__) && defined(__x86_64__))
67494 
67495   __inline__ sqlite_uint64 sqlite3Hwtime(void){
67496       unsigned long val;
67497       __asm__ __volatile__ ("rdtsc" : "=A" (val));
67498       return val;
67499   }
67500 
67501 #elif (defined(__GNUC__) && defined(__ppc__))
67502 
67503   __inline__ sqlite_uint64 sqlite3Hwtime(void){
67504       unsigned long long retval;
67505       unsigned long junk;
67506       __asm__ __volatile__ ("\n\
67507           1:      mftbu   %1\n\
67508                   mftb    %L0\n\
67509                   mftbu   %0\n\
67510                   cmpw    %0,%1\n\
67511                   bne     1b"
67512                   : "=r" (retval), "=r" (junk));
67513       return retval;
67514   }
67515 
67516 #else
67517 
67518   #error Need implementation of sqlite3Hwtime() for your platform.
67519 
67520   /*
67521   ** To compile without implementing sqlite3Hwtime() for your platform,
67522   ** you can remove the above #error and use the following
67523   ** stub function.  You will lose timing support for many
67524   ** of the debugging and testing utilities, but it should at
67525   ** least compile and run.
67526   */
67527 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
67528 
67529 #endif
67530 
67531 #endif /* !defined(_HWTIME_H_) */
67532 
67533 /************** End of hwtime.h **********************************************/
67534 /************** Continuing where we left off in vdbe.c ***********************/
67535 
67536 #endif
67537 
67538 #ifndef NDEBUG
67539 /*
67540 ** This function is only called from within an assert() expression. It
67541 ** checks that the sqlite3.nTransaction variable is correctly set to
67542 ** the number of non-transaction savepoints currently in the
67543 ** linked list starting at sqlite3.pSavepoint.
67544 **
67545 ** Usage:
67546 **
67547 **     assert( checkSavepointCount(db) );
67548 */
67549 static int checkSavepointCount(sqlite3 *db){
67550   int n = 0;
67551   Savepoint *p;
67552   for(p=db->pSavepoint; p; p=p->pNext) n++;
67553   assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
67554   return 1;
67555 }
67556 #endif
67557 
67558 
67559 /*
67560 ** Execute as much of a VDBE program as we can.
67561 ** This is the core of sqlite3_step().
67562 */
67563 SQLITE_PRIVATE int sqlite3VdbeExec(
67564   Vdbe *p                    /* The VDBE */
67565 ){
67566   int pc=0;                  /* The program counter */
67567   Op *aOp = p->aOp;          /* Copy of p->aOp */
67568   Op *pOp;                   /* Current operation */
67569   int rc = SQLITE_OK;        /* Value to return */
67570   sqlite3 *db = p->db;       /* The database */
67571   u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
67572   u8 encoding = ENC(db);     /* The database encoding */
67573   int iCompare = 0;          /* Result of last OP_Compare operation */
67574   unsigned nVmStep = 0;      /* Number of virtual machine steps */
67575 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
67576   unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
67577 #endif
67578   Mem *aMem = p->aMem;       /* Copy of p->aMem */
67579   Mem *pIn1 = 0;             /* 1st input operand */
67580   Mem *pIn2 = 0;             /* 2nd input operand */
67581   Mem *pIn3 = 0;             /* 3rd input operand */
67582   Mem *pOut = 0;             /* Output operand */
67583   int *aPermute = 0;         /* Permutation of columns for OP_Compare */
67584   i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
67585 #ifdef VDBE_PROFILE
67586   u64 start;                 /* CPU clock count at start of opcode */
67587 #endif
67588   /*** INSERT STACK UNION HERE ***/
67589 
67590   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
67591   sqlite3VdbeEnter(p);
67592   if( p->rc==SQLITE_NOMEM ){
67593     /* This happens if a malloc() inside a call to sqlite3_column_text() or
67594     ** sqlite3_column_text16() failed.  */
67595     goto no_mem;
67596   }
67597   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
67598   assert( p->bIsReader || p->readOnly!=0 );
67599   p->rc = SQLITE_OK;
67600   p->iCurrentTime = 0;
67601   assert( p->explain==0 );
67602   p->pResultSet = 0;
67603   db->busyHandler.nBusy = 0;
67604   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
67605   sqlite3VdbeIOTraceSql(p);
67606 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
67607   if( db->xProgress ){
67608     assert( 0 < db->nProgressOps );
67609     nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
67610     if( nProgressLimit==0 ){
67611       nProgressLimit = db->nProgressOps;
67612     }else{
67613       nProgressLimit %= (unsigned)db->nProgressOps;
67614     }
67615   }
67616 #endif
67617 #ifdef SQLITE_DEBUG
67618   sqlite3BeginBenignMalloc();
67619   if( p->pc==0
67620    && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
67621   ){
67622     int i;
67623     int once = 1;
67624     sqlite3VdbePrintSql(p);
67625     if( p->db->flags & SQLITE_VdbeListing ){
67626       printf("VDBE Program Listing:\n");
67627       for(i=0; i<p->nOp; i++){
67628         sqlite3VdbePrintOp(stdout, i, &aOp[i]);
67629       }
67630     }
67631     if( p->db->flags & SQLITE_VdbeEQP ){
67632       for(i=0; i<p->nOp; i++){
67633         if( aOp[i].opcode==OP_Explain ){
67634           if( once ) printf("VDBE Query Plan:\n");
67635           printf("%s\n", aOp[i].p4.z);
67636           once = 0;
67637         }
67638       }
67639     }
67640     if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
67641   }
67642   sqlite3EndBenignMalloc();
67643 #endif
67644   for(pc=p->pc; rc==SQLITE_OK; pc++){
67645     assert( pc>=0 && pc<p->nOp );
67646     if( db->mallocFailed ) goto no_mem;
67647 #ifdef VDBE_PROFILE
67648     start = sqlite3Hwtime();
67649 #endif
67650     nVmStep++;
67651     pOp = &aOp[pc];
67652 
67653     /* Only allow tracing if SQLITE_DEBUG is defined.
67654     */
67655 #ifdef SQLITE_DEBUG
67656     if( db->flags & SQLITE_VdbeTrace ){
67657       sqlite3VdbePrintOp(stdout, pc, pOp);
67658     }
67659 #endif
67660 
67661 
67662     /* Check to see if we need to simulate an interrupt.  This only happens
67663     ** if we have a special test build.
67664     */
67665 #ifdef SQLITE_TEST
67666     if( sqlite3_interrupt_count>0 ){
67667       sqlite3_interrupt_count--;
67668       if( sqlite3_interrupt_count==0 ){
67669         sqlite3_interrupt(db);
67670       }
67671     }
67672 #endif
67673 
67674     /* On any opcode with the "out2-prerelease" tag, free any
67675     ** external allocations out of mem[p2] and set mem[p2] to be
67676     ** an undefined integer.  Opcodes will either fill in the integer
67677     ** value or convert mem[p2] to a different type.
67678     */
67679     assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
67680     if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
67681       assert( pOp->p2>0 );
67682       assert( pOp->p2<=(p->nMem-p->nCursor) );
67683       pOut = &aMem[pOp->p2];
67684       memAboutToChange(p, pOut);
67685       VdbeMemRelease(pOut);
67686       pOut->flags = MEM_Int;
67687     }
67688 
67689     /* Sanity checking on other operands */
67690 #ifdef SQLITE_DEBUG
67691     if( (pOp->opflags & OPFLG_IN1)!=0 ){
67692       assert( pOp->p1>0 );
67693       assert( pOp->p1<=(p->nMem-p->nCursor) );
67694       assert( memIsValid(&aMem[pOp->p1]) );
67695       assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p1]) );
67696       REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
67697     }
67698     if( (pOp->opflags & OPFLG_IN2)!=0 ){
67699       assert( pOp->p2>0 );
67700       assert( pOp->p2<=(p->nMem-p->nCursor) );
67701       assert( memIsValid(&aMem[pOp->p2]) );
67702       assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p2]) );
67703       REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
67704     }
67705     if( (pOp->opflags & OPFLG_IN3)!=0 ){
67706       assert( pOp->p3>0 );
67707       assert( pOp->p3<=(p->nMem-p->nCursor) );
67708       assert( memIsValid(&aMem[pOp->p3]) );
67709       assert( sqlite3VdbeCheckMemInvariants(&aMem[pOp->p3]) );
67710       REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
67711     }
67712     if( (pOp->opflags & OPFLG_OUT2)!=0 ){
67713       assert( pOp->p2>0 );
67714       assert( pOp->p2<=(p->nMem-p->nCursor) );
67715       memAboutToChange(p, &aMem[pOp->p2]);
67716     }
67717     if( (pOp->opflags & OPFLG_OUT3)!=0 ){
67718       assert( pOp->p3>0 );
67719       assert( pOp->p3<=(p->nMem-p->nCursor) );
67720       memAboutToChange(p, &aMem[pOp->p3]);
67721     }
67722 #endif
67723 
67724     switch( pOp->opcode ){
67725 
67726 /*****************************************************************************
67727 ** What follows is a massive switch statement where each case implements a
67728 ** separate instruction in the virtual machine.  If we follow the usual
67729 ** indentation conventions, each case should be indented by 6 spaces.  But
67730 ** that is a lot of wasted space on the left margin.  So the code within
67731 ** the switch statement will break with convention and be flush-left. Another
67732 ** big comment (similar to this one) will mark the point in the code where
67733 ** we transition back to normal indentation.
67734 **
67735 ** The formatting of each case is important.  The makefile for SQLite
67736 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
67737 ** file looking for lines that begin with "case OP_".  The opcodes.h files
67738 ** will be filled with #defines that give unique integer values to each
67739 ** opcode and the opcodes.c file is filled with an array of strings where
67740 ** each string is the symbolic name for the corresponding opcode.  If the
67741 ** case statement is followed by a comment of the form "/# same as ... #/"
67742 ** that comment is used to determine the particular value of the opcode.
67743 **
67744 ** Other keywords in the comment that follows each case are used to
67745 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
67746 ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
67747 ** the mkopcodeh.awk script for additional information.
67748 **
67749 ** Documentation about VDBE opcodes is generated by scanning this file
67750 ** for lines of that contain "Opcode:".  That line and all subsequent
67751 ** comment lines are used in the generation of the opcode.html documentation
67752 ** file.
67753 **
67754 ** SUMMARY:
67755 **
67756 **     Formatting is important to scripts that scan this file.
67757 **     Do not deviate from the formatting style currently in use.
67758 **
67759 *****************************************************************************/
67760 
67761 /* Opcode:  Goto * P2 * * *
67762 **
67763 ** An unconditional jump to address P2.
67764 ** The next instruction executed will be
67765 ** the one at index P2 from the beginning of
67766 ** the program.
67767 **
67768 ** The P1 parameter is not actually used by this opcode.  However, it
67769 ** is sometimes set to 1 instead of 0 as a hint to the command-line shell
67770 ** that this Goto is the bottom of a loop and that the lines from P2 down
67771 ** to the current line should be indented for EXPLAIN output.
67772 */
67773 case OP_Goto: {             /* jump */
67774   pc = pOp->p2 - 1;
67775 
67776   /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
67777   ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
67778   ** completion.  Check to see if sqlite3_interrupt() has been called
67779   ** or if the progress callback needs to be invoked.
67780   **
67781   ** This code uses unstructured "goto" statements and does not look clean.
67782   ** But that is not due to sloppy coding habits. The code is written this
67783   ** way for performance, to avoid having to run the interrupt and progress
67784   ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
67785   ** faster according to "valgrind --tool=cachegrind" */
67786 check_for_interrupt:
67787   if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
67788 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
67789   /* Call the progress callback if it is configured and the required number
67790   ** of VDBE ops have been executed (either since this invocation of
67791   ** sqlite3VdbeExec() or since last time the progress callback was called).
67792   ** If the progress callback returns non-zero, exit the virtual machine with
67793   ** a return code SQLITE_ABORT.
67794   */
67795   if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
67796     assert( db->nProgressOps!=0 );
67797     nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
67798     if( db->xProgress(db->pProgressArg) ){
67799       rc = SQLITE_INTERRUPT;
67800       goto vdbe_error_halt;
67801     }
67802   }
67803 #endif
67804 
67805   break;
67806 }
67807 
67808 /* Opcode:  Gosub P1 P2 * * *
67809 **
67810 ** Write the current address onto register P1
67811 ** and then jump to address P2.
67812 */
67813 case OP_Gosub: {            /* jump */
67814   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
67815   pIn1 = &aMem[pOp->p1];
67816   assert( VdbeMemDynamic(pIn1)==0 );
67817   memAboutToChange(p, pIn1);
67818   pIn1->flags = MEM_Int;
67819   pIn1->u.i = pc;
67820   REGISTER_TRACE(pOp->p1, pIn1);
67821   pc = pOp->p2 - 1;
67822   break;
67823 }
67824 
67825 /* Opcode:  Return P1 * * * *
67826 **
67827 ** Jump to the next instruction after the address in register P1.  After
67828 ** the jump, register P1 becomes undefined.
67829 */
67830 case OP_Return: {           /* in1 */
67831   pIn1 = &aMem[pOp->p1];
67832   assert( pIn1->flags==MEM_Int );
67833   pc = (int)pIn1->u.i;
67834   pIn1->flags = MEM_Undefined;
67835   break;
67836 }
67837 
67838 /* Opcode: InitCoroutine P1 P2 P3 * *
67839 **
67840 ** Set up register P1 so that it will OP_Yield to the co-routine
67841 ** located at address P3.
67842 **
67843 ** If P2!=0 then the co-routine implementation immediately follows
67844 ** this opcode.  So jump over the co-routine implementation to
67845 ** address P2.
67846 */
67847 case OP_InitCoroutine: {     /* jump */
67848   assert( pOp->p1>0 &&  pOp->p1<=(p->nMem-p->nCursor) );
67849   assert( pOp->p2>=0 && pOp->p2<p->nOp );
67850   assert( pOp->p3>=0 && pOp->p3<p->nOp );
67851   pOut = &aMem[pOp->p1];
67852   assert( !VdbeMemDynamic(pOut) );
67853   pOut->u.i = pOp->p3 - 1;
67854   pOut->flags = MEM_Int;
67855   if( pOp->p2 ) pc = pOp->p2 - 1;
67856   break;
67857 }
67858 
67859 /* Opcode:  EndCoroutine P1 * * * *
67860 **
67861 ** The instruction at the address in register P1 is an OP_Yield.
67862 ** Jump to the P2 parameter of that OP_Yield.
67863 ** After the jump, register P1 becomes undefined.
67864 */
67865 case OP_EndCoroutine: {           /* in1 */
67866   VdbeOp *pCaller;
67867   pIn1 = &aMem[pOp->p1];
67868   assert( pIn1->flags==MEM_Int );
67869   assert( pIn1->u.i>=0 && pIn1->u.i<p->nOp );
67870   pCaller = &aOp[pIn1->u.i];
67871   assert( pCaller->opcode==OP_Yield );
67872   assert( pCaller->p2>=0 && pCaller->p2<p->nOp );
67873   pc = pCaller->p2 - 1;
67874   pIn1->flags = MEM_Undefined;
67875   break;
67876 }
67877 
67878 /* Opcode:  Yield P1 P2 * * *
67879 **
67880 ** Swap the program counter with the value in register P1.
67881 **
67882 ** If the co-routine ends with OP_Yield or OP_Return then continue
67883 ** to the next instruction.  But if the co-routine ends with
67884 ** OP_EndCoroutine, jump immediately to P2.
67885 */
67886 case OP_Yield: {            /* in1, jump */
67887   int pcDest;
67888   pIn1 = &aMem[pOp->p1];
67889   assert( VdbeMemDynamic(pIn1)==0 );
67890   pIn1->flags = MEM_Int;
67891   pcDest = (int)pIn1->u.i;
67892   pIn1->u.i = pc;
67893   REGISTER_TRACE(pOp->p1, pIn1);
67894   pc = pcDest;
67895   break;
67896 }
67897 
67898 /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
67899 ** Synopsis:  if r[P3]=null halt
67900 **
67901 ** Check the value in register P3.  If it is NULL then Halt using
67902 ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
67903 ** value in register P3 is not NULL, then this routine is a no-op.
67904 ** The P5 parameter should be 1.
67905 */
67906 case OP_HaltIfNull: {      /* in3 */
67907   pIn3 = &aMem[pOp->p3];
67908   if( (pIn3->flags & MEM_Null)==0 ) break;
67909   /* Fall through into OP_Halt */
67910 }
67911 
67912 /* Opcode:  Halt P1 P2 * P4 P5
67913 **
67914 ** Exit immediately.  All open cursors, etc are closed
67915 ** automatically.
67916 **
67917 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
67918 ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
67919 ** For errors, it can be some other value.  If P1!=0 then P2 will determine
67920 ** whether or not to rollback the current transaction.  Do not rollback
67921 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
67922 ** then back out all changes that have occurred during this execution of the
67923 ** VDBE, but do not rollback the transaction.
67924 **
67925 ** If P4 is not null then it is an error message string.
67926 **
67927 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
67928 **
67929 **    0:  (no change)
67930 **    1:  NOT NULL contraint failed: P4
67931 **    2:  UNIQUE constraint failed: P4
67932 **    3:  CHECK constraint failed: P4
67933 **    4:  FOREIGN KEY constraint failed: P4
67934 **
67935 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
67936 ** omitted.
67937 **
67938 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
67939 ** every program.  So a jump past the last instruction of the program
67940 ** is the same as executing Halt.
67941 */
67942 case OP_Halt: {
67943   const char *zType;
67944   const char *zLogFmt;
67945 
67946   if( pOp->p1==SQLITE_OK && p->pFrame ){
67947     /* Halt the sub-program. Return control to the parent frame. */
67948     VdbeFrame *pFrame = p->pFrame;
67949     p->pFrame = pFrame->pParent;
67950     p->nFrame--;
67951     sqlite3VdbeSetChanges(db, p->nChange);
67952     pc = sqlite3VdbeFrameRestore(pFrame);
67953     lastRowid = db->lastRowid;
67954     if( pOp->p2==OE_Ignore ){
67955       /* Instruction pc is the OP_Program that invoked the sub-program
67956       ** currently being halted. If the p2 instruction of this OP_Halt
67957       ** instruction is set to OE_Ignore, then the sub-program is throwing
67958       ** an IGNORE exception. In this case jump to the address specified
67959       ** as the p2 of the calling OP_Program.  */
67960       pc = p->aOp[pc].p2-1;
67961     }
67962     aOp = p->aOp;
67963     aMem = p->aMem;
67964     break;
67965   }
67966   p->rc = pOp->p1;
67967   p->errorAction = (u8)pOp->p2;
67968   p->pc = pc;
67969   if( p->rc ){
67970     if( pOp->p5 ){
67971       static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
67972                                              "FOREIGN KEY" };
67973       assert( pOp->p5>=1 && pOp->p5<=4 );
67974       testcase( pOp->p5==1 );
67975       testcase( pOp->p5==2 );
67976       testcase( pOp->p5==3 );
67977       testcase( pOp->p5==4 );
67978       zType = azType[pOp->p5-1];
67979     }else{
67980       zType = 0;
67981     }
67982     assert( zType!=0 || pOp->p4.z!=0 );
67983     zLogFmt = "abort at %d in [%s]: %s";
67984     if( zType && pOp->p4.z ){
67985       sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
67986                        zType, pOp->p4.z);
67987     }else if( pOp->p4.z ){
67988       sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
67989     }else{
67990       sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
67991     }
67992     sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
67993   }
67994   rc = sqlite3VdbeHalt(p);
67995   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
67996   if( rc==SQLITE_BUSY ){
67997     p->rc = rc = SQLITE_BUSY;
67998   }else{
67999     assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
68000     assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
68001     rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
68002   }
68003   goto vdbe_return;
68004 }
68005 
68006 /* Opcode: Integer P1 P2 * * *
68007 ** Synopsis: r[P2]=P1
68008 **
68009 ** The 32-bit integer value P1 is written into register P2.
68010 */
68011 case OP_Integer: {         /* out2-prerelease */
68012   pOut->u.i = pOp->p1;
68013   break;
68014 }
68015 
68016 /* Opcode: Int64 * P2 * P4 *
68017 ** Synopsis: r[P2]=P4
68018 **
68019 ** P4 is a pointer to a 64-bit integer value.
68020 ** Write that value into register P2.
68021 */
68022 case OP_Int64: {           /* out2-prerelease */
68023   assert( pOp->p4.pI64!=0 );
68024   pOut->u.i = *pOp->p4.pI64;
68025   break;
68026 }
68027 
68028 #ifndef SQLITE_OMIT_FLOATING_POINT
68029 /* Opcode: Real * P2 * P4 *
68030 ** Synopsis: r[P2]=P4
68031 **
68032 ** P4 is a pointer to a 64-bit floating point value.
68033 ** Write that value into register P2.
68034 */
68035 case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
68036   pOut->flags = MEM_Real;
68037   assert( !sqlite3IsNaN(*pOp->p4.pReal) );
68038   pOut->r = *pOp->p4.pReal;
68039   break;
68040 }
68041 #endif
68042 
68043 /* Opcode: String8 * P2 * P4 *
68044 ** Synopsis: r[P2]='P4'
68045 **
68046 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
68047 ** into an OP_String before it is executed for the first time.  During
68048 ** this transformation, the length of string P4 is computed and stored
68049 ** as the P1 parameter.
68050 */
68051 case OP_String8: {         /* same as TK_STRING, out2-prerelease */
68052   assert( pOp->p4.z!=0 );
68053   pOp->opcode = OP_String;
68054   pOp->p1 = sqlite3Strlen30(pOp->p4.z);
68055 
68056 #ifndef SQLITE_OMIT_UTF16
68057   if( encoding!=SQLITE_UTF8 ){
68058     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
68059     if( rc==SQLITE_TOOBIG ) goto too_big;
68060     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
68061     assert( pOut->zMalloc==pOut->z );
68062     assert( VdbeMemDynamic(pOut)==0 );
68063     pOut->zMalloc = 0;
68064     pOut->flags |= MEM_Static;
68065     if( pOp->p4type==P4_DYNAMIC ){
68066       sqlite3DbFree(db, pOp->p4.z);
68067     }
68068     pOp->p4type = P4_DYNAMIC;
68069     pOp->p4.z = pOut->z;
68070     pOp->p1 = pOut->n;
68071   }
68072 #endif
68073   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
68074     goto too_big;
68075   }
68076   /* Fall through to the next case, OP_String */
68077 }
68078 
68079 /* Opcode: String P1 P2 * P4 *
68080 ** Synopsis: r[P2]='P4' (len=P1)
68081 **
68082 ** The string value P4 of length P1 (bytes) is stored in register P2.
68083 */
68084 case OP_String: {          /* out2-prerelease */
68085   assert( pOp->p4.z!=0 );
68086   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
68087   pOut->z = pOp->p4.z;
68088   pOut->n = pOp->p1;
68089   pOut->enc = encoding;
68090   UPDATE_MAX_BLOBSIZE(pOut);
68091   break;
68092 }
68093 
68094 /* Opcode: Null P1 P2 P3 * *
68095 ** Synopsis:  r[P2..P3]=NULL
68096 **
68097 ** Write a NULL into registers P2.  If P3 greater than P2, then also write
68098 ** NULL into register P3 and every register in between P2 and P3.  If P3
68099 ** is less than P2 (typically P3 is zero) then only register P2 is
68100 ** set to NULL.
68101 **
68102 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
68103 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
68104 ** OP_Ne or OP_Eq.
68105 */
68106 case OP_Null: {           /* out2-prerelease */
68107   int cnt;
68108   u16 nullFlag;
68109   cnt = pOp->p3-pOp->p2;
68110   assert( pOp->p3<=(p->nMem-p->nCursor) );
68111   pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
68112   while( cnt>0 ){
68113     pOut++;
68114     memAboutToChange(p, pOut);
68115     VdbeMemRelease(pOut);
68116     pOut->flags = nullFlag;
68117     cnt--;
68118   }
68119   break;
68120 }
68121 
68122 /* Opcode: SoftNull P1 * * * *
68123 ** Synopsis:  r[P1]=NULL
68124 **
68125 ** Set register P1 to have the value NULL as seen by the OP_MakeRecord
68126 ** instruction, but do not free any string or blob memory associated with
68127 ** the register, so that if the value was a string or blob that was
68128 ** previously copied using OP_SCopy, the copies will continue to be valid.
68129 */
68130 case OP_SoftNull: {
68131   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
68132   pOut = &aMem[pOp->p1];
68133   pOut->flags = (pOut->flags|MEM_Null)&~MEM_Undefined;
68134   break;
68135 }
68136 
68137 /* Opcode: Blob P1 P2 * P4 *
68138 ** Synopsis: r[P2]=P4 (len=P1)
68139 **
68140 ** P4 points to a blob of data P1 bytes long.  Store this
68141 ** blob in register P2.
68142 */
68143 case OP_Blob: {                /* out2-prerelease */
68144   assert( pOp->p1 <= SQLITE_MAX_LENGTH );
68145   sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
68146   pOut->enc = encoding;
68147   UPDATE_MAX_BLOBSIZE(pOut);
68148   break;
68149 }
68150 
68151 /* Opcode: Variable P1 P2 * P4 *
68152 ** Synopsis: r[P2]=parameter(P1,P4)
68153 **
68154 ** Transfer the values of bound parameter P1 into register P2
68155 **
68156 ** If the parameter is named, then its name appears in P4.
68157 ** The P4 value is used by sqlite3_bind_parameter_name().
68158 */
68159 case OP_Variable: {            /* out2-prerelease */
68160   Mem *pVar;       /* Value being transferred */
68161 
68162   assert( pOp->p1>0 && pOp->p1<=p->nVar );
68163   assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
68164   pVar = &p->aVar[pOp->p1 - 1];
68165   if( sqlite3VdbeMemTooBig(pVar) ){
68166     goto too_big;
68167   }
68168   sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
68169   UPDATE_MAX_BLOBSIZE(pOut);
68170   break;
68171 }
68172 
68173 /* Opcode: Move P1 P2 P3 * *
68174 ** Synopsis:  r[P2@P3]=r[P1@P3]
68175 **
68176 ** Move the values in register P1..P1+P3 over into
68177 ** registers P2..P2+P3.  Registers P1..P1+P3 are
68178 ** left holding a NULL.  It is an error for register ranges
68179 ** P1..P1+P3 and P2..P2+P3 to overlap.
68180 */
68181 case OP_Move: {
68182   char *zMalloc;   /* Holding variable for allocated memory */
68183   int n;           /* Number of registers left to copy */
68184   int p1;          /* Register to copy from */
68185   int p2;          /* Register to copy to */
68186 
68187   n = pOp->p3;
68188   p1 = pOp->p1;
68189   p2 = pOp->p2;
68190   assert( n>=0 && p1>0 && p2>0 );
68191   assert( p1+n<=p2 || p2+n<=p1 );
68192 
68193   pIn1 = &aMem[p1];
68194   pOut = &aMem[p2];
68195   do{
68196     assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
68197     assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
68198     assert( memIsValid(pIn1) );
68199     memAboutToChange(p, pOut);
68200     VdbeMemRelease(pOut);
68201     zMalloc = pOut->zMalloc;
68202     memcpy(pOut, pIn1, sizeof(Mem));
68203 #ifdef SQLITE_DEBUG
68204     if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
68205       pOut->pScopyFrom += p1 - pOp->p2;
68206     }
68207 #endif
68208     pIn1->flags = MEM_Undefined;
68209     pIn1->xDel = 0;
68210     pIn1->zMalloc = zMalloc;
68211     REGISTER_TRACE(p2++, pOut);
68212     pIn1++;
68213     pOut++;
68214   }while( n-- );
68215   break;
68216 }
68217 
68218 /* Opcode: Copy P1 P2 P3 * *
68219 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
68220 **
68221 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
68222 **
68223 ** This instruction makes a deep copy of the value.  A duplicate
68224 ** is made of any string or blob constant.  See also OP_SCopy.
68225 */
68226 case OP_Copy: {
68227   int n;
68228 
68229   n = pOp->p3;
68230   pIn1 = &aMem[pOp->p1];
68231   pOut = &aMem[pOp->p2];
68232   assert( pOut!=pIn1 );
68233   while( 1 ){
68234     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
68235     Deephemeralize(pOut);
68236 #ifdef SQLITE_DEBUG
68237     pOut->pScopyFrom = 0;
68238 #endif
68239     REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
68240     if( (n--)==0 ) break;
68241     pOut++;
68242     pIn1++;
68243   }
68244   break;
68245 }
68246 
68247 /* Opcode: SCopy P1 P2 * * *
68248 ** Synopsis: r[P2]=r[P1]
68249 **
68250 ** Make a shallow copy of register P1 into register P2.
68251 **
68252 ** This instruction makes a shallow copy of the value.  If the value
68253 ** is a string or blob, then the copy is only a pointer to the
68254 ** original and hence if the original changes so will the copy.
68255 ** Worse, if the original is deallocated, the copy becomes invalid.
68256 ** Thus the program must guarantee that the original will not change
68257 ** during the lifetime of the copy.  Use OP_Copy to make a complete
68258 ** copy.
68259 */
68260 case OP_SCopy: {            /* out2 */
68261   pIn1 = &aMem[pOp->p1];
68262   pOut = &aMem[pOp->p2];
68263   assert( pOut!=pIn1 );
68264   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
68265 #ifdef SQLITE_DEBUG
68266   if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
68267 #endif
68268   break;
68269 }
68270 
68271 /* Opcode: ResultRow P1 P2 * * *
68272 ** Synopsis:  output=r[P1@P2]
68273 **
68274 ** The registers P1 through P1+P2-1 contain a single row of
68275 ** results. This opcode causes the sqlite3_step() call to terminate
68276 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
68277 ** structure to provide access to the r(P1)..r(P1+P2-1) values as
68278 ** the result row.
68279 */
68280 case OP_ResultRow: {
68281   Mem *pMem;
68282   int i;
68283   assert( p->nResColumn==pOp->p2 );
68284   assert( pOp->p1>0 );
68285   assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
68286 
68287 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
68288   /* Run the progress counter just before returning.
68289   */
68290   if( db->xProgress!=0
68291    && nVmStep>=nProgressLimit
68292    && db->xProgress(db->pProgressArg)!=0
68293   ){
68294     rc = SQLITE_INTERRUPT;
68295     goto vdbe_error_halt;
68296   }
68297 #endif
68298 
68299   /* If this statement has violated immediate foreign key constraints, do
68300   ** not return the number of rows modified. And do not RELEASE the statement
68301   ** transaction. It needs to be rolled back.  */
68302   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
68303     assert( db->flags&SQLITE_CountRows );
68304     assert( p->usesStmtJournal );
68305     break;
68306   }
68307 
68308   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
68309   ** DML statements invoke this opcode to return the number of rows
68310   ** modified to the user. This is the only way that a VM that
68311   ** opens a statement transaction may invoke this opcode.
68312   **
68313   ** In case this is such a statement, close any statement transaction
68314   ** opened by this VM before returning control to the user. This is to
68315   ** ensure that statement-transactions are always nested, not overlapping.
68316   ** If the open statement-transaction is not closed here, then the user
68317   ** may step another VM that opens its own statement transaction. This
68318   ** may lead to overlapping statement transactions.
68319   **
68320   ** The statement transaction is never a top-level transaction.  Hence
68321   ** the RELEASE call below can never fail.
68322   */
68323   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
68324   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
68325   if( NEVER(rc!=SQLITE_OK) ){
68326     break;
68327   }
68328 
68329   /* Invalidate all ephemeral cursor row caches */
68330   p->cacheCtr = (p->cacheCtr + 2)|1;
68331 
68332   /* Make sure the results of the current row are \000 terminated
68333   ** and have an assigned type.  The results are de-ephemeralized as
68334   ** a side effect.
68335   */
68336   pMem = p->pResultSet = &aMem[pOp->p1];
68337   for(i=0; i<pOp->p2; i++){
68338     assert( memIsValid(&pMem[i]) );
68339     Deephemeralize(&pMem[i]);
68340     assert( (pMem[i].flags & MEM_Ephem)==0
68341             || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
68342     sqlite3VdbeMemNulTerminate(&pMem[i]);
68343     REGISTER_TRACE(pOp->p1+i, &pMem[i]);
68344   }
68345   if( db->mallocFailed ) goto no_mem;
68346 
68347   /* Return SQLITE_ROW
68348   */
68349   p->pc = pc + 1;
68350   rc = SQLITE_ROW;
68351   goto vdbe_return;
68352 }
68353 
68354 /* Opcode: Concat P1 P2 P3 * *
68355 ** Synopsis: r[P3]=r[P2]+r[P1]
68356 **
68357 ** Add the text in register P1 onto the end of the text in
68358 ** register P2 and store the result in register P3.
68359 ** If either the P1 or P2 text are NULL then store NULL in P3.
68360 **
68361 **   P3 = P2 || P1
68362 **
68363 ** It is illegal for P1 and P3 to be the same register. Sometimes,
68364 ** if P3 is the same register as P2, the implementation is able
68365 ** to avoid a memcpy().
68366 */
68367 case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
68368   i64 nByte;
68369 
68370   pIn1 = &aMem[pOp->p1];
68371   pIn2 = &aMem[pOp->p2];
68372   pOut = &aMem[pOp->p3];
68373   assert( pIn1!=pOut );
68374   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
68375     sqlite3VdbeMemSetNull(pOut);
68376     break;
68377   }
68378   if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
68379   Stringify(pIn1, encoding);
68380   Stringify(pIn2, encoding);
68381   nByte = pIn1->n + pIn2->n;
68382   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
68383     goto too_big;
68384   }
68385   if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
68386     goto no_mem;
68387   }
68388   MemSetTypeFlag(pOut, MEM_Str);
68389   if( pOut!=pIn2 ){
68390     memcpy(pOut->z, pIn2->z, pIn2->n);
68391   }
68392   memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
68393   pOut->z[nByte]=0;
68394   pOut->z[nByte+1] = 0;
68395   pOut->flags |= MEM_Term;
68396   pOut->n = (int)nByte;
68397   pOut->enc = encoding;
68398   UPDATE_MAX_BLOBSIZE(pOut);
68399   break;
68400 }
68401 
68402 /* Opcode: Add P1 P2 P3 * *
68403 ** Synopsis:  r[P3]=r[P1]+r[P2]
68404 **
68405 ** Add the value in register P1 to the value in register P2
68406 ** and store the result in register P3.
68407 ** If either input is NULL, the result is NULL.
68408 */
68409 /* Opcode: Multiply P1 P2 P3 * *
68410 ** Synopsis:  r[P3]=r[P1]*r[P2]
68411 **
68412 **
68413 ** Multiply the value in register P1 by the value in register P2
68414 ** and store the result in register P3.
68415 ** If either input is NULL, the result is NULL.
68416 */
68417 /* Opcode: Subtract P1 P2 P3 * *
68418 ** Synopsis:  r[P3]=r[P2]-r[P1]
68419 **
68420 ** Subtract the value in register P1 from the value in register P2
68421 ** and store the result in register P3.
68422 ** If either input is NULL, the result is NULL.
68423 */
68424 /* Opcode: Divide P1 P2 P3 * *
68425 ** Synopsis:  r[P3]=r[P2]/r[P1]
68426 **
68427 ** Divide the value in register P1 by the value in register P2
68428 ** and store the result in register P3 (P3=P2/P1). If the value in
68429 ** register P1 is zero, then the result is NULL. If either input is
68430 ** NULL, the result is NULL.
68431 */
68432 /* Opcode: Remainder P1 P2 P3 * *
68433 ** Synopsis:  r[P3]=r[P2]%r[P1]
68434 **
68435 ** Compute the remainder after integer register P2 is divided by
68436 ** register P1 and store the result in register P3.
68437 ** If the value in register P1 is zero the result is NULL.
68438 ** If either operand is NULL, the result is NULL.
68439 */
68440 case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
68441 case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
68442 case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
68443 case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
68444 case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
68445   char bIntint;   /* Started out as two integer operands */
68446   int flags;      /* Combined MEM_* flags from both inputs */
68447   i64 iA;         /* Integer value of left operand */
68448   i64 iB;         /* Integer value of right operand */
68449   double rA;      /* Real value of left operand */
68450   double rB;      /* Real value of right operand */
68451 
68452   pIn1 = &aMem[pOp->p1];
68453   applyNumericAffinity(pIn1);
68454   pIn2 = &aMem[pOp->p2];
68455   applyNumericAffinity(pIn2);
68456   pOut = &aMem[pOp->p3];
68457   flags = pIn1->flags | pIn2->flags;
68458   if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
68459   if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
68460     iA = pIn1->u.i;
68461     iB = pIn2->u.i;
68462     bIntint = 1;
68463     switch( pOp->opcode ){
68464       case OP_Add:       if( sqlite3AddInt64(&iB,iA) ) goto fp_math;  break;
68465       case OP_Subtract:  if( sqlite3SubInt64(&iB,iA) ) goto fp_math;  break;
68466       case OP_Multiply:  if( sqlite3MulInt64(&iB,iA) ) goto fp_math;  break;
68467       case OP_Divide: {
68468         if( iA==0 ) goto arithmetic_result_is_null;
68469         if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
68470         iB /= iA;
68471         break;
68472       }
68473       default: {
68474         if( iA==0 ) goto arithmetic_result_is_null;
68475         if( iA==-1 ) iA = 1;
68476         iB %= iA;
68477         break;
68478       }
68479     }
68480     pOut->u.i = iB;
68481     MemSetTypeFlag(pOut, MEM_Int);
68482   }else{
68483     bIntint = 0;
68484 fp_math:
68485     rA = sqlite3VdbeRealValue(pIn1);
68486     rB = sqlite3VdbeRealValue(pIn2);
68487     switch( pOp->opcode ){
68488       case OP_Add:         rB += rA;       break;
68489       case OP_Subtract:    rB -= rA;       break;
68490       case OP_Multiply:    rB *= rA;       break;
68491       case OP_Divide: {
68492         /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
68493         if( rA==(double)0 ) goto arithmetic_result_is_null;
68494         rB /= rA;
68495         break;
68496       }
68497       default: {
68498         iA = (i64)rA;
68499         iB = (i64)rB;
68500         if( iA==0 ) goto arithmetic_result_is_null;
68501         if( iA==-1 ) iA = 1;
68502         rB = (double)(iB % iA);
68503         break;
68504       }
68505     }
68506 #ifdef SQLITE_OMIT_FLOATING_POINT
68507     pOut->u.i = rB;
68508     MemSetTypeFlag(pOut, MEM_Int);
68509 #else
68510     if( sqlite3IsNaN(rB) ){
68511       goto arithmetic_result_is_null;
68512     }
68513     pOut->r = rB;
68514     MemSetTypeFlag(pOut, MEM_Real);
68515     if( (flags & MEM_Real)==0 && !bIntint ){
68516       sqlite3VdbeIntegerAffinity(pOut);
68517     }
68518 #endif
68519   }
68520   break;
68521 
68522 arithmetic_result_is_null:
68523   sqlite3VdbeMemSetNull(pOut);
68524   break;
68525 }
68526 
68527 /* Opcode: CollSeq P1 * * P4
68528 **
68529 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
68530 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
68531 ** be returned. This is used by the built-in min(), max() and nullif()
68532 ** functions.
68533 **
68534 ** If P1 is not zero, then it is a register that a subsequent min() or
68535 ** max() aggregate will set to 1 if the current row is not the minimum or
68536 ** maximum.  The P1 register is initialized to 0 by this instruction.
68537 **
68538 ** The interface used by the implementation of the aforementioned functions
68539 ** to retrieve the collation sequence set by this opcode is not available
68540 ** publicly, only to user functions defined in func.c.
68541 */
68542 case OP_CollSeq: {
68543   assert( pOp->p4type==P4_COLLSEQ );
68544   if( pOp->p1 ){
68545     sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
68546   }
68547   break;
68548 }
68549 
68550 /* Opcode: Function P1 P2 P3 P4 P5
68551 ** Synopsis: r[P3]=func(r[P2@P5])
68552 **
68553 ** Invoke a user function (P4 is a pointer to a Function structure that
68554 ** defines the function) with P5 arguments taken from register P2 and
68555 ** successors.  The result of the function is stored in register P3.
68556 ** Register P3 must not be one of the function inputs.
68557 **
68558 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
68559 ** function was determined to be constant at compile time. If the first
68560 ** argument was constant then bit 0 of P1 is set. This is used to determine
68561 ** whether meta data associated with a user function argument using the
68562 ** sqlite3_set_auxdata() API may be safely retained until the next
68563 ** invocation of this opcode.
68564 **
68565 ** See also: AggStep and AggFinal
68566 */
68567 case OP_Function: {
68568   int i;
68569   Mem *pArg;
68570   sqlite3_context ctx;
68571   sqlite3_value **apVal;
68572   int n;
68573 
68574   n = pOp->p5;
68575   apVal = p->apArg;
68576   assert( apVal || n==0 );
68577   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
68578   pOut = &aMem[pOp->p3];
68579   memAboutToChange(p, pOut);
68580 
68581   assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
68582   assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
68583   pArg = &aMem[pOp->p2];
68584   for(i=0; i<n; i++, pArg++){
68585     assert( memIsValid(pArg) );
68586     apVal[i] = pArg;
68587     Deephemeralize(pArg);
68588     REGISTER_TRACE(pOp->p2+i, pArg);
68589   }
68590 
68591   assert( pOp->p4type==P4_FUNCDEF );
68592   ctx.pFunc = pOp->p4.pFunc;
68593   ctx.iOp = pc;
68594   ctx.pVdbe = p;
68595 
68596   /* The output cell may already have a buffer allocated. Move
68597   ** the pointer to ctx.s so in case the user-function can use
68598   ** the already allocated buffer instead of allocating a new one.
68599   */
68600   memcpy(&ctx.s, pOut, sizeof(Mem));
68601   pOut->flags = MEM_Null;
68602   pOut->xDel = 0;
68603   pOut->zMalloc = 0;
68604   MemSetTypeFlag(&ctx.s, MEM_Null);
68605 
68606   ctx.fErrorOrAux = 0;
68607   if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
68608     assert( pOp>aOp );
68609     assert( pOp[-1].p4type==P4_COLLSEQ );
68610     assert( pOp[-1].opcode==OP_CollSeq );
68611     ctx.pColl = pOp[-1].p4.pColl;
68612   }
68613   db->lastRowid = lastRowid;
68614   (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
68615   lastRowid = db->lastRowid;
68616 
68617   if( db->mallocFailed ){
68618     /* Even though a malloc() has failed, the implementation of the
68619     ** user function may have called an sqlite3_result_XXX() function
68620     ** to return a value. The following call releases any resources
68621     ** associated with such a value.
68622     */
68623     sqlite3VdbeMemRelease(&ctx.s);
68624     goto no_mem;
68625   }
68626 
68627   /* If the function returned an error, throw an exception */
68628   if( ctx.fErrorOrAux ){
68629     if( ctx.isError ){
68630       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
68631       rc = ctx.isError;
68632     }
68633     sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
68634   }
68635 
68636   /* Copy the result of the function into register P3 */
68637   sqlite3VdbeChangeEncoding(&ctx.s, encoding);
68638   assert( pOut->flags==MEM_Null );
68639   memcpy(pOut, &ctx.s, sizeof(Mem));
68640   if( sqlite3VdbeMemTooBig(pOut) ){
68641     goto too_big;
68642   }
68643 
68644 #if 0
68645   /* The app-defined function has done something that as caused this
68646   ** statement to expire.  (Perhaps the function called sqlite3_exec()
68647   ** with a CREATE TABLE statement.)
68648   */
68649   if( p->expired ) rc = SQLITE_ABORT;
68650 #endif
68651 
68652   REGISTER_TRACE(pOp->p3, pOut);
68653   UPDATE_MAX_BLOBSIZE(pOut);
68654   break;
68655 }
68656 
68657 /* Opcode: BitAnd P1 P2 P3 * *
68658 ** Synopsis:  r[P3]=r[P1]&r[P2]
68659 **
68660 ** Take the bit-wise AND of the values in register P1 and P2 and
68661 ** store the result in register P3.
68662 ** If either input is NULL, the result is NULL.
68663 */
68664 /* Opcode: BitOr P1 P2 P3 * *
68665 ** Synopsis:  r[P3]=r[P1]|r[P2]
68666 **
68667 ** Take the bit-wise OR of the values in register P1 and P2 and
68668 ** store the result in register P3.
68669 ** If either input is NULL, the result is NULL.
68670 */
68671 /* Opcode: ShiftLeft P1 P2 P3 * *
68672 ** Synopsis:  r[P3]=r[P2]<<r[P1]
68673 **
68674 ** Shift the integer value in register P2 to the left by the
68675 ** number of bits specified by the integer in register P1.
68676 ** Store the result in register P3.
68677 ** If either input is NULL, the result is NULL.
68678 */
68679 /* Opcode: ShiftRight P1 P2 P3 * *
68680 ** Synopsis:  r[P3]=r[P2]>>r[P1]
68681 **
68682 ** Shift the integer value in register P2 to the right by the
68683 ** number of bits specified by the integer in register P1.
68684 ** Store the result in register P3.
68685 ** If either input is NULL, the result is NULL.
68686 */
68687 case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
68688 case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
68689 case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
68690 case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
68691   i64 iA;
68692   u64 uA;
68693   i64 iB;
68694   u8 op;
68695 
68696   pIn1 = &aMem[pOp->p1];
68697   pIn2 = &aMem[pOp->p2];
68698   pOut = &aMem[pOp->p3];
68699   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
68700     sqlite3VdbeMemSetNull(pOut);
68701     break;
68702   }
68703   iA = sqlite3VdbeIntValue(pIn2);
68704   iB = sqlite3VdbeIntValue(pIn1);
68705   op = pOp->opcode;
68706   if( op==OP_BitAnd ){
68707     iA &= iB;
68708   }else if( op==OP_BitOr ){
68709     iA |= iB;
68710   }else if( iB!=0 ){
68711     assert( op==OP_ShiftRight || op==OP_ShiftLeft );
68712 
68713     /* If shifting by a negative amount, shift in the other direction */
68714     if( iB<0 ){
68715       assert( OP_ShiftRight==OP_ShiftLeft+1 );
68716       op = 2*OP_ShiftLeft + 1 - op;
68717       iB = iB>(-64) ? -iB : 64;
68718     }
68719 
68720     if( iB>=64 ){
68721       iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
68722     }else{
68723       memcpy(&uA, &iA, sizeof(uA));
68724       if( op==OP_ShiftLeft ){
68725         uA <<= iB;
68726       }else{
68727         uA >>= iB;
68728         /* Sign-extend on a right shift of a negative number */
68729         if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
68730       }
68731       memcpy(&iA, &uA, sizeof(iA));
68732     }
68733   }
68734   pOut->u.i = iA;
68735   MemSetTypeFlag(pOut, MEM_Int);
68736   break;
68737 }
68738 
68739 /* Opcode: AddImm  P1 P2 * * *
68740 ** Synopsis:  r[P1]=r[P1]+P2
68741 **
68742 ** Add the constant P2 to the value in register P1.
68743 ** The result is always an integer.
68744 **
68745 ** To force any register to be an integer, just add 0.
68746 */
68747 case OP_AddImm: {            /* in1 */
68748   pIn1 = &aMem[pOp->p1];
68749   memAboutToChange(p, pIn1);
68750   sqlite3VdbeMemIntegerify(pIn1);
68751   pIn1->u.i += pOp->p2;
68752   break;
68753 }
68754 
68755 /* Opcode: MustBeInt P1 P2 * * *
68756 **
68757 ** Force the value in register P1 to be an integer.  If the value
68758 ** in P1 is not an integer and cannot be converted into an integer
68759 ** without data loss, then jump immediately to P2, or if P2==0
68760 ** raise an SQLITE_MISMATCH exception.
68761 */
68762 case OP_MustBeInt: {            /* jump, in1 */
68763   pIn1 = &aMem[pOp->p1];
68764   if( (pIn1->flags & MEM_Int)==0 ){
68765     applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
68766     VdbeBranchTaken((pIn1->flags&MEM_Int)==0, 2);
68767     if( (pIn1->flags & MEM_Int)==0 ){
68768       if( pOp->p2==0 ){
68769         rc = SQLITE_MISMATCH;
68770         goto abort_due_to_error;
68771       }else{
68772         pc = pOp->p2 - 1;
68773         break;
68774       }
68775     }
68776   }
68777   MemSetTypeFlag(pIn1, MEM_Int);
68778   break;
68779 }
68780 
68781 #ifndef SQLITE_OMIT_FLOATING_POINT
68782 /* Opcode: RealAffinity P1 * * * *
68783 **
68784 ** If register P1 holds an integer convert it to a real value.
68785 **
68786 ** This opcode is used when extracting information from a column that
68787 ** has REAL affinity.  Such column values may still be stored as
68788 ** integers, for space efficiency, but after extraction we want them
68789 ** to have only a real value.
68790 */
68791 case OP_RealAffinity: {                  /* in1 */
68792   pIn1 = &aMem[pOp->p1];
68793   if( pIn1->flags & MEM_Int ){
68794     sqlite3VdbeMemRealify(pIn1);
68795   }
68796   break;
68797 }
68798 #endif
68799 
68800 #ifndef SQLITE_OMIT_CAST
68801 /* Opcode: ToText P1 * * * *
68802 **
68803 ** Force the value in register P1 to be text.
68804 ** If the value is numeric, convert it to a string using the
68805 ** equivalent of sprintf().  Blob values are unchanged and
68806 ** are afterwards simply interpreted as text.
68807 **
68808 ** A NULL value is not changed by this routine.  It remains NULL.
68809 */
68810 case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
68811   pIn1 = &aMem[pOp->p1];
68812   memAboutToChange(p, pIn1);
68813   if( pIn1->flags & MEM_Null ) break;
68814   assert( MEM_Str==(MEM_Blob>>3) );
68815   pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
68816   applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
68817   rc = ExpandBlob(pIn1);
68818   assert( pIn1->flags & MEM_Str || db->mallocFailed );
68819   pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
68820   UPDATE_MAX_BLOBSIZE(pIn1);
68821   break;
68822 }
68823 
68824 /* Opcode: ToBlob P1 * * * *
68825 **
68826 ** Force the value in register P1 to be a BLOB.
68827 ** If the value is numeric, convert it to a string first.
68828 ** Strings are simply reinterpreted as blobs with no change
68829 ** to the underlying data.
68830 **
68831 ** A NULL value is not changed by this routine.  It remains NULL.
68832 */
68833 case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
68834   pIn1 = &aMem[pOp->p1];
68835   if( pIn1->flags & MEM_Null ) break;
68836   if( (pIn1->flags & MEM_Blob)==0 ){
68837     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
68838     assert( pIn1->flags & MEM_Str || db->mallocFailed );
68839     MemSetTypeFlag(pIn1, MEM_Blob);
68840   }else{
68841     pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
68842   }
68843   UPDATE_MAX_BLOBSIZE(pIn1);
68844   break;
68845 }
68846 
68847 /* Opcode: ToNumeric P1 * * * *
68848 **
68849 ** Force the value in register P1 to be numeric (either an
68850 ** integer or a floating-point number.)
68851 ** If the value is text or blob, try to convert it to an using the
68852 ** equivalent of atoi() or atof() and store 0 if no such conversion
68853 ** is possible.
68854 **
68855 ** A NULL value is not changed by this routine.  It remains NULL.
68856 */
68857 case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
68858   pIn1 = &aMem[pOp->p1];
68859   sqlite3VdbeMemNumerify(pIn1);
68860   break;
68861 }
68862 #endif /* SQLITE_OMIT_CAST */
68863 
68864 /* Opcode: ToInt P1 * * * *
68865 **
68866 ** Force the value in register P1 to be an integer.  If
68867 ** The value is currently a real number, drop its fractional part.
68868 ** If the value is text or blob, try to convert it to an integer using the
68869 ** equivalent of atoi() and store 0 if no such conversion is possible.
68870 **
68871 ** A NULL value is not changed by this routine.  It remains NULL.
68872 */
68873 case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
68874   pIn1 = &aMem[pOp->p1];
68875   if( (pIn1->flags & MEM_Null)==0 ){
68876     sqlite3VdbeMemIntegerify(pIn1);
68877   }
68878   break;
68879 }
68880 
68881 #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
68882 /* Opcode: ToReal P1 * * * *
68883 **
68884 ** Force the value in register P1 to be a floating point number.
68885 ** If The value is currently an integer, convert it.
68886 ** If the value is text or blob, try to convert it to an integer using the
68887 ** equivalent of atoi() and store 0.0 if no such conversion is possible.
68888 **
68889 ** A NULL value is not changed by this routine.  It remains NULL.
68890 */
68891 case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
68892   pIn1 = &aMem[pOp->p1];
68893   memAboutToChange(p, pIn1);
68894   if( (pIn1->flags & MEM_Null)==0 ){
68895     sqlite3VdbeMemRealify(pIn1);
68896   }
68897   break;
68898 }
68899 #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
68900 
68901 /* Opcode: Lt P1 P2 P3 P4 P5
68902 ** Synopsis: if r[P1]<r[P3] goto P2
68903 **
68904 ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
68905 ** jump to address P2.
68906 **
68907 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
68908 ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL
68909 ** bit is clear then fall through if either operand is NULL.
68910 **
68911 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
68912 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
68913 ** to coerce both inputs according to this affinity before the
68914 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
68915 ** affinity is used. Note that the affinity conversions are stored
68916 ** back into the input registers P1 and P3.  So this opcode can cause
68917 ** persistent changes to registers P1 and P3.
68918 **
68919 ** Once any conversions have taken place, and neither value is NULL,
68920 ** the values are compared. If both values are blobs then memcmp() is
68921 ** used to determine the results of the comparison.  If both values
68922 ** are text, then the appropriate collating function specified in
68923 ** P4 is  used to do the comparison.  If P4 is not specified then
68924 ** memcmp() is used to compare text string.  If both values are
68925 ** numeric, then a numeric comparison is used. If the two values
68926 ** are of different types, then numbers are considered less than
68927 ** strings and strings are considered less than blobs.
68928 **
68929 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
68930 ** store a boolean result (either 0, or 1, or NULL) in register P2.
68931 **
68932 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
68933 ** equal to one another, provided that they do not have their MEM_Cleared
68934 ** bit set.
68935 */
68936 /* Opcode: Ne P1 P2 P3 P4 P5
68937 ** Synopsis: if r[P1]!=r[P3] goto P2
68938 **
68939 ** This works just like the Lt opcode except that the jump is taken if
68940 ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
68941 ** additional information.
68942 **
68943 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
68944 ** true or false and is never NULL.  If both operands are NULL then the result
68945 ** of comparison is false.  If either operand is NULL then the result is true.
68946 ** If neither operand is NULL the result is the same as it would be if
68947 ** the SQLITE_NULLEQ flag were omitted from P5.
68948 */
68949 /* Opcode: Eq P1 P2 P3 P4 P5
68950 ** Synopsis: if r[P1]==r[P3] goto P2
68951 **
68952 ** This works just like the Lt opcode except that the jump is taken if
68953 ** the operands in registers P1 and P3 are equal.
68954 ** See the Lt opcode for additional information.
68955 **
68956 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
68957 ** true or false and is never NULL.  If both operands are NULL then the result
68958 ** of comparison is true.  If either operand is NULL then the result is false.
68959 ** If neither operand is NULL the result is the same as it would be if
68960 ** the SQLITE_NULLEQ flag were omitted from P5.
68961 */
68962 /* Opcode: Le P1 P2 P3 P4 P5
68963 ** Synopsis: if r[P1]<=r[P3] goto P2
68964 **
68965 ** This works just like the Lt opcode except that the jump is taken if
68966 ** the content of register P3 is less than or equal to the content of
68967 ** register P1.  See the Lt opcode for additional information.
68968 */
68969 /* Opcode: Gt P1 P2 P3 P4 P5
68970 ** Synopsis: if r[P1]>r[P3] goto P2
68971 **
68972 ** This works just like the Lt opcode except that the jump is taken if
68973 ** the content of register P3 is greater than the content of
68974 ** register P1.  See the Lt opcode for additional information.
68975 */
68976 /* Opcode: Ge P1 P2 P3 P4 P5
68977 ** Synopsis: if r[P1]>=r[P3] goto P2
68978 **
68979 ** This works just like the Lt opcode except that the jump is taken if
68980 ** the content of register P3 is greater than or equal to the content of
68981 ** register P1.  See the Lt opcode for additional information.
68982 */
68983 case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
68984 case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
68985 case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
68986 case OP_Le:               /* same as TK_LE, jump, in1, in3 */
68987 case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
68988 case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
68989   int res;            /* Result of the comparison of pIn1 against pIn3 */
68990   char affinity;      /* Affinity to use for comparison */
68991   u16 flags1;         /* Copy of initial value of pIn1->flags */
68992   u16 flags3;         /* Copy of initial value of pIn3->flags */
68993 
68994   pIn1 = &aMem[pOp->p1];
68995   pIn3 = &aMem[pOp->p3];
68996   flags1 = pIn1->flags;
68997   flags3 = pIn3->flags;
68998   if( (flags1 | flags3)&MEM_Null ){
68999     /* One or both operands are NULL */
69000     if( pOp->p5 & SQLITE_NULLEQ ){
69001       /* If SQLITE_NULLEQ is set (which will only happen if the operator is
69002       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
69003       ** or not both operands are null.
69004       */
69005       assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
69006       assert( (flags1 & MEM_Cleared)==0 );
69007       assert( (pOp->p5 & SQLITE_JUMPIFNULL)==0 );
69008       if( (flags1&MEM_Null)!=0
69009        && (flags3&MEM_Null)!=0
69010        && (flags3&MEM_Cleared)==0
69011       ){
69012         res = 0;  /* Results are equal */
69013       }else{
69014         res = 1;  /* Results are not equal */
69015       }
69016     }else{
69017       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
69018       ** then the result is always NULL.
69019       ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
69020       */
69021       if( pOp->p5 & SQLITE_STOREP2 ){
69022         pOut = &aMem[pOp->p2];
69023         MemSetTypeFlag(pOut, MEM_Null);
69024         REGISTER_TRACE(pOp->p2, pOut);
69025       }else{
69026         VdbeBranchTaken(2,3);
69027         if( pOp->p5 & SQLITE_JUMPIFNULL ){
69028           pc = pOp->p2-1;
69029         }
69030       }
69031       break;
69032     }
69033   }else{
69034     /* Neither operand is NULL.  Do a comparison. */
69035     affinity = pOp->p5 & SQLITE_AFF_MASK;
69036     if( affinity ){
69037       applyAffinity(pIn1, affinity, encoding);
69038       applyAffinity(pIn3, affinity, encoding);
69039       if( db->mallocFailed ) goto no_mem;
69040     }
69041 
69042     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
69043     ExpandBlob(pIn1);
69044     ExpandBlob(pIn3);
69045     res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
69046   }
69047   switch( pOp->opcode ){
69048     case OP_Eq:    res = res==0;     break;
69049     case OP_Ne:    res = res!=0;     break;
69050     case OP_Lt:    res = res<0;      break;
69051     case OP_Le:    res = res<=0;     break;
69052     case OP_Gt:    res = res>0;      break;
69053     default:       res = res>=0;     break;
69054   }
69055 
69056   if( pOp->p5 & SQLITE_STOREP2 ){
69057     pOut = &aMem[pOp->p2];
69058     memAboutToChange(p, pOut);
69059     MemSetTypeFlag(pOut, MEM_Int);
69060     pOut->u.i = res;
69061     REGISTER_TRACE(pOp->p2, pOut);
69062   }else{
69063     VdbeBranchTaken(res!=0, (pOp->p5 & SQLITE_NULLEQ)?2:3);
69064     if( res ){
69065       pc = pOp->p2-1;
69066     }
69067   }
69068   /* Undo any changes made by applyAffinity() to the input registers. */
69069   pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
69070   pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
69071   break;
69072 }
69073 
69074 /* Opcode: Permutation * * * P4 *
69075 **
69076 ** Set the permutation used by the OP_Compare operator to be the array
69077 ** of integers in P4.
69078 **
69079 ** The permutation is only valid until the next OP_Compare that has
69080 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
69081 ** occur immediately prior to the OP_Compare.
69082 */
69083 case OP_Permutation: {
69084   assert( pOp->p4type==P4_INTARRAY );
69085   assert( pOp->p4.ai );
69086   aPermute = pOp->p4.ai;
69087   break;
69088 }
69089 
69090 /* Opcode: Compare P1 P2 P3 P4 P5
69091 **
69092 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
69093 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
69094 ** the comparison for use by the next OP_Jump instruct.
69095 **
69096 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
69097 ** determined by the most recent OP_Permutation operator.  If the
69098 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
69099 ** order.
69100 **
69101 ** P4 is a KeyInfo structure that defines collating sequences and sort
69102 ** orders for the comparison.  The permutation applies to registers
69103 ** only.  The KeyInfo elements are used sequentially.
69104 **
69105 ** The comparison is a sort comparison, so NULLs compare equal,
69106 ** NULLs are less than numbers, numbers are less than strings,
69107 ** and strings are less than blobs.
69108 */
69109 case OP_Compare: {
69110   int n;
69111   int i;
69112   int p1;
69113   int p2;
69114   const KeyInfo *pKeyInfo;
69115   int idx;
69116   CollSeq *pColl;    /* Collating sequence to use on this term */
69117   int bRev;          /* True for DESCENDING sort order */
69118 
69119   if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
69120   n = pOp->p3;
69121   pKeyInfo = pOp->p4.pKeyInfo;
69122   assert( n>0 );
69123   assert( pKeyInfo!=0 );
69124   p1 = pOp->p1;
69125   p2 = pOp->p2;
69126 #if SQLITE_DEBUG
69127   if( aPermute ){
69128     int k, mx = 0;
69129     for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
69130     assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
69131     assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
69132   }else{
69133     assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
69134     assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
69135   }
69136 #endif /* SQLITE_DEBUG */
69137   for(i=0; i<n; i++){
69138     idx = aPermute ? aPermute[i] : i;
69139     assert( memIsValid(&aMem[p1+idx]) );
69140     assert( memIsValid(&aMem[p2+idx]) );
69141     REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
69142     REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
69143     assert( i<pKeyInfo->nField );
69144     pColl = pKeyInfo->aColl[i];
69145     bRev = pKeyInfo->aSortOrder[i];
69146     iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
69147     if( iCompare ){
69148       if( bRev ) iCompare = -iCompare;
69149       break;
69150     }
69151   }
69152   aPermute = 0;
69153   break;
69154 }
69155 
69156 /* Opcode: Jump P1 P2 P3 * *
69157 **
69158 ** Jump to the instruction at address P1, P2, or P3 depending on whether
69159 ** in the most recent OP_Compare instruction the P1 vector was less than
69160 ** equal to, or greater than the P2 vector, respectively.
69161 */
69162 case OP_Jump: {             /* jump */
69163   if( iCompare<0 ){
69164     pc = pOp->p1 - 1;  VdbeBranchTaken(0,3);
69165   }else if( iCompare==0 ){
69166     pc = pOp->p2 - 1;  VdbeBranchTaken(1,3);
69167   }else{
69168     pc = pOp->p3 - 1;  VdbeBranchTaken(2,3);
69169   }
69170   break;
69171 }
69172 
69173 /* Opcode: And P1 P2 P3 * *
69174 ** Synopsis: r[P3]=(r[P1] && r[P2])
69175 **
69176 ** Take the logical AND of the values in registers P1 and P2 and
69177 ** write the result into register P3.
69178 **
69179 ** If either P1 or P2 is 0 (false) then the result is 0 even if
69180 ** the other input is NULL.  A NULL and true or two NULLs give
69181 ** a NULL output.
69182 */
69183 /* Opcode: Or P1 P2 P3 * *
69184 ** Synopsis: r[P3]=(r[P1] || r[P2])
69185 **
69186 ** Take the logical OR of the values in register P1 and P2 and
69187 ** store the answer in register P3.
69188 **
69189 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
69190 ** even if the other input is NULL.  A NULL and false or two NULLs
69191 ** give a NULL output.
69192 */
69193 case OP_And:              /* same as TK_AND, in1, in2, out3 */
69194 case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
69195   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
69196   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
69197 
69198   pIn1 = &aMem[pOp->p1];
69199   if( pIn1->flags & MEM_Null ){
69200     v1 = 2;
69201   }else{
69202     v1 = sqlite3VdbeIntValue(pIn1)!=0;
69203   }
69204   pIn2 = &aMem[pOp->p2];
69205   if( pIn2->flags & MEM_Null ){
69206     v2 = 2;
69207   }else{
69208     v2 = sqlite3VdbeIntValue(pIn2)!=0;
69209   }
69210   if( pOp->opcode==OP_And ){
69211     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
69212     v1 = and_logic[v1*3+v2];
69213   }else{
69214     static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
69215     v1 = or_logic[v1*3+v2];
69216   }
69217   pOut = &aMem[pOp->p3];
69218   if( v1==2 ){
69219     MemSetTypeFlag(pOut, MEM_Null);
69220   }else{
69221     pOut->u.i = v1;
69222     MemSetTypeFlag(pOut, MEM_Int);
69223   }
69224   break;
69225 }
69226 
69227 /* Opcode: Not P1 P2 * * *
69228 ** Synopsis: r[P2]= !r[P1]
69229 **
69230 ** Interpret the value in register P1 as a boolean value.  Store the
69231 ** boolean complement in register P2.  If the value in register P1 is
69232 ** NULL, then a NULL is stored in P2.
69233 */
69234 case OP_Not: {                /* same as TK_NOT, in1, out2 */
69235   pIn1 = &aMem[pOp->p1];
69236   pOut = &aMem[pOp->p2];
69237   if( pIn1->flags & MEM_Null ){
69238     sqlite3VdbeMemSetNull(pOut);
69239   }else{
69240     sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
69241   }
69242   break;
69243 }
69244 
69245 /* Opcode: BitNot P1 P2 * * *
69246 ** Synopsis: r[P1]= ~r[P1]
69247 **
69248 ** Interpret the content of register P1 as an integer.  Store the
69249 ** ones-complement of the P1 value into register P2.  If P1 holds
69250 ** a NULL then store a NULL in P2.
69251 */
69252 case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
69253   pIn1 = &aMem[pOp->p1];
69254   pOut = &aMem[pOp->p2];
69255   if( pIn1->flags & MEM_Null ){
69256     sqlite3VdbeMemSetNull(pOut);
69257   }else{
69258     sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
69259   }
69260   break;
69261 }
69262 
69263 /* Opcode: Once P1 P2 * * *
69264 **
69265 ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
69266 ** set the flag and fall through to the next instruction.  In other words,
69267 ** this opcode causes all following opcodes up through P2 (but not including
69268 ** P2) to run just once and to be skipped on subsequent times through the loop.
69269 */
69270 case OP_Once: {             /* jump */
69271   assert( pOp->p1<p->nOnceFlag );
69272   VdbeBranchTaken(p->aOnceFlag[pOp->p1]!=0, 2);
69273   if( p->aOnceFlag[pOp->p1] ){
69274     pc = pOp->p2-1;
69275   }else{
69276     p->aOnceFlag[pOp->p1] = 1;
69277   }
69278   break;
69279 }
69280 
69281 /* Opcode: If P1 P2 P3 * *
69282 **
69283 ** Jump to P2 if the value in register P1 is true.  The value
69284 ** is considered true if it is numeric and non-zero.  If the value
69285 ** in P1 is NULL then take the jump if P3 is non-zero.
69286 */
69287 /* Opcode: IfNot P1 P2 P3 * *
69288 **
69289 ** Jump to P2 if the value in register P1 is False.  The value
69290 ** is considered false if it has a numeric value of zero.  If the value
69291 ** in P1 is NULL then take the jump if P3 is zero.
69292 */
69293 case OP_If:                 /* jump, in1 */
69294 case OP_IfNot: {            /* jump, in1 */
69295   int c;
69296   pIn1 = &aMem[pOp->p1];
69297   if( pIn1->flags & MEM_Null ){
69298     c = pOp->p3;
69299   }else{
69300 #ifdef SQLITE_OMIT_FLOATING_POINT
69301     c = sqlite3VdbeIntValue(pIn1)!=0;
69302 #else
69303     c = sqlite3VdbeRealValue(pIn1)!=0.0;
69304 #endif
69305     if( pOp->opcode==OP_IfNot ) c = !c;
69306   }
69307   VdbeBranchTaken(c!=0, 2);
69308   if( c ){
69309     pc = pOp->p2-1;
69310   }
69311   break;
69312 }
69313 
69314 /* Opcode: IsNull P1 P2 * * *
69315 ** Synopsis:  if r[P1]==NULL goto P2
69316 **
69317 ** Jump to P2 if the value in register P1 is NULL.
69318 */
69319 case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
69320   pIn1 = &aMem[pOp->p1];
69321   VdbeBranchTaken( (pIn1->flags & MEM_Null)!=0, 2);
69322   if( (pIn1->flags & MEM_Null)!=0 ){
69323     pc = pOp->p2 - 1;
69324   }
69325   break;
69326 }
69327 
69328 /* Opcode: NotNull P1 P2 * * *
69329 ** Synopsis: if r[P1]!=NULL goto P2
69330 **
69331 ** Jump to P2 if the value in register P1 is not NULL.
69332 */
69333 case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
69334   pIn1 = &aMem[pOp->p1];
69335   VdbeBranchTaken( (pIn1->flags & MEM_Null)==0, 2);
69336   if( (pIn1->flags & MEM_Null)==0 ){
69337     pc = pOp->p2 - 1;
69338   }
69339   break;
69340 }
69341 
69342 /* Opcode: Column P1 P2 P3 P4 P5
69343 ** Synopsis:  r[P3]=PX
69344 **
69345 ** Interpret the data that cursor P1 points to as a structure built using
69346 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
69347 ** information about the format of the data.)  Extract the P2-th column
69348 ** from this record.  If there are less that (P2+1)
69349 ** values in the record, extract a NULL.
69350 **
69351 ** The value extracted is stored in register P3.
69352 **
69353 ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
69354 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
69355 ** the result.
69356 **
69357 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
69358 ** then the cache of the cursor is reset prior to extracting the column.
69359 ** The first OP_Column against a pseudo-table after the value of the content
69360 ** register has changed should have this bit set.
69361 **
69362 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
69363 ** the result is guaranteed to only be used as the argument of a length()
69364 ** or typeof() function, respectively.  The loading of large blobs can be
69365 ** skipped for length() and all content loading can be skipped for typeof().
69366 */
69367 case OP_Column: {
69368   i64 payloadSize64; /* Number of bytes in the record */
69369   int p2;            /* column number to retrieve */
69370   VdbeCursor *pC;    /* The VDBE cursor */
69371   BtCursor *pCrsr;   /* The BTree cursor */
69372   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
69373   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
69374   int len;           /* The length of the serialized data for the column */
69375   int i;             /* Loop counter */
69376   Mem *pDest;        /* Where to write the extracted value */
69377   Mem sMem;          /* For storing the record being decoded */
69378   const u8 *zData;   /* Part of the record being decoded */
69379   const u8 *zHdr;    /* Next unparsed byte of the header */
69380   const u8 *zEndHdr; /* Pointer to first byte after the header */
69381   u32 offset;        /* Offset into the data */
69382   u32 szField;       /* Number of bytes in the content of a field */
69383   u32 avail;         /* Number of bytes of available data */
69384   u32 t;             /* A type code from the record header */
69385   Mem *pReg;         /* PseudoTable input register */
69386 
69387   p2 = pOp->p2;
69388   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
69389   pDest = &aMem[pOp->p3];
69390   memAboutToChange(p, pDest);
69391   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69392   pC = p->apCsr[pOp->p1];
69393   assert( pC!=0 );
69394   assert( p2<pC->nField );
69395   aType = pC->aType;
69396   aOffset = aType + pC->nField;
69397 #ifndef SQLITE_OMIT_VIRTUALTABLE
69398   assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
69399 #endif
69400   pCrsr = pC->pCursor;
69401   assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
69402   assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
69403 
69404   /* If the cursor cache is stale, bring it up-to-date */
69405   rc = sqlite3VdbeCursorMoveto(pC);
69406   if( rc ) goto abort_due_to_error;
69407   if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
69408     if( pC->nullRow ){
69409       if( pCrsr==0 ){
69410         assert( pC->pseudoTableReg>0 );
69411         pReg = &aMem[pC->pseudoTableReg];
69412         assert( pReg->flags & MEM_Blob );
69413         assert( memIsValid(pReg) );
69414         pC->payloadSize = pC->szRow = avail = pReg->n;
69415         pC->aRow = (u8*)pReg->z;
69416       }else{
69417         MemSetTypeFlag(pDest, MEM_Null);
69418         goto op_column_out;
69419       }
69420     }else{
69421       assert( pCrsr );
69422       if( pC->isTable==0 ){
69423         assert( sqlite3BtreeCursorIsValid(pCrsr) );
69424         VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
69425         assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
69426         /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
69427         ** payload size, so it is impossible for payloadSize64 to be
69428         ** larger than 32 bits. */
69429         assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
69430         pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
69431         pC->payloadSize = (u32)payloadSize64;
69432       }else{
69433         assert( sqlite3BtreeCursorIsValid(pCrsr) );
69434         VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
69435         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
69436         pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
69437       }
69438       assert( avail<=65536 );  /* Maximum page size is 64KiB */
69439       if( pC->payloadSize <= (u32)avail ){
69440         pC->szRow = pC->payloadSize;
69441       }else{
69442         pC->szRow = avail;
69443       }
69444       if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
69445         goto too_big;
69446       }
69447     }
69448     pC->cacheStatus = p->cacheCtr;
69449     pC->iHdrOffset = getVarint32(pC->aRow, offset);
69450     pC->nHdrParsed = 0;
69451     aOffset[0] = offset;
69452     if( avail<offset ){
69453       /* pC->aRow does not have to hold the entire row, but it does at least
69454       ** need to cover the header of the record.  If pC->aRow does not contain
69455       ** the complete header, then set it to zero, forcing the header to be
69456       ** dynamically allocated. */
69457       pC->aRow = 0;
69458       pC->szRow = 0;
69459     }
69460 
69461     /* Make sure a corrupt database has not given us an oversize header.
69462     ** Do this now to avoid an oversize memory allocation.
69463     **
69464     ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
69465     ** types use so much data space that there can only be 4096 and 32 of
69466     ** them, respectively.  So the maximum header length results from a
69467     ** 3-byte type for each of the maximum of 32768 columns plus three
69468     ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
69469     */
69470     if( offset > 98307 || offset > pC->payloadSize ){
69471       rc = SQLITE_CORRUPT_BKPT;
69472       goto op_column_error;
69473     }
69474   }
69475 
69476   /* Make sure at least the first p2+1 entries of the header have been
69477   ** parsed and valid information is in aOffset[] and aType[].
69478   */
69479   if( pC->nHdrParsed<=p2 ){
69480     /* If there is more header available for parsing in the record, try
69481     ** to extract additional fields up through the p2+1-th field
69482     */
69483     if( pC->iHdrOffset<aOffset[0] ){
69484       /* Make sure zData points to enough of the record to cover the header. */
69485       if( pC->aRow==0 ){
69486         memset(&sMem, 0, sizeof(sMem));
69487         rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0],
69488                                      !pC->isTable, &sMem);
69489         if( rc!=SQLITE_OK ){
69490           goto op_column_error;
69491         }
69492         zData = (u8*)sMem.z;
69493       }else{
69494         zData = pC->aRow;
69495       }
69496 
69497       /* Fill in aType[i] and aOffset[i] values through the p2-th field. */
69498       i = pC->nHdrParsed;
69499       offset = aOffset[i];
69500       zHdr = zData + pC->iHdrOffset;
69501       zEndHdr = zData + aOffset[0];
69502       assert( i<=p2 && zHdr<zEndHdr );
69503       do{
69504         if( zHdr[0]<0x80 ){
69505           t = zHdr[0];
69506           zHdr++;
69507         }else{
69508           zHdr += sqlite3GetVarint32(zHdr, &t);
69509         }
69510         aType[i] = t;
69511         szField = sqlite3VdbeSerialTypeLen(t);
69512         offset += szField;
69513         if( offset<szField ){  /* True if offset overflows */
69514           zHdr = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
69515           break;
69516         }
69517         i++;
69518         aOffset[i] = offset;
69519       }while( i<=p2 && zHdr<zEndHdr );
69520       pC->nHdrParsed = i;
69521       pC->iHdrOffset = (u32)(zHdr - zData);
69522       if( pC->aRow==0 ){
69523         sqlite3VdbeMemRelease(&sMem);
69524         sMem.flags = MEM_Null;
69525       }
69526 
69527       /* If we have read more header data than was contained in the header,
69528       ** or if the end of the last field appears to be past the end of the
69529       ** record, or if the end of the last field appears to be before the end
69530       ** of the record (when all fields present), then we must be dealing
69531       ** with a corrupt database.
69532       */
69533       if( (zHdr > zEndHdr)
69534        || (offset > pC->payloadSize)
69535        || (zHdr==zEndHdr && offset!=pC->payloadSize)
69536       ){
69537         rc = SQLITE_CORRUPT_BKPT;
69538         goto op_column_error;
69539       }
69540     }
69541 
69542     /* If after trying to extra new entries from the header, nHdrParsed is
69543     ** still not up to p2, that means that the record has fewer than p2
69544     ** columns.  So the result will be either the default value or a NULL.
69545     */
69546     if( pC->nHdrParsed<=p2 ){
69547       if( pOp->p4type==P4_MEM ){
69548         sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
69549       }else{
69550         MemSetTypeFlag(pDest, MEM_Null);
69551       }
69552       goto op_column_out;
69553     }
69554   }
69555 
69556   /* Extract the content for the p2+1-th column.  Control can only
69557   ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
69558   ** all valid.
69559   */
69560   assert( p2<pC->nHdrParsed );
69561   assert( rc==SQLITE_OK );
69562   assert( sqlite3VdbeCheckMemInvariants(pDest) );
69563   if( pC->szRow>=aOffset[p2+1] ){
69564     /* This is the common case where the desired content fits on the original
69565     ** page - where the content is not on an overflow page */
69566     VdbeMemRelease(pDest);
69567     sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
69568   }else{
69569     /* This branch happens only when content is on overflow pages */
69570     t = aType[p2];
69571     if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
69572           && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
69573      || (len = sqlite3VdbeSerialTypeLen(t))==0
69574     ){
69575       /* Content is irrelevant for the typeof() function and for
69576       ** the length(X) function if X is a blob.  So we might as well use
69577       ** bogus content rather than reading content from disk.  NULL works
69578       ** for text and blob and whatever is in the payloadSize64 variable
69579       ** will work for everything else.  Content is also irrelevant if
69580       ** the content length is 0. */
69581       zData = t<=13 ? (u8*)&payloadSize64 : 0;
69582       sMem.zMalloc = 0;
69583     }else{
69584       memset(&sMem, 0, sizeof(sMem));
69585       sqlite3VdbeMemMove(&sMem, pDest);
69586       rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
69587                                    &sMem);
69588       if( rc!=SQLITE_OK ){
69589         goto op_column_error;
69590       }
69591       zData = (u8*)sMem.z;
69592     }
69593     sqlite3VdbeSerialGet(zData, t, pDest);
69594     /* If we dynamically allocated space to hold the data (in the
69595     ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
69596     ** dynamically allocated space over to the pDest structure.
69597     ** This prevents a memory copy. */
69598     if( sMem.zMalloc ){
69599       assert( sMem.z==sMem.zMalloc );
69600       assert( VdbeMemDynamic(pDest)==0 );
69601       assert( (pDest->flags & (MEM_Blob|MEM_Str))==0 || pDest->z==sMem.z );
69602       pDest->flags &= ~(MEM_Ephem|MEM_Static);
69603       pDest->flags |= MEM_Term;
69604       pDest->z = sMem.z;
69605       pDest->zMalloc = sMem.zMalloc;
69606     }
69607   }
69608   pDest->enc = encoding;
69609 
69610 op_column_out:
69611   Deephemeralize(pDest);
69612 op_column_error:
69613   UPDATE_MAX_BLOBSIZE(pDest);
69614   REGISTER_TRACE(pOp->p3, pDest);
69615   break;
69616 }
69617 
69618 /* Opcode: Affinity P1 P2 * P4 *
69619 ** Synopsis: affinity(r[P1@P2])
69620 **
69621 ** Apply affinities to a range of P2 registers starting with P1.
69622 **
69623 ** P4 is a string that is P2 characters long. The nth character of the
69624 ** string indicates the column affinity that should be used for the nth
69625 ** memory cell in the range.
69626 */
69627 case OP_Affinity: {
69628   const char *zAffinity;   /* The affinity to be applied */
69629   char cAff;               /* A single character of affinity */
69630 
69631   zAffinity = pOp->p4.z;
69632   assert( zAffinity!=0 );
69633   assert( zAffinity[pOp->p2]==0 );
69634   pIn1 = &aMem[pOp->p1];
69635   while( (cAff = *(zAffinity++))!=0 ){
69636     assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
69637     assert( memIsValid(pIn1) );
69638     applyAffinity(pIn1, cAff, encoding);
69639     pIn1++;
69640   }
69641   break;
69642 }
69643 
69644 /* Opcode: MakeRecord P1 P2 P3 P4 *
69645 ** Synopsis: r[P3]=mkrec(r[P1@P2])
69646 **
69647 ** Convert P2 registers beginning with P1 into the [record format]
69648 ** use as a data record in a database table or as a key
69649 ** in an index.  The OP_Column opcode can decode the record later.
69650 **
69651 ** P4 may be a string that is P2 characters long.  The nth character of the
69652 ** string indicates the column affinity that should be used for the nth
69653 ** field of the index key.
69654 **
69655 ** The mapping from character to affinity is given by the SQLITE_AFF_
69656 ** macros defined in sqliteInt.h.
69657 **
69658 ** If P4 is NULL then all index fields have the affinity NONE.
69659 */
69660 case OP_MakeRecord: {
69661   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
69662   Mem *pRec;             /* The new record */
69663   u64 nData;             /* Number of bytes of data space */
69664   int nHdr;              /* Number of bytes of header space */
69665   i64 nByte;             /* Data space required for this record */
69666   int nZero;             /* Number of zero bytes at the end of the record */
69667   int nVarint;           /* Number of bytes in a varint */
69668   u32 serial_type;       /* Type field */
69669   Mem *pData0;           /* First field to be combined into the record */
69670   Mem *pLast;            /* Last field of the record */
69671   int nField;            /* Number of fields in the record */
69672   char *zAffinity;       /* The affinity string for the record */
69673   int file_format;       /* File format to use for encoding */
69674   int i;                 /* Space used in zNewRecord[] header */
69675   int j;                 /* Space used in zNewRecord[] content */
69676   int len;               /* Length of a field */
69677 
69678   /* Assuming the record contains N fields, the record format looks
69679   ** like this:
69680   **
69681   ** ------------------------------------------------------------------------
69682   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
69683   ** ------------------------------------------------------------------------
69684   **
69685   ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
69686   ** and so froth.
69687   **
69688   ** Each type field is a varint representing the serial type of the
69689   ** corresponding data element (see sqlite3VdbeSerialType()). The
69690   ** hdr-size field is also a varint which is the offset from the beginning
69691   ** of the record to data0.
69692   */
69693   nData = 0;         /* Number of bytes of data space */
69694   nHdr = 0;          /* Number of bytes of header space */
69695   nZero = 0;         /* Number of zero bytes at the end of the record */
69696   nField = pOp->p1;
69697   zAffinity = pOp->p4.z;
69698   assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
69699   pData0 = &aMem[nField];
69700   nField = pOp->p2;
69701   pLast = &pData0[nField-1];
69702   file_format = p->minWriteFileFormat;
69703 
69704   /* Identify the output register */
69705   assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
69706   pOut = &aMem[pOp->p3];
69707   memAboutToChange(p, pOut);
69708 
69709   /* Apply the requested affinity to all inputs
69710   */
69711   assert( pData0<=pLast );
69712   if( zAffinity ){
69713     pRec = pData0;
69714     do{
69715       applyAffinity(pRec++, *(zAffinity++), encoding);
69716       assert( zAffinity[0]==0 || pRec<=pLast );
69717     }while( zAffinity[0] );
69718   }
69719 
69720   /* Loop through the elements that will make up the record to figure
69721   ** out how much space is required for the new record.
69722   */
69723   pRec = pLast;
69724   do{
69725     assert( memIsValid(pRec) );
69726     serial_type = sqlite3VdbeSerialType(pRec, file_format);
69727     len = sqlite3VdbeSerialTypeLen(serial_type);
69728     if( pRec->flags & MEM_Zero ){
69729       if( nData ){
69730         sqlite3VdbeMemExpandBlob(pRec);
69731       }else{
69732         nZero += pRec->u.nZero;
69733         len -= pRec->u.nZero;
69734       }
69735     }
69736     nData += len;
69737     testcase( serial_type==127 );
69738     testcase( serial_type==128 );
69739     nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
69740   }while( (--pRec)>=pData0 );
69741 
69742   /* Add the initial header varint and total the size */
69743   testcase( nHdr==126 );
69744   testcase( nHdr==127 );
69745   if( nHdr<=126 ){
69746     /* The common case */
69747     nHdr += 1;
69748   }else{
69749     /* Rare case of a really large header */
69750     nVarint = sqlite3VarintLen(nHdr);
69751     nHdr += nVarint;
69752     if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
69753   }
69754   nByte = nHdr+nData;
69755   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
69756     goto too_big;
69757   }
69758 
69759   /* Make sure the output register has a buffer large enough to store
69760   ** the new record. The output register (pOp->p3) is not allowed to
69761   ** be one of the input registers (because the following call to
69762   ** sqlite3VdbeMemGrow() could clobber the value before it is used).
69763   */
69764   if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
69765     goto no_mem;
69766   }
69767   zNewRecord = (u8 *)pOut->z;
69768 
69769   /* Write the record */
69770   i = putVarint32(zNewRecord, nHdr);
69771   j = nHdr;
69772   assert( pData0<=pLast );
69773   pRec = pData0;
69774   do{
69775     serial_type = sqlite3VdbeSerialType(pRec, file_format);
69776     i += putVarint32(&zNewRecord[i], serial_type);            /* serial type */
69777     j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
69778   }while( (++pRec)<=pLast );
69779   assert( i==nHdr );
69780   assert( j==nByte );
69781 
69782   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
69783   pOut->n = (int)nByte;
69784   pOut->flags = MEM_Blob;
69785   pOut->xDel = 0;
69786   if( nZero ){
69787     pOut->u.nZero = nZero;
69788     pOut->flags |= MEM_Zero;
69789   }
69790   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
69791   REGISTER_TRACE(pOp->p3, pOut);
69792   UPDATE_MAX_BLOBSIZE(pOut);
69793   break;
69794 }
69795 
69796 /* Opcode: Count P1 P2 * * *
69797 ** Synopsis: r[P2]=count()
69798 **
69799 ** Store the number of entries (an integer value) in the table or index
69800 ** opened by cursor P1 in register P2
69801 */
69802 #ifndef SQLITE_OMIT_BTREECOUNT
69803 case OP_Count: {         /* out2-prerelease */
69804   i64 nEntry;
69805   BtCursor *pCrsr;
69806 
69807   pCrsr = p->apCsr[pOp->p1]->pCursor;
69808   assert( pCrsr );
69809   nEntry = 0;  /* Not needed.  Only used to silence a warning. */
69810   rc = sqlite3BtreeCount(pCrsr, &nEntry);
69811   pOut->u.i = nEntry;
69812   break;
69813 }
69814 #endif
69815 
69816 /* Opcode: Savepoint P1 * * P4 *
69817 **
69818 ** Open, release or rollback the savepoint named by parameter P4, depending
69819 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
69820 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
69821 */
69822 case OP_Savepoint: {
69823   int p1;                         /* Value of P1 operand */
69824   char *zName;                    /* Name of savepoint */
69825   int nName;
69826   Savepoint *pNew;
69827   Savepoint *pSavepoint;
69828   Savepoint *pTmp;
69829   int iSavepoint;
69830   int ii;
69831 
69832   p1 = pOp->p1;
69833   zName = pOp->p4.z;
69834 
69835   /* Assert that the p1 parameter is valid. Also that if there is no open
69836   ** transaction, then there cannot be any savepoints.
69837   */
69838   assert( db->pSavepoint==0 || db->autoCommit==0 );
69839   assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
69840   assert( db->pSavepoint || db->isTransactionSavepoint==0 );
69841   assert( checkSavepointCount(db) );
69842   assert( p->bIsReader );
69843 
69844   if( p1==SAVEPOINT_BEGIN ){
69845     if( db->nVdbeWrite>0 ){
69846       /* A new savepoint cannot be created if there are active write
69847       ** statements (i.e. open read/write incremental blob handles).
69848       */
69849       sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
69850         "SQL statements in progress");
69851       rc = SQLITE_BUSY;
69852     }else{
69853       nName = sqlite3Strlen30(zName);
69854 
69855 #ifndef SQLITE_OMIT_VIRTUALTABLE
69856       /* This call is Ok even if this savepoint is actually a transaction
69857       ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
69858       ** If this is a transaction savepoint being opened, it is guaranteed
69859       ** that the db->aVTrans[] array is empty.  */
69860       assert( db->autoCommit==0 || db->nVTrans==0 );
69861       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
69862                                 db->nStatement+db->nSavepoint);
69863       if( rc!=SQLITE_OK ) goto abort_due_to_error;
69864 #endif
69865 
69866       /* Create a new savepoint structure. */
69867       pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
69868       if( pNew ){
69869         pNew->zName = (char *)&pNew[1];
69870         memcpy(pNew->zName, zName, nName+1);
69871 
69872         /* If there is no open transaction, then mark this as a special
69873         ** "transaction savepoint". */
69874         if( db->autoCommit ){
69875           db->autoCommit = 0;
69876           db->isTransactionSavepoint = 1;
69877         }else{
69878           db->nSavepoint++;
69879         }
69880 
69881         /* Link the new savepoint into the database handle's list. */
69882         pNew->pNext = db->pSavepoint;
69883         db->pSavepoint = pNew;
69884         pNew->nDeferredCons = db->nDeferredCons;
69885         pNew->nDeferredImmCons = db->nDeferredImmCons;
69886       }
69887     }
69888   }else{
69889     iSavepoint = 0;
69890 
69891     /* Find the named savepoint. If there is no such savepoint, then an
69892     ** an error is returned to the user.  */
69893     for(
69894       pSavepoint = db->pSavepoint;
69895       pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
69896       pSavepoint = pSavepoint->pNext
69897     ){
69898       iSavepoint++;
69899     }
69900     if( !pSavepoint ){
69901       sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
69902       rc = SQLITE_ERROR;
69903     }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
69904       /* It is not possible to release (commit) a savepoint if there are
69905       ** active write statements.
69906       */
69907       sqlite3SetString(&p->zErrMsg, db,
69908         "cannot release savepoint - SQL statements in progress"
69909       );
69910       rc = SQLITE_BUSY;
69911     }else{
69912 
69913       /* Determine whether or not this is a transaction savepoint. If so,
69914       ** and this is a RELEASE command, then the current transaction
69915       ** is committed.
69916       */
69917       int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
69918       if( isTransaction && p1==SAVEPOINT_RELEASE ){
69919         if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
69920           goto vdbe_return;
69921         }
69922         db->autoCommit = 1;
69923         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
69924           p->pc = pc;
69925           db->autoCommit = 0;
69926           p->rc = rc = SQLITE_BUSY;
69927           goto vdbe_return;
69928         }
69929         db->isTransactionSavepoint = 0;
69930         rc = p->rc;
69931       }else{
69932         iSavepoint = db->nSavepoint - iSavepoint - 1;
69933         if( p1==SAVEPOINT_ROLLBACK ){
69934           for(ii=0; ii<db->nDb; ii++){
69935             sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
69936           }
69937         }
69938         for(ii=0; ii<db->nDb; ii++){
69939           rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
69940           if( rc!=SQLITE_OK ){
69941             goto abort_due_to_error;
69942           }
69943         }
69944         if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
69945           sqlite3ExpirePreparedStatements(db);
69946           sqlite3ResetAllSchemasOfConnection(db);
69947           db->flags = (db->flags | SQLITE_InternChanges);
69948         }
69949       }
69950 
69951       /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
69952       ** savepoints nested inside of the savepoint being operated on. */
69953       while( db->pSavepoint!=pSavepoint ){
69954         pTmp = db->pSavepoint;
69955         db->pSavepoint = pTmp->pNext;
69956         sqlite3DbFree(db, pTmp);
69957         db->nSavepoint--;
69958       }
69959 
69960       /* If it is a RELEASE, then destroy the savepoint being operated on
69961       ** too. If it is a ROLLBACK TO, then set the number of deferred
69962       ** constraint violations present in the database to the value stored
69963       ** when the savepoint was created.  */
69964       if( p1==SAVEPOINT_RELEASE ){
69965         assert( pSavepoint==db->pSavepoint );
69966         db->pSavepoint = pSavepoint->pNext;
69967         sqlite3DbFree(db, pSavepoint);
69968         if( !isTransaction ){
69969           db->nSavepoint--;
69970         }
69971       }else{
69972         db->nDeferredCons = pSavepoint->nDeferredCons;
69973         db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
69974       }
69975 
69976       if( !isTransaction ){
69977         rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
69978         if( rc!=SQLITE_OK ) goto abort_due_to_error;
69979       }
69980     }
69981   }
69982 
69983   break;
69984 }
69985 
69986 /* Opcode: AutoCommit P1 P2 * * *
69987 **
69988 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
69989 ** back any currently active btree transactions. If there are any active
69990 ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
69991 ** there are active writing VMs or active VMs that use shared cache.
69992 **
69993 ** This instruction causes the VM to halt.
69994 */
69995 case OP_AutoCommit: {
69996   int desiredAutoCommit;
69997   int iRollback;
69998   int turnOnAC;
69999 
70000   desiredAutoCommit = pOp->p1;
70001   iRollback = pOp->p2;
70002   turnOnAC = desiredAutoCommit && !db->autoCommit;
70003   assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
70004   assert( desiredAutoCommit==1 || iRollback==0 );
70005   assert( db->nVdbeActive>0 );  /* At least this one VM is active */
70006   assert( p->bIsReader );
70007 
70008 #if 0
70009   if( turnOnAC && iRollback && db->nVdbeActive>1 ){
70010     /* If this instruction implements a ROLLBACK and other VMs are
70011     ** still running, and a transaction is active, return an error indicating
70012     ** that the other VMs must complete first.
70013     */
70014     sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
70015         "SQL statements in progress");
70016     rc = SQLITE_BUSY;
70017   }else
70018 #endif
70019   if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
70020     /* If this instruction implements a COMMIT and other VMs are writing
70021     ** return an error indicating that the other VMs must complete first.
70022     */
70023     sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
70024         "SQL statements in progress");
70025     rc = SQLITE_BUSY;
70026   }else if( desiredAutoCommit!=db->autoCommit ){
70027     if( iRollback ){
70028       assert( desiredAutoCommit==1 );
70029       sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
70030       db->autoCommit = 1;
70031     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
70032       goto vdbe_return;
70033     }else{
70034       db->autoCommit = (u8)desiredAutoCommit;
70035       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
70036         p->pc = pc;
70037         db->autoCommit = (u8)(1-desiredAutoCommit);
70038         p->rc = rc = SQLITE_BUSY;
70039         goto vdbe_return;
70040       }
70041     }
70042     assert( db->nStatement==0 );
70043     sqlite3CloseSavepoints(db);
70044     if( p->rc==SQLITE_OK ){
70045       rc = SQLITE_DONE;
70046     }else{
70047       rc = SQLITE_ERROR;
70048     }
70049     goto vdbe_return;
70050   }else{
70051     sqlite3SetString(&p->zErrMsg, db,
70052         (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
70053         (iRollback)?"cannot rollback - no transaction is active":
70054                    "cannot commit - no transaction is active"));
70055 
70056     rc = SQLITE_ERROR;
70057   }
70058   break;
70059 }
70060 
70061 /* Opcode: Transaction P1 P2 P3 P4 P5
70062 **
70063 ** Begin a transaction on database P1 if a transaction is not already
70064 ** active.
70065 ** If P2 is non-zero, then a write-transaction is started, or if a
70066 ** read-transaction is already active, it is upgraded to a write-transaction.
70067 ** If P2 is zero, then a read-transaction is started.
70068 **
70069 ** P1 is the index of the database file on which the transaction is
70070 ** started.  Index 0 is the main database file and index 1 is the
70071 ** file used for temporary tables.  Indices of 2 or more are used for
70072 ** attached databases.
70073 **
70074 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
70075 ** true (this flag is set if the Vdbe may modify more than one row and may
70076 ** throw an ABORT exception), a statement transaction may also be opened.
70077 ** More specifically, a statement transaction is opened iff the database
70078 ** connection is currently not in autocommit mode, or if there are other
70079 ** active statements. A statement transaction allows the changes made by this
70080 ** VDBE to be rolled back after an error without having to roll back the
70081 ** entire transaction. If no error is encountered, the statement transaction
70082 ** will automatically commit when the VDBE halts.
70083 **
70084 ** If P5!=0 then this opcode also checks the schema cookie against P3
70085 ** and the schema generation counter against P4.
70086 ** The cookie changes its value whenever the database schema changes.
70087 ** This operation is used to detect when that the cookie has changed
70088 ** and that the current process needs to reread the schema.  If the schema
70089 ** cookie in P3 differs from the schema cookie in the database header or
70090 ** if the schema generation counter in P4 differs from the current
70091 ** generation counter, then an SQLITE_SCHEMA error is raised and execution
70092 ** halts.  The sqlite3_step() wrapper function might then reprepare the
70093 ** statement and rerun it from the beginning.
70094 */
70095 case OP_Transaction: {
70096   Btree *pBt;
70097   int iMeta;
70098   int iGen;
70099 
70100   assert( p->bIsReader );
70101   assert( p->readOnly==0 || pOp->p2==0 );
70102   assert( pOp->p1>=0 && pOp->p1<db->nDb );
70103   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
70104   if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
70105     rc = SQLITE_READONLY;
70106     goto abort_due_to_error;
70107   }
70108   pBt = db->aDb[pOp->p1].pBt;
70109 
70110   if( pBt ){
70111     rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
70112     if( rc==SQLITE_BUSY ){
70113       p->pc = pc;
70114       p->rc = rc = SQLITE_BUSY;
70115       goto vdbe_return;
70116     }
70117     if( rc!=SQLITE_OK ){
70118       goto abort_due_to_error;
70119     }
70120 
70121     if( pOp->p2 && p->usesStmtJournal
70122      && (db->autoCommit==0 || db->nVdbeRead>1)
70123     ){
70124       assert( sqlite3BtreeIsInTrans(pBt) );
70125       if( p->iStatement==0 ){
70126         assert( db->nStatement>=0 && db->nSavepoint>=0 );
70127         db->nStatement++;
70128         p->iStatement = db->nSavepoint + db->nStatement;
70129       }
70130 
70131       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
70132       if( rc==SQLITE_OK ){
70133         rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
70134       }
70135 
70136       /* Store the current value of the database handles deferred constraint
70137       ** counter. If the statement transaction needs to be rolled back,
70138       ** the value of this counter needs to be restored too.  */
70139       p->nStmtDefCons = db->nDeferredCons;
70140       p->nStmtDefImmCons = db->nDeferredImmCons;
70141     }
70142 
70143     /* Gather the schema version number for checking */
70144     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
70145     iGen = db->aDb[pOp->p1].pSchema->iGeneration;
70146   }else{
70147     iGen = iMeta = 0;
70148   }
70149   assert( pOp->p5==0 || pOp->p4type==P4_INT32 );
70150   if( pOp->p5 && (iMeta!=pOp->p3 || iGen!=pOp->p4.i) ){
70151     sqlite3DbFree(db, p->zErrMsg);
70152     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
70153     /* If the schema-cookie from the database file matches the cookie
70154     ** stored with the in-memory representation of the schema, do
70155     ** not reload the schema from the database file.
70156     **
70157     ** If virtual-tables are in use, this is not just an optimization.
70158     ** Often, v-tables store their data in other SQLite tables, which
70159     ** are queried from within xNext() and other v-table methods using
70160     ** prepared queries. If such a query is out-of-date, we do not want to
70161     ** discard the database schema, as the user code implementing the
70162     ** v-table would have to be ready for the sqlite3_vtab structure itself
70163     ** to be invalidated whenever sqlite3_step() is called from within
70164     ** a v-table method.
70165     */
70166     if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
70167       sqlite3ResetOneSchema(db, pOp->p1);
70168     }
70169     p->expired = 1;
70170     rc = SQLITE_SCHEMA;
70171   }
70172   break;
70173 }
70174 
70175 /* Opcode: ReadCookie P1 P2 P3 * *
70176 **
70177 ** Read cookie number P3 from database P1 and write it into register P2.
70178 ** P3==1 is the schema version.  P3==2 is the database format.
70179 ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
70180 ** the main database file and P1==1 is the database file used to store
70181 ** temporary tables.
70182 **
70183 ** There must be a read-lock on the database (either a transaction
70184 ** must be started or there must be an open cursor) before
70185 ** executing this instruction.
70186 */
70187 case OP_ReadCookie: {               /* out2-prerelease */
70188   int iMeta;
70189   int iDb;
70190   int iCookie;
70191 
70192   assert( p->bIsReader );
70193   iDb = pOp->p1;
70194   iCookie = pOp->p3;
70195   assert( pOp->p3<SQLITE_N_BTREE_META );
70196   assert( iDb>=0 && iDb<db->nDb );
70197   assert( db->aDb[iDb].pBt!=0 );
70198   assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
70199 
70200   sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
70201   pOut->u.i = iMeta;
70202   break;
70203 }
70204 
70205 /* Opcode: SetCookie P1 P2 P3 * *
70206 **
70207 ** Write the content of register P3 (interpreted as an integer)
70208 ** into cookie number P2 of database P1.  P2==1 is the schema version.
70209 ** P2==2 is the database format. P2==3 is the recommended pager cache
70210 ** size, and so forth.  P1==0 is the main database file and P1==1 is the
70211 ** database file used to store temporary tables.
70212 **
70213 ** A transaction must be started before executing this opcode.
70214 */
70215 case OP_SetCookie: {       /* in3 */
70216   Db *pDb;
70217   assert( pOp->p2<SQLITE_N_BTREE_META );
70218   assert( pOp->p1>=0 && pOp->p1<db->nDb );
70219   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
70220   assert( p->readOnly==0 );
70221   pDb = &db->aDb[pOp->p1];
70222   assert( pDb->pBt!=0 );
70223   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
70224   pIn3 = &aMem[pOp->p3];
70225   sqlite3VdbeMemIntegerify(pIn3);
70226   /* See note about index shifting on OP_ReadCookie */
70227   rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
70228   if( pOp->p2==BTREE_SCHEMA_VERSION ){
70229     /* When the schema cookie changes, record the new cookie internally */
70230     pDb->pSchema->schema_cookie = (int)pIn3->u.i;
70231     db->flags |= SQLITE_InternChanges;
70232   }else if( pOp->p2==BTREE_FILE_FORMAT ){
70233     /* Record changes in the file format */
70234     pDb->pSchema->file_format = (u8)pIn3->u.i;
70235   }
70236   if( pOp->p1==1 ){
70237     /* Invalidate all prepared statements whenever the TEMP database
70238     ** schema is changed.  Ticket #1644 */
70239     sqlite3ExpirePreparedStatements(db);
70240     p->expired = 0;
70241   }
70242   break;
70243 }
70244 
70245 /* Opcode: OpenRead P1 P2 P3 P4 P5
70246 ** Synopsis: root=P2 iDb=P3
70247 **
70248 ** Open a read-only cursor for the database table whose root page is
70249 ** P2 in a database file.  The database file is determined by P3.
70250 ** P3==0 means the main database, P3==1 means the database used for
70251 ** temporary tables, and P3>1 means used the corresponding attached
70252 ** database.  Give the new cursor an identifier of P1.  The P1
70253 ** values need not be contiguous but all P1 values should be small integers.
70254 ** It is an error for P1 to be negative.
70255 **
70256 ** If P5!=0 then use the content of register P2 as the root page, not
70257 ** the value of P2 itself.
70258 **
70259 ** There will be a read lock on the database whenever there is an
70260 ** open cursor.  If the database was unlocked prior to this instruction
70261 ** then a read lock is acquired as part of this instruction.  A read
70262 ** lock allows other processes to read the database but prohibits
70263 ** any other process from modifying the database.  The read lock is
70264 ** released when all cursors are closed.  If this instruction attempts
70265 ** to get a read lock but fails, the script terminates with an
70266 ** SQLITE_BUSY error code.
70267 **
70268 ** The P4 value may be either an integer (P4_INT32) or a pointer to
70269 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
70270 ** structure, then said structure defines the content and collating
70271 ** sequence of the index being opened. Otherwise, if P4 is an integer
70272 ** value, it is set to the number of columns in the table.
70273 **
70274 ** See also OpenWrite.
70275 */
70276 /* Opcode: OpenWrite P1 P2 P3 P4 P5
70277 ** Synopsis: root=P2 iDb=P3
70278 **
70279 ** Open a read/write cursor named P1 on the table or index whose root
70280 ** page is P2.  Or if P5!=0 use the content of register P2 to find the
70281 ** root page.
70282 **
70283 ** The P4 value may be either an integer (P4_INT32) or a pointer to
70284 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
70285 ** structure, then said structure defines the content and collating
70286 ** sequence of the index being opened. Otherwise, if P4 is an integer
70287 ** value, it is set to the number of columns in the table, or to the
70288 ** largest index of any column of the table that is actually used.
70289 **
70290 ** This instruction works just like OpenRead except that it opens the cursor
70291 ** in read/write mode.  For a given table, there can be one or more read-only
70292 ** cursors or a single read/write cursor but not both.
70293 **
70294 ** See also OpenRead.
70295 */
70296 case OP_OpenRead:
70297 case OP_OpenWrite: {
70298   int nField;
70299   KeyInfo *pKeyInfo;
70300   int p2;
70301   int iDb;
70302   int wrFlag;
70303   Btree *pX;
70304   VdbeCursor *pCur;
70305   Db *pDb;
70306 
70307   assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
70308   assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
70309   assert( p->bIsReader );
70310   assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
70311 
70312   if( p->expired ){
70313     rc = SQLITE_ABORT;
70314     break;
70315   }
70316 
70317   nField = 0;
70318   pKeyInfo = 0;
70319   p2 = pOp->p2;
70320   iDb = pOp->p3;
70321   assert( iDb>=0 && iDb<db->nDb );
70322   assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
70323   pDb = &db->aDb[iDb];
70324   pX = pDb->pBt;
70325   assert( pX!=0 );
70326   if( pOp->opcode==OP_OpenWrite ){
70327     wrFlag = 1;
70328     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
70329     if( pDb->pSchema->file_format < p->minWriteFileFormat ){
70330       p->minWriteFileFormat = pDb->pSchema->file_format;
70331     }
70332   }else{
70333     wrFlag = 0;
70334   }
70335   if( pOp->p5 & OPFLAG_P2ISREG ){
70336     assert( p2>0 );
70337     assert( p2<=(p->nMem-p->nCursor) );
70338     pIn2 = &aMem[p2];
70339     assert( memIsValid(pIn2) );
70340     assert( (pIn2->flags & MEM_Int)!=0 );
70341     sqlite3VdbeMemIntegerify(pIn2);
70342     p2 = (int)pIn2->u.i;
70343     /* The p2 value always comes from a prior OP_CreateTable opcode and
70344     ** that opcode will always set the p2 value to 2 or more or else fail.
70345     ** If there were a failure, the prepared statement would have halted
70346     ** before reaching this instruction. */
70347     if( NEVER(p2<2) ) {
70348       rc = SQLITE_CORRUPT_BKPT;
70349       goto abort_due_to_error;
70350     }
70351   }
70352   if( pOp->p4type==P4_KEYINFO ){
70353     pKeyInfo = pOp->p4.pKeyInfo;
70354     assert( pKeyInfo->enc==ENC(db) );
70355     assert( pKeyInfo->db==db );
70356     nField = pKeyInfo->nField+pKeyInfo->nXField;
70357   }else if( pOp->p4type==P4_INT32 ){
70358     nField = pOp->p4.i;
70359   }
70360   assert( pOp->p1>=0 );
70361   assert( nField>=0 );
70362   testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
70363   pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
70364   if( pCur==0 ) goto no_mem;
70365   pCur->nullRow = 1;
70366   pCur->isOrdered = 1;
70367   rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
70368   pCur->pKeyInfo = pKeyInfo;
70369   assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
70370   sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
70371 
70372   /* Since it performs no memory allocation or IO, the only value that
70373   ** sqlite3BtreeCursor() may return is SQLITE_OK. */
70374   assert( rc==SQLITE_OK );
70375 
70376   /* Set the VdbeCursor.isTable variable. Previous versions of
70377   ** SQLite used to check if the root-page flags were sane at this point
70378   ** and report database corruption if they were not, but this check has
70379   ** since moved into the btree layer.  */
70380   pCur->isTable = pOp->p4type!=P4_KEYINFO;
70381   break;
70382 }
70383 
70384 /* Opcode: OpenEphemeral P1 P2 * P4 P5
70385 ** Synopsis: nColumn=P2
70386 **
70387 ** Open a new cursor P1 to a transient table.
70388 ** The cursor is always opened read/write even if
70389 ** the main database is read-only.  The ephemeral
70390 ** table is deleted automatically when the cursor is closed.
70391 **
70392 ** P2 is the number of columns in the ephemeral table.
70393 ** The cursor points to a BTree table if P4==0 and to a BTree index
70394 ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
70395 ** that defines the format of keys in the index.
70396 **
70397 ** The P5 parameter can be a mask of the BTREE_* flags defined
70398 ** in btree.h.  These flags control aspects of the operation of
70399 ** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
70400 ** added automatically.
70401 */
70402 /* Opcode: OpenAutoindex P1 P2 * P4 *
70403 ** Synopsis: nColumn=P2
70404 **
70405 ** This opcode works the same as OP_OpenEphemeral.  It has a
70406 ** different name to distinguish its use.  Tables created using
70407 ** by this opcode will be used for automatically created transient
70408 ** indices in joins.
70409 */
70410 case OP_OpenAutoindex:
70411 case OP_OpenEphemeral: {
70412   VdbeCursor *pCx;
70413   KeyInfo *pKeyInfo;
70414 
70415   static const int vfsFlags =
70416       SQLITE_OPEN_READWRITE |
70417       SQLITE_OPEN_CREATE |
70418       SQLITE_OPEN_EXCLUSIVE |
70419       SQLITE_OPEN_DELETEONCLOSE |
70420       SQLITE_OPEN_TRANSIENT_DB;
70421   assert( pOp->p1>=0 );
70422   assert( pOp->p2>=0 );
70423   pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
70424   if( pCx==0 ) goto no_mem;
70425   pCx->nullRow = 1;
70426   rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
70427                         BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
70428   if( rc==SQLITE_OK ){
70429     rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
70430   }
70431   if( rc==SQLITE_OK ){
70432     /* If a transient index is required, create it by calling
70433     ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
70434     ** opening it. If a transient table is required, just use the
70435     ** automatically created table with root-page 1 (an BLOB_INTKEY table).
70436     */
70437     if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
70438       int pgno;
70439       assert( pOp->p4type==P4_KEYINFO );
70440       rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
70441       if( rc==SQLITE_OK ){
70442         assert( pgno==MASTER_ROOT+1 );
70443         assert( pKeyInfo->db==db );
70444         assert( pKeyInfo->enc==ENC(db) );
70445         pCx->pKeyInfo = pKeyInfo;
70446         rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
70447       }
70448       pCx->isTable = 0;
70449     }else{
70450       rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
70451       pCx->isTable = 1;
70452     }
70453   }
70454   pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
70455   break;
70456 }
70457 
70458 /* Opcode: SorterOpen P1 P2 * P4 *
70459 **
70460 ** This opcode works like OP_OpenEphemeral except that it opens
70461 ** a transient index that is specifically designed to sort large
70462 ** tables using an external merge-sort algorithm.
70463 */
70464 case OP_SorterOpen: {
70465   VdbeCursor *pCx;
70466 
70467   assert( pOp->p1>=0 );
70468   assert( pOp->p2>=0 );
70469   pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
70470   if( pCx==0 ) goto no_mem;
70471   pCx->pKeyInfo = pOp->p4.pKeyInfo;
70472   assert( pCx->pKeyInfo->db==db );
70473   assert( pCx->pKeyInfo->enc==ENC(db) );
70474   rc = sqlite3VdbeSorterInit(db, pCx);
70475   break;
70476 }
70477 
70478 /* Opcode: OpenPseudo P1 P2 P3 * *
70479 ** Synopsis: P3 columns in r[P2]
70480 **
70481 ** Open a new cursor that points to a fake table that contains a single
70482 ** row of data.  The content of that one row is the content of memory
70483 ** register P2.  In other words, cursor P1 becomes an alias for the
70484 ** MEM_Blob content contained in register P2.
70485 **
70486 ** A pseudo-table created by this opcode is used to hold a single
70487 ** row output from the sorter so that the row can be decomposed into
70488 ** individual columns using the OP_Column opcode.  The OP_Column opcode
70489 ** is the only cursor opcode that works with a pseudo-table.
70490 **
70491 ** P3 is the number of fields in the records that will be stored by
70492 ** the pseudo-table.
70493 */
70494 case OP_OpenPseudo: {
70495   VdbeCursor *pCx;
70496 
70497   assert( pOp->p1>=0 );
70498   assert( pOp->p3>=0 );
70499   pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
70500   if( pCx==0 ) goto no_mem;
70501   pCx->nullRow = 1;
70502   pCx->pseudoTableReg = pOp->p2;
70503   pCx->isTable = 1;
70504   assert( pOp->p5==0 );
70505   break;
70506 }
70507 
70508 /* Opcode: Close P1 * * * *
70509 **
70510 ** Close a cursor previously opened as P1.  If P1 is not
70511 ** currently open, this instruction is a no-op.
70512 */
70513 case OP_Close: {
70514   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70515   sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
70516   p->apCsr[pOp->p1] = 0;
70517   break;
70518 }
70519 
70520 /* Opcode: SeekGe P1 P2 P3 P4 *
70521 ** Synopsis: key=r[P3@P4]
70522 **
70523 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70524 ** use the value in register P3 as the key.  If cursor P1 refers
70525 ** to an SQL index, then P3 is the first in an array of P4 registers
70526 ** that are used as an unpacked index key.
70527 **
70528 ** Reposition cursor P1 so that  it points to the smallest entry that
70529 ** is greater than or equal to the key value. If there are no records
70530 ** greater than or equal to the key and P2 is not zero, then jump to P2.
70531 **
70532 ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
70533 */
70534 /* Opcode: SeekGt P1 P2 P3 P4 *
70535 ** Synopsis: key=r[P3@P4]
70536 **
70537 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70538 ** use the value in register P3 as a key. If cursor P1 refers
70539 ** to an SQL index, then P3 is the first in an array of P4 registers
70540 ** that are used as an unpacked index key.
70541 **
70542 ** Reposition cursor P1 so that  it points to the smallest entry that
70543 ** is greater than the key value. If there are no records greater than
70544 ** the key and P2 is not zero, then jump to P2.
70545 **
70546 ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
70547 */
70548 /* Opcode: SeekLt P1 P2 P3 P4 *
70549 ** Synopsis: key=r[P3@P4]
70550 **
70551 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70552 ** use the value in register P3 as a key. If cursor P1 refers
70553 ** to an SQL index, then P3 is the first in an array of P4 registers
70554 ** that are used as an unpacked index key.
70555 **
70556 ** Reposition cursor P1 so that  it points to the largest entry that
70557 ** is less than the key value. If there are no records less than
70558 ** the key and P2 is not zero, then jump to P2.
70559 **
70560 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
70561 */
70562 /* Opcode: SeekLe P1 P2 P3 P4 *
70563 ** Synopsis: key=r[P3@P4]
70564 **
70565 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
70566 ** use the value in register P3 as a key. If cursor P1 refers
70567 ** to an SQL index, then P3 is the first in an array of P4 registers
70568 ** that are used as an unpacked index key.
70569 **
70570 ** Reposition cursor P1 so that it points to the largest entry that
70571 ** is less than or equal to the key value. If there are no records
70572 ** less than or equal to the key and P2 is not zero, then jump to P2.
70573 **
70574 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
70575 */
70576 case OP_SeekLT:         /* jump, in3 */
70577 case OP_SeekLE:         /* jump, in3 */
70578 case OP_SeekGE:         /* jump, in3 */
70579 case OP_SeekGT: {       /* jump, in3 */
70580   int res;
70581   int oc;
70582   VdbeCursor *pC;
70583   UnpackedRecord r;
70584   int nField;
70585   i64 iKey;      /* The rowid we are to seek to */
70586 
70587   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70588   assert( pOp->p2!=0 );
70589   pC = p->apCsr[pOp->p1];
70590   assert( pC!=0 );
70591   assert( pC->pseudoTableReg==0 );
70592   assert( OP_SeekLE == OP_SeekLT+1 );
70593   assert( OP_SeekGE == OP_SeekLT+2 );
70594   assert( OP_SeekGT == OP_SeekLT+3 );
70595   assert( pC->isOrdered );
70596   assert( pC->pCursor!=0 );
70597   oc = pOp->opcode;
70598   pC->nullRow = 0;
70599   if( pC->isTable ){
70600     /* The input value in P3 might be of any type: integer, real, string,
70601     ** blob, or NULL.  But it needs to be an integer before we can do
70602     ** the seek, so covert it. */
70603     pIn3 = &aMem[pOp->p3];
70604     applyNumericAffinity(pIn3);
70605     iKey = sqlite3VdbeIntValue(pIn3);
70606     pC->rowidIsValid = 0;
70607 
70608     /* If the P3 value could not be converted into an integer without
70609     ** loss of information, then special processing is required... */
70610     if( (pIn3->flags & MEM_Int)==0 ){
70611       if( (pIn3->flags & MEM_Real)==0 ){
70612         /* If the P3 value cannot be converted into any kind of a number,
70613         ** then the seek is not possible, so jump to P2 */
70614         pc = pOp->p2 - 1;  VdbeBranchTaken(1,2);
70615         break;
70616       }
70617 
70618       /* If the approximation iKey is larger than the actual real search
70619       ** term, substitute >= for > and < for <=. e.g. if the search term
70620       ** is 4.9 and the integer approximation 5:
70621       **
70622       **        (x >  4.9)    ->     (x >= 5)
70623       **        (x <= 4.9)    ->     (x <  5)
70624       */
70625       if( pIn3->r<(double)iKey ){
70626         assert( OP_SeekGE==(OP_SeekGT-1) );
70627         assert( OP_SeekLT==(OP_SeekLE-1) );
70628         assert( (OP_SeekLE & 0x0001)==(OP_SeekGT & 0x0001) );
70629         if( (oc & 0x0001)==(OP_SeekGT & 0x0001) ) oc--;
70630       }
70631 
70632       /* If the approximation iKey is smaller than the actual real search
70633       ** term, substitute <= for < and > for >=.  */
70634       else if( pIn3->r>(double)iKey ){
70635         assert( OP_SeekLE==(OP_SeekLT+1) );
70636         assert( OP_SeekGT==(OP_SeekGE+1) );
70637         assert( (OP_SeekLT & 0x0001)==(OP_SeekGE & 0x0001) );
70638         if( (oc & 0x0001)==(OP_SeekLT & 0x0001) ) oc++;
70639       }
70640     }
70641     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
70642     if( rc!=SQLITE_OK ){
70643       goto abort_due_to_error;
70644     }
70645     if( res==0 ){
70646       pC->rowidIsValid = 1;
70647       pC->lastRowid = iKey;
70648     }
70649   }else{
70650     nField = pOp->p4.i;
70651     assert( pOp->p4type==P4_INT32 );
70652     assert( nField>0 );
70653     r.pKeyInfo = pC->pKeyInfo;
70654     r.nField = (u16)nField;
70655 
70656     /* The next line of code computes as follows, only faster:
70657     **   if( oc==OP_SeekGT || oc==OP_SeekLE ){
70658     **     r.default_rc = -1;
70659     **   }else{
70660     **     r.default_rc = +1;
70661     **   }
70662     */
70663     r.default_rc = ((1 & (oc - OP_SeekLT)) ? -1 : +1);
70664     assert( oc!=OP_SeekGT || r.default_rc==-1 );
70665     assert( oc!=OP_SeekLE || r.default_rc==-1 );
70666     assert( oc!=OP_SeekGE || r.default_rc==+1 );
70667     assert( oc!=OP_SeekLT || r.default_rc==+1 );
70668 
70669     r.aMem = &aMem[pOp->p3];
70670 #ifdef SQLITE_DEBUG
70671     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
70672 #endif
70673     ExpandBlob(r.aMem);
70674     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
70675     if( rc!=SQLITE_OK ){
70676       goto abort_due_to_error;
70677     }
70678     pC->rowidIsValid = 0;
70679   }
70680   pC->deferredMoveto = 0;
70681   pC->cacheStatus = CACHE_STALE;
70682 #ifdef SQLITE_TEST
70683   sqlite3_search_count++;
70684 #endif
70685   if( oc>=OP_SeekGE ){  assert( oc==OP_SeekGE || oc==OP_SeekGT );
70686     if( res<0 || (res==0 && oc==OP_SeekGT) ){
70687       res = 0;
70688       rc = sqlite3BtreeNext(pC->pCursor, &res);
70689       if( rc!=SQLITE_OK ) goto abort_due_to_error;
70690       pC->rowidIsValid = 0;
70691     }else{
70692       res = 0;
70693     }
70694   }else{
70695     assert( oc==OP_SeekLT || oc==OP_SeekLE );
70696     if( res>0 || (res==0 && oc==OP_SeekLT) ){
70697       res = 0;
70698       rc = sqlite3BtreePrevious(pC->pCursor, &res);
70699       if( rc!=SQLITE_OK ) goto abort_due_to_error;
70700       pC->rowidIsValid = 0;
70701     }else{
70702       /* res might be negative because the table is empty.  Check to
70703       ** see if this is the case.
70704       */
70705       res = sqlite3BtreeEof(pC->pCursor);
70706     }
70707   }
70708   assert( pOp->p2>0 );
70709   VdbeBranchTaken(res!=0,2);
70710   if( res ){
70711     pc = pOp->p2 - 1;
70712   }
70713   break;
70714 }
70715 
70716 /* Opcode: Seek P1 P2 * * *
70717 ** Synopsis:  intkey=r[P2]
70718 **
70719 ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
70720 ** for P1 to move so that it points to the rowid given by P2.
70721 **
70722 ** This is actually a deferred seek.  Nothing actually happens until
70723 ** the cursor is used to read a record.  That way, if no reads
70724 ** occur, no unnecessary I/O happens.
70725 */
70726 case OP_Seek: {    /* in2 */
70727   VdbeCursor *pC;
70728 
70729   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70730   pC = p->apCsr[pOp->p1];
70731   assert( pC!=0 );
70732   assert( pC->pCursor!=0 );
70733   assert( pC->isTable );
70734   pC->nullRow = 0;
70735   pIn2 = &aMem[pOp->p2];
70736   pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
70737   pC->rowidIsValid = 0;
70738   pC->deferredMoveto = 1;
70739   break;
70740 }
70741 
70742 
70743 /* Opcode: Found P1 P2 P3 P4 *
70744 ** Synopsis: key=r[P3@P4]
70745 **
70746 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
70747 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
70748 ** record.
70749 **
70750 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
70751 ** is a prefix of any entry in P1 then a jump is made to P2 and
70752 ** P1 is left pointing at the matching entry.
70753 **
70754 ** See also: NotFound, NoConflict, NotExists. SeekGe
70755 */
70756 /* Opcode: NotFound P1 P2 P3 P4 *
70757 ** Synopsis: key=r[P3@P4]
70758 **
70759 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
70760 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
70761 ** record.
70762 **
70763 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
70764 ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1
70765 ** does contain an entry whose prefix matches the P3/P4 record then control
70766 ** falls through to the next instruction and P1 is left pointing at the
70767 ** matching entry.
70768 **
70769 ** See also: Found, NotExists, NoConflict
70770 */
70771 /* Opcode: NoConflict P1 P2 P3 P4 *
70772 ** Synopsis: key=r[P3@P4]
70773 **
70774 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
70775 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
70776 ** record.
70777 **
70778 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
70779 ** contains any NULL value, jump immediately to P2.  If all terms of the
70780 ** record are not-NULL then a check is done to determine if any row in the
70781 ** P1 index btree has a matching key prefix.  If there are no matches, jump
70782 ** immediately to P2.  If there is a match, fall through and leave the P1
70783 ** cursor pointing to the matching row.
70784 **
70785 ** This opcode is similar to OP_NotFound with the exceptions that the
70786 ** branch is always taken if any part of the search key input is NULL.
70787 **
70788 ** See also: NotFound, Found, NotExists
70789 */
70790 case OP_NoConflict:     /* jump, in3 */
70791 case OP_NotFound:       /* jump, in3 */
70792 case OP_Found: {        /* jump, in3 */
70793   int alreadyExists;
70794   int ii;
70795   VdbeCursor *pC;
70796   int res;
70797   char *pFree;
70798   UnpackedRecord *pIdxKey;
70799   UnpackedRecord r;
70800   char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
70801 
70802 #ifdef SQLITE_TEST
70803   if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
70804 #endif
70805 
70806   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70807   assert( pOp->p4type==P4_INT32 );
70808   pC = p->apCsr[pOp->p1];
70809   assert( pC!=0 );
70810   pIn3 = &aMem[pOp->p3];
70811   assert( pC->pCursor!=0 );
70812   assert( pC->isTable==0 );
70813   pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
70814   if( pOp->p4.i>0 ){
70815     r.pKeyInfo = pC->pKeyInfo;
70816     r.nField = (u16)pOp->p4.i;
70817     r.aMem = pIn3;
70818     for(ii=0; ii<r.nField; ii++){
70819       assert( memIsValid(&r.aMem[ii]) );
70820       ExpandBlob(&r.aMem[ii]);
70821 #ifdef SQLITE_DEBUG
70822       if( ii ) REGISTER_TRACE(pOp->p3+ii, &r.aMem[ii]);
70823 #endif
70824     }
70825     pIdxKey = &r;
70826   }else{
70827     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
70828         pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
70829     );
70830     if( pIdxKey==0 ) goto no_mem;
70831     assert( pIn3->flags & MEM_Blob );
70832     assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
70833     sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
70834   }
70835   pIdxKey->default_rc = 0;
70836   if( pOp->opcode==OP_NoConflict ){
70837     /* For the OP_NoConflict opcode, take the jump if any of the
70838     ** input fields are NULL, since any key with a NULL will not
70839     ** conflict */
70840     for(ii=0; ii<r.nField; ii++){
70841       if( r.aMem[ii].flags & MEM_Null ){
70842         pc = pOp->p2 - 1; VdbeBranchTaken(1,2);
70843         break;
70844       }
70845     }
70846   }
70847   rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
70848   if( pOp->p4.i==0 ){
70849     sqlite3DbFree(db, pFree);
70850   }
70851   if( rc!=SQLITE_OK ){
70852     break;
70853   }
70854   pC->seekResult = res;
70855   alreadyExists = (res==0);
70856   pC->nullRow = 1-alreadyExists;
70857   pC->deferredMoveto = 0;
70858   pC->cacheStatus = CACHE_STALE;
70859   if( pOp->opcode==OP_Found ){
70860     VdbeBranchTaken(alreadyExists!=0,2);
70861     if( alreadyExists ) pc = pOp->p2 - 1;
70862   }else{
70863     VdbeBranchTaken(alreadyExists==0,2);
70864     if( !alreadyExists ) pc = pOp->p2 - 1;
70865   }
70866   break;
70867 }
70868 
70869 /* Opcode: NotExists P1 P2 P3 * *
70870 ** Synopsis: intkey=r[P3]
70871 **
70872 ** P1 is the index of a cursor open on an SQL table btree (with integer
70873 ** keys).  P3 is an integer rowid.  If P1 does not contain a record with
70874 ** rowid P3 then jump immediately to P2.  If P1 does contain a record
70875 ** with rowid P3 then leave the cursor pointing at that record and fall
70876 ** through to the next instruction.
70877 **
70878 ** The OP_NotFound opcode performs the same operation on index btrees
70879 ** (with arbitrary multi-value keys).
70880 **
70881 ** See also: Found, NotFound, NoConflict
70882 */
70883 case OP_NotExists: {        /* jump, in3 */
70884   VdbeCursor *pC;
70885   BtCursor *pCrsr;
70886   int res;
70887   u64 iKey;
70888 
70889   pIn3 = &aMem[pOp->p3];
70890   assert( pIn3->flags & MEM_Int );
70891   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70892   pC = p->apCsr[pOp->p1];
70893   assert( pC!=0 );
70894   assert( pC->isTable );
70895   assert( pC->pseudoTableReg==0 );
70896   pCrsr = pC->pCursor;
70897   assert( pCrsr!=0 );
70898   res = 0;
70899   iKey = pIn3->u.i;
70900   rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
70901   pC->lastRowid = pIn3->u.i;
70902   pC->rowidIsValid = res==0 ?1:0;
70903   pC->nullRow = 0;
70904   pC->cacheStatus = CACHE_STALE;
70905   pC->deferredMoveto = 0;
70906   VdbeBranchTaken(res!=0,2);
70907   if( res!=0 ){
70908     pc = pOp->p2 - 1;
70909     assert( pC->rowidIsValid==0 );
70910   }
70911   pC->seekResult = res;
70912   break;
70913 }
70914 
70915 /* Opcode: Sequence P1 P2 * * *
70916 ** Synopsis: r[P2]=rowid
70917 **
70918 ** Find the next available sequence number for cursor P1.
70919 ** Write the sequence number into register P2.
70920 ** The sequence number on the cursor is incremented after this
70921 ** instruction.
70922 */
70923 case OP_Sequence: {           /* out2-prerelease */
70924   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70925   assert( p->apCsr[pOp->p1]!=0 );
70926   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
70927   break;
70928 }
70929 
70930 
70931 /* Opcode: NewRowid P1 P2 P3 * *
70932 ** Synopsis: r[P2]=rowid
70933 **
70934 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
70935 ** The record number is not previously used as a key in the database
70936 ** table that cursor P1 points to.  The new record number is written
70937 ** written to register P2.
70938 **
70939 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
70940 ** the largest previously generated record number. No new record numbers are
70941 ** allowed to be less than this value. When this value reaches its maximum,
70942 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
70943 ** generated record number. This P3 mechanism is used to help implement the
70944 ** AUTOINCREMENT feature.
70945 */
70946 case OP_NewRowid: {           /* out2-prerelease */
70947   i64 v;                 /* The new rowid */
70948   VdbeCursor *pC;        /* Cursor of table to get the new rowid */
70949   int res;               /* Result of an sqlite3BtreeLast() */
70950   int cnt;               /* Counter to limit the number of searches */
70951   Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
70952   VdbeFrame *pFrame;     /* Root frame of VDBE */
70953 
70954   v = 0;
70955   res = 0;
70956   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70957   pC = p->apCsr[pOp->p1];
70958   assert( pC!=0 );
70959   if( NEVER(pC->pCursor==0) ){
70960     /* The zero initialization above is all that is needed */
70961   }else{
70962     /* The next rowid or record number (different terms for the same
70963     ** thing) is obtained in a two-step algorithm.
70964     **
70965     ** First we attempt to find the largest existing rowid and add one
70966     ** to that.  But if the largest existing rowid is already the maximum
70967     ** positive integer, we have to fall through to the second
70968     ** probabilistic algorithm
70969     **
70970     ** The second algorithm is to select a rowid at random and see if
70971     ** it already exists in the table.  If it does not exist, we have
70972     ** succeeded.  If the random rowid does exist, we select a new one
70973     ** and try again, up to 100 times.
70974     */
70975     assert( pC->isTable );
70976 
70977 #ifdef SQLITE_32BIT_ROWID
70978 #   define MAX_ROWID 0x7fffffff
70979 #else
70980     /* Some compilers complain about constants of the form 0x7fffffffffffffff.
70981     ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
70982     ** to provide the constant while making all compilers happy.
70983     */
70984 #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
70985 #endif
70986 
70987     if( !pC->useRandomRowid ){
70988       rc = sqlite3BtreeLast(pC->pCursor, &res);
70989       if( rc!=SQLITE_OK ){
70990         goto abort_due_to_error;
70991       }
70992       if( res ){
70993         v = 1;   /* IMP: R-61914-48074 */
70994       }else{
70995         assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
70996         rc = sqlite3BtreeKeySize(pC->pCursor, &v);
70997         assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
70998         if( v>=MAX_ROWID ){
70999           pC->useRandomRowid = 1;
71000         }else{
71001           v++;   /* IMP: R-29538-34987 */
71002         }
71003       }
71004     }
71005 
71006 #ifndef SQLITE_OMIT_AUTOINCREMENT
71007     if( pOp->p3 ){
71008       /* Assert that P3 is a valid memory cell. */
71009       assert( pOp->p3>0 );
71010       if( p->pFrame ){
71011         for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
71012         /* Assert that P3 is a valid memory cell. */
71013         assert( pOp->p3<=pFrame->nMem );
71014         pMem = &pFrame->aMem[pOp->p3];
71015       }else{
71016         /* Assert that P3 is a valid memory cell. */
71017         assert( pOp->p3<=(p->nMem-p->nCursor) );
71018         pMem = &aMem[pOp->p3];
71019         memAboutToChange(p, pMem);
71020       }
71021       assert( memIsValid(pMem) );
71022 
71023       REGISTER_TRACE(pOp->p3, pMem);
71024       sqlite3VdbeMemIntegerify(pMem);
71025       assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
71026       if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
71027         rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
71028         goto abort_due_to_error;
71029       }
71030       if( v<pMem->u.i+1 ){
71031         v = pMem->u.i + 1;
71032       }
71033       pMem->u.i = v;
71034     }
71035 #endif
71036     if( pC->useRandomRowid ){
71037       /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
71038       ** largest possible integer (9223372036854775807) then the database
71039       ** engine starts picking positive candidate ROWIDs at random until
71040       ** it finds one that is not previously used. */
71041       assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
71042                              ** an AUTOINCREMENT table. */
71043       /* on the first attempt, simply do one more than previous */
71044       v = lastRowid;
71045       v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
71046       v++; /* ensure non-zero */
71047       cnt = 0;
71048       while(   ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
71049                                                  0, &res))==SQLITE_OK)
71050             && (res==0)
71051             && (++cnt<100)){
71052         /* collision - try another random rowid */
71053         sqlite3_randomness(sizeof(v), &v);
71054         if( cnt<5 ){
71055           /* try "small" random rowids for the initial attempts */
71056           v &= 0xffffff;
71057         }else{
71058           v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
71059         }
71060         v++; /* ensure non-zero */
71061       }
71062       if( rc==SQLITE_OK && res==0 ){
71063         rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
71064         goto abort_due_to_error;
71065       }
71066       assert( v>0 );  /* EV: R-40812-03570 */
71067     }
71068     pC->rowidIsValid = 0;
71069     pC->deferredMoveto = 0;
71070     pC->cacheStatus = CACHE_STALE;
71071   }
71072   pOut->u.i = v;
71073   break;
71074 }
71075 
71076 /* Opcode: Insert P1 P2 P3 P4 P5
71077 ** Synopsis: intkey=r[P3] data=r[P2]
71078 **
71079 ** Write an entry into the table of cursor P1.  A new entry is
71080 ** created if it doesn't already exist or the data for an existing
71081 ** entry is overwritten.  The data is the value MEM_Blob stored in register
71082 ** number P2. The key is stored in register P3. The key must
71083 ** be a MEM_Int.
71084 **
71085 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
71086 ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
71087 ** then rowid is stored for subsequent return by the
71088 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
71089 **
71090 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
71091 ** the last seek operation (OP_NotExists) was a success, then this
71092 ** operation will not attempt to find the appropriate row before doing
71093 ** the insert but will instead overwrite the row that the cursor is
71094 ** currently pointing to.  Presumably, the prior OP_NotExists opcode
71095 ** has already positioned the cursor correctly.  This is an optimization
71096 ** that boosts performance by avoiding redundant seeks.
71097 **
71098 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
71099 ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
71100 ** is part of an INSERT operation.  The difference is only important to
71101 ** the update hook.
71102 **
71103 ** Parameter P4 may point to a string containing the table-name, or
71104 ** may be NULL. If it is not NULL, then the update-hook
71105 ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
71106 **
71107 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
71108 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
71109 ** and register P2 becomes ephemeral.  If the cursor is changed, the
71110 ** value of register P2 will then change.  Make sure this does not
71111 ** cause any problems.)
71112 **
71113 ** This instruction only works on tables.  The equivalent instruction
71114 ** for indices is OP_IdxInsert.
71115 */
71116 /* Opcode: InsertInt P1 P2 P3 P4 P5
71117 ** Synopsis:  intkey=P3 data=r[P2]
71118 **
71119 ** This works exactly like OP_Insert except that the key is the
71120 ** integer value P3, not the value of the integer stored in register P3.
71121 */
71122 case OP_Insert:
71123 case OP_InsertInt: {
71124   Mem *pData;       /* MEM cell holding data for the record to be inserted */
71125   Mem *pKey;        /* MEM cell holding key  for the record */
71126   i64 iKey;         /* The integer ROWID or key for the record to be inserted */
71127   VdbeCursor *pC;   /* Cursor to table into which insert is written */
71128   int nZero;        /* Number of zero-bytes to append */
71129   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
71130   const char *zDb;  /* database name - used by the update hook */
71131   const char *zTbl; /* Table name - used by the opdate hook */
71132   int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
71133 
71134   pData = &aMem[pOp->p2];
71135   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71136   assert( memIsValid(pData) );
71137   pC = p->apCsr[pOp->p1];
71138   assert( pC!=0 );
71139   assert( pC->pCursor!=0 );
71140   assert( pC->pseudoTableReg==0 );
71141   assert( pC->isTable );
71142   REGISTER_TRACE(pOp->p2, pData);
71143 
71144   if( pOp->opcode==OP_Insert ){
71145     pKey = &aMem[pOp->p3];
71146     assert( pKey->flags & MEM_Int );
71147     assert( memIsValid(pKey) );
71148     REGISTER_TRACE(pOp->p3, pKey);
71149     iKey = pKey->u.i;
71150   }else{
71151     assert( pOp->opcode==OP_InsertInt );
71152     iKey = pOp->p3;
71153   }
71154 
71155   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
71156   if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
71157   if( pData->flags & MEM_Null ){
71158     pData->z = 0;
71159     pData->n = 0;
71160   }else{
71161     assert( pData->flags & (MEM_Blob|MEM_Str) );
71162   }
71163   seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
71164   if( pData->flags & MEM_Zero ){
71165     nZero = pData->u.nZero;
71166   }else{
71167     nZero = 0;
71168   }
71169   rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
71170                           pData->z, pData->n, nZero,
71171                           (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
71172   );
71173   pC->rowidIsValid = 0;
71174   pC->deferredMoveto = 0;
71175   pC->cacheStatus = CACHE_STALE;
71176 
71177   /* Invoke the update-hook if required. */
71178   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
71179     zDb = db->aDb[pC->iDb].zName;
71180     zTbl = pOp->p4.z;
71181     op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
71182     assert( pC->isTable );
71183     db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
71184     assert( pC->iDb>=0 );
71185   }
71186   break;
71187 }
71188 
71189 /* Opcode: Delete P1 P2 * P4 *
71190 **
71191 ** Delete the record at which the P1 cursor is currently pointing.
71192 **
71193 ** The cursor will be left pointing at either the next or the previous
71194 ** record in the table. If it is left pointing at the next record, then
71195 ** the next Next instruction will be a no-op.  Hence it is OK to delete
71196 ** a record from within an Next loop.
71197 **
71198 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
71199 ** incremented (otherwise not).
71200 **
71201 ** P1 must not be pseudo-table.  It has to be a real table with
71202 ** multiple rows.
71203 **
71204 ** If P4 is not NULL, then it is the name of the table that P1 is
71205 ** pointing to.  The update hook will be invoked, if it exists.
71206 ** If P4 is not NULL then the P1 cursor must have been positioned
71207 ** using OP_NotFound prior to invoking this opcode.
71208 */
71209 case OP_Delete: {
71210   i64 iKey;
71211   VdbeCursor *pC;
71212 
71213   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71214   pC = p->apCsr[pOp->p1];
71215   assert( pC!=0 );
71216   assert( pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
71217   iKey = pC->lastRowid;      /* Only used for the update hook */
71218 
71219   /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
71220   ** OP_Column on the same table without any intervening operations that
71221   ** might move or invalidate the cursor.  Hence cursor pC is always pointing
71222   ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
71223   ** below is always a no-op and cannot fail.  We will run it anyhow, though,
71224   ** to guard against future changes to the code generator.
71225   **/
71226   assert( pC->deferredMoveto==0 );
71227   rc = sqlite3VdbeCursorMoveto(pC);
71228   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
71229 
71230   rc = sqlite3BtreeDelete(pC->pCursor);
71231   pC->cacheStatus = CACHE_STALE;
71232 
71233   /* Invoke the update-hook if required. */
71234   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
71235     db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
71236                         db->aDb[pC->iDb].zName, pOp->p4.z, iKey);
71237     assert( pC->iDb>=0 );
71238   }
71239   if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
71240   break;
71241 }
71242 /* Opcode: ResetCount * * * * *
71243 **
71244 ** The value of the change counter is copied to the database handle
71245 ** change counter (returned by subsequent calls to sqlite3_changes()).
71246 ** Then the VMs internal change counter resets to 0.
71247 ** This is used by trigger programs.
71248 */
71249 case OP_ResetCount: {
71250   sqlite3VdbeSetChanges(db, p->nChange);
71251   p->nChange = 0;
71252   break;
71253 }
71254 
71255 /* Opcode: SorterCompare P1 P2 P3 P4
71256 ** Synopsis:  if key(P1)!=rtrim(r[P3],P4) goto P2
71257 **
71258 ** P1 is a sorter cursor. This instruction compares a prefix of the
71259 ** the record blob in register P3 against a prefix of the entry that
71260 ** the sorter cursor currently points to.  The final P4 fields of both
71261 ** the P3 and sorter record are ignored.
71262 **
71263 ** If either P3 or the sorter contains a NULL in one of their significant
71264 ** fields (not counting the P4 fields at the end which are ignored) then
71265 ** the comparison is assumed to be equal.
71266 **
71267 ** Fall through to next instruction if the two records compare equal to
71268 ** each other.  Jump to P2 if they are different.
71269 */
71270 case OP_SorterCompare: {
71271   VdbeCursor *pC;
71272   int res;
71273   int nIgnore;
71274 
71275   pC = p->apCsr[pOp->p1];
71276   assert( isSorter(pC) );
71277   assert( pOp->p4type==P4_INT32 );
71278   pIn3 = &aMem[pOp->p3];
71279   nIgnore = pOp->p4.i;
71280   rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
71281   VdbeBranchTaken(res!=0,2);
71282   if( res ){
71283     pc = pOp->p2-1;
71284   }
71285   break;
71286 };
71287 
71288 /* Opcode: SorterData P1 P2 * * *
71289 ** Synopsis: r[P2]=data
71290 **
71291 ** Write into register P2 the current sorter data for sorter cursor P1.
71292 */
71293 case OP_SorterData: {
71294   VdbeCursor *pC;
71295 
71296   pOut = &aMem[pOp->p2];
71297   pC = p->apCsr[pOp->p1];
71298   assert( isSorter(pC) );
71299   rc = sqlite3VdbeSorterRowkey(pC, pOut);
71300   break;
71301 }
71302 
71303 /* Opcode: RowData P1 P2 * * *
71304 ** Synopsis: r[P2]=data
71305 **
71306 ** Write into register P2 the complete row data for cursor P1.
71307 ** There is no interpretation of the data.
71308 ** It is just copied onto the P2 register exactly as
71309 ** it is found in the database file.
71310 **
71311 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
71312 ** of a real table, not a pseudo-table.
71313 */
71314 /* Opcode: RowKey P1 P2 * * *
71315 ** Synopsis: r[P2]=key
71316 **
71317 ** Write into register P2 the complete row key for cursor P1.
71318 ** There is no interpretation of the data.
71319 ** The key is copied onto the P2 register exactly as
71320 ** it is found in the database file.
71321 **
71322 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
71323 ** of a real table, not a pseudo-table.
71324 */
71325 case OP_RowKey:
71326 case OP_RowData: {
71327   VdbeCursor *pC;
71328   BtCursor *pCrsr;
71329   u32 n;
71330   i64 n64;
71331 
71332   pOut = &aMem[pOp->p2];
71333   memAboutToChange(p, pOut);
71334 
71335   /* Note that RowKey and RowData are really exactly the same instruction */
71336   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71337   pC = p->apCsr[pOp->p1];
71338   assert( isSorter(pC)==0 );
71339   assert( pC->isTable || pOp->opcode!=OP_RowData );
71340   assert( pC->isTable==0 || pOp->opcode==OP_RowData );
71341   assert( pC!=0 );
71342   assert( pC->nullRow==0 );
71343   assert( pC->pseudoTableReg==0 );
71344   assert( pC->pCursor!=0 );
71345   pCrsr = pC->pCursor;
71346   assert( sqlite3BtreeCursorIsValid(pCrsr) );
71347 
71348   /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
71349   ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
71350   ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
71351   ** a no-op and can never fail.  But we leave it in place as a safety.
71352   */
71353   assert( pC->deferredMoveto==0 );
71354   rc = sqlite3VdbeCursorMoveto(pC);
71355   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
71356 
71357   if( pC->isTable==0 ){
71358     assert( !pC->isTable );
71359     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
71360     assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
71361     if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
71362       goto too_big;
71363     }
71364     n = (u32)n64;
71365   }else{
71366     VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
71367     assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
71368     if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
71369       goto too_big;
71370     }
71371   }
71372   if( sqlite3VdbeMemGrow(pOut, n, 0) ){
71373     goto no_mem;
71374   }
71375   pOut->n = n;
71376   MemSetTypeFlag(pOut, MEM_Blob);
71377   if( pC->isTable==0 ){
71378     rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
71379   }else{
71380     rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
71381   }
71382   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
71383   UPDATE_MAX_BLOBSIZE(pOut);
71384   REGISTER_TRACE(pOp->p2, pOut);
71385   break;
71386 }
71387 
71388 /* Opcode: Rowid P1 P2 * * *
71389 ** Synopsis: r[P2]=rowid
71390 **
71391 ** Store in register P2 an integer which is the key of the table entry that
71392 ** P1 is currently point to.
71393 **
71394 ** P1 can be either an ordinary table or a virtual table.  There used to
71395 ** be a separate OP_VRowid opcode for use with virtual tables, but this
71396 ** one opcode now works for both table types.
71397 */
71398 case OP_Rowid: {                 /* out2-prerelease */
71399   VdbeCursor *pC;
71400   i64 v;
71401   sqlite3_vtab *pVtab;
71402   const sqlite3_module *pModule;
71403 
71404   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71405   pC = p->apCsr[pOp->p1];
71406   assert( pC!=0 );
71407   assert( pC->pseudoTableReg==0 || pC->nullRow );
71408   if( pC->nullRow ){
71409     pOut->flags = MEM_Null;
71410     break;
71411   }else if( pC->deferredMoveto ){
71412     v = pC->movetoTarget;
71413 #ifndef SQLITE_OMIT_VIRTUALTABLE
71414   }else if( pC->pVtabCursor ){
71415     pVtab = pC->pVtabCursor->pVtab;
71416     pModule = pVtab->pModule;
71417     assert( pModule->xRowid );
71418     rc = pModule->xRowid(pC->pVtabCursor, &v);
71419     sqlite3VtabImportErrmsg(p, pVtab);
71420 #endif /* SQLITE_OMIT_VIRTUALTABLE */
71421   }else{
71422     assert( pC->pCursor!=0 );
71423     rc = sqlite3VdbeCursorMoveto(pC);
71424     if( rc ) goto abort_due_to_error;
71425     if( pC->rowidIsValid ){
71426       v = pC->lastRowid;
71427     }else{
71428       rc = sqlite3BtreeKeySize(pC->pCursor, &v);
71429       assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
71430     }
71431   }
71432   pOut->u.i = v;
71433   break;
71434 }
71435 
71436 /* Opcode: NullRow P1 * * * *
71437 **
71438 ** Move the cursor P1 to a null row.  Any OP_Column operations
71439 ** that occur while the cursor is on the null row will always
71440 ** write a NULL.
71441 */
71442 case OP_NullRow: {
71443   VdbeCursor *pC;
71444 
71445   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71446   pC = p->apCsr[pOp->p1];
71447   assert( pC!=0 );
71448   pC->nullRow = 1;
71449   pC->rowidIsValid = 0;
71450   pC->cacheStatus = CACHE_STALE;
71451   if( pC->pCursor ){
71452     sqlite3BtreeClearCursor(pC->pCursor);
71453   }
71454   break;
71455 }
71456 
71457 /* Opcode: Last P1 P2 * * *
71458 **
71459 ** The next use of the Rowid or Column or Next instruction for P1
71460 ** will refer to the last entry in the database table or index.
71461 ** If the table or index is empty and P2>0, then jump immediately to P2.
71462 ** If P2 is 0 or if the table or index is not empty, fall through
71463 ** to the following instruction.
71464 */
71465 case OP_Last: {        /* jump */
71466   VdbeCursor *pC;
71467   BtCursor *pCrsr;
71468   int res;
71469 
71470   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71471   pC = p->apCsr[pOp->p1];
71472   assert( pC!=0 );
71473   pCrsr = pC->pCursor;
71474   res = 0;
71475   assert( pCrsr!=0 );
71476   rc = sqlite3BtreeLast(pCrsr, &res);
71477   pC->nullRow = (u8)res;
71478   pC->deferredMoveto = 0;
71479   pC->rowidIsValid = 0;
71480   pC->cacheStatus = CACHE_STALE;
71481   if( pOp->p2>0 ){
71482     VdbeBranchTaken(res!=0,2);
71483     if( res ) pc = pOp->p2 - 1;
71484   }
71485   break;
71486 }
71487 
71488 
71489 /* Opcode: Sort P1 P2 * * *
71490 **
71491 ** This opcode does exactly the same thing as OP_Rewind except that
71492 ** it increments an undocumented global variable used for testing.
71493 **
71494 ** Sorting is accomplished by writing records into a sorting index,
71495 ** then rewinding that index and playing it back from beginning to
71496 ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
71497 ** rewinding so that the global variable will be incremented and
71498 ** regression tests can determine whether or not the optimizer is
71499 ** correctly optimizing out sorts.
71500 */
71501 case OP_SorterSort:    /* jump */
71502 case OP_Sort: {        /* jump */
71503 #ifdef SQLITE_TEST
71504   sqlite3_sort_count++;
71505   sqlite3_search_count--;
71506 #endif
71507   p->aCounter[SQLITE_STMTSTATUS_SORT]++;
71508   /* Fall through into OP_Rewind */
71509 }
71510 /* Opcode: Rewind P1 P2 * * *
71511 **
71512 ** The next use of the Rowid or Column or Next instruction for P1
71513 ** will refer to the first entry in the database table or index.
71514 ** If the table or index is empty and P2>0, then jump immediately to P2.
71515 ** If P2 is 0 or if the table or index is not empty, fall through
71516 ** to the following instruction.
71517 */
71518 case OP_Rewind: {        /* jump */
71519   VdbeCursor *pC;
71520   BtCursor *pCrsr;
71521   int res;
71522 
71523   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71524   pC = p->apCsr[pOp->p1];
71525   assert( pC!=0 );
71526   assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
71527   res = 1;
71528   if( isSorter(pC) ){
71529     rc = sqlite3VdbeSorterRewind(db, pC, &res);
71530   }else{
71531     pCrsr = pC->pCursor;
71532     assert( pCrsr );
71533     rc = sqlite3BtreeFirst(pCrsr, &res);
71534     pC->deferredMoveto = 0;
71535     pC->cacheStatus = CACHE_STALE;
71536     pC->rowidIsValid = 0;
71537   }
71538   pC->nullRow = (u8)res;
71539   assert( pOp->p2>0 && pOp->p2<p->nOp );
71540   VdbeBranchTaken(res!=0,2);
71541   if( res ){
71542     pc = pOp->p2 - 1;
71543   }
71544   break;
71545 }
71546 
71547 /* Opcode: Next P1 P2 P3 P4 P5
71548 **
71549 ** Advance cursor P1 so that it points to the next key/data pair in its
71550 ** table or index.  If there are no more key/value pairs then fall through
71551 ** to the following instruction.  But if the cursor advance was successful,
71552 ** jump immediately to P2.
71553 **
71554 ** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
71555 ** been opened prior to this opcode or the program will segfault.
71556 **
71557 ** The P3 value is a hint to the btree implementation. If P3==1, that
71558 ** means P1 is an SQL index and that this instruction could have been
71559 ** omitted if that index had been unique.  P3 is usually 0.  P3 is
71560 ** always either 0 or 1.
71561 **
71562 ** P4 is always of type P4_ADVANCE. The function pointer points to
71563 ** sqlite3BtreeNext().
71564 **
71565 ** If P5 is positive and the jump is taken, then event counter
71566 ** number P5-1 in the prepared statement is incremented.
71567 **
71568 ** See also: Prev, NextIfOpen
71569 */
71570 /* Opcode: NextIfOpen P1 P2 P3 P4 P5
71571 **
71572 ** This opcode works just like OP_Next except that if cursor P1 is not
71573 ** open it behaves a no-op.
71574 */
71575 /* Opcode: Prev P1 P2 P3 P4 P5
71576 **
71577 ** Back up cursor P1 so that it points to the previous key/data pair in its
71578 ** table or index.  If there is no previous key/value pairs then fall through
71579 ** to the following instruction.  But if the cursor backup was successful,
71580 ** jump immediately to P2.
71581 **
71582 ** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
71583 ** not open then the behavior is undefined.
71584 **
71585 ** The P3 value is a hint to the btree implementation. If P3==1, that
71586 ** means P1 is an SQL index and that this instruction could have been
71587 ** omitted if that index had been unique.  P3 is usually 0.  P3 is
71588 ** always either 0 or 1.
71589 **
71590 ** P4 is always of type P4_ADVANCE. The function pointer points to
71591 ** sqlite3BtreePrevious().
71592 **
71593 ** If P5 is positive and the jump is taken, then event counter
71594 ** number P5-1 in the prepared statement is incremented.
71595 */
71596 /* Opcode: PrevIfOpen P1 P2 P3 P4 P5
71597 **
71598 ** This opcode works just like OP_Prev except that if cursor P1 is not
71599 ** open it behaves a no-op.
71600 */
71601 case OP_SorterNext: {  /* jump */
71602   VdbeCursor *pC;
71603   int res;
71604 
71605   pC = p->apCsr[pOp->p1];
71606   assert( isSorter(pC) );
71607   rc = sqlite3VdbeSorterNext(db, pC, &res);
71608   goto next_tail;
71609 case OP_PrevIfOpen:    /* jump */
71610 case OP_NextIfOpen:    /* jump */
71611   if( p->apCsr[pOp->p1]==0 ) break;
71612   /* Fall through */
71613 case OP_Prev:          /* jump */
71614 case OP_Next:          /* jump */
71615   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71616   assert( pOp->p5<ArraySize(p->aCounter) );
71617   pC = p->apCsr[pOp->p1];
71618   res = pOp->p3;
71619   assert( pC!=0 );
71620   assert( pC->deferredMoveto==0 );
71621   assert( pC->pCursor );
71622   assert( res==0 || (res==1 && pC->isTable==0) );
71623   testcase( res==1 );
71624   assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
71625   assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
71626   assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
71627   assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
71628   rc = pOp->p4.xAdvance(pC->pCursor, &res);
71629 next_tail:
71630   pC->cacheStatus = CACHE_STALE;
71631   VdbeBranchTaken(res==0,2);
71632   if( res==0 ){
71633     pC->nullRow = 0;
71634     pc = pOp->p2 - 1;
71635     p->aCounter[pOp->p5]++;
71636 #ifdef SQLITE_TEST
71637     sqlite3_search_count++;
71638 #endif
71639   }else{
71640     pC->nullRow = 1;
71641   }
71642   pC->rowidIsValid = 0;
71643   goto check_for_interrupt;
71644 }
71645 
71646 /* Opcode: IdxInsert P1 P2 P3 * P5
71647 ** Synopsis: key=r[P2]
71648 **
71649 ** Register P2 holds an SQL index key made using the
71650 ** MakeRecord instructions.  This opcode writes that key
71651 ** into the index P1.  Data for the entry is nil.
71652 **
71653 ** P3 is a flag that provides a hint to the b-tree layer that this
71654 ** insert is likely to be an append.
71655 **
71656 ** If P5 has the OPFLAG_NCHANGE bit set, then the change counter is
71657 ** incremented by this instruction.  If the OPFLAG_NCHANGE bit is clear,
71658 ** then the change counter is unchanged.
71659 **
71660 ** If P5 has the OPFLAG_USESEEKRESULT bit set, then the cursor must have
71661 ** just done a seek to the spot where the new entry is to be inserted.
71662 ** This flag avoids doing an extra seek.
71663 **
71664 ** This instruction only works for indices.  The equivalent instruction
71665 ** for tables is OP_Insert.
71666 */
71667 case OP_SorterInsert:       /* in2 */
71668 case OP_IdxInsert: {        /* in2 */
71669   VdbeCursor *pC;
71670   BtCursor *pCrsr;
71671   int nKey;
71672   const char *zKey;
71673 
71674   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71675   pC = p->apCsr[pOp->p1];
71676   assert( pC!=0 );
71677   assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
71678   pIn2 = &aMem[pOp->p2];
71679   assert( pIn2->flags & MEM_Blob );
71680   pCrsr = pC->pCursor;
71681   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
71682   assert( pCrsr!=0 );
71683   assert( pC->isTable==0 );
71684   rc = ExpandBlob(pIn2);
71685   if( rc==SQLITE_OK ){
71686     if( isSorter(pC) ){
71687       rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
71688     }else{
71689       nKey = pIn2->n;
71690       zKey = pIn2->z;
71691       rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
71692           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
71693           );
71694       assert( pC->deferredMoveto==0 );
71695       pC->cacheStatus = CACHE_STALE;
71696     }
71697   }
71698   break;
71699 }
71700 
71701 /* Opcode: IdxDelete P1 P2 P3 * *
71702 ** Synopsis: key=r[P2@P3]
71703 **
71704 ** The content of P3 registers starting at register P2 form
71705 ** an unpacked index key. This opcode removes that entry from the
71706 ** index opened by cursor P1.
71707 */
71708 case OP_IdxDelete: {
71709   VdbeCursor *pC;
71710   BtCursor *pCrsr;
71711   int res;
71712   UnpackedRecord r;
71713 
71714   assert( pOp->p3>0 );
71715   assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
71716   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71717   pC = p->apCsr[pOp->p1];
71718   assert( pC!=0 );
71719   pCrsr = pC->pCursor;
71720   assert( pCrsr!=0 );
71721   assert( pOp->p5==0 );
71722   r.pKeyInfo = pC->pKeyInfo;
71723   r.nField = (u16)pOp->p3;
71724   r.default_rc = 0;
71725   r.aMem = &aMem[pOp->p2];
71726 #ifdef SQLITE_DEBUG
71727   { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71728 #endif
71729   rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
71730   if( rc==SQLITE_OK && res==0 ){
71731     rc = sqlite3BtreeDelete(pCrsr);
71732   }
71733   assert( pC->deferredMoveto==0 );
71734   pC->cacheStatus = CACHE_STALE;
71735   break;
71736 }
71737 
71738 /* Opcode: IdxRowid P1 P2 * * *
71739 ** Synopsis: r[P2]=rowid
71740 **
71741 ** Write into register P2 an integer which is the last entry in the record at
71742 ** the end of the index key pointed to by cursor P1.  This integer should be
71743 ** the rowid of the table entry to which this index entry points.
71744 **
71745 ** See also: Rowid, MakeRecord.
71746 */
71747 case OP_IdxRowid: {              /* out2-prerelease */
71748   BtCursor *pCrsr;
71749   VdbeCursor *pC;
71750   i64 rowid;
71751 
71752   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71753   pC = p->apCsr[pOp->p1];
71754   assert( pC!=0 );
71755   pCrsr = pC->pCursor;
71756   assert( pCrsr!=0 );
71757   pOut->flags = MEM_Null;
71758   rc = sqlite3VdbeCursorMoveto(pC);
71759   if( NEVER(rc) ) goto abort_due_to_error;
71760   assert( pC->deferredMoveto==0 );
71761   assert( pC->isTable==0 );
71762   if( !pC->nullRow ){
71763     rowid = 0;  /* Not needed.  Only used to silence a warning. */
71764     rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
71765     if( rc!=SQLITE_OK ){
71766       goto abort_due_to_error;
71767     }
71768     pOut->u.i = rowid;
71769     pOut->flags = MEM_Int;
71770   }
71771   break;
71772 }
71773 
71774 /* Opcode: IdxGE P1 P2 P3 P4 P5
71775 ** Synopsis: key=r[P3@P4]
71776 **
71777 ** The P4 register values beginning with P3 form an unpacked index
71778 ** key that omits the PRIMARY KEY.  Compare this key value against the index
71779 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
71780 ** fields at the end.
71781 **
71782 ** If the P1 index entry is greater than or equal to the key value
71783 ** then jump to P2.  Otherwise fall through to the next instruction.
71784 */
71785 /* Opcode: IdxGT P1 P2 P3 P4 P5
71786 ** Synopsis: key=r[P3@P4]
71787 **
71788 ** The P4 register values beginning with P3 form an unpacked index
71789 ** key that omits the PRIMARY KEY.  Compare this key value against the index
71790 ** that P1 is currently pointing to, ignoring the PRIMARY KEY or ROWID
71791 ** fields at the end.
71792 **
71793 ** If the P1 index entry is greater than the key value
71794 ** then jump to P2.  Otherwise fall through to the next instruction.
71795 */
71796 /* Opcode: IdxLT P1 P2 P3 P4 P5
71797 ** Synopsis: key=r[P3@P4]
71798 **
71799 ** The P4 register values beginning with P3 form an unpacked index
71800 ** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
71801 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
71802 ** ROWID on the P1 index.
71803 **
71804 ** If the P1 index entry is less than the key value then jump to P2.
71805 ** Otherwise fall through to the next instruction.
71806 */
71807 /* Opcode: IdxLE P1 P2 P3 P4 P5
71808 ** Synopsis: key=r[P3@P4]
71809 **
71810 ** The P4 register values beginning with P3 form an unpacked index
71811 ** key that omits the PRIMARY KEY or ROWID.  Compare this key value against
71812 ** the index that P1 is currently pointing to, ignoring the PRIMARY KEY or
71813 ** ROWID on the P1 index.
71814 **
71815 ** If the P1 index entry is less than or equal to the key value then jump
71816 ** to P2. Otherwise fall through to the next instruction.
71817 */
71818 case OP_IdxLE:          /* jump */
71819 case OP_IdxGT:          /* jump */
71820 case OP_IdxLT:          /* jump */
71821 case OP_IdxGE:  {       /* jump */
71822   VdbeCursor *pC;
71823   int res;
71824   UnpackedRecord r;
71825 
71826   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71827   pC = p->apCsr[pOp->p1];
71828   assert( pC!=0 );
71829   assert( pC->isOrdered );
71830   assert( pC->pCursor!=0);
71831   assert( pC->deferredMoveto==0 );
71832   assert( pOp->p5==0 || pOp->p5==1 );
71833   assert( pOp->p4type==P4_INT32 );
71834   r.pKeyInfo = pC->pKeyInfo;
71835   r.nField = (u16)pOp->p4.i;
71836   if( pOp->opcode<OP_IdxLT ){
71837     assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxGT );
71838     r.default_rc = -1;
71839   }else{
71840     assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxLT );
71841     r.default_rc = 0;
71842   }
71843   r.aMem = &aMem[pOp->p3];
71844 #ifdef SQLITE_DEBUG
71845   { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71846 #endif
71847   res = 0;  /* Not needed.  Only used to silence a warning. */
71848   rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
71849   assert( (OP_IdxLE&1)==(OP_IdxLT&1) && (OP_IdxGE&1)==(OP_IdxGT&1) );
71850   if( (pOp->opcode&1)==(OP_IdxLT&1) ){
71851     assert( pOp->opcode==OP_IdxLE || pOp->opcode==OP_IdxLT );
71852     res = -res;
71853   }else{
71854     assert( pOp->opcode==OP_IdxGE || pOp->opcode==OP_IdxGT );
71855     res++;
71856   }
71857   VdbeBranchTaken(res>0,2);
71858   if( res>0 ){
71859     pc = pOp->p2 - 1 ;
71860   }
71861   break;
71862 }
71863 
71864 /* Opcode: Destroy P1 P2 P3 * *
71865 **
71866 ** Delete an entire database table or index whose root page in the database
71867 ** file is given by P1.
71868 **
71869 ** The table being destroyed is in the main database file if P3==0.  If
71870 ** P3==1 then the table to be clear is in the auxiliary database file
71871 ** that is used to store tables create using CREATE TEMPORARY TABLE.
71872 **
71873 ** If AUTOVACUUM is enabled then it is possible that another root page
71874 ** might be moved into the newly deleted root page in order to keep all
71875 ** root pages contiguous at the beginning of the database.  The former
71876 ** value of the root page that moved - its value before the move occurred -
71877 ** is stored in register P2.  If no page
71878 ** movement was required (because the table being dropped was already
71879 ** the last one in the database) then a zero is stored in register P2.
71880 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
71881 **
71882 ** See also: Clear
71883 */
71884 case OP_Destroy: {     /* out2-prerelease */
71885   int iMoved;
71886   int iCnt;
71887   Vdbe *pVdbe;
71888   int iDb;
71889 
71890   assert( p->readOnly==0 );
71891 #ifndef SQLITE_OMIT_VIRTUALTABLE
71892   iCnt = 0;
71893   for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
71894     if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
71895      && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
71896     ){
71897       iCnt++;
71898     }
71899   }
71900 #else
71901   iCnt = db->nVdbeRead;
71902 #endif
71903   pOut->flags = MEM_Null;
71904   if( iCnt>1 ){
71905     rc = SQLITE_LOCKED;
71906     p->errorAction = OE_Abort;
71907   }else{
71908     iDb = pOp->p3;
71909     assert( iCnt==1 );
71910     assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
71911     iMoved = 0;  /* Not needed.  Only to silence a warning. */
71912     rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
71913     pOut->flags = MEM_Int;
71914     pOut->u.i = iMoved;
71915 #ifndef SQLITE_OMIT_AUTOVACUUM
71916     if( rc==SQLITE_OK && iMoved!=0 ){
71917       sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
71918       /* All OP_Destroy operations occur on the same btree */
71919       assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
71920       resetSchemaOnFault = iDb+1;
71921     }
71922 #endif
71923   }
71924   break;
71925 }
71926 
71927 /* Opcode: Clear P1 P2 P3
71928 **
71929 ** Delete all contents of the database table or index whose root page
71930 ** in the database file is given by P1.  But, unlike Destroy, do not
71931 ** remove the table or index from the database file.
71932 **
71933 ** The table being clear is in the main database file if P2==0.  If
71934 ** P2==1 then the table to be clear is in the auxiliary database file
71935 ** that is used to store tables create using CREATE TEMPORARY TABLE.
71936 **
71937 ** If the P3 value is non-zero, then the table referred to must be an
71938 ** intkey table (an SQL table, not an index). In this case the row change
71939 ** count is incremented by the number of rows in the table being cleared.
71940 ** If P3 is greater than zero, then the value stored in register P3 is
71941 ** also incremented by the number of rows in the table being cleared.
71942 **
71943 ** See also: Destroy
71944 */
71945 case OP_Clear: {
71946   int nChange;
71947 
71948   nChange = 0;
71949   assert( p->readOnly==0 );
71950   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
71951   rc = sqlite3BtreeClearTable(
71952       db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
71953   );
71954   if( pOp->p3 ){
71955     p->nChange += nChange;
71956     if( pOp->p3>0 ){
71957       assert( memIsValid(&aMem[pOp->p3]) );
71958       memAboutToChange(p, &aMem[pOp->p3]);
71959       aMem[pOp->p3].u.i += nChange;
71960     }
71961   }
71962   break;
71963 }
71964 
71965 /* Opcode: CreateTable P1 P2 * * *
71966 ** Synopsis: r[P2]=root iDb=P1
71967 **
71968 ** Allocate a new table in the main database file if P1==0 or in the
71969 ** auxiliary database file if P1==1 or in an attached database if
71970 ** P1>1.  Write the root page number of the new table into
71971 ** register P2
71972 **
71973 ** The difference between a table and an index is this:  A table must
71974 ** have a 4-byte integer key and can have arbitrary data.  An index
71975 ** has an arbitrary key but no data.
71976 **
71977 ** See also: CreateIndex
71978 */
71979 /* Opcode: CreateIndex P1 P2 * * *
71980 ** Synopsis: r[P2]=root iDb=P1
71981 **
71982 ** Allocate a new index in the main database file if P1==0 or in the
71983 ** auxiliary database file if P1==1 or in an attached database if
71984 ** P1>1.  Write the root page number of the new table into
71985 ** register P2.
71986 **
71987 ** See documentation on OP_CreateTable for additional information.
71988 */
71989 case OP_CreateIndex:            /* out2-prerelease */
71990 case OP_CreateTable: {          /* out2-prerelease */
71991   int pgno;
71992   int flags;
71993   Db *pDb;
71994 
71995   pgno = 0;
71996   assert( pOp->p1>=0 && pOp->p1<db->nDb );
71997   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
71998   assert( p->readOnly==0 );
71999   pDb = &db->aDb[pOp->p1];
72000   assert( pDb->pBt!=0 );
72001   if( pOp->opcode==OP_CreateTable ){
72002     /* flags = BTREE_INTKEY; */
72003     flags = BTREE_INTKEY;
72004   }else{
72005     flags = BTREE_BLOBKEY;
72006   }
72007   rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
72008   pOut->u.i = pgno;
72009   break;
72010 }
72011 
72012 /* Opcode: ParseSchema P1 * * P4 *
72013 **
72014 ** Read and parse all entries from the SQLITE_MASTER table of database P1
72015 ** that match the WHERE clause P4.
72016 **
72017 ** This opcode invokes the parser to create a new virtual machine,
72018 ** then runs the new virtual machine.  It is thus a re-entrant opcode.
72019 */
72020 case OP_ParseSchema: {
72021   int iDb;
72022   const char *zMaster;
72023   char *zSql;
72024   InitData initData;
72025 
72026   /* Any prepared statement that invokes this opcode will hold mutexes
72027   ** on every btree.  This is a prerequisite for invoking
72028   ** sqlite3InitCallback().
72029   */
72030 #ifdef SQLITE_DEBUG
72031   for(iDb=0; iDb<db->nDb; iDb++){
72032     assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
72033   }
72034 #endif
72035 
72036   iDb = pOp->p1;
72037   assert( iDb>=0 && iDb<db->nDb );
72038   assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
72039   /* Used to be a conditional */ {
72040     zMaster = SCHEMA_TABLE(iDb);
72041     initData.db = db;
72042     initData.iDb = pOp->p1;
72043     initData.pzErrMsg = &p->zErrMsg;
72044     zSql = sqlite3MPrintf(db,
72045        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
72046        db->aDb[iDb].zName, zMaster, pOp->p4.z);
72047     if( zSql==0 ){
72048       rc = SQLITE_NOMEM;
72049     }else{
72050       assert( db->init.busy==0 );
72051       db->init.busy = 1;
72052       initData.rc = SQLITE_OK;
72053       assert( !db->mallocFailed );
72054       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
72055       if( rc==SQLITE_OK ) rc = initData.rc;
72056       sqlite3DbFree(db, zSql);
72057       db->init.busy = 0;
72058     }
72059   }
72060   if( rc ) sqlite3ResetAllSchemasOfConnection(db);
72061   if( rc==SQLITE_NOMEM ){
72062     goto no_mem;
72063   }
72064   break;
72065 }
72066 
72067 #if !defined(SQLITE_OMIT_ANALYZE)
72068 /* Opcode: LoadAnalysis P1 * * * *
72069 **
72070 ** Read the sqlite_stat1 table for database P1 and load the content
72071 ** of that table into the internal index hash table.  This will cause
72072 ** the analysis to be used when preparing all subsequent queries.
72073 */
72074 case OP_LoadAnalysis: {
72075   assert( pOp->p1>=0 && pOp->p1<db->nDb );
72076   rc = sqlite3AnalysisLoad(db, pOp->p1);
72077   break;
72078 }
72079 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
72080 
72081 /* Opcode: DropTable P1 * * P4 *
72082 **
72083 ** Remove the internal (in-memory) data structures that describe
72084 ** the table named P4 in database P1.  This is called after a table
72085 ** is dropped in order to keep the internal representation of the
72086 ** schema consistent with what is on disk.
72087 */
72088 case OP_DropTable: {
72089   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
72090   break;
72091 }
72092 
72093 /* Opcode: DropIndex P1 * * P4 *
72094 **
72095 ** Remove the internal (in-memory) data structures that describe
72096 ** the index named P4 in database P1.  This is called after an index
72097 ** is dropped in order to keep the internal representation of the
72098 ** schema consistent with what is on disk.
72099 */
72100 case OP_DropIndex: {
72101   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
72102   break;
72103 }
72104 
72105 /* Opcode: DropTrigger P1 * * P4 *
72106 **
72107 ** Remove the internal (in-memory) data structures that describe
72108 ** the trigger named P4 in database P1.  This is called after a trigger
72109 ** is dropped in order to keep the internal representation of the
72110 ** schema consistent with what is on disk.
72111 */
72112 case OP_DropTrigger: {
72113   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
72114   break;
72115 }
72116 
72117 
72118 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
72119 /* Opcode: IntegrityCk P1 P2 P3 * P5
72120 **
72121 ** Do an analysis of the currently open database.  Store in
72122 ** register P1 the text of an error message describing any problems.
72123 ** If no problems are found, store a NULL in register P1.
72124 **
72125 ** The register P3 contains the maximum number of allowed errors.
72126 ** At most reg(P3) errors will be reported.
72127 ** In other words, the analysis stops as soon as reg(P1) errors are
72128 ** seen.  Reg(P1) is updated with the number of errors remaining.
72129 **
72130 ** The root page numbers of all tables in the database are integer
72131 ** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
72132 ** total.
72133 **
72134 ** If P5 is not zero, the check is done on the auxiliary database
72135 ** file, not the main database file.
72136 **
72137 ** This opcode is used to implement the integrity_check pragma.
72138 */
72139 case OP_IntegrityCk: {
72140   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
72141   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
72142   int j;          /* Loop counter */
72143   int nErr;       /* Number of errors reported */
72144   char *z;        /* Text of the error report */
72145   Mem *pnErr;     /* Register keeping track of errors remaining */
72146 
72147   assert( p->bIsReader );
72148   nRoot = pOp->p2;
72149   assert( nRoot>0 );
72150   aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
72151   if( aRoot==0 ) goto no_mem;
72152   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
72153   pnErr = &aMem[pOp->p3];
72154   assert( (pnErr->flags & MEM_Int)!=0 );
72155   assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
72156   pIn1 = &aMem[pOp->p1];
72157   for(j=0; j<nRoot; j++){
72158     aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
72159   }
72160   aRoot[j] = 0;
72161   assert( pOp->p5<db->nDb );
72162   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
72163   z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
72164                                  (int)pnErr->u.i, &nErr);
72165   sqlite3DbFree(db, aRoot);
72166   pnErr->u.i -= nErr;
72167   sqlite3VdbeMemSetNull(pIn1);
72168   if( nErr==0 ){
72169     assert( z==0 );
72170   }else if( z==0 ){
72171     goto no_mem;
72172   }else{
72173     sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
72174   }
72175   UPDATE_MAX_BLOBSIZE(pIn1);
72176   sqlite3VdbeChangeEncoding(pIn1, encoding);
72177   break;
72178 }
72179 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
72180 
72181 /* Opcode: RowSetAdd P1 P2 * * *
72182 ** Synopsis:  rowset(P1)=r[P2]
72183 **
72184 ** Insert the integer value held by register P2 into a boolean index
72185 ** held in register P1.
72186 **
72187 ** An assertion fails if P2 is not an integer.
72188 */
72189 case OP_RowSetAdd: {       /* in1, in2 */
72190   pIn1 = &aMem[pOp->p1];
72191   pIn2 = &aMem[pOp->p2];
72192   assert( (pIn2->flags & MEM_Int)!=0 );
72193   if( (pIn1->flags & MEM_RowSet)==0 ){
72194     sqlite3VdbeMemSetRowSet(pIn1);
72195     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
72196   }
72197   sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
72198   break;
72199 }
72200 
72201 /* Opcode: RowSetRead P1 P2 P3 * *
72202 ** Synopsis:  r[P3]=rowset(P1)
72203 **
72204 ** Extract the smallest value from boolean index P1 and put that value into
72205 ** register P3.  Or, if boolean index P1 is initially empty, leave P3
72206 ** unchanged and jump to instruction P2.
72207 */
72208 case OP_RowSetRead: {       /* jump, in1, out3 */
72209   i64 val;
72210 
72211   pIn1 = &aMem[pOp->p1];
72212   if( (pIn1->flags & MEM_RowSet)==0
72213    || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
72214   ){
72215     /* The boolean index is empty */
72216     sqlite3VdbeMemSetNull(pIn1);
72217     pc = pOp->p2 - 1;
72218     VdbeBranchTaken(1,2);
72219   }else{
72220     /* A value was pulled from the index */
72221     sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
72222     VdbeBranchTaken(0,2);
72223   }
72224   goto check_for_interrupt;
72225 }
72226 
72227 /* Opcode: RowSetTest P1 P2 P3 P4
72228 ** Synopsis: if r[P3] in rowset(P1) goto P2
72229 **
72230 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
72231 ** contains a RowSet object and that RowSet object contains
72232 ** the value held in P3, jump to register P2. Otherwise, insert the
72233 ** integer in P3 into the RowSet and continue on to the
72234 ** next opcode.
72235 **
72236 ** The RowSet object is optimized for the case where successive sets
72237 ** of integers, where each set contains no duplicates. Each set
72238 ** of values is identified by a unique P4 value. The first set
72239 ** must have P4==0, the final set P4=-1.  P4 must be either -1 or
72240 ** non-negative.  For non-negative values of P4 only the lower 4
72241 ** bits are significant.
72242 **
72243 ** This allows optimizations: (a) when P4==0 there is no need to test
72244 ** the rowset object for P3, as it is guaranteed not to contain it,
72245 ** (b) when P4==-1 there is no need to insert the value, as it will
72246 ** never be tested for, and (c) when a value that is part of set X is
72247 ** inserted, there is no need to search to see if the same value was
72248 ** previously inserted as part of set X (only if it was previously
72249 ** inserted as part of some other set).
72250 */
72251 case OP_RowSetTest: {                     /* jump, in1, in3 */
72252   int iSet;
72253   int exists;
72254 
72255   pIn1 = &aMem[pOp->p1];
72256   pIn3 = &aMem[pOp->p3];
72257   iSet = pOp->p4.i;
72258   assert( pIn3->flags&MEM_Int );
72259 
72260   /* If there is anything other than a rowset object in memory cell P1,
72261   ** delete it now and initialize P1 with an empty rowset
72262   */
72263   if( (pIn1->flags & MEM_RowSet)==0 ){
72264     sqlite3VdbeMemSetRowSet(pIn1);
72265     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
72266   }
72267 
72268   assert( pOp->p4type==P4_INT32 );
72269   assert( iSet==-1 || iSet>=0 );
72270   if( iSet ){
72271     exists = sqlite3RowSetTest(pIn1->u.pRowSet,
72272                                (u8)(iSet>=0 ? iSet & 0xf : 0xff),
72273                                pIn3->u.i);
72274     VdbeBranchTaken(exists!=0,2);
72275     if( exists ){
72276       pc = pOp->p2 - 1;
72277       break;
72278     }
72279   }
72280   if( iSet>=0 ){
72281     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
72282   }
72283   break;
72284 }
72285 
72286 
72287 #ifndef SQLITE_OMIT_TRIGGER
72288 
72289 /* Opcode: Program P1 P2 P3 P4 P5
72290 **
72291 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
72292 **
72293 ** P1 contains the address of the memory cell that contains the first memory
72294 ** cell in an array of values used as arguments to the sub-program. P2
72295 ** contains the address to jump to if the sub-program throws an IGNORE
72296 ** exception using the RAISE() function. Register P3 contains the address
72297 ** of a memory cell in this (the parent) VM that is used to allocate the
72298 ** memory required by the sub-vdbe at runtime.
72299 **
72300 ** P4 is a pointer to the VM containing the trigger program.
72301 **
72302 ** If P5 is non-zero, then recursive program invocation is enabled.
72303 */
72304 case OP_Program: {        /* jump */
72305   int nMem;               /* Number of memory registers for sub-program */
72306   int nByte;              /* Bytes of runtime space required for sub-program */
72307   Mem *pRt;               /* Register to allocate runtime space */
72308   Mem *pMem;              /* Used to iterate through memory cells */
72309   Mem *pEnd;              /* Last memory cell in new array */
72310   VdbeFrame *pFrame;      /* New vdbe frame to execute in */
72311   SubProgram *pProgram;   /* Sub-program to execute */
72312   void *t;                /* Token identifying trigger */
72313 
72314   pProgram = pOp->p4.pProgram;
72315   pRt = &aMem[pOp->p3];
72316   assert( pProgram->nOp>0 );
72317 
72318   /* If the p5 flag is clear, then recursive invocation of triggers is
72319   ** disabled for backwards compatibility (p5 is set if this sub-program
72320   ** is really a trigger, not a foreign key action, and the flag set
72321   ** and cleared by the "PRAGMA recursive_triggers" command is clear).
72322   **
72323   ** It is recursive invocation of triggers, at the SQL level, that is
72324   ** disabled. In some cases a single trigger may generate more than one
72325   ** SubProgram (if the trigger may be executed with more than one different
72326   ** ON CONFLICT algorithm). SubProgram structures associated with a
72327   ** single trigger all have the same value for the SubProgram.token
72328   ** variable.  */
72329   if( pOp->p5 ){
72330     t = pProgram->token;
72331     for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
72332     if( pFrame ) break;
72333   }
72334 
72335   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
72336     rc = SQLITE_ERROR;
72337     sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
72338     break;
72339   }
72340 
72341   /* Register pRt is used to store the memory required to save the state
72342   ** of the current program, and the memory required at runtime to execute
72343   ** the trigger program. If this trigger has been fired before, then pRt
72344   ** is already allocated. Otherwise, it must be initialized.  */
72345   if( (pRt->flags&MEM_Frame)==0 ){
72346     /* SubProgram.nMem is set to the number of memory cells used by the
72347     ** program stored in SubProgram.aOp. As well as these, one memory
72348     ** cell is required for each cursor used by the program. Set local
72349     ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
72350     */
72351     nMem = pProgram->nMem + pProgram->nCsr;
72352     nByte = ROUND8(sizeof(VdbeFrame))
72353               + nMem * sizeof(Mem)
72354               + pProgram->nCsr * sizeof(VdbeCursor *)
72355               + pProgram->nOnce * sizeof(u8);
72356     pFrame = sqlite3DbMallocZero(db, nByte);
72357     if( !pFrame ){
72358       goto no_mem;
72359     }
72360     sqlite3VdbeMemRelease(pRt);
72361     pRt->flags = MEM_Frame;
72362     pRt->u.pFrame = pFrame;
72363 
72364     pFrame->v = p;
72365     pFrame->nChildMem = nMem;
72366     pFrame->nChildCsr = pProgram->nCsr;
72367     pFrame->pc = pc;
72368     pFrame->aMem = p->aMem;
72369     pFrame->nMem = p->nMem;
72370     pFrame->apCsr = p->apCsr;
72371     pFrame->nCursor = p->nCursor;
72372     pFrame->aOp = p->aOp;
72373     pFrame->nOp = p->nOp;
72374     pFrame->token = pProgram->token;
72375     pFrame->aOnceFlag = p->aOnceFlag;
72376     pFrame->nOnceFlag = p->nOnceFlag;
72377 
72378     pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
72379     for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
72380       pMem->flags = MEM_Undefined;
72381       pMem->db = db;
72382     }
72383   }else{
72384     pFrame = pRt->u.pFrame;
72385     assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
72386     assert( pProgram->nCsr==pFrame->nChildCsr );
72387     assert( pc==pFrame->pc );
72388   }
72389 
72390   p->nFrame++;
72391   pFrame->pParent = p->pFrame;
72392   pFrame->lastRowid = lastRowid;
72393   pFrame->nChange = p->nChange;
72394   p->nChange = 0;
72395   p->pFrame = pFrame;
72396   p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
72397   p->nMem = pFrame->nChildMem;
72398   p->nCursor = (u16)pFrame->nChildCsr;
72399   p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
72400   p->aOp = aOp = pProgram->aOp;
72401   p->nOp = pProgram->nOp;
72402   p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
72403   p->nOnceFlag = pProgram->nOnce;
72404   pc = -1;
72405   memset(p->aOnceFlag, 0, p->nOnceFlag);
72406 
72407   break;
72408 }
72409 
72410 /* Opcode: Param P1 P2 * * *
72411 **
72412 ** This opcode is only ever present in sub-programs called via the
72413 ** OP_Program instruction. Copy a value currently stored in a memory
72414 ** cell of the calling (parent) frame to cell P2 in the current frames
72415 ** address space. This is used by trigger programs to access the new.*
72416 ** and old.* values.
72417 **
72418 ** The address of the cell in the parent frame is determined by adding
72419 ** the value of the P1 argument to the value of the P1 argument to the
72420 ** calling OP_Program instruction.
72421 */
72422 case OP_Param: {           /* out2-prerelease */
72423   VdbeFrame *pFrame;
72424   Mem *pIn;
72425   pFrame = p->pFrame;
72426   pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
72427   sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
72428   break;
72429 }
72430 
72431 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
72432 
72433 #ifndef SQLITE_OMIT_FOREIGN_KEY
72434 /* Opcode: FkCounter P1 P2 * * *
72435 ** Synopsis: fkctr[P1]+=P2
72436 **
72437 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
72438 ** If P1 is non-zero, the database constraint counter is incremented
72439 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
72440 ** statement counter is incremented (immediate foreign key constraints).
72441 */
72442 case OP_FkCounter: {
72443   if( db->flags & SQLITE_DeferFKs ){
72444     db->nDeferredImmCons += pOp->p2;
72445   }else if( pOp->p1 ){
72446     db->nDeferredCons += pOp->p2;
72447   }else{
72448     p->nFkConstraint += pOp->p2;
72449   }
72450   break;
72451 }
72452 
72453 /* Opcode: FkIfZero P1 P2 * * *
72454 ** Synopsis: if fkctr[P1]==0 goto P2
72455 **
72456 ** This opcode tests if a foreign key constraint-counter is currently zero.
72457 ** If so, jump to instruction P2. Otherwise, fall through to the next
72458 ** instruction.
72459 **
72460 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
72461 ** is zero (the one that counts deferred constraint violations). If P1 is
72462 ** zero, the jump is taken if the statement constraint-counter is zero
72463 ** (immediate foreign key constraint violations).
72464 */
72465 case OP_FkIfZero: {         /* jump */
72466   if( pOp->p1 ){
72467     VdbeBranchTaken(db->nDeferredCons==0 && db->nDeferredImmCons==0, 2);
72468     if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
72469   }else{
72470     VdbeBranchTaken(p->nFkConstraint==0 && db->nDeferredImmCons==0, 2);
72471     if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
72472   }
72473   break;
72474 }
72475 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
72476 
72477 #ifndef SQLITE_OMIT_AUTOINCREMENT
72478 /* Opcode: MemMax P1 P2 * * *
72479 ** Synopsis: r[P1]=max(r[P1],r[P2])
72480 **
72481 ** P1 is a register in the root frame of this VM (the root frame is
72482 ** different from the current frame if this instruction is being executed
72483 ** within a sub-program). Set the value of register P1 to the maximum of
72484 ** its current value and the value in register P2.
72485 **
72486 ** This instruction throws an error if the memory cell is not initially
72487 ** an integer.
72488 */
72489 case OP_MemMax: {        /* in2 */
72490   VdbeFrame *pFrame;
72491   if( p->pFrame ){
72492     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
72493     pIn1 = &pFrame->aMem[pOp->p1];
72494   }else{
72495     pIn1 = &aMem[pOp->p1];
72496   }
72497   assert( memIsValid(pIn1) );
72498   sqlite3VdbeMemIntegerify(pIn1);
72499   pIn2 = &aMem[pOp->p2];
72500   sqlite3VdbeMemIntegerify(pIn2);
72501   if( pIn1->u.i<pIn2->u.i){
72502     pIn1->u.i = pIn2->u.i;
72503   }
72504   break;
72505 }
72506 #endif /* SQLITE_OMIT_AUTOINCREMENT */
72507 
72508 /* Opcode: IfPos P1 P2 * * *
72509 ** Synopsis: if r[P1]>0 goto P2
72510 **
72511 ** If the value of register P1 is 1 or greater, jump to P2.
72512 **
72513 ** It is illegal to use this instruction on a register that does
72514 ** not contain an integer.  An assertion fault will result if you try.
72515 */
72516 case OP_IfPos: {        /* jump, in1 */
72517   pIn1 = &aMem[pOp->p1];
72518   assert( pIn1->flags&MEM_Int );
72519   VdbeBranchTaken( pIn1->u.i>0, 2);
72520   if( pIn1->u.i>0 ){
72521      pc = pOp->p2 - 1;
72522   }
72523   break;
72524 }
72525 
72526 /* Opcode: IfNeg P1 P2 * * *
72527 ** Synopsis: if r[P1]<0 goto P2
72528 **
72529 ** If the value of register P1 is less than zero, jump to P2.
72530 **
72531 ** It is illegal to use this instruction on a register that does
72532 ** not contain an integer.  An assertion fault will result if you try.
72533 */
72534 case OP_IfNeg: {        /* jump, in1 */
72535   pIn1 = &aMem[pOp->p1];
72536   assert( pIn1->flags&MEM_Int );
72537   VdbeBranchTaken(pIn1->u.i<0, 2);
72538   if( pIn1->u.i<0 ){
72539      pc = pOp->p2 - 1;
72540   }
72541   break;
72542 }
72543 
72544 /* Opcode: IfZero P1 P2 P3 * *
72545 ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
72546 **
72547 ** The register P1 must contain an integer.  Add literal P3 to the
72548 ** value in register P1.  If the result is exactly 0, jump to P2.
72549 **
72550 ** It is illegal to use this instruction on a register that does
72551 ** not contain an integer.  An assertion fault will result if you try.
72552 */
72553 case OP_IfZero: {        /* jump, in1 */
72554   pIn1 = &aMem[pOp->p1];
72555   assert( pIn1->flags&MEM_Int );
72556   pIn1->u.i += pOp->p3;
72557   VdbeBranchTaken(pIn1->u.i==0, 2);
72558   if( pIn1->u.i==0 ){
72559      pc = pOp->p2 - 1;
72560   }
72561   break;
72562 }
72563 
72564 /* Opcode: AggStep * P2 P3 P4 P5
72565 ** Synopsis: accum=r[P3] step(r[P2@P5])
72566 **
72567 ** Execute the step function for an aggregate.  The
72568 ** function has P5 arguments.   P4 is a pointer to the FuncDef
72569 ** structure that specifies the function.  Use register
72570 ** P3 as the accumulator.
72571 **
72572 ** The P5 arguments are taken from register P2 and its
72573 ** successors.
72574 */
72575 case OP_AggStep: {
72576   int n;
72577   int i;
72578   Mem *pMem;
72579   Mem *pRec;
72580   sqlite3_context ctx;
72581   sqlite3_value **apVal;
72582 
72583   n = pOp->p5;
72584   assert( n>=0 );
72585   pRec = &aMem[pOp->p2];
72586   apVal = p->apArg;
72587   assert( apVal || n==0 );
72588   for(i=0; i<n; i++, pRec++){
72589     assert( memIsValid(pRec) );
72590     apVal[i] = pRec;
72591     memAboutToChange(p, pRec);
72592   }
72593   ctx.pFunc = pOp->p4.pFunc;
72594   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
72595   ctx.pMem = pMem = &aMem[pOp->p3];
72596   pMem->n++;
72597   ctx.s.flags = MEM_Null;
72598   ctx.s.z = 0;
72599   ctx.s.zMalloc = 0;
72600   ctx.s.xDel = 0;
72601   ctx.s.db = db;
72602   ctx.isError = 0;
72603   ctx.pColl = 0;
72604   ctx.skipFlag = 0;
72605   if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
72606     assert( pOp>p->aOp );
72607     assert( pOp[-1].p4type==P4_COLLSEQ );
72608     assert( pOp[-1].opcode==OP_CollSeq );
72609     ctx.pColl = pOp[-1].p4.pColl;
72610   }
72611   (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
72612   if( ctx.isError ){
72613     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
72614     rc = ctx.isError;
72615   }
72616   if( ctx.skipFlag ){
72617     assert( pOp[-1].opcode==OP_CollSeq );
72618     i = pOp[-1].p1;
72619     if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
72620   }
72621 
72622   sqlite3VdbeMemRelease(&ctx.s);
72623 
72624   break;
72625 }
72626 
72627 /* Opcode: AggFinal P1 P2 * P4 *
72628 ** Synopsis: accum=r[P1] N=P2
72629 **
72630 ** Execute the finalizer function for an aggregate.  P1 is
72631 ** the memory location that is the accumulator for the aggregate.
72632 **
72633 ** P2 is the number of arguments that the step function takes and
72634 ** P4 is a pointer to the FuncDef for this function.  The P2
72635 ** argument is not used by this opcode.  It is only there to disambiguate
72636 ** functions that can take varying numbers of arguments.  The
72637 ** P4 argument is only needed for the degenerate case where
72638 ** the step function was not previously called.
72639 */
72640 case OP_AggFinal: {
72641   Mem *pMem;
72642   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
72643   pMem = &aMem[pOp->p1];
72644   assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
72645   rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
72646   if( rc ){
72647     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
72648   }
72649   sqlite3VdbeChangeEncoding(pMem, encoding);
72650   UPDATE_MAX_BLOBSIZE(pMem);
72651   if( sqlite3VdbeMemTooBig(pMem) ){
72652     goto too_big;
72653   }
72654   break;
72655 }
72656 
72657 #ifndef SQLITE_OMIT_WAL
72658 /* Opcode: Checkpoint P1 P2 P3 * *
72659 **
72660 ** Checkpoint database P1. This is a no-op if P1 is not currently in
72661 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
72662 ** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
72663 ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
72664 ** WAL after the checkpoint into mem[P3+1] and the number of pages
72665 ** in the WAL that have been checkpointed after the checkpoint
72666 ** completes into mem[P3+2].  However on an error, mem[P3+1] and
72667 ** mem[P3+2] are initialized to -1.
72668 */
72669 case OP_Checkpoint: {
72670   int i;                          /* Loop counter */
72671   int aRes[3];                    /* Results */
72672   Mem *pMem;                      /* Write results here */
72673 
72674   assert( p->readOnly==0 );
72675   aRes[0] = 0;
72676   aRes[1] = aRes[2] = -1;
72677   assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
72678        || pOp->p2==SQLITE_CHECKPOINT_FULL
72679        || pOp->p2==SQLITE_CHECKPOINT_RESTART
72680   );
72681   rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
72682   if( rc==SQLITE_BUSY ){
72683     rc = SQLITE_OK;
72684     aRes[0] = 1;
72685   }
72686   for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
72687     sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
72688   }
72689   break;
72690 };
72691 #endif
72692 
72693 #ifndef SQLITE_OMIT_PRAGMA
72694 /* Opcode: JournalMode P1 P2 P3 * *
72695 **
72696 ** Change the journal mode of database P1 to P3. P3 must be one of the
72697 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
72698 ** modes (delete, truncate, persist, off and memory), this is a simple
72699 ** operation. No IO is required.
72700 **
72701 ** If changing into or out of WAL mode the procedure is more complicated.
72702 **
72703 ** Write a string containing the final journal-mode to register P2.
72704 */
72705 case OP_JournalMode: {    /* out2-prerelease */
72706   Btree *pBt;                     /* Btree to change journal mode of */
72707   Pager *pPager;                  /* Pager associated with pBt */
72708   int eNew;                       /* New journal mode */
72709   int eOld;                       /* The old journal mode */
72710 #ifndef SQLITE_OMIT_WAL
72711   const char *zFilename;          /* Name of database file for pPager */
72712 #endif
72713 
72714   eNew = pOp->p3;
72715   assert( eNew==PAGER_JOURNALMODE_DELETE
72716        || eNew==PAGER_JOURNALMODE_TRUNCATE
72717        || eNew==PAGER_JOURNALMODE_PERSIST
72718        || eNew==PAGER_JOURNALMODE_OFF
72719        || eNew==PAGER_JOURNALMODE_MEMORY
72720        || eNew==PAGER_JOURNALMODE_WAL
72721        || eNew==PAGER_JOURNALMODE_QUERY
72722   );
72723   assert( pOp->p1>=0 && pOp->p1<db->nDb );
72724   assert( p->readOnly==0 );
72725 
72726   pBt = db->aDb[pOp->p1].pBt;
72727   pPager = sqlite3BtreePager(pBt);
72728   eOld = sqlite3PagerGetJournalMode(pPager);
72729   if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
72730   if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
72731 
72732 #ifndef SQLITE_OMIT_WAL
72733   zFilename = sqlite3PagerFilename(pPager, 1);
72734 
72735   /* Do not allow a transition to journal_mode=WAL for a database
72736   ** in temporary storage or if the VFS does not support shared memory
72737   */
72738   if( eNew==PAGER_JOURNALMODE_WAL
72739    && (sqlite3Strlen30(zFilename)==0           /* Temp file */
72740        || !sqlite3PagerWalSupported(pPager))   /* No shared-memory support */
72741   ){
72742     eNew = eOld;
72743   }
72744 
72745   if( (eNew!=eOld)
72746    && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
72747   ){
72748     if( !db->autoCommit || db->nVdbeRead>1 ){
72749       rc = SQLITE_ERROR;
72750       sqlite3SetString(&p->zErrMsg, db,
72751           "cannot change %s wal mode from within a transaction",
72752           (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
72753       );
72754       break;
72755     }else{
72756 
72757       if( eOld==PAGER_JOURNALMODE_WAL ){
72758         /* If leaving WAL mode, close the log file. If successful, the call
72759         ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
72760         ** file. An EXCLUSIVE lock may still be held on the database file
72761         ** after a successful return.
72762         */
72763         rc = sqlite3PagerCloseWal(pPager);
72764         if( rc==SQLITE_OK ){
72765           sqlite3PagerSetJournalMode(pPager, eNew);
72766         }
72767       }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
72768         /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
72769         ** as an intermediate */
72770         sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
72771       }
72772 
72773       /* Open a transaction on the database file. Regardless of the journal
72774       ** mode, this transaction always uses a rollback journal.
72775       */
72776       assert( sqlite3BtreeIsInTrans(pBt)==0 );
72777       if( rc==SQLITE_OK ){
72778         rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
72779       }
72780     }
72781   }
72782 #endif /* ifndef SQLITE_OMIT_WAL */
72783 
72784   if( rc ){
72785     eNew = eOld;
72786   }
72787   eNew = sqlite3PagerSetJournalMode(pPager, eNew);
72788 
72789   pOut = &aMem[pOp->p2];
72790   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
72791   pOut->z = (char *)sqlite3JournalModename(eNew);
72792   pOut->n = sqlite3Strlen30(pOut->z);
72793   pOut->enc = SQLITE_UTF8;
72794   sqlite3VdbeChangeEncoding(pOut, encoding);
72795   break;
72796 };
72797 #endif /* SQLITE_OMIT_PRAGMA */
72798 
72799 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
72800 /* Opcode: Vacuum * * * * *
72801 **
72802 ** Vacuum the entire database.  This opcode will cause other virtual
72803 ** machines to be created and run.  It may not be called from within
72804 ** a transaction.
72805 */
72806 case OP_Vacuum: {
72807   assert( p->readOnly==0 );
72808   rc = sqlite3RunVacuum(&p->zErrMsg, db);
72809   break;
72810 }
72811 #endif
72812 
72813 #if !defined(SQLITE_OMIT_AUTOVACUUM)
72814 /* Opcode: IncrVacuum P1 P2 * * *
72815 **
72816 ** Perform a single step of the incremental vacuum procedure on
72817 ** the P1 database. If the vacuum has finished, jump to instruction
72818 ** P2. Otherwise, fall through to the next instruction.
72819 */
72820 case OP_IncrVacuum: {        /* jump */
72821   Btree *pBt;
72822 
72823   assert( pOp->p1>=0 && pOp->p1<db->nDb );
72824   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
72825   assert( p->readOnly==0 );
72826   pBt = db->aDb[pOp->p1].pBt;
72827   rc = sqlite3BtreeIncrVacuum(pBt);
72828   VdbeBranchTaken(rc==SQLITE_DONE,2);
72829   if( rc==SQLITE_DONE ){
72830     pc = pOp->p2 - 1;
72831     rc = SQLITE_OK;
72832   }
72833   break;
72834 }
72835 #endif
72836 
72837 /* Opcode: Expire P1 * * * *
72838 **
72839 ** Cause precompiled statements to become expired. An expired statement
72840 ** fails with an error code of SQLITE_SCHEMA if it is ever executed
72841 ** (via sqlite3_step()).
72842 **
72843 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
72844 ** then only the currently executing statement is affected.
72845 */
72846 case OP_Expire: {
72847   if( !pOp->p1 ){
72848     sqlite3ExpirePreparedStatements(db);
72849   }else{
72850     p->expired = 1;
72851   }
72852   break;
72853 }
72854 
72855 #ifndef SQLITE_OMIT_SHARED_CACHE
72856 /* Opcode: TableLock P1 P2 P3 P4 *
72857 ** Synopsis: iDb=P1 root=P2 write=P3
72858 **
72859 ** Obtain a lock on a particular table. This instruction is only used when
72860 ** the shared-cache feature is enabled.
72861 **
72862 ** P1 is the index of the database in sqlite3.aDb[] of the database
72863 ** on which the lock is acquired.  A readlock is obtained if P3==0 or
72864 ** a write lock if P3==1.
72865 **
72866 ** P2 contains the root-page of the table to lock.
72867 **
72868 ** P4 contains a pointer to the name of the table being locked. This is only
72869 ** used to generate an error message if the lock cannot be obtained.
72870 */
72871 case OP_TableLock: {
72872   u8 isWriteLock = (u8)pOp->p3;
72873   if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
72874     int p1 = pOp->p1;
72875     assert( p1>=0 && p1<db->nDb );
72876     assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
72877     assert( isWriteLock==0 || isWriteLock==1 );
72878     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
72879     if( (rc&0xFF)==SQLITE_LOCKED ){
72880       const char *z = pOp->p4.z;
72881       sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
72882     }
72883   }
72884   break;
72885 }
72886 #endif /* SQLITE_OMIT_SHARED_CACHE */
72887 
72888 #ifndef SQLITE_OMIT_VIRTUALTABLE
72889 /* Opcode: VBegin * * * P4 *
72890 **
72891 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
72892 ** xBegin method for that table.
72893 **
72894 ** Also, whether or not P4 is set, check that this is not being called from
72895 ** within a callback to a virtual table xSync() method. If it is, the error
72896 ** code will be set to SQLITE_LOCKED.
72897 */
72898 case OP_VBegin: {
72899   VTable *pVTab;
72900   pVTab = pOp->p4.pVtab;
72901   rc = sqlite3VtabBegin(db, pVTab);
72902   if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
72903   break;
72904 }
72905 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72906 
72907 #ifndef SQLITE_OMIT_VIRTUALTABLE
72908 /* Opcode: VCreate P1 * * P4 *
72909 **
72910 ** P4 is the name of a virtual table in database P1. Call the xCreate method
72911 ** for that table.
72912 */
72913 case OP_VCreate: {
72914   rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
72915   break;
72916 }
72917 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72918 
72919 #ifndef SQLITE_OMIT_VIRTUALTABLE
72920 /* Opcode: VDestroy P1 * * P4 *
72921 **
72922 ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
72923 ** of that table.
72924 */
72925 case OP_VDestroy: {
72926   p->inVtabMethod = 2;
72927   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
72928   p->inVtabMethod = 0;
72929   break;
72930 }
72931 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72932 
72933 #ifndef SQLITE_OMIT_VIRTUALTABLE
72934 /* Opcode: VOpen P1 * * P4 *
72935 **
72936 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
72937 ** P1 is a cursor number.  This opcode opens a cursor to the virtual
72938 ** table and stores that cursor in P1.
72939 */
72940 case OP_VOpen: {
72941   VdbeCursor *pCur;
72942   sqlite3_vtab_cursor *pVtabCursor;
72943   sqlite3_vtab *pVtab;
72944   sqlite3_module *pModule;
72945 
72946   assert( p->bIsReader );
72947   pCur = 0;
72948   pVtabCursor = 0;
72949   pVtab = pOp->p4.pVtab->pVtab;
72950   pModule = (sqlite3_module *)pVtab->pModule;
72951   assert(pVtab && pModule);
72952   rc = pModule->xOpen(pVtab, &pVtabCursor);
72953   sqlite3VtabImportErrmsg(p, pVtab);
72954   if( SQLITE_OK==rc ){
72955     /* Initialize sqlite3_vtab_cursor base class */
72956     pVtabCursor->pVtab = pVtab;
72957 
72958     /* Initialize vdbe cursor object */
72959     pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
72960     if( pCur ){
72961       pCur->pVtabCursor = pVtabCursor;
72962     }else{
72963       db->mallocFailed = 1;
72964       pModule->xClose(pVtabCursor);
72965     }
72966   }
72967   break;
72968 }
72969 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72970 
72971 #ifndef SQLITE_OMIT_VIRTUALTABLE
72972 /* Opcode: VFilter P1 P2 P3 P4 *
72973 ** Synopsis: iPlan=r[P3] zPlan='P4'
72974 **
72975 ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
72976 ** the filtered result set is empty.
72977 **
72978 ** P4 is either NULL or a string that was generated by the xBestIndex
72979 ** method of the module.  The interpretation of the P4 string is left
72980 ** to the module implementation.
72981 **
72982 ** This opcode invokes the xFilter method on the virtual table specified
72983 ** by P1.  The integer query plan parameter to xFilter is stored in register
72984 ** P3. Register P3+1 stores the argc parameter to be passed to the
72985 ** xFilter method. Registers P3+2..P3+1+argc are the argc
72986 ** additional parameters which are passed to
72987 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
72988 **
72989 ** A jump is made to P2 if the result set after filtering would be empty.
72990 */
72991 case OP_VFilter: {   /* jump */
72992   int nArg;
72993   int iQuery;
72994   const sqlite3_module *pModule;
72995   Mem *pQuery;
72996   Mem *pArgc;
72997   sqlite3_vtab_cursor *pVtabCursor;
72998   sqlite3_vtab *pVtab;
72999   VdbeCursor *pCur;
73000   int res;
73001   int i;
73002   Mem **apArg;
73003 
73004   pQuery = &aMem[pOp->p3];
73005   pArgc = &pQuery[1];
73006   pCur = p->apCsr[pOp->p1];
73007   assert( memIsValid(pQuery) );
73008   REGISTER_TRACE(pOp->p3, pQuery);
73009   assert( pCur->pVtabCursor );
73010   pVtabCursor = pCur->pVtabCursor;
73011   pVtab = pVtabCursor->pVtab;
73012   pModule = pVtab->pModule;
73013 
73014   /* Grab the index number and argc parameters */
73015   assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
73016   nArg = (int)pArgc->u.i;
73017   iQuery = (int)pQuery->u.i;
73018 
73019   /* Invoke the xFilter method */
73020   {
73021     res = 0;
73022     apArg = p->apArg;
73023     for(i = 0; i<nArg; i++){
73024       apArg[i] = &pArgc[i+1];
73025     }
73026 
73027     p->inVtabMethod = 1;
73028     rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
73029     p->inVtabMethod = 0;
73030     sqlite3VtabImportErrmsg(p, pVtab);
73031     if( rc==SQLITE_OK ){
73032       res = pModule->xEof(pVtabCursor);
73033     }
73034     VdbeBranchTaken(res!=0,2);
73035     if( res ){
73036       pc = pOp->p2 - 1;
73037     }
73038   }
73039   pCur->nullRow = 0;
73040 
73041   break;
73042 }
73043 #endif /* SQLITE_OMIT_VIRTUALTABLE */
73044 
73045 #ifndef SQLITE_OMIT_VIRTUALTABLE
73046 /* Opcode: VColumn P1 P2 P3 * *
73047 ** Synopsis: r[P3]=vcolumn(P2)
73048 **
73049 ** Store the value of the P2-th column of
73050 ** the row of the virtual-table that the
73051 ** P1 cursor is pointing to into register P3.
73052 */
73053 case OP_VColumn: {
73054   sqlite3_vtab *pVtab;
73055   const sqlite3_module *pModule;
73056   Mem *pDest;
73057   sqlite3_context sContext;
73058 
73059   VdbeCursor *pCur = p->apCsr[pOp->p1];
73060   assert( pCur->pVtabCursor );
73061   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
73062   pDest = &aMem[pOp->p3];
73063   memAboutToChange(p, pDest);
73064   if( pCur->nullRow ){
73065     sqlite3VdbeMemSetNull(pDest);
73066     break;
73067   }
73068   pVtab = pCur->pVtabCursor->pVtab;
73069   pModule = pVtab->pModule;
73070   assert( pModule->xColumn );
73071   memset(&sContext, 0, sizeof(sContext));
73072 
73073   /* The output cell may already have a buffer allocated. Move
73074   ** the current contents to sContext.s so in case the user-function
73075   ** can use the already allocated buffer instead of allocating a
73076   ** new one.
73077   */
73078   sqlite3VdbeMemMove(&sContext.s, pDest);
73079   MemSetTypeFlag(&sContext.s, MEM_Null);
73080 
73081   rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
73082   sqlite3VtabImportErrmsg(p, pVtab);
73083   if( sContext.isError ){
73084     rc = sContext.isError;
73085   }
73086 
73087   /* Copy the result of the function to the P3 register. We
73088   ** do this regardless of whether or not an error occurred to ensure any
73089   ** dynamic allocation in sContext.s (a Mem struct) is  released.
73090   */
73091   sqlite3VdbeChangeEncoding(&sContext.s, encoding);
73092   sqlite3VdbeMemMove(pDest, &sContext.s);
73093   REGISTER_TRACE(pOp->p3, pDest);
73094   UPDATE_MAX_BLOBSIZE(pDest);
73095 
73096   if( sqlite3VdbeMemTooBig(pDest) ){
73097     goto too_big;
73098   }
73099   break;
73100 }
73101 #endif /* SQLITE_OMIT_VIRTUALTABLE */
73102 
73103 #ifndef SQLITE_OMIT_VIRTUALTABLE
73104 /* Opcode: VNext P1 P2 * * *
73105 **
73106 ** Advance virtual table P1 to the next row in its result set and
73107 ** jump to instruction P2.  Or, if the virtual table has reached
73108 ** the end of its result set, then fall through to the next instruction.
73109 */
73110 case OP_VNext: {   /* jump */
73111   sqlite3_vtab *pVtab;
73112   const sqlite3_module *pModule;
73113   int res;
73114   VdbeCursor *pCur;
73115 
73116   res = 0;
73117   pCur = p->apCsr[pOp->p1];
73118   assert( pCur->pVtabCursor );
73119   if( pCur->nullRow ){
73120     break;
73121   }
73122   pVtab = pCur->pVtabCursor->pVtab;
73123   pModule = pVtab->pModule;
73124   assert( pModule->xNext );
73125 
73126   /* Invoke the xNext() method of the module. There is no way for the
73127   ** underlying implementation to return an error if one occurs during
73128   ** xNext(). Instead, if an error occurs, true is returned (indicating that
73129   ** data is available) and the error code returned when xColumn or
73130   ** some other method is next invoked on the save virtual table cursor.
73131   */
73132   p->inVtabMethod = 1;
73133   rc = pModule->xNext(pCur->pVtabCursor);
73134   p->inVtabMethod = 0;
73135   sqlite3VtabImportErrmsg(p, pVtab);
73136   if( rc==SQLITE_OK ){
73137     res = pModule->xEof(pCur->pVtabCursor);
73138   }
73139   VdbeBranchTaken(!res,2);
73140   if( !res ){
73141     /* If there is data, jump to P2 */
73142     pc = pOp->p2 - 1;
73143   }
73144   goto check_for_interrupt;
73145 }
73146 #endif /* SQLITE_OMIT_VIRTUALTABLE */
73147 
73148 #ifndef SQLITE_OMIT_VIRTUALTABLE
73149 /* Opcode: VRename P1 * * P4 *
73150 **
73151 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
73152 ** This opcode invokes the corresponding xRename method. The value
73153 ** in register P1 is passed as the zName argument to the xRename method.
73154 */
73155 case OP_VRename: {
73156   sqlite3_vtab *pVtab;
73157   Mem *pName;
73158 
73159   pVtab = pOp->p4.pVtab->pVtab;
73160   pName = &aMem[pOp->p1];
73161   assert( pVtab->pModule->xRename );
73162   assert( memIsValid(pName) );
73163   assert( p->readOnly==0 );
73164   REGISTER_TRACE(pOp->p1, pName);
73165   assert( pName->flags & MEM_Str );
73166   testcase( pName->enc==SQLITE_UTF8 );
73167   testcase( pName->enc==SQLITE_UTF16BE );
73168   testcase( pName->enc==SQLITE_UTF16LE );
73169   rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
73170   if( rc==SQLITE_OK ){
73171     rc = pVtab->pModule->xRename(pVtab, pName->z);
73172     sqlite3VtabImportErrmsg(p, pVtab);
73173     p->expired = 0;
73174   }
73175   break;
73176 }
73177 #endif
73178 
73179 #ifndef SQLITE_OMIT_VIRTUALTABLE
73180 /* Opcode: VUpdate P1 P2 P3 P4 P5
73181 ** Synopsis: data=r[P3@P2]
73182 **
73183 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
73184 ** This opcode invokes the corresponding xUpdate method. P2 values
73185 ** are contiguous memory cells starting at P3 to pass to the xUpdate
73186 ** invocation. The value in register (P3+P2-1) corresponds to the
73187 ** p2th element of the argv array passed to xUpdate.
73188 **
73189 ** The xUpdate method will do a DELETE or an INSERT or both.
73190 ** The argv[0] element (which corresponds to memory cell P3)
73191 ** is the rowid of a row to delete.  If argv[0] is NULL then no
73192 ** deletion occurs.  The argv[1] element is the rowid of the new
73193 ** row.  This can be NULL to have the virtual table select the new
73194 ** rowid for itself.  The subsequent elements in the array are
73195 ** the values of columns in the new row.
73196 **
73197 ** If P2==1 then no insert is performed.  argv[0] is the rowid of
73198 ** a row to delete.
73199 **
73200 ** P1 is a boolean flag. If it is set to true and the xUpdate call
73201 ** is successful, then the value returned by sqlite3_last_insert_rowid()
73202 ** is set to the value of the rowid for the row just inserted.
73203 **
73204 ** P5 is the error actions (OE_Replace, OE_Fail, OE_Ignore, etc) to
73205 ** apply in the case of a constraint failure on an insert or update.
73206 */
73207 case OP_VUpdate: {
73208   sqlite3_vtab *pVtab;
73209   sqlite3_module *pModule;
73210   int nArg;
73211   int i;
73212   sqlite_int64 rowid;
73213   Mem **apArg;
73214   Mem *pX;
73215 
73216   assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
73217        || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
73218   );
73219   assert( p->readOnly==0 );
73220   pVtab = pOp->p4.pVtab->pVtab;
73221   pModule = (sqlite3_module *)pVtab->pModule;
73222   nArg = pOp->p2;
73223   assert( pOp->p4type==P4_VTAB );
73224   if( ALWAYS(pModule->xUpdate) ){
73225     u8 vtabOnConflict = db->vtabOnConflict;
73226     apArg = p->apArg;
73227     pX = &aMem[pOp->p3];
73228     for(i=0; i<nArg; i++){
73229       assert( memIsValid(pX) );
73230       memAboutToChange(p, pX);
73231       apArg[i] = pX;
73232       pX++;
73233     }
73234     db->vtabOnConflict = pOp->p5;
73235     rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
73236     db->vtabOnConflict = vtabOnConflict;
73237     sqlite3VtabImportErrmsg(p, pVtab);
73238     if( rc==SQLITE_OK && pOp->p1 ){
73239       assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
73240       db->lastRowid = lastRowid = rowid;
73241     }
73242     if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
73243       if( pOp->p5==OE_Ignore ){
73244         rc = SQLITE_OK;
73245       }else{
73246         p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
73247       }
73248     }else{
73249       p->nChange++;
73250     }
73251   }
73252   break;
73253 }
73254 #endif /* SQLITE_OMIT_VIRTUALTABLE */
73255 
73256 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
73257 /* Opcode: Pagecount P1 P2 * * *
73258 **
73259 ** Write the current number of pages in database P1 to memory cell P2.
73260 */
73261 case OP_Pagecount: {            /* out2-prerelease */
73262   pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
73263   break;
73264 }
73265 #endif
73266 
73267 
73268 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
73269 /* Opcode: MaxPgcnt P1 P2 P3 * *
73270 **
73271 ** Try to set the maximum page count for database P1 to the value in P3.
73272 ** Do not let the maximum page count fall below the current page count and
73273 ** do not change the maximum page count value if P3==0.
73274 **
73275 ** Store the maximum page count after the change in register P2.
73276 */
73277 case OP_MaxPgcnt: {            /* out2-prerelease */
73278   unsigned int newMax;
73279   Btree *pBt;
73280 
73281   pBt = db->aDb[pOp->p1].pBt;
73282   newMax = 0;
73283   if( pOp->p3 ){
73284     newMax = sqlite3BtreeLastPage(pBt);
73285     if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
73286   }
73287   pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
73288   break;
73289 }
73290 #endif
73291 
73292 
73293 /* Opcode: Init * P2 * P4 *
73294 ** Synopsis:  Start at P2
73295 **
73296 ** Programs contain a single instance of this opcode as the very first
73297 ** opcode.
73298 **
73299 ** If tracing is enabled (by the sqlite3_trace()) interface, then
73300 ** the UTF-8 string contained in P4 is emitted on the trace callback.
73301 ** Or if P4 is blank, use the string returned by sqlite3_sql().
73302 **
73303 ** If P2 is not zero, jump to instruction P2.
73304 */
73305 case OP_Init: {          /* jump */
73306   char *zTrace;
73307   char *z;
73308 
73309   if( pOp->p2 ){
73310     pc = pOp->p2 - 1;
73311   }
73312 #ifndef SQLITE_OMIT_TRACE
73313   if( db->xTrace
73314    && !p->doingRerun
73315    && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
73316   ){
73317     z = sqlite3VdbeExpandSql(p, zTrace);
73318     db->xTrace(db->pTraceArg, z);
73319     sqlite3DbFree(db, z);
73320   }
73321 #ifdef SQLITE_USE_FCNTL_TRACE
73322   zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
73323   if( zTrace ){
73324     int i;
73325     for(i=0; i<db->nDb; i++){
73326       if( MASKBIT(i) & p->btreeMask)==0 ) continue;
73327       sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
73328     }
73329   }
73330 #endif /* SQLITE_USE_FCNTL_TRACE */
73331 #ifdef SQLITE_DEBUG
73332   if( (db->flags & SQLITE_SqlTrace)!=0
73333    && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
73334   ){
73335     sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
73336   }
73337 #endif /* SQLITE_DEBUG */
73338 #endif /* SQLITE_OMIT_TRACE */
73339   break;
73340 }
73341 
73342 
73343 /* Opcode: Noop * * * * *
73344 **
73345 ** Do nothing.  This instruction is often useful as a jump
73346 ** destination.
73347 */
73348 /*
73349 ** The magic Explain opcode are only inserted when explain==2 (which
73350 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
73351 ** This opcode records information from the optimizer.  It is the
73352 ** the same as a no-op.  This opcodesnever appears in a real VM program.
73353 */
73354 default: {          /* This is really OP_Noop and OP_Explain */
73355   assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
73356   break;
73357 }
73358 
73359 /*****************************************************************************
73360 ** The cases of the switch statement above this line should all be indented
73361 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
73362 ** readability.  From this point on down, the normal indentation rules are
73363 ** restored.
73364 *****************************************************************************/
73365     }
73366 
73367 #ifdef VDBE_PROFILE
73368     {
73369       u64 elapsed = sqlite3Hwtime() - start;
73370       pOp->cycles += elapsed;
73371       pOp->cnt++;
73372     }
73373 #endif
73374 
73375     /* The following code adds nothing to the actual functionality
73376     ** of the program.  It is only here for testing and debugging.
73377     ** On the other hand, it does burn CPU cycles every time through
73378     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
73379     */
73380 #ifndef NDEBUG
73381     assert( pc>=-1 && pc<p->nOp );
73382 
73383 #ifdef SQLITE_DEBUG
73384     if( db->flags & SQLITE_VdbeTrace ){
73385       if( rc!=0 ) printf("rc=%d\n",rc);
73386       if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
73387         registerTrace(pOp->p2, &aMem[pOp->p2]);
73388       }
73389       if( pOp->opflags & OPFLG_OUT3 ){
73390         registerTrace(pOp->p3, &aMem[pOp->p3]);
73391       }
73392     }
73393 #endif  /* SQLITE_DEBUG */
73394 #endif  /* NDEBUG */
73395   }  /* The end of the for(;;) loop the loops through opcodes */
73396 
73397   /* If we reach this point, it means that execution is finished with
73398   ** an error of some kind.
73399   */
73400 vdbe_error_halt:
73401   assert( rc );
73402   p->rc = rc;
73403   testcase( sqlite3GlobalConfig.xLog!=0 );
73404   sqlite3_log(rc, "statement aborts at %d: [%s] %s",
73405                    pc, p->zSql, p->zErrMsg);
73406   sqlite3VdbeHalt(p);
73407   if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
73408   rc = SQLITE_ERROR;
73409   if( resetSchemaOnFault>0 ){
73410     sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
73411   }
73412 
73413   /* This is the only way out of this procedure.  We have to
73414   ** release the mutexes on btrees that were acquired at the
73415   ** top. */
73416 vdbe_return:
73417   db->lastRowid = lastRowid;
73418   testcase( nVmStep>0 );
73419   p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
73420   sqlite3VdbeLeave(p);
73421   return rc;
73422 
73423   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
73424   ** is encountered.
73425   */
73426 too_big:
73427   sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
73428   rc = SQLITE_TOOBIG;
73429   goto vdbe_error_halt;
73430 
73431   /* Jump to here if a malloc() fails.
73432   */
73433 no_mem:
73434   db->mallocFailed = 1;
73435   sqlite3SetString(&p->zErrMsg, db, "out of memory");
73436   rc = SQLITE_NOMEM;
73437   goto vdbe_error_halt;
73438 
73439   /* Jump to here for any other kind of fatal error.  The "rc" variable
73440   ** should hold the error number.
73441   */
73442 abort_due_to_error:
73443   assert( p->zErrMsg==0 );
73444   if( db->mallocFailed ) rc = SQLITE_NOMEM;
73445   if( rc!=SQLITE_IOERR_NOMEM ){
73446     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
73447   }
73448   goto vdbe_error_halt;
73449 
73450   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
73451   ** flag.
73452   */
73453 abort_due_to_interrupt:
73454   assert( db->u1.isInterrupted );
73455   rc = SQLITE_INTERRUPT;
73456   p->rc = rc;
73457   sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
73458   goto vdbe_error_halt;
73459 }
73460 
73461 
73462 /************** End of vdbe.c ************************************************/
73463 /************** Begin file vdbeblob.c ****************************************/
73464 /*
73465 ** 2007 May 1
73466 **
73467 ** The author disclaims copyright to this source code.  In place of
73468 ** a legal notice, here is a blessing:
73469 **
73470 **    May you do good and not evil.
73471 **    May you find forgiveness for yourself and forgive others.
73472 **    May you share freely, never taking more than you give.
73473 **
73474 *************************************************************************
73475 **
73476 ** This file contains code used to implement incremental BLOB I/O.
73477 */
73478 
73479 
73480 #ifndef SQLITE_OMIT_INCRBLOB
73481 
73482 /*
73483 ** Valid sqlite3_blob* handles point to Incrblob structures.
73484 */
73485 typedef struct Incrblob Incrblob;
73486 struct Incrblob {
73487   int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
73488   int nByte;              /* Size of open blob, in bytes */
73489   int iOffset;            /* Byte offset of blob in cursor data */
73490   int iCol;               /* Table column this handle is open on */
73491   BtCursor *pCsr;         /* Cursor pointing at blob row */
73492   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
73493   sqlite3 *db;            /* The associated database */
73494 };
73495 
73496 
73497 /*
73498 ** This function is used by both blob_open() and blob_reopen(). It seeks
73499 ** the b-tree cursor associated with blob handle p to point to row iRow.
73500 ** If successful, SQLITE_OK is returned and subsequent calls to
73501 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
73502 **
73503 ** If an error occurs, or if the specified row does not exist or does not
73504 ** contain a value of type TEXT or BLOB in the column nominated when the
73505 ** blob handle was opened, then an error code is returned and *pzErr may
73506 ** be set to point to a buffer containing an error message. It is the
73507 ** responsibility of the caller to free the error message buffer using
73508 ** sqlite3DbFree().
73509 **
73510 ** If an error does occur, then the b-tree cursor is closed. All subsequent
73511 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
73512 ** immediately return SQLITE_ABORT.
73513 */
73514 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
73515   int rc;                         /* Error code */
73516   char *zErr = 0;                 /* Error message */
73517   Vdbe *v = (Vdbe *)p->pStmt;
73518 
73519   /* Set the value of the SQL statements only variable to integer iRow.
73520   ** This is done directly instead of using sqlite3_bind_int64() to avoid
73521   ** triggering asserts related to mutexes.
73522   */
73523   assert( v->aVar[0].flags&MEM_Int );
73524   v->aVar[0].u.i = iRow;
73525 
73526   rc = sqlite3_step(p->pStmt);
73527   if( rc==SQLITE_ROW ){
73528     VdbeCursor *pC = v->apCsr[0];
73529     u32 type = pC->aType[p->iCol];
73530     if( type<12 ){
73531       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
73532           type==0?"null": type==7?"real": "integer"
73533       );
73534       rc = SQLITE_ERROR;
73535       sqlite3_finalize(p->pStmt);
73536       p->pStmt = 0;
73537     }else{
73538       p->iOffset = pC->aType[p->iCol + pC->nField];
73539       p->nByte = sqlite3VdbeSerialTypeLen(type);
73540       p->pCsr =  pC->pCursor;
73541       sqlite3BtreeEnterCursor(p->pCsr);
73542       sqlite3BtreeCacheOverflow(p->pCsr);
73543       sqlite3BtreeLeaveCursor(p->pCsr);
73544     }
73545   }
73546 
73547   if( rc==SQLITE_ROW ){
73548     rc = SQLITE_OK;
73549   }else if( p->pStmt ){
73550     rc = sqlite3_finalize(p->pStmt);
73551     p->pStmt = 0;
73552     if( rc==SQLITE_OK ){
73553       zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
73554       rc = SQLITE_ERROR;
73555     }else{
73556       zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
73557     }
73558   }
73559 
73560   assert( rc!=SQLITE_OK || zErr==0 );
73561   assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
73562 
73563   *pzErr = zErr;
73564   return rc;
73565 }
73566 
73567 /*
73568 ** Open a blob handle.
73569 */
73570 SQLITE_API int sqlite3_blob_open(
73571   sqlite3* db,            /* The database connection */
73572   const char *zDb,        /* The attached database containing the blob */
73573   const char *zTable,     /* The table containing the blob */
73574   const char *zColumn,    /* The column containing the blob */
73575   sqlite_int64 iRow,      /* The row containing the glob */
73576   int flags,              /* True -> read/write access, false -> read-only */
73577   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
73578 ){
73579   int nAttempt = 0;
73580   int iCol;               /* Index of zColumn in row-record */
73581 
73582   /* This VDBE program seeks a btree cursor to the identified
73583   ** db/table/row entry. The reason for using a vdbe program instead
73584   ** of writing code to use the b-tree layer directly is that the
73585   ** vdbe program will take advantage of the various transaction,
73586   ** locking and error handling infrastructure built into the vdbe.
73587   **
73588   ** After seeking the cursor, the vdbe executes an OP_ResultRow.
73589   ** Code external to the Vdbe then "borrows" the b-tree cursor and
73590   ** uses it to implement the blob_read(), blob_write() and
73591   ** blob_bytes() functions.
73592   **
73593   ** The sqlite3_blob_close() function finalizes the vdbe program,
73594   ** which closes the b-tree cursor and (possibly) commits the
73595   ** transaction.
73596   */
73597   static const int iLn = VDBE_OFFSET_LINENO(4);
73598   static const VdbeOpList openBlob[] = {
73599     /* {OP_Transaction, 0, 0, 0},  // 0: Inserted separately */
73600     {OP_TableLock, 0, 0, 0},       /* 1: Acquire a read or write lock */
73601     /* One of the following two instructions is replaced by an OP_Noop. */
73602     {OP_OpenRead, 0, 0, 0},        /* 2: Open cursor 0 for reading */
73603     {OP_OpenWrite, 0, 0, 0},       /* 3: Open cursor 0 for read/write */
73604     {OP_Variable, 1, 1, 1},        /* 4: Push the rowid to the stack */
73605     {OP_NotExists, 0, 10, 1},      /* 5: Seek the cursor */
73606     {OP_Column, 0, 0, 1},          /* 6  */
73607     {OP_ResultRow, 1, 0, 0},       /* 7  */
73608     {OP_Goto, 0, 4, 0},            /* 8  */
73609     {OP_Close, 0, 0, 0},           /* 9  */
73610     {OP_Halt, 0, 0, 0},            /* 10 */
73611   };
73612 
73613   int rc = SQLITE_OK;
73614   char *zErr = 0;
73615   Table *pTab;
73616   Parse *pParse = 0;
73617   Incrblob *pBlob = 0;
73618 
73619   flags = !!flags;                /* flags = (flags ? 1 : 0); */
73620   *ppBlob = 0;
73621 
73622   sqlite3_mutex_enter(db->mutex);
73623 
73624   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
73625   if( !pBlob ) goto blob_open_out;
73626   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
73627   if( !pParse ) goto blob_open_out;
73628 
73629   do {
73630     memset(pParse, 0, sizeof(Parse));
73631     pParse->db = db;
73632     sqlite3DbFree(db, zErr);
73633     zErr = 0;
73634 
73635     sqlite3BtreeEnterAll(db);
73636     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
73637     if( pTab && IsVirtual(pTab) ){
73638       pTab = 0;
73639       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
73640     }
73641     if( pTab && !HasRowid(pTab) ){
73642       pTab = 0;
73643       sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
73644     }
73645 #ifndef SQLITE_OMIT_VIEW
73646     if( pTab && pTab->pSelect ){
73647       pTab = 0;
73648       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
73649     }
73650 #endif
73651     if( !pTab ){
73652       if( pParse->zErrMsg ){
73653         sqlite3DbFree(db, zErr);
73654         zErr = pParse->zErrMsg;
73655         pParse->zErrMsg = 0;
73656       }
73657       rc = SQLITE_ERROR;
73658       sqlite3BtreeLeaveAll(db);
73659       goto blob_open_out;
73660     }
73661 
73662     /* Now search pTab for the exact column. */
73663     for(iCol=0; iCol<pTab->nCol; iCol++) {
73664       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
73665         break;
73666       }
73667     }
73668     if( iCol==pTab->nCol ){
73669       sqlite3DbFree(db, zErr);
73670       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
73671       rc = SQLITE_ERROR;
73672       sqlite3BtreeLeaveAll(db);
73673       goto blob_open_out;
73674     }
73675 
73676     /* If the value is being opened for writing, check that the
73677     ** column is not indexed, and that it is not part of a foreign key.
73678     ** It is against the rules to open a column to which either of these
73679     ** descriptions applies for writing.  */
73680     if( flags ){
73681       const char *zFault = 0;
73682       Index *pIdx;
73683 #ifndef SQLITE_OMIT_FOREIGN_KEY
73684       if( db->flags&SQLITE_ForeignKeys ){
73685         /* Check that the column is not part of an FK child key definition. It
73686         ** is not necessary to check if it is part of a parent key, as parent
73687         ** key columns must be indexed. The check below will pick up this
73688         ** case.  */
73689         FKey *pFKey;
73690         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
73691           int j;
73692           for(j=0; j<pFKey->nCol; j++){
73693             if( pFKey->aCol[j].iFrom==iCol ){
73694               zFault = "foreign key";
73695             }
73696           }
73697         }
73698       }
73699 #endif
73700       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
73701         int j;
73702         for(j=0; j<pIdx->nKeyCol; j++){
73703           if( pIdx->aiColumn[j]==iCol ){
73704             zFault = "indexed";
73705           }
73706         }
73707       }
73708       if( zFault ){
73709         sqlite3DbFree(db, zErr);
73710         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
73711         rc = SQLITE_ERROR;
73712         sqlite3BtreeLeaveAll(db);
73713         goto blob_open_out;
73714       }
73715     }
73716 
73717     pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
73718     assert( pBlob->pStmt || db->mallocFailed );
73719     if( pBlob->pStmt ){
73720       Vdbe *v = (Vdbe *)pBlob->pStmt;
73721       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
73722 
73723 
73724       sqlite3VdbeAddOp4Int(v, OP_Transaction, iDb, flags,
73725                            pTab->pSchema->schema_cookie,
73726                            pTab->pSchema->iGeneration);
73727       sqlite3VdbeChangeP5(v, 1);
73728       sqlite3VdbeAddOpList(v, ArraySize(openBlob), openBlob, iLn);
73729 
73730       /* Make sure a mutex is held on the table to be accessed */
73731       sqlite3VdbeUsesBtree(v, iDb);
73732 
73733       /* Configure the OP_TableLock instruction */
73734 #ifdef SQLITE_OMIT_SHARED_CACHE
73735       sqlite3VdbeChangeToNoop(v, 1);
73736 #else
73737       sqlite3VdbeChangeP1(v, 1, iDb);
73738       sqlite3VdbeChangeP2(v, 1, pTab->tnum);
73739       sqlite3VdbeChangeP3(v, 1, flags);
73740       sqlite3VdbeChangeP4(v, 1, pTab->zName, P4_TRANSIENT);
73741 #endif
73742 
73743       /* Remove either the OP_OpenWrite or OpenRead. Set the P2
73744       ** parameter of the other to pTab->tnum.  */
73745       sqlite3VdbeChangeToNoop(v, 3 - flags);
73746       sqlite3VdbeChangeP2(v, 2 + flags, pTab->tnum);
73747       sqlite3VdbeChangeP3(v, 2 + flags, iDb);
73748 
73749       /* Configure the number of columns. Configure the cursor to
73750       ** think that the table has one more column than it really
73751       ** does. An OP_Column to retrieve this imaginary column will
73752       ** always return an SQL NULL. This is useful because it means
73753       ** we can invoke OP_Column to fill in the vdbe cursors type
73754       ** and offset cache without causing any IO.
73755       */
73756       sqlite3VdbeChangeP4(v, 2+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
73757       sqlite3VdbeChangeP2(v, 6, pTab->nCol);
73758       if( !db->mallocFailed ){
73759         pParse->nVar = 1;
73760         pParse->nMem = 1;
73761         pParse->nTab = 1;
73762         sqlite3VdbeMakeReady(v, pParse);
73763       }
73764     }
73765 
73766     pBlob->flags = flags;
73767     pBlob->iCol = iCol;
73768     pBlob->db = db;
73769     sqlite3BtreeLeaveAll(db);
73770     if( db->mallocFailed ){
73771       goto blob_open_out;
73772     }
73773     sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
73774     rc = blobSeekToRow(pBlob, iRow, &zErr);
73775   } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
73776 
73777 blob_open_out:
73778   if( rc==SQLITE_OK && db->mallocFailed==0 ){
73779     *ppBlob = (sqlite3_blob *)pBlob;
73780   }else{
73781     if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
73782     sqlite3DbFree(db, pBlob);
73783   }
73784   sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
73785   sqlite3DbFree(db, zErr);
73786   sqlite3ParserReset(pParse);
73787   sqlite3StackFree(db, pParse);
73788   rc = sqlite3ApiExit(db, rc);
73789   sqlite3_mutex_leave(db->mutex);
73790   return rc;
73791 }
73792 
73793 /*
73794 ** Close a blob handle that was previously created using
73795 ** sqlite3_blob_open().
73796 */
73797 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
73798   Incrblob *p = (Incrblob *)pBlob;
73799   int rc;
73800   sqlite3 *db;
73801 
73802   if( p ){
73803     db = p->db;
73804     sqlite3_mutex_enter(db->mutex);
73805     rc = sqlite3_finalize(p->pStmt);
73806     sqlite3DbFree(db, p);
73807     sqlite3_mutex_leave(db->mutex);
73808   }else{
73809     rc = SQLITE_OK;
73810   }
73811   return rc;
73812 }
73813 
73814 /*
73815 ** Perform a read or write operation on a blob
73816 */
73817 static int blobReadWrite(
73818   sqlite3_blob *pBlob,
73819   void *z,
73820   int n,
73821   int iOffset,
73822   int (*xCall)(BtCursor*, u32, u32, void*)
73823 ){
73824   int rc;
73825   Incrblob *p = (Incrblob *)pBlob;
73826   Vdbe *v;
73827   sqlite3 *db;
73828 
73829   if( p==0 ) return SQLITE_MISUSE_BKPT;
73830   db = p->db;
73831   sqlite3_mutex_enter(db->mutex);
73832   v = (Vdbe*)p->pStmt;
73833 
73834   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
73835     /* Request is out of range. Return a transient error. */
73836     rc = SQLITE_ERROR;
73837     sqlite3Error(db, SQLITE_ERROR, 0);
73838   }else if( v==0 ){
73839     /* If there is no statement handle, then the blob-handle has
73840     ** already been invalidated. Return SQLITE_ABORT in this case.
73841     */
73842     rc = SQLITE_ABORT;
73843   }else{
73844     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
73845     ** returned, clean-up the statement handle.
73846     */
73847     assert( db == v->db );
73848     sqlite3BtreeEnterCursor(p->pCsr);
73849     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
73850     sqlite3BtreeLeaveCursor(p->pCsr);
73851     if( rc==SQLITE_ABORT ){
73852       sqlite3VdbeFinalize(v);
73853       p->pStmt = 0;
73854     }else{
73855       db->errCode = rc;
73856       v->rc = rc;
73857     }
73858   }
73859   rc = sqlite3ApiExit(db, rc);
73860   sqlite3_mutex_leave(db->mutex);
73861   return rc;
73862 }
73863 
73864 /*
73865 ** Read data from a blob handle.
73866 */
73867 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
73868   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
73869 }
73870 
73871 /*
73872 ** Write data to a blob handle.
73873 */
73874 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
73875   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
73876 }
73877 
73878 /*
73879 ** Query a blob handle for the size of the data.
73880 **
73881 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
73882 ** so no mutex is required for access.
73883 */
73884 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
73885   Incrblob *p = (Incrblob *)pBlob;
73886   return (p && p->pStmt) ? p->nByte : 0;
73887 }
73888 
73889 /*
73890 ** Move an existing blob handle to point to a different row of the same
73891 ** database table.
73892 **
73893 ** If an error occurs, or if the specified row does not exist or does not
73894 ** contain a blob or text value, then an error code is returned and the
73895 ** database handle error code and message set. If this happens, then all
73896 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
73897 ** immediately return SQLITE_ABORT.
73898 */
73899 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
73900   int rc;
73901   Incrblob *p = (Incrblob *)pBlob;
73902   sqlite3 *db;
73903 
73904   if( p==0 ) return SQLITE_MISUSE_BKPT;
73905   db = p->db;
73906   sqlite3_mutex_enter(db->mutex);
73907 
73908   if( p->pStmt==0 ){
73909     /* If there is no statement handle, then the blob-handle has
73910     ** already been invalidated. Return SQLITE_ABORT in this case.
73911     */
73912     rc = SQLITE_ABORT;
73913   }else{
73914     char *zErr;
73915     rc = blobSeekToRow(p, iRow, &zErr);
73916     if( rc!=SQLITE_OK ){
73917       sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
73918       sqlite3DbFree(db, zErr);
73919     }
73920     assert( rc!=SQLITE_SCHEMA );
73921   }
73922 
73923   rc = sqlite3ApiExit(db, rc);
73924   assert( rc==SQLITE_OK || p->pStmt==0 );
73925   sqlite3_mutex_leave(db->mutex);
73926   return rc;
73927 }
73928 
73929 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
73930 
73931 /************** End of vdbeblob.c ********************************************/
73932 /************** Begin file vdbesort.c ****************************************/
73933 /*
73934 ** 2011 July 9
73935 **
73936 ** The author disclaims copyright to this source code.  In place of
73937 ** a legal notice, here is a blessing:
73938 **
73939 **    May you do good and not evil.
73940 **    May you find forgiveness for yourself and forgive others.
73941 **    May you share freely, never taking more than you give.
73942 **
73943 *************************************************************************
73944 ** This file contains code for the VdbeSorter object, used in concert with
73945 ** a VdbeCursor to sort large numbers of keys (as may be required, for
73946 ** example, by CREATE INDEX statements on tables too large to fit in main
73947 ** memory).
73948 */
73949 
73950 
73951 
73952 typedef struct VdbeSorterIter VdbeSorterIter;
73953 typedef struct SorterRecord SorterRecord;
73954 typedef struct FileWriter FileWriter;
73955 
73956 /*
73957 ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
73958 **
73959 ** As keys are added to the sorter, they are written to disk in a series
73960 ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
73961 ** the same as the cache-size allowed for temporary databases. In order
73962 ** to allow the caller to extract keys from the sorter in sorted order,
73963 ** all PMAs currently stored on disk must be merged together. This comment
73964 ** describes the data structure used to do so. The structure supports
73965 ** merging any number of arrays in a single pass with no redundant comparison
73966 ** operations.
73967 **
73968 ** The aIter[] array contains an iterator for each of the PMAs being merged.
73969 ** An aIter[] iterator either points to a valid key or else is at EOF. For
73970 ** the purposes of the paragraphs below, we assume that the array is actually
73971 ** N elements in size, where N is the smallest power of 2 greater to or equal
73972 ** to the number of iterators being merged. The extra aIter[] elements are
73973 ** treated as if they are empty (always at EOF).
73974 **
73975 ** The aTree[] array is also N elements in size. The value of N is stored in
73976 ** the VdbeSorter.nTree variable.
73977 **
73978 ** The final (N/2) elements of aTree[] contain the results of comparing
73979 ** pairs of iterator keys together. Element i contains the result of
73980 ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
73981 ** aTree element is set to the index of it.
73982 **
73983 ** For the purposes of this comparison, EOF is considered greater than any
73984 ** other key value. If the keys are equal (only possible with two EOF
73985 ** values), it doesn't matter which index is stored.
73986 **
73987 ** The (N/4) elements of aTree[] that precede the final (N/2) described
73988 ** above contains the index of the smallest of each block of 4 iterators.
73989 ** And so on. So that aTree[1] contains the index of the iterator that
73990 ** currently points to the smallest key value. aTree[0] is unused.
73991 **
73992 ** Example:
73993 **
73994 **     aIter[0] -> Banana
73995 **     aIter[1] -> Feijoa
73996 **     aIter[2] -> Elderberry
73997 **     aIter[3] -> Currant
73998 **     aIter[4] -> Grapefruit
73999 **     aIter[5] -> Apple
74000 **     aIter[6] -> Durian
74001 **     aIter[7] -> EOF
74002 **
74003 **     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
74004 **
74005 ** The current element is "Apple" (the value of the key indicated by
74006 ** iterator 5). When the Next() operation is invoked, iterator 5 will
74007 ** be advanced to the next key in its segment. Say the next key is
74008 ** "Eggplant":
74009 **
74010 **     aIter[5] -> Eggplant
74011 **
74012 ** The contents of aTree[] are updated first by comparing the new iterator
74013 ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
74014 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
74015 ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
74016 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
74017 ** so the value written into element 1 of the array is 0. As follows:
74018 **
74019 **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
74020 **
74021 ** In other words, each time we advance to the next sorter element, log2(N)
74022 ** key comparison operations are required, where N is the number of segments
74023 ** being merged (rounded up to the next power of 2).
74024 */
74025 struct VdbeSorter {
74026   i64 iWriteOff;                  /* Current write offset within file pTemp1 */
74027   i64 iReadOff;                   /* Current read offset within file pTemp1 */
74028   int nInMemory;                  /* Current size of pRecord list as PMA */
74029   int nTree;                      /* Used size of aTree/aIter (power of 2) */
74030   int nPMA;                       /* Number of PMAs stored in pTemp1 */
74031   int mnPmaSize;                  /* Minimum PMA size, in bytes */
74032   int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
74033   VdbeSorterIter *aIter;          /* Array of iterators to merge */
74034   int *aTree;                     /* Current state of incremental merge */
74035   sqlite3_file *pTemp1;           /* PMA file 1 */
74036   SorterRecord *pRecord;          /* Head of in-memory record list */
74037   UnpackedRecord *pUnpacked;      /* Used to unpack keys */
74038 };
74039 
74040 /*
74041 ** The following type is an iterator for a PMA. It caches the current key in
74042 ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
74043 */
74044 struct VdbeSorterIter {
74045   i64 iReadOff;                   /* Current read offset */
74046   i64 iEof;                       /* 1 byte past EOF for this iterator */
74047   int nAlloc;                     /* Bytes of space at aAlloc */
74048   int nKey;                       /* Number of bytes in key */
74049   sqlite3_file *pFile;            /* File iterator is reading from */
74050   u8 *aAlloc;                     /* Allocated space */
74051   u8 *aKey;                       /* Pointer to current key */
74052   u8 *aBuffer;                    /* Current read buffer */
74053   int nBuffer;                    /* Size of read buffer in bytes */
74054 };
74055 
74056 /*
74057 ** An instance of this structure is used to organize the stream of records
74058 ** being written to files by the merge-sort code into aligned, page-sized
74059 ** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
74060 ** faster on many operating systems.
74061 */
74062 struct FileWriter {
74063   int eFWErr;                     /* Non-zero if in an error state */
74064   u8 *aBuffer;                    /* Pointer to write buffer */
74065   int nBuffer;                    /* Size of write buffer in bytes */
74066   int iBufStart;                  /* First byte of buffer to write */
74067   int iBufEnd;                    /* Last byte of buffer to write */
74068   i64 iWriteOff;                  /* Offset of start of buffer in file */
74069   sqlite3_file *pFile;            /* File to write to */
74070 };
74071 
74072 /*
74073 ** A structure to store a single record. All in-memory records are connected
74074 ** together into a linked list headed at VdbeSorter.pRecord using the
74075 ** SorterRecord.pNext pointer.
74076 */
74077 struct SorterRecord {
74078   void *pVal;
74079   int nVal;
74080   SorterRecord *pNext;
74081 };
74082 
74083 /* Minimum allowable value for the VdbeSorter.nWorking variable */
74084 #define SORTER_MIN_WORKING 10
74085 
74086 /* Maximum number of segments to merge in a single pass. */
74087 #define SORTER_MAX_MERGE_COUNT 16
74088 
74089 /*
74090 ** Free all memory belonging to the VdbeSorterIter object passed as the second
74091 ** argument. All structure fields are set to zero before returning.
74092 */
74093 static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
74094   sqlite3DbFree(db, pIter->aAlloc);
74095   sqlite3DbFree(db, pIter->aBuffer);
74096   memset(pIter, 0, sizeof(VdbeSorterIter));
74097 }
74098 
74099 /*
74100 ** Read nByte bytes of data from the stream of data iterated by object p.
74101 ** If successful, set *ppOut to point to a buffer containing the data
74102 ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
74103 ** error code.
74104 **
74105 ** The buffer indicated by *ppOut may only be considered valid until the
74106 ** next call to this function.
74107 */
74108 static int vdbeSorterIterRead(
74109   sqlite3 *db,                    /* Database handle (for malloc) */
74110   VdbeSorterIter *p,              /* Iterator */
74111   int nByte,                      /* Bytes of data to read */
74112   u8 **ppOut                      /* OUT: Pointer to buffer containing data */
74113 ){
74114   int iBuf;                       /* Offset within buffer to read from */
74115   int nAvail;                     /* Bytes of data available in buffer */
74116   assert( p->aBuffer );
74117 
74118   /* If there is no more data to be read from the buffer, read the next
74119   ** p->nBuffer bytes of data from the file into it. Or, if there are less
74120   ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
74121   iBuf = p->iReadOff % p->nBuffer;
74122   if( iBuf==0 ){
74123     int nRead;                    /* Bytes to read from disk */
74124     int rc;                       /* sqlite3OsRead() return code */
74125 
74126     /* Determine how many bytes of data to read. */
74127     if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
74128       nRead = p->nBuffer;
74129     }else{
74130       nRead = (int)(p->iEof - p->iReadOff);
74131     }
74132     assert( nRead>0 );
74133 
74134     /* Read data from the file. Return early if an error occurs. */
74135     rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
74136     assert( rc!=SQLITE_IOERR_SHORT_READ );
74137     if( rc!=SQLITE_OK ) return rc;
74138   }
74139   nAvail = p->nBuffer - iBuf;
74140 
74141   if( nByte<=nAvail ){
74142     /* The requested data is available in the in-memory buffer. In this
74143     ** case there is no need to make a copy of the data, just return a
74144     ** pointer into the buffer to the caller.  */
74145     *ppOut = &p->aBuffer[iBuf];
74146     p->iReadOff += nByte;
74147   }else{
74148     /* The requested data is not all available in the in-memory buffer.
74149     ** In this case, allocate space at p->aAlloc[] to copy the requested
74150     ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
74151     int nRem;                     /* Bytes remaining to copy */
74152 
74153     /* Extend the p->aAlloc[] allocation if required. */
74154     if( p->nAlloc<nByte ){
74155       int nNew = p->nAlloc*2;
74156       while( nByte>nNew ) nNew = nNew*2;
74157       p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
74158       if( !p->aAlloc ) return SQLITE_NOMEM;
74159       p->nAlloc = nNew;
74160     }
74161 
74162     /* Copy as much data as is available in the buffer into the start of
74163     ** p->aAlloc[].  */
74164     memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
74165     p->iReadOff += nAvail;
74166     nRem = nByte - nAvail;
74167 
74168     /* The following loop copies up to p->nBuffer bytes per iteration into
74169     ** the p->aAlloc[] buffer.  */
74170     while( nRem>0 ){
74171       int rc;                     /* vdbeSorterIterRead() return code */
74172       int nCopy;                  /* Number of bytes to copy */
74173       u8 *aNext;                  /* Pointer to buffer to copy data from */
74174 
74175       nCopy = nRem;
74176       if( nRem>p->nBuffer ) nCopy = p->nBuffer;
74177       rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
74178       if( rc!=SQLITE_OK ) return rc;
74179       assert( aNext!=p->aAlloc );
74180       memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
74181       nRem -= nCopy;
74182     }
74183 
74184     *ppOut = p->aAlloc;
74185   }
74186 
74187   return SQLITE_OK;
74188 }
74189 
74190 /*
74191 ** Read a varint from the stream of data accessed by p. Set *pnOut to
74192 ** the value read.
74193 */
74194 static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
74195   int iBuf;
74196 
74197   iBuf = p->iReadOff % p->nBuffer;
74198   if( iBuf && (p->nBuffer-iBuf)>=9 ){
74199     p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
74200   }else{
74201     u8 aVarint[16], *a;
74202     int i = 0, rc;
74203     do{
74204       rc = vdbeSorterIterRead(db, p, 1, &a);
74205       if( rc ) return rc;
74206       aVarint[(i++)&0xf] = a[0];
74207     }while( (a[0]&0x80)!=0 );
74208     sqlite3GetVarint(aVarint, pnOut);
74209   }
74210 
74211   return SQLITE_OK;
74212 }
74213 
74214 
74215 /*
74216 ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
74217 ** no error occurs, or an SQLite error code if one does.
74218 */
74219 static int vdbeSorterIterNext(
74220   sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
74221   VdbeSorterIter *pIter           /* Iterator to advance */
74222 ){
74223   int rc;                         /* Return Code */
74224   u64 nRec = 0;                   /* Size of record in bytes */
74225 
74226   if( pIter->iReadOff>=pIter->iEof ){
74227     /* This is an EOF condition */
74228     vdbeSorterIterZero(db, pIter);
74229     return SQLITE_OK;
74230   }
74231 
74232   rc = vdbeSorterIterVarint(db, pIter, &nRec);
74233   if( rc==SQLITE_OK ){
74234     pIter->nKey = (int)nRec;
74235     rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
74236   }
74237 
74238   return rc;
74239 }
74240 
74241 /*
74242 ** Initialize iterator pIter to scan through the PMA stored in file pFile
74243 ** starting at offset iStart and ending at offset iEof-1. This function
74244 ** leaves the iterator pointing to the first key in the PMA (or EOF if the
74245 ** PMA is empty).
74246 */
74247 static int vdbeSorterIterInit(
74248   sqlite3 *db,                    /* Database handle */
74249   const VdbeSorter *pSorter,      /* Sorter object */
74250   i64 iStart,                     /* Start offset in pFile */
74251   VdbeSorterIter *pIter,          /* Iterator to populate */
74252   i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
74253 ){
74254   int rc = SQLITE_OK;
74255   int nBuf;
74256 
74257   nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
74258 
74259   assert( pSorter->iWriteOff>iStart );
74260   assert( pIter->aAlloc==0 );
74261   assert( pIter->aBuffer==0 );
74262   pIter->pFile = pSorter->pTemp1;
74263   pIter->iReadOff = iStart;
74264   pIter->nAlloc = 128;
74265   pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
74266   pIter->nBuffer = nBuf;
74267   pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
74268 
74269   if( !pIter->aBuffer ){
74270     rc = SQLITE_NOMEM;
74271   }else{
74272     int iBuf;
74273 
74274     iBuf = iStart % nBuf;
74275     if( iBuf ){
74276       int nRead = nBuf - iBuf;
74277       if( (iStart + nRead) > pSorter->iWriteOff ){
74278         nRead = (int)(pSorter->iWriteOff - iStart);
74279       }
74280       rc = sqlite3OsRead(
74281           pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
74282       );
74283       assert( rc!=SQLITE_IOERR_SHORT_READ );
74284     }
74285 
74286     if( rc==SQLITE_OK ){
74287       u64 nByte;                       /* Size of PMA in bytes */
74288       pIter->iEof = pSorter->iWriteOff;
74289       rc = vdbeSorterIterVarint(db, pIter, &nByte);
74290       pIter->iEof = pIter->iReadOff + nByte;
74291       *pnByte += nByte;
74292     }
74293   }
74294 
74295   if( rc==SQLITE_OK ){
74296     rc = vdbeSorterIterNext(db, pIter);
74297   }
74298   return rc;
74299 }
74300 
74301 
74302 /*
74303 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
74304 ** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
74305 ** used by the comparison. If an error occurs, return an SQLite error code.
74306 ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
74307 ** value, depending on whether key1 is smaller, equal to or larger than key2.
74308 **
74309 ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
74310 ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
74311 ** is true and key1 contains even a single NULL value, it is considered to
74312 ** be less than key2. Even if key2 also contains NULL values.
74313 **
74314 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
74315 ** has been allocated and contains an unpacked record that is used as key2.
74316 */
74317 static void vdbeSorterCompare(
74318   const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
74319   int nIgnore,                    /* Ignore the last nIgnore fields */
74320   const void *pKey1, int nKey1,   /* Left side of comparison */
74321   const void *pKey2, int nKey2,   /* Right side of comparison */
74322   int *pRes                       /* OUT: Result of comparison */
74323 ){
74324   KeyInfo *pKeyInfo = pCsr->pKeyInfo;
74325   VdbeSorter *pSorter = pCsr->pSorter;
74326   UnpackedRecord *r2 = pSorter->pUnpacked;
74327   int i;
74328 
74329   if( pKey2 ){
74330     sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
74331   }
74332 
74333   if( nIgnore ){
74334     r2->nField = pKeyInfo->nField - nIgnore;
74335     assert( r2->nField>0 );
74336     for(i=0; i<r2->nField; i++){
74337       if( r2->aMem[i].flags & MEM_Null ){
74338         *pRes = -1;
74339         return;
74340       }
74341     }
74342     assert( r2->default_rc==0 );
74343   }
74344 
74345   *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2, 0);
74346 }
74347 
74348 /*
74349 ** This function is called to compare two iterator keys when merging
74350 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
74351 ** value to recalculate.
74352 */
74353 static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
74354   VdbeSorter *pSorter = pCsr->pSorter;
74355   int i1;
74356   int i2;
74357   int iRes;
74358   VdbeSorterIter *p1;
74359   VdbeSorterIter *p2;
74360 
74361   assert( iOut<pSorter->nTree && iOut>0 );
74362 
74363   if( iOut>=(pSorter->nTree/2) ){
74364     i1 = (iOut - pSorter->nTree/2) * 2;
74365     i2 = i1 + 1;
74366   }else{
74367     i1 = pSorter->aTree[iOut*2];
74368     i2 = pSorter->aTree[iOut*2+1];
74369   }
74370 
74371   p1 = &pSorter->aIter[i1];
74372   p2 = &pSorter->aIter[i2];
74373 
74374   if( p1->pFile==0 ){
74375     iRes = i2;
74376   }else if( p2->pFile==0 ){
74377     iRes = i1;
74378   }else{
74379     int res;
74380     assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
74381     vdbeSorterCompare(
74382         pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
74383     );
74384     if( res<=0 ){
74385       iRes = i1;
74386     }else{
74387       iRes = i2;
74388     }
74389   }
74390 
74391   pSorter->aTree[iOut] = iRes;
74392   return SQLITE_OK;
74393 }
74394 
74395 /*
74396 ** Initialize the temporary index cursor just opened as a sorter cursor.
74397 */
74398 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
74399   int pgsz;                       /* Page size of main database */
74400   int mxCache;                    /* Cache size */
74401   VdbeSorter *pSorter;            /* The new sorter */
74402   char *d;                        /* Dummy */
74403 
74404   assert( pCsr->pKeyInfo && pCsr->pBt==0 );
74405   pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
74406   if( pSorter==0 ){
74407     return SQLITE_NOMEM;
74408   }
74409 
74410   pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
74411   if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
74412   assert( pSorter->pUnpacked==(UnpackedRecord *)d );
74413 
74414   if( !sqlite3TempInMemory(db) ){
74415     pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
74416     pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
74417     mxCache = db->aDb[0].pSchema->cache_size;
74418     if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
74419     pSorter->mxPmaSize = mxCache * pgsz;
74420   }
74421 
74422   return SQLITE_OK;
74423 }
74424 
74425 /*
74426 ** Free the list of sorted records starting at pRecord.
74427 */
74428 static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
74429   SorterRecord *p;
74430   SorterRecord *pNext;
74431   for(p=pRecord; p; p=pNext){
74432     pNext = p->pNext;
74433     sqlite3DbFree(db, p);
74434   }
74435 }
74436 
74437 /*
74438 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
74439 */
74440 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
74441   VdbeSorter *pSorter = pCsr->pSorter;
74442   if( pSorter ){
74443     if( pSorter->aIter ){
74444       int i;
74445       for(i=0; i<pSorter->nTree; i++){
74446         vdbeSorterIterZero(db, &pSorter->aIter[i]);
74447       }
74448       sqlite3DbFree(db, pSorter->aIter);
74449     }
74450     if( pSorter->pTemp1 ){
74451       sqlite3OsCloseFree(pSorter->pTemp1);
74452     }
74453     vdbeSorterRecordFree(db, pSorter->pRecord);
74454     sqlite3DbFree(db, pSorter->pUnpacked);
74455     sqlite3DbFree(db, pSorter);
74456     pCsr->pSorter = 0;
74457   }
74458 }
74459 
74460 /*
74461 ** Allocate space for a file-handle and open a temporary file. If successful,
74462 ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
74463 ** Otherwise, set *ppFile to 0 and return an SQLite error code.
74464 */
74465 static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
74466   int dummy;
74467   return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
74468       SQLITE_OPEN_TEMP_JOURNAL |
74469       SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
74470       SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
74471   );
74472 }
74473 
74474 /*
74475 ** Merge the two sorted lists p1 and p2 into a single list.
74476 ** Set *ppOut to the head of the new list.
74477 */
74478 static void vdbeSorterMerge(
74479   const VdbeCursor *pCsr,         /* For pKeyInfo */
74480   SorterRecord *p1,               /* First list to merge */
74481   SorterRecord *p2,               /* Second list to merge */
74482   SorterRecord **ppOut            /* OUT: Head of merged list */
74483 ){
74484   SorterRecord *pFinal = 0;
74485   SorterRecord **pp = &pFinal;
74486   void *pVal2 = p2 ? p2->pVal : 0;
74487 
74488   while( p1 && p2 ){
74489     int res;
74490     vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
74491     if( res<=0 ){
74492       *pp = p1;
74493       pp = &p1->pNext;
74494       p1 = p1->pNext;
74495       pVal2 = 0;
74496     }else{
74497       *pp = p2;
74498        pp = &p2->pNext;
74499       p2 = p2->pNext;
74500       if( p2==0 ) break;
74501       pVal2 = p2->pVal;
74502     }
74503   }
74504   *pp = p1 ? p1 : p2;
74505   *ppOut = pFinal;
74506 }
74507 
74508 /*
74509 ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
74510 ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
74511 ** occurs.
74512 */
74513 static int vdbeSorterSort(const VdbeCursor *pCsr){
74514   int i;
74515   SorterRecord **aSlot;
74516   SorterRecord *p;
74517   VdbeSorter *pSorter = pCsr->pSorter;
74518 
74519   aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
74520   if( !aSlot ){
74521     return SQLITE_NOMEM;
74522   }
74523 
74524   p = pSorter->pRecord;
74525   while( p ){
74526     SorterRecord *pNext = p->pNext;
74527     p->pNext = 0;
74528     for(i=0; aSlot[i]; i++){
74529       vdbeSorterMerge(pCsr, p, aSlot[i], &p);
74530       aSlot[i] = 0;
74531     }
74532     aSlot[i] = p;
74533     p = pNext;
74534   }
74535 
74536   p = 0;
74537   for(i=0; i<64; i++){
74538     vdbeSorterMerge(pCsr, p, aSlot[i], &p);
74539   }
74540   pSorter->pRecord = p;
74541 
74542   sqlite3_free(aSlot);
74543   return SQLITE_OK;
74544 }
74545 
74546 /*
74547 ** Initialize a file-writer object.
74548 */
74549 static void fileWriterInit(
74550   sqlite3 *db,                    /* Database (for malloc) */
74551   sqlite3_file *pFile,            /* File to write to */
74552   FileWriter *p,                  /* Object to populate */
74553   i64 iStart                      /* Offset of pFile to begin writing at */
74554 ){
74555   int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
74556 
74557   memset(p, 0, sizeof(FileWriter));
74558   p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
74559   if( !p->aBuffer ){
74560     p->eFWErr = SQLITE_NOMEM;
74561   }else{
74562     p->iBufEnd = p->iBufStart = (iStart % nBuf);
74563     p->iWriteOff = iStart - p->iBufStart;
74564     p->nBuffer = nBuf;
74565     p->pFile = pFile;
74566   }
74567 }
74568 
74569 /*
74570 ** Write nData bytes of data to the file-write object. Return SQLITE_OK
74571 ** if successful, or an SQLite error code if an error occurs.
74572 */
74573 static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
74574   int nRem = nData;
74575   while( nRem>0 && p->eFWErr==0 ){
74576     int nCopy = nRem;
74577     if( nCopy>(p->nBuffer - p->iBufEnd) ){
74578       nCopy = p->nBuffer - p->iBufEnd;
74579     }
74580 
74581     memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
74582     p->iBufEnd += nCopy;
74583     if( p->iBufEnd==p->nBuffer ){
74584       p->eFWErr = sqlite3OsWrite(p->pFile,
74585           &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
74586           p->iWriteOff + p->iBufStart
74587       );
74588       p->iBufStart = p->iBufEnd = 0;
74589       p->iWriteOff += p->nBuffer;
74590     }
74591     assert( p->iBufEnd<p->nBuffer );
74592 
74593     nRem -= nCopy;
74594   }
74595 }
74596 
74597 /*
74598 ** Flush any buffered data to disk and clean up the file-writer object.
74599 ** The results of using the file-writer after this call are undefined.
74600 ** Return SQLITE_OK if flushing the buffered data succeeds or is not
74601 ** required. Otherwise, return an SQLite error code.
74602 **
74603 ** Before returning, set *piEof to the offset immediately following the
74604 ** last byte written to the file.
74605 */
74606 static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
74607   int rc;
74608   if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
74609     p->eFWErr = sqlite3OsWrite(p->pFile,
74610         &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
74611         p->iWriteOff + p->iBufStart
74612     );
74613   }
74614   *piEof = (p->iWriteOff + p->iBufEnd);
74615   sqlite3DbFree(db, p->aBuffer);
74616   rc = p->eFWErr;
74617   memset(p, 0, sizeof(FileWriter));
74618   return rc;
74619 }
74620 
74621 /*
74622 ** Write value iVal encoded as a varint to the file-write object. Return
74623 ** SQLITE_OK if successful, or an SQLite error code if an error occurs.
74624 */
74625 static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
74626   int nByte;
74627   u8 aByte[10];
74628   nByte = sqlite3PutVarint(aByte, iVal);
74629   fileWriterWrite(p, aByte, nByte);
74630 }
74631 
74632 /*
74633 ** Write the current contents of the in-memory linked-list to a PMA. Return
74634 ** SQLITE_OK if successful, or an SQLite error code otherwise.
74635 **
74636 ** The format of a PMA is:
74637 **
74638 **     * A varint. This varint contains the total number of bytes of content
74639 **       in the PMA (not including the varint itself).
74640 **
74641 **     * One or more records packed end-to-end in order of ascending keys.
74642 **       Each record consists of a varint followed by a blob of data (the
74643 **       key). The varint is the number of bytes in the blob of data.
74644 */
74645 static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
74646   int rc = SQLITE_OK;             /* Return code */
74647   VdbeSorter *pSorter = pCsr->pSorter;
74648   FileWriter writer;
74649 
74650   memset(&writer, 0, sizeof(FileWriter));
74651 
74652   if( pSorter->nInMemory==0 ){
74653     assert( pSorter->pRecord==0 );
74654     return rc;
74655   }
74656 
74657   rc = vdbeSorterSort(pCsr);
74658 
74659   /* If the first temporary PMA file has not been opened, open it now. */
74660   if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
74661     rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
74662     assert( rc!=SQLITE_OK || pSorter->pTemp1 );
74663     assert( pSorter->iWriteOff==0 );
74664     assert( pSorter->nPMA==0 );
74665   }
74666 
74667   if( rc==SQLITE_OK ){
74668     SorterRecord *p;
74669     SorterRecord *pNext = 0;
74670 
74671     fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
74672     pSorter->nPMA++;
74673     fileWriterWriteVarint(&writer, pSorter->nInMemory);
74674     for(p=pSorter->pRecord; p; p=pNext){
74675       pNext = p->pNext;
74676       fileWriterWriteVarint(&writer, p->nVal);
74677       fileWriterWrite(&writer, p->pVal, p->nVal);
74678       sqlite3DbFree(db, p);
74679     }
74680     pSorter->pRecord = p;
74681     rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
74682   }
74683 
74684   return rc;
74685 }
74686 
74687 /*
74688 ** Add a record to the sorter.
74689 */
74690 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
74691   sqlite3 *db,                    /* Database handle */
74692   const VdbeCursor *pCsr,               /* Sorter cursor */
74693   Mem *pVal                       /* Memory cell containing record */
74694 ){
74695   VdbeSorter *pSorter = pCsr->pSorter;
74696   int rc = SQLITE_OK;             /* Return Code */
74697   SorterRecord *pNew;             /* New list element */
74698 
74699   assert( pSorter );
74700   pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
74701 
74702   pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
74703   if( pNew==0 ){
74704     rc = SQLITE_NOMEM;
74705   }else{
74706     pNew->pVal = (void *)&pNew[1];
74707     memcpy(pNew->pVal, pVal->z, pVal->n);
74708     pNew->nVal = pVal->n;
74709     pNew->pNext = pSorter->pRecord;
74710     pSorter->pRecord = pNew;
74711   }
74712 
74713   /* See if the contents of the sorter should now be written out. They
74714   ** are written out when either of the following are true:
74715   **
74716   **   * The total memory allocated for the in-memory list is greater
74717   **     than (page-size * cache-size), or
74718   **
74719   **   * The total memory allocated for the in-memory list is greater
74720   **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
74721   */
74722   if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
74723         (pSorter->nInMemory>pSorter->mxPmaSize)
74724      || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
74725   )){
74726 #ifdef SQLITE_DEBUG
74727     i64 nExpect = pSorter->iWriteOff
74728                 + sqlite3VarintLen(pSorter->nInMemory)
74729                 + pSorter->nInMemory;
74730 #endif
74731     rc = vdbeSorterListToPMA(db, pCsr);
74732     pSorter->nInMemory = 0;
74733     assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
74734   }
74735 
74736   return rc;
74737 }
74738 
74739 /*
74740 ** Helper function for sqlite3VdbeSorterRewind().
74741 */
74742 static int vdbeSorterInitMerge(
74743   sqlite3 *db,                    /* Database handle */
74744   const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
74745   i64 *pnByte                     /* Sum of bytes in all opened PMAs */
74746 ){
74747   VdbeSorter *pSorter = pCsr->pSorter;
74748   int rc = SQLITE_OK;             /* Return code */
74749   int i;                          /* Used to iterator through aIter[] */
74750   i64 nByte = 0;                  /* Total bytes in all opened PMAs */
74751 
74752   /* Initialize the iterators. */
74753   for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
74754     VdbeSorterIter *pIter = &pSorter->aIter[i];
74755     rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
74756     pSorter->iReadOff = pIter->iEof;
74757     assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
74758     if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
74759   }
74760 
74761   /* Initialize the aTree[] array. */
74762   for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
74763     rc = vdbeSorterDoCompare(pCsr, i);
74764   }
74765 
74766   *pnByte = nByte;
74767   return rc;
74768 }
74769 
74770 /*
74771 ** Once the sorter has been populated, this function is called to prepare
74772 ** for iterating through its contents in sorted order.
74773 */
74774 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
74775   VdbeSorter *pSorter = pCsr->pSorter;
74776   int rc;                         /* Return code */
74777   sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
74778   i64 iWrite2 = 0;                /* Write offset for pTemp2 */
74779   int nIter;                      /* Number of iterators used */
74780   int nByte;                      /* Bytes of space required for aIter/aTree */
74781   int N = 2;                      /* Power of 2 >= nIter */
74782 
74783   assert( pSorter );
74784 
74785   /* If no data has been written to disk, then do not do so now. Instead,
74786   ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
74787   ** from the in-memory list.  */
74788   if( pSorter->nPMA==0 ){
74789     *pbEof = !pSorter->pRecord;
74790     assert( pSorter->aTree==0 );
74791     return vdbeSorterSort(pCsr);
74792   }
74793 
74794   /* Write the current in-memory list to a PMA. */
74795   rc = vdbeSorterListToPMA(db, pCsr);
74796   if( rc!=SQLITE_OK ) return rc;
74797 
74798   /* Allocate space for aIter[] and aTree[]. */
74799   nIter = pSorter->nPMA;
74800   if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
74801   assert( nIter>0 );
74802   while( N<nIter ) N += N;
74803   nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
74804   pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
74805   if( !pSorter->aIter ) return SQLITE_NOMEM;
74806   pSorter->aTree = (int *)&pSorter->aIter[N];
74807   pSorter->nTree = N;
74808 
74809   do {
74810     int iNew;                     /* Index of new, merged, PMA */
74811 
74812     for(iNew=0;
74813         rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
74814         iNew++
74815     ){
74816       int rc2;                    /* Return code from fileWriterFinish() */
74817       FileWriter writer;          /* Object used to write to disk */
74818       i64 nWrite;                 /* Number of bytes in new PMA */
74819 
74820       memset(&writer, 0, sizeof(FileWriter));
74821 
74822       /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
74823       ** initialize an iterator for each of them and break out of the loop.
74824       ** These iterators will be incrementally merged as the VDBE layer calls
74825       ** sqlite3VdbeSorterNext().
74826       **
74827       ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
74828       ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
74829       ** are merged into a single PMA that is written to file pTemp2.
74830       */
74831       rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
74832       assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
74833       if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
74834         break;
74835       }
74836 
74837       /* Open the second temp file, if it is not already open. */
74838       if( pTemp2==0 ){
74839         assert( iWrite2==0 );
74840         rc = vdbeSorterOpenTempFile(db, &pTemp2);
74841       }
74842 
74843       if( rc==SQLITE_OK ){
74844         int bEof = 0;
74845         fileWriterInit(db, pTemp2, &writer, iWrite2);
74846         fileWriterWriteVarint(&writer, nWrite);
74847         while( rc==SQLITE_OK && bEof==0 ){
74848           VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
74849           assert( pIter->pFile );
74850 
74851           fileWriterWriteVarint(&writer, pIter->nKey);
74852           fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
74853           rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
74854         }
74855         rc2 = fileWriterFinish(db, &writer, &iWrite2);
74856         if( rc==SQLITE_OK ) rc = rc2;
74857       }
74858     }
74859 
74860     if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
74861       break;
74862     }else{
74863       sqlite3_file *pTmp = pSorter->pTemp1;
74864       pSorter->nPMA = iNew;
74865       pSorter->pTemp1 = pTemp2;
74866       pTemp2 = pTmp;
74867       pSorter->iWriteOff = iWrite2;
74868       pSorter->iReadOff = 0;
74869       iWrite2 = 0;
74870     }
74871   }while( rc==SQLITE_OK );
74872 
74873   if( pTemp2 ){
74874     sqlite3OsCloseFree(pTemp2);
74875   }
74876   *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
74877   return rc;
74878 }
74879 
74880 /*
74881 ** Advance to the next element in the sorter.
74882 */
74883 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
74884   VdbeSorter *pSorter = pCsr->pSorter;
74885   int rc;                         /* Return code */
74886 
74887   if( pSorter->aTree ){
74888     int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
74889     int i;                        /* Index of aTree[] to recalculate */
74890 
74891     rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
74892     for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
74893       rc = vdbeSorterDoCompare(pCsr, i);
74894     }
74895 
74896     *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
74897   }else{
74898     SorterRecord *pFree = pSorter->pRecord;
74899     pSorter->pRecord = pFree->pNext;
74900     pFree->pNext = 0;
74901     vdbeSorterRecordFree(db, pFree);
74902     *pbEof = !pSorter->pRecord;
74903     rc = SQLITE_OK;
74904   }
74905   return rc;
74906 }
74907 
74908 /*
74909 ** Return a pointer to a buffer owned by the sorter that contains the
74910 ** current key.
74911 */
74912 static void *vdbeSorterRowkey(
74913   const VdbeSorter *pSorter,      /* Sorter object */
74914   int *pnKey                      /* OUT: Size of current key in bytes */
74915 ){
74916   void *pKey;
74917   if( pSorter->aTree ){
74918     VdbeSorterIter *pIter;
74919     pIter = &pSorter->aIter[ pSorter->aTree[1] ];
74920     *pnKey = pIter->nKey;
74921     pKey = pIter->aKey;
74922   }else{
74923     *pnKey = pSorter->pRecord->nVal;
74924     pKey = pSorter->pRecord->pVal;
74925   }
74926   return pKey;
74927 }
74928 
74929 /*
74930 ** Copy the current sorter key into the memory cell pOut.
74931 */
74932 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
74933   VdbeSorter *pSorter = pCsr->pSorter;
74934   void *pKey; int nKey;           /* Sorter key to copy into pOut */
74935 
74936   pKey = vdbeSorterRowkey(pSorter, &nKey);
74937   if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
74938     return SQLITE_NOMEM;
74939   }
74940   pOut->n = nKey;
74941   MemSetTypeFlag(pOut, MEM_Blob);
74942   memcpy(pOut->z, pKey, nKey);
74943 
74944   return SQLITE_OK;
74945 }
74946 
74947 /*
74948 ** Compare the key in memory cell pVal with the key that the sorter cursor
74949 ** passed as the first argument currently points to. For the purposes of
74950 ** the comparison, ignore the rowid field at the end of each record.
74951 **
74952 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
74953 ** Otherwise, set *pRes to a negative, zero or positive value if the
74954 ** key in pVal is smaller than, equal to or larger than the current sorter
74955 ** key.
74956 */
74957 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
74958   const VdbeCursor *pCsr,         /* Sorter cursor */
74959   Mem *pVal,                      /* Value to compare to current sorter key */
74960   int nIgnore,                    /* Ignore this many fields at the end */
74961   int *pRes                       /* OUT: Result of comparison */
74962 ){
74963   VdbeSorter *pSorter = pCsr->pSorter;
74964   void *pKey; int nKey;           /* Sorter key to compare pVal with */
74965 
74966   pKey = vdbeSorterRowkey(pSorter, &nKey);
74967   vdbeSorterCompare(pCsr, nIgnore, pVal->z, pVal->n, pKey, nKey, pRes);
74968   return SQLITE_OK;
74969 }
74970 
74971 /************** End of vdbesort.c ********************************************/
74972 /************** Begin file journal.c *****************************************/
74973 /*
74974 ** 2007 August 22
74975 **
74976 ** The author disclaims copyright to this source code.  In place of
74977 ** a legal notice, here is a blessing:
74978 **
74979 **    May you do good and not evil.
74980 **    May you find forgiveness for yourself and forgive others.
74981 **    May you share freely, never taking more than you give.
74982 **
74983 *************************************************************************
74984 **
74985 ** This file implements a special kind of sqlite3_file object used
74986 ** by SQLite to create journal files if the atomic-write optimization
74987 ** is enabled.
74988 **
74989 ** The distinctive characteristic of this sqlite3_file is that the
74990 ** actual on disk file is created lazily. When the file is created,
74991 ** the caller specifies a buffer size for an in-memory buffer to
74992 ** be used to service read() and write() requests. The actual file
74993 ** on disk is not created or populated until either:
74994 **
74995 **   1) The in-memory representation grows too large for the allocated
74996 **      buffer, or
74997 **   2) The sqlite3JournalCreate() function is called.
74998 */
74999 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
75000 
75001 
75002 /*
75003 ** A JournalFile object is a subclass of sqlite3_file used by
75004 ** as an open file handle for journal files.
75005 */
75006 struct JournalFile {
75007   sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
75008   int nBuf;                       /* Size of zBuf[] in bytes */
75009   char *zBuf;                     /* Space to buffer journal writes */
75010   int iSize;                      /* Amount of zBuf[] currently used */
75011   int flags;                      /* xOpen flags */
75012   sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
75013   sqlite3_file *pReal;            /* The "real" underlying file descriptor */
75014   const char *zJournal;           /* Name of the journal file */
75015 };
75016 typedef struct JournalFile JournalFile;
75017 
75018 /*
75019 ** If it does not already exists, create and populate the on-disk file
75020 ** for JournalFile p.
75021 */
75022 static int createFile(JournalFile *p){
75023   int rc = SQLITE_OK;
75024   if( !p->pReal ){
75025     sqlite3_file *pReal = (sqlite3_file *)&p[1];
75026     rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
75027     if( rc==SQLITE_OK ){
75028       p->pReal = pReal;
75029       if( p->iSize>0 ){
75030         assert(p->iSize<=p->nBuf);
75031         rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
75032       }
75033       if( rc!=SQLITE_OK ){
75034         /* If an error occurred while writing to the file, close it before
75035         ** returning. This way, SQLite uses the in-memory journal data to
75036         ** roll back changes made to the internal page-cache before this
75037         ** function was called.  */
75038         sqlite3OsClose(pReal);
75039         p->pReal = 0;
75040       }
75041     }
75042   }
75043   return rc;
75044 }
75045 
75046 /*
75047 ** Close the file.
75048 */
75049 static int jrnlClose(sqlite3_file *pJfd){
75050   JournalFile *p = (JournalFile *)pJfd;
75051   if( p->pReal ){
75052     sqlite3OsClose(p->pReal);
75053   }
75054   sqlite3_free(p->zBuf);
75055   return SQLITE_OK;
75056 }
75057 
75058 /*
75059 ** Read data from the file.
75060 */
75061 static int jrnlRead(
75062   sqlite3_file *pJfd,    /* The journal file from which to read */
75063   void *zBuf,            /* Put the results here */
75064   int iAmt,              /* Number of bytes to read */
75065   sqlite_int64 iOfst     /* Begin reading at this offset */
75066 ){
75067   int rc = SQLITE_OK;
75068   JournalFile *p = (JournalFile *)pJfd;
75069   if( p->pReal ){
75070     rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
75071   }else if( (iAmt+iOfst)>p->iSize ){
75072     rc = SQLITE_IOERR_SHORT_READ;
75073   }else{
75074     memcpy(zBuf, &p->zBuf[iOfst], iAmt);
75075   }
75076   return rc;
75077 }
75078 
75079 /*
75080 ** Write data to the file.
75081 */
75082 static int jrnlWrite(
75083   sqlite3_file *pJfd,    /* The journal file into which to write */
75084   const void *zBuf,      /* Take data to be written from here */
75085   int iAmt,              /* Number of bytes to write */
75086   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
75087 ){
75088   int rc = SQLITE_OK;
75089   JournalFile *p = (JournalFile *)pJfd;
75090   if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
75091     rc = createFile(p);
75092   }
75093   if( rc==SQLITE_OK ){
75094     if( p->pReal ){
75095       rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
75096     }else{
75097       memcpy(&p->zBuf[iOfst], zBuf, iAmt);
75098       if( p->iSize<(iOfst+iAmt) ){
75099         p->iSize = (iOfst+iAmt);
75100       }
75101     }
75102   }
75103   return rc;
75104 }
75105 
75106 /*
75107 ** Truncate the file.
75108 */
75109 static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
75110   int rc = SQLITE_OK;
75111   JournalFile *p = (JournalFile *)pJfd;
75112   if( p->pReal ){
75113     rc = sqlite3OsTruncate(p->pReal, size);
75114   }else if( size<p->iSize ){
75115     p->iSize = size;
75116   }
75117   return rc;
75118 }
75119 
75120 /*
75121 ** Sync the file.
75122 */
75123 static int jrnlSync(sqlite3_file *pJfd, int flags){
75124   int rc;
75125   JournalFile *p = (JournalFile *)pJfd;
75126   if( p->pReal ){
75127     rc = sqlite3OsSync(p->pReal, flags);
75128   }else{
75129     rc = SQLITE_OK;
75130   }
75131   return rc;
75132 }
75133 
75134 /*
75135 ** Query the size of the file in bytes.
75136 */
75137 static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
75138   int rc = SQLITE_OK;
75139   JournalFile *p = (JournalFile *)pJfd;
75140   if( p->pReal ){
75141     rc = sqlite3OsFileSize(p->pReal, pSize);
75142   }else{
75143     *pSize = (sqlite_int64) p->iSize;
75144   }
75145   return rc;
75146 }
75147 
75148 /*
75149 ** Table of methods for JournalFile sqlite3_file object.
75150 */
75151 static struct sqlite3_io_methods JournalFileMethods = {
75152   1,             /* iVersion */
75153   jrnlClose,     /* xClose */
75154   jrnlRead,      /* xRead */
75155   jrnlWrite,     /* xWrite */
75156   jrnlTruncate,  /* xTruncate */
75157   jrnlSync,      /* xSync */
75158   jrnlFileSize,  /* xFileSize */
75159   0,             /* xLock */
75160   0,             /* xUnlock */
75161   0,             /* xCheckReservedLock */
75162   0,             /* xFileControl */
75163   0,             /* xSectorSize */
75164   0,             /* xDeviceCharacteristics */
75165   0,             /* xShmMap */
75166   0,             /* xShmLock */
75167   0,             /* xShmBarrier */
75168   0              /* xShmUnmap */
75169 };
75170 
75171 /*
75172 ** Open a journal file.
75173 */
75174 SQLITE_PRIVATE int sqlite3JournalOpen(
75175   sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
75176   const char *zName,         /* Name of the journal file */
75177   sqlite3_file *pJfd,        /* Preallocated, blank file handle */
75178   int flags,                 /* Opening flags */
75179   int nBuf                   /* Bytes buffered before opening the file */
75180 ){
75181   JournalFile *p = (JournalFile *)pJfd;
75182   memset(p, 0, sqlite3JournalSize(pVfs));
75183   if( nBuf>0 ){
75184     p->zBuf = sqlite3MallocZero(nBuf);
75185     if( !p->zBuf ){
75186       return SQLITE_NOMEM;
75187     }
75188   }else{
75189     return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
75190   }
75191   p->pMethod = &JournalFileMethods;
75192   p->nBuf = nBuf;
75193   p->flags = flags;
75194   p->zJournal = zName;
75195   p->pVfs = pVfs;
75196   return SQLITE_OK;
75197 }
75198 
75199 /*
75200 ** If the argument p points to a JournalFile structure, and the underlying
75201 ** file has not yet been created, create it now.
75202 */
75203 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
75204   if( p->pMethods!=&JournalFileMethods ){
75205     return SQLITE_OK;
75206   }
75207   return createFile((JournalFile *)p);
75208 }
75209 
75210 /*
75211 ** The file-handle passed as the only argument is guaranteed to be an open
75212 ** file. It may or may not be of class JournalFile. If the file is a
75213 ** JournalFile, and the underlying file on disk has not yet been opened,
75214 ** return 0. Otherwise, return 1.
75215 */
75216 SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
75217   return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
75218 }
75219 
75220 /*
75221 ** Return the number of bytes required to store a JournalFile that uses vfs
75222 ** pVfs to create the underlying on-disk files.
75223 */
75224 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
75225   return (pVfs->szOsFile+sizeof(JournalFile));
75226 }
75227 #endif
75228 
75229 /************** End of journal.c *********************************************/
75230 /************** Begin file memjournal.c **************************************/
75231 /*
75232 ** 2008 October 7
75233 **
75234 ** The author disclaims copyright to this source code.  In place of
75235 ** a legal notice, here is a blessing:
75236 **
75237 **    May you do good and not evil.
75238 **    May you find forgiveness for yourself and forgive others.
75239 **    May you share freely, never taking more than you give.
75240 **
75241 *************************************************************************
75242 **
75243 ** This file contains code use to implement an in-memory rollback journal.
75244 ** The in-memory rollback journal is used to journal transactions for
75245 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
75246 */
75247 
75248 /* Forward references to internal structures */
75249 typedef struct MemJournal MemJournal;
75250 typedef struct FilePoint FilePoint;
75251 typedef struct FileChunk FileChunk;
75252 
75253 /* Space to hold the rollback journal is allocated in increments of
75254 ** this many bytes.
75255 **
75256 ** The size chosen is a little less than a power of two.  That way,
75257 ** the FileChunk object will have a size that almost exactly fills
75258 ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
75259 ** memory allocators.
75260 */
75261 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
75262 
75263 /*
75264 ** The rollback journal is composed of a linked list of these structures.
75265 */
75266 struct FileChunk {
75267   FileChunk *pNext;               /* Next chunk in the journal */
75268   u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
75269 };
75270 
75271 /*
75272 ** An instance of this object serves as a cursor into the rollback journal.
75273 ** The cursor can be either for reading or writing.
75274 */
75275 struct FilePoint {
75276   sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
75277   FileChunk *pChunk;              /* Specific chunk into which cursor points */
75278 };
75279 
75280 /*
75281 ** This subclass is a subclass of sqlite3_file.  Each open memory-journal
75282 ** is an instance of this class.
75283 */
75284 struct MemJournal {
75285   sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
75286   FileChunk *pFirst;              /* Head of in-memory chunk-list */
75287   FilePoint endpoint;             /* Pointer to the end of the file */
75288   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
75289 };
75290 
75291 /*
75292 ** Read data from the in-memory journal file.  This is the implementation
75293 ** of the sqlite3_vfs.xRead method.
75294 */
75295 static int memjrnlRead(
75296   sqlite3_file *pJfd,    /* The journal file from which to read */
75297   void *zBuf,            /* Put the results here */
75298   int iAmt,              /* Number of bytes to read */
75299   sqlite_int64 iOfst     /* Begin reading at this offset */
75300 ){
75301   MemJournal *p = (MemJournal *)pJfd;
75302   u8 *zOut = zBuf;
75303   int nRead = iAmt;
75304   int iChunkOffset;
75305   FileChunk *pChunk;
75306 
75307   /* SQLite never tries to read past the end of a rollback journal file */
75308   assert( iOfst+iAmt<=p->endpoint.iOffset );
75309 
75310   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
75311     sqlite3_int64 iOff = 0;
75312     for(pChunk=p->pFirst;
75313         ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
75314         pChunk=pChunk->pNext
75315     ){
75316       iOff += JOURNAL_CHUNKSIZE;
75317     }
75318   }else{
75319     pChunk = p->readpoint.pChunk;
75320   }
75321 
75322   iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
75323   do {
75324     int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
75325     int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
75326     memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
75327     zOut += nCopy;
75328     nRead -= iSpace;
75329     iChunkOffset = 0;
75330   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
75331   p->readpoint.iOffset = iOfst+iAmt;
75332   p->readpoint.pChunk = pChunk;
75333 
75334   return SQLITE_OK;
75335 }
75336 
75337 /*
75338 ** Write data to the file.
75339 */
75340 static int memjrnlWrite(
75341   sqlite3_file *pJfd,    /* The journal file into which to write */
75342   const void *zBuf,      /* Take data to be written from here */
75343   int iAmt,              /* Number of bytes to write */
75344   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
75345 ){
75346   MemJournal *p = (MemJournal *)pJfd;
75347   int nWrite = iAmt;
75348   u8 *zWrite = (u8 *)zBuf;
75349 
75350   /* An in-memory journal file should only ever be appended to. Random
75351   ** access writes are not required by sqlite.
75352   */
75353   assert( iOfst==p->endpoint.iOffset );
75354   UNUSED_PARAMETER(iOfst);
75355 
75356   while( nWrite>0 ){
75357     FileChunk *pChunk = p->endpoint.pChunk;
75358     int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
75359     int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
75360 
75361     if( iChunkOffset==0 ){
75362       /* New chunk is required to extend the file. */
75363       FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
75364       if( !pNew ){
75365         return SQLITE_IOERR_NOMEM;
75366       }
75367       pNew->pNext = 0;
75368       if( pChunk ){
75369         assert( p->pFirst );
75370         pChunk->pNext = pNew;
75371       }else{
75372         assert( !p->pFirst );
75373         p->pFirst = pNew;
75374       }
75375       p->endpoint.pChunk = pNew;
75376     }
75377 
75378     memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
75379     zWrite += iSpace;
75380     nWrite -= iSpace;
75381     p->endpoint.iOffset += iSpace;
75382   }
75383 
75384   return SQLITE_OK;
75385 }
75386 
75387 /*
75388 ** Truncate the file.
75389 */
75390 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
75391   MemJournal *p = (MemJournal *)pJfd;
75392   FileChunk *pChunk;
75393   assert(size==0);
75394   UNUSED_PARAMETER(size);
75395   pChunk = p->pFirst;
75396   while( pChunk ){
75397     FileChunk *pTmp = pChunk;
75398     pChunk = pChunk->pNext;
75399     sqlite3_free(pTmp);
75400   }
75401   sqlite3MemJournalOpen(pJfd);
75402   return SQLITE_OK;
75403 }
75404 
75405 /*
75406 ** Close the file.
75407 */
75408 static int memjrnlClose(sqlite3_file *pJfd){
75409   memjrnlTruncate(pJfd, 0);
75410   return SQLITE_OK;
75411 }
75412 
75413 
75414 /*
75415 ** Sync the file.
75416 **
75417 ** Syncing an in-memory journal is a no-op.  And, in fact, this routine
75418 ** is never called in a working implementation.  This implementation
75419 ** exists purely as a contingency, in case some malfunction in some other
75420 ** part of SQLite causes Sync to be called by mistake.
75421 */
75422 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
75423   UNUSED_PARAMETER2(NotUsed, NotUsed2);
75424   return SQLITE_OK;
75425 }
75426 
75427 /*
75428 ** Query the size of the file in bytes.
75429 */
75430 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
75431   MemJournal *p = (MemJournal *)pJfd;
75432   *pSize = (sqlite_int64) p->endpoint.iOffset;
75433   return SQLITE_OK;
75434 }
75435 
75436 /*
75437 ** Table of methods for MemJournal sqlite3_file object.
75438 */
75439 static const struct sqlite3_io_methods MemJournalMethods = {
75440   1,                /* iVersion */
75441   memjrnlClose,     /* xClose */
75442   memjrnlRead,      /* xRead */
75443   memjrnlWrite,     /* xWrite */
75444   memjrnlTruncate,  /* xTruncate */
75445   memjrnlSync,      /* xSync */
75446   memjrnlFileSize,  /* xFileSize */
75447   0,                /* xLock */
75448   0,                /* xUnlock */
75449   0,                /* xCheckReservedLock */
75450   0,                /* xFileControl */
75451   0,                /* xSectorSize */
75452   0,                /* xDeviceCharacteristics */
75453   0,                /* xShmMap */
75454   0,                /* xShmLock */
75455   0,                /* xShmBarrier */
75456   0,                /* xShmUnmap */
75457   0,                /* xFetch */
75458   0                 /* xUnfetch */
75459 };
75460 
75461 /*
75462 ** Open a journal file.
75463 */
75464 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
75465   MemJournal *p = (MemJournal *)pJfd;
75466   assert( EIGHT_BYTE_ALIGNMENT(p) );
75467   memset(p, 0, sqlite3MemJournalSize());
75468   p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
75469 }
75470 
75471 /*
75472 ** Return true if the file-handle passed as an argument is
75473 ** an in-memory journal
75474 */
75475 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
75476   return pJfd->pMethods==&MemJournalMethods;
75477 }
75478 
75479 /*
75480 ** Return the number of bytes required to store a MemJournal file descriptor.
75481 */
75482 SQLITE_PRIVATE int sqlite3MemJournalSize(void){
75483   return sizeof(MemJournal);
75484 }
75485 
75486 /************** End of memjournal.c ******************************************/
75487 /************** Begin file walker.c ******************************************/
75488 /*
75489 ** 2008 August 16
75490 **
75491 ** The author disclaims copyright to this source code.  In place of
75492 ** a legal notice, here is a blessing:
75493 **
75494 **    May you do good and not evil.
75495 **    May you find forgiveness for yourself and forgive others.
75496 **    May you share freely, never taking more than you give.
75497 **
75498 *************************************************************************
75499 ** This file contains routines used for walking the parser tree for
75500 ** an SQL statement.
75501 */
75502 /* #include <stdlib.h> */
75503 /* #include <string.h> */
75504 
75505 
75506 /*
75507 ** Walk an expression tree.  Invoke the callback once for each node
75508 ** of the expression, while decending.  (In other words, the callback
75509 ** is invoked before visiting children.)
75510 **
75511 ** The return value from the callback should be one of the WRC_*
75512 ** constants to specify how to proceed with the walk.
75513 **
75514 **    WRC_Continue      Continue descending down the tree.
75515 **
75516 **    WRC_Prune         Do not descend into child nodes.  But allow
75517 **                      the walk to continue with sibling nodes.
75518 **
75519 **    WRC_Abort         Do no more callbacks.  Unwind the stack and
75520 **                      return the top-level walk call.
75521 **
75522 ** The return value from this routine is WRC_Abort to abandon the tree walk
75523 ** and WRC_Continue to continue.
75524 */
75525 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
75526   int rc;
75527   if( pExpr==0 ) return WRC_Continue;
75528   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
75529   testcase( ExprHasProperty(pExpr, EP_Reduced) );
75530   rc = pWalker->xExprCallback(pWalker, pExpr);
75531   if( rc==WRC_Continue
75532               && !ExprHasProperty(pExpr,EP_TokenOnly) ){
75533     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
75534     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
75535     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
75536       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
75537     }else{
75538       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
75539     }
75540   }
75541   return rc & WRC_Abort;
75542 }
75543 
75544 /*
75545 ** Call sqlite3WalkExpr() for every expression in list p or until
75546 ** an abort request is seen.
75547 */
75548 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
75549   int i;
75550   struct ExprList_item *pItem;
75551   if( p ){
75552     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
75553       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
75554     }
75555   }
75556   return WRC_Continue;
75557 }
75558 
75559 /*
75560 ** Walk all expressions associated with SELECT statement p.  Do
75561 ** not invoke the SELECT callback on p, but do (of course) invoke
75562 ** any expr callbacks and SELECT callbacks that come from subqueries.
75563 ** Return WRC_Abort or WRC_Continue.
75564 */
75565 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
75566   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
75567   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
75568   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
75569   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
75570   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
75571   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
75572   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
75573   return WRC_Continue;
75574 }
75575 
75576 /*
75577 ** Walk the parse trees associated with all subqueries in the
75578 ** FROM clause of SELECT statement p.  Do not invoke the select
75579 ** callback on p, but do invoke it on each FROM clause subquery
75580 ** and on any subqueries further down in the tree.  Return
75581 ** WRC_Abort or WRC_Continue;
75582 */
75583 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
75584   SrcList *pSrc;
75585   int i;
75586   struct SrcList_item *pItem;
75587 
75588   pSrc = p->pSrc;
75589   if( ALWAYS(pSrc) ){
75590     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
75591       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
75592         return WRC_Abort;
75593       }
75594     }
75595   }
75596   return WRC_Continue;
75597 }
75598 
75599 /*
75600 ** Call sqlite3WalkExpr() for every expression in Select statement p.
75601 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
75602 ** on the compound select chain, p->pPrior.
75603 **
75604 ** If it is not NULL, the xSelectCallback() callback is invoked before
75605 ** the walk of the expressions and FROM clause. The xSelectCallback2()
75606 ** method, if it is not NULL, is invoked following the walk of the
75607 ** expressions and FROM clause.
75608 **
75609 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
75610 ** there is an abort request.
75611 **
75612 ** If the Walker does not have an xSelectCallback() then this routine
75613 ** is a no-op returning WRC_Continue.
75614 */
75615 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
75616   int rc;
75617   if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
75618     return WRC_Continue;
75619   }
75620   rc = WRC_Continue;
75621   pWalker->walkerDepth++;
75622   while( p ){
75623     if( pWalker->xSelectCallback ){
75624        rc = pWalker->xSelectCallback(pWalker, p);
75625        if( rc ) break;
75626     }
75627     if( sqlite3WalkSelectExpr(pWalker, p)
75628      || sqlite3WalkSelectFrom(pWalker, p)
75629     ){
75630       pWalker->walkerDepth--;
75631       return WRC_Abort;
75632     }
75633     if( pWalker->xSelectCallback2 ){
75634       pWalker->xSelectCallback2(pWalker, p);
75635     }
75636     p = p->pPrior;
75637   }
75638   pWalker->walkerDepth--;
75639   return rc & WRC_Abort;
75640 }
75641 
75642 /************** End of walker.c **********************************************/
75643 /************** Begin file resolve.c *****************************************/
75644 /*
75645 ** 2008 August 18
75646 **
75647 ** The author disclaims copyright to this source code.  In place of
75648 ** a legal notice, here is a blessing:
75649 **
75650 **    May you do good and not evil.
75651 **    May you find forgiveness for yourself and forgive others.
75652 **    May you share freely, never taking more than you give.
75653 **
75654 *************************************************************************
75655 **
75656 ** This file contains routines used for walking the parser tree and
75657 ** resolve all identifiers by associating them with a particular
75658 ** table and column.
75659 */
75660 /* #include <stdlib.h> */
75661 /* #include <string.h> */
75662 
75663 /*
75664 ** Walk the expression tree pExpr and increase the aggregate function
75665 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
75666 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
75667 ** outer query into an inner subquery.
75668 **
75669 ** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
75670 ** is a helper function - a callback for the tree walker.
75671 */
75672 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
75673   if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
75674   return WRC_Continue;
75675 }
75676 static void incrAggFunctionDepth(Expr *pExpr, int N){
75677   if( N>0 ){
75678     Walker w;
75679     memset(&w, 0, sizeof(w));
75680     w.xExprCallback = incrAggDepth;
75681     w.u.i = N;
75682     sqlite3WalkExpr(&w, pExpr);
75683   }
75684 }
75685 
75686 /*
75687 ** Turn the pExpr expression into an alias for the iCol-th column of the
75688 ** result set in pEList.
75689 **
75690 ** If the result set column is a simple column reference, then this routine
75691 ** makes an exact copy.  But for any other kind of expression, this
75692 ** routine make a copy of the result set column as the argument to the
75693 ** TK_AS operator.  The TK_AS operator causes the expression to be
75694 ** evaluated just once and then reused for each alias.
75695 **
75696 ** The reason for suppressing the TK_AS term when the expression is a simple
75697 ** column reference is so that the column reference will be recognized as
75698 ** usable by indices within the WHERE clause processing logic.
75699 **
75700 ** The TK_AS operator is inhibited if zType[0]=='G'.  This means
75701 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
75702 **
75703 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
75704 **
75705 ** Is equivalent to:
75706 **
75707 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
75708 **
75709 ** The result of random()%5 in the GROUP BY clause is probably different
75710 ** from the result in the result-set.  On the other hand Standard SQL does
75711 ** not allow the GROUP BY clause to contain references to result-set columns.
75712 ** So this should never come up in well-formed queries.
75713 **
75714 ** If the reference is followed by a COLLATE operator, then make sure
75715 ** the COLLATE operator is preserved.  For example:
75716 **
75717 **     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
75718 **
75719 ** Should be transformed into:
75720 **
75721 **     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
75722 **
75723 ** The nSubquery parameter specifies how many levels of subquery the
75724 ** alias is removed from the original expression.  The usually value is
75725 ** zero but it might be more if the alias is contained within a subquery
75726 ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
75727 ** structures must be increased by the nSubquery amount.
75728 */
75729 static void resolveAlias(
75730   Parse *pParse,         /* Parsing context */
75731   ExprList *pEList,      /* A result set */
75732   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
75733   Expr *pExpr,           /* Transform this into an alias to the result set */
75734   const char *zType,     /* "GROUP" or "ORDER" or "" */
75735   int nSubquery          /* Number of subqueries that the label is moving */
75736 ){
75737   Expr *pOrig;           /* The iCol-th column of the result set */
75738   Expr *pDup;            /* Copy of pOrig */
75739   sqlite3 *db;           /* The database connection */
75740 
75741   assert( iCol>=0 && iCol<pEList->nExpr );
75742   pOrig = pEList->a[iCol].pExpr;
75743   assert( pOrig!=0 );
75744   assert( pOrig->flags & EP_Resolved );
75745   db = pParse->db;
75746   pDup = sqlite3ExprDup(db, pOrig, 0);
75747   if( pDup==0 ) return;
75748   if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
75749     incrAggFunctionDepth(pDup, nSubquery);
75750     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
75751     if( pDup==0 ) return;
75752     ExprSetProperty(pDup, EP_Skip);
75753     if( pEList->a[iCol].u.x.iAlias==0 ){
75754       pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
75755     }
75756     pDup->iTable = pEList->a[iCol].u.x.iAlias;
75757   }
75758   if( pExpr->op==TK_COLLATE ){
75759     pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
75760   }
75761 
75762   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
75763   ** prevents ExprDelete() from deleting the Expr structure itself,
75764   ** allowing it to be repopulated by the memcpy() on the following line.
75765   ** The pExpr->u.zToken might point into memory that will be freed by the
75766   ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
75767   ** make a copy of the token before doing the sqlite3DbFree().
75768   */
75769   ExprSetProperty(pExpr, EP_Static);
75770   sqlite3ExprDelete(db, pExpr);
75771   memcpy(pExpr, pDup, sizeof(*pExpr));
75772   if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
75773     assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
75774     pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
75775     pExpr->flags |= EP_MemToken;
75776   }
75777   sqlite3DbFree(db, pDup);
75778 }
75779 
75780 
75781 /*
75782 ** Return TRUE if the name zCol occurs anywhere in the USING clause.
75783 **
75784 ** Return FALSE if the USING clause is NULL or if it does not contain
75785 ** zCol.
75786 */
75787 static int nameInUsingClause(IdList *pUsing, const char *zCol){
75788   if( pUsing ){
75789     int k;
75790     for(k=0; k<pUsing->nId; k++){
75791       if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
75792     }
75793   }
75794   return 0;
75795 }
75796 
75797 /*
75798 ** Subqueries stores the original database, table and column names for their
75799 ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
75800 ** Check to see if the zSpan given to this routine matches the zDb, zTab,
75801 ** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
75802 ** match anything.
75803 */
75804 SQLITE_PRIVATE int sqlite3MatchSpanName(
75805   const char *zSpan,
75806   const char *zCol,
75807   const char *zTab,
75808   const char *zDb
75809 ){
75810   int n;
75811   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
75812   if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
75813     return 0;
75814   }
75815   zSpan += n+1;
75816   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
75817   if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
75818     return 0;
75819   }
75820   zSpan += n+1;
75821   if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
75822     return 0;
75823   }
75824   return 1;
75825 }
75826 
75827 /*
75828 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
75829 ** that name in the set of source tables in pSrcList and make the pExpr
75830 ** expression node refer back to that source column.  The following changes
75831 ** are made to pExpr:
75832 **
75833 **    pExpr->iDb           Set the index in db->aDb[] of the database X
75834 **                         (even if X is implied).
75835 **    pExpr->iTable        Set to the cursor number for the table obtained
75836 **                         from pSrcList.
75837 **    pExpr->pTab          Points to the Table structure of X.Y (even if
75838 **                         X and/or Y are implied.)
75839 **    pExpr->iColumn       Set to the column number within the table.
75840 **    pExpr->op            Set to TK_COLUMN.
75841 **    pExpr->pLeft         Any expression this points to is deleted
75842 **    pExpr->pRight        Any expression this points to is deleted.
75843 **
75844 ** The zDb variable is the name of the database (the "X").  This value may be
75845 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
75846 ** can be used.  The zTable variable is the name of the table (the "Y").  This
75847 ** value can be NULL if zDb is also NULL.  If zTable is NULL it
75848 ** means that the form of the name is Z and that columns from any table
75849 ** can be used.
75850 **
75851 ** If the name cannot be resolved unambiguously, leave an error message
75852 ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
75853 */
75854 static int lookupName(
75855   Parse *pParse,       /* The parsing context */
75856   const char *zDb,     /* Name of the database containing table, or NULL */
75857   const char *zTab,    /* Name of table containing column, or NULL */
75858   const char *zCol,    /* Name of the column. */
75859   NameContext *pNC,    /* The name context used to resolve the name */
75860   Expr *pExpr          /* Make this EXPR node point to the selected column */
75861 ){
75862   int i, j;                         /* Loop counters */
75863   int cnt = 0;                      /* Number of matching column names */
75864   int cntTab = 0;                   /* Number of matching table names */
75865   int nSubquery = 0;                /* How many levels of subquery */
75866   sqlite3 *db = pParse->db;         /* The database connection */
75867   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
75868   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
75869   NameContext *pTopNC = pNC;        /* First namecontext in the list */
75870   Schema *pSchema = 0;              /* Schema of the expression */
75871   int isTrigger = 0;                /* True if resolved to a trigger column */
75872   Table *pTab = 0;                  /* Table hold the row */
75873   Column *pCol;                     /* A column of pTab */
75874 
75875   assert( pNC );     /* the name context cannot be NULL. */
75876   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
75877   assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
75878 
75879   /* Initialize the node to no-match */
75880   pExpr->iTable = -1;
75881   pExpr->pTab = 0;
75882   ExprSetVVAProperty(pExpr, EP_NoReduce);
75883 
75884   /* Translate the schema name in zDb into a pointer to the corresponding
75885   ** schema.  If not found, pSchema will remain NULL and nothing will match
75886   ** resulting in an appropriate error message toward the end of this routine
75887   */
75888   if( zDb ){
75889     testcase( pNC->ncFlags & NC_PartIdx );
75890     testcase( pNC->ncFlags & NC_IsCheck );
75891     if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
75892       /* Silently ignore database qualifiers inside CHECK constraints and partial
75893       ** indices.  Do not raise errors because that might break legacy and
75894       ** because it does not hurt anything to just ignore the database name. */
75895       zDb = 0;
75896     }else{
75897       for(i=0; i<db->nDb; i++){
75898         assert( db->aDb[i].zName );
75899         if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
75900           pSchema = db->aDb[i].pSchema;
75901           break;
75902         }
75903       }
75904     }
75905   }
75906 
75907   /* Start at the inner-most context and move outward until a match is found */
75908   while( pNC && cnt==0 ){
75909     ExprList *pEList;
75910     SrcList *pSrcList = pNC->pSrcList;
75911 
75912     if( pSrcList ){
75913       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
75914         pTab = pItem->pTab;
75915         assert( pTab!=0 && pTab->zName!=0 );
75916         assert( pTab->nCol>0 );
75917         if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
75918           int hit = 0;
75919           pEList = pItem->pSelect->pEList;
75920           for(j=0; j<pEList->nExpr; j++){
75921             if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
75922               cnt++;
75923               cntTab = 2;
75924               pMatch = pItem;
75925               pExpr->iColumn = j;
75926               hit = 1;
75927             }
75928           }
75929           if( hit || zTab==0 ) continue;
75930         }
75931         if( zDb && pTab->pSchema!=pSchema ){
75932           continue;
75933         }
75934         if( zTab ){
75935           const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
75936           assert( zTabName!=0 );
75937           if( sqlite3StrICmp(zTabName, zTab)!=0 ){
75938             continue;
75939           }
75940         }
75941         if( 0==(cntTab++) ){
75942           pMatch = pItem;
75943         }
75944         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
75945           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
75946             /* If there has been exactly one prior match and this match
75947             ** is for the right-hand table of a NATURAL JOIN or is in a
75948             ** USING clause, then skip this match.
75949             */
75950             if( cnt==1 ){
75951               if( pItem->jointype & JT_NATURAL ) continue;
75952               if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
75953             }
75954             cnt++;
75955             pMatch = pItem;
75956             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
75957             pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
75958             break;
75959           }
75960         }
75961       }
75962       if( pMatch ){
75963         pExpr->iTable = pMatch->iCursor;
75964         pExpr->pTab = pMatch->pTab;
75965         pSchema = pExpr->pTab->pSchema;
75966       }
75967     } /* if( pSrcList ) */
75968 
75969 #ifndef SQLITE_OMIT_TRIGGER
75970     /* If we have not already resolved the name, then maybe
75971     ** it is a new.* or old.* trigger argument reference
75972     */
75973     if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
75974       int op = pParse->eTriggerOp;
75975       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
75976       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
75977         pExpr->iTable = 1;
75978         pTab = pParse->pTriggerTab;
75979       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
75980         pExpr->iTable = 0;
75981         pTab = pParse->pTriggerTab;
75982       }else{
75983         pTab = 0;
75984       }
75985 
75986       if( pTab ){
75987         int iCol;
75988         pSchema = pTab->pSchema;
75989         cntTab++;
75990         for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
75991           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
75992             if( iCol==pTab->iPKey ){
75993               iCol = -1;
75994             }
75995             break;
75996           }
75997         }
75998         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
75999           /* IMP: R-24309-18625 */
76000           /* IMP: R-44911-55124 */
76001           iCol = -1;
76002         }
76003         if( iCol<pTab->nCol ){
76004           cnt++;
76005           if( iCol<0 ){
76006             pExpr->affinity = SQLITE_AFF_INTEGER;
76007           }else if( pExpr->iTable==0 ){
76008             testcase( iCol==31 );
76009             testcase( iCol==32 );
76010             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
76011           }else{
76012             testcase( iCol==31 );
76013             testcase( iCol==32 );
76014             pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
76015           }
76016           pExpr->iColumn = (i16)iCol;
76017           pExpr->pTab = pTab;
76018           isTrigger = 1;
76019         }
76020       }
76021     }
76022 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
76023 
76024     /*
76025     ** Perhaps the name is a reference to the ROWID
76026     */
76027     if( cnt==0 && cntTab==1 && pMatch && sqlite3IsRowid(zCol)
76028      && HasRowid(pMatch->pTab) ){
76029       cnt = 1;
76030       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
76031       pExpr->affinity = SQLITE_AFF_INTEGER;
76032     }
76033 
76034     /*
76035     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
76036     ** might refer to an result-set alias.  This happens, for example, when
76037     ** we are resolving names in the WHERE clause of the following command:
76038     **
76039     **     SELECT a+b AS x FROM table WHERE x<10;
76040     **
76041     ** In cases like this, replace pExpr with a copy of the expression that
76042     ** forms the result set entry ("a+b" in the example) and return immediately.
76043     ** Note that the expression in the result set should have already been
76044     ** resolved by the time the WHERE clause is resolved.
76045     **
76046     ** The ability to use an output result-set column in the WHERE, GROUP BY,
76047     ** or HAVING clauses, or as part of a larger expression in the ORDRE BY
76048     ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
76049     ** is supported for backwards compatibility only.  TO DO: Issue a warning
76050     ** on sqlite3_log() whenever the capability is used.
76051     */
76052     if( (pEList = pNC->pEList)!=0
76053      && zTab==0
76054      && cnt==0
76055     ){
76056       for(j=0; j<pEList->nExpr; j++){
76057         char *zAs = pEList->a[j].zName;
76058         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
76059           Expr *pOrig;
76060           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
76061           assert( pExpr->x.pList==0 );
76062           assert( pExpr->x.pSelect==0 );
76063           pOrig = pEList->a[j].pExpr;
76064           if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
76065             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
76066             return WRC_Abort;
76067           }
76068           resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
76069           cnt = 1;
76070           pMatch = 0;
76071           assert( zTab==0 && zDb==0 );
76072           goto lookupname_end;
76073         }
76074       }
76075     }
76076 
76077     /* Advance to the next name context.  The loop will exit when either
76078     ** we have a match (cnt>0) or when we run out of name contexts.
76079     */
76080     if( cnt==0 ){
76081       pNC = pNC->pNext;
76082       nSubquery++;
76083     }
76084   }
76085 
76086   /*
76087   ** If X and Y are NULL (in other words if only the column name Z is
76088   ** supplied) and the value of Z is enclosed in double-quotes, then
76089   ** Z is a string literal if it doesn't match any column names.  In that
76090   ** case, we need to return right away and not make any changes to
76091   ** pExpr.
76092   **
76093   ** Because no reference was made to outer contexts, the pNC->nRef
76094   ** fields are not changed in any context.
76095   */
76096   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
76097     pExpr->op = TK_STRING;
76098     pExpr->pTab = 0;
76099     return WRC_Prune;
76100   }
76101 
76102   /*
76103   ** cnt==0 means there was not match.  cnt>1 means there were two or
76104   ** more matches.  Either way, we have an error.
76105   */
76106   if( cnt!=1 ){
76107     const char *zErr;
76108     zErr = cnt==0 ? "no such column" : "ambiguous column name";
76109     if( zDb ){
76110       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
76111     }else if( zTab ){
76112       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
76113     }else{
76114       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
76115     }
76116     pParse->checkSchema = 1;
76117     pTopNC->nErr++;
76118   }
76119 
76120   /* If a column from a table in pSrcList is referenced, then record
76121   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
76122   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
76123   ** column number is greater than the number of bits in the bitmask
76124   ** then set the high-order bit of the bitmask.
76125   */
76126   if( pExpr->iColumn>=0 && pMatch!=0 ){
76127     int n = pExpr->iColumn;
76128     testcase( n==BMS-1 );
76129     if( n>=BMS ){
76130       n = BMS-1;
76131     }
76132     assert( pMatch->iCursor==pExpr->iTable );
76133     pMatch->colUsed |= ((Bitmask)1)<<n;
76134   }
76135 
76136   /* Clean up and return
76137   */
76138   sqlite3ExprDelete(db, pExpr->pLeft);
76139   pExpr->pLeft = 0;
76140   sqlite3ExprDelete(db, pExpr->pRight);
76141   pExpr->pRight = 0;
76142   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
76143 lookupname_end:
76144   if( cnt==1 ){
76145     assert( pNC!=0 );
76146     if( pExpr->op!=TK_AS ){
76147       sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
76148     }
76149     /* Increment the nRef value on all name contexts from TopNC up to
76150     ** the point where the name matched. */
76151     for(;;){
76152       assert( pTopNC!=0 );
76153       pTopNC->nRef++;
76154       if( pTopNC==pNC ) break;
76155       pTopNC = pTopNC->pNext;
76156     }
76157     return WRC_Prune;
76158   } else {
76159     return WRC_Abort;
76160   }
76161 }
76162 
76163 /*
76164 ** Allocate and return a pointer to an expression to load the column iCol
76165 ** from datasource iSrc in SrcList pSrc.
76166 */
76167 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
76168   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
76169   if( p ){
76170     struct SrcList_item *pItem = &pSrc->a[iSrc];
76171     p->pTab = pItem->pTab;
76172     p->iTable = pItem->iCursor;
76173     if( p->pTab->iPKey==iCol ){
76174       p->iColumn = -1;
76175     }else{
76176       p->iColumn = (ynVar)iCol;
76177       testcase( iCol==BMS );
76178       testcase( iCol==BMS-1 );
76179       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
76180     }
76181     ExprSetProperty(p, EP_Resolved);
76182   }
76183   return p;
76184 }
76185 
76186 /*
76187 ** Report an error that an expression is not valid for a partial index WHERE
76188 ** clause.
76189 */
76190 static void notValidPartIdxWhere(
76191   Parse *pParse,       /* Leave error message here */
76192   NameContext *pNC,    /* The name context */
76193   const char *zMsg     /* Type of error */
76194 ){
76195   if( (pNC->ncFlags & NC_PartIdx)!=0 ){
76196     sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
76197                     zMsg);
76198   }
76199 }
76200 
76201 #ifndef SQLITE_OMIT_CHECK
76202 /*
76203 ** Report an error that an expression is not valid for a CHECK constraint.
76204 */
76205 static void notValidCheckConstraint(
76206   Parse *pParse,       /* Leave error message here */
76207   NameContext *pNC,    /* The name context */
76208   const char *zMsg     /* Type of error */
76209 ){
76210   if( (pNC->ncFlags & NC_IsCheck)!=0 ){
76211     sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
76212   }
76213 }
76214 #else
76215 # define notValidCheckConstraint(P,N,M)
76216 #endif
76217 
76218 /*
76219 ** Expression p should encode a floating point value between 1.0 and 0.0.
76220 ** Return 1024 times this value.  Or return -1 if p is not a floating point
76221 ** value between 1.0 and 0.0.
76222 */
76223 static int exprProbability(Expr *p){
76224   double r = -1.0;
76225   if( p->op!=TK_FLOAT ) return -1;
76226   sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
76227   assert( r>=0.0 );
76228   if( r>1.0 ) return -1;
76229   return (int)(r*1000.0);
76230 }
76231 
76232 /*
76233 ** This routine is callback for sqlite3WalkExpr().
76234 **
76235 ** Resolve symbolic names into TK_COLUMN operators for the current
76236 ** node in the expression tree.  Return 0 to continue the search down
76237 ** the tree or 2 to abort the tree walk.
76238 **
76239 ** This routine also does error checking and name resolution for
76240 ** function names.  The operator for aggregate functions is changed
76241 ** to TK_AGG_FUNCTION.
76242 */
76243 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
76244   NameContext *pNC;
76245   Parse *pParse;
76246 
76247   pNC = pWalker->u.pNC;
76248   assert( pNC!=0 );
76249   pParse = pNC->pParse;
76250   assert( pParse==pWalker->pParse );
76251 
76252   if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
76253   ExprSetProperty(pExpr, EP_Resolved);
76254 #ifndef NDEBUG
76255   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
76256     SrcList *pSrcList = pNC->pSrcList;
76257     int i;
76258     for(i=0; i<pNC->pSrcList->nSrc; i++){
76259       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
76260     }
76261   }
76262 #endif
76263   switch( pExpr->op ){
76264 
76265 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
76266     /* The special operator TK_ROW means use the rowid for the first
76267     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
76268     ** clause processing on UPDATE and DELETE statements.
76269     */
76270     case TK_ROW: {
76271       SrcList *pSrcList = pNC->pSrcList;
76272       struct SrcList_item *pItem;
76273       assert( pSrcList && pSrcList->nSrc==1 );
76274       pItem = pSrcList->a;
76275       pExpr->op = TK_COLUMN;
76276       pExpr->pTab = pItem->pTab;
76277       pExpr->iTable = pItem->iCursor;
76278       pExpr->iColumn = -1;
76279       pExpr->affinity = SQLITE_AFF_INTEGER;
76280       break;
76281     }
76282 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
76283 
76284     /* A lone identifier is the name of a column.
76285     */
76286     case TK_ID: {
76287       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
76288     }
76289 
76290     /* A table name and column name:     ID.ID
76291     ** Or a database, table and column:  ID.ID.ID
76292     */
76293     case TK_DOT: {
76294       const char *zColumn;
76295       const char *zTable;
76296       const char *zDb;
76297       Expr *pRight;
76298 
76299       /* if( pSrcList==0 ) break; */
76300       pRight = pExpr->pRight;
76301       if( pRight->op==TK_ID ){
76302         zDb = 0;
76303         zTable = pExpr->pLeft->u.zToken;
76304         zColumn = pRight->u.zToken;
76305       }else{
76306         assert( pRight->op==TK_DOT );
76307         zDb = pExpr->pLeft->u.zToken;
76308         zTable = pRight->pLeft->u.zToken;
76309         zColumn = pRight->pRight->u.zToken;
76310       }
76311       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
76312     }
76313 
76314     /* Resolve function names
76315     */
76316     case TK_FUNCTION: {
76317       ExprList *pList = pExpr->x.pList;    /* The argument list */
76318       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
76319       int no_such_func = 0;       /* True if no such function exists */
76320       int wrong_num_args = 0;     /* True if wrong number of arguments */
76321       int is_agg = 0;             /* True if is an aggregate function */
76322       int auth;                   /* Authorization to use the function */
76323       int nId;                    /* Number of characters in function name */
76324       const char *zId;            /* The function name. */
76325       FuncDef *pDef;              /* Information about the function */
76326       u8 enc = ENC(pParse->db);   /* The database encoding */
76327 
76328       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
76329       notValidPartIdxWhere(pParse, pNC, "functions");
76330       zId = pExpr->u.zToken;
76331       nId = sqlite3Strlen30(zId);
76332       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
76333       if( pDef==0 ){
76334         pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
76335         if( pDef==0 ){
76336           no_such_func = 1;
76337         }else{
76338           wrong_num_args = 1;
76339         }
76340       }else{
76341         is_agg = pDef->xFunc==0;
76342         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
76343           ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
76344           if( n==2 ){
76345             pExpr->iTable = exprProbability(pList->a[1].pExpr);
76346             if( pExpr->iTable<0 ){
76347               sqlite3ErrorMsg(pParse, "second argument to likelihood() must be a "
76348                                       "constant between 0.0 and 1.0");
76349               pNC->nErr++;
76350             }
76351           }else{
76352             /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
76353             ** likelihood(X, 0.0625).
76354             ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
76355             ** likelihood(X,0.0625). */
76356             pExpr->iTable = 62;  /* TUNING:  Default 2nd arg to unlikely() is 0.0625 */
76357           }
76358         }
76359       }
76360 #ifndef SQLITE_OMIT_AUTHORIZATION
76361       if( pDef ){
76362         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
76363         if( auth!=SQLITE_OK ){
76364           if( auth==SQLITE_DENY ){
76365             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
76366                                     pDef->zName);
76367             pNC->nErr++;
76368           }
76369           pExpr->op = TK_NULL;
76370           return WRC_Prune;
76371         }
76372         if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Constant);
76373       }
76374 #endif
76375       if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
76376         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
76377         pNC->nErr++;
76378         is_agg = 0;
76379       }else if( no_such_func && pParse->db->init.busy==0 ){
76380         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
76381         pNC->nErr++;
76382       }else if( wrong_num_args ){
76383         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
76384              nId, zId);
76385         pNC->nErr++;
76386       }
76387       if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
76388       sqlite3WalkExprList(pWalker, pList);
76389       if( is_agg ){
76390         NameContext *pNC2 = pNC;
76391         pExpr->op = TK_AGG_FUNCTION;
76392         pExpr->op2 = 0;
76393         while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
76394           pExpr->op2++;
76395           pNC2 = pNC2->pNext;
76396         }
76397         if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
76398         pNC->ncFlags |= NC_AllowAgg;
76399       }
76400       /* FIX ME:  Compute pExpr->affinity based on the expected return
76401       ** type of the function
76402       */
76403       return WRC_Prune;
76404     }
76405 #ifndef SQLITE_OMIT_SUBQUERY
76406     case TK_SELECT:
76407     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
76408 #endif
76409     case TK_IN: {
76410       testcase( pExpr->op==TK_IN );
76411       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
76412         int nRef = pNC->nRef;
76413         notValidCheckConstraint(pParse, pNC, "subqueries");
76414         notValidPartIdxWhere(pParse, pNC, "subqueries");
76415         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
76416         assert( pNC->nRef>=nRef );
76417         if( nRef!=pNC->nRef ){
76418           ExprSetProperty(pExpr, EP_VarSelect);
76419         }
76420       }
76421       break;
76422     }
76423     case TK_VARIABLE: {
76424       notValidCheckConstraint(pParse, pNC, "parameters");
76425       notValidPartIdxWhere(pParse, pNC, "parameters");
76426       break;
76427     }
76428   }
76429   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
76430 }
76431 
76432 /*
76433 ** pEList is a list of expressions which are really the result set of the
76434 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
76435 ** This routine checks to see if pE is a simple identifier which corresponds
76436 ** to the AS-name of one of the terms of the expression list.  If it is,
76437 ** this routine return an integer between 1 and N where N is the number of
76438 ** elements in pEList, corresponding to the matching entry.  If there is
76439 ** no match, or if pE is not a simple identifier, then this routine
76440 ** return 0.
76441 **
76442 ** pEList has been resolved.  pE has not.
76443 */
76444 static int resolveAsName(
76445   Parse *pParse,     /* Parsing context for error messages */
76446   ExprList *pEList,  /* List of expressions to scan */
76447   Expr *pE           /* Expression we are trying to match */
76448 ){
76449   int i;             /* Loop counter */
76450 
76451   UNUSED_PARAMETER(pParse);
76452 
76453   if( pE->op==TK_ID ){
76454     char *zCol = pE->u.zToken;
76455     for(i=0; i<pEList->nExpr; i++){
76456       char *zAs = pEList->a[i].zName;
76457       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
76458         return i+1;
76459       }
76460     }
76461   }
76462   return 0;
76463 }
76464 
76465 /*
76466 ** pE is a pointer to an expression which is a single term in the
76467 ** ORDER BY of a compound SELECT.  The expression has not been
76468 ** name resolved.
76469 **
76470 ** At the point this routine is called, we already know that the
76471 ** ORDER BY term is not an integer index into the result set.  That
76472 ** case is handled by the calling routine.
76473 **
76474 ** Attempt to match pE against result set columns in the left-most
76475 ** SELECT statement.  Return the index i of the matching column,
76476 ** as an indication to the caller that it should sort by the i-th column.
76477 ** The left-most column is 1.  In other words, the value returned is the
76478 ** same integer value that would be used in the SQL statement to indicate
76479 ** the column.
76480 **
76481 ** If there is no match, return 0.  Return -1 if an error occurs.
76482 */
76483 static int resolveOrderByTermToExprList(
76484   Parse *pParse,     /* Parsing context for error messages */
76485   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
76486   Expr *pE           /* The specific ORDER BY term */
76487 ){
76488   int i;             /* Loop counter */
76489   ExprList *pEList;  /* The columns of the result set */
76490   NameContext nc;    /* Name context for resolving pE */
76491   sqlite3 *db;       /* Database connection */
76492   int rc;            /* Return code from subprocedures */
76493   u8 savedSuppErr;   /* Saved value of db->suppressErr */
76494 
76495   assert( sqlite3ExprIsInteger(pE, &i)==0 );
76496   pEList = pSelect->pEList;
76497 
76498   /* Resolve all names in the ORDER BY term expression
76499   */
76500   memset(&nc, 0, sizeof(nc));
76501   nc.pParse = pParse;
76502   nc.pSrcList = pSelect->pSrc;
76503   nc.pEList = pEList;
76504   nc.ncFlags = NC_AllowAgg;
76505   nc.nErr = 0;
76506   db = pParse->db;
76507   savedSuppErr = db->suppressErr;
76508   db->suppressErr = 1;
76509   rc = sqlite3ResolveExprNames(&nc, pE);
76510   db->suppressErr = savedSuppErr;
76511   if( rc ) return 0;
76512 
76513   /* Try to match the ORDER BY expression against an expression
76514   ** in the result set.  Return an 1-based index of the matching
76515   ** result-set entry.
76516   */
76517   for(i=0; i<pEList->nExpr; i++){
76518     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
76519       return i+1;
76520     }
76521   }
76522 
76523   /* If no match, return 0. */
76524   return 0;
76525 }
76526 
76527 /*
76528 ** Generate an ORDER BY or GROUP BY term out-of-range error.
76529 */
76530 static void resolveOutOfRangeError(
76531   Parse *pParse,         /* The error context into which to write the error */
76532   const char *zType,     /* "ORDER" or "GROUP" */
76533   int i,                 /* The index (1-based) of the term out of range */
76534   int mx                 /* Largest permissible value of i */
76535 ){
76536   sqlite3ErrorMsg(pParse,
76537     "%r %s BY term out of range - should be "
76538     "between 1 and %d", i, zType, mx);
76539 }
76540 
76541 /*
76542 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
76543 ** each term of the ORDER BY clause is a constant integer between 1
76544 ** and N where N is the number of columns in the compound SELECT.
76545 **
76546 ** ORDER BY terms that are already an integer between 1 and N are
76547 ** unmodified.  ORDER BY terms that are integers outside the range of
76548 ** 1 through N generate an error.  ORDER BY terms that are expressions
76549 ** are matched against result set expressions of compound SELECT
76550 ** beginning with the left-most SELECT and working toward the right.
76551 ** At the first match, the ORDER BY expression is transformed into
76552 ** the integer column number.
76553 **
76554 ** Return the number of errors seen.
76555 */
76556 static int resolveCompoundOrderBy(
76557   Parse *pParse,        /* Parsing context.  Leave error messages here */
76558   Select *pSelect       /* The SELECT statement containing the ORDER BY */
76559 ){
76560   int i;
76561   ExprList *pOrderBy;
76562   ExprList *pEList;
76563   sqlite3 *db;
76564   int moreToDo = 1;
76565 
76566   pOrderBy = pSelect->pOrderBy;
76567   if( pOrderBy==0 ) return 0;
76568   db = pParse->db;
76569 #if SQLITE_MAX_COLUMN
76570   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
76571     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
76572     return 1;
76573   }
76574 #endif
76575   for(i=0; i<pOrderBy->nExpr; i++){
76576     pOrderBy->a[i].done = 0;
76577   }
76578   pSelect->pNext = 0;
76579   while( pSelect->pPrior ){
76580     pSelect->pPrior->pNext = pSelect;
76581     pSelect = pSelect->pPrior;
76582   }
76583   while( pSelect && moreToDo ){
76584     struct ExprList_item *pItem;
76585     moreToDo = 0;
76586     pEList = pSelect->pEList;
76587     assert( pEList!=0 );
76588     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76589       int iCol = -1;
76590       Expr *pE, *pDup;
76591       if( pItem->done ) continue;
76592       pE = sqlite3ExprSkipCollate(pItem->pExpr);
76593       if( sqlite3ExprIsInteger(pE, &iCol) ){
76594         if( iCol<=0 || iCol>pEList->nExpr ){
76595           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
76596           return 1;
76597         }
76598       }else{
76599         iCol = resolveAsName(pParse, pEList, pE);
76600         if( iCol==0 ){
76601           pDup = sqlite3ExprDup(db, pE, 0);
76602           if( !db->mallocFailed ){
76603             assert(pDup);
76604             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
76605           }
76606           sqlite3ExprDelete(db, pDup);
76607         }
76608       }
76609       if( iCol>0 ){
76610         /* Convert the ORDER BY term into an integer column number iCol,
76611         ** taking care to preserve the COLLATE clause if it exists */
76612         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
76613         if( pNew==0 ) return 1;
76614         pNew->flags |= EP_IntValue;
76615         pNew->u.iValue = iCol;
76616         if( pItem->pExpr==pE ){
76617           pItem->pExpr = pNew;
76618         }else{
76619           assert( pItem->pExpr->op==TK_COLLATE );
76620           assert( pItem->pExpr->pLeft==pE );
76621           pItem->pExpr->pLeft = pNew;
76622         }
76623         sqlite3ExprDelete(db, pE);
76624         pItem->u.x.iOrderByCol = (u16)iCol;
76625         pItem->done = 1;
76626       }else{
76627         moreToDo = 1;
76628       }
76629     }
76630     pSelect = pSelect->pNext;
76631   }
76632   for(i=0; i<pOrderBy->nExpr; i++){
76633     if( pOrderBy->a[i].done==0 ){
76634       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
76635             "column in the result set", i+1);
76636       return 1;
76637     }
76638   }
76639   return 0;
76640 }
76641 
76642 /*
76643 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
76644 ** the SELECT statement pSelect.  If any term is reference to a
76645 ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
76646 ** field) then convert that term into a copy of the corresponding result set
76647 ** column.
76648 **
76649 ** If any errors are detected, add an error message to pParse and
76650 ** return non-zero.  Return zero if no errors are seen.
76651 */
76652 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
76653   Parse *pParse,        /* Parsing context.  Leave error messages here */
76654   Select *pSelect,      /* The SELECT statement containing the clause */
76655   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
76656   const char *zType     /* "ORDER" or "GROUP" */
76657 ){
76658   int i;
76659   sqlite3 *db = pParse->db;
76660   ExprList *pEList;
76661   struct ExprList_item *pItem;
76662 
76663   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
76664 #if SQLITE_MAX_COLUMN
76665   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
76666     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
76667     return 1;
76668   }
76669 #endif
76670   pEList = pSelect->pEList;
76671   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
76672   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76673     if( pItem->u.x.iOrderByCol ){
76674       if( pItem->u.x.iOrderByCol>pEList->nExpr ){
76675         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
76676         return 1;
76677       }
76678       resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
76679     }
76680   }
76681   return 0;
76682 }
76683 
76684 /*
76685 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
76686 ** The Name context of the SELECT statement is pNC.  zType is either
76687 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
76688 **
76689 ** This routine resolves each term of the clause into an expression.
76690 ** If the order-by term is an integer I between 1 and N (where N is the
76691 ** number of columns in the result set of the SELECT) then the expression
76692 ** in the resolution is a copy of the I-th result-set expression.  If
76693 ** the order-by term is an identifier that corresponds to the AS-name of
76694 ** a result-set expression, then the term resolves to a copy of the
76695 ** result-set expression.  Otherwise, the expression is resolved in
76696 ** the usual way - using sqlite3ResolveExprNames().
76697 **
76698 ** This routine returns the number of errors.  If errors occur, then
76699 ** an appropriate error message might be left in pParse.  (OOM errors
76700 ** excepted.)
76701 */
76702 static int resolveOrderGroupBy(
76703   NameContext *pNC,     /* The name context of the SELECT statement */
76704   Select *pSelect,      /* The SELECT statement holding pOrderBy */
76705   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
76706   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
76707 ){
76708   int i, j;                      /* Loop counters */
76709   int iCol;                      /* Column number */
76710   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
76711   Parse *pParse;                 /* Parsing context */
76712   int nResult;                   /* Number of terms in the result set */
76713 
76714   if( pOrderBy==0 ) return 0;
76715   nResult = pSelect->pEList->nExpr;
76716   pParse = pNC->pParse;
76717   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76718     Expr *pE = pItem->pExpr;
76719     Expr *pE2 = sqlite3ExprSkipCollate(pE);
76720     if( zType[0]!='G' ){
76721       iCol = resolveAsName(pParse, pSelect->pEList, pE2);
76722       if( iCol>0 ){
76723         /* If an AS-name match is found, mark this ORDER BY column as being
76724         ** a copy of the iCol-th result-set column.  The subsequent call to
76725         ** sqlite3ResolveOrderGroupBy() will convert the expression to a
76726         ** copy of the iCol-th result-set expression. */
76727         pItem->u.x.iOrderByCol = (u16)iCol;
76728         continue;
76729       }
76730     }
76731     if( sqlite3ExprIsInteger(pE2, &iCol) ){
76732       /* The ORDER BY term is an integer constant.  Again, set the column
76733       ** number so that sqlite3ResolveOrderGroupBy() will convert the
76734       ** order-by term to a copy of the result-set expression */
76735       if( iCol<1 || iCol>0xffff ){
76736         resolveOutOfRangeError(pParse, zType, i+1, nResult);
76737         return 1;
76738       }
76739       pItem->u.x.iOrderByCol = (u16)iCol;
76740       continue;
76741     }
76742 
76743     /* Otherwise, treat the ORDER BY term as an ordinary expression */
76744     pItem->u.x.iOrderByCol = 0;
76745     if( sqlite3ResolveExprNames(pNC, pE) ){
76746       return 1;
76747     }
76748     for(j=0; j<pSelect->pEList->nExpr; j++){
76749       if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
76750         pItem->u.x.iOrderByCol = j+1;
76751       }
76752     }
76753   }
76754   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
76755 }
76756 
76757 /*
76758 ** Resolve names in the SELECT statement p and all of its descendents.
76759 */
76760 static int resolveSelectStep(Walker *pWalker, Select *p){
76761   NameContext *pOuterNC;  /* Context that contains this SELECT */
76762   NameContext sNC;        /* Name context of this SELECT */
76763   int isCompound;         /* True if p is a compound select */
76764   int nCompound;          /* Number of compound terms processed so far */
76765   Parse *pParse;          /* Parsing context */
76766   ExprList *pEList;       /* Result set expression list */
76767   int i;                  /* Loop counter */
76768   ExprList *pGroupBy;     /* The GROUP BY clause */
76769   Select *pLeftmost;      /* Left-most of SELECT of a compound */
76770   sqlite3 *db;            /* Database connection */
76771 
76772 
76773   assert( p!=0 );
76774   if( p->selFlags & SF_Resolved ){
76775     return WRC_Prune;
76776   }
76777   pOuterNC = pWalker->u.pNC;
76778   pParse = pWalker->pParse;
76779   db = pParse->db;
76780 
76781   /* Normally sqlite3SelectExpand() will be called first and will have
76782   ** already expanded this SELECT.  However, if this is a subquery within
76783   ** an expression, sqlite3ResolveExprNames() will be called without a
76784   ** prior call to sqlite3SelectExpand().  When that happens, let
76785   ** sqlite3SelectPrep() do all of the processing for this SELECT.
76786   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
76787   ** this routine in the correct order.
76788   */
76789   if( (p->selFlags & SF_Expanded)==0 ){
76790     sqlite3SelectPrep(pParse, p, pOuterNC);
76791     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
76792   }
76793 
76794   isCompound = p->pPrior!=0;
76795   nCompound = 0;
76796   pLeftmost = p;
76797   while( p ){
76798     assert( (p->selFlags & SF_Expanded)!=0 );
76799     assert( (p->selFlags & SF_Resolved)==0 );
76800     p->selFlags |= SF_Resolved;
76801 
76802     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
76803     ** are not allowed to refer to any names, so pass an empty NameContext.
76804     */
76805     memset(&sNC, 0, sizeof(sNC));
76806     sNC.pParse = pParse;
76807     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
76808         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
76809       return WRC_Abort;
76810     }
76811 
76812     /* Recursively resolve names in all subqueries
76813     */
76814     for(i=0; i<p->pSrc->nSrc; i++){
76815       struct SrcList_item *pItem = &p->pSrc->a[i];
76816       if( pItem->pSelect ){
76817         NameContext *pNC;         /* Used to iterate name contexts */
76818         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
76819         const char *zSavedContext = pParse->zAuthContext;
76820 
76821         /* Count the total number of references to pOuterNC and all of its
76822         ** parent contexts. After resolving references to expressions in
76823         ** pItem->pSelect, check if this value has changed. If so, then
76824         ** SELECT statement pItem->pSelect must be correlated. Set the
76825         ** pItem->isCorrelated flag if this is the case. */
76826         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
76827 
76828         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
76829         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
76830         pParse->zAuthContext = zSavedContext;
76831         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
76832 
76833         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
76834         assert( pItem->isCorrelated==0 && nRef<=0 );
76835         pItem->isCorrelated = (nRef!=0);
76836       }
76837     }
76838 
76839     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
76840     ** resolve the result-set expression list.
76841     */
76842     sNC.ncFlags = NC_AllowAgg;
76843     sNC.pSrcList = p->pSrc;
76844     sNC.pNext = pOuterNC;
76845 
76846     /* Resolve names in the result set. */
76847     pEList = p->pEList;
76848     assert( pEList!=0 );
76849     for(i=0; i<pEList->nExpr; i++){
76850       Expr *pX = pEList->a[i].pExpr;
76851       if( sqlite3ResolveExprNames(&sNC, pX) ){
76852         return WRC_Abort;
76853       }
76854     }
76855 
76856     /* If there are no aggregate functions in the result-set, and no GROUP BY
76857     ** expression, do not allow aggregates in any of the other expressions.
76858     */
76859     assert( (p->selFlags & SF_Aggregate)==0 );
76860     pGroupBy = p->pGroupBy;
76861     if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
76862       p->selFlags |= SF_Aggregate;
76863     }else{
76864       sNC.ncFlags &= ~NC_AllowAgg;
76865     }
76866 
76867     /* If a HAVING clause is present, then there must be a GROUP BY clause.
76868     */
76869     if( p->pHaving && !pGroupBy ){
76870       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
76871       return WRC_Abort;
76872     }
76873 
76874     /* Add the output column list to the name-context before parsing the
76875     ** other expressions in the SELECT statement. This is so that
76876     ** expressions in the WHERE clause (etc.) can refer to expressions by
76877     ** aliases in the result set.
76878     **
76879     ** Minor point: If this is the case, then the expression will be
76880     ** re-evaluated for each reference to it.
76881     */
76882     sNC.pEList = p->pEList;
76883     if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
76884     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
76885 
76886     /* The ORDER BY and GROUP BY clauses may not refer to terms in
76887     ** outer queries
76888     */
76889     sNC.pNext = 0;
76890     sNC.ncFlags |= NC_AllowAgg;
76891 
76892     /* Process the ORDER BY clause for singleton SELECT statements.
76893     ** The ORDER BY clause for compounds SELECT statements is handled
76894     ** below, after all of the result-sets for all of the elements of
76895     ** the compound have been resolved.
76896     */
76897     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
76898       return WRC_Abort;
76899     }
76900     if( db->mallocFailed ){
76901       return WRC_Abort;
76902     }
76903 
76904     /* Resolve the GROUP BY clause.  At the same time, make sure
76905     ** the GROUP BY clause does not contain aggregate functions.
76906     */
76907     if( pGroupBy ){
76908       struct ExprList_item *pItem;
76909 
76910       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
76911         return WRC_Abort;
76912       }
76913       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
76914         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
76915           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
76916               "the GROUP BY clause");
76917           return WRC_Abort;
76918         }
76919       }
76920     }
76921 
76922     /* Advance to the next term of the compound
76923     */
76924     p = p->pPrior;
76925     nCompound++;
76926   }
76927 
76928   /* Resolve the ORDER BY on a compound SELECT after all terms of
76929   ** the compound have been resolved.
76930   */
76931   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
76932     return WRC_Abort;
76933   }
76934 
76935   return WRC_Prune;
76936 }
76937 
76938 /*
76939 ** This routine walks an expression tree and resolves references to
76940 ** table columns and result-set columns.  At the same time, do error
76941 ** checking on function usage and set a flag if any aggregate functions
76942 ** are seen.
76943 **
76944 ** To resolve table columns references we look for nodes (or subtrees) of the
76945 ** form X.Y.Z or Y.Z or just Z where
76946 **
76947 **      X:   The name of a database.  Ex:  "main" or "temp" or
76948 **           the symbolic name assigned to an ATTACH-ed database.
76949 **
76950 **      Y:   The name of a table in a FROM clause.  Or in a trigger
76951 **           one of the special names "old" or "new".
76952 **
76953 **      Z:   The name of a column in table Y.
76954 **
76955 ** The node at the root of the subtree is modified as follows:
76956 **
76957 **    Expr.op        Changed to TK_COLUMN
76958 **    Expr.pTab      Points to the Table object for X.Y
76959 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
76960 **    Expr.iTable    The VDBE cursor number for X.Y
76961 **
76962 **
76963 ** To resolve result-set references, look for expression nodes of the
76964 ** form Z (with no X and Y prefix) where the Z matches the right-hand
76965 ** size of an AS clause in the result-set of a SELECT.  The Z expression
76966 ** is replaced by a copy of the left-hand side of the result-set expression.
76967 ** Table-name and function resolution occurs on the substituted expression
76968 ** tree.  For example, in:
76969 **
76970 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
76971 **
76972 ** The "x" term of the order by is replaced by "a+b" to render:
76973 **
76974 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
76975 **
76976 ** Function calls are checked to make sure that the function is
76977 ** defined and that the correct number of arguments are specified.
76978 ** If the function is an aggregate function, then the NC_HasAgg flag is
76979 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
76980 ** If an expression contains aggregate functions then the EP_Agg
76981 ** property on the expression is set.
76982 **
76983 ** An error message is left in pParse if anything is amiss.  The number
76984 ** if errors is returned.
76985 */
76986 SQLITE_PRIVATE int sqlite3ResolveExprNames(
76987   NameContext *pNC,       /* Namespace to resolve expressions in. */
76988   Expr *pExpr             /* The expression to be analyzed. */
76989 ){
76990   u8 savedHasAgg;
76991   Walker w;
76992 
76993   if( pExpr==0 ) return 0;
76994 #if SQLITE_MAX_EXPR_DEPTH>0
76995   {
76996     Parse *pParse = pNC->pParse;
76997     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
76998       return 1;
76999     }
77000     pParse->nHeight += pExpr->nHeight;
77001   }
77002 #endif
77003   savedHasAgg = pNC->ncFlags & NC_HasAgg;
77004   pNC->ncFlags &= ~NC_HasAgg;
77005   memset(&w, 0, sizeof(w));
77006   w.xExprCallback = resolveExprStep;
77007   w.xSelectCallback = resolveSelectStep;
77008   w.pParse = pNC->pParse;
77009   w.u.pNC = pNC;
77010   sqlite3WalkExpr(&w, pExpr);
77011 #if SQLITE_MAX_EXPR_DEPTH>0
77012   pNC->pParse->nHeight -= pExpr->nHeight;
77013 #endif
77014   if( pNC->nErr>0 || w.pParse->nErr>0 ){
77015     ExprSetProperty(pExpr, EP_Error);
77016   }
77017   if( pNC->ncFlags & NC_HasAgg ){
77018     ExprSetProperty(pExpr, EP_Agg);
77019   }else if( savedHasAgg ){
77020     pNC->ncFlags |= NC_HasAgg;
77021   }
77022   return ExprHasProperty(pExpr, EP_Error);
77023 }
77024 
77025 
77026 /*
77027 ** Resolve all names in all expressions of a SELECT and in all
77028 ** decendents of the SELECT, including compounds off of p->pPrior,
77029 ** subqueries in expressions, and subqueries used as FROM clause
77030 ** terms.
77031 **
77032 ** See sqlite3ResolveExprNames() for a description of the kinds of
77033 ** transformations that occur.
77034 **
77035 ** All SELECT statements should have been expanded using
77036 ** sqlite3SelectExpand() prior to invoking this routine.
77037 */
77038 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
77039   Parse *pParse,         /* The parser context */
77040   Select *p,             /* The SELECT statement being coded. */
77041   NameContext *pOuterNC  /* Name context for parent SELECT statement */
77042 ){
77043   Walker w;
77044 
77045   assert( p!=0 );
77046   memset(&w, 0, sizeof(w));
77047   w.xExprCallback = resolveExprStep;
77048   w.xSelectCallback = resolveSelectStep;
77049   w.pParse = pParse;
77050   w.u.pNC = pOuterNC;
77051   sqlite3WalkSelect(&w, p);
77052 }
77053 
77054 /*
77055 ** Resolve names in expressions that can only reference a single table:
77056 **
77057 **    *   CHECK constraints
77058 **    *   WHERE clauses on partial indices
77059 **
77060 ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
77061 ** is set to -1 and the Expr.iColumn value is set to the column number.
77062 **
77063 ** Any errors cause an error message to be set in pParse.
77064 */
77065 SQLITE_PRIVATE void sqlite3ResolveSelfReference(
77066   Parse *pParse,      /* Parsing context */
77067   Table *pTab,        /* The table being referenced */
77068   int type,           /* NC_IsCheck or NC_PartIdx */
77069   Expr *pExpr,        /* Expression to resolve.  May be NULL. */
77070   ExprList *pList     /* Expression list to resolve.  May be NUL. */
77071 ){
77072   SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
77073   NameContext sNC;                /* Name context for pParse->pNewTable */
77074   int i;                          /* Loop counter */
77075 
77076   assert( type==NC_IsCheck || type==NC_PartIdx );
77077   memset(&sNC, 0, sizeof(sNC));
77078   memset(&sSrc, 0, sizeof(sSrc));
77079   sSrc.nSrc = 1;
77080   sSrc.a[0].zName = pTab->zName;
77081   sSrc.a[0].pTab = pTab;
77082   sSrc.a[0].iCursor = -1;
77083   sNC.pParse = pParse;
77084   sNC.pSrcList = &sSrc;
77085   sNC.ncFlags = type;
77086   if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
77087   if( pList ){
77088     for(i=0; i<pList->nExpr; i++){
77089       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
77090         return;
77091       }
77092     }
77093   }
77094 }
77095 
77096 /************** End of resolve.c *********************************************/
77097 /************** Begin file expr.c ********************************************/
77098 /*
77099 ** 2001 September 15
77100 **
77101 ** The author disclaims copyright to this source code.  In place of
77102 ** a legal notice, here is a blessing:
77103 **
77104 **    May you do good and not evil.
77105 **    May you find forgiveness for yourself and forgive others.
77106 **    May you share freely, never taking more than you give.
77107 **
77108 *************************************************************************
77109 ** This file contains routines used for analyzing expressions and
77110 ** for generating VDBE code that evaluates expressions in SQLite.
77111 */
77112 
77113 /*
77114 ** Return the 'affinity' of the expression pExpr if any.
77115 **
77116 ** If pExpr is a column, a reference to a column via an 'AS' alias,
77117 ** or a sub-select with a column as the return value, then the
77118 ** affinity of that column is returned. Otherwise, 0x00 is returned,
77119 ** indicating no affinity for the expression.
77120 **
77121 ** i.e. the WHERE clause expresssions in the following statements all
77122 ** have an affinity:
77123 **
77124 ** CREATE TABLE t1(a);
77125 ** SELECT * FROM t1 WHERE a;
77126 ** SELECT a AS b FROM t1 WHERE b;
77127 ** SELECT * FROM t1 WHERE (select a from t1);
77128 */
77129 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
77130   int op;
77131   pExpr = sqlite3ExprSkipCollate(pExpr);
77132   op = pExpr->op;
77133   if( op==TK_SELECT ){
77134     assert( pExpr->flags&EP_xIsSelect );
77135     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
77136   }
77137 #ifndef SQLITE_OMIT_CAST
77138   if( op==TK_CAST ){
77139     assert( !ExprHasProperty(pExpr, EP_IntValue) );
77140     return sqlite3AffinityType(pExpr->u.zToken, 0);
77141   }
77142 #endif
77143   if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
77144    && pExpr->pTab!=0
77145   ){
77146     /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
77147     ** a TK_COLUMN but was previously evaluated and cached in a register */
77148     int j = pExpr->iColumn;
77149     if( j<0 ) return SQLITE_AFF_INTEGER;
77150     assert( pExpr->pTab && j<pExpr->pTab->nCol );
77151     return pExpr->pTab->aCol[j].affinity;
77152   }
77153   return pExpr->affinity;
77154 }
77155 
77156 /*
77157 ** Set the collating sequence for expression pExpr to be the collating
77158 ** sequence named by pToken.   Return a pointer to a new Expr node that
77159 ** implements the COLLATE operator.
77160 **
77161 ** If a memory allocation error occurs, that fact is recorded in pParse->db
77162 ** and the pExpr parameter is returned unchanged.
77163 */
77164 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
77165   if( pCollName->n>0 ){
77166     Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
77167     if( pNew ){
77168       pNew->pLeft = pExpr;
77169       pNew->flags |= EP_Collate|EP_Skip;
77170       pExpr = pNew;
77171     }
77172   }
77173   return pExpr;
77174 }
77175 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
77176   Token s;
77177   assert( zC!=0 );
77178   s.z = zC;
77179   s.n = sqlite3Strlen30(s.z);
77180   return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
77181 }
77182 
77183 /*
77184 ** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
77185 ** or likelihood() function at the root of an expression.
77186 */
77187 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
77188   while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
77189     if( ExprHasProperty(pExpr, EP_Unlikely) ){
77190       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
77191       assert( pExpr->x.pList->nExpr>0 );
77192       assert( pExpr->op==TK_FUNCTION );
77193       pExpr = pExpr->x.pList->a[0].pExpr;
77194     }else{
77195       assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS );
77196       pExpr = pExpr->pLeft;
77197     }
77198   }
77199   return pExpr;
77200 }
77201 
77202 /*
77203 ** Return the collation sequence for the expression pExpr. If
77204 ** there is no defined collating sequence, return NULL.
77205 **
77206 ** The collating sequence might be determined by a COLLATE operator
77207 ** or by the presence of a column with a defined collating sequence.
77208 ** COLLATE operators take first precedence.  Left operands take
77209 ** precedence over right operands.
77210 */
77211 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
77212   sqlite3 *db = pParse->db;
77213   CollSeq *pColl = 0;
77214   Expr *p = pExpr;
77215   while( p ){
77216     int op = p->op;
77217     if( op==TK_CAST || op==TK_UPLUS ){
77218       p = p->pLeft;
77219       continue;
77220     }
77221     if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
77222       pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
77223       break;
77224     }
77225     if( p->pTab!=0
77226      && (op==TK_AGG_COLUMN || op==TK_COLUMN
77227           || op==TK_REGISTER || op==TK_TRIGGER)
77228     ){
77229       /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
77230       ** a TK_COLUMN but was previously evaluated and cached in a register */
77231       int j = p->iColumn;
77232       if( j>=0 ){
77233         const char *zColl = p->pTab->aCol[j].zColl;
77234         pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
77235       }
77236       break;
77237     }
77238     if( p->flags & EP_Collate ){
77239       if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
77240         p = p->pLeft;
77241       }else{
77242         p = p->pRight;
77243       }
77244     }else{
77245       break;
77246     }
77247   }
77248   if( sqlite3CheckCollSeq(pParse, pColl) ){
77249     pColl = 0;
77250   }
77251   return pColl;
77252 }
77253 
77254 /*
77255 ** pExpr is an operand of a comparison operator.  aff2 is the
77256 ** type affinity of the other operand.  This routine returns the
77257 ** type affinity that should be used for the comparison operator.
77258 */
77259 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
77260   char aff1 = sqlite3ExprAffinity(pExpr);
77261   if( aff1 && aff2 ){
77262     /* Both sides of the comparison are columns. If one has numeric
77263     ** affinity, use that. Otherwise use no affinity.
77264     */
77265     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
77266       return SQLITE_AFF_NUMERIC;
77267     }else{
77268       return SQLITE_AFF_NONE;
77269     }
77270   }else if( !aff1 && !aff2 ){
77271     /* Neither side of the comparison is a column.  Compare the
77272     ** results directly.
77273     */
77274     return SQLITE_AFF_NONE;
77275   }else{
77276     /* One side is a column, the other is not. Use the columns affinity. */
77277     assert( aff1==0 || aff2==0 );
77278     return (aff1 + aff2);
77279   }
77280 }
77281 
77282 /*
77283 ** pExpr is a comparison operator.  Return the type affinity that should
77284 ** be applied to both operands prior to doing the comparison.
77285 */
77286 static char comparisonAffinity(Expr *pExpr){
77287   char aff;
77288   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
77289           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
77290           pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
77291   assert( pExpr->pLeft );
77292   aff = sqlite3ExprAffinity(pExpr->pLeft);
77293   if( pExpr->pRight ){
77294     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
77295   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
77296     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
77297   }else if( !aff ){
77298     aff = SQLITE_AFF_NONE;
77299   }
77300   return aff;
77301 }
77302 
77303 /*
77304 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
77305 ** idx_affinity is the affinity of an indexed column. Return true
77306 ** if the index with affinity idx_affinity may be used to implement
77307 ** the comparison in pExpr.
77308 */
77309 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
77310   char aff = comparisonAffinity(pExpr);
77311   switch( aff ){
77312     case SQLITE_AFF_NONE:
77313       return 1;
77314     case SQLITE_AFF_TEXT:
77315       return idx_affinity==SQLITE_AFF_TEXT;
77316     default:
77317       return sqlite3IsNumericAffinity(idx_affinity);
77318   }
77319 }
77320 
77321 /*
77322 ** Return the P5 value that should be used for a binary comparison
77323 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
77324 */
77325 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
77326   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
77327   aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
77328   return aff;
77329 }
77330 
77331 /*
77332 ** Return a pointer to the collation sequence that should be used by
77333 ** a binary comparison operator comparing pLeft and pRight.
77334 **
77335 ** If the left hand expression has a collating sequence type, then it is
77336 ** used. Otherwise the collation sequence for the right hand expression
77337 ** is used, or the default (BINARY) if neither expression has a collating
77338 ** type.
77339 **
77340 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
77341 ** it is not considered.
77342 */
77343 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
77344   Parse *pParse,
77345   Expr *pLeft,
77346   Expr *pRight
77347 ){
77348   CollSeq *pColl;
77349   assert( pLeft );
77350   if( pLeft->flags & EP_Collate ){
77351     pColl = sqlite3ExprCollSeq(pParse, pLeft);
77352   }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
77353     pColl = sqlite3ExprCollSeq(pParse, pRight);
77354   }else{
77355     pColl = sqlite3ExprCollSeq(pParse, pLeft);
77356     if( !pColl ){
77357       pColl = sqlite3ExprCollSeq(pParse, pRight);
77358     }
77359   }
77360   return pColl;
77361 }
77362 
77363 /*
77364 ** Generate code for a comparison operator.
77365 */
77366 static int codeCompare(
77367   Parse *pParse,    /* The parsing (and code generating) context */
77368   Expr *pLeft,      /* The left operand */
77369   Expr *pRight,     /* The right operand */
77370   int opcode,       /* The comparison opcode */
77371   int in1, int in2, /* Register holding operands */
77372   int dest,         /* Jump here if true.  */
77373   int jumpIfNull    /* If true, jump if either operand is NULL */
77374 ){
77375   int p5;
77376   int addr;
77377   CollSeq *p4;
77378 
77379   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
77380   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
77381   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
77382                            (void*)p4, P4_COLLSEQ);
77383   sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
77384   return addr;
77385 }
77386 
77387 #if SQLITE_MAX_EXPR_DEPTH>0
77388 /*
77389 ** Check that argument nHeight is less than or equal to the maximum
77390 ** expression depth allowed. If it is not, leave an error message in
77391 ** pParse.
77392 */
77393 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
77394   int rc = SQLITE_OK;
77395   int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
77396   if( nHeight>mxHeight ){
77397     sqlite3ErrorMsg(pParse,
77398        "Expression tree is too large (maximum depth %d)", mxHeight
77399     );
77400     rc = SQLITE_ERROR;
77401   }
77402   return rc;
77403 }
77404 
77405 /* The following three functions, heightOfExpr(), heightOfExprList()
77406 ** and heightOfSelect(), are used to determine the maximum height
77407 ** of any expression tree referenced by the structure passed as the
77408 ** first argument.
77409 **
77410 ** If this maximum height is greater than the current value pointed
77411 ** to by pnHeight, the second parameter, then set *pnHeight to that
77412 ** value.
77413 */
77414 static void heightOfExpr(Expr *p, int *pnHeight){
77415   if( p ){
77416     if( p->nHeight>*pnHeight ){
77417       *pnHeight = p->nHeight;
77418     }
77419   }
77420 }
77421 static void heightOfExprList(ExprList *p, int *pnHeight){
77422   if( p ){
77423     int i;
77424     for(i=0; i<p->nExpr; i++){
77425       heightOfExpr(p->a[i].pExpr, pnHeight);
77426     }
77427   }
77428 }
77429 static void heightOfSelect(Select *p, int *pnHeight){
77430   if( p ){
77431     heightOfExpr(p->pWhere, pnHeight);
77432     heightOfExpr(p->pHaving, pnHeight);
77433     heightOfExpr(p->pLimit, pnHeight);
77434     heightOfExpr(p->pOffset, pnHeight);
77435     heightOfExprList(p->pEList, pnHeight);
77436     heightOfExprList(p->pGroupBy, pnHeight);
77437     heightOfExprList(p->pOrderBy, pnHeight);
77438     heightOfSelect(p->pPrior, pnHeight);
77439   }
77440 }
77441 
77442 /*
77443 ** Set the Expr.nHeight variable in the structure passed as an
77444 ** argument. An expression with no children, Expr.pList or
77445 ** Expr.pSelect member has a height of 1. Any other expression
77446 ** has a height equal to the maximum height of any other
77447 ** referenced Expr plus one.
77448 */
77449 static void exprSetHeight(Expr *p){
77450   int nHeight = 0;
77451   heightOfExpr(p->pLeft, &nHeight);
77452   heightOfExpr(p->pRight, &nHeight);
77453   if( ExprHasProperty(p, EP_xIsSelect) ){
77454     heightOfSelect(p->x.pSelect, &nHeight);
77455   }else{
77456     heightOfExprList(p->x.pList, &nHeight);
77457   }
77458   p->nHeight = nHeight + 1;
77459 }
77460 
77461 /*
77462 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
77463 ** the height is greater than the maximum allowed expression depth,
77464 ** leave an error in pParse.
77465 */
77466 SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
77467   exprSetHeight(p);
77468   sqlite3ExprCheckHeight(pParse, p->nHeight);
77469 }
77470 
77471 /*
77472 ** Return the maximum height of any expression tree referenced
77473 ** by the select statement passed as an argument.
77474 */
77475 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
77476   int nHeight = 0;
77477   heightOfSelect(p, &nHeight);
77478   return nHeight;
77479 }
77480 #else
77481   #define exprSetHeight(y)
77482 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
77483 
77484 /*
77485 ** This routine is the core allocator for Expr nodes.
77486 **
77487 ** Construct a new expression node and return a pointer to it.  Memory
77488 ** for this node and for the pToken argument is a single allocation
77489 ** obtained from sqlite3DbMalloc().  The calling function
77490 ** is responsible for making sure the node eventually gets freed.
77491 **
77492 ** If dequote is true, then the token (if it exists) is dequoted.
77493 ** If dequote is false, no dequoting is performance.  The deQuote
77494 ** parameter is ignored if pToken is NULL or if the token does not
77495 ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
77496 ** then the EP_DblQuoted flag is set on the expression node.
77497 **
77498 ** Special case:  If op==TK_INTEGER and pToken points to a string that
77499 ** can be translated into a 32-bit integer, then the token is not
77500 ** stored in u.zToken.  Instead, the integer values is written
77501 ** into u.iValue and the EP_IntValue flag is set.  No extra storage
77502 ** is allocated to hold the integer text and the dequote flag is ignored.
77503 */
77504 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
77505   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
77506   int op,                 /* Expression opcode */
77507   const Token *pToken,    /* Token argument.  Might be NULL */
77508   int dequote             /* True to dequote */
77509 ){
77510   Expr *pNew;
77511   int nExtra = 0;
77512   int iValue = 0;
77513 
77514   if( pToken ){
77515     if( op!=TK_INTEGER || pToken->z==0
77516           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
77517       nExtra = pToken->n+1;
77518       assert( iValue>=0 );
77519     }
77520   }
77521   pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
77522   if( pNew ){
77523     pNew->op = (u8)op;
77524     pNew->iAgg = -1;
77525     if( pToken ){
77526       if( nExtra==0 ){
77527         pNew->flags |= EP_IntValue;
77528         pNew->u.iValue = iValue;
77529       }else{
77530         int c;
77531         pNew->u.zToken = (char*)&pNew[1];
77532         assert( pToken->z!=0 || pToken->n==0 );
77533         if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
77534         pNew->u.zToken[pToken->n] = 0;
77535         if( dequote && nExtra>=3
77536              && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
77537           sqlite3Dequote(pNew->u.zToken);
77538           if( c=='"' ) pNew->flags |= EP_DblQuoted;
77539         }
77540       }
77541     }
77542 #if SQLITE_MAX_EXPR_DEPTH>0
77543     pNew->nHeight = 1;
77544 #endif
77545   }
77546   return pNew;
77547 }
77548 
77549 /*
77550 ** Allocate a new expression node from a zero-terminated token that has
77551 ** already been dequoted.
77552 */
77553 SQLITE_PRIVATE Expr *sqlite3Expr(
77554   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
77555   int op,                 /* Expression opcode */
77556   const char *zToken      /* Token argument.  Might be NULL */
77557 ){
77558   Token x;
77559   x.z = zToken;
77560   x.n = zToken ? sqlite3Strlen30(zToken) : 0;
77561   return sqlite3ExprAlloc(db, op, &x, 0);
77562 }
77563 
77564 /*
77565 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
77566 **
77567 ** If pRoot==NULL that means that a memory allocation error has occurred.
77568 ** In that case, delete the subtrees pLeft and pRight.
77569 */
77570 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
77571   sqlite3 *db,
77572   Expr *pRoot,
77573   Expr *pLeft,
77574   Expr *pRight
77575 ){
77576   if( pRoot==0 ){
77577     assert( db->mallocFailed );
77578     sqlite3ExprDelete(db, pLeft);
77579     sqlite3ExprDelete(db, pRight);
77580   }else{
77581     if( pRight ){
77582       pRoot->pRight = pRight;
77583       pRoot->flags |= EP_Collate & pRight->flags;
77584     }
77585     if( pLeft ){
77586       pRoot->pLeft = pLeft;
77587       pRoot->flags |= EP_Collate & pLeft->flags;
77588     }
77589     exprSetHeight(pRoot);
77590   }
77591 }
77592 
77593 /*
77594 ** Allocate a Expr node which joins as many as two subtrees.
77595 **
77596 ** One or both of the subtrees can be NULL.  Return a pointer to the new
77597 ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
77598 ** free the subtrees and return NULL.
77599 */
77600 SQLITE_PRIVATE Expr *sqlite3PExpr(
77601   Parse *pParse,          /* Parsing context */
77602   int op,                 /* Expression opcode */
77603   Expr *pLeft,            /* Left operand */
77604   Expr *pRight,           /* Right operand */
77605   const Token *pToken     /* Argument token */
77606 ){
77607   Expr *p;
77608   if( op==TK_AND && pLeft && pRight ){
77609     /* Take advantage of short-circuit false optimization for AND */
77610     p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
77611   }else{
77612     p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
77613     sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
77614   }
77615   if( p ) {
77616     sqlite3ExprCheckHeight(pParse, p->nHeight);
77617   }
77618   return p;
77619 }
77620 
77621 /*
77622 ** If the expression is always either TRUE or FALSE (respectively),
77623 ** then return 1.  If one cannot determine the truth value of the
77624 ** expression at compile-time return 0.
77625 **
77626 ** This is an optimization.  If is OK to return 0 here even if
77627 ** the expression really is always false or false (a false negative).
77628 ** But it is a bug to return 1 if the expression might have different
77629 ** boolean values in different circumstances (a false positive.)
77630 **
77631 ** Note that if the expression is part of conditional for a
77632 ** LEFT JOIN, then we cannot determine at compile-time whether or not
77633 ** is it true or false, so always return 0.
77634 */
77635 static int exprAlwaysTrue(Expr *p){
77636   int v = 0;
77637   if( ExprHasProperty(p, EP_FromJoin) ) return 0;
77638   if( !sqlite3ExprIsInteger(p, &v) ) return 0;
77639   return v!=0;
77640 }
77641 static int exprAlwaysFalse(Expr *p){
77642   int v = 0;
77643   if( ExprHasProperty(p, EP_FromJoin) ) return 0;
77644   if( !sqlite3ExprIsInteger(p, &v) ) return 0;
77645   return v==0;
77646 }
77647 
77648 /*
77649 ** Join two expressions using an AND operator.  If either expression is
77650 ** NULL, then just return the other expression.
77651 **
77652 ** If one side or the other of the AND is known to be false, then instead
77653 ** of returning an AND expression, just return a constant expression with
77654 ** a value of false.
77655 */
77656 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
77657   if( pLeft==0 ){
77658     return pRight;
77659   }else if( pRight==0 ){
77660     return pLeft;
77661   }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
77662     sqlite3ExprDelete(db, pLeft);
77663     sqlite3ExprDelete(db, pRight);
77664     return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
77665   }else{
77666     Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
77667     sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
77668     return pNew;
77669   }
77670 }
77671 
77672 /*
77673 ** Construct a new expression node for a function with multiple
77674 ** arguments.
77675 */
77676 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
77677   Expr *pNew;
77678   sqlite3 *db = pParse->db;
77679   assert( pToken );
77680   pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
77681   if( pNew==0 ){
77682     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
77683     return 0;
77684   }
77685   pNew->x.pList = pList;
77686   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
77687   sqlite3ExprSetHeight(pParse, pNew);
77688   return pNew;
77689 }
77690 
77691 /*
77692 ** Assign a variable number to an expression that encodes a wildcard
77693 ** in the original SQL statement.
77694 **
77695 ** Wildcards consisting of a single "?" are assigned the next sequential
77696 ** variable number.
77697 **
77698 ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
77699 ** sure "nnn" is not too be to avoid a denial of service attack when
77700 ** the SQL statement comes from an external source.
77701 **
77702 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
77703 ** as the previous instance of the same wildcard.  Or if this is the first
77704 ** instance of the wildcard, the next sequenial variable number is
77705 ** assigned.
77706 */
77707 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
77708   sqlite3 *db = pParse->db;
77709   const char *z;
77710 
77711   if( pExpr==0 ) return;
77712   assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
77713   z = pExpr->u.zToken;
77714   assert( z!=0 );
77715   assert( z[0]!=0 );
77716   if( z[1]==0 ){
77717     /* Wildcard of the form "?".  Assign the next variable number */
77718     assert( z[0]=='?' );
77719     pExpr->iColumn = (ynVar)(++pParse->nVar);
77720   }else{
77721     ynVar x = 0;
77722     u32 n = sqlite3Strlen30(z);
77723     if( z[0]=='?' ){
77724       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
77725       ** use it as the variable number */
77726       i64 i;
77727       int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
77728       pExpr->iColumn = x = (ynVar)i;
77729       testcase( i==0 );
77730       testcase( i==1 );
77731       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
77732       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
77733       if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
77734         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
77735             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
77736         x = 0;
77737       }
77738       if( i>pParse->nVar ){
77739         pParse->nVar = (int)i;
77740       }
77741     }else{
77742       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
77743       ** number as the prior appearance of the same name, or if the name
77744       ** has never appeared before, reuse the same variable number
77745       */
77746       ynVar i;
77747       for(i=0; i<pParse->nzVar; i++){
77748         if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
77749           pExpr->iColumn = x = (ynVar)i+1;
77750           break;
77751         }
77752       }
77753       if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
77754     }
77755     if( x>0 ){
77756       if( x>pParse->nzVar ){
77757         char **a;
77758         a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
77759         if( a==0 ) return;  /* Error reported through db->mallocFailed */
77760         pParse->azVar = a;
77761         memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
77762         pParse->nzVar = x;
77763       }
77764       if( z[0]!='?' || pParse->azVar[x-1]==0 ){
77765         sqlite3DbFree(db, pParse->azVar[x-1]);
77766         pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
77767       }
77768     }
77769   }
77770   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
77771     sqlite3ErrorMsg(pParse, "too many SQL variables");
77772   }
77773 }
77774 
77775 /*
77776 ** Recursively delete an expression tree.
77777 */
77778 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
77779   if( p==0 ) return;
77780   /* Sanity check: Assert that the IntValue is non-negative if it exists */
77781   assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
77782   if( !ExprHasProperty(p, EP_TokenOnly) ){
77783     /* The Expr.x union is never used at the same time as Expr.pRight */
77784     assert( p->x.pList==0 || p->pRight==0 );
77785     sqlite3ExprDelete(db, p->pLeft);
77786     sqlite3ExprDelete(db, p->pRight);
77787     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
77788     if( ExprHasProperty(p, EP_xIsSelect) ){
77789       sqlite3SelectDelete(db, p->x.pSelect);
77790     }else{
77791       sqlite3ExprListDelete(db, p->x.pList);
77792     }
77793   }
77794   if( !ExprHasProperty(p, EP_Static) ){
77795     sqlite3DbFree(db, p);
77796   }
77797 }
77798 
77799 /*
77800 ** Return the number of bytes allocated for the expression structure
77801 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
77802 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
77803 */
77804 static int exprStructSize(Expr *p){
77805   if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
77806   if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
77807   return EXPR_FULLSIZE;
77808 }
77809 
77810 /*
77811 ** The dupedExpr*Size() routines each return the number of bytes required
77812 ** to store a copy of an expression or expression tree.  They differ in
77813 ** how much of the tree is measured.
77814 **
77815 **     dupedExprStructSize()     Size of only the Expr structure
77816 **     dupedExprNodeSize()       Size of Expr + space for token
77817 **     dupedExprSize()           Expr + token + subtree components
77818 **
77819 ***************************************************************************
77820 **
77821 ** The dupedExprStructSize() function returns two values OR-ed together:
77822 ** (1) the space required for a copy of the Expr structure only and
77823 ** (2) the EP_xxx flags that indicate what the structure size should be.
77824 ** The return values is always one of:
77825 **
77826 **      EXPR_FULLSIZE
77827 **      EXPR_REDUCEDSIZE   | EP_Reduced
77828 **      EXPR_TOKENONLYSIZE | EP_TokenOnly
77829 **
77830 ** The size of the structure can be found by masking the return value
77831 ** of this routine with 0xfff.  The flags can be found by masking the
77832 ** return value with EP_Reduced|EP_TokenOnly.
77833 **
77834 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
77835 ** (unreduced) Expr objects as they or originally constructed by the parser.
77836 ** During expression analysis, extra information is computed and moved into
77837 ** later parts of teh Expr object and that extra information might get chopped
77838 ** off if the expression is reduced.  Note also that it does not work to
77839 ** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
77840 ** to reduce a pristine expression tree from the parser.  The implementation
77841 ** of dupedExprStructSize() contain multiple assert() statements that attempt
77842 ** to enforce this constraint.
77843 */
77844 static int dupedExprStructSize(Expr *p, int flags){
77845   int nSize;
77846   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
77847   assert( EXPR_FULLSIZE<=0xfff );
77848   assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
77849   if( 0==(flags&EXPRDUP_REDUCE) ){
77850     nSize = EXPR_FULLSIZE;
77851   }else{
77852     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
77853     assert( !ExprHasProperty(p, EP_FromJoin) );
77854     assert( !ExprHasProperty(p, EP_MemToken) );
77855     assert( !ExprHasProperty(p, EP_NoReduce) );
77856     if( p->pLeft || p->x.pList ){
77857       nSize = EXPR_REDUCEDSIZE | EP_Reduced;
77858     }else{
77859       assert( p->pRight==0 );
77860       nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
77861     }
77862   }
77863   return nSize;
77864 }
77865 
77866 /*
77867 ** This function returns the space in bytes required to store the copy
77868 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
77869 ** string is defined.)
77870 */
77871 static int dupedExprNodeSize(Expr *p, int flags){
77872   int nByte = dupedExprStructSize(p, flags) & 0xfff;
77873   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
77874     nByte += sqlite3Strlen30(p->u.zToken)+1;
77875   }
77876   return ROUND8(nByte);
77877 }
77878 
77879 /*
77880 ** Return the number of bytes required to create a duplicate of the
77881 ** expression passed as the first argument. The second argument is a
77882 ** mask containing EXPRDUP_XXX flags.
77883 **
77884 ** The value returned includes space to create a copy of the Expr struct
77885 ** itself and the buffer referred to by Expr.u.zToken, if any.
77886 **
77887 ** If the EXPRDUP_REDUCE flag is set, then the return value includes
77888 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
77889 ** and Expr.pRight variables (but not for any structures pointed to or
77890 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
77891 */
77892 static int dupedExprSize(Expr *p, int flags){
77893   int nByte = 0;
77894   if( p ){
77895     nByte = dupedExprNodeSize(p, flags);
77896     if( flags&EXPRDUP_REDUCE ){
77897       nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
77898     }
77899   }
77900   return nByte;
77901 }
77902 
77903 /*
77904 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer
77905 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
77906 ** to store the copy of expression p, the copies of p->u.zToken
77907 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
77908 ** if any. Before returning, *pzBuffer is set to the first byte passed the
77909 ** portion of the buffer copied into by this function.
77910 */
77911 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
77912   Expr *pNew = 0;                      /* Value to return */
77913   if( p ){
77914     const int isReduced = (flags&EXPRDUP_REDUCE);
77915     u8 *zAlloc;
77916     u32 staticFlag = 0;
77917 
77918     assert( pzBuffer==0 || isReduced );
77919 
77920     /* Figure out where to write the new Expr structure. */
77921     if( pzBuffer ){
77922       zAlloc = *pzBuffer;
77923       staticFlag = EP_Static;
77924     }else{
77925       zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
77926     }
77927     pNew = (Expr *)zAlloc;
77928 
77929     if( pNew ){
77930       /* Set nNewSize to the size allocated for the structure pointed to
77931       ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
77932       ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
77933       ** by the copy of the p->u.zToken string (if any).
77934       */
77935       const unsigned nStructSize = dupedExprStructSize(p, flags);
77936       const int nNewSize = nStructSize & 0xfff;
77937       int nToken;
77938       if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
77939         nToken = sqlite3Strlen30(p->u.zToken) + 1;
77940       }else{
77941         nToken = 0;
77942       }
77943       if( isReduced ){
77944         assert( ExprHasProperty(p, EP_Reduced)==0 );
77945         memcpy(zAlloc, p, nNewSize);
77946       }else{
77947         int nSize = exprStructSize(p);
77948         memcpy(zAlloc, p, nSize);
77949         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
77950       }
77951 
77952       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
77953       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
77954       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
77955       pNew->flags |= staticFlag;
77956 
77957       /* Copy the p->u.zToken string, if any. */
77958       if( nToken ){
77959         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
77960         memcpy(zToken, p->u.zToken, nToken);
77961       }
77962 
77963       if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
77964         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
77965         if( ExprHasProperty(p, EP_xIsSelect) ){
77966           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
77967         }else{
77968           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
77969         }
77970       }
77971 
77972       /* Fill in pNew->pLeft and pNew->pRight. */
77973       if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
77974         zAlloc += dupedExprNodeSize(p, flags);
77975         if( ExprHasProperty(pNew, EP_Reduced) ){
77976           pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
77977           pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
77978         }
77979         if( pzBuffer ){
77980           *pzBuffer = zAlloc;
77981         }
77982       }else{
77983         if( !ExprHasProperty(p, EP_TokenOnly) ){
77984           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
77985           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
77986         }
77987       }
77988 
77989     }
77990   }
77991   return pNew;
77992 }
77993 
77994 /*
77995 ** Create and return a deep copy of the object passed as the second
77996 ** argument. If an OOM condition is encountered, NULL is returned
77997 ** and the db->mallocFailed flag set.
77998 */
77999 #ifndef SQLITE_OMIT_CTE
78000 static With *withDup(sqlite3 *db, With *p){
78001   With *pRet = 0;
78002   if( p ){
78003     int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
78004     pRet = sqlite3DbMallocZero(db, nByte);
78005     if( pRet ){
78006       int i;
78007       pRet->nCte = p->nCte;
78008       for(i=0; i<p->nCte; i++){
78009         pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
78010         pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
78011         pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
78012       }
78013     }
78014   }
78015   return pRet;
78016 }
78017 #else
78018 # define withDup(x,y) 0
78019 #endif
78020 
78021 /*
78022 ** The following group of routines make deep copies of expressions,
78023 ** expression lists, ID lists, and select statements.  The copies can
78024 ** be deleted (by being passed to their respective ...Delete() routines)
78025 ** without effecting the originals.
78026 **
78027 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
78028 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
78029 ** by subsequent calls to sqlite*ListAppend() routines.
78030 **
78031 ** Any tables that the SrcList might point to are not duplicated.
78032 **
78033 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
78034 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
78035 ** truncated version of the usual Expr structure that will be stored as
78036 ** part of the in-memory representation of the database schema.
78037 */
78038 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
78039   return exprDup(db, p, flags, 0);
78040 }
78041 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
78042   ExprList *pNew;
78043   struct ExprList_item *pItem, *pOldItem;
78044   int i;
78045   if( p==0 ) return 0;
78046   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
78047   if( pNew==0 ) return 0;
78048   pNew->iECursor = 0;
78049   pNew->nExpr = i = p->nExpr;
78050   if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
78051   pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
78052   if( pItem==0 ){
78053     sqlite3DbFree(db, pNew);
78054     return 0;
78055   }
78056   pOldItem = p->a;
78057   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
78058     Expr *pOldExpr = pOldItem->pExpr;
78059     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
78060     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
78061     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
78062     pItem->sortOrder = pOldItem->sortOrder;
78063     pItem->done = 0;
78064     pItem->bSpanIsTab = pOldItem->bSpanIsTab;
78065     pItem->u = pOldItem->u;
78066   }
78067   return pNew;
78068 }
78069 
78070 /*
78071 ** If cursors, triggers, views and subqueries are all omitted from
78072 ** the build, then none of the following routines, except for
78073 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
78074 ** called with a NULL argument.
78075 */
78076 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
78077  || !defined(SQLITE_OMIT_SUBQUERY)
78078 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
78079   SrcList *pNew;
78080   int i;
78081   int nByte;
78082   if( p==0 ) return 0;
78083   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
78084   pNew = sqlite3DbMallocRaw(db, nByte );
78085   if( pNew==0 ) return 0;
78086   pNew->nSrc = pNew->nAlloc = p->nSrc;
78087   for(i=0; i<p->nSrc; i++){
78088     struct SrcList_item *pNewItem = &pNew->a[i];
78089     struct SrcList_item *pOldItem = &p->a[i];
78090     Table *pTab;
78091     pNewItem->pSchema = pOldItem->pSchema;
78092     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
78093     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
78094     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
78095     pNewItem->jointype = pOldItem->jointype;
78096     pNewItem->iCursor = pOldItem->iCursor;
78097     pNewItem->addrFillSub = pOldItem->addrFillSub;
78098     pNewItem->regReturn = pOldItem->regReturn;
78099     pNewItem->isCorrelated = pOldItem->isCorrelated;
78100     pNewItem->viaCoroutine = pOldItem->viaCoroutine;
78101     pNewItem->isRecursive = pOldItem->isRecursive;
78102     pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
78103     pNewItem->notIndexed = pOldItem->notIndexed;
78104     pNewItem->pIndex = pOldItem->pIndex;
78105     pTab = pNewItem->pTab = pOldItem->pTab;
78106     if( pTab ){
78107       pTab->nRef++;
78108     }
78109     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
78110     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
78111     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
78112     pNewItem->colUsed = pOldItem->colUsed;
78113   }
78114   return pNew;
78115 }
78116 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
78117   IdList *pNew;
78118   int i;
78119   if( p==0 ) return 0;
78120   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
78121   if( pNew==0 ) return 0;
78122   pNew->nId = p->nId;
78123   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
78124   if( pNew->a==0 ){
78125     sqlite3DbFree(db, pNew);
78126     return 0;
78127   }
78128   /* Note that because the size of the allocation for p->a[] is not
78129   ** necessarily a power of two, sqlite3IdListAppend() may not be called
78130   ** on the duplicate created by this function. */
78131   for(i=0; i<p->nId; i++){
78132     struct IdList_item *pNewItem = &pNew->a[i];
78133     struct IdList_item *pOldItem = &p->a[i];
78134     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
78135     pNewItem->idx = pOldItem->idx;
78136   }
78137   return pNew;
78138 }
78139 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
78140   Select *pNew, *pPrior;
78141   if( p==0 ) return 0;
78142   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
78143   if( pNew==0 ) return 0;
78144   pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
78145   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
78146   pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
78147   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
78148   pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
78149   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
78150   pNew->op = p->op;
78151   pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
78152   if( pPrior ) pPrior->pNext = pNew;
78153   pNew->pNext = 0;
78154   pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
78155   pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
78156   pNew->iLimit = 0;
78157   pNew->iOffset = 0;
78158   pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
78159   pNew->addrOpenEphm[0] = -1;
78160   pNew->addrOpenEphm[1] = -1;
78161   pNew->addrOpenEphm[2] = -1;
78162   pNew->nSelectRow = p->nSelectRow;
78163   pNew->pWith = withDup(db, p->pWith);
78164   return pNew;
78165 }
78166 #else
78167 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
78168   assert( p==0 );
78169   return 0;
78170 }
78171 #endif
78172 
78173 
78174 /*
78175 ** Add a new element to the end of an expression list.  If pList is
78176 ** initially NULL, then create a new expression list.
78177 **
78178 ** If a memory allocation error occurs, the entire list is freed and
78179 ** NULL is returned.  If non-NULL is returned, then it is guaranteed
78180 ** that the new entry was successfully appended.
78181 */
78182 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
78183   Parse *pParse,          /* Parsing context */
78184   ExprList *pList,        /* List to which to append. Might be NULL */
78185   Expr *pExpr             /* Expression to be appended. Might be NULL */
78186 ){
78187   sqlite3 *db = pParse->db;
78188   if( pList==0 ){
78189     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
78190     if( pList==0 ){
78191       goto no_mem;
78192     }
78193     pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
78194     if( pList->a==0 ) goto no_mem;
78195   }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
78196     struct ExprList_item *a;
78197     assert( pList->nExpr>0 );
78198     a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
78199     if( a==0 ){
78200       goto no_mem;
78201     }
78202     pList->a = a;
78203   }
78204   assert( pList->a!=0 );
78205   if( 1 ){
78206     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
78207     memset(pItem, 0, sizeof(*pItem));
78208     pItem->pExpr = pExpr;
78209   }
78210   return pList;
78211 
78212 no_mem:
78213   /* Avoid leaking memory if malloc has failed. */
78214   sqlite3ExprDelete(db, pExpr);
78215   sqlite3ExprListDelete(db, pList);
78216   return 0;
78217 }
78218 
78219 /*
78220 ** Set the ExprList.a[].zName element of the most recently added item
78221 ** on the expression list.
78222 **
78223 ** pList might be NULL following an OOM error.  But pName should never be
78224 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
78225 ** is set.
78226 */
78227 SQLITE_PRIVATE void sqlite3ExprListSetName(
78228   Parse *pParse,          /* Parsing context */
78229   ExprList *pList,        /* List to which to add the span. */
78230   Token *pName,           /* Name to be added */
78231   int dequote             /* True to cause the name to be dequoted */
78232 ){
78233   assert( pList!=0 || pParse->db->mallocFailed!=0 );
78234   if( pList ){
78235     struct ExprList_item *pItem;
78236     assert( pList->nExpr>0 );
78237     pItem = &pList->a[pList->nExpr-1];
78238     assert( pItem->zName==0 );
78239     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
78240     if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
78241   }
78242 }
78243 
78244 /*
78245 ** Set the ExprList.a[].zSpan element of the most recently added item
78246 ** on the expression list.
78247 **
78248 ** pList might be NULL following an OOM error.  But pSpan should never be
78249 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
78250 ** is set.
78251 */
78252 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
78253   Parse *pParse,          /* Parsing context */
78254   ExprList *pList,        /* List to which to add the span. */
78255   ExprSpan *pSpan         /* The span to be added */
78256 ){
78257   sqlite3 *db = pParse->db;
78258   assert( pList!=0 || db->mallocFailed!=0 );
78259   if( pList ){
78260     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
78261     assert( pList->nExpr>0 );
78262     assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
78263     sqlite3DbFree(db, pItem->zSpan);
78264     pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
78265                                     (int)(pSpan->zEnd - pSpan->zStart));
78266   }
78267 }
78268 
78269 /*
78270 ** If the expression list pEList contains more than iLimit elements,
78271 ** leave an error message in pParse.
78272 */
78273 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
78274   Parse *pParse,
78275   ExprList *pEList,
78276   const char *zObject
78277 ){
78278   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
78279   testcase( pEList && pEList->nExpr==mx );
78280   testcase( pEList && pEList->nExpr==mx+1 );
78281   if( pEList && pEList->nExpr>mx ){
78282     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
78283   }
78284 }
78285 
78286 /*
78287 ** Delete an entire expression list.
78288 */
78289 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
78290   int i;
78291   struct ExprList_item *pItem;
78292   if( pList==0 ) return;
78293   assert( pList->a!=0 || pList->nExpr==0 );
78294   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
78295     sqlite3ExprDelete(db, pItem->pExpr);
78296     sqlite3DbFree(db, pItem->zName);
78297     sqlite3DbFree(db, pItem->zSpan);
78298   }
78299   sqlite3DbFree(db, pList->a);
78300   sqlite3DbFree(db, pList);
78301 }
78302 
78303 /*
78304 ** These routines are Walker callbacks.  Walker.u.pi is a pointer
78305 ** to an integer.  These routines are checking an expression to see
78306 ** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
78307 ** not constant.
78308 **
78309 ** These callback routines are used to implement the following:
78310 **
78311 **     sqlite3ExprIsConstant()
78312 **     sqlite3ExprIsConstantNotJoin()
78313 **     sqlite3ExprIsConstantOrFunction()
78314 **
78315 */
78316 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
78317 
78318   /* If pWalker->u.i is 3 then any term of the expression that comes from
78319   ** the ON or USING clauses of a join disqualifies the expression
78320   ** from being considered constant. */
78321   if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){
78322     pWalker->u.i = 0;
78323     return WRC_Abort;
78324   }
78325 
78326   switch( pExpr->op ){
78327     /* Consider functions to be constant if all their arguments are constant
78328     ** and either pWalker->u.i==2 or the function as the SQLITE_FUNC_CONST
78329     ** flag. */
78330     case TK_FUNCTION:
78331       if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){
78332         return WRC_Continue;
78333       }
78334       /* Fall through */
78335     case TK_ID:
78336     case TK_COLUMN:
78337     case TK_AGG_FUNCTION:
78338     case TK_AGG_COLUMN:
78339       testcase( pExpr->op==TK_ID );
78340       testcase( pExpr->op==TK_COLUMN );
78341       testcase( pExpr->op==TK_AGG_FUNCTION );
78342       testcase( pExpr->op==TK_AGG_COLUMN );
78343       pWalker->u.i = 0;
78344       return WRC_Abort;
78345     default:
78346       testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
78347       testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
78348       return WRC_Continue;
78349   }
78350 }
78351 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
78352   UNUSED_PARAMETER(NotUsed);
78353   pWalker->u.i = 0;
78354   return WRC_Abort;
78355 }
78356 static int exprIsConst(Expr *p, int initFlag){
78357   Walker w;
78358   memset(&w, 0, sizeof(w));
78359   w.u.i = initFlag;
78360   w.xExprCallback = exprNodeIsConstant;
78361   w.xSelectCallback = selectNodeIsConstant;
78362   sqlite3WalkExpr(&w, p);
78363   return w.u.i;
78364 }
78365 
78366 /*
78367 ** Walk an expression tree.  Return 1 if the expression is constant
78368 ** and 0 if it involves variables or function calls.
78369 **
78370 ** For the purposes of this function, a double-quoted string (ex: "abc")
78371 ** is considered a variable but a single-quoted string (ex: 'abc') is
78372 ** a constant.
78373 */
78374 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
78375   return exprIsConst(p, 1);
78376 }
78377 
78378 /*
78379 ** Walk an expression tree.  Return 1 if the expression is constant
78380 ** that does no originate from the ON or USING clauses of a join.
78381 ** Return 0 if it involves variables or function calls or terms from
78382 ** an ON or USING clause.
78383 */
78384 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
78385   return exprIsConst(p, 3);
78386 }
78387 
78388 /*
78389 ** Walk an expression tree.  Return 1 if the expression is constant
78390 ** or a function call with constant arguments.  Return and 0 if there
78391 ** are any variables.
78392 **
78393 ** For the purposes of this function, a double-quoted string (ex: "abc")
78394 ** is considered a variable but a single-quoted string (ex: 'abc') is
78395 ** a constant.
78396 */
78397 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
78398   return exprIsConst(p, 2);
78399 }
78400 
78401 /*
78402 ** If the expression p codes a constant integer that is small enough
78403 ** to fit in a 32-bit integer, return 1 and put the value of the integer
78404 ** in *pValue.  If the expression is not an integer or if it is too big
78405 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
78406 */
78407 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
78408   int rc = 0;
78409 
78410   /* If an expression is an integer literal that fits in a signed 32-bit
78411   ** integer, then the EP_IntValue flag will have already been set */
78412   assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
78413            || sqlite3GetInt32(p->u.zToken, &rc)==0 );
78414 
78415   if( p->flags & EP_IntValue ){
78416     *pValue = p->u.iValue;
78417     return 1;
78418   }
78419   switch( p->op ){
78420     case TK_UPLUS: {
78421       rc = sqlite3ExprIsInteger(p->pLeft, pValue);
78422       break;
78423     }
78424     case TK_UMINUS: {
78425       int v;
78426       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
78427         assert( v!=(-2147483647-1) );
78428         *pValue = -v;
78429         rc = 1;
78430       }
78431       break;
78432     }
78433     default: break;
78434   }
78435   return rc;
78436 }
78437 
78438 /*
78439 ** Return FALSE if there is no chance that the expression can be NULL.
78440 **
78441 ** If the expression might be NULL or if the expression is too complex
78442 ** to tell return TRUE.
78443 **
78444 ** This routine is used as an optimization, to skip OP_IsNull opcodes
78445 ** when we know that a value cannot be NULL.  Hence, a false positive
78446 ** (returning TRUE when in fact the expression can never be NULL) might
78447 ** be a small performance hit but is otherwise harmless.  On the other
78448 ** hand, a false negative (returning FALSE when the result could be NULL)
78449 ** will likely result in an incorrect answer.  So when in doubt, return
78450 ** TRUE.
78451 */
78452 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
78453   u8 op;
78454   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
78455   op = p->op;
78456   if( op==TK_REGISTER ) op = p->op2;
78457   switch( op ){
78458     case TK_INTEGER:
78459     case TK_STRING:
78460     case TK_FLOAT:
78461     case TK_BLOB:
78462       return 0;
78463     default:
78464       return 1;
78465   }
78466 }
78467 
78468 /*
78469 ** Return TRUE if the given expression is a constant which would be
78470 ** unchanged by OP_Affinity with the affinity given in the second
78471 ** argument.
78472 **
78473 ** This routine is used to determine if the OP_Affinity operation
78474 ** can be omitted.  When in doubt return FALSE.  A false negative
78475 ** is harmless.  A false positive, however, can result in the wrong
78476 ** answer.
78477 */
78478 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
78479   u8 op;
78480   if( aff==SQLITE_AFF_NONE ) return 1;
78481   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
78482   op = p->op;
78483   if( op==TK_REGISTER ) op = p->op2;
78484   switch( op ){
78485     case TK_INTEGER: {
78486       return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
78487     }
78488     case TK_FLOAT: {
78489       return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
78490     }
78491     case TK_STRING: {
78492       return aff==SQLITE_AFF_TEXT;
78493     }
78494     case TK_BLOB: {
78495       return 1;
78496     }
78497     case TK_COLUMN: {
78498       assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
78499       return p->iColumn<0
78500           && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
78501     }
78502     default: {
78503       return 0;
78504     }
78505   }
78506 }
78507 
78508 /*
78509 ** Return TRUE if the given string is a row-id column name.
78510 */
78511 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
78512   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
78513   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
78514   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
78515   return 0;
78516 }
78517 
78518 /*
78519 ** Return true if we are able to the IN operator optimization on a
78520 ** query of the form
78521 **
78522 **       x IN (SELECT ...)
78523 **
78524 ** Where the SELECT... clause is as specified by the parameter to this
78525 ** routine.
78526 **
78527 ** The Select object passed in has already been preprocessed and no
78528 ** errors have been found.
78529 */
78530 #ifndef SQLITE_OMIT_SUBQUERY
78531 static int isCandidateForInOpt(Select *p){
78532   SrcList *pSrc;
78533   ExprList *pEList;
78534   Table *pTab;
78535   if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
78536   if( p->pPrior ) return 0;              /* Not a compound SELECT */
78537   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
78538     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
78539     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
78540     return 0; /* No DISTINCT keyword and no aggregate functions */
78541   }
78542   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
78543   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
78544   assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
78545   if( p->pWhere ) return 0;              /* Has no WHERE clause */
78546   pSrc = p->pSrc;
78547   assert( pSrc!=0 );
78548   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
78549   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
78550   pTab = pSrc->a[0].pTab;
78551   if( NEVER(pTab==0) ) return 0;
78552   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
78553   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
78554   pEList = p->pEList;
78555   if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
78556   if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
78557   return 1;
78558 }
78559 #endif /* SQLITE_OMIT_SUBQUERY */
78560 
78561 /*
78562 ** Code an OP_Once instruction and allocate space for its flag. Return the
78563 ** address of the new instruction.
78564 */
78565 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
78566   Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
78567   return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
78568 }
78569 
78570 /*
78571 ** This function is used by the implementation of the IN (...) operator.
78572 ** The pX parameter is the expression on the RHS of the IN operator, which
78573 ** might be either a list of expressions or a subquery.
78574 **
78575 ** The job of this routine is to find or create a b-tree object that can
78576 ** be used either to test for membership in the RHS set or to iterate through
78577 ** all members of the RHS set, skipping duplicates.
78578 **
78579 ** A cursor is opened on the b-tree object that the RHS of the IN operator
78580 ** and pX->iTable is set to the index of that cursor.
78581 **
78582 ** The returned value of this function indicates the b-tree type, as follows:
78583 **
78584 **   IN_INDEX_ROWID      - The cursor was opened on a database table.
78585 **   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
78586 **   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
78587 **   IN_INDEX_EPH        - The cursor was opened on a specially created and
78588 **                         populated epheremal table.
78589 **
78590 ** An existing b-tree might be used if the RHS expression pX is a simple
78591 ** subquery such as:
78592 **
78593 **     SELECT <column> FROM <table>
78594 **
78595 ** If the RHS of the IN operator is a list or a more complex subquery, then
78596 ** an ephemeral table might need to be generated from the RHS and then
78597 ** pX->iTable made to point to the ephermeral table instead of an
78598 ** existing table.
78599 **
78600 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
78601 ** through the set members, skipping any duplicates. In this case an
78602 ** epheremal table must be used unless the selected <column> is guaranteed
78603 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
78604 ** has a UNIQUE constraint or UNIQUE index.
78605 **
78606 ** If the prNotFound parameter is not 0, then the b-tree will be used
78607 ** for fast set membership tests. In this case an epheremal table must
78608 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
78609 ** be found with <column> as its left-most column.
78610 **
78611 ** When the b-tree is being used for membership tests, the calling function
78612 ** needs to know whether or not the structure contains an SQL NULL
78613 ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
78614 ** If there is any chance that the (...) might contain a NULL value at
78615 ** runtime, then a register is allocated and the register number written
78616 ** to *prNotFound. If there is no chance that the (...) contains a
78617 ** NULL value, then *prNotFound is left unchanged.
78618 **
78619 ** If a register is allocated and its location stored in *prNotFound, then
78620 ** its initial value is NULL.  If the (...) does not remain constant
78621 ** for the duration of the query (i.e. the SELECT within the (...)
78622 ** is a correlated subquery) then the value of the allocated register is
78623 ** reset to NULL each time the subquery is rerun. This allows the
78624 ** caller to use vdbe code equivalent to the following:
78625 **
78626 **   if( register==NULL ){
78627 **     has_null = <test if data structure contains null>
78628 **     register = 1
78629 **   }
78630 **
78631 ** in order to avoid running the <test if data structure contains null>
78632 ** test more often than is necessary.
78633 */
78634 #ifndef SQLITE_OMIT_SUBQUERY
78635 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
78636   Select *p;                            /* SELECT to the right of IN operator */
78637   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
78638   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
78639   int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
78640   Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
78641 
78642   assert( pX->op==TK_IN );
78643 
78644   /* Check to see if an existing table or index can be used to
78645   ** satisfy the query.  This is preferable to generating a new
78646   ** ephemeral table.
78647   */
78648   p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
78649   if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
78650     sqlite3 *db = pParse->db;              /* Database connection */
78651     Table *pTab;                           /* Table <table>. */
78652     Expr *pExpr;                           /* Expression <column> */
78653     i16 iCol;                              /* Index of column <column> */
78654     i16 iDb;                               /* Database idx for pTab */
78655 
78656     assert( p );                        /* Because of isCandidateForInOpt(p) */
78657     assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
78658     assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
78659     assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
78660     pTab = p->pSrc->a[0].pTab;
78661     pExpr = p->pEList->a[0].pExpr;
78662     iCol = (i16)pExpr->iColumn;
78663 
78664     /* Code an OP_Transaction and OP_TableLock for <table>. */
78665     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
78666     sqlite3CodeVerifySchema(pParse, iDb);
78667     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
78668 
78669     /* This function is only called from two places. In both cases the vdbe
78670     ** has already been allocated. So assume sqlite3GetVdbe() is always
78671     ** successful here.
78672     */
78673     assert(v);
78674     if( iCol<0 ){
78675       int iAddr = sqlite3CodeOnce(pParse);
78676       VdbeCoverage(v);
78677 
78678       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
78679       eType = IN_INDEX_ROWID;
78680 
78681       sqlite3VdbeJumpHere(v, iAddr);
78682     }else{
78683       Index *pIdx;                         /* Iterator variable */
78684 
78685       /* The collation sequence used by the comparison. If an index is to
78686       ** be used in place of a temp-table, it must be ordered according
78687       ** to this collation sequence.  */
78688       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
78689 
78690       /* Check that the affinity that will be used to perform the
78691       ** comparison is the same as the affinity of the column. If
78692       ** it is not, it is not possible to use any index.
78693       */
78694       int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
78695 
78696       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
78697         if( (pIdx->aiColumn[0]==iCol)
78698          && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
78699          && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
78700         ){
78701           int iAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
78702           sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
78703           sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
78704           VdbeComment((v, "%s", pIdx->zName));
78705           assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
78706           eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
78707 
78708           if( prNotFound && !pTab->aCol[iCol].notNull ){
78709             *prNotFound = ++pParse->nMem;
78710             sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
78711           }
78712           sqlite3VdbeJumpHere(v, iAddr);
78713         }
78714       }
78715     }
78716   }
78717 
78718   if( eType==0 ){
78719     /* Could not found an existing table or index to use as the RHS b-tree.
78720     ** We will have to generate an ephemeral table to do the job.
78721     */
78722     u32 savedNQueryLoop = pParse->nQueryLoop;
78723     int rMayHaveNull = 0;
78724     eType = IN_INDEX_EPH;
78725     if( prNotFound ){
78726       *prNotFound = rMayHaveNull = ++pParse->nMem;
78727       sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
78728     }else{
78729       testcase( pParse->nQueryLoop>0 );
78730       pParse->nQueryLoop = 0;
78731       if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
78732         eType = IN_INDEX_ROWID;
78733       }
78734     }
78735     sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
78736     pParse->nQueryLoop = savedNQueryLoop;
78737   }else{
78738     pX->iTable = iTab;
78739   }
78740   return eType;
78741 }
78742 #endif
78743 
78744 /*
78745 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
78746 ** or IN operators.  Examples:
78747 **
78748 **     (SELECT a FROM b)          -- subquery
78749 **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
78750 **     x IN (4,5,11)              -- IN operator with list on right-hand side
78751 **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
78752 **
78753 ** The pExpr parameter describes the expression that contains the IN
78754 ** operator or subquery.
78755 **
78756 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
78757 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
78758 ** to some integer key column of a table B-Tree. In this case, use an
78759 ** intkey B-Tree to store the set of IN(...) values instead of the usual
78760 ** (slower) variable length keys B-Tree.
78761 **
78762 ** If rMayHaveNull is non-zero, that means that the operation is an IN
78763 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
78764 ** Furthermore, the IN is in a WHERE clause and that we really want
78765 ** to iterate over the RHS of the IN operator in order to quickly locate
78766 ** all corresponding LHS elements.  All this routine does is initialize
78767 ** the register given by rMayHaveNull to NULL.  Calling routines will take
78768 ** care of changing this register value to non-NULL if the RHS is NULL-free.
78769 **
78770 ** If rMayHaveNull is zero, that means that the subquery is being used
78771 ** for membership testing only.  There is no need to initialize any
78772 ** registers to indicate the presence or absence of NULLs on the RHS.
78773 **
78774 ** For a SELECT or EXISTS operator, return the register that holds the
78775 ** result.  For IN operators or if an error occurs, the return value is 0.
78776 */
78777 #ifndef SQLITE_OMIT_SUBQUERY
78778 SQLITE_PRIVATE int sqlite3CodeSubselect(
78779   Parse *pParse,          /* Parsing context */
78780   Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
78781   int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
78782   int isRowid             /* If true, LHS of IN operator is a rowid */
78783 ){
78784   int testAddr = -1;                      /* One-time test address */
78785   int rReg = 0;                           /* Register storing resulting */
78786   Vdbe *v = sqlite3GetVdbe(pParse);
78787   if( NEVER(v==0) ) return 0;
78788   sqlite3ExprCachePush(pParse);
78789 
78790   /* This code must be run in its entirety every time it is encountered
78791   ** if any of the following is true:
78792   **
78793   **    *  The right-hand side is a correlated subquery
78794   **    *  The right-hand side is an expression list containing variables
78795   **    *  We are inside a trigger
78796   **
78797   ** If all of the above are false, then we can run this code just once
78798   ** save the results, and reuse the same result on subsequent invocations.
78799   */
78800   if( !ExprHasProperty(pExpr, EP_VarSelect) ){
78801     testAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
78802   }
78803 
78804 #ifndef SQLITE_OMIT_EXPLAIN
78805   if( pParse->explain==2 ){
78806     char *zMsg = sqlite3MPrintf(
78807         pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
78808         pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
78809     );
78810     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
78811   }
78812 #endif
78813 
78814   switch( pExpr->op ){
78815     case TK_IN: {
78816       char affinity;              /* Affinity of the LHS of the IN */
78817       int addr;                   /* Address of OP_OpenEphemeral instruction */
78818       Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
78819       KeyInfo *pKeyInfo = 0;      /* Key information */
78820 
78821       if( rMayHaveNull ){
78822         sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
78823       }
78824 
78825       affinity = sqlite3ExprAffinity(pLeft);
78826 
78827       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
78828       ** expression it is handled the same way.  An ephemeral table is
78829       ** filled with single-field index keys representing the results
78830       ** from the SELECT or the <exprlist>.
78831       **
78832       ** If the 'x' expression is a column value, or the SELECT...
78833       ** statement returns a column value, then the affinity of that
78834       ** column is used to build the index keys. If both 'x' and the
78835       ** SELECT... statement are columns, then numeric affinity is used
78836       ** if either column has NUMERIC or INTEGER affinity. If neither
78837       ** 'x' nor the SELECT... statement are columns, then numeric affinity
78838       ** is used.
78839       */
78840       pExpr->iTable = pParse->nTab++;
78841       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
78842       pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
78843 
78844       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
78845         /* Case 1:     expr IN (SELECT ...)
78846         **
78847         ** Generate code to write the results of the select into the temporary
78848         ** table allocated and opened above.
78849         */
78850         SelectDest dest;
78851         ExprList *pEList;
78852 
78853         assert( !isRowid );
78854         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
78855         dest.affSdst = (u8)affinity;
78856         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
78857         pExpr->x.pSelect->iLimit = 0;
78858         testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
78859         if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
78860           sqlite3KeyInfoUnref(pKeyInfo);
78861           return 0;
78862         }
78863         pEList = pExpr->x.pSelect->pEList;
78864         assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
78865         assert( pEList!=0 );
78866         assert( pEList->nExpr>0 );
78867         assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
78868         pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
78869                                                          pEList->a[0].pExpr);
78870       }else if( ALWAYS(pExpr->x.pList!=0) ){
78871         /* Case 2:     expr IN (exprlist)
78872         **
78873         ** For each expression, build an index key from the evaluation and
78874         ** store it in the temporary table. If <expr> is a column, then use
78875         ** that columns affinity when building index keys. If <expr> is not
78876         ** a column, use numeric affinity.
78877         */
78878         int i;
78879         ExprList *pList = pExpr->x.pList;
78880         struct ExprList_item *pItem;
78881         int r1, r2, r3;
78882 
78883         if( !affinity ){
78884           affinity = SQLITE_AFF_NONE;
78885         }
78886         if( pKeyInfo ){
78887           assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
78888           pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
78889         }
78890 
78891         /* Loop through each expression in <exprlist>. */
78892         r1 = sqlite3GetTempReg(pParse);
78893         r2 = sqlite3GetTempReg(pParse);
78894         sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
78895         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
78896           Expr *pE2 = pItem->pExpr;
78897           int iValToIns;
78898 
78899           /* If the expression is not constant then we will need to
78900           ** disable the test that was generated above that makes sure
78901           ** this code only executes once.  Because for a non-constant
78902           ** expression we need to rerun this code each time.
78903           */
78904           if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
78905             sqlite3VdbeChangeToNoop(v, testAddr);
78906             testAddr = -1;
78907           }
78908 
78909           /* Evaluate the expression and insert it into the temp table */
78910           if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
78911             sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
78912           }else{
78913             r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
78914             if( isRowid ){
78915               sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
78916                                 sqlite3VdbeCurrentAddr(v)+2);
78917               VdbeCoverage(v);
78918               sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
78919             }else{
78920               sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
78921               sqlite3ExprCacheAffinityChange(pParse, r3, 1);
78922               sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
78923             }
78924           }
78925         }
78926         sqlite3ReleaseTempReg(pParse, r1);
78927         sqlite3ReleaseTempReg(pParse, r2);
78928       }
78929       if( pKeyInfo ){
78930         sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
78931       }
78932       break;
78933     }
78934 
78935     case TK_EXISTS:
78936     case TK_SELECT:
78937     default: {
78938       /* If this has to be a scalar SELECT.  Generate code to put the
78939       ** value of this select in a memory cell and record the number
78940       ** of the memory cell in iColumn.  If this is an EXISTS, write
78941       ** an integer 0 (not exists) or 1 (exists) into a memory cell
78942       ** and record that memory cell in iColumn.
78943       */
78944       Select *pSel;                         /* SELECT statement to encode */
78945       SelectDest dest;                      /* How to deal with SELECt result */
78946 
78947       testcase( pExpr->op==TK_EXISTS );
78948       testcase( pExpr->op==TK_SELECT );
78949       assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
78950 
78951       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
78952       pSel = pExpr->x.pSelect;
78953       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
78954       if( pExpr->op==TK_SELECT ){
78955         dest.eDest = SRT_Mem;
78956         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
78957         VdbeComment((v, "Init subquery result"));
78958       }else{
78959         dest.eDest = SRT_Exists;
78960         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
78961         VdbeComment((v, "Init EXISTS result"));
78962       }
78963       sqlite3ExprDelete(pParse->db, pSel->pLimit);
78964       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
78965                                   &sqlite3IntTokens[1]);
78966       pSel->iLimit = 0;
78967       if( sqlite3Select(pParse, pSel, &dest) ){
78968         return 0;
78969       }
78970       rReg = dest.iSDParm;
78971       ExprSetVVAProperty(pExpr, EP_NoReduce);
78972       break;
78973     }
78974   }
78975 
78976   if( testAddr>=0 ){
78977     sqlite3VdbeJumpHere(v, testAddr);
78978   }
78979   sqlite3ExprCachePop(pParse, 1);
78980 
78981   return rReg;
78982 }
78983 #endif /* SQLITE_OMIT_SUBQUERY */
78984 
78985 #ifndef SQLITE_OMIT_SUBQUERY
78986 /*
78987 ** Generate code for an IN expression.
78988 **
78989 **      x IN (SELECT ...)
78990 **      x IN (value, value, ...)
78991 **
78992 ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
78993 ** is an array of zero or more values.  The expression is true if the LHS is
78994 ** contained within the RHS.  The value of the expression is unknown (NULL)
78995 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
78996 ** RHS contains one or more NULL values.
78997 **
78998 ** This routine generates code will jump to destIfFalse if the LHS is not
78999 ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
79000 ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
79001 ** within the RHS then fall through.
79002 */
79003 static void sqlite3ExprCodeIN(
79004   Parse *pParse,        /* Parsing and code generating context */
79005   Expr *pExpr,          /* The IN expression */
79006   int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
79007   int destIfNull        /* Jump here if the results are unknown due to NULLs */
79008 ){
79009   int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
79010   char affinity;        /* Comparison affinity to use */
79011   int eType;            /* Type of the RHS */
79012   int r1;               /* Temporary use register */
79013   Vdbe *v;              /* Statement under construction */
79014 
79015   /* Compute the RHS.   After this step, the table with cursor
79016   ** pExpr->iTable will contains the values that make up the RHS.
79017   */
79018   v = pParse->pVdbe;
79019   assert( v!=0 );       /* OOM detected prior to this routine */
79020   VdbeNoopComment((v, "begin IN expr"));
79021   eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
79022 
79023   /* Figure out the affinity to use to create a key from the results
79024   ** of the expression. affinityStr stores a static string suitable for
79025   ** P4 of OP_MakeRecord.
79026   */
79027   affinity = comparisonAffinity(pExpr);
79028 
79029   /* Code the LHS, the <expr> from "<expr> IN (...)".
79030   */
79031   sqlite3ExprCachePush(pParse);
79032   r1 = sqlite3GetTempReg(pParse);
79033   sqlite3ExprCode(pParse, pExpr->pLeft, r1);
79034 
79035   /* If the LHS is NULL, then the result is either false or NULL depending
79036   ** on whether the RHS is empty or not, respectively.
79037   */
79038   if( destIfNull==destIfFalse ){
79039     /* Shortcut for the common case where the false and NULL outcomes are
79040     ** the same. */
79041     sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); VdbeCoverage(v);
79042   }else{
79043     int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); VdbeCoverage(v);
79044     sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
79045     VdbeCoverage(v);
79046     sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
79047     sqlite3VdbeJumpHere(v, addr1);
79048   }
79049 
79050   if( eType==IN_INDEX_ROWID ){
79051     /* In this case, the RHS is the ROWID of table b-tree
79052     */
79053     sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); VdbeCoverage(v);
79054     sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
79055     VdbeCoverage(v);
79056   }else{
79057     /* In this case, the RHS is an index b-tree.
79058     */
79059     sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
79060 
79061     /* If the set membership test fails, then the result of the
79062     ** "x IN (...)" expression must be either 0 or NULL. If the set
79063     ** contains no NULL values, then the result is 0. If the set
79064     ** contains one or more NULL values, then the result of the
79065     ** expression is also NULL.
79066     */
79067     if( rRhsHasNull==0 || destIfFalse==destIfNull ){
79068       /* This branch runs if it is known at compile time that the RHS
79069       ** cannot contain NULL values. This happens as the result
79070       ** of a "NOT NULL" constraint in the database schema.
79071       **
79072       ** Also run this branch if NULL is equivalent to FALSE
79073       ** for this particular IN operator.
79074       */
79075       sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
79076       VdbeCoverage(v);
79077     }else{
79078       /* In this branch, the RHS of the IN might contain a NULL and
79079       ** the presence of a NULL on the RHS makes a difference in the
79080       ** outcome.
79081       */
79082       int j1, j2;
79083 
79084       /* First check to see if the LHS is contained in the RHS.  If so,
79085       ** then the presence of NULLs in the RHS does not matter, so jump
79086       ** over all of the code that follows.
79087       */
79088       j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
79089       VdbeCoverage(v);
79090 
79091       /* Here we begin generating code that runs if the LHS is not
79092       ** contained within the RHS.  Generate additional code that
79093       ** tests the RHS for NULLs.  If the RHS contains a NULL then
79094       ** jump to destIfNull.  If there are no NULLs in the RHS then
79095       ** jump to destIfFalse.
79096       */
79097       sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); VdbeCoverage(v);
79098       sqlite3VdbeAddOp2(v, OP_IfNot, rRhsHasNull, destIfFalse); VdbeCoverage(v);
79099       j2 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
79100       VdbeCoverage(v);
79101       sqlite3VdbeAddOp2(v, OP_Integer, 0, rRhsHasNull);
79102       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
79103       sqlite3VdbeJumpHere(v, j2);
79104       sqlite3VdbeAddOp2(v, OP_Integer, 1, rRhsHasNull);
79105       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
79106 
79107       /* The OP_Found at the top of this branch jumps here when true,
79108       ** causing the overall IN expression evaluation to fall through.
79109       */
79110       sqlite3VdbeJumpHere(v, j1);
79111     }
79112   }
79113   sqlite3ReleaseTempReg(pParse, r1);
79114   sqlite3ExprCachePop(pParse, 1);
79115   VdbeComment((v, "end IN expr"));
79116 }
79117 #endif /* SQLITE_OMIT_SUBQUERY */
79118 
79119 /*
79120 ** Duplicate an 8-byte value
79121 */
79122 static char *dup8bytes(Vdbe *v, const char *in){
79123   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
79124   if( out ){
79125     memcpy(out, in, 8);
79126   }
79127   return out;
79128 }
79129 
79130 #ifndef SQLITE_OMIT_FLOATING_POINT
79131 /*
79132 ** Generate an instruction that will put the floating point
79133 ** value described by z[0..n-1] into register iMem.
79134 **
79135 ** The z[] string will probably not be zero-terminated.  But the
79136 ** z[n] character is guaranteed to be something that does not look
79137 ** like the continuation of the number.
79138 */
79139 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
79140   if( ALWAYS(z!=0) ){
79141     double value;
79142     char *zV;
79143     sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
79144     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
79145     if( negateFlag ) value = -value;
79146     zV = dup8bytes(v, (char*)&value);
79147     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
79148   }
79149 }
79150 #endif
79151 
79152 
79153 /*
79154 ** Generate an instruction that will put the integer describe by
79155 ** text z[0..n-1] into register iMem.
79156 **
79157 ** Expr.u.zToken is always UTF8 and zero-terminated.
79158 */
79159 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
79160   Vdbe *v = pParse->pVdbe;
79161   if( pExpr->flags & EP_IntValue ){
79162     int i = pExpr->u.iValue;
79163     assert( i>=0 );
79164     if( negFlag ) i = -i;
79165     sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
79166   }else{
79167     int c;
79168     i64 value;
79169     const char *z = pExpr->u.zToken;
79170     assert( z!=0 );
79171     c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
79172     if( c==0 || (c==2 && negFlag) ){
79173       char *zV;
79174       if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
79175       zV = dup8bytes(v, (char*)&value);
79176       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
79177     }else{
79178 #ifdef SQLITE_OMIT_FLOATING_POINT
79179       sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
79180 #else
79181       codeReal(v, z, negFlag, iMem);
79182 #endif
79183     }
79184   }
79185 }
79186 
79187 /*
79188 ** Clear a cache entry.
79189 */
79190 static void cacheEntryClear(Parse *pParse, struct yColCache *p){
79191   if( p->tempReg ){
79192     if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
79193       pParse->aTempReg[pParse->nTempReg++] = p->iReg;
79194     }
79195     p->tempReg = 0;
79196   }
79197 }
79198 
79199 
79200 /*
79201 ** Record in the column cache that a particular column from a
79202 ** particular table is stored in a particular register.
79203 */
79204 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
79205   int i;
79206   int minLru;
79207   int idxLru;
79208   struct yColCache *p;
79209 
79210   assert( iReg>0 );  /* Register numbers are always positive */
79211   assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
79212 
79213   /* The SQLITE_ColumnCache flag disables the column cache.  This is used
79214   ** for testing only - to verify that SQLite always gets the same answer
79215   ** with and without the column cache.
79216   */
79217   if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
79218 
79219   /* First replace any existing entry.
79220   **
79221   ** Actually, the way the column cache is currently used, we are guaranteed
79222   ** that the object will never already be in cache.  Verify this guarantee.
79223   */
79224 #ifndef NDEBUG
79225   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79226     assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
79227   }
79228 #endif
79229 
79230   /* Find an empty slot and replace it */
79231   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79232     if( p->iReg==0 ){
79233       p->iLevel = pParse->iCacheLevel;
79234       p->iTable = iTab;
79235       p->iColumn = iCol;
79236       p->iReg = iReg;
79237       p->tempReg = 0;
79238       p->lru = pParse->iCacheCnt++;
79239       return;
79240     }
79241   }
79242 
79243   /* Replace the last recently used */
79244   minLru = 0x7fffffff;
79245   idxLru = -1;
79246   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79247     if( p->lru<minLru ){
79248       idxLru = i;
79249       minLru = p->lru;
79250     }
79251   }
79252   if( ALWAYS(idxLru>=0) ){
79253     p = &pParse->aColCache[idxLru];
79254     p->iLevel = pParse->iCacheLevel;
79255     p->iTable = iTab;
79256     p->iColumn = iCol;
79257     p->iReg = iReg;
79258     p->tempReg = 0;
79259     p->lru = pParse->iCacheCnt++;
79260     return;
79261   }
79262 }
79263 
79264 /*
79265 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
79266 ** Purge the range of registers from the column cache.
79267 */
79268 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
79269   int i;
79270   int iLast = iReg + nReg - 1;
79271   struct yColCache *p;
79272   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79273     int r = p->iReg;
79274     if( r>=iReg && r<=iLast ){
79275       cacheEntryClear(pParse, p);
79276       p->iReg = 0;
79277     }
79278   }
79279 }
79280 
79281 /*
79282 ** Remember the current column cache context.  Any new entries added
79283 ** added to the column cache after this call are removed when the
79284 ** corresponding pop occurs.
79285 */
79286 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
79287   pParse->iCacheLevel++;
79288 #ifdef SQLITE_DEBUG
79289   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
79290     printf("PUSH to %d\n", pParse->iCacheLevel);
79291   }
79292 #endif
79293 }
79294 
79295 /*
79296 ** Remove from the column cache any entries that were added since the
79297 ** the previous N Push operations.  In other words, restore the cache
79298 ** to the state it was in N Pushes ago.
79299 */
79300 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
79301   int i;
79302   struct yColCache *p;
79303   assert( N>0 );
79304   assert( pParse->iCacheLevel>=N );
79305   pParse->iCacheLevel -= N;
79306 #ifdef SQLITE_DEBUG
79307   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
79308     printf("POP  to %d\n", pParse->iCacheLevel);
79309   }
79310 #endif
79311   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79312     if( p->iReg && p->iLevel>pParse->iCacheLevel ){
79313       cacheEntryClear(pParse, p);
79314       p->iReg = 0;
79315     }
79316   }
79317 }
79318 
79319 /*
79320 ** When a cached column is reused, make sure that its register is
79321 ** no longer available as a temp register.  ticket #3879:  that same
79322 ** register might be in the cache in multiple places, so be sure to
79323 ** get them all.
79324 */
79325 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
79326   int i;
79327   struct yColCache *p;
79328   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79329     if( p->iReg==iReg ){
79330       p->tempReg = 0;
79331     }
79332   }
79333 }
79334 
79335 /*
79336 ** Generate code to extract the value of the iCol-th column of a table.
79337 */
79338 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
79339   Vdbe *v,        /* The VDBE under construction */
79340   Table *pTab,    /* The table containing the value */
79341   int iTabCur,    /* The table cursor.  Or the PK cursor for WITHOUT ROWID */
79342   int iCol,       /* Index of the column to extract */
79343   int regOut      /* Extract the value into this register */
79344 ){
79345   if( iCol<0 || iCol==pTab->iPKey ){
79346     sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
79347   }else{
79348     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
79349     int x = iCol;
79350     if( !HasRowid(pTab) ){
79351       x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
79352     }
79353     sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
79354   }
79355   if( iCol>=0 ){
79356     sqlite3ColumnDefault(v, pTab, iCol, regOut);
79357   }
79358 }
79359 
79360 /*
79361 ** Generate code that will extract the iColumn-th column from
79362 ** table pTab and store the column value in a register.  An effort
79363 ** is made to store the column value in register iReg, but this is
79364 ** not guaranteed.  The location of the column value is returned.
79365 **
79366 ** There must be an open cursor to pTab in iTable when this routine
79367 ** is called.  If iColumn<0 then code is generated that extracts the rowid.
79368 */
79369 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
79370   Parse *pParse,   /* Parsing and code generating context */
79371   Table *pTab,     /* Description of the table we are reading from */
79372   int iColumn,     /* Index of the table column */
79373   int iTable,      /* The cursor pointing to the table */
79374   int iReg,        /* Store results here */
79375   u8 p5            /* P5 value for OP_Column */
79376 ){
79377   Vdbe *v = pParse->pVdbe;
79378   int i;
79379   struct yColCache *p;
79380 
79381   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79382     if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
79383       p->lru = pParse->iCacheCnt++;
79384       sqlite3ExprCachePinRegister(pParse, p->iReg);
79385       return p->iReg;
79386     }
79387   }
79388   assert( v!=0 );
79389   sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
79390   if( p5 ){
79391     sqlite3VdbeChangeP5(v, p5);
79392   }else{
79393     sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
79394   }
79395   return iReg;
79396 }
79397 
79398 /*
79399 ** Clear all column cache entries.
79400 */
79401 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
79402   int i;
79403   struct yColCache *p;
79404 
79405 #if SQLITE_DEBUG
79406   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
79407     printf("CLEAR\n");
79408   }
79409 #endif
79410   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79411     if( p->iReg ){
79412       cacheEntryClear(pParse, p);
79413       p->iReg = 0;
79414     }
79415   }
79416 }
79417 
79418 /*
79419 ** Record the fact that an affinity change has occurred on iCount
79420 ** registers starting with iStart.
79421 */
79422 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
79423   sqlite3ExprCacheRemove(pParse, iStart, iCount);
79424 }
79425 
79426 /*
79427 ** Generate code to move content from registers iFrom...iFrom+nReg-1
79428 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
79429 */
79430 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
79431   int i;
79432   struct yColCache *p;
79433   assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
79434   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
79435   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79436     int x = p->iReg;
79437     if( x>=iFrom && x<iFrom+nReg ){
79438       p->iReg += iTo-iFrom;
79439     }
79440   }
79441 }
79442 
79443 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
79444 /*
79445 ** Return true if any register in the range iFrom..iTo (inclusive)
79446 ** is used as part of the column cache.
79447 **
79448 ** This routine is used within assert() and testcase() macros only
79449 ** and does not appear in a normal build.
79450 */
79451 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
79452   int i;
79453   struct yColCache *p;
79454   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
79455     int r = p->iReg;
79456     if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
79457   }
79458   return 0;
79459 }
79460 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
79461 
79462 /*
79463 ** Convert an expression node to a TK_REGISTER
79464 */
79465 static void exprToRegister(Expr *p, int iReg){
79466   p->op2 = p->op;
79467   p->op = TK_REGISTER;
79468   p->iTable = iReg;
79469   ExprClearProperty(p, EP_Skip);
79470 }
79471 
79472 /*
79473 ** Generate code into the current Vdbe to evaluate the given
79474 ** expression.  Attempt to store the results in register "target".
79475 ** Return the register where results are stored.
79476 **
79477 ** With this routine, there is no guarantee that results will
79478 ** be stored in target.  The result might be stored in some other
79479 ** register if it is convenient to do so.  The calling function
79480 ** must check the return code and move the results to the desired
79481 ** register.
79482 */
79483 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
79484   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
79485   int op;                   /* The opcode being coded */
79486   int inReg = target;       /* Results stored in register inReg */
79487   int regFree1 = 0;         /* If non-zero free this temporary register */
79488   int regFree2 = 0;         /* If non-zero free this temporary register */
79489   int r1, r2, r3, r4;       /* Various register numbers */
79490   sqlite3 *db = pParse->db; /* The database connection */
79491   Expr tempX;               /* Temporary expression node */
79492 
79493   assert( target>0 && target<=pParse->nMem );
79494   if( v==0 ){
79495     assert( pParse->db->mallocFailed );
79496     return 0;
79497   }
79498 
79499   if( pExpr==0 ){
79500     op = TK_NULL;
79501   }else{
79502     op = pExpr->op;
79503   }
79504   switch( op ){
79505     case TK_AGG_COLUMN: {
79506       AggInfo *pAggInfo = pExpr->pAggInfo;
79507       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
79508       if( !pAggInfo->directMode ){
79509         assert( pCol->iMem>0 );
79510         inReg = pCol->iMem;
79511         break;
79512       }else if( pAggInfo->useSortingIdx ){
79513         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
79514                               pCol->iSorterColumn, target);
79515         break;
79516       }
79517       /* Otherwise, fall thru into the TK_COLUMN case */
79518     }
79519     case TK_COLUMN: {
79520       int iTab = pExpr->iTable;
79521       if( iTab<0 ){
79522         if( pParse->ckBase>0 ){
79523           /* Generating CHECK constraints or inserting into partial index */
79524           inReg = pExpr->iColumn + pParse->ckBase;
79525           break;
79526         }else{
79527           /* Deleting from a partial index */
79528           iTab = pParse->iPartIdxTab;
79529         }
79530       }
79531       inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
79532                                pExpr->iColumn, iTab, target,
79533                                pExpr->op2);
79534       break;
79535     }
79536     case TK_INTEGER: {
79537       codeInteger(pParse, pExpr, 0, target);
79538       break;
79539     }
79540 #ifndef SQLITE_OMIT_FLOATING_POINT
79541     case TK_FLOAT: {
79542       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79543       codeReal(v, pExpr->u.zToken, 0, target);
79544       break;
79545     }
79546 #endif
79547     case TK_STRING: {
79548       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79549       sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
79550       break;
79551     }
79552     case TK_NULL: {
79553       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
79554       break;
79555     }
79556 #ifndef SQLITE_OMIT_BLOB_LITERAL
79557     case TK_BLOB: {
79558       int n;
79559       const char *z;
79560       char *zBlob;
79561       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79562       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
79563       assert( pExpr->u.zToken[1]=='\'' );
79564       z = &pExpr->u.zToken[2];
79565       n = sqlite3Strlen30(z) - 1;
79566       assert( z[n]=='\'' );
79567       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
79568       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
79569       break;
79570     }
79571 #endif
79572     case TK_VARIABLE: {
79573       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79574       assert( pExpr->u.zToken!=0 );
79575       assert( pExpr->u.zToken[0]!=0 );
79576       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
79577       if( pExpr->u.zToken[1]!=0 ){
79578         assert( pExpr->u.zToken[0]=='?'
79579              || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
79580         sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
79581       }
79582       break;
79583     }
79584     case TK_REGISTER: {
79585       inReg = pExpr->iTable;
79586       break;
79587     }
79588     case TK_AS: {
79589       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
79590       break;
79591     }
79592 #ifndef SQLITE_OMIT_CAST
79593     case TK_CAST: {
79594       /* Expressions of the form:   CAST(pLeft AS token) */
79595       int aff, to_op;
79596       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
79597       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79598       aff = sqlite3AffinityType(pExpr->u.zToken, 0);
79599       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
79600       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
79601       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
79602       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
79603       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
79604       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
79605       testcase( to_op==OP_ToText );
79606       testcase( to_op==OP_ToBlob );
79607       testcase( to_op==OP_ToNumeric );
79608       testcase( to_op==OP_ToInt );
79609       testcase( to_op==OP_ToReal );
79610       if( inReg!=target ){
79611         sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
79612         inReg = target;
79613       }
79614       sqlite3VdbeAddOp1(v, to_op, inReg);
79615       testcase( usedAsColumnCache(pParse, inReg, inReg) );
79616       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
79617       break;
79618     }
79619 #endif /* SQLITE_OMIT_CAST */
79620     case TK_LT:
79621     case TK_LE:
79622     case TK_GT:
79623     case TK_GE:
79624     case TK_NE:
79625     case TK_EQ: {
79626       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79627       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
79628       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
79629                   r1, r2, inReg, SQLITE_STOREP2);
79630       assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
79631       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
79632       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
79633       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
79634       assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
79635       assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
79636       testcase( regFree1==0 );
79637       testcase( regFree2==0 );
79638       break;
79639     }
79640     case TK_IS:
79641     case TK_ISNOT: {
79642       testcase( op==TK_IS );
79643       testcase( op==TK_ISNOT );
79644       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79645       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
79646       op = (op==TK_IS) ? TK_EQ : TK_NE;
79647       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
79648                   r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
79649       VdbeCoverageIf(v, op==TK_EQ);
79650       VdbeCoverageIf(v, op==TK_NE);
79651       testcase( regFree1==0 );
79652       testcase( regFree2==0 );
79653       break;
79654     }
79655     case TK_AND:
79656     case TK_OR:
79657     case TK_PLUS:
79658     case TK_STAR:
79659     case TK_MINUS:
79660     case TK_REM:
79661     case TK_BITAND:
79662     case TK_BITOR:
79663     case TK_SLASH:
79664     case TK_LSHIFT:
79665     case TK_RSHIFT:
79666     case TK_CONCAT: {
79667       assert( TK_AND==OP_And );            testcase( op==TK_AND );
79668       assert( TK_OR==OP_Or );              testcase( op==TK_OR );
79669       assert( TK_PLUS==OP_Add );           testcase( op==TK_PLUS );
79670       assert( TK_MINUS==OP_Subtract );     testcase( op==TK_MINUS );
79671       assert( TK_REM==OP_Remainder );      testcase( op==TK_REM );
79672       assert( TK_BITAND==OP_BitAnd );      testcase( op==TK_BITAND );
79673       assert( TK_BITOR==OP_BitOr );        testcase( op==TK_BITOR );
79674       assert( TK_SLASH==OP_Divide );       testcase( op==TK_SLASH );
79675       assert( TK_LSHIFT==OP_ShiftLeft );   testcase( op==TK_LSHIFT );
79676       assert( TK_RSHIFT==OP_ShiftRight );  testcase( op==TK_RSHIFT );
79677       assert( TK_CONCAT==OP_Concat );      testcase( op==TK_CONCAT );
79678       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79679       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
79680       sqlite3VdbeAddOp3(v, op, r2, r1, target);
79681       testcase( regFree1==0 );
79682       testcase( regFree2==0 );
79683       break;
79684     }
79685     case TK_UMINUS: {
79686       Expr *pLeft = pExpr->pLeft;
79687       assert( pLeft );
79688       if( pLeft->op==TK_INTEGER ){
79689         codeInteger(pParse, pLeft, 1, target);
79690 #ifndef SQLITE_OMIT_FLOATING_POINT
79691       }else if( pLeft->op==TK_FLOAT ){
79692         assert( !ExprHasProperty(pExpr, EP_IntValue) );
79693         codeReal(v, pLeft->u.zToken, 1, target);
79694 #endif
79695       }else{
79696         tempX.op = TK_INTEGER;
79697         tempX.flags = EP_IntValue|EP_TokenOnly;
79698         tempX.u.iValue = 0;
79699         r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
79700         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
79701         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
79702         testcase( regFree2==0 );
79703       }
79704       inReg = target;
79705       break;
79706     }
79707     case TK_BITNOT:
79708     case TK_NOT: {
79709       assert( TK_BITNOT==OP_BitNot );   testcase( op==TK_BITNOT );
79710       assert( TK_NOT==OP_Not );         testcase( op==TK_NOT );
79711       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79712       testcase( regFree1==0 );
79713       inReg = target;
79714       sqlite3VdbeAddOp2(v, op, r1, inReg);
79715       break;
79716     }
79717     case TK_ISNULL:
79718     case TK_NOTNULL: {
79719       int addr;
79720       assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
79721       assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
79722       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
79723       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79724       testcase( regFree1==0 );
79725       addr = sqlite3VdbeAddOp1(v, op, r1);
79726       VdbeCoverageIf(v, op==TK_ISNULL);
79727       VdbeCoverageIf(v, op==TK_NOTNULL);
79728       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
79729       sqlite3VdbeJumpHere(v, addr);
79730       break;
79731     }
79732     case TK_AGG_FUNCTION: {
79733       AggInfo *pInfo = pExpr->pAggInfo;
79734       if( pInfo==0 ){
79735         assert( !ExprHasProperty(pExpr, EP_IntValue) );
79736         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
79737       }else{
79738         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
79739       }
79740       break;
79741     }
79742     case TK_FUNCTION: {
79743       ExprList *pFarg;       /* List of function arguments */
79744       int nFarg;             /* Number of function arguments */
79745       FuncDef *pDef;         /* The function definition object */
79746       int nId;               /* Length of the function name in bytes */
79747       const char *zId;       /* The function name */
79748       u32 constMask = 0;     /* Mask of function arguments that are constant */
79749       int i;                 /* Loop counter */
79750       u8 enc = ENC(db);      /* The text encoding used by this database */
79751       CollSeq *pColl = 0;    /* A collating sequence */
79752 
79753       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
79754       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
79755         pFarg = 0;
79756       }else{
79757         pFarg = pExpr->x.pList;
79758       }
79759       nFarg = pFarg ? pFarg->nExpr : 0;
79760       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79761       zId = pExpr->u.zToken;
79762       nId = sqlite3Strlen30(zId);
79763       pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
79764       if( pDef==0 ){
79765         sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
79766         break;
79767       }
79768 
79769       /* Attempt a direct implementation of the built-in COALESCE() and
79770       ** IFNULL() functions.  This avoids unnecessary evalation of
79771       ** arguments past the first non-NULL argument.
79772       */
79773       if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
79774         int endCoalesce = sqlite3VdbeMakeLabel(v);
79775         assert( nFarg>=2 );
79776         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
79777         for(i=1; i<nFarg; i++){
79778           sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
79779           VdbeCoverage(v);
79780           sqlite3ExprCacheRemove(pParse, target, 1);
79781           sqlite3ExprCachePush(pParse);
79782           sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
79783           sqlite3ExprCachePop(pParse, 1);
79784         }
79785         sqlite3VdbeResolveLabel(v, endCoalesce);
79786         break;
79787       }
79788 
79789       /* The UNLIKELY() function is a no-op.  The result is the value
79790       ** of the first argument.
79791       */
79792       if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
79793         assert( nFarg>=1 );
79794         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
79795         break;
79796       }
79797 
79798       for(i=0; i<nFarg; i++){
79799         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
79800           testcase( i==31 );
79801           constMask |= MASKBIT32(i);
79802         }
79803         if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
79804           pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
79805         }
79806       }
79807       if( pFarg ){
79808         if( constMask ){
79809           r1 = pParse->nMem+1;
79810           pParse->nMem += nFarg;
79811         }else{
79812           r1 = sqlite3GetTempRange(pParse, nFarg);
79813         }
79814 
79815         /* For length() and typeof() functions with a column argument,
79816         ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
79817         ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
79818         ** loading.
79819         */
79820         if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
79821           u8 exprOp;
79822           assert( nFarg==1 );
79823           assert( pFarg->a[0].pExpr!=0 );
79824           exprOp = pFarg->a[0].pExpr->op;
79825           if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
79826             assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
79827             assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
79828             testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
79829             pFarg->a[0].pExpr->op2 =
79830                   pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
79831           }
79832         }
79833 
79834         sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
79835         sqlite3ExprCodeExprList(pParse, pFarg, r1,
79836                                 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
79837         sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
79838       }else{
79839         r1 = 0;
79840       }
79841 #ifndef SQLITE_OMIT_VIRTUALTABLE
79842       /* Possibly overload the function if the first argument is
79843       ** a virtual table column.
79844       **
79845       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
79846       ** second argument, not the first, as the argument to test to
79847       ** see if it is a column in a virtual table.  This is done because
79848       ** the left operand of infix functions (the operand we want to
79849       ** control overloading) ends up as the second argument to the
79850       ** function.  The expression "A glob B" is equivalent to
79851       ** "glob(B,A).  We want to use the A in "A glob B" to test
79852       ** for function overloading.  But we use the B term in "glob(B,A)".
79853       */
79854       if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
79855         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
79856       }else if( nFarg>0 ){
79857         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
79858       }
79859 #endif
79860       if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
79861         if( !pColl ) pColl = db->pDfltColl;
79862         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
79863       }
79864       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
79865                         (char*)pDef, P4_FUNCDEF);
79866       sqlite3VdbeChangeP5(v, (u8)nFarg);
79867       if( nFarg && constMask==0 ){
79868         sqlite3ReleaseTempRange(pParse, r1, nFarg);
79869       }
79870       break;
79871     }
79872 #ifndef SQLITE_OMIT_SUBQUERY
79873     case TK_EXISTS:
79874     case TK_SELECT: {
79875       testcase( op==TK_EXISTS );
79876       testcase( op==TK_SELECT );
79877       inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
79878       break;
79879     }
79880     case TK_IN: {
79881       int destIfFalse = sqlite3VdbeMakeLabel(v);
79882       int destIfNull = sqlite3VdbeMakeLabel(v);
79883       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
79884       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
79885       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
79886       sqlite3VdbeResolveLabel(v, destIfFalse);
79887       sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
79888       sqlite3VdbeResolveLabel(v, destIfNull);
79889       break;
79890     }
79891 #endif /* SQLITE_OMIT_SUBQUERY */
79892 
79893 
79894     /*
79895     **    x BETWEEN y AND z
79896     **
79897     ** This is equivalent to
79898     **
79899     **    x>=y AND x<=z
79900     **
79901     ** X is stored in pExpr->pLeft.
79902     ** Y is stored in pExpr->pList->a[0].pExpr.
79903     ** Z is stored in pExpr->pList->a[1].pExpr.
79904     */
79905     case TK_BETWEEN: {
79906       Expr *pLeft = pExpr->pLeft;
79907       struct ExprList_item *pLItem = pExpr->x.pList->a;
79908       Expr *pRight = pLItem->pExpr;
79909 
79910       r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
79911       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
79912       testcase( regFree1==0 );
79913       testcase( regFree2==0 );
79914       r3 = sqlite3GetTempReg(pParse);
79915       r4 = sqlite3GetTempReg(pParse);
79916       codeCompare(pParse, pLeft, pRight, OP_Ge,
79917                   r1, r2, r3, SQLITE_STOREP2);  VdbeCoverage(v);
79918       pLItem++;
79919       pRight = pLItem->pExpr;
79920       sqlite3ReleaseTempReg(pParse, regFree2);
79921       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
79922       testcase( regFree2==0 );
79923       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
79924       VdbeCoverage(v);
79925       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
79926       sqlite3ReleaseTempReg(pParse, r3);
79927       sqlite3ReleaseTempReg(pParse, r4);
79928       break;
79929     }
79930     case TK_COLLATE:
79931     case TK_UPLUS: {
79932       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
79933       break;
79934     }
79935 
79936     case TK_TRIGGER: {
79937       /* If the opcode is TK_TRIGGER, then the expression is a reference
79938       ** to a column in the new.* or old.* pseudo-tables available to
79939       ** trigger programs. In this case Expr.iTable is set to 1 for the
79940       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
79941       ** is set to the column of the pseudo-table to read, or to -1 to
79942       ** read the rowid field.
79943       **
79944       ** The expression is implemented using an OP_Param opcode. The p1
79945       ** parameter is set to 0 for an old.rowid reference, or to (i+1)
79946       ** to reference another column of the old.* pseudo-table, where
79947       ** i is the index of the column. For a new.rowid reference, p1 is
79948       ** set to (n+1), where n is the number of columns in each pseudo-table.
79949       ** For a reference to any other column in the new.* pseudo-table, p1
79950       ** is set to (n+2+i), where n and i are as defined previously. For
79951       ** example, if the table on which triggers are being fired is
79952       ** declared as:
79953       **
79954       **   CREATE TABLE t1(a, b);
79955       **
79956       ** Then p1 is interpreted as follows:
79957       **
79958       **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
79959       **   p1==1   ->    old.a         p1==4   ->    new.a
79960       **   p1==2   ->    old.b         p1==5   ->    new.b
79961       */
79962       Table *pTab = pExpr->pTab;
79963       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
79964 
79965       assert( pExpr->iTable==0 || pExpr->iTable==1 );
79966       assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
79967       assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
79968       assert( p1>=0 && p1<(pTab->nCol*2+2) );
79969 
79970       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
79971       VdbeComment((v, "%s.%s -> $%d",
79972         (pExpr->iTable ? "new" : "old"),
79973         (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
79974         target
79975       ));
79976 
79977 #ifndef SQLITE_OMIT_FLOATING_POINT
79978       /* If the column has REAL affinity, it may currently be stored as an
79979       ** integer. Use OP_RealAffinity to make sure it is really real.  */
79980       if( pExpr->iColumn>=0
79981        && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
79982       ){
79983         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
79984       }
79985 #endif
79986       break;
79987     }
79988 
79989 
79990     /*
79991     ** Form A:
79992     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
79993     **
79994     ** Form B:
79995     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
79996     **
79997     ** Form A is can be transformed into the equivalent form B as follows:
79998     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
79999     **        WHEN x=eN THEN rN ELSE y END
80000     **
80001     ** X (if it exists) is in pExpr->pLeft.
80002     ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
80003     ** odd.  The Y is also optional.  If the number of elements in x.pList
80004     ** is even, then Y is omitted and the "otherwise" result is NULL.
80005     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
80006     **
80007     ** The result of the expression is the Ri for the first matching Ei,
80008     ** or if there is no matching Ei, the ELSE term Y, or if there is
80009     ** no ELSE term, NULL.
80010     */
80011     default: assert( op==TK_CASE ); {
80012       int endLabel;                     /* GOTO label for end of CASE stmt */
80013       int nextCase;                     /* GOTO label for next WHEN clause */
80014       int nExpr;                        /* 2x number of WHEN terms */
80015       int i;                            /* Loop counter */
80016       ExprList *pEList;                 /* List of WHEN terms */
80017       struct ExprList_item *aListelem;  /* Array of WHEN terms */
80018       Expr opCompare;                   /* The X==Ei expression */
80019       Expr *pX;                         /* The X expression */
80020       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
80021       VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
80022 
80023       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
80024       assert(pExpr->x.pList->nExpr > 0);
80025       pEList = pExpr->x.pList;
80026       aListelem = pEList->a;
80027       nExpr = pEList->nExpr;
80028       endLabel = sqlite3VdbeMakeLabel(v);
80029       if( (pX = pExpr->pLeft)!=0 ){
80030         tempX = *pX;
80031         testcase( pX->op==TK_COLUMN );
80032         exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
80033         testcase( regFree1==0 );
80034         opCompare.op = TK_EQ;
80035         opCompare.pLeft = &tempX;
80036         pTest = &opCompare;
80037         /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
80038         ** The value in regFree1 might get SCopy-ed into the file result.
80039         ** So make sure that the regFree1 register is not reused for other
80040         ** purposes and possibly overwritten.  */
80041         regFree1 = 0;
80042       }
80043       for(i=0; i<nExpr-1; i=i+2){
80044         sqlite3ExprCachePush(pParse);
80045         if( pX ){
80046           assert( pTest!=0 );
80047           opCompare.pRight = aListelem[i].pExpr;
80048         }else{
80049           pTest = aListelem[i].pExpr;
80050         }
80051         nextCase = sqlite3VdbeMakeLabel(v);
80052         testcase( pTest->op==TK_COLUMN );
80053         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
80054         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
80055         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
80056         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
80057         sqlite3ExprCachePop(pParse, 1);
80058         sqlite3VdbeResolveLabel(v, nextCase);
80059       }
80060       if( (nExpr&1)!=0 ){
80061         sqlite3ExprCachePush(pParse);
80062         sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
80063         sqlite3ExprCachePop(pParse, 1);
80064       }else{
80065         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
80066       }
80067       assert( db->mallocFailed || pParse->nErr>0
80068            || pParse->iCacheLevel==iCacheLevel );
80069       sqlite3VdbeResolveLabel(v, endLabel);
80070       break;
80071     }
80072 #ifndef SQLITE_OMIT_TRIGGER
80073     case TK_RAISE: {
80074       assert( pExpr->affinity==OE_Rollback
80075            || pExpr->affinity==OE_Abort
80076            || pExpr->affinity==OE_Fail
80077            || pExpr->affinity==OE_Ignore
80078       );
80079       if( !pParse->pTriggerTab ){
80080         sqlite3ErrorMsg(pParse,
80081                        "RAISE() may only be used within a trigger-program");
80082         return 0;
80083       }
80084       if( pExpr->affinity==OE_Abort ){
80085         sqlite3MayAbort(pParse);
80086       }
80087       assert( !ExprHasProperty(pExpr, EP_IntValue) );
80088       if( pExpr->affinity==OE_Ignore ){
80089         sqlite3VdbeAddOp4(
80090             v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
80091         VdbeCoverage(v);
80092       }else{
80093         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
80094                               pExpr->affinity, pExpr->u.zToken, 0, 0);
80095       }
80096 
80097       break;
80098     }
80099 #endif
80100   }
80101   sqlite3ReleaseTempReg(pParse, regFree1);
80102   sqlite3ReleaseTempReg(pParse, regFree2);
80103   return inReg;
80104 }
80105 
80106 /*
80107 ** Factor out the code of the given expression to initialization time.
80108 */
80109 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
80110   Parse *pParse,    /* Parsing context */
80111   Expr *pExpr,      /* The expression to code when the VDBE initializes */
80112   int regDest,      /* Store the value in this register */
80113   u8 reusable       /* True if this expression is reusable */
80114 ){
80115   ExprList *p;
80116   assert( ConstFactorOk(pParse) );
80117   p = pParse->pConstExpr;
80118   pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
80119   p = sqlite3ExprListAppend(pParse, p, pExpr);
80120   if( p ){
80121      struct ExprList_item *pItem = &p->a[p->nExpr-1];
80122      pItem->u.iConstExprReg = regDest;
80123      pItem->reusable = reusable;
80124   }
80125   pParse->pConstExpr = p;
80126 }
80127 
80128 /*
80129 ** Generate code to evaluate an expression and store the results
80130 ** into a register.  Return the register number where the results
80131 ** are stored.
80132 **
80133 ** If the register is a temporary register that can be deallocated,
80134 ** then write its number into *pReg.  If the result register is not
80135 ** a temporary, then set *pReg to zero.
80136 **
80137 ** If pExpr is a constant, then this routine might generate this
80138 ** code to fill the register in the initialization section of the
80139 ** VDBE program, in order to factor it out of the evaluation loop.
80140 */
80141 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
80142   int r2;
80143   pExpr = sqlite3ExprSkipCollate(pExpr);
80144   if( ConstFactorOk(pParse)
80145    && pExpr->op!=TK_REGISTER
80146    && sqlite3ExprIsConstantNotJoin(pExpr)
80147   ){
80148     ExprList *p = pParse->pConstExpr;
80149     int i;
80150     *pReg  = 0;
80151     if( p ){
80152       struct ExprList_item *pItem;
80153       for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
80154         if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
80155           return pItem->u.iConstExprReg;
80156         }
80157       }
80158     }
80159     r2 = ++pParse->nMem;
80160     sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
80161   }else{
80162     int r1 = sqlite3GetTempReg(pParse);
80163     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
80164     if( r2==r1 ){
80165       *pReg = r1;
80166     }else{
80167       sqlite3ReleaseTempReg(pParse, r1);
80168       *pReg = 0;
80169     }
80170   }
80171   return r2;
80172 }
80173 
80174 /*
80175 ** Generate code that will evaluate expression pExpr and store the
80176 ** results in register target.  The results are guaranteed to appear
80177 ** in register target.
80178 */
80179 SQLITE_PRIVATE void sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
80180   int inReg;
80181 
80182   assert( target>0 && target<=pParse->nMem );
80183   if( pExpr && pExpr->op==TK_REGISTER ){
80184     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
80185   }else{
80186     inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
80187     assert( pParse->pVdbe || pParse->db->mallocFailed );
80188     if( inReg!=target && pParse->pVdbe ){
80189       sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
80190     }
80191   }
80192 }
80193 
80194 /*
80195 ** Generate code that will evaluate expression pExpr and store the
80196 ** results in register target.  The results are guaranteed to appear
80197 ** in register target.  If the expression is constant, then this routine
80198 ** might choose to code the expression at initialization time.
80199 */
80200 SQLITE_PRIVATE void sqlite3ExprCodeFactorable(Parse *pParse, Expr *pExpr, int target){
80201   if( pParse->okConstFactor && sqlite3ExprIsConstant(pExpr) ){
80202     sqlite3ExprCodeAtInit(pParse, pExpr, target, 0);
80203   }else{
80204     sqlite3ExprCode(pParse, pExpr, target);
80205   }
80206 }
80207 
80208 /*
80209 ** Generate code that evalutes the given expression and puts the result
80210 ** in register target.
80211 **
80212 ** Also make a copy of the expression results into another "cache" register
80213 ** and modify the expression so that the next time it is evaluated,
80214 ** the result is a copy of the cache register.
80215 **
80216 ** This routine is used for expressions that are used multiple
80217 ** times.  They are evaluated once and the results of the expression
80218 ** are reused.
80219 */
80220 SQLITE_PRIVATE void sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
80221   Vdbe *v = pParse->pVdbe;
80222   int iMem;
80223 
80224   assert( target>0 );
80225   assert( pExpr->op!=TK_REGISTER );
80226   sqlite3ExprCode(pParse, pExpr, target);
80227   iMem = ++pParse->nMem;
80228   sqlite3VdbeAddOp2(v, OP_Copy, target, iMem);
80229   exprToRegister(pExpr, iMem);
80230 }
80231 
80232 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
80233 /*
80234 ** Generate a human-readable explanation of an expression tree.
80235 */
80236 SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
80237   int op;                   /* The opcode being coded */
80238   const char *zBinOp = 0;   /* Binary operator */
80239   const char *zUniOp = 0;   /* Unary operator */
80240   if( pExpr==0 ){
80241     op = TK_NULL;
80242   }else{
80243     op = pExpr->op;
80244   }
80245   switch( op ){
80246     case TK_AGG_COLUMN: {
80247       sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
80248             pExpr->iTable, pExpr->iColumn);
80249       break;
80250     }
80251     case TK_COLUMN: {
80252       if( pExpr->iTable<0 ){
80253         /* This only happens when coding check constraints */
80254         sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
80255       }else{
80256         sqlite3ExplainPrintf(pOut, "{%d:%d}",
80257                              pExpr->iTable, pExpr->iColumn);
80258       }
80259       break;
80260     }
80261     case TK_INTEGER: {
80262       if( pExpr->flags & EP_IntValue ){
80263         sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
80264       }else{
80265         sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
80266       }
80267       break;
80268     }
80269 #ifndef SQLITE_OMIT_FLOATING_POINT
80270     case TK_FLOAT: {
80271       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
80272       break;
80273     }
80274 #endif
80275     case TK_STRING: {
80276       sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
80277       break;
80278     }
80279     case TK_NULL: {
80280       sqlite3ExplainPrintf(pOut,"NULL");
80281       break;
80282     }
80283 #ifndef SQLITE_OMIT_BLOB_LITERAL
80284     case TK_BLOB: {
80285       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
80286       break;
80287     }
80288 #endif
80289     case TK_VARIABLE: {
80290       sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
80291                            pExpr->u.zToken, pExpr->iColumn);
80292       break;
80293     }
80294     case TK_REGISTER: {
80295       sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
80296       break;
80297     }
80298     case TK_AS: {
80299       sqlite3ExplainExpr(pOut, pExpr->pLeft);
80300       break;
80301     }
80302 #ifndef SQLITE_OMIT_CAST
80303     case TK_CAST: {
80304       /* Expressions of the form:   CAST(pLeft AS token) */
80305       const char *zAff = "unk";
80306       switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){
80307         case SQLITE_AFF_TEXT:    zAff = "TEXT";     break;
80308         case SQLITE_AFF_NONE:    zAff = "NONE";     break;
80309         case SQLITE_AFF_NUMERIC: zAff = "NUMERIC";  break;
80310         case SQLITE_AFF_INTEGER: zAff = "INTEGER";  break;
80311         case SQLITE_AFF_REAL:    zAff = "REAL";     break;
80312       }
80313       sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
80314       sqlite3ExplainExpr(pOut, pExpr->pLeft);
80315       sqlite3ExplainPrintf(pOut, ")");
80316       break;
80317     }
80318 #endif /* SQLITE_OMIT_CAST */
80319     case TK_LT:      zBinOp = "LT";     break;
80320     case TK_LE:      zBinOp = "LE";     break;
80321     case TK_GT:      zBinOp = "GT";     break;
80322     case TK_GE:      zBinOp = "GE";     break;
80323     case TK_NE:      zBinOp = "NE";     break;
80324     case TK_EQ:      zBinOp = "EQ";     break;
80325     case TK_IS:      zBinOp = "IS";     break;
80326     case TK_ISNOT:   zBinOp = "ISNOT";  break;
80327     case TK_AND:     zBinOp = "AND";    break;
80328     case TK_OR:      zBinOp = "OR";     break;
80329     case TK_PLUS:    zBinOp = "ADD";    break;
80330     case TK_STAR:    zBinOp = "MUL";    break;
80331     case TK_MINUS:   zBinOp = "SUB";    break;
80332     case TK_REM:     zBinOp = "REM";    break;
80333     case TK_BITAND:  zBinOp = "BITAND"; break;
80334     case TK_BITOR:   zBinOp = "BITOR";  break;
80335     case TK_SLASH:   zBinOp = "DIV";    break;
80336     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
80337     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
80338     case TK_CONCAT:  zBinOp = "CONCAT"; break;
80339 
80340     case TK_UMINUS:  zUniOp = "UMINUS"; break;
80341     case TK_UPLUS:   zUniOp = "UPLUS";  break;
80342     case TK_BITNOT:  zUniOp = "BITNOT"; break;
80343     case TK_NOT:     zUniOp = "NOT";    break;
80344     case TK_ISNULL:  zUniOp = "ISNULL"; break;
80345     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
80346 
80347     case TK_COLLATE: {
80348       sqlite3ExplainExpr(pOut, pExpr->pLeft);
80349       sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
80350       break;
80351     }
80352 
80353     case TK_AGG_FUNCTION:
80354     case TK_FUNCTION: {
80355       ExprList *pFarg;       /* List of function arguments */
80356       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
80357         pFarg = 0;
80358       }else{
80359         pFarg = pExpr->x.pList;
80360       }
80361       if( op==TK_AGG_FUNCTION ){
80362         sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
80363                              pExpr->op2, pExpr->u.zToken);
80364       }else{
80365         sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
80366       }
80367       if( pFarg ){
80368         sqlite3ExplainExprList(pOut, pFarg);
80369       }
80370       sqlite3ExplainPrintf(pOut, ")");
80371       break;
80372     }
80373 #ifndef SQLITE_OMIT_SUBQUERY
80374     case TK_EXISTS: {
80375       sqlite3ExplainPrintf(pOut, "EXISTS(");
80376       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
80377       sqlite3ExplainPrintf(pOut,")");
80378       break;
80379     }
80380     case TK_SELECT: {
80381       sqlite3ExplainPrintf(pOut, "(");
80382       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
80383       sqlite3ExplainPrintf(pOut, ")");
80384       break;
80385     }
80386     case TK_IN: {
80387       sqlite3ExplainPrintf(pOut, "IN(");
80388       sqlite3ExplainExpr(pOut, pExpr->pLeft);
80389       sqlite3ExplainPrintf(pOut, ",");
80390       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
80391         sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
80392       }else{
80393         sqlite3ExplainExprList(pOut, pExpr->x.pList);
80394       }
80395       sqlite3ExplainPrintf(pOut, ")");
80396       break;
80397     }
80398 #endif /* SQLITE_OMIT_SUBQUERY */
80399 
80400     /*
80401     **    x BETWEEN y AND z
80402     **
80403     ** This is equivalent to
80404     **
80405     **    x>=y AND x<=z
80406     **
80407     ** X is stored in pExpr->pLeft.
80408     ** Y is stored in pExpr->pList->a[0].pExpr.
80409     ** Z is stored in pExpr->pList->a[1].pExpr.
80410     */
80411     case TK_BETWEEN: {
80412       Expr *pX = pExpr->pLeft;
80413       Expr *pY = pExpr->x.pList->a[0].pExpr;
80414       Expr *pZ = pExpr->x.pList->a[1].pExpr;
80415       sqlite3ExplainPrintf(pOut, "BETWEEN(");
80416       sqlite3ExplainExpr(pOut, pX);
80417       sqlite3ExplainPrintf(pOut, ",");
80418       sqlite3ExplainExpr(pOut, pY);
80419       sqlite3ExplainPrintf(pOut, ",");
80420       sqlite3ExplainExpr(pOut, pZ);
80421       sqlite3ExplainPrintf(pOut, ")");
80422       break;
80423     }
80424     case TK_TRIGGER: {
80425       /* If the opcode is TK_TRIGGER, then the expression is a reference
80426       ** to a column in the new.* or old.* pseudo-tables available to
80427       ** trigger programs. In this case Expr.iTable is set to 1 for the
80428       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
80429       ** is set to the column of the pseudo-table to read, or to -1 to
80430       ** read the rowid field.
80431       */
80432       sqlite3ExplainPrintf(pOut, "%s(%d)",
80433           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
80434       break;
80435     }
80436     case TK_CASE: {
80437       sqlite3ExplainPrintf(pOut, "CASE(");
80438       sqlite3ExplainExpr(pOut, pExpr->pLeft);
80439       sqlite3ExplainPrintf(pOut, ",");
80440       sqlite3ExplainExprList(pOut, pExpr->x.pList);
80441       break;
80442     }
80443 #ifndef SQLITE_OMIT_TRIGGER
80444     case TK_RAISE: {
80445       const char *zType = "unk";
80446       switch( pExpr->affinity ){
80447         case OE_Rollback:   zType = "rollback";  break;
80448         case OE_Abort:      zType = "abort";     break;
80449         case OE_Fail:       zType = "fail";      break;
80450         case OE_Ignore:     zType = "ignore";    break;
80451       }
80452       sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
80453       break;
80454     }
80455 #endif
80456   }
80457   if( zBinOp ){
80458     sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
80459     sqlite3ExplainExpr(pOut, pExpr->pLeft);
80460     sqlite3ExplainPrintf(pOut,",");
80461     sqlite3ExplainExpr(pOut, pExpr->pRight);
80462     sqlite3ExplainPrintf(pOut,")");
80463   }else if( zUniOp ){
80464     sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
80465     sqlite3ExplainExpr(pOut, pExpr->pLeft);
80466     sqlite3ExplainPrintf(pOut,")");
80467   }
80468 }
80469 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
80470 
80471 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
80472 /*
80473 ** Generate a human-readable explanation of an expression list.
80474 */
80475 SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
80476   int i;
80477   if( pList==0 || pList->nExpr==0 ){
80478     sqlite3ExplainPrintf(pOut, "(empty-list)");
80479     return;
80480   }else if( pList->nExpr==1 ){
80481     sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
80482   }else{
80483     sqlite3ExplainPush(pOut);
80484     for(i=0; i<pList->nExpr; i++){
80485       sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
80486       sqlite3ExplainPush(pOut);
80487       sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
80488       sqlite3ExplainPop(pOut);
80489       if( pList->a[i].zName ){
80490         sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
80491       }
80492       if( pList->a[i].bSpanIsTab ){
80493         sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
80494       }
80495       if( i<pList->nExpr-1 ){
80496         sqlite3ExplainNL(pOut);
80497       }
80498     }
80499     sqlite3ExplainPop(pOut);
80500   }
80501 }
80502 #endif /* SQLITE_DEBUG */
80503 
80504 /*
80505 ** Generate code that pushes the value of every element of the given
80506 ** expression list into a sequence of registers beginning at target.
80507 **
80508 ** Return the number of elements evaluated.
80509 **
80510 ** The SQLITE_ECEL_DUP flag prevents the arguments from being
80511 ** filled using OP_SCopy.  OP_Copy must be used instead.
80512 **
80513 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
80514 ** factored out into initialization code.
80515 */
80516 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
80517   Parse *pParse,     /* Parsing context */
80518   ExprList *pList,   /* The expression list to be coded */
80519   int target,        /* Where to write results */
80520   u8 flags           /* SQLITE_ECEL_* flags */
80521 ){
80522   struct ExprList_item *pItem;
80523   int i, n;
80524   u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
80525   assert( pList!=0 );
80526   assert( target>0 );
80527   assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
80528   n = pList->nExpr;
80529   if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
80530   for(pItem=pList->a, i=0; i<n; i++, pItem++){
80531     Expr *pExpr = pItem->pExpr;
80532     if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
80533       sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
80534     }else{
80535       int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
80536       if( inReg!=target+i ){
80537         VdbeOp *pOp;
80538         Vdbe *v = pParse->pVdbe;
80539         if( copyOp==OP_Copy
80540          && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
80541          && pOp->p1+pOp->p3+1==inReg
80542          && pOp->p2+pOp->p3+1==target+i
80543         ){
80544           pOp->p3++;
80545         }else{
80546           sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
80547         }
80548       }
80549     }
80550   }
80551   return n;
80552 }
80553 
80554 /*
80555 ** Generate code for a BETWEEN operator.
80556 **
80557 **    x BETWEEN y AND z
80558 **
80559 ** The above is equivalent to
80560 **
80561 **    x>=y AND x<=z
80562 **
80563 ** Code it as such, taking care to do the common subexpression
80564 ** elementation of x.
80565 */
80566 static void exprCodeBetween(
80567   Parse *pParse,    /* Parsing and code generating context */
80568   Expr *pExpr,      /* The BETWEEN expression */
80569   int dest,         /* Jump here if the jump is taken */
80570   int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
80571   int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
80572 ){
80573   Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
80574   Expr compLeft;    /* The  x>=y  term */
80575   Expr compRight;   /* The  x<=z  term */
80576   Expr exprX;       /* The  x  subexpression */
80577   int regFree1 = 0; /* Temporary use register */
80578 
80579   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
80580   exprX = *pExpr->pLeft;
80581   exprAnd.op = TK_AND;
80582   exprAnd.pLeft = &compLeft;
80583   exprAnd.pRight = &compRight;
80584   compLeft.op = TK_GE;
80585   compLeft.pLeft = &exprX;
80586   compLeft.pRight = pExpr->x.pList->a[0].pExpr;
80587   compRight.op = TK_LE;
80588   compRight.pLeft = &exprX;
80589   compRight.pRight = pExpr->x.pList->a[1].pExpr;
80590   exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
80591   if( jumpIfTrue ){
80592     sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
80593   }else{
80594     sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
80595   }
80596   sqlite3ReleaseTempReg(pParse, regFree1);
80597 
80598   /* Ensure adequate test coverage */
80599   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
80600   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
80601   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
80602   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
80603   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
80604   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
80605   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
80606   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
80607 }
80608 
80609 /*
80610 ** Generate code for a boolean expression such that a jump is made
80611 ** to the label "dest" if the expression is true but execution
80612 ** continues straight thru if the expression is false.
80613 **
80614 ** If the expression evaluates to NULL (neither true nor false), then
80615 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
80616 **
80617 ** This code depends on the fact that certain token values (ex: TK_EQ)
80618 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
80619 ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
80620 ** the make process cause these values to align.  Assert()s in the code
80621 ** below verify that the numbers are aligned correctly.
80622 */
80623 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
80624   Vdbe *v = pParse->pVdbe;
80625   int op = 0;
80626   int regFree1 = 0;
80627   int regFree2 = 0;
80628   int r1, r2;
80629 
80630   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
80631   if( NEVER(v==0) )     return;  /* Existence of VDBE checked by caller */
80632   if( NEVER(pExpr==0) ) return;  /* No way this can happen */
80633   op = pExpr->op;
80634   switch( op ){
80635     case TK_AND: {
80636       int d2 = sqlite3VdbeMakeLabel(v);
80637       testcase( jumpIfNull==0 );
80638       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
80639       sqlite3ExprCachePush(pParse);
80640       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
80641       sqlite3VdbeResolveLabel(v, d2);
80642       sqlite3ExprCachePop(pParse, 1);
80643       break;
80644     }
80645     case TK_OR: {
80646       testcase( jumpIfNull==0 );
80647       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
80648       sqlite3ExprCachePush(pParse);
80649       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
80650       sqlite3ExprCachePop(pParse, 1);
80651       break;
80652     }
80653     case TK_NOT: {
80654       testcase( jumpIfNull==0 );
80655       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
80656       break;
80657     }
80658     case TK_LT:
80659     case TK_LE:
80660     case TK_GT:
80661     case TK_GE:
80662     case TK_NE:
80663     case TK_EQ: {
80664       testcase( jumpIfNull==0 );
80665       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80666       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80667       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80668                   r1, r2, dest, jumpIfNull);
80669       assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
80670       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
80671       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
80672       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
80673       assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
80674       assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
80675       testcase( regFree1==0 );
80676       testcase( regFree2==0 );
80677       break;
80678     }
80679     case TK_IS:
80680     case TK_ISNOT: {
80681       testcase( op==TK_IS );
80682       testcase( op==TK_ISNOT );
80683       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80684       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80685       op = (op==TK_IS) ? TK_EQ : TK_NE;
80686       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80687                   r1, r2, dest, SQLITE_NULLEQ);
80688       VdbeCoverageIf(v, op==TK_EQ);
80689       VdbeCoverageIf(v, op==TK_NE);
80690       testcase( regFree1==0 );
80691       testcase( regFree2==0 );
80692       break;
80693     }
80694     case TK_ISNULL:
80695     case TK_NOTNULL: {
80696       assert( TK_ISNULL==OP_IsNull );   testcase( op==TK_ISNULL );
80697       assert( TK_NOTNULL==OP_NotNull ); testcase( op==TK_NOTNULL );
80698       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80699       sqlite3VdbeAddOp2(v, op, r1, dest);
80700       VdbeCoverageIf(v, op==TK_ISNULL);
80701       VdbeCoverageIf(v, op==TK_NOTNULL);
80702       testcase( regFree1==0 );
80703       break;
80704     }
80705     case TK_BETWEEN: {
80706       testcase( jumpIfNull==0 );
80707       exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
80708       break;
80709     }
80710 #ifndef SQLITE_OMIT_SUBQUERY
80711     case TK_IN: {
80712       int destIfFalse = sqlite3VdbeMakeLabel(v);
80713       int destIfNull = jumpIfNull ? dest : destIfFalse;
80714       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
80715       sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
80716       sqlite3VdbeResolveLabel(v, destIfFalse);
80717       break;
80718     }
80719 #endif
80720     default: {
80721       if( exprAlwaysTrue(pExpr) ){
80722         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
80723       }else if( exprAlwaysFalse(pExpr) ){
80724         /* No-op */
80725       }else{
80726         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
80727         sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
80728         VdbeCoverage(v);
80729         testcase( regFree1==0 );
80730         testcase( jumpIfNull==0 );
80731       }
80732       break;
80733     }
80734   }
80735   sqlite3ReleaseTempReg(pParse, regFree1);
80736   sqlite3ReleaseTempReg(pParse, regFree2);
80737 }
80738 
80739 /*
80740 ** Generate code for a boolean expression such that a jump is made
80741 ** to the label "dest" if the expression is false but execution
80742 ** continues straight thru if the expression is true.
80743 **
80744 ** If the expression evaluates to NULL (neither true nor false) then
80745 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
80746 ** is 0.
80747 */
80748 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
80749   Vdbe *v = pParse->pVdbe;
80750   int op = 0;
80751   int regFree1 = 0;
80752   int regFree2 = 0;
80753   int r1, r2;
80754 
80755   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
80756   if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
80757   if( pExpr==0 )    return;
80758 
80759   /* The value of pExpr->op and op are related as follows:
80760   **
80761   **       pExpr->op            op
80762   **       ---------          ----------
80763   **       TK_ISNULL          OP_NotNull
80764   **       TK_NOTNULL         OP_IsNull
80765   **       TK_NE              OP_Eq
80766   **       TK_EQ              OP_Ne
80767   **       TK_GT              OP_Le
80768   **       TK_LE              OP_Gt
80769   **       TK_GE              OP_Lt
80770   **       TK_LT              OP_Ge
80771   **
80772   ** For other values of pExpr->op, op is undefined and unused.
80773   ** The value of TK_ and OP_ constants are arranged such that we
80774   ** can compute the mapping above using the following expression.
80775   ** Assert()s verify that the computation is correct.
80776   */
80777   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
80778 
80779   /* Verify correct alignment of TK_ and OP_ constants
80780   */
80781   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
80782   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
80783   assert( pExpr->op!=TK_NE || op==OP_Eq );
80784   assert( pExpr->op!=TK_EQ || op==OP_Ne );
80785   assert( pExpr->op!=TK_LT || op==OP_Ge );
80786   assert( pExpr->op!=TK_LE || op==OP_Gt );
80787   assert( pExpr->op!=TK_GT || op==OP_Le );
80788   assert( pExpr->op!=TK_GE || op==OP_Lt );
80789 
80790   switch( pExpr->op ){
80791     case TK_AND: {
80792       testcase( jumpIfNull==0 );
80793       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
80794       sqlite3ExprCachePush(pParse);
80795       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
80796       sqlite3ExprCachePop(pParse, 1);
80797       break;
80798     }
80799     case TK_OR: {
80800       int d2 = sqlite3VdbeMakeLabel(v);
80801       testcase( jumpIfNull==0 );
80802       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
80803       sqlite3ExprCachePush(pParse);
80804       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
80805       sqlite3VdbeResolveLabel(v, d2);
80806       sqlite3ExprCachePop(pParse, 1);
80807       break;
80808     }
80809     case TK_NOT: {
80810       testcase( jumpIfNull==0 );
80811       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
80812       break;
80813     }
80814     case TK_LT:
80815     case TK_LE:
80816     case TK_GT:
80817     case TK_GE:
80818     case TK_NE:
80819     case TK_EQ: {
80820       testcase( jumpIfNull==0 );
80821       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80822       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80823       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80824                   r1, r2, dest, jumpIfNull);
80825       assert(TK_LT==OP_Lt); testcase(op==OP_Lt); VdbeCoverageIf(v,op==OP_Lt);
80826       assert(TK_LE==OP_Le); testcase(op==OP_Le); VdbeCoverageIf(v,op==OP_Le);
80827       assert(TK_GT==OP_Gt); testcase(op==OP_Gt); VdbeCoverageIf(v,op==OP_Gt);
80828       assert(TK_GE==OP_Ge); testcase(op==OP_Ge); VdbeCoverageIf(v,op==OP_Ge);
80829       assert(TK_EQ==OP_Eq); testcase(op==OP_Eq); VdbeCoverageIf(v,op==OP_Eq);
80830       assert(TK_NE==OP_Ne); testcase(op==OP_Ne); VdbeCoverageIf(v,op==OP_Ne);
80831       testcase( regFree1==0 );
80832       testcase( regFree2==0 );
80833       break;
80834     }
80835     case TK_IS:
80836     case TK_ISNOT: {
80837       testcase( pExpr->op==TK_IS );
80838       testcase( pExpr->op==TK_ISNOT );
80839       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80840       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80841       op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
80842       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80843                   r1, r2, dest, SQLITE_NULLEQ);
80844       VdbeCoverageIf(v, op==TK_EQ);
80845       VdbeCoverageIf(v, op==TK_NE);
80846       testcase( regFree1==0 );
80847       testcase( regFree2==0 );
80848       break;
80849     }
80850     case TK_ISNULL:
80851     case TK_NOTNULL: {
80852       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80853       sqlite3VdbeAddOp2(v, op, r1, dest);
80854       testcase( op==TK_ISNULL );   VdbeCoverageIf(v, op==TK_ISNULL);
80855       testcase( op==TK_NOTNULL );  VdbeCoverageIf(v, op==TK_NOTNULL);
80856       testcase( regFree1==0 );
80857       break;
80858     }
80859     case TK_BETWEEN: {
80860       testcase( jumpIfNull==0 );
80861       exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
80862       break;
80863     }
80864 #ifndef SQLITE_OMIT_SUBQUERY
80865     case TK_IN: {
80866       if( jumpIfNull ){
80867         sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
80868       }else{
80869         int destIfNull = sqlite3VdbeMakeLabel(v);
80870         sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
80871         sqlite3VdbeResolveLabel(v, destIfNull);
80872       }
80873       break;
80874     }
80875 #endif
80876     default: {
80877       if( exprAlwaysFalse(pExpr) ){
80878         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
80879       }else if( exprAlwaysTrue(pExpr) ){
80880         /* no-op */
80881       }else{
80882         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
80883         sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
80884         VdbeCoverage(v);
80885         testcase( regFree1==0 );
80886         testcase( jumpIfNull==0 );
80887       }
80888       break;
80889     }
80890   }
80891   sqlite3ReleaseTempReg(pParse, regFree1);
80892   sqlite3ReleaseTempReg(pParse, regFree2);
80893 }
80894 
80895 /*
80896 ** Do a deep comparison of two expression trees.  Return 0 if the two
80897 ** expressions are completely identical.  Return 1 if they differ only
80898 ** by a COLLATE operator at the top level.  Return 2 if there are differences
80899 ** other than the top-level COLLATE operator.
80900 **
80901 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
80902 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
80903 **
80904 ** The pA side might be using TK_REGISTER.  If that is the case and pB is
80905 ** not using TK_REGISTER but is otherwise equivalent, then still return 0.
80906 **
80907 ** Sometimes this routine will return 2 even if the two expressions
80908 ** really are equivalent.  If we cannot prove that the expressions are
80909 ** identical, we return 2 just to be safe.  So if this routine
80910 ** returns 2, then you do not really know for certain if the two
80911 ** expressions are the same.  But if you get a 0 or 1 return, then you
80912 ** can be sure the expressions are the same.  In the places where
80913 ** this routine is used, it does not hurt to get an extra 2 - that
80914 ** just might result in some slightly slower code.  But returning
80915 ** an incorrect 0 or 1 could lead to a malfunction.
80916 */
80917 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
80918   u32 combinedFlags;
80919   if( pA==0 || pB==0 ){
80920     return pB==pA ? 0 : 2;
80921   }
80922   combinedFlags = pA->flags | pB->flags;
80923   if( combinedFlags & EP_IntValue ){
80924     if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
80925       return 0;
80926     }
80927     return 2;
80928   }
80929   if( pA->op!=pB->op ){
80930     if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
80931       return 1;
80932     }
80933     if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
80934       return 1;
80935     }
80936     return 2;
80937   }
80938   if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
80939     if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
80940       return pA->op==TK_COLLATE ? 1 : 2;
80941     }
80942   }
80943   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
80944   if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
80945     if( combinedFlags & EP_xIsSelect ) return 2;
80946     if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
80947     if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
80948     if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
80949     if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
80950       if( pA->iColumn!=pB->iColumn ) return 2;
80951       if( pA->iTable!=pB->iTable
80952        && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
80953     }
80954   }
80955   return 0;
80956 }
80957 
80958 /*
80959 ** Compare two ExprList objects.  Return 0 if they are identical and
80960 ** non-zero if they differ in any way.
80961 **
80962 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
80963 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
80964 **
80965 ** This routine might return non-zero for equivalent ExprLists.  The
80966 ** only consequence will be disabled optimizations.  But this routine
80967 ** must never return 0 if the two ExprList objects are different, or
80968 ** a malfunction will result.
80969 **
80970 ** Two NULL pointers are considered to be the same.  But a NULL pointer
80971 ** always differs from a non-NULL pointer.
80972 */
80973 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
80974   int i;
80975   if( pA==0 && pB==0 ) return 0;
80976   if( pA==0 || pB==0 ) return 1;
80977   if( pA->nExpr!=pB->nExpr ) return 1;
80978   for(i=0; i<pA->nExpr; i++){
80979     Expr *pExprA = pA->a[i].pExpr;
80980     Expr *pExprB = pB->a[i].pExpr;
80981     if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
80982     if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
80983   }
80984   return 0;
80985 }
80986 
80987 /*
80988 ** Return true if we can prove the pE2 will always be true if pE1 is
80989 ** true.  Return false if we cannot complete the proof or if pE2 might
80990 ** be false.  Examples:
80991 **
80992 **     pE1: x==5       pE2: x==5             Result: true
80993 **     pE1: x>0        pE2: x==5             Result: false
80994 **     pE1: x=21       pE2: x=21 OR y=43     Result: true
80995 **     pE1: x!=123     pE2: x IS NOT NULL    Result: true
80996 **     pE1: x!=?1      pE2: x IS NOT NULL    Result: true
80997 **     pE1: x IS NULL  pE2: x IS NOT NULL    Result: false
80998 **     pE1: x IS ?2    pE2: x IS NOT NULL    Reuslt: false
80999 **
81000 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
81001 ** Expr.iTable<0 then assume a table number given by iTab.
81002 **
81003 ** When in doubt, return false.  Returning true might give a performance
81004 ** improvement.  Returning false might cause a performance reduction, but
81005 ** it will always give the correct answer and is hence always safe.
81006 */
81007 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
81008   if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
81009     return 1;
81010   }
81011   if( pE2->op==TK_OR
81012    && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
81013              || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
81014   ){
81015     return 1;
81016   }
81017   if( pE2->op==TK_NOTNULL
81018    && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
81019    && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
81020   ){
81021     return 1;
81022   }
81023   return 0;
81024 }
81025 
81026 /*
81027 ** An instance of the following structure is used by the tree walker
81028 ** to count references to table columns in the arguments of an
81029 ** aggregate function, in order to implement the
81030 ** sqlite3FunctionThisSrc() routine.
81031 */
81032 struct SrcCount {
81033   SrcList *pSrc;   /* One particular FROM clause in a nested query */
81034   int nThis;       /* Number of references to columns in pSrcList */
81035   int nOther;      /* Number of references to columns in other FROM clauses */
81036 };
81037 
81038 /*
81039 ** Count the number of references to columns.
81040 */
81041 static int exprSrcCount(Walker *pWalker, Expr *pExpr){
81042   /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
81043   ** is always called before sqlite3ExprAnalyzeAggregates() and so the
81044   ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
81045   ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
81046   ** NEVER() will need to be removed. */
81047   if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
81048     int i;
81049     struct SrcCount *p = pWalker->u.pSrcCount;
81050     SrcList *pSrc = p->pSrc;
81051     for(i=0; i<pSrc->nSrc; i++){
81052       if( pExpr->iTable==pSrc->a[i].iCursor ) break;
81053     }
81054     if( i<pSrc->nSrc ){
81055       p->nThis++;
81056     }else{
81057       p->nOther++;
81058     }
81059   }
81060   return WRC_Continue;
81061 }
81062 
81063 /*
81064 ** Determine if any of the arguments to the pExpr Function reference
81065 ** pSrcList.  Return true if they do.  Also return true if the function
81066 ** has no arguments or has only constant arguments.  Return false if pExpr
81067 ** references columns but not columns of tables found in pSrcList.
81068 */
81069 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
81070   Walker w;
81071   struct SrcCount cnt;
81072   assert( pExpr->op==TK_AGG_FUNCTION );
81073   memset(&w, 0, sizeof(w));
81074   w.xExprCallback = exprSrcCount;
81075   w.u.pSrcCount = &cnt;
81076   cnt.pSrc = pSrcList;
81077   cnt.nThis = 0;
81078   cnt.nOther = 0;
81079   sqlite3WalkExprList(&w, pExpr->x.pList);
81080   return cnt.nThis>0 || cnt.nOther==0;
81081 }
81082 
81083 /*
81084 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
81085 ** the new element.  Return a negative number if malloc fails.
81086 */
81087 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
81088   int i;
81089   pInfo->aCol = sqlite3ArrayAllocate(
81090        db,
81091        pInfo->aCol,
81092        sizeof(pInfo->aCol[0]),
81093        &pInfo->nColumn,
81094        &i
81095   );
81096   return i;
81097 }
81098 
81099 /*
81100 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
81101 ** the new element.  Return a negative number if malloc fails.
81102 */
81103 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
81104   int i;
81105   pInfo->aFunc = sqlite3ArrayAllocate(
81106        db,
81107        pInfo->aFunc,
81108        sizeof(pInfo->aFunc[0]),
81109        &pInfo->nFunc,
81110        &i
81111   );
81112   return i;
81113 }
81114 
81115 /*
81116 ** This is the xExprCallback for a tree walker.  It is used to
81117 ** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
81118 ** for additional information.
81119 */
81120 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
81121   int i;
81122   NameContext *pNC = pWalker->u.pNC;
81123   Parse *pParse = pNC->pParse;
81124   SrcList *pSrcList = pNC->pSrcList;
81125   AggInfo *pAggInfo = pNC->pAggInfo;
81126 
81127   switch( pExpr->op ){
81128     case TK_AGG_COLUMN:
81129     case TK_COLUMN: {
81130       testcase( pExpr->op==TK_AGG_COLUMN );
81131       testcase( pExpr->op==TK_COLUMN );
81132       /* Check to see if the column is in one of the tables in the FROM
81133       ** clause of the aggregate query */
81134       if( ALWAYS(pSrcList!=0) ){
81135         struct SrcList_item *pItem = pSrcList->a;
81136         for(i=0; i<pSrcList->nSrc; i++, pItem++){
81137           struct AggInfo_col *pCol;
81138           assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
81139           if( pExpr->iTable==pItem->iCursor ){
81140             /* If we reach this point, it means that pExpr refers to a table
81141             ** that is in the FROM clause of the aggregate query.
81142             **
81143             ** Make an entry for the column in pAggInfo->aCol[] if there
81144             ** is not an entry there already.
81145             */
81146             int k;
81147             pCol = pAggInfo->aCol;
81148             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
81149               if( pCol->iTable==pExpr->iTable &&
81150                   pCol->iColumn==pExpr->iColumn ){
81151                 break;
81152               }
81153             }
81154             if( (k>=pAggInfo->nColumn)
81155              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
81156             ){
81157               pCol = &pAggInfo->aCol[k];
81158               pCol->pTab = pExpr->pTab;
81159               pCol->iTable = pExpr->iTable;
81160               pCol->iColumn = pExpr->iColumn;
81161               pCol->iMem = ++pParse->nMem;
81162               pCol->iSorterColumn = -1;
81163               pCol->pExpr = pExpr;
81164               if( pAggInfo->pGroupBy ){
81165                 int j, n;
81166                 ExprList *pGB = pAggInfo->pGroupBy;
81167                 struct ExprList_item *pTerm = pGB->a;
81168                 n = pGB->nExpr;
81169                 for(j=0; j<n; j++, pTerm++){
81170                   Expr *pE = pTerm->pExpr;
81171                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
81172                       pE->iColumn==pExpr->iColumn ){
81173                     pCol->iSorterColumn = j;
81174                     break;
81175                   }
81176                 }
81177               }
81178               if( pCol->iSorterColumn<0 ){
81179                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
81180               }
81181             }
81182             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
81183             ** because it was there before or because we just created it).
81184             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
81185             ** pAggInfo->aCol[] entry.
81186             */
81187             ExprSetVVAProperty(pExpr, EP_NoReduce);
81188             pExpr->pAggInfo = pAggInfo;
81189             pExpr->op = TK_AGG_COLUMN;
81190             pExpr->iAgg = (i16)k;
81191             break;
81192           } /* endif pExpr->iTable==pItem->iCursor */
81193         } /* end loop over pSrcList */
81194       }
81195       return WRC_Prune;
81196     }
81197     case TK_AGG_FUNCTION: {
81198       if( (pNC->ncFlags & NC_InAggFunc)==0
81199        && pWalker->walkerDepth==pExpr->op2
81200       ){
81201         /* Check to see if pExpr is a duplicate of another aggregate
81202         ** function that is already in the pAggInfo structure
81203         */
81204         struct AggInfo_func *pItem = pAggInfo->aFunc;
81205         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
81206           if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
81207             break;
81208           }
81209         }
81210         if( i>=pAggInfo->nFunc ){
81211           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
81212           */
81213           u8 enc = ENC(pParse->db);
81214           i = addAggInfoFunc(pParse->db, pAggInfo);
81215           if( i>=0 ){
81216             assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
81217             pItem = &pAggInfo->aFunc[i];
81218             pItem->pExpr = pExpr;
81219             pItem->iMem = ++pParse->nMem;
81220             assert( !ExprHasProperty(pExpr, EP_IntValue) );
81221             pItem->pFunc = sqlite3FindFunction(pParse->db,
81222                    pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
81223                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
81224             if( pExpr->flags & EP_Distinct ){
81225               pItem->iDistinct = pParse->nTab++;
81226             }else{
81227               pItem->iDistinct = -1;
81228             }
81229           }
81230         }
81231         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
81232         */
81233         assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
81234         ExprSetVVAProperty(pExpr, EP_NoReduce);
81235         pExpr->iAgg = (i16)i;
81236         pExpr->pAggInfo = pAggInfo;
81237         return WRC_Prune;
81238       }else{
81239         return WRC_Continue;
81240       }
81241     }
81242   }
81243   return WRC_Continue;
81244 }
81245 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
81246   UNUSED_PARAMETER(pWalker);
81247   UNUSED_PARAMETER(pSelect);
81248   return WRC_Continue;
81249 }
81250 
81251 /*
81252 ** Analyze the pExpr expression looking for aggregate functions and
81253 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
81254 ** points to.  Additional entries are made on the AggInfo object as
81255 ** necessary.
81256 **
81257 ** This routine should only be called after the expression has been
81258 ** analyzed by sqlite3ResolveExprNames().
81259 */
81260 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
81261   Walker w;
81262   memset(&w, 0, sizeof(w));
81263   w.xExprCallback = analyzeAggregate;
81264   w.xSelectCallback = analyzeAggregatesInSelect;
81265   w.u.pNC = pNC;
81266   assert( pNC->pSrcList!=0 );
81267   sqlite3WalkExpr(&w, pExpr);
81268 }
81269 
81270 /*
81271 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
81272 ** expression list.  Return the number of errors.
81273 **
81274 ** If an error is found, the analysis is cut short.
81275 */
81276 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
81277   struct ExprList_item *pItem;
81278   int i;
81279   if( pList ){
81280     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
81281       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
81282     }
81283   }
81284 }
81285 
81286 /*
81287 ** Allocate a single new register for use to hold some intermediate result.
81288 */
81289 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
81290   if( pParse->nTempReg==0 ){
81291     return ++pParse->nMem;
81292   }
81293   return pParse->aTempReg[--pParse->nTempReg];
81294 }
81295 
81296 /*
81297 ** Deallocate a register, making available for reuse for some other
81298 ** purpose.
81299 **
81300 ** If a register is currently being used by the column cache, then
81301 ** the dallocation is deferred until the column cache line that uses
81302 ** the register becomes stale.
81303 */
81304 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
81305   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
81306     int i;
81307     struct yColCache *p;
81308     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
81309       if( p->iReg==iReg ){
81310         p->tempReg = 1;
81311         return;
81312       }
81313     }
81314     pParse->aTempReg[pParse->nTempReg++] = iReg;
81315   }
81316 }
81317 
81318 /*
81319 ** Allocate or deallocate a block of nReg consecutive registers
81320 */
81321 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
81322   int i, n;
81323   i = pParse->iRangeReg;
81324   n = pParse->nRangeReg;
81325   if( nReg<=n ){
81326     assert( !usedAsColumnCache(pParse, i, i+n-1) );
81327     pParse->iRangeReg += nReg;
81328     pParse->nRangeReg -= nReg;
81329   }else{
81330     i = pParse->nMem+1;
81331     pParse->nMem += nReg;
81332   }
81333   return i;
81334 }
81335 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
81336   sqlite3ExprCacheRemove(pParse, iReg, nReg);
81337   if( nReg>pParse->nRangeReg ){
81338     pParse->nRangeReg = nReg;
81339     pParse->iRangeReg = iReg;
81340   }
81341 }
81342 
81343 /*
81344 ** Mark all temporary registers as being unavailable for reuse.
81345 */
81346 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
81347   pParse->nTempReg = 0;
81348   pParse->nRangeReg = 0;
81349 }
81350 
81351 /************** End of expr.c ************************************************/
81352 /************** Begin file alter.c *******************************************/
81353 /*
81354 ** 2005 February 15
81355 **
81356 ** The author disclaims copyright to this source code.  In place of
81357 ** a legal notice, here is a blessing:
81358 **
81359 **    May you do good and not evil.
81360 **    May you find forgiveness for yourself and forgive others.
81361 **    May you share freely, never taking more than you give.
81362 **
81363 *************************************************************************
81364 ** This file contains C code routines that used to generate VDBE code
81365 ** that implements the ALTER TABLE command.
81366 */
81367 
81368 /*
81369 ** The code in this file only exists if we are not omitting the
81370 ** ALTER TABLE logic from the build.
81371 */
81372 #ifndef SQLITE_OMIT_ALTERTABLE
81373 
81374 
81375 /*
81376 ** This function is used by SQL generated to implement the
81377 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
81378 ** CREATE INDEX command. The second is a table name. The table name in
81379 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
81380 ** argument and the result returned. Examples:
81381 **
81382 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
81383 **     -> 'CREATE TABLE def(a, b, c)'
81384 **
81385 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
81386 **     -> 'CREATE INDEX i ON def(a, b, c)'
81387 */
81388 static void renameTableFunc(
81389   sqlite3_context *context,
81390   int NotUsed,
81391   sqlite3_value **argv
81392 ){
81393   unsigned char const *zSql = sqlite3_value_text(argv[0]);
81394   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
81395 
81396   int token;
81397   Token tname;
81398   unsigned char const *zCsr = zSql;
81399   int len = 0;
81400   char *zRet;
81401 
81402   sqlite3 *db = sqlite3_context_db_handle(context);
81403 
81404   UNUSED_PARAMETER(NotUsed);
81405 
81406   /* The principle used to locate the table name in the CREATE TABLE
81407   ** statement is that the table name is the first non-space token that
81408   ** is immediately followed by a TK_LP or TK_USING token.
81409   */
81410   if( zSql ){
81411     do {
81412       if( !*zCsr ){
81413         /* Ran out of input before finding an opening bracket. Return NULL. */
81414         return;
81415       }
81416 
81417       /* Store the token that zCsr points to in tname. */
81418       tname.z = (char*)zCsr;
81419       tname.n = len;
81420 
81421       /* Advance zCsr to the next token. Store that token type in 'token',
81422       ** and its length in 'len' (to be used next iteration of this loop).
81423       */
81424       do {
81425         zCsr += len;
81426         len = sqlite3GetToken(zCsr, &token);
81427       } while( token==TK_SPACE );
81428       assert( len>0 );
81429     } while( token!=TK_LP && token!=TK_USING );
81430 
81431     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
81432        zSql, zTableName, tname.z+tname.n);
81433     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
81434   }
81435 }
81436 
81437 /*
81438 ** This C function implements an SQL user function that is used by SQL code
81439 ** generated by the ALTER TABLE ... RENAME command to modify the definition
81440 ** of any foreign key constraints that use the table being renamed as the
81441 ** parent table. It is passed three arguments:
81442 **
81443 **   1) The complete text of the CREATE TABLE statement being modified,
81444 **   2) The old name of the table being renamed, and
81445 **   3) The new name of the table being renamed.
81446 **
81447 ** It returns the new CREATE TABLE statement. For example:
81448 **
81449 **   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
81450 **       -> 'CREATE TABLE t1(a REFERENCES t3)'
81451 */
81452 #ifndef SQLITE_OMIT_FOREIGN_KEY
81453 static void renameParentFunc(
81454   sqlite3_context *context,
81455   int NotUsed,
81456   sqlite3_value **argv
81457 ){
81458   sqlite3 *db = sqlite3_context_db_handle(context);
81459   char *zOutput = 0;
81460   char *zResult;
81461   unsigned char const *zInput = sqlite3_value_text(argv[0]);
81462   unsigned char const *zOld = sqlite3_value_text(argv[1]);
81463   unsigned char const *zNew = sqlite3_value_text(argv[2]);
81464 
81465   unsigned const char *z;         /* Pointer to token */
81466   int n;                          /* Length of token z */
81467   int token;                      /* Type of token */
81468 
81469   UNUSED_PARAMETER(NotUsed);
81470   for(z=zInput; *z; z=z+n){
81471     n = sqlite3GetToken(z, &token);
81472     if( token==TK_REFERENCES ){
81473       char *zParent;
81474       do {
81475         z += n;
81476         n = sqlite3GetToken(z, &token);
81477       }while( token==TK_SPACE );
81478 
81479       zParent = sqlite3DbStrNDup(db, (const char *)z, n);
81480       if( zParent==0 ) break;
81481       sqlite3Dequote(zParent);
81482       if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
81483         char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
81484             (zOutput?zOutput:""), (int)(z-zInput), zInput, (const char *)zNew
81485         );
81486         sqlite3DbFree(db, zOutput);
81487         zOutput = zOut;
81488         zInput = &z[n];
81489       }
81490       sqlite3DbFree(db, zParent);
81491     }
81492   }
81493 
81494   zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
81495   sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
81496   sqlite3DbFree(db, zOutput);
81497 }
81498 #endif
81499 
81500 #ifndef SQLITE_OMIT_TRIGGER
81501 /* This function is used by SQL generated to implement the
81502 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
81503 ** statement. The second is a table name. The table name in the CREATE
81504 ** TRIGGER statement is replaced with the third argument and the result
81505 ** returned. This is analagous to renameTableFunc() above, except for CREATE
81506 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
81507 */
81508 static void renameTriggerFunc(
81509   sqlite3_context *context,
81510   int NotUsed,
81511   sqlite3_value **argv
81512 ){
81513   unsigned char const *zSql = sqlite3_value_text(argv[0]);
81514   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
81515 
81516   int token;
81517   Token tname;
81518   int dist = 3;
81519   unsigned char const *zCsr = zSql;
81520   int len = 0;
81521   char *zRet;
81522   sqlite3 *db = sqlite3_context_db_handle(context);
81523 
81524   UNUSED_PARAMETER(NotUsed);
81525 
81526   /* The principle used to locate the table name in the CREATE TRIGGER
81527   ** statement is that the table name is the first token that is immediatedly
81528   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
81529   ** of TK_WHEN, TK_BEGIN or TK_FOR.
81530   */
81531   if( zSql ){
81532     do {
81533 
81534       if( !*zCsr ){
81535         /* Ran out of input before finding the table name. Return NULL. */
81536         return;
81537       }
81538 
81539       /* Store the token that zCsr points to in tname. */
81540       tname.z = (char*)zCsr;
81541       tname.n = len;
81542 
81543       /* Advance zCsr to the next token. Store that token type in 'token',
81544       ** and its length in 'len' (to be used next iteration of this loop).
81545       */
81546       do {
81547         zCsr += len;
81548         len = sqlite3GetToken(zCsr, &token);
81549       }while( token==TK_SPACE );
81550       assert( len>0 );
81551 
81552       /* Variable 'dist' stores the number of tokens read since the most
81553       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
81554       ** token is read and 'dist' equals 2, the condition stated above
81555       ** to be met.
81556       **
81557       ** Note that ON cannot be a database, table or column name, so
81558       ** there is no need to worry about syntax like
81559       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
81560       */
81561       dist++;
81562       if( token==TK_DOT || token==TK_ON ){
81563         dist = 0;
81564       }
81565     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
81566 
81567     /* Variable tname now contains the token that is the old table-name
81568     ** in the CREATE TRIGGER statement.
81569     */
81570     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", (int)(((u8*)tname.z) - zSql),
81571        zSql, zTableName, tname.z+tname.n);
81572     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
81573   }
81574 }
81575 #endif   /* !SQLITE_OMIT_TRIGGER */
81576 
81577 /*
81578 ** Register built-in functions used to help implement ALTER TABLE
81579 */
81580 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
81581   static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
81582     FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
81583 #ifndef SQLITE_OMIT_TRIGGER
81584     FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
81585 #endif
81586 #ifndef SQLITE_OMIT_FOREIGN_KEY
81587     FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
81588 #endif
81589   };
81590   int i;
81591   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
81592   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
81593 
81594   for(i=0; i<ArraySize(aAlterTableFuncs); i++){
81595     sqlite3FuncDefInsert(pHash, &aFunc[i]);
81596   }
81597 }
81598 
81599 /*
81600 ** This function is used to create the text of expressions of the form:
81601 **
81602 **   name=<constant1> OR name=<constant2> OR ...
81603 **
81604 ** If argument zWhere is NULL, then a pointer string containing the text
81605 ** "name=<constant>" is returned, where <constant> is the quoted version
81606 ** of the string passed as argument zConstant. The returned buffer is
81607 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
81608 ** caller to ensure that it is eventually freed.
81609 **
81610 ** If argument zWhere is not NULL, then the string returned is
81611 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
81612 ** In this case zWhere is passed to sqlite3DbFree() before returning.
81613 **
81614 */
81615 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
81616   char *zNew;
81617   if( !zWhere ){
81618     zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
81619   }else{
81620     zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
81621     sqlite3DbFree(db, zWhere);
81622   }
81623   return zNew;
81624 }
81625 
81626 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
81627 /*
81628 ** Generate the text of a WHERE expression which can be used to select all
81629 ** tables that have foreign key constraints that refer to table pTab (i.e.
81630 ** constraints for which pTab is the parent table) from the sqlite_master
81631 ** table.
81632 */
81633 static char *whereForeignKeys(Parse *pParse, Table *pTab){
81634   FKey *p;
81635   char *zWhere = 0;
81636   for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
81637     zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
81638   }
81639   return zWhere;
81640 }
81641 #endif
81642 
81643 /*
81644 ** Generate the text of a WHERE expression which can be used to select all
81645 ** temporary triggers on table pTab from the sqlite_temp_master table. If
81646 ** table pTab has no temporary triggers, or is itself stored in the
81647 ** temporary database, NULL is returned.
81648 */
81649 static char *whereTempTriggers(Parse *pParse, Table *pTab){
81650   Trigger *pTrig;
81651   char *zWhere = 0;
81652   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
81653 
81654   /* If the table is not located in the temp-db (in which case NULL is
81655   ** returned, loop through the tables list of triggers. For each trigger
81656   ** that is not part of the temp-db schema, add a clause to the WHERE
81657   ** expression being built up in zWhere.
81658   */
81659   if( pTab->pSchema!=pTempSchema ){
81660     sqlite3 *db = pParse->db;
81661     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
81662       if( pTrig->pSchema==pTempSchema ){
81663         zWhere = whereOrName(db, zWhere, pTrig->zName);
81664       }
81665     }
81666   }
81667   if( zWhere ){
81668     char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
81669     sqlite3DbFree(pParse->db, zWhere);
81670     zWhere = zNew;
81671   }
81672   return zWhere;
81673 }
81674 
81675 /*
81676 ** Generate code to drop and reload the internal representation of table
81677 ** pTab from the database, including triggers and temporary triggers.
81678 ** Argument zName is the name of the table in the database schema at
81679 ** the time the generated code is executed. This can be different from
81680 ** pTab->zName if this function is being called to code part of an
81681 ** "ALTER TABLE RENAME TO" statement.
81682 */
81683 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
81684   Vdbe *v;
81685   char *zWhere;
81686   int iDb;                   /* Index of database containing pTab */
81687 #ifndef SQLITE_OMIT_TRIGGER
81688   Trigger *pTrig;
81689 #endif
81690 
81691   v = sqlite3GetVdbe(pParse);
81692   if( NEVER(v==0) ) return;
81693   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
81694   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
81695   assert( iDb>=0 );
81696 
81697 #ifndef SQLITE_OMIT_TRIGGER
81698   /* Drop any table triggers from the internal schema. */
81699   for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
81700     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
81701     assert( iTrigDb==iDb || iTrigDb==1 );
81702     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
81703   }
81704 #endif
81705 
81706   /* Drop the table and index from the internal schema.  */
81707   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81708 
81709   /* Reload the table, index and permanent trigger schemas. */
81710   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
81711   if( !zWhere ) return;
81712   sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
81713 
81714 #ifndef SQLITE_OMIT_TRIGGER
81715   /* Now, if the table is not stored in the temp database, reload any temp
81716   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
81717   */
81718   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
81719     sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
81720   }
81721 #endif
81722 }
81723 
81724 /*
81725 ** Parameter zName is the name of a table that is about to be altered
81726 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
81727 ** If the table is a system table, this function leaves an error message
81728 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
81729 **
81730 ** Or, if zName is not a system table, zero is returned.
81731 */
81732 static int isSystemTable(Parse *pParse, const char *zName){
81733   if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
81734     sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
81735     return 1;
81736   }
81737   return 0;
81738 }
81739 
81740 /*
81741 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
81742 ** command.
81743 */
81744 SQLITE_PRIVATE void sqlite3AlterRenameTable(
81745   Parse *pParse,            /* Parser context. */
81746   SrcList *pSrc,            /* The table to rename. */
81747   Token *pName              /* The new table name. */
81748 ){
81749   int iDb;                  /* Database that contains the table */
81750   char *zDb;                /* Name of database iDb */
81751   Table *pTab;              /* Table being renamed */
81752   char *zName = 0;          /* NULL-terminated version of pName */
81753   sqlite3 *db = pParse->db; /* Database connection */
81754   int nTabName;             /* Number of UTF-8 characters in zTabName */
81755   const char *zTabName;     /* Original name of the table */
81756   Vdbe *v;
81757 #ifndef SQLITE_OMIT_TRIGGER
81758   char *zWhere = 0;         /* Where clause to locate temp triggers */
81759 #endif
81760   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
81761   int savedDbFlags;         /* Saved value of db->flags */
81762 
81763   savedDbFlags = db->flags;
81764   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
81765   assert( pSrc->nSrc==1 );
81766   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
81767 
81768   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
81769   if( !pTab ) goto exit_rename_table;
81770   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
81771   zDb = db->aDb[iDb].zName;
81772   db->flags |= SQLITE_PreferBuiltin;
81773 
81774   /* Get a NULL terminated version of the new table name. */
81775   zName = sqlite3NameFromToken(db, pName);
81776   if( !zName ) goto exit_rename_table;
81777 
81778   /* Check that a table or index named 'zName' does not already exist
81779   ** in database iDb. If so, this is an error.
81780   */
81781   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
81782     sqlite3ErrorMsg(pParse,
81783         "there is already another table or index with this name: %s", zName);
81784     goto exit_rename_table;
81785   }
81786 
81787   /* Make sure it is not a system table being altered, or a reserved name
81788   ** that the table is being renamed to.
81789   */
81790   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
81791     goto exit_rename_table;
81792   }
81793   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
81794     exit_rename_table;
81795   }
81796 
81797 #ifndef SQLITE_OMIT_VIEW
81798   if( pTab->pSelect ){
81799     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
81800     goto exit_rename_table;
81801   }
81802 #endif
81803 
81804 #ifndef SQLITE_OMIT_AUTHORIZATION
81805   /* Invoke the authorization callback. */
81806   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
81807     goto exit_rename_table;
81808   }
81809 #endif
81810 
81811 #ifndef SQLITE_OMIT_VIRTUALTABLE
81812   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
81813     goto exit_rename_table;
81814   }
81815   if( IsVirtual(pTab) ){
81816     pVTab = sqlite3GetVTable(db, pTab);
81817     if( pVTab->pVtab->pModule->xRename==0 ){
81818       pVTab = 0;
81819     }
81820   }
81821 #endif
81822 
81823   /* Begin a transaction for database iDb.
81824   ** Then modify the schema cookie (since the ALTER TABLE modifies the
81825   ** schema). Open a statement transaction if the table is a virtual
81826   ** table.
81827   */
81828   v = sqlite3GetVdbe(pParse);
81829   if( v==0 ){
81830     goto exit_rename_table;
81831   }
81832   sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
81833   sqlite3ChangeCookie(pParse, iDb);
81834 
81835   /* If this is a virtual table, invoke the xRename() function if
81836   ** one is defined. The xRename() callback will modify the names
81837   ** of any resources used by the v-table implementation (including other
81838   ** SQLite tables) that are identified by the name of the virtual table.
81839   */
81840 #ifndef SQLITE_OMIT_VIRTUALTABLE
81841   if( pVTab ){
81842     int i = ++pParse->nMem;
81843     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
81844     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
81845     sqlite3MayAbort(pParse);
81846   }
81847 #endif
81848 
81849   /* figure out how many UTF-8 characters are in zName */
81850   zTabName = pTab->zName;
81851   nTabName = sqlite3Utf8CharLen(zTabName, -1);
81852 
81853 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
81854   if( db->flags&SQLITE_ForeignKeys ){
81855     /* If foreign-key support is enabled, rewrite the CREATE TABLE
81856     ** statements corresponding to all child tables of foreign key constraints
81857     ** for which the renamed table is the parent table.  */
81858     if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
81859       sqlite3NestedParse(pParse,
81860           "UPDATE \"%w\".%s SET "
81861               "sql = sqlite_rename_parent(sql, %Q, %Q) "
81862               "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
81863       sqlite3DbFree(db, zWhere);
81864     }
81865   }
81866 #endif
81867 
81868   /* Modify the sqlite_master table to use the new table name. */
81869   sqlite3NestedParse(pParse,
81870       "UPDATE %Q.%s SET "
81871 #ifdef SQLITE_OMIT_TRIGGER
81872           "sql = sqlite_rename_table(sql, %Q), "
81873 #else
81874           "sql = CASE "
81875             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
81876             "ELSE sqlite_rename_table(sql, %Q) END, "
81877 #endif
81878           "tbl_name = %Q, "
81879           "name = CASE "
81880             "WHEN type='table' THEN %Q "
81881             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
81882              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
81883             "ELSE name END "
81884       "WHERE tbl_name=%Q COLLATE nocase AND "
81885           "(type='table' OR type='index' OR type='trigger');",
81886       zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
81887 #ifndef SQLITE_OMIT_TRIGGER
81888       zName,
81889 #endif
81890       zName, nTabName, zTabName
81891   );
81892 
81893 #ifndef SQLITE_OMIT_AUTOINCREMENT
81894   /* If the sqlite_sequence table exists in this database, then update
81895   ** it with the new table name.
81896   */
81897   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
81898     sqlite3NestedParse(pParse,
81899         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
81900         zDb, zName, pTab->zName);
81901   }
81902 #endif
81903 
81904 #ifndef SQLITE_OMIT_TRIGGER
81905   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
81906   ** table. Don't do this if the table being ALTERed is itself located in
81907   ** the temp database.
81908   */
81909   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
81910     sqlite3NestedParse(pParse,
81911         "UPDATE sqlite_temp_master SET "
81912             "sql = sqlite_rename_trigger(sql, %Q), "
81913             "tbl_name = %Q "
81914             "WHERE %s;", zName, zName, zWhere);
81915     sqlite3DbFree(db, zWhere);
81916   }
81917 #endif
81918 
81919 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
81920   if( db->flags&SQLITE_ForeignKeys ){
81921     FKey *p;
81922     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
81923       Table *pFrom = p->pFrom;
81924       if( pFrom!=pTab ){
81925         reloadTableSchema(pParse, p->pFrom, pFrom->zName);
81926       }
81927     }
81928   }
81929 #endif
81930 
81931   /* Drop and reload the internal table schema. */
81932   reloadTableSchema(pParse, pTab, zName);
81933 
81934 exit_rename_table:
81935   sqlite3SrcListDelete(db, pSrc);
81936   sqlite3DbFree(db, zName);
81937   db->flags = savedDbFlags;
81938 }
81939 
81940 
81941 /*
81942 ** Generate code to make sure the file format number is at least minFormat.
81943 ** The generated code will increase the file format number if necessary.
81944 */
81945 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
81946   Vdbe *v;
81947   v = sqlite3GetVdbe(pParse);
81948   /* The VDBE should have been allocated before this routine is called.
81949   ** If that allocation failed, we would have quit before reaching this
81950   ** point */
81951   if( ALWAYS(v) ){
81952     int r1 = sqlite3GetTempReg(pParse);
81953     int r2 = sqlite3GetTempReg(pParse);
81954     int j1;
81955     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
81956     sqlite3VdbeUsesBtree(v, iDb);
81957     sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
81958     j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
81959     sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
81960     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
81961     sqlite3VdbeJumpHere(v, j1);
81962     sqlite3ReleaseTempReg(pParse, r1);
81963     sqlite3ReleaseTempReg(pParse, r2);
81964   }
81965 }
81966 
81967 /*
81968 ** This function is called after an "ALTER TABLE ... ADD" statement
81969 ** has been parsed. Argument pColDef contains the text of the new
81970 ** column definition.
81971 **
81972 ** The Table structure pParse->pNewTable was extended to include
81973 ** the new column during parsing.
81974 */
81975 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
81976   Table *pNew;              /* Copy of pParse->pNewTable */
81977   Table *pTab;              /* Table being altered */
81978   int iDb;                  /* Database number */
81979   const char *zDb;          /* Database name */
81980   const char *zTab;         /* Table name */
81981   char *zCol;               /* Null-terminated column definition */
81982   Column *pCol;             /* The new column */
81983   Expr *pDflt;              /* Default value for the new column */
81984   sqlite3 *db;              /* The database connection; */
81985 
81986   db = pParse->db;
81987   if( pParse->nErr || db->mallocFailed ) return;
81988   pNew = pParse->pNewTable;
81989   assert( pNew );
81990 
81991   assert( sqlite3BtreeHoldsAllMutexes(db) );
81992   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
81993   zDb = db->aDb[iDb].zName;
81994   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
81995   pCol = &pNew->aCol[pNew->nCol-1];
81996   pDflt = pCol->pDflt;
81997   pTab = sqlite3FindTable(db, zTab, zDb);
81998   assert( pTab );
81999 
82000 #ifndef SQLITE_OMIT_AUTHORIZATION
82001   /* Invoke the authorization callback. */
82002   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
82003     return;
82004   }
82005 #endif
82006 
82007   /* If the default value for the new column was specified with a
82008   ** literal NULL, then set pDflt to 0. This simplifies checking
82009   ** for an SQL NULL default below.
82010   */
82011   if( pDflt && pDflt->op==TK_NULL ){
82012     pDflt = 0;
82013   }
82014 
82015   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
82016   ** If there is a NOT NULL constraint, then the default value for the
82017   ** column must not be NULL.
82018   */
82019   if( pCol->colFlags & COLFLAG_PRIMKEY ){
82020     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
82021     return;
82022   }
82023   if( pNew->pIndex ){
82024     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
82025     return;
82026   }
82027   if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
82028     sqlite3ErrorMsg(pParse,
82029         "Cannot add a REFERENCES column with non-NULL default value");
82030     return;
82031   }
82032   if( pCol->notNull && !pDflt ){
82033     sqlite3ErrorMsg(pParse,
82034         "Cannot add a NOT NULL column with default value NULL");
82035     return;
82036   }
82037 
82038   /* Ensure the default expression is something that sqlite3ValueFromExpr()
82039   ** can handle (i.e. not CURRENT_TIME etc.)
82040   */
82041   if( pDflt ){
82042     sqlite3_value *pVal = 0;
82043     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
82044       db->mallocFailed = 1;
82045       return;
82046     }
82047     if( !pVal ){
82048       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
82049       return;
82050     }
82051     sqlite3ValueFree(pVal);
82052   }
82053 
82054   /* Modify the CREATE TABLE statement. */
82055   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
82056   if( zCol ){
82057     char *zEnd = &zCol[pColDef->n-1];
82058     int savedDbFlags = db->flags;
82059     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
82060       *zEnd-- = '\0';
82061     }
82062     db->flags |= SQLITE_PreferBuiltin;
82063     sqlite3NestedParse(pParse,
82064         "UPDATE \"%w\".%s SET "
82065           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
82066         "WHERE type = 'table' AND name = %Q",
82067       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
82068       zTab
82069     );
82070     sqlite3DbFree(db, zCol);
82071     db->flags = savedDbFlags;
82072   }
82073 
82074   /* If the default value of the new column is NULL, then set the file
82075   ** format to 2. If the default value of the new column is not NULL,
82076   ** the file format becomes 3.
82077   */
82078   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
82079 
82080   /* Reload the schema of the modified table. */
82081   reloadTableSchema(pParse, pTab, pTab->zName);
82082 }
82083 
82084 /*
82085 ** This function is called by the parser after the table-name in
82086 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
82087 ** pSrc is the full-name of the table being altered.
82088 **
82089 ** This routine makes a (partial) copy of the Table structure
82090 ** for the table being altered and sets Parse.pNewTable to point
82091 ** to it. Routines called by the parser as the column definition
82092 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
82093 ** the copy. The copy of the Table structure is deleted by tokenize.c
82094 ** after parsing is finished.
82095 **
82096 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
82097 ** coding the "ALTER TABLE ... ADD" statement.
82098 */
82099 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
82100   Table *pNew;
82101   Table *pTab;
82102   Vdbe *v;
82103   int iDb;
82104   int i;
82105   int nAlloc;
82106   sqlite3 *db = pParse->db;
82107 
82108   /* Look up the table being altered. */
82109   assert( pParse->pNewTable==0 );
82110   assert( sqlite3BtreeHoldsAllMutexes(db) );
82111   if( db->mallocFailed ) goto exit_begin_add_column;
82112   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
82113   if( !pTab ) goto exit_begin_add_column;
82114 
82115 #ifndef SQLITE_OMIT_VIRTUALTABLE
82116   if( IsVirtual(pTab) ){
82117     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
82118     goto exit_begin_add_column;
82119   }
82120 #endif
82121 
82122   /* Make sure this is not an attempt to ALTER a view. */
82123   if( pTab->pSelect ){
82124     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
82125     goto exit_begin_add_column;
82126   }
82127   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
82128     goto exit_begin_add_column;
82129   }
82130 
82131   assert( pTab->addColOffset>0 );
82132   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
82133 
82134   /* Put a copy of the Table struct in Parse.pNewTable for the
82135   ** sqlite3AddColumn() function and friends to modify.  But modify
82136   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
82137   ** prefix, we insure that the name will not collide with an existing
82138   ** table because user table are not allowed to have the "sqlite_"
82139   ** prefix on their name.
82140   */
82141   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
82142   if( !pNew ) goto exit_begin_add_column;
82143   pParse->pNewTable = pNew;
82144   pNew->nRef = 1;
82145   pNew->nCol = pTab->nCol;
82146   assert( pNew->nCol>0 );
82147   nAlloc = (((pNew->nCol-1)/8)*8)+8;
82148   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
82149   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
82150   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
82151   if( !pNew->aCol || !pNew->zName ){
82152     db->mallocFailed = 1;
82153     goto exit_begin_add_column;
82154   }
82155   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
82156   for(i=0; i<pNew->nCol; i++){
82157     Column *pCol = &pNew->aCol[i];
82158     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
82159     pCol->zColl = 0;
82160     pCol->zType = 0;
82161     pCol->pDflt = 0;
82162     pCol->zDflt = 0;
82163   }
82164   pNew->pSchema = db->aDb[iDb].pSchema;
82165   pNew->addColOffset = pTab->addColOffset;
82166   pNew->nRef = 1;
82167 
82168   /* Begin a transaction and increment the schema cookie.  */
82169   sqlite3BeginWriteOperation(pParse, 0, iDb);
82170   v = sqlite3GetVdbe(pParse);
82171   if( !v ) goto exit_begin_add_column;
82172   sqlite3ChangeCookie(pParse, iDb);
82173 
82174 exit_begin_add_column:
82175   sqlite3SrcListDelete(db, pSrc);
82176   return;
82177 }
82178 #endif  /* SQLITE_ALTER_TABLE */
82179 
82180 /************** End of alter.c ***********************************************/
82181 /************** Begin file analyze.c *****************************************/
82182 /*
82183 ** 2005-07-08
82184 **
82185 ** The author disclaims copyright to this source code.  In place of
82186 ** a legal notice, here is a blessing:
82187 **
82188 **    May you do good and not evil.
82189 **    May you find forgiveness for yourself and forgive others.
82190 **    May you share freely, never taking more than you give.
82191 **
82192 *************************************************************************
82193 ** This file contains code associated with the ANALYZE command.
82194 **
82195 ** The ANALYZE command gather statistics about the content of tables
82196 ** and indices.  These statistics are made available to the query planner
82197 ** to help it make better decisions about how to perform queries.
82198 **
82199 ** The following system tables are or have been supported:
82200 **
82201 **    CREATE TABLE sqlite_stat1(tbl, idx, stat);
82202 **    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
82203 **    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
82204 **    CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
82205 **
82206 ** Additional tables might be added in future releases of SQLite.
82207 ** The sqlite_stat2 table is not created or used unless the SQLite version
82208 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
82209 ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
82210 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
82211 ** created and used by SQLite versions 3.7.9 and later and with
82212 ** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
82213 ** is a superset of sqlite_stat2.  The sqlite_stat4 is an enhanced
82214 ** version of sqlite_stat3 and is only available when compiled with
82215 ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later.  It is
82216 ** not possible to enable both STAT3 and STAT4 at the same time.  If they
82217 ** are both enabled, then STAT4 takes precedence.
82218 **
82219 ** For most applications, sqlite_stat1 provides all the statisics required
82220 ** for the query planner to make good choices.
82221 **
82222 ** Format of sqlite_stat1:
82223 **
82224 ** There is normally one row per index, with the index identified by the
82225 ** name in the idx column.  The tbl column is the name of the table to
82226 ** which the index belongs.  In each such row, the stat column will be
82227 ** a string consisting of a list of integers.  The first integer in this
82228 ** list is the number of rows in the index.  (This is the same as the
82229 ** number of rows in the table, except for partial indices.)  The second
82230 ** integer is the average number of rows in the index that have the same
82231 ** value in the first column of the index.  The third integer is the average
82232 ** number of rows in the index that have the same value for the first two
82233 ** columns.  The N-th integer (for N>1) is the average number of rows in
82234 ** the index which have the same value for the first N-1 columns.  For
82235 ** a K-column index, there will be K+1 integers in the stat column.  If
82236 ** the index is unique, then the last integer will be 1.
82237 **
82238 ** The list of integers in the stat column can optionally be followed
82239 ** by the keyword "unordered".  The "unordered" keyword, if it is present,
82240 ** must be separated from the last integer by a single space.  If the
82241 ** "unordered" keyword is present, then the query planner assumes that
82242 ** the index is unordered and will not use the index for a range query.
82243 **
82244 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
82245 ** column contains a single integer which is the (estimated) number of
82246 ** rows in the table identified by sqlite_stat1.tbl.
82247 **
82248 ** Format of sqlite_stat2:
82249 **
82250 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
82251 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
82252 ** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
82253 ** about the distribution of keys within an index.  The index is identified by
82254 ** the "idx" column and the "tbl" column is the name of the table to which
82255 ** the index belongs.  There are usually 10 rows in the sqlite_stat2
82256 ** table for each index.
82257 **
82258 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
82259 ** inclusive are samples of the left-most key value in the index taken at
82260 ** evenly spaced points along the index.  Let the number of samples be S
82261 ** (10 in the standard build) and let C be the number of rows in the index.
82262 ** Then the sampled rows are given by:
82263 **
82264 **     rownumber = (i*C*2 + C)/(S*2)
82265 **
82266 ** For i between 0 and S-1.  Conceptually, the index space is divided into
82267 ** S uniform buckets and the samples are the middle row from each bucket.
82268 **
82269 ** The format for sqlite_stat2 is recorded here for legacy reference.  This
82270 ** version of SQLite does not support sqlite_stat2.  It neither reads nor
82271 ** writes the sqlite_stat2 table.  This version of SQLite only supports
82272 ** sqlite_stat3.
82273 **
82274 ** Format for sqlite_stat3:
82275 **
82276 ** The sqlite_stat3 format is a subset of sqlite_stat4.  Hence, the
82277 ** sqlite_stat4 format will be described first.  Further information
82278 ** about sqlite_stat3 follows the sqlite_stat4 description.
82279 **
82280 ** Format for sqlite_stat4:
82281 **
82282 ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
82283 ** to aid the query planner in choosing good indices based on the values
82284 ** that indexed columns are compared against in the WHERE clauses of
82285 ** queries.
82286 **
82287 ** The sqlite_stat4 table contains multiple entries for each index.
82288 ** The idx column names the index and the tbl column is the table of the
82289 ** index.  If the idx and tbl columns are the same, then the sample is
82290 ** of the INTEGER PRIMARY KEY.  The sample column is a blob which is the
82291 ** binary encoding of a key from the index.  The nEq column is a
82292 ** list of integers.  The first integer is the approximate number
82293 ** of entries in the index whose left-most column exactly matches
82294 ** the left-most column of the sample.  The second integer in nEq
82295 ** is the approximate number of entries in the index where the
82296 ** first two columns match the first two columns of the sample.
82297 ** And so forth.  nLt is another list of integers that show the approximate
82298 ** number of entries that are strictly less than the sample.  The first
82299 ** integer in nLt contains the number of entries in the index where the
82300 ** left-most column is less than the left-most column of the sample.
82301 ** The K-th integer in the nLt entry is the number of index entries
82302 ** where the first K columns are less than the first K columns of the
82303 ** sample.  The nDLt column is like nLt except that it contains the
82304 ** number of distinct entries in the index that are less than the
82305 ** sample.
82306 **
82307 ** There can be an arbitrary number of sqlite_stat4 entries per index.
82308 ** The ANALYZE command will typically generate sqlite_stat4 tables
82309 ** that contain between 10 and 40 samples which are distributed across
82310 ** the key space, though not uniformly, and which include samples with
82311 ** large nEq values.
82312 **
82313 ** Format for sqlite_stat3 redux:
82314 **
82315 ** The sqlite_stat3 table is like sqlite_stat4 except that it only
82316 ** looks at the left-most column of the index.  The sqlite_stat3.sample
82317 ** column contains the actual value of the left-most column instead
82318 ** of a blob encoding of the complete index key as is found in
82319 ** sqlite_stat4.sample.  The nEq, nLt, and nDLt entries of sqlite_stat3
82320 ** all contain just a single integer which is the same as the first
82321 ** integer in the equivalent columns in sqlite_stat4.
82322 */
82323 #ifndef SQLITE_OMIT_ANALYZE
82324 
82325 #if defined(SQLITE_ENABLE_STAT4)
82326 # define IsStat4     1
82327 # define IsStat3     0
82328 #elif defined(SQLITE_ENABLE_STAT3)
82329 # define IsStat4     0
82330 # define IsStat3     1
82331 #else
82332 # define IsStat4     0
82333 # define IsStat3     0
82334 # undef SQLITE_STAT4_SAMPLES
82335 # define SQLITE_STAT4_SAMPLES 1
82336 #endif
82337 #define IsStat34    (IsStat3+IsStat4)  /* 1 for STAT3 or STAT4. 0 otherwise */
82338 
82339 /*
82340 ** This routine generates code that opens the sqlite_statN tables.
82341 ** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
82342 ** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
82343 ** appropriate compile-time options are provided.
82344 **
82345 ** If the sqlite_statN tables do not previously exist, it is created.
82346 **
82347 ** Argument zWhere may be a pointer to a buffer containing a table name,
82348 ** or it may be a NULL pointer. If it is not NULL, then all entries in
82349 ** the sqlite_statN tables associated with the named table are deleted.
82350 ** If zWhere==0, then code is generated to delete all stat table entries.
82351 */
82352 static void openStatTable(
82353   Parse *pParse,          /* Parsing context */
82354   int iDb,                /* The database we are looking in */
82355   int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
82356   const char *zWhere,     /* Delete entries for this table or index */
82357   const char *zWhereType  /* Either "tbl" or "idx" */
82358 ){
82359   static const struct {
82360     const char *zName;
82361     const char *zCols;
82362   } aTable[] = {
82363     { "sqlite_stat1", "tbl,idx,stat" },
82364 #if defined(SQLITE_ENABLE_STAT4)
82365     { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
82366     { "sqlite_stat3", 0 },
82367 #elif defined(SQLITE_ENABLE_STAT3)
82368     { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
82369     { "sqlite_stat4", 0 },
82370 #else
82371     { "sqlite_stat3", 0 },
82372     { "sqlite_stat4", 0 },
82373 #endif
82374   };
82375   int i;
82376   sqlite3 *db = pParse->db;
82377   Db *pDb;
82378   Vdbe *v = sqlite3GetVdbe(pParse);
82379   int aRoot[ArraySize(aTable)];
82380   u8 aCreateTbl[ArraySize(aTable)];
82381 
82382   if( v==0 ) return;
82383   assert( sqlite3BtreeHoldsAllMutexes(db) );
82384   assert( sqlite3VdbeDb(v)==db );
82385   pDb = &db->aDb[iDb];
82386 
82387   /* Create new statistic tables if they do not exist, or clear them
82388   ** if they do already exist.
82389   */
82390   for(i=0; i<ArraySize(aTable); i++){
82391     const char *zTab = aTable[i].zName;
82392     Table *pStat;
82393     if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
82394       if( aTable[i].zCols ){
82395         /* The sqlite_statN table does not exist. Create it. Note that a
82396         ** side-effect of the CREATE TABLE statement is to leave the rootpage
82397         ** of the new table in register pParse->regRoot. This is important
82398         ** because the OpenWrite opcode below will be needing it. */
82399         sqlite3NestedParse(pParse,
82400             "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
82401         );
82402         aRoot[i] = pParse->regRoot;
82403         aCreateTbl[i] = OPFLAG_P2ISREG;
82404       }
82405     }else{
82406       /* The table already exists. If zWhere is not NULL, delete all entries
82407       ** associated with the table zWhere. If zWhere is NULL, delete the
82408       ** entire contents of the table. */
82409       aRoot[i] = pStat->tnum;
82410       aCreateTbl[i] = 0;
82411       sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
82412       if( zWhere ){
82413         sqlite3NestedParse(pParse,
82414            "DELETE FROM %Q.%s WHERE %s=%Q",
82415            pDb->zName, zTab, zWhereType, zWhere
82416         );
82417       }else{
82418         /* The sqlite_stat[134] table already exists.  Delete all rows. */
82419         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
82420       }
82421     }
82422   }
82423 
82424   /* Open the sqlite_stat[134] tables for writing. */
82425   for(i=0; aTable[i].zCols; i++){
82426     assert( i<ArraySize(aTable) );
82427     sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
82428     sqlite3VdbeChangeP5(v, aCreateTbl[i]);
82429   }
82430 }
82431 
82432 /*
82433 ** Recommended number of samples for sqlite_stat4
82434 */
82435 #ifndef SQLITE_STAT4_SAMPLES
82436 # define SQLITE_STAT4_SAMPLES 24
82437 #endif
82438 
82439 /*
82440 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
82441 ** share an instance of the following structure to hold their state
82442 ** information.
82443 */
82444 typedef struct Stat4Accum Stat4Accum;
82445 typedef struct Stat4Sample Stat4Sample;
82446 struct Stat4Sample {
82447   tRowcnt *anEq;                  /* sqlite_stat4.nEq */
82448   tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
82449 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82450   tRowcnt *anLt;                  /* sqlite_stat4.nLt */
82451   union {
82452     i64 iRowid;                     /* Rowid in main table of the key */
82453     u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
82454   } u;
82455   u32 nRowid;                     /* Sizeof aRowid[] */
82456   u8 isPSample;                   /* True if a periodic sample */
82457   int iCol;                       /* If !isPSample, the reason for inclusion */
82458   u32 iHash;                      /* Tiebreaker hash */
82459 #endif
82460 };
82461 struct Stat4Accum {
82462   tRowcnt nRow;             /* Number of rows in the entire table */
82463   tRowcnt nPSample;         /* How often to do a periodic sample */
82464   int nCol;                 /* Number of columns in index + rowid */
82465   int mxSample;             /* Maximum number of samples to accumulate */
82466   Stat4Sample current;      /* Current row as a Stat4Sample */
82467   u32 iPrn;                 /* Pseudo-random number used for sampling */
82468   Stat4Sample *aBest;       /* Array of nCol best samples */
82469   int iMin;                 /* Index in a[] of entry with minimum score */
82470   int nSample;              /* Current number of samples */
82471   int iGet;                 /* Index of current sample accessed by stat_get() */
82472   Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
82473   sqlite3 *db;              /* Database connection, for malloc() */
82474 };
82475 
82476 /* Reclaim memory used by a Stat4Sample
82477 */
82478 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82479 static void sampleClear(sqlite3 *db, Stat4Sample *p){
82480   assert( db!=0 );
82481   if( p->nRowid ){
82482     sqlite3DbFree(db, p->u.aRowid);
82483     p->nRowid = 0;
82484   }
82485 }
82486 #endif
82487 
82488 /* Initialize the BLOB value of a ROWID
82489 */
82490 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82491 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
82492   assert( db!=0 );
82493   if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
82494   p->u.aRowid = sqlite3DbMallocRaw(db, n);
82495   if( p->u.aRowid ){
82496     p->nRowid = n;
82497     memcpy(p->u.aRowid, pData, n);
82498   }else{
82499     p->nRowid = 0;
82500   }
82501 }
82502 #endif
82503 
82504 /* Initialize the INTEGER value of a ROWID.
82505 */
82506 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82507 static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
82508   assert( db!=0 );
82509   if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
82510   p->nRowid = 0;
82511   p->u.iRowid = iRowid;
82512 }
82513 #endif
82514 
82515 
82516 /*
82517 ** Copy the contents of object (*pFrom) into (*pTo).
82518 */
82519 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82520 static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
82521   pTo->isPSample = pFrom->isPSample;
82522   pTo->iCol = pFrom->iCol;
82523   pTo->iHash = pFrom->iHash;
82524   memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
82525   memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
82526   memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
82527   if( pFrom->nRowid ){
82528     sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
82529   }else{
82530     sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
82531   }
82532 }
82533 #endif
82534 
82535 /*
82536 ** Reclaim all memory of a Stat4Accum structure.
82537 */
82538 static void stat4Destructor(void *pOld){
82539   Stat4Accum *p = (Stat4Accum*)pOld;
82540 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82541   int i;
82542   for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
82543   for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
82544   sampleClear(p->db, &p->current);
82545 #endif
82546   sqlite3DbFree(p->db, p);
82547 }
82548 
82549 /*
82550 ** Implementation of the stat_init(N,C) SQL function. The two parameters
82551 ** are the number of rows in the table or index (C) and the number of columns
82552 ** in the index (N).  The second argument (C) is only used for STAT3 and STAT4.
82553 **
82554 ** This routine allocates the Stat4Accum object in heap memory. The return
82555 ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
82556 ** the size of the blob is sizeof(void*) bytes).
82557 */
82558 static void statInit(
82559   sqlite3_context *context,
82560   int argc,
82561   sqlite3_value **argv
82562 ){
82563   Stat4Accum *p;
82564   int nCol;                       /* Number of columns in index being sampled */
82565   int nColUp;                     /* nCol rounded up for alignment */
82566   int n;                          /* Bytes of space to allocate */
82567   sqlite3 *db;                    /* Database connection */
82568 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82569   int mxSample = SQLITE_STAT4_SAMPLES;
82570 #endif
82571 
82572   /* Decode the three function arguments */
82573   UNUSED_PARAMETER(argc);
82574   nCol = sqlite3_value_int(argv[0]);
82575   assert( nCol>1 );               /* >1 because it includes the rowid column */
82576   nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
82577 
82578   /* Allocate the space required for the Stat4Accum object */
82579   n = sizeof(*p)
82580     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
82581     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
82582 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82583     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
82584     + sizeof(Stat4Sample)*(nCol+mxSample)     /* Stat4Accum.aBest[], a[] */
82585     + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
82586 #endif
82587   ;
82588   db = sqlite3_context_db_handle(context);
82589   p = sqlite3DbMallocZero(db, n);
82590   if( p==0 ){
82591     sqlite3_result_error_nomem(context);
82592     return;
82593   }
82594 
82595   p->db = db;
82596   p->nRow = 0;
82597   p->nCol = nCol;
82598   p->current.anDLt = (tRowcnt*)&p[1];
82599   p->current.anEq = &p->current.anDLt[nColUp];
82600 
82601 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82602   {
82603     u8 *pSpace;                     /* Allocated space not yet assigned */
82604     int i;                          /* Used to iterate through p->aSample[] */
82605 
82606     p->iGet = -1;
82607     p->mxSample = mxSample;
82608     p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
82609     p->current.anLt = &p->current.anEq[nColUp];
82610     p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[1])*0xd0944565;
82611 
82612     /* Set up the Stat4Accum.a[] and aBest[] arrays */
82613     p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
82614     p->aBest = &p->a[mxSample];
82615     pSpace = (u8*)(&p->a[mxSample+nCol]);
82616     for(i=0; i<(mxSample+nCol); i++){
82617       p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
82618       p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
82619       p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
82620     }
82621     assert( (pSpace - (u8*)p)==n );
82622 
82623     for(i=0; i<nCol; i++){
82624       p->aBest[i].iCol = i;
82625     }
82626   }
82627 #endif
82628 
82629   /* Return a pointer to the allocated object to the caller */
82630   sqlite3_result_blob(context, p, sizeof(p), stat4Destructor);
82631 }
82632 static const FuncDef statInitFuncdef = {
82633   1+IsStat34,      /* nArg */
82634   SQLITE_UTF8,     /* funcFlags */
82635   0,               /* pUserData */
82636   0,               /* pNext */
82637   statInit,        /* xFunc */
82638   0,               /* xStep */
82639   0,               /* xFinalize */
82640   "stat_init",     /* zName */
82641   0,               /* pHash */
82642   0                /* pDestructor */
82643 };
82644 
82645 #ifdef SQLITE_ENABLE_STAT4
82646 /*
82647 ** pNew and pOld are both candidate non-periodic samples selected for
82648 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
82649 ** considering only any trailing columns and the sample hash value, this
82650 ** function returns true if sample pNew is to be preferred over pOld.
82651 ** In other words, if we assume that the cardinalities of the selected
82652 ** column for pNew and pOld are equal, is pNew to be preferred over pOld.
82653 **
82654 ** This function assumes that for each argument sample, the contents of
82655 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
82656 */
82657 static int sampleIsBetterPost(
82658   Stat4Accum *pAccum,
82659   Stat4Sample *pNew,
82660   Stat4Sample *pOld
82661 ){
82662   int nCol = pAccum->nCol;
82663   int i;
82664   assert( pNew->iCol==pOld->iCol );
82665   for(i=pNew->iCol+1; i<nCol; i++){
82666     if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
82667     if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
82668   }
82669   if( pNew->iHash>pOld->iHash ) return 1;
82670   return 0;
82671 }
82672 #endif
82673 
82674 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82675 /*
82676 ** Return true if pNew is to be preferred over pOld.
82677 **
82678 ** This function assumes that for each argument sample, the contents of
82679 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
82680 */
82681 static int sampleIsBetter(
82682   Stat4Accum *pAccum,
82683   Stat4Sample *pNew,
82684   Stat4Sample *pOld
82685 ){
82686   tRowcnt nEqNew = pNew->anEq[pNew->iCol];
82687   tRowcnt nEqOld = pOld->anEq[pOld->iCol];
82688 
82689   assert( pOld->isPSample==0 && pNew->isPSample==0 );
82690   assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
82691 
82692   if( (nEqNew>nEqOld) ) return 1;
82693 #ifdef SQLITE_ENABLE_STAT4
82694   if( nEqNew==nEqOld ){
82695     if( pNew->iCol<pOld->iCol ) return 1;
82696     return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
82697   }
82698   return 0;
82699 #else
82700   return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
82701 #endif
82702 }
82703 
82704 /*
82705 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
82706 ** remove the least desirable sample from p->a[] to make room.
82707 */
82708 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
82709   Stat4Sample *pSample = 0;
82710   int i;
82711 
82712   assert( IsStat4 || nEqZero==0 );
82713 
82714 #ifdef SQLITE_ENABLE_STAT4
82715   if( pNew->isPSample==0 ){
82716     Stat4Sample *pUpgrade = 0;
82717     assert( pNew->anEq[pNew->iCol]>0 );
82718 
82719     /* This sample is being added because the prefix that ends in column
82720     ** iCol occurs many times in the table. However, if we have already
82721     ** added a sample that shares this prefix, there is no need to add
82722     ** this one. Instead, upgrade the priority of the highest priority
82723     ** existing sample that shares this prefix.  */
82724     for(i=p->nSample-1; i>=0; i--){
82725       Stat4Sample *pOld = &p->a[i];
82726       if( pOld->anEq[pNew->iCol]==0 ){
82727         if( pOld->isPSample ) return;
82728         assert( pOld->iCol>pNew->iCol );
82729         assert( sampleIsBetter(p, pNew, pOld) );
82730         if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
82731           pUpgrade = pOld;
82732         }
82733       }
82734     }
82735     if( pUpgrade ){
82736       pUpgrade->iCol = pNew->iCol;
82737       pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
82738       goto find_new_min;
82739     }
82740   }
82741 #endif
82742 
82743   /* If necessary, remove sample iMin to make room for the new sample. */
82744   if( p->nSample>=p->mxSample ){
82745     Stat4Sample *pMin = &p->a[p->iMin];
82746     tRowcnt *anEq = pMin->anEq;
82747     tRowcnt *anLt = pMin->anLt;
82748     tRowcnt *anDLt = pMin->anDLt;
82749     sampleClear(p->db, pMin);
82750     memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
82751     pSample = &p->a[p->nSample-1];
82752     pSample->nRowid = 0;
82753     pSample->anEq = anEq;
82754     pSample->anDLt = anDLt;
82755     pSample->anLt = anLt;
82756     p->nSample = p->mxSample-1;
82757   }
82758 
82759   /* The "rows less-than" for the rowid column must be greater than that
82760   ** for the last sample in the p->a[] array. Otherwise, the samples would
82761   ** be out of order. */
82762 #ifdef SQLITE_ENABLE_STAT4
82763   assert( p->nSample==0
82764        || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
82765 #endif
82766 
82767   /* Insert the new sample */
82768   pSample = &p->a[p->nSample];
82769   sampleCopy(p, pSample, pNew);
82770   p->nSample++;
82771 
82772   /* Zero the first nEqZero entries in the anEq[] array. */
82773   memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
82774 
82775 #ifdef SQLITE_ENABLE_STAT4
82776  find_new_min:
82777 #endif
82778   if( p->nSample>=p->mxSample ){
82779     int iMin = -1;
82780     for(i=0; i<p->mxSample; i++){
82781       if( p->a[i].isPSample ) continue;
82782       if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
82783         iMin = i;
82784       }
82785     }
82786     assert( iMin>=0 );
82787     p->iMin = iMin;
82788   }
82789 }
82790 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82791 
82792 /*
82793 ** Field iChng of the index being scanned has changed. So at this point
82794 ** p->current contains a sample that reflects the previous row of the
82795 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
82796 ** correct at this point.
82797 */
82798 static void samplePushPrevious(Stat4Accum *p, int iChng){
82799 #ifdef SQLITE_ENABLE_STAT4
82800   int i;
82801 
82802   /* Check if any samples from the aBest[] array should be pushed
82803   ** into IndexSample.a[] at this point.  */
82804   for(i=(p->nCol-2); i>=iChng; i--){
82805     Stat4Sample *pBest = &p->aBest[i];
82806     pBest->anEq[i] = p->current.anEq[i];
82807     if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
82808       sampleInsert(p, pBest, i);
82809     }
82810   }
82811 
82812   /* Update the anEq[] fields of any samples already collected. */
82813   for(i=p->nSample-1; i>=0; i--){
82814     int j;
82815     for(j=iChng; j<p->nCol; j++){
82816       if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
82817     }
82818   }
82819 #endif
82820 
82821 #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
82822   if( iChng==0 ){
82823     tRowcnt nLt = p->current.anLt[0];
82824     tRowcnt nEq = p->current.anEq[0];
82825 
82826     /* Check if this is to be a periodic sample. If so, add it. */
82827     if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
82828       p->current.isPSample = 1;
82829       sampleInsert(p, &p->current, 0);
82830       p->current.isPSample = 0;
82831     }else
82832 
82833     /* Or if it is a non-periodic sample. Add it in this case too. */
82834     if( p->nSample<p->mxSample
82835      || sampleIsBetter(p, &p->current, &p->a[p->iMin])
82836     ){
82837       sampleInsert(p, &p->current, 0);
82838     }
82839   }
82840 #endif
82841 
82842 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
82843   UNUSED_PARAMETER( p );
82844   UNUSED_PARAMETER( iChng );
82845 #endif
82846 }
82847 
82848 /*
82849 ** Implementation of the stat_push SQL function:  stat_push(P,C,R)
82850 ** Arguments:
82851 **
82852 **    P     Pointer to the Stat4Accum object created by stat_init()
82853 **    C     Index of left-most column to differ from previous row
82854 **    R     Rowid for the current row.  Might be a key record for
82855 **          WITHOUT ROWID tables.
82856 **
82857 ** The SQL function always returns NULL.
82858 **
82859 ** The R parameter is only used for STAT3 and STAT4
82860 */
82861 static void statPush(
82862   sqlite3_context *context,
82863   int argc,
82864   sqlite3_value **argv
82865 ){
82866   int i;
82867 
82868   /* The three function arguments */
82869   Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
82870   int iChng = sqlite3_value_int(argv[1]);
82871 
82872   UNUSED_PARAMETER( argc );
82873   UNUSED_PARAMETER( context );
82874   assert( p->nCol>1 );        /* Includes rowid field */
82875   assert( iChng<p->nCol );
82876 
82877   if( p->nRow==0 ){
82878     /* This is the first call to this function. Do initialization. */
82879     for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
82880   }else{
82881     /* Second and subsequent calls get processed here */
82882     samplePushPrevious(p, iChng);
82883 
82884     /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
82885     ** to the current row of the index. */
82886     for(i=0; i<iChng; i++){
82887       p->current.anEq[i]++;
82888     }
82889     for(i=iChng; i<p->nCol; i++){
82890       p->current.anDLt[i]++;
82891 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82892       p->current.anLt[i] += p->current.anEq[i];
82893 #endif
82894       p->current.anEq[i] = 1;
82895     }
82896   }
82897   p->nRow++;
82898 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82899   if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
82900     sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
82901   }else{
82902     sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
82903                                        sqlite3_value_blob(argv[2]));
82904   }
82905   p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
82906 #endif
82907 
82908 #ifdef SQLITE_ENABLE_STAT4
82909   {
82910     tRowcnt nLt = p->current.anLt[p->nCol-1];
82911 
82912     /* Check if this is to be a periodic sample. If so, add it. */
82913     if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
82914       p->current.isPSample = 1;
82915       p->current.iCol = 0;
82916       sampleInsert(p, &p->current, p->nCol-1);
82917       p->current.isPSample = 0;
82918     }
82919 
82920     /* Update the aBest[] array. */
82921     for(i=0; i<(p->nCol-1); i++){
82922       p->current.iCol = i;
82923       if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
82924         sampleCopy(p, &p->aBest[i], &p->current);
82925       }
82926     }
82927   }
82928 #endif
82929 }
82930 static const FuncDef statPushFuncdef = {
82931   2+IsStat34,      /* nArg */
82932   SQLITE_UTF8,     /* funcFlags */
82933   0,               /* pUserData */
82934   0,               /* pNext */
82935   statPush,        /* xFunc */
82936   0,               /* xStep */
82937   0,               /* xFinalize */
82938   "stat_push",     /* zName */
82939   0,               /* pHash */
82940   0                /* pDestructor */
82941 };
82942 
82943 #define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
82944 #define STAT_GET_ROWID 1          /* "rowid" column of stat[34] entry */
82945 #define STAT_GET_NEQ   2          /* "neq" column of stat[34] entry */
82946 #define STAT_GET_NLT   3          /* "nlt" column of stat[34] entry */
82947 #define STAT_GET_NDLT  4          /* "ndlt" column of stat[34] entry */
82948 
82949 /*
82950 ** Implementation of the stat_get(P,J) SQL function.  This routine is
82951 ** used to query the results.  Content is returned for parameter J
82952 ** which is one of the STAT_GET_xxxx values defined above.
82953 **
82954 ** If neither STAT3 nor STAT4 are enabled, then J is always
82955 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
82956 ** a one-parameter function, stat_get(P), that always returns the
82957 ** stat1 table entry information.
82958 */
82959 static void statGet(
82960   sqlite3_context *context,
82961   int argc,
82962   sqlite3_value **argv
82963 ){
82964   Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
82965 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82966   /* STAT3 and STAT4 have a parameter on this routine. */
82967   int eCall = sqlite3_value_int(argv[1]);
82968   assert( argc==2 );
82969   assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
82970        || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
82971        || eCall==STAT_GET_NDLT
82972   );
82973   if( eCall==STAT_GET_STAT1 )
82974 #else
82975   assert( argc==1 );
82976 #endif
82977   {
82978     /* Return the value to store in the "stat" column of the sqlite_stat1
82979     ** table for this index.
82980     **
82981     ** The value is a string composed of a list of integers describing
82982     ** the index. The first integer in the list is the total number of
82983     ** entries in the index. There is one additional integer in the list
82984     ** for each indexed column. This additional integer is an estimate of
82985     ** the number of rows matched by a stabbing query on the index using
82986     ** a key with the corresponding number of fields. In other words,
82987     ** if the index is on columns (a,b) and the sqlite_stat1 value is
82988     ** "100 10 2", then SQLite estimates that:
82989     **
82990     **   * the index contains 100 rows,
82991     **   * "WHERE a=?" matches 10 rows, and
82992     **   * "WHERE a=? AND b=?" matches 2 rows.
82993     **
82994     ** If D is the count of distinct values and K is the total number of
82995     ** rows, then each estimate is computed as:
82996     **
82997     **        I = (K+D-1)/D
82998     */
82999     char *z;
83000     int i;
83001 
83002     char *zRet = sqlite3MallocZero(p->nCol * 25);
83003     if( zRet==0 ){
83004       sqlite3_result_error_nomem(context);
83005       return;
83006     }
83007 
83008     sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
83009     z = zRet + sqlite3Strlen30(zRet);
83010     for(i=0; i<(p->nCol-1); i++){
83011       u64 nDistinct = p->current.anDLt[i] + 1;
83012       u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
83013       sqlite3_snprintf(24, z, " %llu", iVal);
83014       z += sqlite3Strlen30(z);
83015       assert( p->current.anEq[i] );
83016     }
83017     assert( z[0]=='\0' && z>zRet );
83018 
83019     sqlite3_result_text(context, zRet, -1, sqlite3_free);
83020   }
83021 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83022   else if( eCall==STAT_GET_ROWID ){
83023     if( p->iGet<0 ){
83024       samplePushPrevious(p, 0);
83025       p->iGet = 0;
83026     }
83027     if( p->iGet<p->nSample ){
83028       Stat4Sample *pS = p->a + p->iGet;
83029       if( pS->nRowid==0 ){
83030         sqlite3_result_int64(context, pS->u.iRowid);
83031       }else{
83032         sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
83033                             SQLITE_TRANSIENT);
83034       }
83035     }
83036   }else{
83037     tRowcnt *aCnt = 0;
83038 
83039     assert( p->iGet<p->nSample );
83040     switch( eCall ){
83041       case STAT_GET_NEQ:  aCnt = p->a[p->iGet].anEq; break;
83042       case STAT_GET_NLT:  aCnt = p->a[p->iGet].anLt; break;
83043       default: {
83044         aCnt = p->a[p->iGet].anDLt;
83045         p->iGet++;
83046         break;
83047       }
83048     }
83049 
83050     if( IsStat3 ){
83051       sqlite3_result_int64(context, (i64)aCnt[0]);
83052     }else{
83053       char *zRet = sqlite3MallocZero(p->nCol * 25);
83054       if( zRet==0 ){
83055         sqlite3_result_error_nomem(context);
83056       }else{
83057         int i;
83058         char *z = zRet;
83059         for(i=0; i<p->nCol; i++){
83060           sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
83061           z += sqlite3Strlen30(z);
83062         }
83063         assert( z[0]=='\0' && z>zRet );
83064         z[-1] = '\0';
83065         sqlite3_result_text(context, zRet, -1, sqlite3_free);
83066       }
83067     }
83068   }
83069 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
83070 #ifndef SQLITE_DEBUG
83071   UNUSED_PARAMETER( argc );
83072 #endif
83073 }
83074 static const FuncDef statGetFuncdef = {
83075   1+IsStat34,      /* nArg */
83076   SQLITE_UTF8,     /* funcFlags */
83077   0,               /* pUserData */
83078   0,               /* pNext */
83079   statGet,         /* xFunc */
83080   0,               /* xStep */
83081   0,               /* xFinalize */
83082   "stat_get",      /* zName */
83083   0,               /* pHash */
83084   0                /* pDestructor */
83085 };
83086 
83087 static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
83088   assert( regOut!=regStat4 && regOut!=regStat4+1 );
83089 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83090   sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
83091 #elif SQLITE_DEBUG
83092   assert( iParam==STAT_GET_STAT1 );
83093 #else
83094   UNUSED_PARAMETER( iParam );
83095 #endif
83096   sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
83097   sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
83098   sqlite3VdbeChangeP5(v, 1 + IsStat34);
83099 }
83100 
83101 /*
83102 ** Generate code to do an analysis of all indices associated with
83103 ** a single table.
83104 */
83105 static void analyzeOneTable(
83106   Parse *pParse,   /* Parser context */
83107   Table *pTab,     /* Table whose indices are to be analyzed */
83108   Index *pOnlyIdx, /* If not NULL, only analyze this one index */
83109   int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
83110   int iMem,        /* Available memory locations begin here */
83111   int iTab         /* Next available cursor */
83112 ){
83113   sqlite3 *db = pParse->db;    /* Database handle */
83114   Index *pIdx;                 /* An index to being analyzed */
83115   int iIdxCur;                 /* Cursor open on index being analyzed */
83116   int iTabCur;                 /* Table cursor */
83117   Vdbe *v;                     /* The virtual machine being built up */
83118   int i;                       /* Loop counter */
83119   int jZeroRows = -1;          /* Jump from here if number of rows is zero */
83120   int iDb;                     /* Index of database containing pTab */
83121   u8 needTableCnt = 1;         /* True to count the table */
83122   int regNewRowid = iMem++;    /* Rowid for the inserted record */
83123   int regStat4 = iMem++;       /* Register to hold Stat4Accum object */
83124   int regChng = iMem++;        /* Index of changed index field */
83125 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83126   int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
83127 #endif
83128   int regTemp = iMem++;        /* Temporary use register */
83129   int regTabname = iMem++;     /* Register containing table name */
83130   int regIdxname = iMem++;     /* Register containing index name */
83131   int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
83132   int regPrev = iMem;          /* MUST BE LAST (see below) */
83133 
83134   pParse->nMem = MAX(pParse->nMem, iMem);
83135   v = sqlite3GetVdbe(pParse);
83136   if( v==0 || NEVER(pTab==0) ){
83137     return;
83138   }
83139   if( pTab->tnum==0 ){
83140     /* Do not gather statistics on views or virtual tables */
83141     return;
83142   }
83143   if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
83144     /* Do not gather statistics on system tables */
83145     return;
83146   }
83147   assert( sqlite3BtreeHoldsAllMutexes(db) );
83148   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
83149   assert( iDb>=0 );
83150   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
83151 #ifndef SQLITE_OMIT_AUTHORIZATION
83152   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
83153       db->aDb[iDb].zName ) ){
83154     return;
83155   }
83156 #endif
83157 
83158   /* Establish a read-lock on the table at the shared-cache level.
83159   ** Open a read-only cursor on the table. Also allocate a cursor number
83160   ** to use for scanning indexes (iIdxCur). No index cursor is opened at
83161   ** this time though.  */
83162   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
83163   iTabCur = iTab++;
83164   iIdxCur = iTab++;
83165   pParse->nTab = MAX(pParse->nTab, iTab);
83166   sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
83167   sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
83168 
83169   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
83170     int nCol;                     /* Number of columns indexed by pIdx */
83171     int *aGotoChng;               /* Array of jump instruction addresses */
83172     int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
83173     int addrGotoChng0;            /* Address of "Goto addr_chng_0" */
83174     int addrNextRow;              /* Address of "next_row:" */
83175     const char *zIdxName;         /* Name of the index */
83176 
83177     if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
83178     if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
83179     VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
83180     nCol = pIdx->nKeyCol;
83181     aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
83182     if( aGotoChng==0 ) continue;
83183 
83184     /* Populate the register containing the index name. */
83185     if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
83186       zIdxName = pTab->zName;
83187     }else{
83188       zIdxName = pIdx->zName;
83189     }
83190     sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
83191 
83192     /*
83193     ** Pseudo-code for loop that calls stat_push():
83194     **
83195     **   Rewind csr
83196     **   if eof(csr) goto end_of_scan;
83197     **   regChng = 0
83198     **   goto chng_addr_0;
83199     **
83200     **  next_row:
83201     **   regChng = 0
83202     **   if( idx(0) != regPrev(0) ) goto chng_addr_0
83203     **   regChng = 1
83204     **   if( idx(1) != regPrev(1) ) goto chng_addr_1
83205     **   ...
83206     **   regChng = N
83207     **   goto chng_addr_N
83208     **
83209     **  chng_addr_0:
83210     **   regPrev(0) = idx(0)
83211     **  chng_addr_1:
83212     **   regPrev(1) = idx(1)
83213     **  ...
83214     **
83215     **  chng_addr_N:
83216     **   regRowid = idx(rowid)
83217     **   stat_push(P, regChng, regRowid)
83218     **   Next csr
83219     **   if !eof(csr) goto next_row;
83220     **
83221     **  end_of_scan:
83222     */
83223 
83224     /* Make sure there are enough memory cells allocated to accommodate
83225     ** the regPrev array and a trailing rowid (the rowid slot is required
83226     ** when building a record to insert into the sample column of
83227     ** the sqlite_stat4 table.  */
83228     pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
83229 
83230     /* Open a read-only cursor on the index being analyzed. */
83231     assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
83232     sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
83233     sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
83234     VdbeComment((v, "%s", pIdx->zName));
83235 
83236     /* Invoke the stat_init() function. The arguments are:
83237     **
83238     **    (1) the number of columns in the index including the rowid,
83239     **    (2) the number of rows in the index,
83240     **
83241     ** The second argument is only used for STAT3 and STAT4
83242     */
83243 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83244     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
83245 #endif
83246     sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
83247     sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
83248     sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
83249     sqlite3VdbeChangeP5(v, 1+IsStat34);
83250 
83251     /* Implementation of the following:
83252     **
83253     **   Rewind csr
83254     **   if eof(csr) goto end_of_scan;
83255     **   regChng = 0
83256     **   goto next_push_0;
83257     **
83258     */
83259     addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
83260     VdbeCoverage(v);
83261     sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
83262     addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
83263 
83264     /*
83265     **  next_row:
83266     **   regChng = 0
83267     **   if( idx(0) != regPrev(0) ) goto chng_addr_0
83268     **   regChng = 1
83269     **   if( idx(1) != regPrev(1) ) goto chng_addr_1
83270     **   ...
83271     **   regChng = N
83272     **   goto chng_addr_N
83273     */
83274     addrNextRow = sqlite3VdbeCurrentAddr(v);
83275     for(i=0; i<nCol; i++){
83276       char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
83277       sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
83278       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
83279       aGotoChng[i] =
83280       sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
83281       sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
83282       VdbeCoverage(v);
83283     }
83284     sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
83285     aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
83286 
83287     /*
83288     **  chng_addr_0:
83289     **   regPrev(0) = idx(0)
83290     **  chng_addr_1:
83291     **   regPrev(1) = idx(1)
83292     **  ...
83293     */
83294     sqlite3VdbeJumpHere(v, addrGotoChng0);
83295     for(i=0; i<nCol; i++){
83296       sqlite3VdbeJumpHere(v, aGotoChng[i]);
83297       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
83298     }
83299 
83300     /*
83301     **  chng_addr_N:
83302     **   regRowid = idx(rowid)            // STAT34 only
83303     **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
83304     **   Next csr
83305     **   if !eof(csr) goto next_row;
83306     */
83307     sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
83308 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83309     assert( regRowid==(regStat4+2) );
83310     if( HasRowid(pTab) ){
83311       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
83312     }else{
83313       Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
83314       int j, k, regKey;
83315       regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
83316       for(j=0; j<pPk->nKeyCol; j++){
83317         k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
83318         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
83319         VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
83320       }
83321       sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
83322       sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
83323     }
83324 #endif
83325     assert( regChng==(regStat4+1) );
83326     sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
83327     sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
83328     sqlite3VdbeChangeP5(v, 2+IsStat34);
83329     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow); VdbeCoverage(v);
83330 
83331     /* Add the entry to the stat1 table. */
83332     callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
83333     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
83334     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
83335     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
83336     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
83337 
83338     /* Add the entries to the stat3 or stat4 table. */
83339 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83340     {
83341       int regEq = regStat1;
83342       int regLt = regStat1+1;
83343       int regDLt = regStat1+2;
83344       int regSample = regStat1+3;
83345       int regCol = regStat1+4;
83346       int regSampleRowid = regCol + nCol;
83347       int addrNext;
83348       int addrIsNull;
83349       u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
83350 
83351       pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
83352 
83353       addrNext = sqlite3VdbeCurrentAddr(v);
83354       callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
83355       addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
83356       VdbeCoverage(v);
83357       callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
83358       callStatGet(v, regStat4, STAT_GET_NLT, regLt);
83359       callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
83360       sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
83361       /* We know that the regSampleRowid row exists because it was read by
83362       ** the previous loop.  Thus the not-found jump of seekOp will never
83363       ** be taken */
83364       VdbeCoverageNeverTaken(v);
83365 #ifdef SQLITE_ENABLE_STAT3
83366       sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
83367                                       pIdx->aiColumn[0], regSample);
83368 #else
83369       for(i=0; i<nCol; i++){
83370         i16 iCol = pIdx->aiColumn[i];
83371         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
83372       }
83373       sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
83374 #endif
83375       sqlite3VdbeAddOp3(v, OP_MakeRecord, regTabname, 6, regTemp);
83376       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
83377       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
83378       sqlite3VdbeAddOp2(v, OP_Goto, 1, addrNext); /* P1==1 for end-of-loop */
83379       sqlite3VdbeJumpHere(v, addrIsNull);
83380     }
83381 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
83382 
83383     /* End of analysis */
83384     sqlite3VdbeJumpHere(v, addrRewind);
83385     sqlite3DbFree(db, aGotoChng);
83386   }
83387 
83388 
83389   /* Create a single sqlite_stat1 entry containing NULL as the index
83390   ** name and the row count as the content.
83391   */
83392   if( pOnlyIdx==0 && needTableCnt ){
83393     VdbeComment((v, "%s", pTab->zName));
83394     sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
83395     jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1); VdbeCoverage(v);
83396     sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
83397     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
83398     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
83399     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
83400     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
83401     sqlite3VdbeJumpHere(v, jZeroRows);
83402   }
83403 }
83404 
83405 
83406 /*
83407 ** Generate code that will cause the most recent index analysis to
83408 ** be loaded into internal hash tables where is can be used.
83409 */
83410 static void loadAnalysis(Parse *pParse, int iDb){
83411   Vdbe *v = sqlite3GetVdbe(pParse);
83412   if( v ){
83413     sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
83414   }
83415 }
83416 
83417 /*
83418 ** Generate code that will do an analysis of an entire database
83419 */
83420 static void analyzeDatabase(Parse *pParse, int iDb){
83421   sqlite3 *db = pParse->db;
83422   Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
83423   HashElem *k;
83424   int iStatCur;
83425   int iMem;
83426   int iTab;
83427 
83428   sqlite3BeginWriteOperation(pParse, 0, iDb);
83429   iStatCur = pParse->nTab;
83430   pParse->nTab += 3;
83431   openStatTable(pParse, iDb, iStatCur, 0, 0);
83432   iMem = pParse->nMem+1;
83433   iTab = pParse->nTab;
83434   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
83435   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
83436     Table *pTab = (Table*)sqliteHashData(k);
83437     analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
83438   }
83439   loadAnalysis(pParse, iDb);
83440 }
83441 
83442 /*
83443 ** Generate code that will do an analysis of a single table in
83444 ** a database.  If pOnlyIdx is not NULL then it is a single index
83445 ** in pTab that should be analyzed.
83446 */
83447 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
83448   int iDb;
83449   int iStatCur;
83450 
83451   assert( pTab!=0 );
83452   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
83453   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
83454   sqlite3BeginWriteOperation(pParse, 0, iDb);
83455   iStatCur = pParse->nTab;
83456   pParse->nTab += 3;
83457   if( pOnlyIdx ){
83458     openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
83459   }else{
83460     openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
83461   }
83462   analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
83463   loadAnalysis(pParse, iDb);
83464 }
83465 
83466 /*
83467 ** Generate code for the ANALYZE command.  The parser calls this routine
83468 ** when it recognizes an ANALYZE command.
83469 **
83470 **        ANALYZE                            -- 1
83471 **        ANALYZE  <database>                -- 2
83472 **        ANALYZE  ?<database>.?<tablename>  -- 3
83473 **
83474 ** Form 1 causes all indices in all attached databases to be analyzed.
83475 ** Form 2 analyzes all indices the single database named.
83476 ** Form 3 analyzes all indices associated with the named table.
83477 */
83478 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
83479   sqlite3 *db = pParse->db;
83480   int iDb;
83481   int i;
83482   char *z, *zDb;
83483   Table *pTab;
83484   Index *pIdx;
83485   Token *pTableName;
83486 
83487   /* Read the database schema. If an error occurs, leave an error message
83488   ** and code in pParse and return NULL. */
83489   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
83490   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
83491     return;
83492   }
83493 
83494   assert( pName2!=0 || pName1==0 );
83495   if( pName1==0 ){
83496     /* Form 1:  Analyze everything */
83497     for(i=0; i<db->nDb; i++){
83498       if( i==1 ) continue;  /* Do not analyze the TEMP database */
83499       analyzeDatabase(pParse, i);
83500     }
83501   }else if( pName2->n==0 ){
83502     /* Form 2:  Analyze the database or table named */
83503     iDb = sqlite3FindDb(db, pName1);
83504     if( iDb>=0 ){
83505       analyzeDatabase(pParse, iDb);
83506     }else{
83507       z = sqlite3NameFromToken(db, pName1);
83508       if( z ){
83509         if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
83510           analyzeTable(pParse, pIdx->pTable, pIdx);
83511         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
83512           analyzeTable(pParse, pTab, 0);
83513         }
83514         sqlite3DbFree(db, z);
83515       }
83516     }
83517   }else{
83518     /* Form 3: Analyze the fully qualified table name */
83519     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
83520     if( iDb>=0 ){
83521       zDb = db->aDb[iDb].zName;
83522       z = sqlite3NameFromToken(db, pTableName);
83523       if( z ){
83524         if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
83525           analyzeTable(pParse, pIdx->pTable, pIdx);
83526         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
83527           analyzeTable(pParse, pTab, 0);
83528         }
83529         sqlite3DbFree(db, z);
83530       }
83531     }
83532   }
83533 }
83534 
83535 /*
83536 ** Used to pass information from the analyzer reader through to the
83537 ** callback routine.
83538 */
83539 typedef struct analysisInfo analysisInfo;
83540 struct analysisInfo {
83541   sqlite3 *db;
83542   const char *zDatabase;
83543 };
83544 
83545 /*
83546 ** The first argument points to a nul-terminated string containing a
83547 ** list of space separated integers. Read the first nOut of these into
83548 ** the array aOut[].
83549 */
83550 static void decodeIntArray(
83551   char *zIntArray,       /* String containing int array to decode */
83552   int nOut,              /* Number of slots in aOut[] */
83553   tRowcnt *aOut,         /* Store integers here */
83554   Index *pIndex          /* Handle extra flags for this index, if not NULL */
83555 ){
83556   char *z = zIntArray;
83557   int c;
83558   int i;
83559   tRowcnt v;
83560 
83561 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83562   if( z==0 ) z = "";
83563 #else
83564   if( NEVER(z==0) ) z = "";
83565 #endif
83566   for(i=0; *z && i<nOut; i++){
83567     v = 0;
83568     while( (c=z[0])>='0' && c<='9' ){
83569       v = v*10 + c - '0';
83570       z++;
83571     }
83572     aOut[i] = v;
83573     if( *z==' ' ) z++;
83574   }
83575 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
83576   assert( pIndex!=0 );
83577 #else
83578   if( pIndex )
83579 #endif
83580   {
83581     if( strcmp(z, "unordered")==0 ){
83582       pIndex->bUnordered = 1;
83583     }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
83584       int v32 = 0;
83585       sqlite3GetInt32(z+3, &v32);
83586       pIndex->szIdxRow = sqlite3LogEst(v32);
83587     }
83588   }
83589 }
83590 
83591 /*
83592 ** This callback is invoked once for each index when reading the
83593 ** sqlite_stat1 table.
83594 **
83595 **     argv[0] = name of the table
83596 **     argv[1] = name of the index (might be NULL)
83597 **     argv[2] = results of analysis - on integer for each column
83598 **
83599 ** Entries for which argv[1]==NULL simply record the number of rows in
83600 ** the table.
83601 */
83602 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
83603   analysisInfo *pInfo = (analysisInfo*)pData;
83604   Index *pIndex;
83605   Table *pTable;
83606   const char *z;
83607 
83608   assert( argc==3 );
83609   UNUSED_PARAMETER2(NotUsed, argc);
83610 
83611   if( argv==0 || argv[0]==0 || argv[2]==0 ){
83612     return 0;
83613   }
83614   pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
83615   if( pTable==0 ){
83616     return 0;
83617   }
83618   if( argv[1]==0 ){
83619     pIndex = 0;
83620   }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
83621     pIndex = sqlite3PrimaryKeyIndex(pTable);
83622   }else{
83623     pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
83624   }
83625   z = argv[2];
83626 
83627   if( pIndex ){
83628     decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex);
83629     if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
83630   }else{
83631     Index fakeIdx;
83632     fakeIdx.szIdxRow = pTable->szTabRow;
83633     decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
83634     pTable->szTabRow = fakeIdx.szIdxRow;
83635   }
83636 
83637   return 0;
83638 }
83639 
83640 /*
83641 ** If the Index.aSample variable is not NULL, delete the aSample[] array
83642 ** and its contents.
83643 */
83644 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
83645 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83646   if( pIdx->aSample ){
83647     int j;
83648     for(j=0; j<pIdx->nSample; j++){
83649       IndexSample *p = &pIdx->aSample[j];
83650       sqlite3DbFree(db, p->p);
83651     }
83652     sqlite3DbFree(db, pIdx->aSample);
83653   }
83654   if( db && db->pnBytesFreed==0 ){
83655     pIdx->nSample = 0;
83656     pIdx->aSample = 0;
83657   }
83658 #else
83659   UNUSED_PARAMETER(db);
83660   UNUSED_PARAMETER(pIdx);
83661 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
83662 }
83663 
83664 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83665 /*
83666 ** Populate the pIdx->aAvgEq[] array based on the samples currently
83667 ** stored in pIdx->aSample[].
83668 */
83669 static void initAvgEq(Index *pIdx){
83670   if( pIdx ){
83671     IndexSample *aSample = pIdx->aSample;
83672     IndexSample *pFinal = &aSample[pIdx->nSample-1];
83673     int iCol;
83674     for(iCol=0; iCol<pIdx->nKeyCol; iCol++){
83675       int i;                    /* Used to iterate through samples */
83676       tRowcnt sumEq = 0;        /* Sum of the nEq values */
83677       tRowcnt nSum = 0;         /* Number of terms contributing to sumEq */
83678       tRowcnt avgEq = 0;
83679       tRowcnt nDLt = pFinal->anDLt[iCol];
83680 
83681       /* Set nSum to the number of distinct (iCol+1) field prefixes that
83682       ** occur in the stat4 table for this index before pFinal. Set
83683       ** sumEq to the sum of the nEq values for column iCol for the same
83684       ** set (adding the value only once where there exist dupicate
83685       ** prefixes).  */
83686       for(i=0; i<(pIdx->nSample-1); i++){
83687         if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
83688           sumEq += aSample[i].anEq[iCol];
83689           nSum++;
83690         }
83691       }
83692       if( nDLt>nSum ){
83693         avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
83694       }
83695       if( avgEq==0 ) avgEq = 1;
83696       pIdx->aAvgEq[iCol] = avgEq;
83697       if( pIdx->nSampleCol==1 ) break;
83698     }
83699   }
83700 }
83701 
83702 /*
83703 ** Look up an index by name.  Or, if the name of a WITHOUT ROWID table
83704 ** is supplied instead, find the PRIMARY KEY index for that table.
83705 */
83706 static Index *findIndexOrPrimaryKey(
83707   sqlite3 *db,
83708   const char *zName,
83709   const char *zDb
83710 ){
83711   Index *pIdx = sqlite3FindIndex(db, zName, zDb);
83712   if( pIdx==0 ){
83713     Table *pTab = sqlite3FindTable(db, zName, zDb);
83714     if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
83715   }
83716   return pIdx;
83717 }
83718 
83719 /*
83720 ** Load the content from either the sqlite_stat4 or sqlite_stat3 table
83721 ** into the relevant Index.aSample[] arrays.
83722 **
83723 ** Arguments zSql1 and zSql2 must point to SQL statements that return
83724 ** data equivalent to the following (statements are different for stat3,
83725 ** see the caller of this function for details):
83726 **
83727 **    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
83728 **    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
83729 **
83730 ** where %Q is replaced with the database name before the SQL is executed.
83731 */
83732 static int loadStatTbl(
83733   sqlite3 *db,                  /* Database handle */
83734   int bStat3,                   /* Assume single column records only */
83735   const char *zSql1,            /* SQL statement 1 (see above) */
83736   const char *zSql2,            /* SQL statement 2 (see above) */
83737   const char *zDb               /* Database name (e.g. "main") */
83738 ){
83739   int rc;                       /* Result codes from subroutines */
83740   sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
83741   char *zSql;                   /* Text of the SQL statement */
83742   Index *pPrevIdx = 0;          /* Previous index in the loop */
83743   IndexSample *pSample;         /* A slot in pIdx->aSample[] */
83744 
83745   assert( db->lookaside.bEnabled==0 );
83746   zSql = sqlite3MPrintf(db, zSql1, zDb);
83747   if( !zSql ){
83748     return SQLITE_NOMEM;
83749   }
83750   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
83751   sqlite3DbFree(db, zSql);
83752   if( rc ) return rc;
83753 
83754   while( sqlite3_step(pStmt)==SQLITE_ROW ){
83755     int nIdxCol = 1;              /* Number of columns in stat4 records */
83756     int nAvgCol = 1;              /* Number of entries in Index.aAvgEq */
83757 
83758     char *zIndex;   /* Index name */
83759     Index *pIdx;    /* Pointer to the index object */
83760     int nSample;    /* Number of samples */
83761     int nByte;      /* Bytes of space required */
83762     int i;          /* Bytes of space required */
83763     tRowcnt *pSpace;
83764 
83765     zIndex = (char *)sqlite3_column_text(pStmt, 0);
83766     if( zIndex==0 ) continue;
83767     nSample = sqlite3_column_int(pStmt, 1);
83768     pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
83769     assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
83770     /* Index.nSample is non-zero at this point if data has already been
83771     ** loaded from the stat4 table. In this case ignore stat3 data.  */
83772     if( pIdx==0 || pIdx->nSample ) continue;
83773     if( bStat3==0 ){
83774       nIdxCol = pIdx->nKeyCol+1;
83775       nAvgCol = pIdx->nKeyCol;
83776     }
83777     pIdx->nSampleCol = nIdxCol;
83778     nByte = sizeof(IndexSample) * nSample;
83779     nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
83780     nByte += nAvgCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
83781 
83782     pIdx->aSample = sqlite3DbMallocZero(db, nByte);
83783     if( pIdx->aSample==0 ){
83784       sqlite3_finalize(pStmt);
83785       return SQLITE_NOMEM;
83786     }
83787     pSpace = (tRowcnt*)&pIdx->aSample[nSample];
83788     pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
83789     for(i=0; i<nSample; i++){
83790       pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
83791       pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
83792       pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
83793     }
83794     assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
83795   }
83796   rc = sqlite3_finalize(pStmt);
83797   if( rc ) return rc;
83798 
83799   zSql = sqlite3MPrintf(db, zSql2, zDb);
83800   if( !zSql ){
83801     return SQLITE_NOMEM;
83802   }
83803   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
83804   sqlite3DbFree(db, zSql);
83805   if( rc ) return rc;
83806 
83807   while( sqlite3_step(pStmt)==SQLITE_ROW ){
83808     char *zIndex;                 /* Index name */
83809     Index *pIdx;                  /* Pointer to the index object */
83810     int nCol = 1;                 /* Number of columns in index */
83811 
83812     zIndex = (char *)sqlite3_column_text(pStmt, 0);
83813     if( zIndex==0 ) continue;
83814     pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
83815     if( pIdx==0 ) continue;
83816     /* This next condition is true if data has already been loaded from
83817     ** the sqlite_stat4 table. In this case ignore stat3 data.  */
83818     nCol = pIdx->nSampleCol;
83819     if( bStat3 && nCol>1 ) continue;
83820     if( pIdx!=pPrevIdx ){
83821       initAvgEq(pPrevIdx);
83822       pPrevIdx = pIdx;
83823     }
83824     pSample = &pIdx->aSample[pIdx->nSample];
83825     decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
83826     decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
83827     decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
83828 
83829     /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
83830     ** This is in case the sample record is corrupted. In that case, the
83831     ** sqlite3VdbeRecordCompare() may read up to two varints past the
83832     ** end of the allocated buffer before it realizes it is dealing with
83833     ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
83834     ** a buffer overread.  */
83835     pSample->n = sqlite3_column_bytes(pStmt, 4);
83836     pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
83837     if( pSample->p==0 ){
83838       sqlite3_finalize(pStmt);
83839       return SQLITE_NOMEM;
83840     }
83841     memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
83842     pIdx->nSample++;
83843   }
83844   rc = sqlite3_finalize(pStmt);
83845   if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
83846   return rc;
83847 }
83848 
83849 /*
83850 ** Load content from the sqlite_stat4 and sqlite_stat3 tables into
83851 ** the Index.aSample[] arrays of all indices.
83852 */
83853 static int loadStat4(sqlite3 *db, const char *zDb){
83854   int rc = SQLITE_OK;             /* Result codes from subroutines */
83855 
83856   assert( db->lookaside.bEnabled==0 );
83857   if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
83858     rc = loadStatTbl(db, 0,
83859       "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
83860       "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
83861       zDb
83862     );
83863   }
83864 
83865   if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
83866     rc = loadStatTbl(db, 1,
83867       "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
83868       "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
83869       zDb
83870     );
83871   }
83872 
83873   return rc;
83874 }
83875 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
83876 
83877 /*
83878 ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
83879 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
83880 ** arrays. The contents of sqlite_stat3/4 are used to populate the
83881 ** Index.aSample[] arrays.
83882 **
83883 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
83884 ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
83885 ** during compilation and the sqlite_stat3/4 table is present, no data is
83886 ** read from it.
83887 **
83888 ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
83889 ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
83890 ** returned. However, in this case, data is read from the sqlite_stat1
83891 ** table (if it is present) before returning.
83892 **
83893 ** If an OOM error occurs, this function always sets db->mallocFailed.
83894 ** This means if the caller does not care about other errors, the return
83895 ** code may be ignored.
83896 */
83897 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
83898   analysisInfo sInfo;
83899   HashElem *i;
83900   char *zSql;
83901   int rc;
83902 
83903   assert( iDb>=0 && iDb<db->nDb );
83904   assert( db->aDb[iDb].pBt!=0 );
83905 
83906   /* Clear any prior statistics */
83907   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
83908   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
83909     Index *pIdx = sqliteHashData(i);
83910     sqlite3DefaultRowEst(pIdx);
83911 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83912     sqlite3DeleteIndexSamples(db, pIdx);
83913     pIdx->aSample = 0;
83914 #endif
83915   }
83916 
83917   /* Check to make sure the sqlite_stat1 table exists */
83918   sInfo.db = db;
83919   sInfo.zDatabase = db->aDb[iDb].zName;
83920   if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
83921     return SQLITE_ERROR;
83922   }
83923 
83924   /* Load new statistics out of the sqlite_stat1 table */
83925   zSql = sqlite3MPrintf(db,
83926       "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
83927   if( zSql==0 ){
83928     rc = SQLITE_NOMEM;
83929   }else{
83930     rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
83931     sqlite3DbFree(db, zSql);
83932   }
83933 
83934 
83935   /* Load the statistics from the sqlite_stat4 table. */
83936 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83937   if( rc==SQLITE_OK ){
83938     int lookasideEnabled = db->lookaside.bEnabled;
83939     db->lookaside.bEnabled = 0;
83940     rc = loadStat4(db, sInfo.zDatabase);
83941     db->lookaside.bEnabled = lookasideEnabled;
83942   }
83943 #endif
83944 
83945   if( rc==SQLITE_NOMEM ){
83946     db->mallocFailed = 1;
83947   }
83948   return rc;
83949 }
83950 
83951 
83952 #endif /* SQLITE_OMIT_ANALYZE */
83953 
83954 /************** End of analyze.c *********************************************/
83955 /************** Begin file attach.c ******************************************/
83956 /*
83957 ** 2003 April 6
83958 **
83959 ** The author disclaims copyright to this source code.  In place of
83960 ** a legal notice, here is a blessing:
83961 **
83962 **    May you do good and not evil.
83963 **    May you find forgiveness for yourself and forgive others.
83964 **    May you share freely, never taking more than you give.
83965 **
83966 *************************************************************************
83967 ** This file contains code used to implement the ATTACH and DETACH commands.
83968 */
83969 
83970 #ifndef SQLITE_OMIT_ATTACH
83971 /*
83972 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
83973 ** is slightly different from resolving a normal SQL expression, because simple
83974 ** identifiers are treated as strings, not possible column names or aliases.
83975 **
83976 ** i.e. if the parser sees:
83977 **
83978 **     ATTACH DATABASE abc AS def
83979 **
83980 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
83981 ** looking for columns of the same name.
83982 **
83983 ** This only applies to the root node of pExpr, so the statement:
83984 **
83985 **     ATTACH DATABASE abc||def AS 'db2'
83986 **
83987 ** will fail because neither abc or def can be resolved.
83988 */
83989 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
83990 {
83991   int rc = SQLITE_OK;
83992   if( pExpr ){
83993     if( pExpr->op!=TK_ID ){
83994       rc = sqlite3ResolveExprNames(pName, pExpr);
83995     }else{
83996       pExpr->op = TK_STRING;
83997     }
83998   }
83999   return rc;
84000 }
84001 
84002 /*
84003 ** An SQL user-function registered to do the work of an ATTACH statement. The
84004 ** three arguments to the function come directly from an attach statement:
84005 **
84006 **     ATTACH DATABASE x AS y KEY z
84007 **
84008 **     SELECT sqlite_attach(x, y, z)
84009 **
84010 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
84011 ** third argument.
84012 */
84013 static void attachFunc(
84014   sqlite3_context *context,
84015   int NotUsed,
84016   sqlite3_value **argv
84017 ){
84018   int i;
84019   int rc = 0;
84020   sqlite3 *db = sqlite3_context_db_handle(context);
84021   const char *zName;
84022   const char *zFile;
84023   char *zPath = 0;
84024   char *zErr = 0;
84025   unsigned int flags;
84026   Db *aNew;
84027   char *zErrDyn = 0;
84028   sqlite3_vfs *pVfs;
84029 
84030   UNUSED_PARAMETER(NotUsed);
84031 
84032   zFile = (const char *)sqlite3_value_text(argv[0]);
84033   zName = (const char *)sqlite3_value_text(argv[1]);
84034   if( zFile==0 ) zFile = "";
84035   if( zName==0 ) zName = "";
84036 
84037   /* Check for the following errors:
84038   **
84039   **     * Too many attached databases,
84040   **     * Transaction currently open
84041   **     * Specified database name already being used.
84042   */
84043   if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
84044     zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
84045       db->aLimit[SQLITE_LIMIT_ATTACHED]
84046     );
84047     goto attach_error;
84048   }
84049   if( !db->autoCommit ){
84050     zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
84051     goto attach_error;
84052   }
84053   for(i=0; i<db->nDb; i++){
84054     char *z = db->aDb[i].zName;
84055     assert( z && zName );
84056     if( sqlite3StrICmp(z, zName)==0 ){
84057       zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
84058       goto attach_error;
84059     }
84060   }
84061 
84062   /* Allocate the new entry in the db->aDb[] array and initialize the schema
84063   ** hash tables.
84064   */
84065   if( db->aDb==db->aDbStatic ){
84066     aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
84067     if( aNew==0 ) return;
84068     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
84069   }else{
84070     aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
84071     if( aNew==0 ) return;
84072   }
84073   db->aDb = aNew;
84074   aNew = &db->aDb[db->nDb];
84075   memset(aNew, 0, sizeof(*aNew));
84076 
84077   /* Open the database file. If the btree is successfully opened, use
84078   ** it to obtain the database schema. At this point the schema may
84079   ** or may not be initialized.
84080   */
84081   flags = db->openFlags;
84082   rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
84083   if( rc!=SQLITE_OK ){
84084     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
84085     sqlite3_result_error(context, zErr, -1);
84086     sqlite3_free(zErr);
84087     return;
84088   }
84089   assert( pVfs );
84090   flags |= SQLITE_OPEN_MAIN_DB;
84091   rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
84092   sqlite3_free( zPath );
84093   db->nDb++;
84094   if( rc==SQLITE_CONSTRAINT ){
84095     rc = SQLITE_ERROR;
84096     zErrDyn = sqlite3MPrintf(db, "database is already attached");
84097   }else if( rc==SQLITE_OK ){
84098     Pager *pPager;
84099     aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
84100     if( !aNew->pSchema ){
84101       rc = SQLITE_NOMEM;
84102     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
84103       zErrDyn = sqlite3MPrintf(db,
84104         "attached databases must use the same text encoding as main database");
84105       rc = SQLITE_ERROR;
84106     }
84107     pPager = sqlite3BtreePager(aNew->pBt);
84108     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
84109     sqlite3BtreeSecureDelete(aNew->pBt,
84110                              sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
84111 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
84112     sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
84113 #endif
84114   }
84115   aNew->safety_level = 3;
84116   aNew->zName = sqlite3DbStrDup(db, zName);
84117   if( rc==SQLITE_OK && aNew->zName==0 ){
84118     rc = SQLITE_NOMEM;
84119   }
84120 
84121 
84122 #ifdef SQLITE_HAS_CODEC
84123   if( rc==SQLITE_OK ){
84124     extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
84125     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
84126     int nKey;
84127     char *zKey;
84128     int t = sqlite3_value_type(argv[2]);
84129     switch( t ){
84130       case SQLITE_INTEGER:
84131       case SQLITE_FLOAT:
84132         zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
84133         rc = SQLITE_ERROR;
84134         break;
84135 
84136       case SQLITE_TEXT:
84137       case SQLITE_BLOB:
84138         nKey = sqlite3_value_bytes(argv[2]);
84139         zKey = (char *)sqlite3_value_blob(argv[2]);
84140         rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
84141         break;
84142 
84143       case SQLITE_NULL:
84144         /* No key specified.  Use the key from the main database */
84145         sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
84146         if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
84147           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
84148         }
84149         break;
84150     }
84151   }
84152 #endif
84153 
84154   /* If the file was opened successfully, read the schema for the new database.
84155   ** If this fails, or if opening the file failed, then close the file and
84156   ** remove the entry from the db->aDb[] array. i.e. put everything back the way
84157   ** we found it.
84158   */
84159   if( rc==SQLITE_OK ){
84160     sqlite3BtreeEnterAll(db);
84161     rc = sqlite3Init(db, &zErrDyn);
84162     sqlite3BtreeLeaveAll(db);
84163   }
84164   if( rc ){
84165     int iDb = db->nDb - 1;
84166     assert( iDb>=2 );
84167     if( db->aDb[iDb].pBt ){
84168       sqlite3BtreeClose(db->aDb[iDb].pBt);
84169       db->aDb[iDb].pBt = 0;
84170       db->aDb[iDb].pSchema = 0;
84171     }
84172     sqlite3ResetAllSchemasOfConnection(db);
84173     db->nDb = iDb;
84174     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
84175       db->mallocFailed = 1;
84176       sqlite3DbFree(db, zErrDyn);
84177       zErrDyn = sqlite3MPrintf(db, "out of memory");
84178     }else if( zErrDyn==0 ){
84179       zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
84180     }
84181     goto attach_error;
84182   }
84183 
84184   return;
84185 
84186 attach_error:
84187   /* Return an error if we get here */
84188   if( zErrDyn ){
84189     sqlite3_result_error(context, zErrDyn, -1);
84190     sqlite3DbFree(db, zErrDyn);
84191   }
84192   if( rc ) sqlite3_result_error_code(context, rc);
84193 }
84194 
84195 /*
84196 ** An SQL user-function registered to do the work of an DETACH statement. The
84197 ** three arguments to the function come directly from a detach statement:
84198 **
84199 **     DETACH DATABASE x
84200 **
84201 **     SELECT sqlite_detach(x)
84202 */
84203 static void detachFunc(
84204   sqlite3_context *context,
84205   int NotUsed,
84206   sqlite3_value **argv
84207 ){
84208   const char *zName = (const char *)sqlite3_value_text(argv[0]);
84209   sqlite3 *db = sqlite3_context_db_handle(context);
84210   int i;
84211   Db *pDb = 0;
84212   char zErr[128];
84213 
84214   UNUSED_PARAMETER(NotUsed);
84215 
84216   if( zName==0 ) zName = "";
84217   for(i=0; i<db->nDb; i++){
84218     pDb = &db->aDb[i];
84219     if( pDb->pBt==0 ) continue;
84220     if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
84221   }
84222 
84223   if( i>=db->nDb ){
84224     sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
84225     goto detach_error;
84226   }
84227   if( i<2 ){
84228     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
84229     goto detach_error;
84230   }
84231   if( !db->autoCommit ){
84232     sqlite3_snprintf(sizeof(zErr), zErr,
84233                      "cannot DETACH database within transaction");
84234     goto detach_error;
84235   }
84236   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
84237     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
84238     goto detach_error;
84239   }
84240 
84241   sqlite3BtreeClose(pDb->pBt);
84242   pDb->pBt = 0;
84243   pDb->pSchema = 0;
84244   sqlite3ResetAllSchemasOfConnection(db);
84245   return;
84246 
84247 detach_error:
84248   sqlite3_result_error(context, zErr, -1);
84249 }
84250 
84251 /*
84252 ** This procedure generates VDBE code for a single invocation of either the
84253 ** sqlite_detach() or sqlite_attach() SQL user functions.
84254 */
84255 static void codeAttach(
84256   Parse *pParse,       /* The parser context */
84257   int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
84258   FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
84259   Expr *pAuthArg,      /* Expression to pass to authorization callback */
84260   Expr *pFilename,     /* Name of database file */
84261   Expr *pDbname,       /* Name of the database to use internally */
84262   Expr *pKey           /* Database key for encryption extension */
84263 ){
84264   int rc;
84265   NameContext sName;
84266   Vdbe *v;
84267   sqlite3* db = pParse->db;
84268   int regArgs;
84269 
84270   memset(&sName, 0, sizeof(NameContext));
84271   sName.pParse = pParse;
84272 
84273   if(
84274       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
84275       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
84276       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
84277   ){
84278     pParse->nErr++;
84279     goto attach_end;
84280   }
84281 
84282 #ifndef SQLITE_OMIT_AUTHORIZATION
84283   if( pAuthArg ){
84284     char *zAuthArg;
84285     if( pAuthArg->op==TK_STRING ){
84286       zAuthArg = pAuthArg->u.zToken;
84287     }else{
84288       zAuthArg = 0;
84289     }
84290     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
84291     if(rc!=SQLITE_OK ){
84292       goto attach_end;
84293     }
84294   }
84295 #endif /* SQLITE_OMIT_AUTHORIZATION */
84296 
84297 
84298   v = sqlite3GetVdbe(pParse);
84299   regArgs = sqlite3GetTempRange(pParse, 4);
84300   sqlite3ExprCode(pParse, pFilename, regArgs);
84301   sqlite3ExprCode(pParse, pDbname, regArgs+1);
84302   sqlite3ExprCode(pParse, pKey, regArgs+2);
84303 
84304   assert( v || db->mallocFailed );
84305   if( v ){
84306     sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
84307     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
84308     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
84309     sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
84310 
84311     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
84312     ** statement only). For DETACH, set it to false (expire all existing
84313     ** statements).
84314     */
84315     sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
84316   }
84317 
84318 attach_end:
84319   sqlite3ExprDelete(db, pFilename);
84320   sqlite3ExprDelete(db, pDbname);
84321   sqlite3ExprDelete(db, pKey);
84322 }
84323 
84324 /*
84325 ** Called by the parser to compile a DETACH statement.
84326 **
84327 **     DETACH pDbname
84328 */
84329 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
84330   static const FuncDef detach_func = {
84331     1,                /* nArg */
84332     SQLITE_UTF8,      /* funcFlags */
84333     0,                /* pUserData */
84334     0,                /* pNext */
84335     detachFunc,       /* xFunc */
84336     0,                /* xStep */
84337     0,                /* xFinalize */
84338     "sqlite_detach",  /* zName */
84339     0,                /* pHash */
84340     0                 /* pDestructor */
84341   };
84342   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
84343 }
84344 
84345 /*
84346 ** Called by the parser to compile an ATTACH statement.
84347 **
84348 **     ATTACH p AS pDbname KEY pKey
84349 */
84350 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
84351   static const FuncDef attach_func = {
84352     3,                /* nArg */
84353     SQLITE_UTF8,      /* funcFlags */
84354     0,                /* pUserData */
84355     0,                /* pNext */
84356     attachFunc,       /* xFunc */
84357     0,                /* xStep */
84358     0,                /* xFinalize */
84359     "sqlite_attach",  /* zName */
84360     0,                /* pHash */
84361     0                 /* pDestructor */
84362   };
84363   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
84364 }
84365 #endif /* SQLITE_OMIT_ATTACH */
84366 
84367 /*
84368 ** Initialize a DbFixer structure.  This routine must be called prior
84369 ** to passing the structure to one of the sqliteFixAAAA() routines below.
84370 */
84371 SQLITE_PRIVATE void sqlite3FixInit(
84372   DbFixer *pFix,      /* The fixer to be initialized */
84373   Parse *pParse,      /* Error messages will be written here */
84374   int iDb,            /* This is the database that must be used */
84375   const char *zType,  /* "view", "trigger", or "index" */
84376   const Token *pName  /* Name of the view, trigger, or index */
84377 ){
84378   sqlite3 *db;
84379 
84380   db = pParse->db;
84381   assert( db->nDb>iDb );
84382   pFix->pParse = pParse;
84383   pFix->zDb = db->aDb[iDb].zName;
84384   pFix->pSchema = db->aDb[iDb].pSchema;
84385   pFix->zType = zType;
84386   pFix->pName = pName;
84387   pFix->bVarOnly = (iDb==1);
84388 }
84389 
84390 /*
84391 ** The following set of routines walk through the parse tree and assign
84392 ** a specific database to all table references where the database name
84393 ** was left unspecified in the original SQL statement.  The pFix structure
84394 ** must have been initialized by a prior call to sqlite3FixInit().
84395 **
84396 ** These routines are used to make sure that an index, trigger, or
84397 ** view in one database does not refer to objects in a different database.
84398 ** (Exception: indices, triggers, and views in the TEMP database are
84399 ** allowed to refer to anything.)  If a reference is explicitly made
84400 ** to an object in a different database, an error message is added to
84401 ** pParse->zErrMsg and these routines return non-zero.  If everything
84402 ** checks out, these routines return 0.
84403 */
84404 SQLITE_PRIVATE int sqlite3FixSrcList(
84405   DbFixer *pFix,       /* Context of the fixation */
84406   SrcList *pList       /* The Source list to check and modify */
84407 ){
84408   int i;
84409   const char *zDb;
84410   struct SrcList_item *pItem;
84411 
84412   if( NEVER(pList==0) ) return 0;
84413   zDb = pFix->zDb;
84414   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
84415     if( pFix->bVarOnly==0 ){
84416       if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
84417         sqlite3ErrorMsg(pFix->pParse,
84418             "%s %T cannot reference objects in database %s",
84419             pFix->zType, pFix->pName, pItem->zDatabase);
84420         return 1;
84421       }
84422       sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
84423       pItem->zDatabase = 0;
84424       pItem->pSchema = pFix->pSchema;
84425     }
84426 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
84427     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
84428     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
84429 #endif
84430   }
84431   return 0;
84432 }
84433 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
84434 SQLITE_PRIVATE int sqlite3FixSelect(
84435   DbFixer *pFix,       /* Context of the fixation */
84436   Select *pSelect      /* The SELECT statement to be fixed to one database */
84437 ){
84438   while( pSelect ){
84439     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
84440       return 1;
84441     }
84442     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
84443       return 1;
84444     }
84445     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
84446       return 1;
84447     }
84448     if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
84449       return 1;
84450     }
84451     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
84452       return 1;
84453     }
84454     if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
84455       return 1;
84456     }
84457     if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
84458       return 1;
84459     }
84460     if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
84461       return 1;
84462     }
84463     pSelect = pSelect->pPrior;
84464   }
84465   return 0;
84466 }
84467 SQLITE_PRIVATE int sqlite3FixExpr(
84468   DbFixer *pFix,     /* Context of the fixation */
84469   Expr *pExpr        /* The expression to be fixed to one database */
84470 ){
84471   while( pExpr ){
84472     if( pExpr->op==TK_VARIABLE ){
84473       if( pFix->pParse->db->init.busy ){
84474         pExpr->op = TK_NULL;
84475       }else{
84476         sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
84477         return 1;
84478       }
84479     }
84480     if( ExprHasProperty(pExpr, EP_TokenOnly) ) break;
84481     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
84482       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
84483     }else{
84484       if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
84485     }
84486     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
84487       return 1;
84488     }
84489     pExpr = pExpr->pLeft;
84490   }
84491   return 0;
84492 }
84493 SQLITE_PRIVATE int sqlite3FixExprList(
84494   DbFixer *pFix,     /* Context of the fixation */
84495   ExprList *pList    /* The expression to be fixed to one database */
84496 ){
84497   int i;
84498   struct ExprList_item *pItem;
84499   if( pList==0 ) return 0;
84500   for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
84501     if( sqlite3FixExpr(pFix, pItem->pExpr) ){
84502       return 1;
84503     }
84504   }
84505   return 0;
84506 }
84507 #endif
84508 
84509 #ifndef SQLITE_OMIT_TRIGGER
84510 SQLITE_PRIVATE int sqlite3FixTriggerStep(
84511   DbFixer *pFix,     /* Context of the fixation */
84512   TriggerStep *pStep /* The trigger step be fixed to one database */
84513 ){
84514   while( pStep ){
84515     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
84516       return 1;
84517     }
84518     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
84519       return 1;
84520     }
84521     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
84522       return 1;
84523     }
84524     pStep = pStep->pNext;
84525   }
84526   return 0;
84527 }
84528 #endif
84529 
84530 /************** End of attach.c **********************************************/
84531 /************** Begin file auth.c ********************************************/
84532 /*
84533 ** 2003 January 11
84534 **
84535 ** The author disclaims copyright to this source code.  In place of
84536 ** a legal notice, here is a blessing:
84537 **
84538 **    May you do good and not evil.
84539 **    May you find forgiveness for yourself and forgive others.
84540 **    May you share freely, never taking more than you give.
84541 **
84542 *************************************************************************
84543 ** This file contains code used to implement the sqlite3_set_authorizer()
84544 ** API.  This facility is an optional feature of the library.  Embedded
84545 ** systems that do not need this facility may omit it by recompiling
84546 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
84547 */
84548 
84549 /*
84550 ** All of the code in this file may be omitted by defining a single
84551 ** macro.
84552 */
84553 #ifndef SQLITE_OMIT_AUTHORIZATION
84554 
84555 /*
84556 ** Set or clear the access authorization function.
84557 **
84558 ** The access authorization function is be called during the compilation
84559 ** phase to verify that the user has read and/or write access permission on
84560 ** various fields of the database.  The first argument to the auth function
84561 ** is a copy of the 3rd argument to this routine.  The second argument
84562 ** to the auth function is one of these constants:
84563 **
84564 **       SQLITE_CREATE_INDEX
84565 **       SQLITE_CREATE_TABLE
84566 **       SQLITE_CREATE_TEMP_INDEX
84567 **       SQLITE_CREATE_TEMP_TABLE
84568 **       SQLITE_CREATE_TEMP_TRIGGER
84569 **       SQLITE_CREATE_TEMP_VIEW
84570 **       SQLITE_CREATE_TRIGGER
84571 **       SQLITE_CREATE_VIEW
84572 **       SQLITE_DELETE
84573 **       SQLITE_DROP_INDEX
84574 **       SQLITE_DROP_TABLE
84575 **       SQLITE_DROP_TEMP_INDEX
84576 **       SQLITE_DROP_TEMP_TABLE
84577 **       SQLITE_DROP_TEMP_TRIGGER
84578 **       SQLITE_DROP_TEMP_VIEW
84579 **       SQLITE_DROP_TRIGGER
84580 **       SQLITE_DROP_VIEW
84581 **       SQLITE_INSERT
84582 **       SQLITE_PRAGMA
84583 **       SQLITE_READ
84584 **       SQLITE_SELECT
84585 **       SQLITE_TRANSACTION
84586 **       SQLITE_UPDATE
84587 **
84588 ** The third and fourth arguments to the auth function are the name of
84589 ** the table and the column that are being accessed.  The auth function
84590 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
84591 ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
84592 ** means that the SQL statement will never-run - the sqlite3_exec() call
84593 ** will return with an error.  SQLITE_IGNORE means that the SQL statement
84594 ** should run but attempts to read the specified column will return NULL
84595 ** and attempts to write the column will be ignored.
84596 **
84597 ** Setting the auth function to NULL disables this hook.  The default
84598 ** setting of the auth function is NULL.
84599 */
84600 SQLITE_API int sqlite3_set_authorizer(
84601   sqlite3 *db,
84602   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
84603   void *pArg
84604 ){
84605   sqlite3_mutex_enter(db->mutex);
84606   db->xAuth = xAuth;
84607   db->pAuthArg = pArg;
84608   sqlite3ExpirePreparedStatements(db);
84609   sqlite3_mutex_leave(db->mutex);
84610   return SQLITE_OK;
84611 }
84612 
84613 /*
84614 ** Write an error message into pParse->zErrMsg that explains that the
84615 ** user-supplied authorization function returned an illegal value.
84616 */
84617 static void sqliteAuthBadReturnCode(Parse *pParse){
84618   sqlite3ErrorMsg(pParse, "authorizer malfunction");
84619   pParse->rc = SQLITE_ERROR;
84620 }
84621 
84622 /*
84623 ** Invoke the authorization callback for permission to read column zCol from
84624 ** table zTab in database zDb. This function assumes that an authorization
84625 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
84626 **
84627 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
84628 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
84629 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
84630 */
84631 SQLITE_PRIVATE int sqlite3AuthReadCol(
84632   Parse *pParse,                  /* The parser context */
84633   const char *zTab,               /* Table name */
84634   const char *zCol,               /* Column name */
84635   int iDb                         /* Index of containing database. */
84636 ){
84637   sqlite3 *db = pParse->db;       /* Database handle */
84638   char *zDb = db->aDb[iDb].zName; /* Name of attached database */
84639   int rc;                         /* Auth callback return code */
84640 
84641   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
84642   if( rc==SQLITE_DENY ){
84643     if( db->nDb>2 || iDb!=0 ){
84644       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
84645     }else{
84646       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
84647     }
84648     pParse->rc = SQLITE_AUTH;
84649   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
84650     sqliteAuthBadReturnCode(pParse);
84651   }
84652   return rc;
84653 }
84654 
84655 /*
84656 ** The pExpr should be a TK_COLUMN expression.  The table referred to
84657 ** is in pTabList or else it is the NEW or OLD table of a trigger.
84658 ** Check to see if it is OK to read this particular column.
84659 **
84660 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
84661 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
84662 ** then generate an error.
84663 */
84664 SQLITE_PRIVATE void sqlite3AuthRead(
84665   Parse *pParse,        /* The parser context */
84666   Expr *pExpr,          /* The expression to check authorization on */
84667   Schema *pSchema,      /* The schema of the expression */
84668   SrcList *pTabList     /* All table that pExpr might refer to */
84669 ){
84670   sqlite3 *db = pParse->db;
84671   Table *pTab = 0;      /* The table being read */
84672   const char *zCol;     /* Name of the column of the table */
84673   int iSrc;             /* Index in pTabList->a[] of table being read */
84674   int iDb;              /* The index of the database the expression refers to */
84675   int iCol;             /* Index of column in table */
84676 
84677   if( db->xAuth==0 ) return;
84678   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
84679   if( iDb<0 ){
84680     /* An attempt to read a column out of a subquery or other
84681     ** temporary table. */
84682     return;
84683   }
84684 
84685   assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
84686   if( pExpr->op==TK_TRIGGER ){
84687     pTab = pParse->pTriggerTab;
84688   }else{
84689     assert( pTabList );
84690     for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
84691       if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
84692         pTab = pTabList->a[iSrc].pTab;
84693         break;
84694       }
84695     }
84696   }
84697   iCol = pExpr->iColumn;
84698   if( NEVER(pTab==0) ) return;
84699 
84700   if( iCol>=0 ){
84701     assert( iCol<pTab->nCol );
84702     zCol = pTab->aCol[iCol].zName;
84703   }else if( pTab->iPKey>=0 ){
84704     assert( pTab->iPKey<pTab->nCol );
84705     zCol = pTab->aCol[pTab->iPKey].zName;
84706   }else{
84707     zCol = "ROWID";
84708   }
84709   assert( iDb>=0 && iDb<db->nDb );
84710   if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
84711     pExpr->op = TK_NULL;
84712   }
84713 }
84714 
84715 /*
84716 ** Do an authorization check using the code and arguments given.  Return
84717 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
84718 ** is returned, then the error count and error message in pParse are
84719 ** modified appropriately.
84720 */
84721 SQLITE_PRIVATE int sqlite3AuthCheck(
84722   Parse *pParse,
84723   int code,
84724   const char *zArg1,
84725   const char *zArg2,
84726   const char *zArg3
84727 ){
84728   sqlite3 *db = pParse->db;
84729   int rc;
84730 
84731   /* Don't do any authorization checks if the database is initialising
84732   ** or if the parser is being invoked from within sqlite3_declare_vtab.
84733   */
84734   if( db->init.busy || IN_DECLARE_VTAB ){
84735     return SQLITE_OK;
84736   }
84737 
84738   if( db->xAuth==0 ){
84739     return SQLITE_OK;
84740   }
84741   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
84742   if( rc==SQLITE_DENY ){
84743     sqlite3ErrorMsg(pParse, "not authorized");
84744     pParse->rc = SQLITE_AUTH;
84745   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
84746     rc = SQLITE_DENY;
84747     sqliteAuthBadReturnCode(pParse);
84748   }
84749   return rc;
84750 }
84751 
84752 /*
84753 ** Push an authorization context.  After this routine is called, the
84754 ** zArg3 argument to authorization callbacks will be zContext until
84755 ** popped.  Or if pParse==0, this routine is a no-op.
84756 */
84757 SQLITE_PRIVATE void sqlite3AuthContextPush(
84758   Parse *pParse,
84759   AuthContext *pContext,
84760   const char *zContext
84761 ){
84762   assert( pParse );
84763   pContext->pParse = pParse;
84764   pContext->zAuthContext = pParse->zAuthContext;
84765   pParse->zAuthContext = zContext;
84766 }
84767 
84768 /*
84769 ** Pop an authorization context that was previously pushed
84770 ** by sqlite3AuthContextPush
84771 */
84772 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
84773   if( pContext->pParse ){
84774     pContext->pParse->zAuthContext = pContext->zAuthContext;
84775     pContext->pParse = 0;
84776   }
84777 }
84778 
84779 #endif /* SQLITE_OMIT_AUTHORIZATION */
84780 
84781 /************** End of auth.c ************************************************/
84782 /************** Begin file build.c *******************************************/
84783 /*
84784 ** 2001 September 15
84785 **
84786 ** The author disclaims copyright to this source code.  In place of
84787 ** a legal notice, here is a blessing:
84788 **
84789 **    May you do good and not evil.
84790 **    May you find forgiveness for yourself and forgive others.
84791 **    May you share freely, never taking more than you give.
84792 **
84793 *************************************************************************
84794 ** This file contains C code routines that are called by the SQLite parser
84795 ** when syntax rules are reduced.  The routines in this file handle the
84796 ** following kinds of SQL syntax:
84797 **
84798 **     CREATE TABLE
84799 **     DROP TABLE
84800 **     CREATE INDEX
84801 **     DROP INDEX
84802 **     creating ID lists
84803 **     BEGIN TRANSACTION
84804 **     COMMIT
84805 **     ROLLBACK
84806 */
84807 
84808 /*
84809 ** This routine is called when a new SQL statement is beginning to
84810 ** be parsed.  Initialize the pParse structure as needed.
84811 */
84812 SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
84813   pParse->explain = (u8)explainFlag;
84814   pParse->nVar = 0;
84815 }
84816 
84817 #ifndef SQLITE_OMIT_SHARED_CACHE
84818 /*
84819 ** The TableLock structure is only used by the sqlite3TableLock() and
84820 ** codeTableLocks() functions.
84821 */
84822 struct TableLock {
84823   int iDb;             /* The database containing the table to be locked */
84824   int iTab;            /* The root page of the table to be locked */
84825   u8 isWriteLock;      /* True for write lock.  False for a read lock */
84826   const char *zName;   /* Name of the table */
84827 };
84828 
84829 /*
84830 ** Record the fact that we want to lock a table at run-time.
84831 **
84832 ** The table to be locked has root page iTab and is found in database iDb.
84833 ** A read or a write lock can be taken depending on isWritelock.
84834 **
84835 ** This routine just records the fact that the lock is desired.  The
84836 ** code to make the lock occur is generated by a later call to
84837 ** codeTableLocks() which occurs during sqlite3FinishCoding().
84838 */
84839 SQLITE_PRIVATE void sqlite3TableLock(
84840   Parse *pParse,     /* Parsing context */
84841   int iDb,           /* Index of the database containing the table to lock */
84842   int iTab,          /* Root page number of the table to be locked */
84843   u8 isWriteLock,    /* True for a write lock */
84844   const char *zName  /* Name of the table to be locked */
84845 ){
84846   Parse *pToplevel = sqlite3ParseToplevel(pParse);
84847   int i;
84848   int nBytes;
84849   TableLock *p;
84850   assert( iDb>=0 );
84851 
84852   for(i=0; i<pToplevel->nTableLock; i++){
84853     p = &pToplevel->aTableLock[i];
84854     if( p->iDb==iDb && p->iTab==iTab ){
84855       p->isWriteLock = (p->isWriteLock || isWriteLock);
84856       return;
84857     }
84858   }
84859 
84860   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
84861   pToplevel->aTableLock =
84862       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
84863   if( pToplevel->aTableLock ){
84864     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
84865     p->iDb = iDb;
84866     p->iTab = iTab;
84867     p->isWriteLock = isWriteLock;
84868     p->zName = zName;
84869   }else{
84870     pToplevel->nTableLock = 0;
84871     pToplevel->db->mallocFailed = 1;
84872   }
84873 }
84874 
84875 /*
84876 ** Code an OP_TableLock instruction for each table locked by the
84877 ** statement (configured by calls to sqlite3TableLock()).
84878 */
84879 static void codeTableLocks(Parse *pParse){
84880   int i;
84881   Vdbe *pVdbe;
84882 
84883   pVdbe = sqlite3GetVdbe(pParse);
84884   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
84885 
84886   for(i=0; i<pParse->nTableLock; i++){
84887     TableLock *p = &pParse->aTableLock[i];
84888     int p1 = p->iDb;
84889     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
84890                       p->zName, P4_STATIC);
84891   }
84892 }
84893 #else
84894   #define codeTableLocks(x)
84895 #endif
84896 
84897 /*
84898 ** This routine is called after a single SQL statement has been
84899 ** parsed and a VDBE program to execute that statement has been
84900 ** prepared.  This routine puts the finishing touches on the
84901 ** VDBE program and resets the pParse structure for the next
84902 ** parse.
84903 **
84904 ** Note that if an error occurred, it might be the case that
84905 ** no VDBE code was generated.
84906 */
84907 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
84908   sqlite3 *db;
84909   Vdbe *v;
84910 
84911   assert( pParse->pToplevel==0 );
84912   db = pParse->db;
84913   if( db->mallocFailed ) return;
84914   if( pParse->nested ) return;
84915   if( pParse->nErr ) return;
84916 
84917   /* Begin by generating some termination code at the end of the
84918   ** vdbe program
84919   */
84920   v = sqlite3GetVdbe(pParse);
84921   assert( !pParse->isMultiWrite
84922        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
84923   if( v ){
84924     while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
84925     sqlite3VdbeAddOp0(v, OP_Halt);
84926 
84927     /* The cookie mask contains one bit for each database file open.
84928     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
84929     ** set for each database that is used.  Generate code to start a
84930     ** transaction on each used database and to verify the schema cookie
84931     ** on each used database.
84932     */
84933     if( db->mallocFailed==0 && (pParse->cookieMask || pParse->pConstExpr) ){
84934       yDbMask mask;
84935       int iDb, i;
84936       assert( sqlite3VdbeGetOp(v, 0)->opcode==OP_Init );
84937       sqlite3VdbeJumpHere(v, 0);
84938       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
84939         if( (mask & pParse->cookieMask)==0 ) continue;
84940         sqlite3VdbeUsesBtree(v, iDb);
84941         sqlite3VdbeAddOp4Int(v,
84942           OP_Transaction,                    /* Opcode */
84943           iDb,                               /* P1 */
84944           (mask & pParse->writeMask)!=0,     /* P2 */
84945           pParse->cookieValue[iDb],          /* P3 */
84946           db->aDb[iDb].pSchema->iGeneration  /* P4 */
84947         );
84948         if( db->init.busy==0 ) sqlite3VdbeChangeP5(v, 1);
84949       }
84950 #ifndef SQLITE_OMIT_VIRTUALTABLE
84951       for(i=0; i<pParse->nVtabLock; i++){
84952         char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
84953         sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
84954       }
84955       pParse->nVtabLock = 0;
84956 #endif
84957 
84958       /* Once all the cookies have been verified and transactions opened,
84959       ** obtain the required table-locks. This is a no-op unless the
84960       ** shared-cache feature is enabled.
84961       */
84962       codeTableLocks(pParse);
84963 
84964       /* Initialize any AUTOINCREMENT data structures required.
84965       */
84966       sqlite3AutoincrementBegin(pParse);
84967 
84968       /* Code constant expressions that where factored out of inner loops */
84969       if( pParse->pConstExpr ){
84970         ExprList *pEL = pParse->pConstExpr;
84971         pParse->okConstFactor = 0;
84972         for(i=0; i<pEL->nExpr; i++){
84973           sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
84974         }
84975       }
84976 
84977       /* Finally, jump back to the beginning of the executable code. */
84978       sqlite3VdbeAddOp2(v, OP_Goto, 0, 1);
84979     }
84980   }
84981 
84982 
84983   /* Get the VDBE program ready for execution
84984   */
84985   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
84986     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
84987     /* A minimum of one cursor is required if autoincrement is used
84988     *  See ticket [a696379c1f08866] */
84989     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
84990     sqlite3VdbeMakeReady(v, pParse);
84991     pParse->rc = SQLITE_DONE;
84992     pParse->colNamesSet = 0;
84993   }else{
84994     pParse->rc = SQLITE_ERROR;
84995   }
84996   pParse->nTab = 0;
84997   pParse->nMem = 0;
84998   pParse->nSet = 0;
84999   pParse->nVar = 0;
85000   pParse->cookieMask = 0;
85001 }
85002 
85003 /*
85004 ** Run the parser and code generator recursively in order to generate
85005 ** code for the SQL statement given onto the end of the pParse context
85006 ** currently under construction.  When the parser is run recursively
85007 ** this way, the final OP_Halt is not appended and other initialization
85008 ** and finalization steps are omitted because those are handling by the
85009 ** outermost parser.
85010 **
85011 ** Not everything is nestable.  This facility is designed to permit
85012 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
85013 ** care if you decide to try to use this routine for some other purposes.
85014 */
85015 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
85016   va_list ap;
85017   char *zSql;
85018   char *zErrMsg = 0;
85019   sqlite3 *db = pParse->db;
85020 # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
85021   char saveBuf[SAVE_SZ];
85022 
85023   if( pParse->nErr ) return;
85024   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
85025   va_start(ap, zFormat);
85026   zSql = sqlite3VMPrintf(db, zFormat, ap);
85027   va_end(ap);
85028   if( zSql==0 ){
85029     return;   /* A malloc must have failed */
85030   }
85031   pParse->nested++;
85032   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
85033   memset(&pParse->nVar, 0, SAVE_SZ);
85034   sqlite3RunParser(pParse, zSql, &zErrMsg);
85035   sqlite3DbFree(db, zErrMsg);
85036   sqlite3DbFree(db, zSql);
85037   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
85038   pParse->nested--;
85039 }
85040 
85041 /*
85042 ** Locate the in-memory structure that describes a particular database
85043 ** table given the name of that table and (optionally) the name of the
85044 ** database containing the table.  Return NULL if not found.
85045 **
85046 ** If zDatabase is 0, all databases are searched for the table and the
85047 ** first matching table is returned.  (No checking for duplicate table
85048 ** names is done.)  The search order is TEMP first, then MAIN, then any
85049 ** auxiliary databases added using the ATTACH command.
85050 **
85051 ** See also sqlite3LocateTable().
85052 */
85053 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
85054   Table *p = 0;
85055   int i;
85056   int nName;
85057   assert( zName!=0 );
85058   nName = sqlite3Strlen30(zName);
85059   /* All mutexes are required for schema access.  Make sure we hold them. */
85060   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
85061   for(i=OMIT_TEMPDB; i<db->nDb; i++){
85062     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
85063     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
85064     assert( sqlite3SchemaMutexHeld(db, j, 0) );
85065     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
85066     if( p ) break;
85067   }
85068   return p;
85069 }
85070 
85071 /*
85072 ** Locate the in-memory structure that describes a particular database
85073 ** table given the name of that table and (optionally) the name of the
85074 ** database containing the table.  Return NULL if not found.  Also leave an
85075 ** error message in pParse->zErrMsg.
85076 **
85077 ** The difference between this routine and sqlite3FindTable() is that this
85078 ** routine leaves an error message in pParse->zErrMsg where
85079 ** sqlite3FindTable() does not.
85080 */
85081 SQLITE_PRIVATE Table *sqlite3LocateTable(
85082   Parse *pParse,         /* context in which to report errors */
85083   int isView,            /* True if looking for a VIEW rather than a TABLE */
85084   const char *zName,     /* Name of the table we are looking for */
85085   const char *zDbase     /* Name of the database.  Might be NULL */
85086 ){
85087   Table *p;
85088 
85089   /* Read the database schema. If an error occurs, leave an error message
85090   ** and code in pParse and return NULL. */
85091   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
85092     return 0;
85093   }
85094 
85095   p = sqlite3FindTable(pParse->db, zName, zDbase);
85096   if( p==0 ){
85097     const char *zMsg = isView ? "no such view" : "no such table";
85098     if( zDbase ){
85099       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
85100     }else{
85101       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
85102     }
85103     pParse->checkSchema = 1;
85104   }
85105   return p;
85106 }
85107 
85108 /*
85109 ** Locate the table identified by *p.
85110 **
85111 ** This is a wrapper around sqlite3LocateTable(). The difference between
85112 ** sqlite3LocateTable() and this function is that this function restricts
85113 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
85114 ** non-NULL if it is part of a view or trigger program definition. See
85115 ** sqlite3FixSrcList() for details.
85116 */
85117 SQLITE_PRIVATE Table *sqlite3LocateTableItem(
85118   Parse *pParse,
85119   int isView,
85120   struct SrcList_item *p
85121 ){
85122   const char *zDb;
85123   assert( p->pSchema==0 || p->zDatabase==0 );
85124   if( p->pSchema ){
85125     int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
85126     zDb = pParse->db->aDb[iDb].zName;
85127   }else{
85128     zDb = p->zDatabase;
85129   }
85130   return sqlite3LocateTable(pParse, isView, p->zName, zDb);
85131 }
85132 
85133 /*
85134 ** Locate the in-memory structure that describes
85135 ** a particular index given the name of that index
85136 ** and the name of the database that contains the index.
85137 ** Return NULL if not found.
85138 **
85139 ** If zDatabase is 0, all databases are searched for the
85140 ** table and the first matching index is returned.  (No checking
85141 ** for duplicate index names is done.)  The search order is
85142 ** TEMP first, then MAIN, then any auxiliary databases added
85143 ** using the ATTACH command.
85144 */
85145 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
85146   Index *p = 0;
85147   int i;
85148   int nName = sqlite3Strlen30(zName);
85149   /* All mutexes are required for schema access.  Make sure we hold them. */
85150   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
85151   for(i=OMIT_TEMPDB; i<db->nDb; i++){
85152     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
85153     Schema *pSchema = db->aDb[j].pSchema;
85154     assert( pSchema );
85155     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
85156     assert( sqlite3SchemaMutexHeld(db, j, 0) );
85157     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
85158     if( p ) break;
85159   }
85160   return p;
85161 }
85162 
85163 /*
85164 ** Reclaim the memory used by an index
85165 */
85166 static void freeIndex(sqlite3 *db, Index *p){
85167 #ifndef SQLITE_OMIT_ANALYZE
85168   sqlite3DeleteIndexSamples(db, p);
85169 #endif
85170   if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo);
85171   sqlite3ExprDelete(db, p->pPartIdxWhere);
85172   sqlite3DbFree(db, p->zColAff);
85173   if( p->isResized ) sqlite3DbFree(db, p->azColl);
85174   sqlite3DbFree(db, p);
85175 }
85176 
85177 /*
85178 ** For the index called zIdxName which is found in the database iDb,
85179 ** unlike that index from its Table then remove the index from
85180 ** the index hash table and free all memory structures associated
85181 ** with the index.
85182 */
85183 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
85184   Index *pIndex;
85185   int len;
85186   Hash *pHash;
85187 
85188   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
85189   pHash = &db->aDb[iDb].pSchema->idxHash;
85190   len = sqlite3Strlen30(zIdxName);
85191   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
85192   if( ALWAYS(pIndex) ){
85193     if( pIndex->pTable->pIndex==pIndex ){
85194       pIndex->pTable->pIndex = pIndex->pNext;
85195     }else{
85196       Index *p;
85197       /* Justification of ALWAYS();  The index must be on the list of
85198       ** indices. */
85199       p = pIndex->pTable->pIndex;
85200       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
85201       if( ALWAYS(p && p->pNext==pIndex) ){
85202         p->pNext = pIndex->pNext;
85203       }
85204     }
85205     freeIndex(db, pIndex);
85206   }
85207   db->flags |= SQLITE_InternChanges;
85208 }
85209 
85210 /*
85211 ** Look through the list of open database files in db->aDb[] and if
85212 ** any have been closed, remove them from the list.  Reallocate the
85213 ** db->aDb[] structure to a smaller size, if possible.
85214 **
85215 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
85216 ** are never candidates for being collapsed.
85217 */
85218 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
85219   int i, j;
85220   for(i=j=2; i<db->nDb; i++){
85221     struct Db *pDb = &db->aDb[i];
85222     if( pDb->pBt==0 ){
85223       sqlite3DbFree(db, pDb->zName);
85224       pDb->zName = 0;
85225       continue;
85226     }
85227     if( j<i ){
85228       db->aDb[j] = db->aDb[i];
85229     }
85230     j++;
85231   }
85232   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
85233   db->nDb = j;
85234   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
85235     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
85236     sqlite3DbFree(db, db->aDb);
85237     db->aDb = db->aDbStatic;
85238   }
85239 }
85240 
85241 /*
85242 ** Reset the schema for the database at index iDb.  Also reset the
85243 ** TEMP schema.
85244 */
85245 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
85246   Db *pDb;
85247   assert( iDb<db->nDb );
85248 
85249   /* Case 1:  Reset the single schema identified by iDb */
85250   pDb = &db->aDb[iDb];
85251   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
85252   assert( pDb->pSchema!=0 );
85253   sqlite3SchemaClear(pDb->pSchema);
85254 
85255   /* If any database other than TEMP is reset, then also reset TEMP
85256   ** since TEMP might be holding triggers that reference tables in the
85257   ** other database.
85258   */
85259   if( iDb!=1 ){
85260     pDb = &db->aDb[1];
85261     assert( pDb->pSchema!=0 );
85262     sqlite3SchemaClear(pDb->pSchema);
85263   }
85264   return;
85265 }
85266 
85267 /*
85268 ** Erase all schema information from all attached databases (including
85269 ** "main" and "temp") for a single database connection.
85270 */
85271 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
85272   int i;
85273   sqlite3BtreeEnterAll(db);
85274   for(i=0; i<db->nDb; i++){
85275     Db *pDb = &db->aDb[i];
85276     if( pDb->pSchema ){
85277       sqlite3SchemaClear(pDb->pSchema);
85278     }
85279   }
85280   db->flags &= ~SQLITE_InternChanges;
85281   sqlite3VtabUnlockList(db);
85282   sqlite3BtreeLeaveAll(db);
85283   sqlite3CollapseDatabaseArray(db);
85284 }
85285 
85286 /*
85287 ** This routine is called when a commit occurs.
85288 */
85289 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
85290   db->flags &= ~SQLITE_InternChanges;
85291 }
85292 
85293 /*
85294 ** Delete memory allocated for the column names of a table or view (the
85295 ** Table.aCol[] array).
85296 */
85297 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
85298   int i;
85299   Column *pCol;
85300   assert( pTable!=0 );
85301   if( (pCol = pTable->aCol)!=0 ){
85302     for(i=0; i<pTable->nCol; i++, pCol++){
85303       sqlite3DbFree(db, pCol->zName);
85304       sqlite3ExprDelete(db, pCol->pDflt);
85305       sqlite3DbFree(db, pCol->zDflt);
85306       sqlite3DbFree(db, pCol->zType);
85307       sqlite3DbFree(db, pCol->zColl);
85308     }
85309     sqlite3DbFree(db, pTable->aCol);
85310   }
85311 }
85312 
85313 /*
85314 ** Remove the memory data structures associated with the given
85315 ** Table.  No changes are made to disk by this routine.
85316 **
85317 ** This routine just deletes the data structure.  It does not unlink
85318 ** the table data structure from the hash table.  But it does destroy
85319 ** memory structures of the indices and foreign keys associated with
85320 ** the table.
85321 **
85322 ** The db parameter is optional.  It is needed if the Table object
85323 ** contains lookaside memory.  (Table objects in the schema do not use
85324 ** lookaside memory, but some ephemeral Table objects do.)  Or the
85325 ** db parameter can be used with db->pnBytesFreed to measure the memory
85326 ** used by the Table object.
85327 */
85328 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
85329   Index *pIndex, *pNext;
85330   TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
85331 
85332   assert( !pTable || pTable->nRef>0 );
85333 
85334   /* Do not delete the table until the reference count reaches zero. */
85335   if( !pTable ) return;
85336   if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
85337 
85338   /* Record the number of outstanding lookaside allocations in schema Tables
85339   ** prior to doing any free() operations.  Since schema Tables do not use
85340   ** lookaside, this number should not change. */
85341   TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
85342                          db->lookaside.nOut : 0 );
85343 
85344   /* Delete all indices associated with this table. */
85345   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
85346     pNext = pIndex->pNext;
85347     assert( pIndex->pSchema==pTable->pSchema );
85348     if( !db || db->pnBytesFreed==0 ){
85349       char *zName = pIndex->zName;
85350       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
85351          &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
85352       );
85353       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
85354       assert( pOld==pIndex || pOld==0 );
85355     }
85356     freeIndex(db, pIndex);
85357   }
85358 
85359   /* Delete any foreign keys attached to this table. */
85360   sqlite3FkDelete(db, pTable);
85361 
85362   /* Delete the Table structure itself.
85363   */
85364   sqliteDeleteColumnNames(db, pTable);
85365   sqlite3DbFree(db, pTable->zName);
85366   sqlite3DbFree(db, pTable->zColAff);
85367   sqlite3SelectDelete(db, pTable->pSelect);
85368 #ifndef SQLITE_OMIT_CHECK
85369   sqlite3ExprListDelete(db, pTable->pCheck);
85370 #endif
85371 #ifndef SQLITE_OMIT_VIRTUALTABLE
85372   sqlite3VtabClear(db, pTable);
85373 #endif
85374   sqlite3DbFree(db, pTable);
85375 
85376   /* Verify that no lookaside memory was used by schema tables */
85377   assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
85378 }
85379 
85380 /*
85381 ** Unlink the given table from the hash tables and the delete the
85382 ** table structure with all its indices and foreign keys.
85383 */
85384 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
85385   Table *p;
85386   Db *pDb;
85387 
85388   assert( db!=0 );
85389   assert( iDb>=0 && iDb<db->nDb );
85390   assert( zTabName );
85391   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
85392   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
85393   pDb = &db->aDb[iDb];
85394   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
85395                         sqlite3Strlen30(zTabName),0);
85396   sqlite3DeleteTable(db, p);
85397   db->flags |= SQLITE_InternChanges;
85398 }
85399 
85400 /*
85401 ** Given a token, return a string that consists of the text of that
85402 ** token.  Space to hold the returned string
85403 ** is obtained from sqliteMalloc() and must be freed by the calling
85404 ** function.
85405 **
85406 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
85407 ** surround the body of the token are removed.
85408 **
85409 ** Tokens are often just pointers into the original SQL text and so
85410 ** are not \000 terminated and are not persistent.  The returned string
85411 ** is \000 terminated and is persistent.
85412 */
85413 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
85414   char *zName;
85415   if( pName ){
85416     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
85417     sqlite3Dequote(zName);
85418   }else{
85419     zName = 0;
85420   }
85421   return zName;
85422 }
85423 
85424 /*
85425 ** Open the sqlite_master table stored in database number iDb for
85426 ** writing. The table is opened using cursor 0.
85427 */
85428 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
85429   Vdbe *v = sqlite3GetVdbe(p);
85430   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
85431   sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
85432   if( p->nTab==0 ){
85433     p->nTab = 1;
85434   }
85435 }
85436 
85437 /*
85438 ** Parameter zName points to a nul-terminated buffer containing the name
85439 ** of a database ("main", "temp" or the name of an attached db). This
85440 ** function returns the index of the named database in db->aDb[], or
85441 ** -1 if the named db cannot be found.
85442 */
85443 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
85444   int i = -1;         /* Database number */
85445   if( zName ){
85446     Db *pDb;
85447     int n = sqlite3Strlen30(zName);
85448     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
85449       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
85450           0==sqlite3StrICmp(pDb->zName, zName) ){
85451         break;
85452       }
85453     }
85454   }
85455   return i;
85456 }
85457 
85458 /*
85459 ** The token *pName contains the name of a database (either "main" or
85460 ** "temp" or the name of an attached db). This routine returns the
85461 ** index of the named database in db->aDb[], or -1 if the named db
85462 ** does not exist.
85463 */
85464 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
85465   int i;                               /* Database number */
85466   char *zName;                         /* Name we are searching for */
85467   zName = sqlite3NameFromToken(db, pName);
85468   i = sqlite3FindDbName(db, zName);
85469   sqlite3DbFree(db, zName);
85470   return i;
85471 }
85472 
85473 /* The table or view or trigger name is passed to this routine via tokens
85474 ** pName1 and pName2. If the table name was fully qualified, for example:
85475 **
85476 ** CREATE TABLE xxx.yyy (...);
85477 **
85478 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
85479 ** the table name is not fully qualified, i.e.:
85480 **
85481 ** CREATE TABLE yyy(...);
85482 **
85483 ** Then pName1 is set to "yyy" and pName2 is "".
85484 **
85485 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
85486 ** pName2) that stores the unqualified table name.  The index of the
85487 ** database "xxx" is returned.
85488 */
85489 SQLITE_PRIVATE int sqlite3TwoPartName(
85490   Parse *pParse,      /* Parsing and code generating context */
85491   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
85492   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
85493   Token **pUnqual     /* Write the unqualified object name here */
85494 ){
85495   int iDb;                    /* Database holding the object */
85496   sqlite3 *db = pParse->db;
85497 
85498   if( ALWAYS(pName2!=0) && pName2->n>0 ){
85499     if( db->init.busy ) {
85500       sqlite3ErrorMsg(pParse, "corrupt database");
85501       pParse->nErr++;
85502       return -1;
85503     }
85504     *pUnqual = pName2;
85505     iDb = sqlite3FindDb(db, pName1);
85506     if( iDb<0 ){
85507       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
85508       pParse->nErr++;
85509       return -1;
85510     }
85511   }else{
85512     assert( db->init.iDb==0 || db->init.busy );
85513     iDb = db->init.iDb;
85514     *pUnqual = pName1;
85515   }
85516   return iDb;
85517 }
85518 
85519 /*
85520 ** This routine is used to check if the UTF-8 string zName is a legal
85521 ** unqualified name for a new schema object (table, index, view or
85522 ** trigger). All names are legal except those that begin with the string
85523 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
85524 ** is reserved for internal use.
85525 */
85526 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
85527   if( !pParse->db->init.busy && pParse->nested==0
85528           && (pParse->db->flags & SQLITE_WriteSchema)==0
85529           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
85530     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
85531     return SQLITE_ERROR;
85532   }
85533   return SQLITE_OK;
85534 }
85535 
85536 /*
85537 ** Return the PRIMARY KEY index of a table
85538 */
85539 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
85540   Index *p;
85541   for(p=pTab->pIndex; p && p->autoIndex!=2; p=p->pNext){}
85542   return p;
85543 }
85544 
85545 /*
85546 ** Return the column of index pIdx that corresponds to table
85547 ** column iCol.  Return -1 if not found.
85548 */
85549 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
85550   int i;
85551   for(i=0; i<pIdx->nColumn; i++){
85552     if( iCol==pIdx->aiColumn[i] ) return i;
85553   }
85554   return -1;
85555 }
85556 
85557 /*
85558 ** Begin constructing a new table representation in memory.  This is
85559 ** the first of several action routines that get called in response
85560 ** to a CREATE TABLE statement.  In particular, this routine is called
85561 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
85562 ** flag is true if the table should be stored in the auxiliary database
85563 ** file instead of in the main database file.  This is normally the case
85564 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
85565 ** CREATE and TABLE.
85566 **
85567 ** The new table record is initialized and put in pParse->pNewTable.
85568 ** As more of the CREATE TABLE statement is parsed, additional action
85569 ** routines will be called to add more information to this record.
85570 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
85571 ** is called to complete the construction of the new table record.
85572 */
85573 SQLITE_PRIVATE void sqlite3StartTable(
85574   Parse *pParse,   /* Parser context */
85575   Token *pName1,   /* First part of the name of the table or view */
85576   Token *pName2,   /* Second part of the name of the table or view */
85577   int isTemp,      /* True if this is a TEMP table */
85578   int isView,      /* True if this is a VIEW */
85579   int isVirtual,   /* True if this is a VIRTUAL table */
85580   int noErr        /* Do nothing if table already exists */
85581 ){
85582   Table *pTable;
85583   char *zName = 0; /* The name of the new table */
85584   sqlite3 *db = pParse->db;
85585   Vdbe *v;
85586   int iDb;         /* Database number to create the table in */
85587   Token *pName;    /* Unqualified name of the table to create */
85588 
85589   /* The table or view name to create is passed to this routine via tokens
85590   ** pName1 and pName2. If the table name was fully qualified, for example:
85591   **
85592   ** CREATE TABLE xxx.yyy (...);
85593   **
85594   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
85595   ** the table name is not fully qualified, i.e.:
85596   **
85597   ** CREATE TABLE yyy(...);
85598   **
85599   ** Then pName1 is set to "yyy" and pName2 is "".
85600   **
85601   ** The call below sets the pName pointer to point at the token (pName1 or
85602   ** pName2) that stores the unqualified table name. The variable iDb is
85603   ** set to the index of the database that the table or view is to be
85604   ** created in.
85605   */
85606   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
85607   if( iDb<0 ) return;
85608   if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
85609     /* If creating a temp table, the name may not be qualified. Unless
85610     ** the database name is "temp" anyway.  */
85611     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
85612     return;
85613   }
85614   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
85615 
85616   pParse->sNameToken = *pName;
85617   zName = sqlite3NameFromToken(db, pName);
85618   if( zName==0 ) return;
85619   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
85620     goto begin_table_error;
85621   }
85622   if( db->init.iDb==1 ) isTemp = 1;
85623 #ifndef SQLITE_OMIT_AUTHORIZATION
85624   assert( (isTemp & 1)==isTemp );
85625   {
85626     int code;
85627     char *zDb = db->aDb[iDb].zName;
85628     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
85629       goto begin_table_error;
85630     }
85631     if( isView ){
85632       if( !OMIT_TEMPDB && isTemp ){
85633         code = SQLITE_CREATE_TEMP_VIEW;
85634       }else{
85635         code = SQLITE_CREATE_VIEW;
85636       }
85637     }else{
85638       if( !OMIT_TEMPDB && isTemp ){
85639         code = SQLITE_CREATE_TEMP_TABLE;
85640       }else{
85641         code = SQLITE_CREATE_TABLE;
85642       }
85643     }
85644     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
85645       goto begin_table_error;
85646     }
85647   }
85648 #endif
85649 
85650   /* Make sure the new table name does not collide with an existing
85651   ** index or table name in the same database.  Issue an error message if
85652   ** it does. The exception is if the statement being parsed was passed
85653   ** to an sqlite3_declare_vtab() call. In that case only the column names
85654   ** and types will be used, so there is no need to test for namespace
85655   ** collisions.
85656   */
85657   if( !IN_DECLARE_VTAB ){
85658     char *zDb = db->aDb[iDb].zName;
85659     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
85660       goto begin_table_error;
85661     }
85662     pTable = sqlite3FindTable(db, zName, zDb);
85663     if( pTable ){
85664       if( !noErr ){
85665         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
85666       }else{
85667         assert( !db->init.busy );
85668         sqlite3CodeVerifySchema(pParse, iDb);
85669       }
85670       goto begin_table_error;
85671     }
85672     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
85673       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
85674       goto begin_table_error;
85675     }
85676   }
85677 
85678   pTable = sqlite3DbMallocZero(db, sizeof(Table));
85679   if( pTable==0 ){
85680     db->mallocFailed = 1;
85681     pParse->rc = SQLITE_NOMEM;
85682     pParse->nErr++;
85683     goto begin_table_error;
85684   }
85685   pTable->zName = zName;
85686   pTable->iPKey = -1;
85687   pTable->pSchema = db->aDb[iDb].pSchema;
85688   pTable->nRef = 1;
85689   pTable->nRowEst = 1048576;
85690   assert( pParse->pNewTable==0 );
85691   pParse->pNewTable = pTable;
85692 
85693   /* If this is the magic sqlite_sequence table used by autoincrement,
85694   ** then record a pointer to this table in the main database structure
85695   ** so that INSERT can find the table easily.
85696   */
85697 #ifndef SQLITE_OMIT_AUTOINCREMENT
85698   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
85699     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
85700     pTable->pSchema->pSeqTab = pTable;
85701   }
85702 #endif
85703 
85704   /* Begin generating the code that will insert the table record into
85705   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
85706   ** and allocate the record number for the table entry now.  Before any
85707   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
85708   ** indices to be created and the table record must come before the
85709   ** indices.  Hence, the record number for the table must be allocated
85710   ** now.
85711   */
85712   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
85713     int j1;
85714     int fileFormat;
85715     int reg1, reg2, reg3;
85716     sqlite3BeginWriteOperation(pParse, 0, iDb);
85717 
85718 #ifndef SQLITE_OMIT_VIRTUALTABLE
85719     if( isVirtual ){
85720       sqlite3VdbeAddOp0(v, OP_VBegin);
85721     }
85722 #endif
85723 
85724     /* If the file format and encoding in the database have not been set,
85725     ** set them now.
85726     */
85727     reg1 = pParse->regRowid = ++pParse->nMem;
85728     reg2 = pParse->regRoot = ++pParse->nMem;
85729     reg3 = ++pParse->nMem;
85730     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
85731     sqlite3VdbeUsesBtree(v, iDb);
85732     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
85733     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
85734                   1 : SQLITE_MAX_FILE_FORMAT;
85735     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
85736     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
85737     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
85738     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
85739     sqlite3VdbeJumpHere(v, j1);
85740 
85741     /* This just creates a place-holder record in the sqlite_master table.
85742     ** The record created does not contain anything yet.  It will be replaced
85743     ** by the real entry in code generated at sqlite3EndTable().
85744     **
85745     ** The rowid for the new entry is left in register pParse->regRowid.
85746     ** The root page number of the new table is left in reg pParse->regRoot.
85747     ** The rowid and root page number values are needed by the code that
85748     ** sqlite3EndTable will generate.
85749     */
85750 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
85751     if( isView || isVirtual ){
85752       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
85753     }else
85754 #endif
85755     {
85756       pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
85757     }
85758     sqlite3OpenMasterTable(pParse, iDb);
85759     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
85760     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
85761     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
85762     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
85763     sqlite3VdbeAddOp0(v, OP_Close);
85764   }
85765 
85766   /* Normal (non-error) return. */
85767   return;
85768 
85769   /* If an error occurs, we jump here */
85770 begin_table_error:
85771   sqlite3DbFree(db, zName);
85772   return;
85773 }
85774 
85775 /*
85776 ** This macro is used to compare two strings in a case-insensitive manner.
85777 ** It is slightly faster than calling sqlite3StrICmp() directly, but
85778 ** produces larger code.
85779 **
85780 ** WARNING: This macro is not compatible with the strcmp() family. It
85781 ** returns true if the two strings are equal, otherwise false.
85782 */
85783 #define STRICMP(x, y) (\
85784 sqlite3UpperToLower[*(unsigned char *)(x)]==   \
85785 sqlite3UpperToLower[*(unsigned char *)(y)]     \
85786 && sqlite3StrICmp((x)+1,(y)+1)==0 )
85787 
85788 /*
85789 ** Add a new column to the table currently being constructed.
85790 **
85791 ** The parser calls this routine once for each column declaration
85792 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
85793 ** first to get things going.  Then this routine is called for each
85794 ** column.
85795 */
85796 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
85797   Table *p;
85798   int i;
85799   char *z;
85800   Column *pCol;
85801   sqlite3 *db = pParse->db;
85802   if( (p = pParse->pNewTable)==0 ) return;
85803 #if SQLITE_MAX_COLUMN
85804   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
85805     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
85806     return;
85807   }
85808 #endif
85809   z = sqlite3NameFromToken(db, pName);
85810   if( z==0 ) return;
85811   for(i=0; i<p->nCol; i++){
85812     if( STRICMP(z, p->aCol[i].zName) ){
85813       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
85814       sqlite3DbFree(db, z);
85815       return;
85816     }
85817   }
85818   if( (p->nCol & 0x7)==0 ){
85819     Column *aNew;
85820     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
85821     if( aNew==0 ){
85822       sqlite3DbFree(db, z);
85823       return;
85824     }
85825     p->aCol = aNew;
85826   }
85827   pCol = &p->aCol[p->nCol];
85828   memset(pCol, 0, sizeof(p->aCol[0]));
85829   pCol->zName = z;
85830 
85831   /* If there is no type specified, columns have the default affinity
85832   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
85833   ** be called next to set pCol->affinity correctly.
85834   */
85835   pCol->affinity = SQLITE_AFF_NONE;
85836   pCol->szEst = 1;
85837   p->nCol++;
85838 }
85839 
85840 /*
85841 ** This routine is called by the parser while in the middle of
85842 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
85843 ** been seen on a column.  This routine sets the notNull flag on
85844 ** the column currently under construction.
85845 */
85846 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
85847   Table *p;
85848   p = pParse->pNewTable;
85849   if( p==0 || NEVER(p->nCol<1) ) return;
85850   p->aCol[p->nCol-1].notNull = (u8)onError;
85851 }
85852 
85853 /*
85854 ** Scan the column type name zType (length nType) and return the
85855 ** associated affinity type.
85856 **
85857 ** This routine does a case-independent search of zType for the
85858 ** substrings in the following table. If one of the substrings is
85859 ** found, the corresponding affinity is returned. If zType contains
85860 ** more than one of the substrings, entries toward the top of
85861 ** the table take priority. For example, if zType is 'BLOBINT',
85862 ** SQLITE_AFF_INTEGER is returned.
85863 **
85864 ** Substring     | Affinity
85865 ** --------------------------------
85866 ** 'INT'         | SQLITE_AFF_INTEGER
85867 ** 'CHAR'        | SQLITE_AFF_TEXT
85868 ** 'CLOB'        | SQLITE_AFF_TEXT
85869 ** 'TEXT'        | SQLITE_AFF_TEXT
85870 ** 'BLOB'        | SQLITE_AFF_NONE
85871 ** 'REAL'        | SQLITE_AFF_REAL
85872 ** 'FLOA'        | SQLITE_AFF_REAL
85873 ** 'DOUB'        | SQLITE_AFF_REAL
85874 **
85875 ** If none of the substrings in the above table are found,
85876 ** SQLITE_AFF_NUMERIC is returned.
85877 */
85878 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
85879   u32 h = 0;
85880   char aff = SQLITE_AFF_NUMERIC;
85881   const char *zChar = 0;
85882 
85883   if( zIn==0 ) return aff;
85884   while( zIn[0] ){
85885     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
85886     zIn++;
85887     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
85888       aff = SQLITE_AFF_TEXT;
85889       zChar = zIn;
85890     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
85891       aff = SQLITE_AFF_TEXT;
85892     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
85893       aff = SQLITE_AFF_TEXT;
85894     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
85895         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
85896       aff = SQLITE_AFF_NONE;
85897       if( zIn[0]=='(' ) zChar = zIn;
85898 #ifndef SQLITE_OMIT_FLOATING_POINT
85899     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
85900         && aff==SQLITE_AFF_NUMERIC ){
85901       aff = SQLITE_AFF_REAL;
85902     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
85903         && aff==SQLITE_AFF_NUMERIC ){
85904       aff = SQLITE_AFF_REAL;
85905     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
85906         && aff==SQLITE_AFF_NUMERIC ){
85907       aff = SQLITE_AFF_REAL;
85908 #endif
85909     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
85910       aff = SQLITE_AFF_INTEGER;
85911       break;
85912     }
85913   }
85914 
85915   /* If pszEst is not NULL, store an estimate of the field size.  The
85916   ** estimate is scaled so that the size of an integer is 1.  */
85917   if( pszEst ){
85918     *pszEst = 1;   /* default size is approx 4 bytes */
85919     if( aff<=SQLITE_AFF_NONE ){
85920       if( zChar ){
85921         while( zChar[0] ){
85922           if( sqlite3Isdigit(zChar[0]) ){
85923             int v = 0;
85924             sqlite3GetInt32(zChar, &v);
85925             v = v/4 + 1;
85926             if( v>255 ) v = 255;
85927             *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
85928             break;
85929           }
85930           zChar++;
85931         }
85932       }else{
85933         *pszEst = 5;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
85934       }
85935     }
85936   }
85937   return aff;
85938 }
85939 
85940 /*
85941 ** This routine is called by the parser while in the middle of
85942 ** parsing a CREATE TABLE statement.  The pFirst token is the first
85943 ** token in the sequence of tokens that describe the type of the
85944 ** column currently under construction.   pLast is the last token
85945 ** in the sequence.  Use this information to construct a string
85946 ** that contains the typename of the column and store that string
85947 ** in zType.
85948 */
85949 SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
85950   Table *p;
85951   Column *pCol;
85952 
85953   p = pParse->pNewTable;
85954   if( p==0 || NEVER(p->nCol<1) ) return;
85955   pCol = &p->aCol[p->nCol-1];
85956   assert( pCol->zType==0 );
85957   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
85958   pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
85959 }
85960 
85961 /*
85962 ** The expression is the default value for the most recently added column
85963 ** of the table currently under construction.
85964 **
85965 ** Default value expressions must be constant.  Raise an exception if this
85966 ** is not the case.
85967 **
85968 ** This routine is called by the parser while in the middle of
85969 ** parsing a CREATE TABLE statement.
85970 */
85971 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
85972   Table *p;
85973   Column *pCol;
85974   sqlite3 *db = pParse->db;
85975   p = pParse->pNewTable;
85976   if( p!=0 ){
85977     pCol = &(p->aCol[p->nCol-1]);
85978     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
85979       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
85980           pCol->zName);
85981     }else{
85982       /* A copy of pExpr is used instead of the original, as pExpr contains
85983       ** tokens that point to volatile memory. The 'span' of the expression
85984       ** is required by pragma table_info.
85985       */
85986       sqlite3ExprDelete(db, pCol->pDflt);
85987       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
85988       sqlite3DbFree(db, pCol->zDflt);
85989       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
85990                                      (int)(pSpan->zEnd - pSpan->zStart));
85991     }
85992   }
85993   sqlite3ExprDelete(db, pSpan->pExpr);
85994 }
85995 
85996 /*
85997 ** Designate the PRIMARY KEY for the table.  pList is a list of names
85998 ** of columns that form the primary key.  If pList is NULL, then the
85999 ** most recently added column of the table is the primary key.
86000 **
86001 ** A table can have at most one primary key.  If the table already has
86002 ** a primary key (and this is the second primary key) then create an
86003 ** error.
86004 **
86005 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
86006 ** then we will try to use that column as the rowid.  Set the Table.iPKey
86007 ** field of the table under construction to be the index of the
86008 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
86009 ** no INTEGER PRIMARY KEY.
86010 **
86011 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
86012 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
86013 */
86014 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
86015   Parse *pParse,    /* Parsing context */
86016   ExprList *pList,  /* List of field names to be indexed */
86017   int onError,      /* What to do with a uniqueness conflict */
86018   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
86019   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
86020 ){
86021   Table *pTab = pParse->pNewTable;
86022   char *zType = 0;
86023   int iCol = -1, i;
86024   int nTerm;
86025   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
86026   if( pTab->tabFlags & TF_HasPrimaryKey ){
86027     sqlite3ErrorMsg(pParse,
86028       "table \"%s\" has more than one primary key", pTab->zName);
86029     goto primary_key_exit;
86030   }
86031   pTab->tabFlags |= TF_HasPrimaryKey;
86032   if( pList==0 ){
86033     iCol = pTab->nCol - 1;
86034     pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
86035     zType = pTab->aCol[iCol].zType;
86036     nTerm = 1;
86037   }else{
86038     nTerm = pList->nExpr;
86039     for(i=0; i<nTerm; i++){
86040       for(iCol=0; iCol<pTab->nCol; iCol++){
86041         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
86042           pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
86043           zType = pTab->aCol[iCol].zType;
86044           break;
86045         }
86046       }
86047     }
86048   }
86049   if( nTerm==1
86050    && zType && sqlite3StrICmp(zType, "INTEGER")==0
86051    && sortOrder==SQLITE_SO_ASC
86052   ){
86053     pTab->iPKey = iCol;
86054     pTab->keyConf = (u8)onError;
86055     assert( autoInc==0 || autoInc==1 );
86056     pTab->tabFlags |= autoInc*TF_Autoincrement;
86057     if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
86058   }else if( autoInc ){
86059 #ifndef SQLITE_OMIT_AUTOINCREMENT
86060     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
86061        "INTEGER PRIMARY KEY");
86062 #endif
86063   }else{
86064     Vdbe *v = pParse->pVdbe;
86065     Index *p;
86066     if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
86067     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
86068                            0, sortOrder, 0);
86069     if( p ){
86070       p->autoIndex = 2;
86071       if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
86072     }
86073     pList = 0;
86074   }
86075 
86076 primary_key_exit:
86077   sqlite3ExprListDelete(pParse->db, pList);
86078   return;
86079 }
86080 
86081 /*
86082 ** Add a new CHECK constraint to the table currently under construction.
86083 */
86084 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
86085   Parse *pParse,    /* Parsing context */
86086   Expr *pCheckExpr  /* The check expression */
86087 ){
86088 #ifndef SQLITE_OMIT_CHECK
86089   Table *pTab = pParse->pNewTable;
86090   if( pTab && !IN_DECLARE_VTAB ){
86091     pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
86092     if( pParse->constraintName.n ){
86093       sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
86094     }
86095   }else
86096 #endif
86097   {
86098     sqlite3ExprDelete(pParse->db, pCheckExpr);
86099   }
86100 }
86101 
86102 /*
86103 ** Set the collation function of the most recently parsed table column
86104 ** to the CollSeq given.
86105 */
86106 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
86107   Table *p;
86108   int i;
86109   char *zColl;              /* Dequoted name of collation sequence */
86110   sqlite3 *db;
86111 
86112   if( (p = pParse->pNewTable)==0 ) return;
86113   i = p->nCol-1;
86114   db = pParse->db;
86115   zColl = sqlite3NameFromToken(db, pToken);
86116   if( !zColl ) return;
86117 
86118   if( sqlite3LocateCollSeq(pParse, zColl) ){
86119     Index *pIdx;
86120     sqlite3DbFree(db, p->aCol[i].zColl);
86121     p->aCol[i].zColl = zColl;
86122 
86123     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
86124     ** then an index may have been created on this column before the
86125     ** collation type was added. Correct this if it is the case.
86126     */
86127     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
86128       assert( pIdx->nKeyCol==1 );
86129       if( pIdx->aiColumn[0]==i ){
86130         pIdx->azColl[0] = p->aCol[i].zColl;
86131       }
86132     }
86133   }else{
86134     sqlite3DbFree(db, zColl);
86135   }
86136 }
86137 
86138 /*
86139 ** This function returns the collation sequence for database native text
86140 ** encoding identified by the string zName, length nName.
86141 **
86142 ** If the requested collation sequence is not available, or not available
86143 ** in the database native encoding, the collation factory is invoked to
86144 ** request it. If the collation factory does not supply such a sequence,
86145 ** and the sequence is available in another text encoding, then that is
86146 ** returned instead.
86147 **
86148 ** If no versions of the requested collations sequence are available, or
86149 ** another error occurs, NULL is returned and an error message written into
86150 ** pParse.
86151 **
86152 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
86153 ** invokes the collation factory if the named collation cannot be found
86154 ** and generates an error message.
86155 **
86156 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
86157 */
86158 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
86159   sqlite3 *db = pParse->db;
86160   u8 enc = ENC(db);
86161   u8 initbusy = db->init.busy;
86162   CollSeq *pColl;
86163 
86164   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
86165   if( !initbusy && (!pColl || !pColl->xCmp) ){
86166     pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
86167   }
86168 
86169   return pColl;
86170 }
86171 
86172 
86173 /*
86174 ** Generate code that will increment the schema cookie.
86175 **
86176 ** The schema cookie is used to determine when the schema for the
86177 ** database changes.  After each schema change, the cookie value
86178 ** changes.  When a process first reads the schema it records the
86179 ** cookie.  Thereafter, whenever it goes to access the database,
86180 ** it checks the cookie to make sure the schema has not changed
86181 ** since it was last read.
86182 **
86183 ** This plan is not completely bullet-proof.  It is possible for
86184 ** the schema to change multiple times and for the cookie to be
86185 ** set back to prior value.  But schema changes are infrequent
86186 ** and the probability of hitting the same cookie value is only
86187 ** 1 chance in 2^32.  So we're safe enough.
86188 */
86189 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
86190   int r1 = sqlite3GetTempReg(pParse);
86191   sqlite3 *db = pParse->db;
86192   Vdbe *v = pParse->pVdbe;
86193   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86194   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
86195   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
86196   sqlite3ReleaseTempReg(pParse, r1);
86197 }
86198 
86199 /*
86200 ** Measure the number of characters needed to output the given
86201 ** identifier.  The number returned includes any quotes used
86202 ** but does not include the null terminator.
86203 **
86204 ** The estimate is conservative.  It might be larger that what is
86205 ** really needed.
86206 */
86207 static int identLength(const char *z){
86208   int n;
86209   for(n=0; *z; n++, z++){
86210     if( *z=='"' ){ n++; }
86211   }
86212   return n + 2;
86213 }
86214 
86215 /*
86216 ** The first parameter is a pointer to an output buffer. The second
86217 ** parameter is a pointer to an integer that contains the offset at
86218 ** which to write into the output buffer. This function copies the
86219 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
86220 ** to the specified offset in the buffer and updates *pIdx to refer
86221 ** to the first byte after the last byte written before returning.
86222 **
86223 ** If the string zSignedIdent consists entirely of alpha-numeric
86224 ** characters, does not begin with a digit and is not an SQL keyword,
86225 ** then it is copied to the output buffer exactly as it is. Otherwise,
86226 ** it is quoted using double-quotes.
86227 */
86228 static void identPut(char *z, int *pIdx, char *zSignedIdent){
86229   unsigned char *zIdent = (unsigned char*)zSignedIdent;
86230   int i, j, needQuote;
86231   i = *pIdx;
86232 
86233   for(j=0; zIdent[j]; j++){
86234     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
86235   }
86236   needQuote = sqlite3Isdigit(zIdent[0])
86237             || sqlite3KeywordCode(zIdent, j)!=TK_ID
86238             || zIdent[j]!=0
86239             || j==0;
86240 
86241   if( needQuote ) z[i++] = '"';
86242   for(j=0; zIdent[j]; j++){
86243     z[i++] = zIdent[j];
86244     if( zIdent[j]=='"' ) z[i++] = '"';
86245   }
86246   if( needQuote ) z[i++] = '"';
86247   z[i] = 0;
86248   *pIdx = i;
86249 }
86250 
86251 /*
86252 ** Generate a CREATE TABLE statement appropriate for the given
86253 ** table.  Memory to hold the text of the statement is obtained
86254 ** from sqliteMalloc() and must be freed by the calling function.
86255 */
86256 static char *createTableStmt(sqlite3 *db, Table *p){
86257   int i, k, n;
86258   char *zStmt;
86259   char *zSep, *zSep2, *zEnd;
86260   Column *pCol;
86261   n = 0;
86262   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
86263     n += identLength(pCol->zName) + 5;
86264   }
86265   n += identLength(p->zName);
86266   if( n<50 ){
86267     zSep = "";
86268     zSep2 = ",";
86269     zEnd = ")";
86270   }else{
86271     zSep = "\n  ";
86272     zSep2 = ",\n  ";
86273     zEnd = "\n)";
86274   }
86275   n += 35 + 6*p->nCol;
86276   zStmt = sqlite3DbMallocRaw(0, n);
86277   if( zStmt==0 ){
86278     db->mallocFailed = 1;
86279     return 0;
86280   }
86281   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
86282   k = sqlite3Strlen30(zStmt);
86283   identPut(zStmt, &k, p->zName);
86284   zStmt[k++] = '(';
86285   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
86286     static const char * const azType[] = {
86287         /* SQLITE_AFF_TEXT    */ " TEXT",
86288         /* SQLITE_AFF_NONE    */ "",
86289         /* SQLITE_AFF_NUMERIC */ " NUM",
86290         /* SQLITE_AFF_INTEGER */ " INT",
86291         /* SQLITE_AFF_REAL    */ " REAL"
86292     };
86293     int len;
86294     const char *zType;
86295 
86296     sqlite3_snprintf(n-k, &zStmt[k], zSep);
86297     k += sqlite3Strlen30(&zStmt[k]);
86298     zSep = zSep2;
86299     identPut(zStmt, &k, pCol->zName);
86300     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
86301     assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
86302     testcase( pCol->affinity==SQLITE_AFF_TEXT );
86303     testcase( pCol->affinity==SQLITE_AFF_NONE );
86304     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
86305     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
86306     testcase( pCol->affinity==SQLITE_AFF_REAL );
86307 
86308     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
86309     len = sqlite3Strlen30(zType);
86310     assert( pCol->affinity==SQLITE_AFF_NONE
86311             || pCol->affinity==sqlite3AffinityType(zType, 0) );
86312     memcpy(&zStmt[k], zType, len);
86313     k += len;
86314     assert( k<=n );
86315   }
86316   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
86317   return zStmt;
86318 }
86319 
86320 /*
86321 ** Resize an Index object to hold N columns total.  Return SQLITE_OK
86322 ** on success and SQLITE_NOMEM on an OOM error.
86323 */
86324 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
86325   char *zExtra;
86326   int nByte;
86327   if( pIdx->nColumn>=N ) return SQLITE_OK;
86328   assert( pIdx->isResized==0 );
86329   nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
86330   zExtra = sqlite3DbMallocZero(db, nByte);
86331   if( zExtra==0 ) return SQLITE_NOMEM;
86332   memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
86333   pIdx->azColl = (char**)zExtra;
86334   zExtra += sizeof(char*)*N;
86335   memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
86336   pIdx->aiColumn = (i16*)zExtra;
86337   zExtra += sizeof(i16)*N;
86338   memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
86339   pIdx->aSortOrder = (u8*)zExtra;
86340   pIdx->nColumn = N;
86341   pIdx->isResized = 1;
86342   return SQLITE_OK;
86343 }
86344 
86345 /*
86346 ** Estimate the total row width for a table.
86347 */
86348 static void estimateTableWidth(Table *pTab){
86349   unsigned wTable = 0;
86350   const Column *pTabCol;
86351   int i;
86352   for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
86353     wTable += pTabCol->szEst;
86354   }
86355   if( pTab->iPKey<0 ) wTable++;
86356   pTab->szTabRow = sqlite3LogEst(wTable*4);
86357 }
86358 
86359 /*
86360 ** Estimate the average size of a row for an index.
86361 */
86362 static void estimateIndexWidth(Index *pIdx){
86363   unsigned wIndex = 0;
86364   int i;
86365   const Column *aCol = pIdx->pTable->aCol;
86366   for(i=0; i<pIdx->nColumn; i++){
86367     i16 x = pIdx->aiColumn[i];
86368     assert( x<pIdx->pTable->nCol );
86369     wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
86370   }
86371   pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
86372 }
86373 
86374 /* Return true if value x is found any of the first nCol entries of aiCol[]
86375 */
86376 static int hasColumn(const i16 *aiCol, int nCol, int x){
86377   while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
86378   return 0;
86379 }
86380 
86381 /*
86382 ** This routine runs at the end of parsing a CREATE TABLE statement that
86383 ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
86384 ** internal schema data structures and the generated VDBE code so that they
86385 ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
86386 ** Changes include:
86387 **
86388 **     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
86389 **          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
86390 **          data storage is a covering index btree.
86391 **     (2)  Bypass the creation of the sqlite_master table entry
86392 **          for the PRIMARY KEY as the the primary key index is now
86393 **          identified by the sqlite_master table entry of the table itself.
86394 **     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
86395 **          schema to the rootpage from the main table.
86396 **     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
86397 **     (5)  Add all table columns to the PRIMARY KEY Index object
86398 **          so that the PRIMARY KEY is a covering index.  The surplus
86399 **          columns are part of KeyInfo.nXField and are not used for
86400 **          sorting or lookup or uniqueness checks.
86401 **     (6)  Replace the rowid tail on all automatically generated UNIQUE
86402 **          indices with the PRIMARY KEY columns.
86403 */
86404 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
86405   Index *pIdx;
86406   Index *pPk;
86407   int nPk;
86408   int i, j;
86409   sqlite3 *db = pParse->db;
86410   Vdbe *v = pParse->pVdbe;
86411 
86412   /* Convert the OP_CreateTable opcode that would normally create the
86413   ** root-page for the table into a OP_CreateIndex opcode.  The index
86414   ** created will become the PRIMARY KEY index.
86415   */
86416   if( pParse->addrCrTab ){
86417     assert( v );
86418     sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
86419   }
86420 
86421   /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
86422   ** table entry.
86423   */
86424   if( pParse->addrSkipPK ){
86425     assert( v );
86426     sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto;
86427   }
86428 
86429   /* Locate the PRIMARY KEY index.  Or, if this table was originally
86430   ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
86431   */
86432   if( pTab->iPKey>=0 ){
86433     ExprList *pList;
86434     pList = sqlite3ExprListAppend(pParse, 0, 0);
86435     if( pList==0 ) return;
86436     pList->a[0].zName = sqlite3DbStrDup(pParse->db,
86437                                         pTab->aCol[pTab->iPKey].zName);
86438     pList->a[0].sortOrder = pParse->iPkSortOrder;
86439     assert( pParse->pNewTable==pTab );
86440     pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
86441     if( pPk==0 ) return;
86442     pPk->autoIndex = 2;
86443     pTab->iPKey = -1;
86444   }else{
86445     pPk = sqlite3PrimaryKeyIndex(pTab);
86446   }
86447   pPk->isCovering = 1;
86448   assert( pPk!=0 );
86449   nPk = pPk->nKeyCol;
86450 
86451   /* Make sure every column of the PRIMARY KEY is NOT NULL */
86452   for(i=0; i<nPk; i++){
86453     pTab->aCol[pPk->aiColumn[i]].notNull = 1;
86454   }
86455   pPk->uniqNotNull = 1;
86456 
86457   /* The root page of the PRIMARY KEY is the table root page */
86458   pPk->tnum = pTab->tnum;
86459 
86460   /* Update the in-memory representation of all UNIQUE indices by converting
86461   ** the final rowid column into one or more columns of the PRIMARY KEY.
86462   */
86463   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
86464     int n;
86465     if( pIdx->autoIndex==2 ) continue;
86466     for(i=n=0; i<nPk; i++){
86467       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
86468     }
86469     if( n==0 ){
86470       /* This index is a superset of the primary key */
86471       pIdx->nColumn = pIdx->nKeyCol;
86472       continue;
86473     }
86474     if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
86475     for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
86476       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
86477         pIdx->aiColumn[j] = pPk->aiColumn[i];
86478         pIdx->azColl[j] = pPk->azColl[i];
86479         j++;
86480       }
86481     }
86482     assert( pIdx->nColumn>=pIdx->nKeyCol+n );
86483     assert( pIdx->nColumn>=j );
86484   }
86485 
86486   /* Add all table columns to the PRIMARY KEY index
86487   */
86488   if( nPk<pTab->nCol ){
86489     if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
86490     for(i=0, j=nPk; i<pTab->nCol; i++){
86491       if( !hasColumn(pPk->aiColumn, j, i) ){
86492         assert( j<pPk->nColumn );
86493         pPk->aiColumn[j] = i;
86494         pPk->azColl[j] = "BINARY";
86495         j++;
86496       }
86497     }
86498     assert( pPk->nColumn==j );
86499     assert( pTab->nCol==j );
86500   }else{
86501     pPk->nColumn = pTab->nCol;
86502   }
86503 }
86504 
86505 /*
86506 ** This routine is called to report the final ")" that terminates
86507 ** a CREATE TABLE statement.
86508 **
86509 ** The table structure that other action routines have been building
86510 ** is added to the internal hash tables, assuming no errors have
86511 ** occurred.
86512 **
86513 ** An entry for the table is made in the master table on disk, unless
86514 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
86515 ** it means we are reading the sqlite_master table because we just
86516 ** connected to the database or because the sqlite_master table has
86517 ** recently changed, so the entry for this table already exists in
86518 ** the sqlite_master table.  We do not want to create it again.
86519 **
86520 ** If the pSelect argument is not NULL, it means that this routine
86521 ** was called to create a table generated from a
86522 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
86523 ** the new table will match the result set of the SELECT.
86524 */
86525 SQLITE_PRIVATE void sqlite3EndTable(
86526   Parse *pParse,          /* Parse context */
86527   Token *pCons,           /* The ',' token after the last column defn. */
86528   Token *pEnd,            /* The ')' before options in the CREATE TABLE */
86529   u8 tabOpts,             /* Extra table options. Usually 0. */
86530   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
86531 ){
86532   Table *p;                 /* The new table */
86533   sqlite3 *db = pParse->db; /* The database connection */
86534   int iDb;                  /* Database in which the table lives */
86535   Index *pIdx;              /* An implied index of the table */
86536 
86537   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
86538     return;
86539   }
86540   p = pParse->pNewTable;
86541   if( p==0 ) return;
86542 
86543   assert( !db->init.busy || !pSelect );
86544 
86545   /* If the db->init.busy is 1 it means we are reading the SQL off the
86546   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
86547   ** So do not write to the disk again.  Extract the root page number
86548   ** for the table from the db->init.newTnum field.  (The page number
86549   ** should have been put there by the sqliteOpenCb routine.)
86550   */
86551   if( db->init.busy ){
86552     p->tnum = db->init.newTnum;
86553   }
86554 
86555   /* Special processing for WITHOUT ROWID Tables */
86556   if( tabOpts & TF_WithoutRowid ){
86557     if( (p->tabFlags & TF_Autoincrement) ){
86558       sqlite3ErrorMsg(pParse,
86559           "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
86560       return;
86561     }
86562     if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
86563       sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
86564     }else{
86565       p->tabFlags |= TF_WithoutRowid;
86566       convertToWithoutRowidTable(pParse, p);
86567     }
86568   }
86569 
86570   iDb = sqlite3SchemaToIndex(db, p->pSchema);
86571 
86572 #ifndef SQLITE_OMIT_CHECK
86573   /* Resolve names in all CHECK constraint expressions.
86574   */
86575   if( p->pCheck ){
86576     sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
86577   }
86578 #endif /* !defined(SQLITE_OMIT_CHECK) */
86579 
86580   /* Estimate the average row size for the table and for all implied indices */
86581   estimateTableWidth(p);
86582   for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
86583     estimateIndexWidth(pIdx);
86584   }
86585 
86586   /* If not initializing, then create a record for the new table
86587   ** in the SQLITE_MASTER table of the database.
86588   **
86589   ** If this is a TEMPORARY table, write the entry into the auxiliary
86590   ** file instead of into the main database file.
86591   */
86592   if( !db->init.busy ){
86593     int n;
86594     Vdbe *v;
86595     char *zType;    /* "view" or "table" */
86596     char *zType2;   /* "VIEW" or "TABLE" */
86597     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
86598 
86599     v = sqlite3GetVdbe(pParse);
86600     if( NEVER(v==0) ) return;
86601 
86602     sqlite3VdbeAddOp1(v, OP_Close, 0);
86603 
86604     /*
86605     ** Initialize zType for the new view or table.
86606     */
86607     if( p->pSelect==0 ){
86608       /* A regular table */
86609       zType = "table";
86610       zType2 = "TABLE";
86611 #ifndef SQLITE_OMIT_VIEW
86612     }else{
86613       /* A view */
86614       zType = "view";
86615       zType2 = "VIEW";
86616 #endif
86617     }
86618 
86619     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
86620     ** statement to populate the new table. The root-page number for the
86621     ** new table is in register pParse->regRoot.
86622     **
86623     ** Once the SELECT has been coded by sqlite3Select(), it is in a
86624     ** suitable state to query for the column names and types to be used
86625     ** by the new table.
86626     **
86627     ** A shared-cache write-lock is not required to write to the new table,
86628     ** as a schema-lock must have already been obtained to create it. Since
86629     ** a schema-lock excludes all other database users, the write-lock would
86630     ** be redundant.
86631     */
86632     if( pSelect ){
86633       SelectDest dest;
86634       Table *pSelTab;
86635 
86636       assert(pParse->nTab==1);
86637       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
86638       sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
86639       pParse->nTab = 2;
86640       sqlite3SelectDestInit(&dest, SRT_Table, 1);
86641       sqlite3Select(pParse, pSelect, &dest);
86642       sqlite3VdbeAddOp1(v, OP_Close, 1);
86643       if( pParse->nErr==0 ){
86644         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
86645         if( pSelTab==0 ) return;
86646         assert( p->aCol==0 );
86647         p->nCol = pSelTab->nCol;
86648         p->aCol = pSelTab->aCol;
86649         pSelTab->nCol = 0;
86650         pSelTab->aCol = 0;
86651         sqlite3DeleteTable(db, pSelTab);
86652       }
86653     }
86654 
86655     /* Compute the complete text of the CREATE statement */
86656     if( pSelect ){
86657       zStmt = createTableStmt(db, p);
86658     }else{
86659       Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
86660       n = (int)(pEnd2->z - pParse->sNameToken.z);
86661       if( pEnd2->z[0]!=';' ) n += pEnd2->n;
86662       zStmt = sqlite3MPrintf(db,
86663           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
86664       );
86665     }
86666 
86667     /* A slot for the record has already been allocated in the
86668     ** SQLITE_MASTER table.  We just need to update that slot with all
86669     ** the information we've collected.
86670     */
86671     sqlite3NestedParse(pParse,
86672       "UPDATE %Q.%s "
86673          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
86674        "WHERE rowid=#%d",
86675       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
86676       zType,
86677       p->zName,
86678       p->zName,
86679       pParse->regRoot,
86680       zStmt,
86681       pParse->regRowid
86682     );
86683     sqlite3DbFree(db, zStmt);
86684     sqlite3ChangeCookie(pParse, iDb);
86685 
86686 #ifndef SQLITE_OMIT_AUTOINCREMENT
86687     /* Check to see if we need to create an sqlite_sequence table for
86688     ** keeping track of autoincrement keys.
86689     */
86690     if( p->tabFlags & TF_Autoincrement ){
86691       Db *pDb = &db->aDb[iDb];
86692       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86693       if( pDb->pSchema->pSeqTab==0 ){
86694         sqlite3NestedParse(pParse,
86695           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
86696           pDb->zName
86697         );
86698       }
86699     }
86700 #endif
86701 
86702     /* Reparse everything to update our internal data structures */
86703     sqlite3VdbeAddParseSchemaOp(v, iDb,
86704            sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
86705   }
86706 
86707 
86708   /* Add the table to the in-memory representation of the database.
86709   */
86710   if( db->init.busy ){
86711     Table *pOld;
86712     Schema *pSchema = p->pSchema;
86713     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86714     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
86715                              sqlite3Strlen30(p->zName),p);
86716     if( pOld ){
86717       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
86718       db->mallocFailed = 1;
86719       return;
86720     }
86721     pParse->pNewTable = 0;
86722     db->flags |= SQLITE_InternChanges;
86723 
86724 #ifndef SQLITE_OMIT_ALTERTABLE
86725     if( !p->pSelect ){
86726       const char *zName = (const char *)pParse->sNameToken.z;
86727       int nName;
86728       assert( !pSelect && pCons && pEnd );
86729       if( pCons->z==0 ){
86730         pCons = pEnd;
86731       }
86732       nName = (int)((const char *)pCons->z - zName);
86733       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
86734     }
86735 #endif
86736   }
86737 }
86738 
86739 #ifndef SQLITE_OMIT_VIEW
86740 /*
86741 ** The parser calls this routine in order to create a new VIEW
86742 */
86743 SQLITE_PRIVATE void sqlite3CreateView(
86744   Parse *pParse,     /* The parsing context */
86745   Token *pBegin,     /* The CREATE token that begins the statement */
86746   Token *pName1,     /* The token that holds the name of the view */
86747   Token *pName2,     /* The token that holds the name of the view */
86748   Select *pSelect,   /* A SELECT statement that will become the new view */
86749   int isTemp,        /* TRUE for a TEMPORARY view */
86750   int noErr          /* Suppress error messages if VIEW already exists */
86751 ){
86752   Table *p;
86753   int n;
86754   const char *z;
86755   Token sEnd;
86756   DbFixer sFix;
86757   Token *pName = 0;
86758   int iDb;
86759   sqlite3 *db = pParse->db;
86760 
86761   if( pParse->nVar>0 ){
86762     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
86763     sqlite3SelectDelete(db, pSelect);
86764     return;
86765   }
86766   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
86767   p = pParse->pNewTable;
86768   if( p==0 || pParse->nErr ){
86769     sqlite3SelectDelete(db, pSelect);
86770     return;
86771   }
86772   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
86773   iDb = sqlite3SchemaToIndex(db, p->pSchema);
86774   sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
86775   if( sqlite3FixSelect(&sFix, pSelect) ){
86776     sqlite3SelectDelete(db, pSelect);
86777     return;
86778   }
86779 
86780   /* Make a copy of the entire SELECT statement that defines the view.
86781   ** This will force all the Expr.token.z values to be dynamically
86782   ** allocated rather than point to the input string - which means that
86783   ** they will persist after the current sqlite3_exec() call returns.
86784   */
86785   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
86786   sqlite3SelectDelete(db, pSelect);
86787   if( db->mallocFailed ){
86788     return;
86789   }
86790   if( !db->init.busy ){
86791     sqlite3ViewGetColumnNames(pParse, p);
86792   }
86793 
86794   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
86795   ** the end.
86796   */
86797   sEnd = pParse->sLastToken;
86798   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
86799     sEnd.z += sEnd.n;
86800   }
86801   sEnd.n = 0;
86802   n = (int)(sEnd.z - pBegin->z);
86803   z = pBegin->z;
86804   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
86805   sEnd.z = &z[n-1];
86806   sEnd.n = 1;
86807 
86808   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
86809   sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
86810   return;
86811 }
86812 #endif /* SQLITE_OMIT_VIEW */
86813 
86814 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
86815 /*
86816 ** The Table structure pTable is really a VIEW.  Fill in the names of
86817 ** the columns of the view in the pTable structure.  Return the number
86818 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
86819 */
86820 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
86821   Table *pSelTab;   /* A fake table from which we get the result set */
86822   Select *pSel;     /* Copy of the SELECT that implements the view */
86823   int nErr = 0;     /* Number of errors encountered */
86824   int n;            /* Temporarily holds the number of cursors assigned */
86825   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
86826   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
86827 
86828   assert( pTable );
86829 
86830 #ifndef SQLITE_OMIT_VIRTUALTABLE
86831   if( sqlite3VtabCallConnect(pParse, pTable) ){
86832     return SQLITE_ERROR;
86833   }
86834   if( IsVirtual(pTable) ) return 0;
86835 #endif
86836 
86837 #ifndef SQLITE_OMIT_VIEW
86838   /* A positive nCol means the columns names for this view are
86839   ** already known.
86840   */
86841   if( pTable->nCol>0 ) return 0;
86842 
86843   /* A negative nCol is a special marker meaning that we are currently
86844   ** trying to compute the column names.  If we enter this routine with
86845   ** a negative nCol, it means two or more views form a loop, like this:
86846   **
86847   **     CREATE VIEW one AS SELECT * FROM two;
86848   **     CREATE VIEW two AS SELECT * FROM one;
86849   **
86850   ** Actually, the error above is now caught prior to reaching this point.
86851   ** But the following test is still important as it does come up
86852   ** in the following:
86853   **
86854   **     CREATE TABLE main.ex1(a);
86855   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
86856   **     SELECT * FROM temp.ex1;
86857   */
86858   if( pTable->nCol<0 ){
86859     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
86860     return 1;
86861   }
86862   assert( pTable->nCol>=0 );
86863 
86864   /* If we get this far, it means we need to compute the table names.
86865   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
86866   ** "*" elements in the results set of the view and will assign cursors
86867   ** to the elements of the FROM clause.  But we do not want these changes
86868   ** to be permanent.  So the computation is done on a copy of the SELECT
86869   ** statement that defines the view.
86870   */
86871   assert( pTable->pSelect );
86872   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
86873   if( pSel ){
86874     u8 enableLookaside = db->lookaside.bEnabled;
86875     n = pParse->nTab;
86876     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
86877     pTable->nCol = -1;
86878     db->lookaside.bEnabled = 0;
86879 #ifndef SQLITE_OMIT_AUTHORIZATION
86880     xAuth = db->xAuth;
86881     db->xAuth = 0;
86882     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
86883     db->xAuth = xAuth;
86884 #else
86885     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
86886 #endif
86887     db->lookaside.bEnabled = enableLookaside;
86888     pParse->nTab = n;
86889     if( pSelTab ){
86890       assert( pTable->aCol==0 );
86891       pTable->nCol = pSelTab->nCol;
86892       pTable->aCol = pSelTab->aCol;
86893       pSelTab->nCol = 0;
86894       pSelTab->aCol = 0;
86895       sqlite3DeleteTable(db, pSelTab);
86896       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
86897       pTable->pSchema->flags |= DB_UnresetViews;
86898     }else{
86899       pTable->nCol = 0;
86900       nErr++;
86901     }
86902     sqlite3SelectDelete(db, pSel);
86903   } else {
86904     nErr++;
86905   }
86906 #endif /* SQLITE_OMIT_VIEW */
86907   return nErr;
86908 }
86909 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
86910 
86911 #ifndef SQLITE_OMIT_VIEW
86912 /*
86913 ** Clear the column names from every VIEW in database idx.
86914 */
86915 static void sqliteViewResetAll(sqlite3 *db, int idx){
86916   HashElem *i;
86917   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
86918   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
86919   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
86920     Table *pTab = sqliteHashData(i);
86921     if( pTab->pSelect ){
86922       sqliteDeleteColumnNames(db, pTab);
86923       pTab->aCol = 0;
86924       pTab->nCol = 0;
86925     }
86926   }
86927   DbClearProperty(db, idx, DB_UnresetViews);
86928 }
86929 #else
86930 # define sqliteViewResetAll(A,B)
86931 #endif /* SQLITE_OMIT_VIEW */
86932 
86933 /*
86934 ** This function is called by the VDBE to adjust the internal schema
86935 ** used by SQLite when the btree layer moves a table root page. The
86936 ** root-page of a table or index in database iDb has changed from iFrom
86937 ** to iTo.
86938 **
86939 ** Ticket #1728:  The symbol table might still contain information
86940 ** on tables and/or indices that are the process of being deleted.
86941 ** If you are unlucky, one of those deleted indices or tables might
86942 ** have the same rootpage number as the real table or index that is
86943 ** being moved.  So we cannot stop searching after the first match
86944 ** because the first match might be for one of the deleted indices
86945 ** or tables and not the table/index that is actually being moved.
86946 ** We must continue looping until all tables and indices with
86947 ** rootpage==iFrom have been converted to have a rootpage of iTo
86948 ** in order to be certain that we got the right one.
86949 */
86950 #ifndef SQLITE_OMIT_AUTOVACUUM
86951 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
86952   HashElem *pElem;
86953   Hash *pHash;
86954   Db *pDb;
86955 
86956   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86957   pDb = &db->aDb[iDb];
86958   pHash = &pDb->pSchema->tblHash;
86959   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
86960     Table *pTab = sqliteHashData(pElem);
86961     if( pTab->tnum==iFrom ){
86962       pTab->tnum = iTo;
86963     }
86964   }
86965   pHash = &pDb->pSchema->idxHash;
86966   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
86967     Index *pIdx = sqliteHashData(pElem);
86968     if( pIdx->tnum==iFrom ){
86969       pIdx->tnum = iTo;
86970     }
86971   }
86972 }
86973 #endif
86974 
86975 /*
86976 ** Write code to erase the table with root-page iTable from database iDb.
86977 ** Also write code to modify the sqlite_master table and internal schema
86978 ** if a root-page of another table is moved by the btree-layer whilst
86979 ** erasing iTable (this can happen with an auto-vacuum database).
86980 */
86981 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
86982   Vdbe *v = sqlite3GetVdbe(pParse);
86983   int r1 = sqlite3GetTempReg(pParse);
86984   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
86985   sqlite3MayAbort(pParse);
86986 #ifndef SQLITE_OMIT_AUTOVACUUM
86987   /* OP_Destroy stores an in integer r1. If this integer
86988   ** is non-zero, then it is the root page number of a table moved to
86989   ** location iTable. The following code modifies the sqlite_master table to
86990   ** reflect this.
86991   **
86992   ** The "#NNN" in the SQL is a special constant that means whatever value
86993   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
86994   ** token for additional information.
86995   */
86996   sqlite3NestedParse(pParse,
86997      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
86998      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
86999 #endif
87000   sqlite3ReleaseTempReg(pParse, r1);
87001 }
87002 
87003 /*
87004 ** Write VDBE code to erase table pTab and all associated indices on disk.
87005 ** Code to update the sqlite_master tables and internal schema definitions
87006 ** in case a root-page belonging to another table is moved by the btree layer
87007 ** is also added (this can happen with an auto-vacuum database).
87008 */
87009 static void destroyTable(Parse *pParse, Table *pTab){
87010 #ifdef SQLITE_OMIT_AUTOVACUUM
87011   Index *pIdx;
87012   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
87013   destroyRootPage(pParse, pTab->tnum, iDb);
87014   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
87015     destroyRootPage(pParse, pIdx->tnum, iDb);
87016   }
87017 #else
87018   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
87019   ** is not defined), then it is important to call OP_Destroy on the
87020   ** table and index root-pages in order, starting with the numerically
87021   ** largest root-page number. This guarantees that none of the root-pages
87022   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
87023   ** following were coded:
87024   **
87025   ** OP_Destroy 4 0
87026   ** ...
87027   ** OP_Destroy 5 0
87028   **
87029   ** and root page 5 happened to be the largest root-page number in the
87030   ** database, then root page 5 would be moved to page 4 by the
87031   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
87032   ** a free-list page.
87033   */
87034   int iTab = pTab->tnum;
87035   int iDestroyed = 0;
87036 
87037   while( 1 ){
87038     Index *pIdx;
87039     int iLargest = 0;
87040 
87041     if( iDestroyed==0 || iTab<iDestroyed ){
87042       iLargest = iTab;
87043     }
87044     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
87045       int iIdx = pIdx->tnum;
87046       assert( pIdx->pSchema==pTab->pSchema );
87047       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
87048         iLargest = iIdx;
87049       }
87050     }
87051     if( iLargest==0 ){
87052       return;
87053     }else{
87054       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
87055       assert( iDb>=0 && iDb<pParse->db->nDb );
87056       destroyRootPage(pParse, iLargest, iDb);
87057       iDestroyed = iLargest;
87058     }
87059   }
87060 #endif
87061 }
87062 
87063 /*
87064 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
87065 ** after a DROP INDEX or DROP TABLE command.
87066 */
87067 static void sqlite3ClearStatTables(
87068   Parse *pParse,         /* The parsing context */
87069   int iDb,               /* The database number */
87070   const char *zType,     /* "idx" or "tbl" */
87071   const char *zName      /* Name of index or table */
87072 ){
87073   int i;
87074   const char *zDbName = pParse->db->aDb[iDb].zName;
87075   for(i=1; i<=4; i++){
87076     char zTab[24];
87077     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
87078     if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
87079       sqlite3NestedParse(pParse,
87080         "DELETE FROM %Q.%s WHERE %s=%Q",
87081         zDbName, zTab, zType, zName
87082       );
87083     }
87084   }
87085 }
87086 
87087 /*
87088 ** Generate code to drop a table.
87089 */
87090 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
87091   Vdbe *v;
87092   sqlite3 *db = pParse->db;
87093   Trigger *pTrigger;
87094   Db *pDb = &db->aDb[iDb];
87095 
87096   v = sqlite3GetVdbe(pParse);
87097   assert( v!=0 );
87098   sqlite3BeginWriteOperation(pParse, 1, iDb);
87099 
87100 #ifndef SQLITE_OMIT_VIRTUALTABLE
87101   if( IsVirtual(pTab) ){
87102     sqlite3VdbeAddOp0(v, OP_VBegin);
87103   }
87104 #endif
87105 
87106   /* Drop all triggers associated with the table being dropped. Code
87107   ** is generated to remove entries from sqlite_master and/or
87108   ** sqlite_temp_master if required.
87109   */
87110   pTrigger = sqlite3TriggerList(pParse, pTab);
87111   while( pTrigger ){
87112     assert( pTrigger->pSchema==pTab->pSchema ||
87113         pTrigger->pSchema==db->aDb[1].pSchema );
87114     sqlite3DropTriggerPtr(pParse, pTrigger);
87115     pTrigger = pTrigger->pNext;
87116   }
87117 
87118 #ifndef SQLITE_OMIT_AUTOINCREMENT
87119   /* Remove any entries of the sqlite_sequence table associated with
87120   ** the table being dropped. This is done before the table is dropped
87121   ** at the btree level, in case the sqlite_sequence table needs to
87122   ** move as a result of the drop (can happen in auto-vacuum mode).
87123   */
87124   if( pTab->tabFlags & TF_Autoincrement ){
87125     sqlite3NestedParse(pParse,
87126       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
87127       pDb->zName, pTab->zName
87128     );
87129   }
87130 #endif
87131 
87132   /* Drop all SQLITE_MASTER table and index entries that refer to the
87133   ** table. The program name loops through the master table and deletes
87134   ** every row that refers to a table of the same name as the one being
87135   ** dropped. Triggers are handled separately because a trigger can be
87136   ** created in the temp database that refers to a table in another
87137   ** database.
87138   */
87139   sqlite3NestedParse(pParse,
87140       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
87141       pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
87142   if( !isView && !IsVirtual(pTab) ){
87143     destroyTable(pParse, pTab);
87144   }
87145 
87146   /* Remove the table entry from SQLite's internal schema and modify
87147   ** the schema cookie.
87148   */
87149   if( IsVirtual(pTab) ){
87150     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
87151   }
87152   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
87153   sqlite3ChangeCookie(pParse, iDb);
87154   sqliteViewResetAll(db, iDb);
87155 }
87156 
87157 /*
87158 ** This routine is called to do the work of a DROP TABLE statement.
87159 ** pName is the name of the table to be dropped.
87160 */
87161 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
87162   Table *pTab;
87163   Vdbe *v;
87164   sqlite3 *db = pParse->db;
87165   int iDb;
87166 
87167   if( db->mallocFailed ){
87168     goto exit_drop_table;
87169   }
87170   assert( pParse->nErr==0 );
87171   assert( pName->nSrc==1 );
87172   if( noErr ) db->suppressErr++;
87173   pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
87174   if( noErr ) db->suppressErr--;
87175 
87176   if( pTab==0 ){
87177     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
87178     goto exit_drop_table;
87179   }
87180   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
87181   assert( iDb>=0 && iDb<db->nDb );
87182 
87183   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
87184   ** it is initialized.
87185   */
87186   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
87187     goto exit_drop_table;
87188   }
87189 #ifndef SQLITE_OMIT_AUTHORIZATION
87190   {
87191     int code;
87192     const char *zTab = SCHEMA_TABLE(iDb);
87193     const char *zDb = db->aDb[iDb].zName;
87194     const char *zArg2 = 0;
87195     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
87196       goto exit_drop_table;
87197     }
87198     if( isView ){
87199       if( !OMIT_TEMPDB && iDb==1 ){
87200         code = SQLITE_DROP_TEMP_VIEW;
87201       }else{
87202         code = SQLITE_DROP_VIEW;
87203       }
87204 #ifndef SQLITE_OMIT_VIRTUALTABLE
87205     }else if( IsVirtual(pTab) ){
87206       code = SQLITE_DROP_VTABLE;
87207       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
87208 #endif
87209     }else{
87210       if( !OMIT_TEMPDB && iDb==1 ){
87211         code = SQLITE_DROP_TEMP_TABLE;
87212       }else{
87213         code = SQLITE_DROP_TABLE;
87214       }
87215     }
87216     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
87217       goto exit_drop_table;
87218     }
87219     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
87220       goto exit_drop_table;
87221     }
87222   }
87223 #endif
87224   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
87225     && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
87226     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
87227     goto exit_drop_table;
87228   }
87229 
87230 #ifndef SQLITE_OMIT_VIEW
87231   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
87232   ** on a table.
87233   */
87234   if( isView && pTab->pSelect==0 ){
87235     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
87236     goto exit_drop_table;
87237   }
87238   if( !isView && pTab->pSelect ){
87239     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
87240     goto exit_drop_table;
87241   }
87242 #endif
87243 
87244   /* Generate code to remove the table from the master table
87245   ** on disk.
87246   */
87247   v = sqlite3GetVdbe(pParse);
87248   if( v ){
87249     sqlite3BeginWriteOperation(pParse, 1, iDb);
87250     sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
87251     sqlite3FkDropTable(pParse, pName, pTab);
87252     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
87253   }
87254 
87255 exit_drop_table:
87256   sqlite3SrcListDelete(db, pName);
87257 }
87258 
87259 /*
87260 ** This routine is called to create a new foreign key on the table
87261 ** currently under construction.  pFromCol determines which columns
87262 ** in the current table point to the foreign key.  If pFromCol==0 then
87263 ** connect the key to the last column inserted.  pTo is the name of
87264 ** the table referred to (a.k.a the "parent" table).  pToCol is a list
87265 ** of tables in the parent pTo table.  flags contains all
87266 ** information about the conflict resolution algorithms specified
87267 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
87268 **
87269 ** An FKey structure is created and added to the table currently
87270 ** under construction in the pParse->pNewTable field.
87271 **
87272 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
87273 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
87274 */
87275 SQLITE_PRIVATE void sqlite3CreateForeignKey(
87276   Parse *pParse,       /* Parsing context */
87277   ExprList *pFromCol,  /* Columns in this table that point to other table */
87278   Token *pTo,          /* Name of the other table */
87279   ExprList *pToCol,    /* Columns in the other table */
87280   int flags            /* Conflict resolution algorithms. */
87281 ){
87282   sqlite3 *db = pParse->db;
87283 #ifndef SQLITE_OMIT_FOREIGN_KEY
87284   FKey *pFKey = 0;
87285   FKey *pNextTo;
87286   Table *p = pParse->pNewTable;
87287   int nByte;
87288   int i;
87289   int nCol;
87290   char *z;
87291 
87292   assert( pTo!=0 );
87293   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
87294   if( pFromCol==0 ){
87295     int iCol = p->nCol-1;
87296     if( NEVER(iCol<0) ) goto fk_end;
87297     if( pToCol && pToCol->nExpr!=1 ){
87298       sqlite3ErrorMsg(pParse, "foreign key on %s"
87299          " should reference only one column of table %T",
87300          p->aCol[iCol].zName, pTo);
87301       goto fk_end;
87302     }
87303     nCol = 1;
87304   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
87305     sqlite3ErrorMsg(pParse,
87306         "number of columns in foreign key does not match the number of "
87307         "columns in the referenced table");
87308     goto fk_end;
87309   }else{
87310     nCol = pFromCol->nExpr;
87311   }
87312   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
87313   if( pToCol ){
87314     for(i=0; i<pToCol->nExpr; i++){
87315       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
87316     }
87317   }
87318   pFKey = sqlite3DbMallocZero(db, nByte );
87319   if( pFKey==0 ){
87320     goto fk_end;
87321   }
87322   pFKey->pFrom = p;
87323   pFKey->pNextFrom = p->pFKey;
87324   z = (char*)&pFKey->aCol[nCol];
87325   pFKey->zTo = z;
87326   memcpy(z, pTo->z, pTo->n);
87327   z[pTo->n] = 0;
87328   sqlite3Dequote(z);
87329   z += pTo->n+1;
87330   pFKey->nCol = nCol;
87331   if( pFromCol==0 ){
87332     pFKey->aCol[0].iFrom = p->nCol-1;
87333   }else{
87334     for(i=0; i<nCol; i++){
87335       int j;
87336       for(j=0; j<p->nCol; j++){
87337         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
87338           pFKey->aCol[i].iFrom = j;
87339           break;
87340         }
87341       }
87342       if( j>=p->nCol ){
87343         sqlite3ErrorMsg(pParse,
87344           "unknown column \"%s\" in foreign key definition",
87345           pFromCol->a[i].zName);
87346         goto fk_end;
87347       }
87348     }
87349   }
87350   if( pToCol ){
87351     for(i=0; i<nCol; i++){
87352       int n = sqlite3Strlen30(pToCol->a[i].zName);
87353       pFKey->aCol[i].zCol = z;
87354       memcpy(z, pToCol->a[i].zName, n);
87355       z[n] = 0;
87356       z += n+1;
87357     }
87358   }
87359   pFKey->isDeferred = 0;
87360   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
87361   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
87362 
87363   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
87364   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
87365       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
87366   );
87367   if( pNextTo==pFKey ){
87368     db->mallocFailed = 1;
87369     goto fk_end;
87370   }
87371   if( pNextTo ){
87372     assert( pNextTo->pPrevTo==0 );
87373     pFKey->pNextTo = pNextTo;
87374     pNextTo->pPrevTo = pFKey;
87375   }
87376 
87377   /* Link the foreign key to the table as the last step.
87378   */
87379   p->pFKey = pFKey;
87380   pFKey = 0;
87381 
87382 fk_end:
87383   sqlite3DbFree(db, pFKey);
87384 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
87385   sqlite3ExprListDelete(db, pFromCol);
87386   sqlite3ExprListDelete(db, pToCol);
87387 }
87388 
87389 /*
87390 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
87391 ** clause is seen as part of a foreign key definition.  The isDeferred
87392 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
87393 ** The behavior of the most recently created foreign key is adjusted
87394 ** accordingly.
87395 */
87396 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
87397 #ifndef SQLITE_OMIT_FOREIGN_KEY
87398   Table *pTab;
87399   FKey *pFKey;
87400   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
87401   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
87402   pFKey->isDeferred = (u8)isDeferred;
87403 #endif
87404 }
87405 
87406 /*
87407 ** Generate code that will erase and refill index *pIdx.  This is
87408 ** used to initialize a newly created index or to recompute the
87409 ** content of an index in response to a REINDEX command.
87410 **
87411 ** if memRootPage is not negative, it means that the index is newly
87412 ** created.  The register specified by memRootPage contains the
87413 ** root page number of the index.  If memRootPage is negative, then
87414 ** the index already exists and must be cleared before being refilled and
87415 ** the root page number of the index is taken from pIndex->tnum.
87416 */
87417 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
87418   Table *pTab = pIndex->pTable;  /* The table that is indexed */
87419   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
87420   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
87421   int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
87422   int addr1;                     /* Address of top of loop */
87423   int addr2;                     /* Address to jump to for next iteration */
87424   int tnum;                      /* Root page of index */
87425   int iPartIdxLabel;             /* Jump to this label to skip a row */
87426   Vdbe *v;                       /* Generate code into this virtual machine */
87427   KeyInfo *pKey;                 /* KeyInfo for index */
87428   int regRecord;                 /* Register holding assemblied index record */
87429   sqlite3 *db = pParse->db;      /* The database connection */
87430   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
87431 
87432 #ifndef SQLITE_OMIT_AUTHORIZATION
87433   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
87434       db->aDb[iDb].zName ) ){
87435     return;
87436   }
87437 #endif
87438 
87439   /* Require a write-lock on the table to perform this operation */
87440   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
87441 
87442   v = sqlite3GetVdbe(pParse);
87443   if( v==0 ) return;
87444   if( memRootPage>=0 ){
87445     tnum = memRootPage;
87446   }else{
87447     tnum = pIndex->tnum;
87448   }
87449   pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
87450 
87451   /* Open the sorter cursor if we are to use one. */
87452   iSorter = pParse->nTab++;
87453   sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)
87454                     sqlite3KeyInfoRef(pKey), P4_KEYINFO);
87455 
87456   /* Open the table. Loop through all rows of the table, inserting index
87457   ** records into the sorter. */
87458   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
87459   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); VdbeCoverage(v);
87460   regRecord = sqlite3GetTempReg(pParse);
87461 
87462   sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
87463   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
87464   sqlite3VdbeResolveLabel(v, iPartIdxLabel);
87465   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); VdbeCoverage(v);
87466   sqlite3VdbeJumpHere(v, addr1);
87467   if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
87468   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
87469                     (char *)pKey, P4_KEYINFO);
87470   sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
87471 
87472   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0); VdbeCoverage(v);
87473   assert( pKey!=0 || db->mallocFailed || pParse->nErr );
87474   if( pIndex->onError!=OE_None && pKey!=0 ){
87475     int j2 = sqlite3VdbeCurrentAddr(v) + 3;
87476     sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
87477     addr2 = sqlite3VdbeCurrentAddr(v);
87478     sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
87479                          pKey->nField - pIndex->nKeyCol); VdbeCoverage(v);
87480     sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
87481   }else{
87482     addr2 = sqlite3VdbeCurrentAddr(v);
87483   }
87484   sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
87485   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
87486   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
87487   sqlite3ReleaseTempReg(pParse, regRecord);
87488   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2); VdbeCoverage(v);
87489   sqlite3VdbeJumpHere(v, addr1);
87490 
87491   sqlite3VdbeAddOp1(v, OP_Close, iTab);
87492   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
87493   sqlite3VdbeAddOp1(v, OP_Close, iSorter);
87494 }
87495 
87496 /*
87497 ** Allocate heap space to hold an Index object with nCol columns.
87498 **
87499 ** Increase the allocation size to provide an extra nExtra bytes
87500 ** of 8-byte aligned space after the Index object and return a
87501 ** pointer to this extra space in *ppExtra.
87502 */
87503 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
87504   sqlite3 *db,         /* Database connection */
87505   i16 nCol,            /* Total number of columns in the index */
87506   int nExtra,          /* Number of bytes of extra space to alloc */
87507   char **ppExtra       /* Pointer to the "extra" space */
87508 ){
87509   Index *p;            /* Allocated index object */
87510   int nByte;           /* Bytes of space for Index object + arrays */
87511 
87512   nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
87513           ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
87514           ROUND8(sizeof(tRowcnt)*(nCol+1) +    /* Index.aiRowEst   */
87515                  sizeof(i16)*nCol +            /* Index.aiColumn   */
87516                  sizeof(u8)*nCol);             /* Index.aSortOrder */
87517   p = sqlite3DbMallocZero(db, nByte + nExtra);
87518   if( p ){
87519     char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
87520     p->azColl = (char**)pExtra;      pExtra += ROUND8(sizeof(char*)*nCol);
87521     p->aiRowEst = (tRowcnt*)pExtra;  pExtra += sizeof(tRowcnt)*(nCol+1);
87522     p->aiColumn = (i16*)pExtra;      pExtra += sizeof(i16)*nCol;
87523     p->aSortOrder = (u8*)pExtra;
87524     p->nColumn = nCol;
87525     p->nKeyCol = nCol - 1;
87526     *ppExtra = ((char*)p) + nByte;
87527   }
87528   return p;
87529 }
87530 
87531 /*
87532 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index
87533 ** and pTblList is the name of the table that is to be indexed.  Both will
87534 ** be NULL for a primary key or an index that is created to satisfy a
87535 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
87536 ** as the table to be indexed.  pParse->pNewTable is a table that is
87537 ** currently being constructed by a CREATE TABLE statement.
87538 **
87539 ** pList is a list of columns to be indexed.  pList will be NULL if this
87540 ** is a primary key or unique-constraint on the most recent column added
87541 ** to the table currently under construction.
87542 **
87543 ** If the index is created successfully, return a pointer to the new Index
87544 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
87545 ** as the tables primary key (Index.autoIndex==2).
87546 */
87547 SQLITE_PRIVATE Index *sqlite3CreateIndex(
87548   Parse *pParse,     /* All information about this parse */
87549   Token *pName1,     /* First part of index name. May be NULL */
87550   Token *pName2,     /* Second part of index name. May be NULL */
87551   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
87552   ExprList *pList,   /* A list of columns to be indexed */
87553   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
87554   Token *pStart,     /* The CREATE token that begins this statement */
87555   Expr *pPIWhere,    /* WHERE clause for partial indices */
87556   int sortOrder,     /* Sort order of primary key when pList==NULL */
87557   int ifNotExist     /* Omit error if index already exists */
87558 ){
87559   Index *pRet = 0;     /* Pointer to return */
87560   Table *pTab = 0;     /* Table to be indexed */
87561   Index *pIndex = 0;   /* The index to be created */
87562   char *zName = 0;     /* Name of the index */
87563   int nName;           /* Number of characters in zName */
87564   int i, j;
87565   DbFixer sFix;        /* For assigning database names to pTable */
87566   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
87567   sqlite3 *db = pParse->db;
87568   Db *pDb;             /* The specific table containing the indexed database */
87569   int iDb;             /* Index of the database that is being written */
87570   Token *pName = 0;    /* Unqualified name of the index to create */
87571   struct ExprList_item *pListItem; /* For looping over pList */
87572   const Column *pTabCol;           /* A column in the table */
87573   int nExtra = 0;                  /* Space allocated for zExtra[] */
87574   int nExtraCol;                   /* Number of extra columns needed */
87575   char *zExtra = 0;                /* Extra space after the Index object */
87576   Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
87577 
87578   assert( pParse->nErr==0 );      /* Never called with prior errors */
87579   if( db->mallocFailed || IN_DECLARE_VTAB ){
87580     goto exit_create_index;
87581   }
87582   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
87583     goto exit_create_index;
87584   }
87585 
87586   /*
87587   ** Find the table that is to be indexed.  Return early if not found.
87588   */
87589   if( pTblName!=0 ){
87590 
87591     /* Use the two-part index name to determine the database
87592     ** to search for the table. 'Fix' the table name to this db
87593     ** before looking up the table.
87594     */
87595     assert( pName1 && pName2 );
87596     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
87597     if( iDb<0 ) goto exit_create_index;
87598     assert( pName && pName->z );
87599 
87600 #ifndef SQLITE_OMIT_TEMPDB
87601     /* If the index name was unqualified, check if the table
87602     ** is a temp table. If so, set the database to 1. Do not do this
87603     ** if initialising a database schema.
87604     */
87605     if( !db->init.busy ){
87606       pTab = sqlite3SrcListLookup(pParse, pTblName);
87607       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
87608         iDb = 1;
87609       }
87610     }
87611 #endif
87612 
87613     sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
87614     if( sqlite3FixSrcList(&sFix, pTblName) ){
87615       /* Because the parser constructs pTblName from a single identifier,
87616       ** sqlite3FixSrcList can never fail. */
87617       assert(0);
87618     }
87619     pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
87620     assert( db->mallocFailed==0 || pTab==0 );
87621     if( pTab==0 ) goto exit_create_index;
87622     if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
87623       sqlite3ErrorMsg(pParse,
87624            "cannot create a TEMP index on non-TEMP table \"%s\"",
87625            pTab->zName);
87626       goto exit_create_index;
87627     }
87628     if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
87629   }else{
87630     assert( pName==0 );
87631     assert( pStart==0 );
87632     pTab = pParse->pNewTable;
87633     if( !pTab ) goto exit_create_index;
87634     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
87635   }
87636   pDb = &db->aDb[iDb];
87637 
87638   assert( pTab!=0 );
87639   assert( pParse->nErr==0 );
87640   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
87641        && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
87642     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
87643     goto exit_create_index;
87644   }
87645 #ifndef SQLITE_OMIT_VIEW
87646   if( pTab->pSelect ){
87647     sqlite3ErrorMsg(pParse, "views may not be indexed");
87648     goto exit_create_index;
87649   }
87650 #endif
87651 #ifndef SQLITE_OMIT_VIRTUALTABLE
87652   if( IsVirtual(pTab) ){
87653     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
87654     goto exit_create_index;
87655   }
87656 #endif
87657 
87658   /*
87659   ** Find the name of the index.  Make sure there is not already another
87660   ** index or table with the same name.
87661   **
87662   ** Exception:  If we are reading the names of permanent indices from the
87663   ** sqlite_master table (because some other process changed the schema) and
87664   ** one of the index names collides with the name of a temporary table or
87665   ** index, then we will continue to process this index.
87666   **
87667   ** If pName==0 it means that we are
87668   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
87669   ** own name.
87670   */
87671   if( pName ){
87672     zName = sqlite3NameFromToken(db, pName);
87673     if( zName==0 ) goto exit_create_index;
87674     assert( pName->z!=0 );
87675     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
87676       goto exit_create_index;
87677     }
87678     if( !db->init.busy ){
87679       if( sqlite3FindTable(db, zName, 0)!=0 ){
87680         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
87681         goto exit_create_index;
87682       }
87683     }
87684     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
87685       if( !ifNotExist ){
87686         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
87687       }else{
87688         assert( !db->init.busy );
87689         sqlite3CodeVerifySchema(pParse, iDb);
87690       }
87691       goto exit_create_index;
87692     }
87693   }else{
87694     int n;
87695     Index *pLoop;
87696     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
87697     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
87698     if( zName==0 ){
87699       goto exit_create_index;
87700     }
87701   }
87702 
87703   /* Check for authorization to create an index.
87704   */
87705 #ifndef SQLITE_OMIT_AUTHORIZATION
87706   {
87707     const char *zDb = pDb->zName;
87708     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
87709       goto exit_create_index;
87710     }
87711     i = SQLITE_CREATE_INDEX;
87712     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
87713     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
87714       goto exit_create_index;
87715     }
87716   }
87717 #endif
87718 
87719   /* If pList==0, it means this routine was called to make a primary
87720   ** key out of the last column added to the table under construction.
87721   ** So create a fake list to simulate this.
87722   */
87723   if( pList==0 ){
87724     pList = sqlite3ExprListAppend(pParse, 0, 0);
87725     if( pList==0 ) goto exit_create_index;
87726     pList->a[0].zName = sqlite3DbStrDup(pParse->db,
87727                                         pTab->aCol[pTab->nCol-1].zName);
87728     pList->a[0].sortOrder = (u8)sortOrder;
87729   }
87730 
87731   /* Figure out how many bytes of space are required to store explicitly
87732   ** specified collation sequence names.
87733   */
87734   for(i=0; i<pList->nExpr; i++){
87735     Expr *pExpr = pList->a[i].pExpr;
87736     if( pExpr ){
87737       assert( pExpr->op==TK_COLLATE );
87738       nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
87739     }
87740   }
87741 
87742   /*
87743   ** Allocate the index structure.
87744   */
87745   nName = sqlite3Strlen30(zName);
87746   nExtraCol = pPk ? pPk->nKeyCol : 1;
87747   pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
87748                                       nName + nExtra + 1, &zExtra);
87749   if( db->mallocFailed ){
87750     goto exit_create_index;
87751   }
87752   assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
87753   assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
87754   pIndex->zName = zExtra;
87755   zExtra += nName + 1;
87756   memcpy(pIndex->zName, zName, nName+1);
87757   pIndex->pTable = pTab;
87758   pIndex->onError = (u8)onError;
87759   pIndex->uniqNotNull = onError!=OE_None;
87760   pIndex->autoIndex = (u8)(pName==0);
87761   pIndex->pSchema = db->aDb[iDb].pSchema;
87762   pIndex->nKeyCol = pList->nExpr;
87763   if( pPIWhere ){
87764     sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
87765     pIndex->pPartIdxWhere = pPIWhere;
87766     pPIWhere = 0;
87767   }
87768   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
87769 
87770   /* Check to see if we should honor DESC requests on index columns
87771   */
87772   if( pDb->pSchema->file_format>=4 ){
87773     sortOrderMask = -1;   /* Honor DESC */
87774   }else{
87775     sortOrderMask = 0;    /* Ignore DESC */
87776   }
87777 
87778   /* Scan the names of the columns of the table to be indexed and
87779   ** load the column indices into the Index structure.  Report an error
87780   ** if any column is not found.
87781   **
87782   ** TODO:  Add a test to make sure that the same column is not named
87783   ** more than once within the same index.  Only the first instance of
87784   ** the column will ever be used by the optimizer.  Note that using the
87785   ** same column more than once cannot be an error because that would
87786   ** break backwards compatibility - it needs to be a warning.
87787   */
87788   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
87789     const char *zColName = pListItem->zName;
87790     int requestedSortOrder;
87791     char *zColl;                   /* Collation sequence name */
87792 
87793     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
87794       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
87795     }
87796     if( j>=pTab->nCol ){
87797       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
87798         pTab->zName, zColName);
87799       pParse->checkSchema = 1;
87800       goto exit_create_index;
87801     }
87802     assert( pTab->nCol<=0x7fff && j<=0x7fff );
87803     pIndex->aiColumn[i] = (i16)j;
87804     if( pListItem->pExpr ){
87805       int nColl;
87806       assert( pListItem->pExpr->op==TK_COLLATE );
87807       zColl = pListItem->pExpr->u.zToken;
87808       nColl = sqlite3Strlen30(zColl) + 1;
87809       assert( nExtra>=nColl );
87810       memcpy(zExtra, zColl, nColl);
87811       zColl = zExtra;
87812       zExtra += nColl;
87813       nExtra -= nColl;
87814     }else{
87815       zColl = pTab->aCol[j].zColl;
87816       if( !zColl ) zColl = "BINARY";
87817     }
87818     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
87819       goto exit_create_index;
87820     }
87821     pIndex->azColl[i] = zColl;
87822     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
87823     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
87824     if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
87825   }
87826   if( pPk ){
87827     for(j=0; j<pPk->nKeyCol; j++){
87828       int x = pPk->aiColumn[j];
87829       if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
87830         pIndex->nColumn--;
87831       }else{
87832         pIndex->aiColumn[i] = x;
87833         pIndex->azColl[i] = pPk->azColl[j];
87834         pIndex->aSortOrder[i] = pPk->aSortOrder[j];
87835         i++;
87836       }
87837     }
87838     assert( i==pIndex->nColumn );
87839   }else{
87840     pIndex->aiColumn[i] = -1;
87841     pIndex->azColl[i] = "BINARY";
87842   }
87843   sqlite3DefaultRowEst(pIndex);
87844   if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
87845 
87846   if( pTab==pParse->pNewTable ){
87847     /* This routine has been called to create an automatic index as a
87848     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
87849     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
87850     ** i.e. one of:
87851     **
87852     ** CREATE TABLE t(x PRIMARY KEY, y);
87853     ** CREATE TABLE t(x, y, UNIQUE(x, y));
87854     **
87855     ** Either way, check to see if the table already has such an index. If
87856     ** so, don't bother creating this one. This only applies to
87857     ** automatically created indices. Users can do as they wish with
87858     ** explicit indices.
87859     **
87860     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
87861     ** (and thus suppressing the second one) even if they have different
87862     ** sort orders.
87863     **
87864     ** If there are different collating sequences or if the columns of
87865     ** the constraint occur in different orders, then the constraints are
87866     ** considered distinct and both result in separate indices.
87867     */
87868     Index *pIdx;
87869     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
87870       int k;
87871       assert( pIdx->onError!=OE_None );
87872       assert( pIdx->autoIndex );
87873       assert( pIndex->onError!=OE_None );
87874 
87875       if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
87876       for(k=0; k<pIdx->nKeyCol; k++){
87877         const char *z1;
87878         const char *z2;
87879         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
87880         z1 = pIdx->azColl[k];
87881         z2 = pIndex->azColl[k];
87882         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
87883       }
87884       if( k==pIdx->nKeyCol ){
87885         if( pIdx->onError!=pIndex->onError ){
87886           /* This constraint creates the same index as a previous
87887           ** constraint specified somewhere in the CREATE TABLE statement.
87888           ** However the ON CONFLICT clauses are different. If both this
87889           ** constraint and the previous equivalent constraint have explicit
87890           ** ON CONFLICT clauses this is an error. Otherwise, use the
87891           ** explicitly specified behavior for the index.
87892           */
87893           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
87894             sqlite3ErrorMsg(pParse,
87895                 "conflicting ON CONFLICT clauses specified", 0);
87896           }
87897           if( pIdx->onError==OE_Default ){
87898             pIdx->onError = pIndex->onError;
87899           }
87900         }
87901         goto exit_create_index;
87902       }
87903     }
87904   }
87905 
87906   /* Link the new Index structure to its table and to the other
87907   ** in-memory database structures.
87908   */
87909   if( db->init.busy ){
87910     Index *p;
87911     assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
87912     p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
87913                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
87914                           pIndex);
87915     if( p ){
87916       assert( p==pIndex );  /* Malloc must have failed */
87917       db->mallocFailed = 1;
87918       goto exit_create_index;
87919     }
87920     db->flags |= SQLITE_InternChanges;
87921     if( pTblName!=0 ){
87922       pIndex->tnum = db->init.newTnum;
87923     }
87924   }
87925 
87926   /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
87927   ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
87928   ** emit code to allocate the index rootpage on disk and make an entry for
87929   ** the index in the sqlite_master table and populate the index with
87930   ** content.  But, do not do this if we are simply reading the sqlite_master
87931   ** table to parse the schema, or if this index is the PRIMARY KEY index
87932   ** of a WITHOUT ROWID table.
87933   **
87934   ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
87935   ** or UNIQUE index in a CREATE TABLE statement.  Since the table
87936   ** has just been created, it contains no data and the index initialization
87937   ** step can be skipped.
87938   */
87939   else if( pParse->nErr==0 && (HasRowid(pTab) || pTblName!=0) ){
87940     Vdbe *v;
87941     char *zStmt;
87942     int iMem = ++pParse->nMem;
87943 
87944     v = sqlite3GetVdbe(pParse);
87945     if( v==0 ) goto exit_create_index;
87946 
87947 
87948     /* Create the rootpage for the index
87949     */
87950     sqlite3BeginWriteOperation(pParse, 1, iDb);
87951     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
87952 
87953     /* Gather the complete text of the CREATE INDEX statement into
87954     ** the zStmt variable
87955     */
87956     if( pStart ){
87957       int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
87958       if( pName->z[n-1]==';' ) n--;
87959       /* A named index with an explicit CREATE INDEX statement */
87960       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
87961         onError==OE_None ? "" : " UNIQUE", n, pName->z);
87962     }else{
87963       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
87964       /* zStmt = sqlite3MPrintf(""); */
87965       zStmt = 0;
87966     }
87967 
87968     /* Add an entry in sqlite_master for this index
87969     */
87970     sqlite3NestedParse(pParse,
87971         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
87972         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
87973         pIndex->zName,
87974         pTab->zName,
87975         iMem,
87976         zStmt
87977     );
87978     sqlite3DbFree(db, zStmt);
87979 
87980     /* Fill the index with data and reparse the schema. Code an OP_Expire
87981     ** to invalidate all pre-compiled statements.
87982     */
87983     if( pTblName ){
87984       sqlite3RefillIndex(pParse, pIndex, iMem);
87985       sqlite3ChangeCookie(pParse, iDb);
87986       sqlite3VdbeAddParseSchemaOp(v, iDb,
87987          sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
87988       sqlite3VdbeAddOp1(v, OP_Expire, 0);
87989     }
87990   }
87991 
87992   /* When adding an index to the list of indices for a table, make
87993   ** sure all indices labeled OE_Replace come after all those labeled
87994   ** OE_Ignore.  This is necessary for the correct constraint check
87995   ** processing (in sqlite3GenerateConstraintChecks()) as part of
87996   ** UPDATE and INSERT statements.
87997   */
87998   if( db->init.busy || pTblName==0 ){
87999     if( onError!=OE_Replace || pTab->pIndex==0
88000          || pTab->pIndex->onError==OE_Replace){
88001       pIndex->pNext = pTab->pIndex;
88002       pTab->pIndex = pIndex;
88003     }else{
88004       Index *pOther = pTab->pIndex;
88005       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
88006         pOther = pOther->pNext;
88007       }
88008       pIndex->pNext = pOther->pNext;
88009       pOther->pNext = pIndex;
88010     }
88011     pRet = pIndex;
88012     pIndex = 0;
88013   }
88014 
88015   /* Clean up before exiting */
88016 exit_create_index:
88017   if( pIndex ) freeIndex(db, pIndex);
88018   sqlite3ExprDelete(db, pPIWhere);
88019   sqlite3ExprListDelete(db, pList);
88020   sqlite3SrcListDelete(db, pTblName);
88021   sqlite3DbFree(db, zName);
88022   return pRet;
88023 }
88024 
88025 /*
88026 ** Fill the Index.aiRowEst[] array with default information - information
88027 ** to be used when we have not run the ANALYZE command.
88028 **
88029 ** aiRowEst[0] is suppose to contain the number of elements in the index.
88030 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
88031 ** number of rows in the table that match any particular value of the
88032 ** first column of the index.  aiRowEst[2] is an estimate of the number
88033 ** of rows that match any particular combiniation of the first 2 columns
88034 ** of the index.  And so forth.  It must always be the case that
88035 *
88036 **           aiRowEst[N]<=aiRowEst[N-1]
88037 **           aiRowEst[N]>=1
88038 **
88039 ** Apart from that, we have little to go on besides intuition as to
88040 ** how aiRowEst[] should be initialized.  The numbers generated here
88041 ** are based on typical values found in actual indices.
88042 */
88043 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
88044   tRowcnt *a = pIdx->aiRowEst;
88045   int i;
88046   tRowcnt n;
88047   assert( a!=0 );
88048   a[0] = pIdx->pTable->nRowEst;
88049   if( a[0]<10 ) a[0] = 10;
88050   n = 10;
88051   for(i=1; i<=pIdx->nKeyCol; i++){
88052     a[i] = n;
88053     if( n>5 ) n--;
88054   }
88055   if( pIdx->onError!=OE_None ){
88056     a[pIdx->nKeyCol] = 1;
88057   }
88058 }
88059 
88060 /*
88061 ** This routine will drop an existing named index.  This routine
88062 ** implements the DROP INDEX statement.
88063 */
88064 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
88065   Index *pIndex;
88066   Vdbe *v;
88067   sqlite3 *db = pParse->db;
88068   int iDb;
88069 
88070   assert( pParse->nErr==0 );   /* Never called with prior errors */
88071   if( db->mallocFailed ){
88072     goto exit_drop_index;
88073   }
88074   assert( pName->nSrc==1 );
88075   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
88076     goto exit_drop_index;
88077   }
88078   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
88079   if( pIndex==0 ){
88080     if( !ifExists ){
88081       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
88082     }else{
88083       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
88084     }
88085     pParse->checkSchema = 1;
88086     goto exit_drop_index;
88087   }
88088   if( pIndex->autoIndex ){
88089     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
88090       "or PRIMARY KEY constraint cannot be dropped", 0);
88091     goto exit_drop_index;
88092   }
88093   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
88094 #ifndef SQLITE_OMIT_AUTHORIZATION
88095   {
88096     int code = SQLITE_DROP_INDEX;
88097     Table *pTab = pIndex->pTable;
88098     const char *zDb = db->aDb[iDb].zName;
88099     const char *zTab = SCHEMA_TABLE(iDb);
88100     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
88101       goto exit_drop_index;
88102     }
88103     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
88104     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
88105       goto exit_drop_index;
88106     }
88107   }
88108 #endif
88109 
88110   /* Generate code to remove the index and from the master table */
88111   v = sqlite3GetVdbe(pParse);
88112   if( v ){
88113     sqlite3BeginWriteOperation(pParse, 1, iDb);
88114     sqlite3NestedParse(pParse,
88115        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
88116        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
88117     );
88118     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
88119     sqlite3ChangeCookie(pParse, iDb);
88120     destroyRootPage(pParse, pIndex->tnum, iDb);
88121     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
88122   }
88123 
88124 exit_drop_index:
88125   sqlite3SrcListDelete(db, pName);
88126 }
88127 
88128 /*
88129 ** pArray is a pointer to an array of objects. Each object in the
88130 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
88131 ** to extend the array so that there is space for a new object at the end.
88132 **
88133 ** When this function is called, *pnEntry contains the current size of
88134 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
88135 ** in total).
88136 **
88137 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
88138 ** space allocated for the new object is zeroed, *pnEntry updated to
88139 ** reflect the new size of the array and a pointer to the new allocation
88140 ** returned. *pIdx is set to the index of the new array entry in this case.
88141 **
88142 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
88143 ** unchanged and a copy of pArray returned.
88144 */
88145 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
88146   sqlite3 *db,      /* Connection to notify of malloc failures */
88147   void *pArray,     /* Array of objects.  Might be reallocated */
88148   int szEntry,      /* Size of each object in the array */
88149   int *pnEntry,     /* Number of objects currently in use */
88150   int *pIdx         /* Write the index of a new slot here */
88151 ){
88152   char *z;
88153   int n = *pnEntry;
88154   if( (n & (n-1))==0 ){
88155     int sz = (n==0) ? 1 : 2*n;
88156     void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
88157     if( pNew==0 ){
88158       *pIdx = -1;
88159       return pArray;
88160     }
88161     pArray = pNew;
88162   }
88163   z = (char*)pArray;
88164   memset(&z[n * szEntry], 0, szEntry);
88165   *pIdx = n;
88166   ++*pnEntry;
88167   return pArray;
88168 }
88169 
88170 /*
88171 ** Append a new element to the given IdList.  Create a new IdList if
88172 ** need be.
88173 **
88174 ** A new IdList is returned, or NULL if malloc() fails.
88175 */
88176 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
88177   int i;
88178   if( pList==0 ){
88179     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
88180     if( pList==0 ) return 0;
88181   }
88182   pList->a = sqlite3ArrayAllocate(
88183       db,
88184       pList->a,
88185       sizeof(pList->a[0]),
88186       &pList->nId,
88187       &i
88188   );
88189   if( i<0 ){
88190     sqlite3IdListDelete(db, pList);
88191     return 0;
88192   }
88193   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
88194   return pList;
88195 }
88196 
88197 /*
88198 ** Delete an IdList.
88199 */
88200 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
88201   int i;
88202   if( pList==0 ) return;
88203   for(i=0; i<pList->nId; i++){
88204     sqlite3DbFree(db, pList->a[i].zName);
88205   }
88206   sqlite3DbFree(db, pList->a);
88207   sqlite3DbFree(db, pList);
88208 }
88209 
88210 /*
88211 ** Return the index in pList of the identifier named zId.  Return -1
88212 ** if not found.
88213 */
88214 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
88215   int i;
88216   if( pList==0 ) return -1;
88217   for(i=0; i<pList->nId; i++){
88218     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
88219   }
88220   return -1;
88221 }
88222 
88223 /*
88224 ** Expand the space allocated for the given SrcList object by
88225 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
88226 ** New slots are zeroed.
88227 **
88228 ** For example, suppose a SrcList initially contains two entries: A,B.
88229 ** To append 3 new entries onto the end, do this:
88230 **
88231 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
88232 **
88233 ** After the call above it would contain:  A, B, nil, nil, nil.
88234 ** If the iStart argument had been 1 instead of 2, then the result
88235 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
88236 ** the iStart value would be 0.  The result then would
88237 ** be: nil, nil, nil, A, B.
88238 **
88239 ** If a memory allocation fails the SrcList is unchanged.  The
88240 ** db->mallocFailed flag will be set to true.
88241 */
88242 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
88243   sqlite3 *db,       /* Database connection to notify of OOM errors */
88244   SrcList *pSrc,     /* The SrcList to be enlarged */
88245   int nExtra,        /* Number of new slots to add to pSrc->a[] */
88246   int iStart         /* Index in pSrc->a[] of first new slot */
88247 ){
88248   int i;
88249 
88250   /* Sanity checking on calling parameters */
88251   assert( iStart>=0 );
88252   assert( nExtra>=1 );
88253   assert( pSrc!=0 );
88254   assert( iStart<=pSrc->nSrc );
88255 
88256   /* Allocate additional space if needed */
88257   if( (u32)pSrc->nSrc+nExtra>pSrc->nAlloc ){
88258     SrcList *pNew;
88259     int nAlloc = pSrc->nSrc+nExtra;
88260     int nGot;
88261     pNew = sqlite3DbRealloc(db, pSrc,
88262                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
88263     if( pNew==0 ){
88264       assert( db->mallocFailed );
88265       return pSrc;
88266     }
88267     pSrc = pNew;
88268     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
88269     pSrc->nAlloc = nGot;
88270   }
88271 
88272   /* Move existing slots that come after the newly inserted slots
88273   ** out of the way */
88274   for(i=pSrc->nSrc-1; i>=iStart; i--){
88275     pSrc->a[i+nExtra] = pSrc->a[i];
88276   }
88277   pSrc->nSrc += nExtra;
88278 
88279   /* Zero the newly allocated slots */
88280   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
88281   for(i=iStart; i<iStart+nExtra; i++){
88282     pSrc->a[i].iCursor = -1;
88283   }
88284 
88285   /* Return a pointer to the enlarged SrcList */
88286   return pSrc;
88287 }
88288 
88289 
88290 /*
88291 ** Append a new table name to the given SrcList.  Create a new SrcList if
88292 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
88293 **
88294 ** A SrcList is returned, or NULL if there is an OOM error.  The returned
88295 ** SrcList might be the same as the SrcList that was input or it might be
88296 ** a new one.  If an OOM error does occurs, then the prior value of pList
88297 ** that is input to this routine is automatically freed.
88298 **
88299 ** If pDatabase is not null, it means that the table has an optional
88300 ** database name prefix.  Like this:  "database.table".  The pDatabase
88301 ** points to the table name and the pTable points to the database name.
88302 ** The SrcList.a[].zName field is filled with the table name which might
88303 ** come from pTable (if pDatabase is NULL) or from pDatabase.
88304 ** SrcList.a[].zDatabase is filled with the database name from pTable,
88305 ** or with NULL if no database is specified.
88306 **
88307 ** In other words, if call like this:
88308 **
88309 **         sqlite3SrcListAppend(D,A,B,0);
88310 **
88311 ** Then B is a table name and the database name is unspecified.  If called
88312 ** like this:
88313 **
88314 **         sqlite3SrcListAppend(D,A,B,C);
88315 **
88316 ** Then C is the table name and B is the database name.  If C is defined
88317 ** then so is B.  In other words, we never have a case where:
88318 **
88319 **         sqlite3SrcListAppend(D,A,0,C);
88320 **
88321 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
88322 ** before being added to the SrcList.
88323 */
88324 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
88325   sqlite3 *db,        /* Connection to notify of malloc failures */
88326   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
88327   Token *pTable,      /* Table to append */
88328   Token *pDatabase    /* Database of the table */
88329 ){
88330   struct SrcList_item *pItem;
88331   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
88332   if( pList==0 ){
88333     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
88334     if( pList==0 ) return 0;
88335     pList->nAlloc = 1;
88336   }
88337   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
88338   if( db->mallocFailed ){
88339     sqlite3SrcListDelete(db, pList);
88340     return 0;
88341   }
88342   pItem = &pList->a[pList->nSrc-1];
88343   if( pDatabase && pDatabase->z==0 ){
88344     pDatabase = 0;
88345   }
88346   if( pDatabase ){
88347     Token *pTemp = pDatabase;
88348     pDatabase = pTable;
88349     pTable = pTemp;
88350   }
88351   pItem->zName = sqlite3NameFromToken(db, pTable);
88352   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
88353   return pList;
88354 }
88355 
88356 /*
88357 ** Assign VdbeCursor index numbers to all tables in a SrcList
88358 */
88359 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
88360   int i;
88361   struct SrcList_item *pItem;
88362   assert(pList || pParse->db->mallocFailed );
88363   if( pList ){
88364     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
88365       if( pItem->iCursor>=0 ) break;
88366       pItem->iCursor = pParse->nTab++;
88367       if( pItem->pSelect ){
88368         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
88369       }
88370     }
88371   }
88372 }
88373 
88374 /*
88375 ** Delete an entire SrcList including all its substructure.
88376 */
88377 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
88378   int i;
88379   struct SrcList_item *pItem;
88380   if( pList==0 ) return;
88381   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
88382     sqlite3DbFree(db, pItem->zDatabase);
88383     sqlite3DbFree(db, pItem->zName);
88384     sqlite3DbFree(db, pItem->zAlias);
88385     sqlite3DbFree(db, pItem->zIndex);
88386     sqlite3DeleteTable(db, pItem->pTab);
88387     sqlite3SelectDelete(db, pItem->pSelect);
88388     sqlite3ExprDelete(db, pItem->pOn);
88389     sqlite3IdListDelete(db, pItem->pUsing);
88390   }
88391   sqlite3DbFree(db, pList);
88392 }
88393 
88394 /*
88395 ** This routine is called by the parser to add a new term to the
88396 ** end of a growing FROM clause.  The "p" parameter is the part of
88397 ** the FROM clause that has already been constructed.  "p" is NULL
88398 ** if this is the first term of the FROM clause.  pTable and pDatabase
88399 ** are the name of the table and database named in the FROM clause term.
88400 ** pDatabase is NULL if the database name qualifier is missing - the
88401 ** usual case.  If the term has a alias, then pAlias points to the
88402 ** alias token.  If the term is a subquery, then pSubquery is the
88403 ** SELECT statement that the subquery encodes.  The pTable and
88404 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
88405 ** parameters are the content of the ON and USING clauses.
88406 **
88407 ** Return a new SrcList which encodes is the FROM with the new
88408 ** term added.
88409 */
88410 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
88411   Parse *pParse,          /* Parsing context */
88412   SrcList *p,             /* The left part of the FROM clause already seen */
88413   Token *pTable,          /* Name of the table to add to the FROM clause */
88414   Token *pDatabase,       /* Name of the database containing pTable */
88415   Token *pAlias,          /* The right-hand side of the AS subexpression */
88416   Select *pSubquery,      /* A subquery used in place of a table name */
88417   Expr *pOn,              /* The ON clause of a join */
88418   IdList *pUsing          /* The USING clause of a join */
88419 ){
88420   struct SrcList_item *pItem;
88421   sqlite3 *db = pParse->db;
88422   if( !p && (pOn || pUsing) ){
88423     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
88424       (pOn ? "ON" : "USING")
88425     );
88426     goto append_from_error;
88427   }
88428   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
88429   if( p==0 || NEVER(p->nSrc==0) ){
88430     goto append_from_error;
88431   }
88432   pItem = &p->a[p->nSrc-1];
88433   assert( pAlias!=0 );
88434   if( pAlias->n ){
88435     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
88436   }
88437   pItem->pSelect = pSubquery;
88438   pItem->pOn = pOn;
88439   pItem->pUsing = pUsing;
88440   return p;
88441 
88442  append_from_error:
88443   assert( p==0 );
88444   sqlite3ExprDelete(db, pOn);
88445   sqlite3IdListDelete(db, pUsing);
88446   sqlite3SelectDelete(db, pSubquery);
88447   return 0;
88448 }
88449 
88450 /*
88451 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
88452 ** element of the source-list passed as the second argument.
88453 */
88454 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
88455   assert( pIndexedBy!=0 );
88456   if( p && ALWAYS(p->nSrc>0) ){
88457     struct SrcList_item *pItem = &p->a[p->nSrc-1];
88458     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
88459     if( pIndexedBy->n==1 && !pIndexedBy->z ){
88460       /* A "NOT INDEXED" clause was supplied. See parse.y
88461       ** construct "indexed_opt" for details. */
88462       pItem->notIndexed = 1;
88463     }else{
88464       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
88465     }
88466   }
88467 }
88468 
88469 /*
88470 ** When building up a FROM clause in the parser, the join operator
88471 ** is initially attached to the left operand.  But the code generator
88472 ** expects the join operator to be on the right operand.  This routine
88473 ** Shifts all join operators from left to right for an entire FROM
88474 ** clause.
88475 **
88476 ** Example: Suppose the join is like this:
88477 **
88478 **           A natural cross join B
88479 **
88480 ** The operator is "natural cross join".  The A and B operands are stored
88481 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
88482 ** operator with A.  This routine shifts that operator over to B.
88483 */
88484 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
88485   if( p ){
88486     int i;
88487     assert( p->a || p->nSrc==0 );
88488     for(i=p->nSrc-1; i>0; i--){
88489       p->a[i].jointype = p->a[i-1].jointype;
88490     }
88491     p->a[0].jointype = 0;
88492   }
88493 }
88494 
88495 /*
88496 ** Begin a transaction
88497 */
88498 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
88499   sqlite3 *db;
88500   Vdbe *v;
88501   int i;
88502 
88503   assert( pParse!=0 );
88504   db = pParse->db;
88505   assert( db!=0 );
88506 /*  if( db->aDb[0].pBt==0 ) return; */
88507   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
88508     return;
88509   }
88510   v = sqlite3GetVdbe(pParse);
88511   if( !v ) return;
88512   if( type!=TK_DEFERRED ){
88513     for(i=0; i<db->nDb; i++){
88514       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
88515       sqlite3VdbeUsesBtree(v, i);
88516     }
88517   }
88518   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
88519 }
88520 
88521 /*
88522 ** Commit a transaction
88523 */
88524 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
88525   Vdbe *v;
88526 
88527   assert( pParse!=0 );
88528   assert( pParse->db!=0 );
88529   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
88530     return;
88531   }
88532   v = sqlite3GetVdbe(pParse);
88533   if( v ){
88534     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
88535   }
88536 }
88537 
88538 /*
88539 ** Rollback a transaction
88540 */
88541 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
88542   Vdbe *v;
88543 
88544   assert( pParse!=0 );
88545   assert( pParse->db!=0 );
88546   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
88547     return;
88548   }
88549   v = sqlite3GetVdbe(pParse);
88550   if( v ){
88551     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
88552   }
88553 }
88554 
88555 /*
88556 ** This function is called by the parser when it parses a command to create,
88557 ** release or rollback an SQL savepoint.
88558 */
88559 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
88560   char *zName = sqlite3NameFromToken(pParse->db, pName);
88561   if( zName ){
88562     Vdbe *v = sqlite3GetVdbe(pParse);
88563 #ifndef SQLITE_OMIT_AUTHORIZATION
88564     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
88565     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
88566 #endif
88567     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
88568       sqlite3DbFree(pParse->db, zName);
88569       return;
88570     }
88571     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
88572   }
88573 }
88574 
88575 /*
88576 ** Make sure the TEMP database is open and available for use.  Return
88577 ** the number of errors.  Leave any error messages in the pParse structure.
88578 */
88579 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
88580   sqlite3 *db = pParse->db;
88581   if( db->aDb[1].pBt==0 && !pParse->explain ){
88582     int rc;
88583     Btree *pBt;
88584     static const int flags =
88585           SQLITE_OPEN_READWRITE |
88586           SQLITE_OPEN_CREATE |
88587           SQLITE_OPEN_EXCLUSIVE |
88588           SQLITE_OPEN_DELETEONCLOSE |
88589           SQLITE_OPEN_TEMP_DB;
88590 
88591     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
88592     if( rc!=SQLITE_OK ){
88593       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
88594         "file for storing temporary tables");
88595       pParse->rc = rc;
88596       return 1;
88597     }
88598     db->aDb[1].pBt = pBt;
88599     assert( db->aDb[1].pSchema );
88600     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
88601       db->mallocFailed = 1;
88602       return 1;
88603     }
88604   }
88605   return 0;
88606 }
88607 
88608 /*
88609 ** Record the fact that the schema cookie will need to be verified
88610 ** for database iDb.  The code to actually verify the schema cookie
88611 ** will occur at the end of the top-level VDBE and will be generated
88612 ** later, by sqlite3FinishCoding().
88613 */
88614 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
88615   Parse *pToplevel = sqlite3ParseToplevel(pParse);
88616   sqlite3 *db = pToplevel->db;
88617   yDbMask mask;
88618 
88619   assert( iDb>=0 && iDb<db->nDb );
88620   assert( db->aDb[iDb].pBt!=0 || iDb==1 );
88621   assert( iDb<SQLITE_MAX_ATTACHED+2 );
88622   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
88623   mask = ((yDbMask)1)<<iDb;
88624   if( (pToplevel->cookieMask & mask)==0 ){
88625     pToplevel->cookieMask |= mask;
88626     pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
88627     if( !OMIT_TEMPDB && iDb==1 ){
88628       sqlite3OpenTempDatabase(pToplevel);
88629     }
88630   }
88631 }
88632 
88633 /*
88634 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
88635 ** attached database. Otherwise, invoke it for the database named zDb only.
88636 */
88637 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
88638   sqlite3 *db = pParse->db;
88639   int i;
88640   for(i=0; i<db->nDb; i++){
88641     Db *pDb = &db->aDb[i];
88642     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
88643       sqlite3CodeVerifySchema(pParse, i);
88644     }
88645   }
88646 }
88647 
88648 /*
88649 ** Generate VDBE code that prepares for doing an operation that
88650 ** might change the database.
88651 **
88652 ** This routine starts a new transaction if we are not already within
88653 ** a transaction.  If we are already within a transaction, then a checkpoint
88654 ** is set if the setStatement parameter is true.  A checkpoint should
88655 ** be set for operations that might fail (due to a constraint) part of
88656 ** the way through and which will need to undo some writes without having to
88657 ** rollback the whole transaction.  For operations where all constraints
88658 ** can be checked before any changes are made to the database, it is never
88659 ** necessary to undo a write and the checkpoint should not be set.
88660 */
88661 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
88662   Parse *pToplevel = sqlite3ParseToplevel(pParse);
88663   sqlite3CodeVerifySchema(pParse, iDb);
88664   pToplevel->writeMask |= ((yDbMask)1)<<iDb;
88665   pToplevel->isMultiWrite |= setStatement;
88666 }
88667 
88668 /*
88669 ** Indicate that the statement currently under construction might write
88670 ** more than one entry (example: deleting one row then inserting another,
88671 ** inserting multiple rows in a table, or inserting a row and index entries.)
88672 ** If an abort occurs after some of these writes have completed, then it will
88673 ** be necessary to undo the completed writes.
88674 */
88675 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
88676   Parse *pToplevel = sqlite3ParseToplevel(pParse);
88677   pToplevel->isMultiWrite = 1;
88678 }
88679 
88680 /*
88681 ** The code generator calls this routine if is discovers that it is
88682 ** possible to abort a statement prior to completion.  In order to
88683 ** perform this abort without corrupting the database, we need to make
88684 ** sure that the statement is protected by a statement transaction.
88685 **
88686 ** Technically, we only need to set the mayAbort flag if the
88687 ** isMultiWrite flag was previously set.  There is a time dependency
88688 ** such that the abort must occur after the multiwrite.  This makes
88689 ** some statements involving the REPLACE conflict resolution algorithm
88690 ** go a little faster.  But taking advantage of this time dependency
88691 ** makes it more difficult to prove that the code is correct (in
88692 ** particular, it prevents us from writing an effective
88693 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
88694 ** to take the safe route and skip the optimization.
88695 */
88696 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
88697   Parse *pToplevel = sqlite3ParseToplevel(pParse);
88698   pToplevel->mayAbort = 1;
88699 }
88700 
88701 /*
88702 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
88703 ** error. The onError parameter determines which (if any) of the statement
88704 ** and/or current transaction is rolled back.
88705 */
88706 SQLITE_PRIVATE void sqlite3HaltConstraint(
88707   Parse *pParse,    /* Parsing context */
88708   int errCode,      /* extended error code */
88709   int onError,      /* Constraint type */
88710   char *p4,         /* Error message */
88711   i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
88712   u8 p5Errmsg       /* P5_ErrMsg type */
88713 ){
88714   Vdbe *v = sqlite3GetVdbe(pParse);
88715   assert( (errCode&0xff)==SQLITE_CONSTRAINT );
88716   if( onError==OE_Abort ){
88717     sqlite3MayAbort(pParse);
88718   }
88719   sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
88720   if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
88721 }
88722 
88723 /*
88724 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
88725 */
88726 SQLITE_PRIVATE void sqlite3UniqueConstraint(
88727   Parse *pParse,    /* Parsing context */
88728   int onError,      /* Constraint type */
88729   Index *pIdx       /* The index that triggers the constraint */
88730 ){
88731   char *zErr;
88732   int j;
88733   StrAccum errMsg;
88734   Table *pTab = pIdx->pTable;
88735 
88736   sqlite3StrAccumInit(&errMsg, 0, 0, 200);
88737   errMsg.db = pParse->db;
88738   for(j=0; j<pIdx->nKeyCol; j++){
88739     char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
88740     if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
88741     sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
88742     sqlite3StrAccumAppend(&errMsg, ".", 1);
88743     sqlite3StrAccumAppendAll(&errMsg, zCol);
88744   }
88745   zErr = sqlite3StrAccumFinish(&errMsg);
88746   sqlite3HaltConstraint(pParse,
88747     (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
88748     onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
88749 }
88750 
88751 
88752 /*
88753 ** Code an OP_Halt due to non-unique rowid.
88754 */
88755 SQLITE_PRIVATE void sqlite3RowidConstraint(
88756   Parse *pParse,    /* Parsing context */
88757   int onError,      /* Conflict resolution algorithm */
88758   Table *pTab       /* The table with the non-unique rowid */
88759 ){
88760   char *zMsg;
88761   int rc;
88762   if( pTab->iPKey>=0 ){
88763     zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
88764                           pTab->aCol[pTab->iPKey].zName);
88765     rc = SQLITE_CONSTRAINT_PRIMARYKEY;
88766   }else{
88767     zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
88768     rc = SQLITE_CONSTRAINT_ROWID;
88769   }
88770   sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
88771                         P5_ConstraintUnique);
88772 }
88773 
88774 /*
88775 ** Check to see if pIndex uses the collating sequence pColl.  Return
88776 ** true if it does and false if it does not.
88777 */
88778 #ifndef SQLITE_OMIT_REINDEX
88779 static int collationMatch(const char *zColl, Index *pIndex){
88780   int i;
88781   assert( zColl!=0 );
88782   for(i=0; i<pIndex->nColumn; i++){
88783     const char *z = pIndex->azColl[i];
88784     assert( z!=0 || pIndex->aiColumn[i]<0 );
88785     if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
88786       return 1;
88787     }
88788   }
88789   return 0;
88790 }
88791 #endif
88792 
88793 /*
88794 ** Recompute all indices of pTab that use the collating sequence pColl.
88795 ** If pColl==0 then recompute all indices of pTab.
88796 */
88797 #ifndef SQLITE_OMIT_REINDEX
88798 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
88799   Index *pIndex;              /* An index associated with pTab */
88800 
88801   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
88802     if( zColl==0 || collationMatch(zColl, pIndex) ){
88803       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
88804       sqlite3BeginWriteOperation(pParse, 0, iDb);
88805       sqlite3RefillIndex(pParse, pIndex, -1);
88806     }
88807   }
88808 }
88809 #endif
88810 
88811 /*
88812 ** Recompute all indices of all tables in all databases where the
88813 ** indices use the collating sequence pColl.  If pColl==0 then recompute
88814 ** all indices everywhere.
88815 */
88816 #ifndef SQLITE_OMIT_REINDEX
88817 static void reindexDatabases(Parse *pParse, char const *zColl){
88818   Db *pDb;                    /* A single database */
88819   int iDb;                    /* The database index number */
88820   sqlite3 *db = pParse->db;   /* The database connection */
88821   HashElem *k;                /* For looping over tables in pDb */
88822   Table *pTab;                /* A table in the database */
88823 
88824   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
88825   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
88826     assert( pDb!=0 );
88827     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
88828       pTab = (Table*)sqliteHashData(k);
88829       reindexTable(pParse, pTab, zColl);
88830     }
88831   }
88832 }
88833 #endif
88834 
88835 /*
88836 ** Generate code for the REINDEX command.
88837 **
88838 **        REINDEX                            -- 1
88839 **        REINDEX  <collation>               -- 2
88840 **        REINDEX  ?<database>.?<tablename>  -- 3
88841 **        REINDEX  ?<database>.?<indexname>  -- 4
88842 **
88843 ** Form 1 causes all indices in all attached databases to be rebuilt.
88844 ** Form 2 rebuilds all indices in all databases that use the named
88845 ** collating function.  Forms 3 and 4 rebuild the named index or all
88846 ** indices associated with the named table.
88847 */
88848 #ifndef SQLITE_OMIT_REINDEX
88849 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
88850   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
88851   char *z;                    /* Name of a table or index */
88852   const char *zDb;            /* Name of the database */
88853   Table *pTab;                /* A table in the database */
88854   Index *pIndex;              /* An index associated with pTab */
88855   int iDb;                    /* The database index number */
88856   sqlite3 *db = pParse->db;   /* The database connection */
88857   Token *pObjName;            /* Name of the table or index to be reindexed */
88858 
88859   /* Read the database schema. If an error occurs, leave an error message
88860   ** and code in pParse and return NULL. */
88861   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
88862     return;
88863   }
88864 
88865   if( pName1==0 ){
88866     reindexDatabases(pParse, 0);
88867     return;
88868   }else if( NEVER(pName2==0) || pName2->z==0 ){
88869     char *zColl;
88870     assert( pName1->z );
88871     zColl = sqlite3NameFromToken(pParse->db, pName1);
88872     if( !zColl ) return;
88873     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
88874     if( pColl ){
88875       reindexDatabases(pParse, zColl);
88876       sqlite3DbFree(db, zColl);
88877       return;
88878     }
88879     sqlite3DbFree(db, zColl);
88880   }
88881   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
88882   if( iDb<0 ) return;
88883   z = sqlite3NameFromToken(db, pObjName);
88884   if( z==0 ) return;
88885   zDb = db->aDb[iDb].zName;
88886   pTab = sqlite3FindTable(db, z, zDb);
88887   if( pTab ){
88888     reindexTable(pParse, pTab, 0);
88889     sqlite3DbFree(db, z);
88890     return;
88891   }
88892   pIndex = sqlite3FindIndex(db, z, zDb);
88893   sqlite3DbFree(db, z);
88894   if( pIndex ){
88895     sqlite3BeginWriteOperation(pParse, 0, iDb);
88896     sqlite3RefillIndex(pParse, pIndex, -1);
88897     return;
88898   }
88899   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
88900 }
88901 #endif
88902 
88903 /*
88904 ** Return a KeyInfo structure that is appropriate for the given Index.
88905 **
88906 ** The KeyInfo structure for an index is cached in the Index object.
88907 ** So there might be multiple references to the returned pointer.  The
88908 ** caller should not try to modify the KeyInfo object.
88909 **
88910 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
88911 ** when it has finished using it.
88912 */
88913 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
88914   if( pParse->nErr ) return 0;
88915 #ifndef SQLITE_OMIT_SHARED_CACHE
88916   if( pIdx->pKeyInfo && pIdx->pKeyInfo->db!=pParse->db ){
88917     sqlite3KeyInfoUnref(pIdx->pKeyInfo);
88918     pIdx->pKeyInfo = 0;
88919   }
88920 #endif
88921   if( pIdx->pKeyInfo==0 ){
88922     int i;
88923     int nCol = pIdx->nColumn;
88924     int nKey = pIdx->nKeyCol;
88925     KeyInfo *pKey;
88926     if( pIdx->uniqNotNull ){
88927       pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
88928     }else{
88929       pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
88930     }
88931     if( pKey ){
88932       assert( sqlite3KeyInfoIsWriteable(pKey) );
88933       for(i=0; i<nCol; i++){
88934         char *zColl = pIdx->azColl[i];
88935         assert( zColl!=0 );
88936         pKey->aColl[i] = strcmp(zColl,"BINARY")==0 ? 0 :
88937                           sqlite3LocateCollSeq(pParse, zColl);
88938         pKey->aSortOrder[i] = pIdx->aSortOrder[i];
88939       }
88940       if( pParse->nErr ){
88941         sqlite3KeyInfoUnref(pKey);
88942       }else{
88943         pIdx->pKeyInfo = pKey;
88944       }
88945     }
88946   }
88947   return sqlite3KeyInfoRef(pIdx->pKeyInfo);
88948 }
88949 
88950 #ifndef SQLITE_OMIT_CTE
88951 /*
88952 ** This routine is invoked once per CTE by the parser while parsing a
88953 ** WITH clause.
88954 */
88955 SQLITE_PRIVATE With *sqlite3WithAdd(
88956   Parse *pParse,          /* Parsing context */
88957   With *pWith,            /* Existing WITH clause, or NULL */
88958   Token *pName,           /* Name of the common-table */
88959   ExprList *pArglist,     /* Optional column name list for the table */
88960   Select *pQuery          /* Query used to initialize the table */
88961 ){
88962   sqlite3 *db = pParse->db;
88963   With *pNew;
88964   char *zName;
88965 
88966   /* Check that the CTE name is unique within this WITH clause. If
88967   ** not, store an error in the Parse structure. */
88968   zName = sqlite3NameFromToken(pParse->db, pName);
88969   if( zName && pWith ){
88970     int i;
88971     for(i=0; i<pWith->nCte; i++){
88972       if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
88973         sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
88974       }
88975     }
88976   }
88977 
88978   if( pWith ){
88979     int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
88980     pNew = sqlite3DbRealloc(db, pWith, nByte);
88981   }else{
88982     pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
88983   }
88984   assert( zName!=0 || pNew==0 );
88985   assert( db->mallocFailed==0 || pNew==0 );
88986 
88987   if( pNew==0 ){
88988     sqlite3ExprListDelete(db, pArglist);
88989     sqlite3SelectDelete(db, pQuery);
88990     sqlite3DbFree(db, zName);
88991     pNew = pWith;
88992   }else{
88993     pNew->a[pNew->nCte].pSelect = pQuery;
88994     pNew->a[pNew->nCte].pCols = pArglist;
88995     pNew->a[pNew->nCte].zName = zName;
88996     pNew->a[pNew->nCte].zErr = 0;
88997     pNew->nCte++;
88998   }
88999 
89000   return pNew;
89001 }
89002 
89003 /*
89004 ** Free the contents of the With object passed as the second argument.
89005 */
89006 SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
89007   if( pWith ){
89008     int i;
89009     for(i=0; i<pWith->nCte; i++){
89010       struct Cte *pCte = &pWith->a[i];
89011       sqlite3ExprListDelete(db, pCte->pCols);
89012       sqlite3SelectDelete(db, pCte->pSelect);
89013       sqlite3DbFree(db, pCte->zName);
89014     }
89015     sqlite3DbFree(db, pWith);
89016   }
89017 }
89018 #endif /* !defined(SQLITE_OMIT_CTE) */
89019 
89020 /************** End of build.c ***********************************************/
89021 /************** Begin file callback.c ****************************************/
89022 /*
89023 ** 2005 May 23
89024 **
89025 ** The author disclaims copyright to this source code.  In place of
89026 ** a legal notice, here is a blessing:
89027 **
89028 **    May you do good and not evil.
89029 **    May you find forgiveness for yourself and forgive others.
89030 **    May you share freely, never taking more than you give.
89031 **
89032 *************************************************************************
89033 **
89034 ** This file contains functions used to access the internal hash tables
89035 ** of user defined functions and collation sequences.
89036 */
89037 
89038 
89039 /*
89040 ** Invoke the 'collation needed' callback to request a collation sequence
89041 ** in the encoding enc of name zName, length nName.
89042 */
89043 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
89044   assert( !db->xCollNeeded || !db->xCollNeeded16 );
89045   if( db->xCollNeeded ){
89046     char *zExternal = sqlite3DbStrDup(db, zName);
89047     if( !zExternal ) return;
89048     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
89049     sqlite3DbFree(db, zExternal);
89050   }
89051 #ifndef SQLITE_OMIT_UTF16
89052   if( db->xCollNeeded16 ){
89053     char const *zExternal;
89054     sqlite3_value *pTmp = sqlite3ValueNew(db);
89055     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
89056     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
89057     if( zExternal ){
89058       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
89059     }
89060     sqlite3ValueFree(pTmp);
89061   }
89062 #endif
89063 }
89064 
89065 /*
89066 ** This routine is called if the collation factory fails to deliver a
89067 ** collation function in the best encoding but there may be other versions
89068 ** of this collation function (for other text encodings) available. Use one
89069 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
89070 ** possible.
89071 */
89072 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
89073   CollSeq *pColl2;
89074   char *z = pColl->zName;
89075   int i;
89076   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
89077   for(i=0; i<3; i++){
89078     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
89079     if( pColl2->xCmp!=0 ){
89080       memcpy(pColl, pColl2, sizeof(CollSeq));
89081       pColl->xDel = 0;         /* Do not copy the destructor */
89082       return SQLITE_OK;
89083     }
89084   }
89085   return SQLITE_ERROR;
89086 }
89087 
89088 /*
89089 ** This function is responsible for invoking the collation factory callback
89090 ** or substituting a collation sequence of a different encoding when the
89091 ** requested collation sequence is not available in the desired encoding.
89092 **
89093 ** If it is not NULL, then pColl must point to the database native encoding
89094 ** collation sequence with name zName, length nName.
89095 **
89096 ** The return value is either the collation sequence to be used in database
89097 ** db for collation type name zName, length nName, or NULL, if no collation
89098 ** sequence can be found.  If no collation is found, leave an error message.
89099 **
89100 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
89101 */
89102 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
89103   Parse *pParse,        /* Parsing context */
89104   u8 enc,               /* The desired encoding for the collating sequence */
89105   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
89106   const char *zName     /* Collating sequence name */
89107 ){
89108   CollSeq *p;
89109   sqlite3 *db = pParse->db;
89110 
89111   p = pColl;
89112   if( !p ){
89113     p = sqlite3FindCollSeq(db, enc, zName, 0);
89114   }
89115   if( !p || !p->xCmp ){
89116     /* No collation sequence of this type for this encoding is registered.
89117     ** Call the collation factory to see if it can supply us with one.
89118     */
89119     callCollNeeded(db, enc, zName);
89120     p = sqlite3FindCollSeq(db, enc, zName, 0);
89121   }
89122   if( p && !p->xCmp && synthCollSeq(db, p) ){
89123     p = 0;
89124   }
89125   assert( !p || p->xCmp );
89126   if( p==0 ){
89127     sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
89128   }
89129   return p;
89130 }
89131 
89132 /*
89133 ** This routine is called on a collation sequence before it is used to
89134 ** check that it is defined. An undefined collation sequence exists when
89135 ** a database is loaded that contains references to collation sequences
89136 ** that have not been defined by sqlite3_create_collation() etc.
89137 **
89138 ** If required, this routine calls the 'collation needed' callback to
89139 ** request a definition of the collating sequence. If this doesn't work,
89140 ** an equivalent collating sequence that uses a text encoding different
89141 ** from the main database is substituted, if one is available.
89142 */
89143 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
89144   if( pColl ){
89145     const char *zName = pColl->zName;
89146     sqlite3 *db = pParse->db;
89147     CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
89148     if( !p ){
89149       return SQLITE_ERROR;
89150     }
89151     assert( p==pColl );
89152   }
89153   return SQLITE_OK;
89154 }
89155 
89156 
89157 
89158 /*
89159 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
89160 ** specified by zName and nName is not found and parameter 'create' is
89161 ** true, then create a new entry. Otherwise return NULL.
89162 **
89163 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
89164 ** array of three CollSeq structures. The first is the collation sequence
89165 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
89166 **
89167 ** Stored immediately after the three collation sequences is a copy of
89168 ** the collation sequence name. A pointer to this string is stored in
89169 ** each collation sequence structure.
89170 */
89171 static CollSeq *findCollSeqEntry(
89172   sqlite3 *db,          /* Database connection */
89173   const char *zName,    /* Name of the collating sequence */
89174   int create            /* Create a new entry if true */
89175 ){
89176   CollSeq *pColl;
89177   int nName = sqlite3Strlen30(zName);
89178   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
89179 
89180   if( 0==pColl && create ){
89181     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
89182     if( pColl ){
89183       CollSeq *pDel = 0;
89184       pColl[0].zName = (char*)&pColl[3];
89185       pColl[0].enc = SQLITE_UTF8;
89186       pColl[1].zName = (char*)&pColl[3];
89187       pColl[1].enc = SQLITE_UTF16LE;
89188       pColl[2].zName = (char*)&pColl[3];
89189       pColl[2].enc = SQLITE_UTF16BE;
89190       memcpy(pColl[0].zName, zName, nName);
89191       pColl[0].zName[nName] = 0;
89192       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
89193 
89194       /* If a malloc() failure occurred in sqlite3HashInsert(), it will
89195       ** return the pColl pointer to be deleted (because it wasn't added
89196       ** to the hash table).
89197       */
89198       assert( pDel==0 || pDel==pColl );
89199       if( pDel!=0 ){
89200         db->mallocFailed = 1;
89201         sqlite3DbFree(db, pDel);
89202         pColl = 0;
89203       }
89204     }
89205   }
89206   return pColl;
89207 }
89208 
89209 /*
89210 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
89211 ** Return the CollSeq* pointer for the collation sequence named zName
89212 ** for the encoding 'enc' from the database 'db'.
89213 **
89214 ** If the entry specified is not found and 'create' is true, then create a
89215 ** new entry.  Otherwise return NULL.
89216 **
89217 ** A separate function sqlite3LocateCollSeq() is a wrapper around
89218 ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
89219 ** if necessary and generates an error message if the collating sequence
89220 ** cannot be found.
89221 **
89222 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
89223 */
89224 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
89225   sqlite3 *db,
89226   u8 enc,
89227   const char *zName,
89228   int create
89229 ){
89230   CollSeq *pColl;
89231   if( zName ){
89232     pColl = findCollSeqEntry(db, zName, create);
89233   }else{
89234     pColl = db->pDfltColl;
89235   }
89236   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
89237   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
89238   if( pColl ) pColl += enc-1;
89239   return pColl;
89240 }
89241 
89242 /* During the search for the best function definition, this procedure
89243 ** is called to test how well the function passed as the first argument
89244 ** matches the request for a function with nArg arguments in a system
89245 ** that uses encoding enc. The value returned indicates how well the
89246 ** request is matched. A higher value indicates a better match.
89247 **
89248 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
89249 ** is also -1.  In other words, we are searching for a function that
89250 ** takes a variable number of arguments.
89251 **
89252 ** If nArg is -2 that means that we are searching for any function
89253 ** regardless of the number of arguments it uses, so return a positive
89254 ** match score for any
89255 **
89256 ** The returned value is always between 0 and 6, as follows:
89257 **
89258 ** 0: Not a match.
89259 ** 1: UTF8/16 conversion required and function takes any number of arguments.
89260 ** 2: UTF16 byte order change required and function takes any number of args.
89261 ** 3: encoding matches and function takes any number of arguments
89262 ** 4: UTF8/16 conversion required - argument count matches exactly
89263 ** 5: UTF16 byte order conversion required - argument count matches exactly
89264 ** 6: Perfect match:  encoding and argument count match exactly.
89265 **
89266 ** If nArg==(-2) then any function with a non-null xStep or xFunc is
89267 ** a perfect match and any function with both xStep and xFunc NULL is
89268 ** a non-match.
89269 */
89270 #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
89271 static int matchQuality(
89272   FuncDef *p,     /* The function we are evaluating for match quality */
89273   int nArg,       /* Desired number of arguments.  (-1)==any */
89274   u8 enc          /* Desired text encoding */
89275 ){
89276   int match;
89277 
89278   /* nArg of -2 is a special case */
89279   if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
89280 
89281   /* Wrong number of arguments means "no match" */
89282   if( p->nArg!=nArg && p->nArg>=0 ) return 0;
89283 
89284   /* Give a better score to a function with a specific number of arguments
89285   ** than to function that accepts any number of arguments. */
89286   if( p->nArg==nArg ){
89287     match = 4;
89288   }else{
89289     match = 1;
89290   }
89291 
89292   /* Bonus points if the text encoding matches */
89293   if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
89294     match += 2;  /* Exact encoding match */
89295   }else if( (enc & p->funcFlags & 2)!=0 ){
89296     match += 1;  /* Both are UTF16, but with different byte orders */
89297   }
89298 
89299   return match;
89300 }
89301 
89302 /*
89303 ** Search a FuncDefHash for a function with the given name.  Return
89304 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
89305 */
89306 static FuncDef *functionSearch(
89307   FuncDefHash *pHash,  /* Hash table to search */
89308   int h,               /* Hash of the name */
89309   const char *zFunc,   /* Name of function */
89310   int nFunc            /* Number of bytes in zFunc */
89311 ){
89312   FuncDef *p;
89313   for(p=pHash->a[h]; p; p=p->pHash){
89314     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
89315       return p;
89316     }
89317   }
89318   return 0;
89319 }
89320 
89321 /*
89322 ** Insert a new FuncDef into a FuncDefHash hash table.
89323 */
89324 SQLITE_PRIVATE void sqlite3FuncDefInsert(
89325   FuncDefHash *pHash,  /* The hash table into which to insert */
89326   FuncDef *pDef        /* The function definition to insert */
89327 ){
89328   FuncDef *pOther;
89329   int nName = sqlite3Strlen30(pDef->zName);
89330   u8 c1 = (u8)pDef->zName[0];
89331   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
89332   pOther = functionSearch(pHash, h, pDef->zName, nName);
89333   if( pOther ){
89334     assert( pOther!=pDef && pOther->pNext!=pDef );
89335     pDef->pNext = pOther->pNext;
89336     pOther->pNext = pDef;
89337   }else{
89338     pDef->pNext = 0;
89339     pDef->pHash = pHash->a[h];
89340     pHash->a[h] = pDef;
89341   }
89342 }
89343 
89344 
89345 
89346 /*
89347 ** Locate a user function given a name, a number of arguments and a flag
89348 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
89349 ** pointer to the FuncDef structure that defines that function, or return
89350 ** NULL if the function does not exist.
89351 **
89352 ** If the createFlag argument is true, then a new (blank) FuncDef
89353 ** structure is created and liked into the "db" structure if a
89354 ** no matching function previously existed.
89355 **
89356 ** If nArg is -2, then the first valid function found is returned.  A
89357 ** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
89358 ** case is used to see if zName is a valid function name for some number
89359 ** of arguments.  If nArg is -2, then createFlag must be 0.
89360 **
89361 ** If createFlag is false, then a function with the required name and
89362 ** number of arguments may be returned even if the eTextRep flag does not
89363 ** match that requested.
89364 */
89365 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
89366   sqlite3 *db,       /* An open database */
89367   const char *zName, /* Name of the function.  Not null-terminated */
89368   int nName,         /* Number of characters in the name */
89369   int nArg,          /* Number of arguments.  -1 means any number */
89370   u8 enc,            /* Preferred text encoding */
89371   u8 createFlag      /* Create new entry if true and does not otherwise exist */
89372 ){
89373   FuncDef *p;         /* Iterator variable */
89374   FuncDef *pBest = 0; /* Best match found so far */
89375   int bestScore = 0;  /* Score of best match */
89376   int h;              /* Hash value */
89377 
89378   assert( nArg>=(-2) );
89379   assert( nArg>=(-1) || createFlag==0 );
89380   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
89381 
89382   /* First search for a match amongst the application-defined functions.
89383   */
89384   p = functionSearch(&db->aFunc, h, zName, nName);
89385   while( p ){
89386     int score = matchQuality(p, nArg, enc);
89387     if( score>bestScore ){
89388       pBest = p;
89389       bestScore = score;
89390     }
89391     p = p->pNext;
89392   }
89393 
89394   /* If no match is found, search the built-in functions.
89395   **
89396   ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
89397   ** functions even if a prior app-defined function was found.  And give
89398   ** priority to built-in functions.
89399   **
89400   ** Except, if createFlag is true, that means that we are trying to
89401   ** install a new function.  Whatever FuncDef structure is returned it will
89402   ** have fields overwritten with new information appropriate for the
89403   ** new function.  But the FuncDefs for built-in functions are read-only.
89404   ** So we must not search for built-ins when creating a new function.
89405   */
89406   if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
89407     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
89408     bestScore = 0;
89409     p = functionSearch(pHash, h, zName, nName);
89410     while( p ){
89411       int score = matchQuality(p, nArg, enc);
89412       if( score>bestScore ){
89413         pBest = p;
89414         bestScore = score;
89415       }
89416       p = p->pNext;
89417     }
89418   }
89419 
89420   /* If the createFlag parameter is true and the search did not reveal an
89421   ** exact match for the name, number of arguments and encoding, then add a
89422   ** new entry to the hash table and return it.
89423   */
89424   if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
89425       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
89426     pBest->zName = (char *)&pBest[1];
89427     pBest->nArg = (u16)nArg;
89428     pBest->funcFlags = enc;
89429     memcpy(pBest->zName, zName, nName);
89430     pBest->zName[nName] = 0;
89431     sqlite3FuncDefInsert(&db->aFunc, pBest);
89432   }
89433 
89434   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
89435     return pBest;
89436   }
89437   return 0;
89438 }
89439 
89440 /*
89441 ** Free all resources held by the schema structure. The void* argument points
89442 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
89443 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
89444 ** of the schema hash tables).
89445 **
89446 ** The Schema.cache_size variable is not cleared.
89447 */
89448 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
89449   Hash temp1;
89450   Hash temp2;
89451   HashElem *pElem;
89452   Schema *pSchema = (Schema *)p;
89453 
89454   temp1 = pSchema->tblHash;
89455   temp2 = pSchema->trigHash;
89456   sqlite3HashInit(&pSchema->trigHash);
89457   sqlite3HashClear(&pSchema->idxHash);
89458   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
89459     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
89460   }
89461   sqlite3HashClear(&temp2);
89462   sqlite3HashInit(&pSchema->tblHash);
89463   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
89464     Table *pTab = sqliteHashData(pElem);
89465     sqlite3DeleteTable(0, pTab);
89466   }
89467   sqlite3HashClear(&temp1);
89468   sqlite3HashClear(&pSchema->fkeyHash);
89469   pSchema->pSeqTab = 0;
89470   if( pSchema->flags & DB_SchemaLoaded ){
89471     pSchema->iGeneration++;
89472     pSchema->flags &= ~DB_SchemaLoaded;
89473   }
89474 }
89475 
89476 /*
89477 ** Find and return the schema associated with a BTree.  Create
89478 ** a new one if necessary.
89479 */
89480 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
89481   Schema * p;
89482   if( pBt ){
89483     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
89484   }else{
89485     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
89486   }
89487   if( !p ){
89488     db->mallocFailed = 1;
89489   }else if ( 0==p->file_format ){
89490     sqlite3HashInit(&p->tblHash);
89491     sqlite3HashInit(&p->idxHash);
89492     sqlite3HashInit(&p->trigHash);
89493     sqlite3HashInit(&p->fkeyHash);
89494     p->enc = SQLITE_UTF8;
89495   }
89496   return p;
89497 }
89498 
89499 /************** End of callback.c ********************************************/
89500 /************** Begin file delete.c ******************************************/
89501 /*
89502 ** 2001 September 15
89503 **
89504 ** The author disclaims copyright to this source code.  In place of
89505 ** a legal notice, here is a blessing:
89506 **
89507 **    May you do good and not evil.
89508 **    May you find forgiveness for yourself and forgive others.
89509 **    May you share freely, never taking more than you give.
89510 **
89511 *************************************************************************
89512 ** This file contains C code routines that are called by the parser
89513 ** in order to generate code for DELETE FROM statements.
89514 */
89515 
89516 /*
89517 ** While a SrcList can in general represent multiple tables and subqueries
89518 ** (as in the FROM clause of a SELECT statement) in this case it contains
89519 ** the name of a single table, as one might find in an INSERT, DELETE,
89520 ** or UPDATE statement.  Look up that table in the symbol table and
89521 ** return a pointer.  Set an error message and return NULL if the table
89522 ** name is not found or if any other error occurs.
89523 **
89524 ** The following fields are initialized appropriate in pSrc:
89525 **
89526 **    pSrc->a[0].pTab       Pointer to the Table object
89527 **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
89528 **
89529 */
89530 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
89531   struct SrcList_item *pItem = pSrc->a;
89532   Table *pTab;
89533   assert( pItem && pSrc->nSrc==1 );
89534   pTab = sqlite3LocateTableItem(pParse, 0, pItem);
89535   sqlite3DeleteTable(pParse->db, pItem->pTab);
89536   pItem->pTab = pTab;
89537   if( pTab ){
89538     pTab->nRef++;
89539   }
89540   if( sqlite3IndexedByLookup(pParse, pItem) ){
89541     pTab = 0;
89542   }
89543   return pTab;
89544 }
89545 
89546 /*
89547 ** Check to make sure the given table is writable.  If it is not
89548 ** writable, generate an error message and return 1.  If it is
89549 ** writable return 0;
89550 */
89551 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
89552   /* A table is not writable under the following circumstances:
89553   **
89554   **   1) It is a virtual table and no implementation of the xUpdate method
89555   **      has been provided, or
89556   **   2) It is a system table (i.e. sqlite_master), this call is not
89557   **      part of a nested parse and writable_schema pragma has not
89558   **      been specified.
89559   **
89560   ** In either case leave an error message in pParse and return non-zero.
89561   */
89562   if( ( IsVirtual(pTab)
89563      && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
89564    || ( (pTab->tabFlags & TF_Readonly)!=0
89565      && (pParse->db->flags & SQLITE_WriteSchema)==0
89566      && pParse->nested==0 )
89567   ){
89568     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
89569     return 1;
89570   }
89571 
89572 #ifndef SQLITE_OMIT_VIEW
89573   if( !viewOk && pTab->pSelect ){
89574     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
89575     return 1;
89576   }
89577 #endif
89578   return 0;
89579 }
89580 
89581 
89582 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
89583 /*
89584 ** Evaluate a view and store its result in an ephemeral table.  The
89585 ** pWhere argument is an optional WHERE clause that restricts the
89586 ** set of rows in the view that are to be added to the ephemeral table.
89587 */
89588 SQLITE_PRIVATE void sqlite3MaterializeView(
89589   Parse *pParse,       /* Parsing context */
89590   Table *pView,        /* View definition */
89591   Expr *pWhere,        /* Optional WHERE clause to be added */
89592   int iCur             /* Cursor number for ephemerial table */
89593 ){
89594   SelectDest dest;
89595   Select *pSel;
89596   SrcList *pFrom;
89597   sqlite3 *db = pParse->db;
89598   int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
89599   pWhere = sqlite3ExprDup(db, pWhere, 0);
89600   pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
89601   if( pFrom ){
89602     assert( pFrom->nSrc==1 );
89603     pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
89604     pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
89605     assert( pFrom->a[0].pOn==0 );
89606     assert( pFrom->a[0].pUsing==0 );
89607   }
89608   pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
89609   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
89610   sqlite3Select(pParse, pSel, &dest);
89611   sqlite3SelectDelete(db, pSel);
89612 }
89613 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
89614 
89615 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
89616 /*
89617 ** Generate an expression tree to implement the WHERE, ORDER BY,
89618 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
89619 **
89620 **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
89621 **                            \__________________________/
89622 **                               pLimitWhere (pInClause)
89623 */
89624 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
89625   Parse *pParse,               /* The parser context */
89626   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
89627   Expr *pWhere,                /* The WHERE clause.  May be null */
89628   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
89629   Expr *pLimit,                /* The LIMIT clause.  May be null */
89630   Expr *pOffset,               /* The OFFSET clause.  May be null */
89631   char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
89632 ){
89633   Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
89634   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
89635   Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
89636   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
89637   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
89638   Select *pSelect = NULL;      /* Complete SELECT tree */
89639 
89640   /* Check that there isn't an ORDER BY without a LIMIT clause.
89641   */
89642   if( pOrderBy && (pLimit == 0) ) {
89643     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
89644     goto limit_where_cleanup_2;
89645   }
89646 
89647   /* We only need to generate a select expression if there
89648   ** is a limit/offset term to enforce.
89649   */
89650   if( pLimit == 0 ) {
89651     /* if pLimit is null, pOffset will always be null as well. */
89652     assert( pOffset == 0 );
89653     return pWhere;
89654   }
89655 
89656   /* Generate a select expression tree to enforce the limit/offset
89657   ** term for the DELETE or UPDATE statement.  For example:
89658   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
89659   ** becomes:
89660   **   DELETE FROM table_a WHERE rowid IN (
89661   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
89662   **   );
89663   */
89664 
89665   pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
89666   if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
89667   pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
89668   if( pEList == 0 ) goto limit_where_cleanup_2;
89669 
89670   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
89671   ** and the SELECT subtree. */
89672   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
89673   if( pSelectSrc == 0 ) {
89674     sqlite3ExprListDelete(pParse->db, pEList);
89675     goto limit_where_cleanup_2;
89676   }
89677 
89678   /* generate the SELECT expression tree. */
89679   pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
89680                              pOrderBy,0,pLimit,pOffset);
89681   if( pSelect == 0 ) return 0;
89682 
89683   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
89684   pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
89685   if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
89686   pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
89687   if( pInClause == 0 ) goto limit_where_cleanup_1;
89688 
89689   pInClause->x.pSelect = pSelect;
89690   pInClause->flags |= EP_xIsSelect;
89691   sqlite3ExprSetHeight(pParse, pInClause);
89692   return pInClause;
89693 
89694   /* something went wrong. clean up anything allocated. */
89695 limit_where_cleanup_1:
89696   sqlite3SelectDelete(pParse->db, pSelect);
89697   return 0;
89698 
89699 limit_where_cleanup_2:
89700   sqlite3ExprDelete(pParse->db, pWhere);
89701   sqlite3ExprListDelete(pParse->db, pOrderBy);
89702   sqlite3ExprDelete(pParse->db, pLimit);
89703   sqlite3ExprDelete(pParse->db, pOffset);
89704   return 0;
89705 }
89706 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
89707        /*      && !defined(SQLITE_OMIT_SUBQUERY) */
89708 
89709 /*
89710 ** Generate code for a DELETE FROM statement.
89711 **
89712 **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
89713 **                 \________/       \________________/
89714 **                  pTabList              pWhere
89715 */
89716 SQLITE_PRIVATE void sqlite3DeleteFrom(
89717   Parse *pParse,         /* The parser context */
89718   SrcList *pTabList,     /* The table from which we should delete things */
89719   Expr *pWhere           /* The WHERE clause.  May be null */
89720 ){
89721   Vdbe *v;               /* The virtual database engine */
89722   Table *pTab;           /* The table from which records will be deleted */
89723   const char *zDb;       /* Name of database holding pTab */
89724   int i;                 /* Loop counter */
89725   WhereInfo *pWInfo;     /* Information about the WHERE clause */
89726   Index *pIdx;           /* For looping over indices of the table */
89727   int iTabCur;           /* Cursor number for the table */
89728   int iDataCur;          /* VDBE cursor for the canonical data source */
89729   int iIdxCur;           /* Cursor number of the first index */
89730   int nIdx;              /* Number of indices */
89731   sqlite3 *db;           /* Main database structure */
89732   AuthContext sContext;  /* Authorization context */
89733   NameContext sNC;       /* Name context to resolve expressions in */
89734   int iDb;               /* Database number */
89735   int memCnt = -1;       /* Memory cell used for change counting */
89736   int rcauth;            /* Value returned by authorization callback */
89737   int okOnePass;         /* True for one-pass algorithm without the FIFO */
89738   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
89739   u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
89740   Index *pPk;            /* The PRIMARY KEY index on the table */
89741   int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
89742   i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
89743   int iKey;              /* Memory cell holding key of row to be deleted */
89744   i16 nKey;              /* Number of memory cells in the row key */
89745   int iEphCur = 0;       /* Ephemeral table holding all primary key values */
89746   int iRowSet = 0;       /* Register for rowset of rows to delete */
89747   int addrBypass = 0;    /* Address of jump over the delete logic */
89748   int addrLoop = 0;      /* Top of the delete loop */
89749   int addrDelete = 0;    /* Jump directly to the delete logic */
89750   int addrEphOpen = 0;   /* Instruction to open the Ephermeral table */
89751 
89752 #ifndef SQLITE_OMIT_TRIGGER
89753   int isView;                  /* True if attempting to delete from a view */
89754   Trigger *pTrigger;           /* List of table triggers, if required */
89755 #endif
89756 
89757   memset(&sContext, 0, sizeof(sContext));
89758   db = pParse->db;
89759   if( pParse->nErr || db->mallocFailed ){
89760     goto delete_from_cleanup;
89761   }
89762   assert( pTabList->nSrc==1 );
89763 
89764   /* Locate the table which we want to delete.  This table has to be
89765   ** put in an SrcList structure because some of the subroutines we
89766   ** will be calling are designed to work with multiple tables and expect
89767   ** an SrcList* parameter instead of just a Table* parameter.
89768   */
89769   pTab = sqlite3SrcListLookup(pParse, pTabList);
89770   if( pTab==0 )  goto delete_from_cleanup;
89771 
89772   /* Figure out if we have any triggers and if the table being
89773   ** deleted from is a view
89774   */
89775 #ifndef SQLITE_OMIT_TRIGGER
89776   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
89777   isView = pTab->pSelect!=0;
89778 #else
89779 # define pTrigger 0
89780 # define isView 0
89781 #endif
89782 #ifdef SQLITE_OMIT_VIEW
89783 # undef isView
89784 # define isView 0
89785 #endif
89786 
89787   /* If pTab is really a view, make sure it has been initialized.
89788   */
89789   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
89790     goto delete_from_cleanup;
89791   }
89792 
89793   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
89794     goto delete_from_cleanup;
89795   }
89796   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
89797   assert( iDb<db->nDb );
89798   zDb = db->aDb[iDb].zName;
89799   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
89800   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
89801   if( rcauth==SQLITE_DENY ){
89802     goto delete_from_cleanup;
89803   }
89804   assert(!isView || pTrigger);
89805 
89806   /* Assign cursor numbers to the table and all its indices.
89807   */
89808   assert( pTabList->nSrc==1 );
89809   iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
89810   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
89811     pParse->nTab++;
89812   }
89813 
89814   /* Start the view context
89815   */
89816   if( isView ){
89817     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
89818   }
89819 
89820   /* Begin generating code.
89821   */
89822   v = sqlite3GetVdbe(pParse);
89823   if( v==0 ){
89824     goto delete_from_cleanup;
89825   }
89826   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
89827   sqlite3BeginWriteOperation(pParse, 1, iDb);
89828 
89829   /* If we are trying to delete from a view, realize that view into
89830   ** a ephemeral table.
89831   */
89832 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
89833   if( isView ){
89834     sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
89835     iDataCur = iIdxCur = iTabCur;
89836   }
89837 #endif
89838 
89839   /* Resolve the column names in the WHERE clause.
89840   */
89841   memset(&sNC, 0, sizeof(sNC));
89842   sNC.pParse = pParse;
89843   sNC.pSrcList = pTabList;
89844   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
89845     goto delete_from_cleanup;
89846   }
89847 
89848   /* Initialize the counter of the number of rows deleted, if
89849   ** we are counting rows.
89850   */
89851   if( db->flags & SQLITE_CountRows ){
89852     memCnt = ++pParse->nMem;
89853     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
89854   }
89855 
89856 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
89857   /* Special case: A DELETE without a WHERE clause deletes everything.
89858   ** It is easier just to erase the whole table. Prior to version 3.6.5,
89859   ** this optimization caused the row change count (the value returned by
89860   ** API function sqlite3_count_changes) to be set incorrectly.  */
89861   if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab)
89862    && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
89863   ){
89864     assert( !isView );
89865     sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
89866     if( HasRowid(pTab) ){
89867       sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
89868                         pTab->zName, P4_STATIC);
89869     }
89870     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
89871       assert( pIdx->pSchema==pTab->pSchema );
89872       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
89873     }
89874   }else
89875 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
89876   {
89877     if( HasRowid(pTab) ){
89878       /* For a rowid table, initialize the RowSet to an empty set */
89879       pPk = 0;
89880       nPk = 1;
89881       iRowSet = ++pParse->nMem;
89882       sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
89883     }else{
89884       /* For a WITHOUT ROWID table, create an ephermeral table used to
89885       ** hold all primary keys for rows to be deleted. */
89886       pPk = sqlite3PrimaryKeyIndex(pTab);
89887       assert( pPk!=0 );
89888       nPk = pPk->nKeyCol;
89889       iPk = pParse->nMem+1;
89890       pParse->nMem += nPk;
89891       iEphCur = pParse->nTab++;
89892       addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
89893       sqlite3VdbeSetP4KeyInfo(pParse, pPk);
89894     }
89895 
89896     /* Construct a query to find the rowid or primary key for every row
89897     ** to be deleted, based on the WHERE clause.
89898     */
89899     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
89900                                WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
89901                                iTabCur+1);
89902     if( pWInfo==0 ) goto delete_from_cleanup;
89903     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
89904 
89905     /* Keep track of the number of rows to be deleted */
89906     if( db->flags & SQLITE_CountRows ){
89907       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89908     }
89909 
89910     /* Extract the rowid or primary key for the current row */
89911     if( pPk ){
89912       for(i=0; i<nPk; i++){
89913         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
89914                                         pPk->aiColumn[i], iPk+i);
89915       }
89916       iKey = iPk;
89917     }else{
89918       iKey = pParse->nMem + 1;
89919       iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
89920       if( iKey>pParse->nMem ) pParse->nMem = iKey;
89921     }
89922 
89923     if( okOnePass ){
89924       /* For ONEPASS, no need to store the rowid/primary-key.  There is only
89925       ** one, so just keep it in its register(s) and fall through to the
89926       ** delete code.
89927       */
89928       nKey = nPk; /* OP_Found will use an unpacked key */
89929       aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
89930       if( aToOpen==0 ){
89931         sqlite3WhereEnd(pWInfo);
89932         goto delete_from_cleanup;
89933       }
89934       memset(aToOpen, 1, nIdx+1);
89935       aToOpen[nIdx+1] = 0;
89936       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
89937       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
89938       if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
89939       addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
89940     }else if( pPk ){
89941       /* Construct a composite key for the row to be deleted and remember it */
89942       iKey = ++pParse->nMem;
89943       nKey = 0;   /* Zero tells OP_Found to use a composite key */
89944       sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
89945                         sqlite3IndexAffinityStr(v, pPk), nPk);
89946       sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
89947     }else{
89948       /* Get the rowid of the row to be deleted and remember it in the RowSet */
89949       nKey = 1;  /* OP_Seek always uses a single rowid */
89950       sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
89951     }
89952 
89953     /* End of the WHERE loop */
89954     sqlite3WhereEnd(pWInfo);
89955     if( okOnePass ){
89956       /* Bypass the delete logic below if the WHERE loop found zero rows */
89957       addrBypass = sqlite3VdbeMakeLabel(v);
89958       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
89959       sqlite3VdbeJumpHere(v, addrDelete);
89960     }
89961 
89962     /* Unless this is a view, open cursors for the table we are
89963     ** deleting from and all its indices. If this is a view, then the
89964     ** only effect this statement has is to fire the INSTEAD OF
89965     ** triggers.
89966     */
89967     if( !isView ){
89968       sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
89969                                  &iDataCur, &iIdxCur);
89970       assert( pPk || iDataCur==iTabCur );
89971       assert( pPk || iIdxCur==iDataCur+1 );
89972     }
89973 
89974     /* Set up a loop over the rowids/primary-keys that were found in the
89975     ** where-clause loop above.
89976     */
89977     if( okOnePass ){
89978       /* Just one row.  Hence the top-of-loop is a no-op */
89979       assert( nKey==nPk ); /* OP_Found will use an unpacked key */
89980       if( aToOpen[iDataCur-iTabCur] ){
89981         assert( pPk!=0 );
89982         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
89983         VdbeCoverage(v);
89984       }
89985     }else if( pPk ){
89986       addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur); VdbeCoverage(v);
89987       sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
89988       assert( nKey==0 );  /* OP_Found will use a composite key */
89989     }else{
89990       addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
89991       VdbeCoverage(v);
89992       assert( nKey==1 );
89993     }
89994 
89995     /* Delete the row */
89996 #ifndef SQLITE_OMIT_VIRTUALTABLE
89997     if( IsVirtual(pTab) ){
89998       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
89999       sqlite3VtabMakeWritable(pParse, pTab);
90000       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
90001       sqlite3VdbeChangeP5(v, OE_Abort);
90002       sqlite3MayAbort(pParse);
90003     }else
90004 #endif
90005     {
90006       int count = (pParse->nested==0);    /* True to count changes */
90007       sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
90008                                iKey, nKey, count, OE_Default, okOnePass);
90009     }
90010 
90011     /* End of the loop over all rowids/primary-keys. */
90012     if( okOnePass ){
90013       sqlite3VdbeResolveLabel(v, addrBypass);
90014     }else if( pPk ){
90015       sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1); VdbeCoverage(v);
90016       sqlite3VdbeJumpHere(v, addrLoop);
90017     }else{
90018       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
90019       sqlite3VdbeJumpHere(v, addrLoop);
90020     }
90021 
90022     /* Close the cursors open on the table and its indexes. */
90023     if( !isView && !IsVirtual(pTab) ){
90024       if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
90025       for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
90026         sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
90027       }
90028     }
90029   } /* End non-truncate path */
90030 
90031   /* Update the sqlite_sequence table by storing the content of the
90032   ** maximum rowid counter values recorded while inserting into
90033   ** autoincrement tables.
90034   */
90035   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
90036     sqlite3AutoincrementEnd(pParse);
90037   }
90038 
90039   /* Return the number of rows that were deleted. If this routine is
90040   ** generating code because of a call to sqlite3NestedParse(), do not
90041   ** invoke the callback function.
90042   */
90043   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
90044     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
90045     sqlite3VdbeSetNumCols(v, 1);
90046     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
90047   }
90048 
90049 delete_from_cleanup:
90050   sqlite3AuthContextPop(&sContext);
90051   sqlite3SrcListDelete(db, pTabList);
90052   sqlite3ExprDelete(db, pWhere);
90053   sqlite3DbFree(db, aToOpen);
90054   return;
90055 }
90056 /* Make sure "isView" and other macros defined above are undefined. Otherwise
90057 ** thely may interfere with compilation of other functions in this file
90058 ** (or in another file, if this file becomes part of the amalgamation).  */
90059 #ifdef isView
90060  #undef isView
90061 #endif
90062 #ifdef pTrigger
90063  #undef pTrigger
90064 #endif
90065 
90066 /*
90067 ** This routine generates VDBE code that causes a single row of a
90068 ** single table to be deleted.  Both the original table entry and
90069 ** all indices are removed.
90070 **
90071 ** Preconditions:
90072 **
90073 **   1.  iDataCur is an open cursor on the btree that is the canonical data
90074 **       store for the table.  (This will be either the table itself,
90075 **       in the case of a rowid table, or the PRIMARY KEY index in the case
90076 **       of a WITHOUT ROWID table.)
90077 **
90078 **   2.  Read/write cursors for all indices of pTab must be open as
90079 **       cursor number iIdxCur+i for the i-th index.
90080 **
90081 **   3.  The primary key for the row to be deleted must be stored in a
90082 **       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
90083 **       that a search record formed from OP_MakeRecord is contained in the
90084 **       single memory location iPk.
90085 */
90086 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
90087   Parse *pParse,     /* Parsing context */
90088   Table *pTab,       /* Table containing the row to be deleted */
90089   Trigger *pTrigger, /* List of triggers to (potentially) fire */
90090   int iDataCur,      /* Cursor from which column data is extracted */
90091   int iIdxCur,       /* First index cursor */
90092   int iPk,           /* First memory cell containing the PRIMARY KEY */
90093   i16 nPk,           /* Number of PRIMARY KEY memory cells */
90094   u8 count,          /* If non-zero, increment the row change counter */
90095   u8 onconf,         /* Default ON CONFLICT policy for triggers */
90096   u8 bNoSeek         /* iDataCur is already pointing to the row to delete */
90097 ){
90098   Vdbe *v = pParse->pVdbe;        /* Vdbe */
90099   int iOld = 0;                   /* First register in OLD.* array */
90100   int iLabel;                     /* Label resolved to end of generated code */
90101   u8 opSeek;                      /* Seek opcode */
90102 
90103   /* Vdbe is guaranteed to have been allocated by this stage. */
90104   assert( v );
90105   VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
90106                          iDataCur, iIdxCur, iPk, (int)nPk));
90107 
90108   /* Seek cursor iCur to the row to delete. If this row no longer exists
90109   ** (this can happen if a trigger program has already deleted it), do
90110   ** not attempt to delete it or fire any DELETE triggers.  */
90111   iLabel = sqlite3VdbeMakeLabel(v);
90112   opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
90113   if( !bNoSeek ){
90114     sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
90115     VdbeCoverageIf(v, opSeek==OP_NotExists);
90116     VdbeCoverageIf(v, opSeek==OP_NotFound);
90117   }
90118 
90119   /* If there are any triggers to fire, allocate a range of registers to
90120   ** use for the old.* references in the triggers.  */
90121   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
90122     u32 mask;                     /* Mask of OLD.* columns in use */
90123     int iCol;                     /* Iterator used while populating OLD.* */
90124     int addrStart;                /* Start of BEFORE trigger programs */
90125 
90126     /* TODO: Could use temporary registers here. Also could attempt to
90127     ** avoid copying the contents of the rowid register.  */
90128     mask = sqlite3TriggerColmask(
90129         pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
90130     );
90131     mask |= sqlite3FkOldmask(pParse, pTab);
90132     iOld = pParse->nMem+1;
90133     pParse->nMem += (1 + pTab->nCol);
90134 
90135     /* Populate the OLD.* pseudo-table register array. These values will be
90136     ** used by any BEFORE and AFTER triggers that exist.  */
90137     sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
90138     for(iCol=0; iCol<pTab->nCol; iCol++){
90139       testcase( mask!=0xffffffff && iCol==31 );
90140       testcase( mask!=0xffffffff && iCol==32 );
90141       if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
90142         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
90143       }
90144     }
90145 
90146     /* Invoke BEFORE DELETE trigger programs. */
90147     addrStart = sqlite3VdbeCurrentAddr(v);
90148     sqlite3CodeRowTrigger(pParse, pTrigger,
90149         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
90150     );
90151 
90152     /* If any BEFORE triggers were coded, then seek the cursor to the
90153     ** row to be deleted again. It may be that the BEFORE triggers moved
90154     ** the cursor or of already deleted the row that the cursor was
90155     ** pointing to.
90156     */
90157     if( addrStart<sqlite3VdbeCurrentAddr(v) ){
90158       sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
90159       VdbeCoverageIf(v, opSeek==OP_NotExists);
90160       VdbeCoverageIf(v, opSeek==OP_NotFound);
90161     }
90162 
90163     /* Do FK processing. This call checks that any FK constraints that
90164     ** refer to this table (i.e. constraints attached to other tables)
90165     ** are not violated by deleting this row.  */
90166     sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
90167   }
90168 
90169   /* Delete the index and table entries. Skip this step if pTab is really
90170   ** a view (in which case the only effect of the DELETE statement is to
90171   ** fire the INSTEAD OF triggers).  */
90172   if( pTab->pSelect==0 ){
90173     sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
90174     sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
90175     if( count ){
90176       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
90177     }
90178   }
90179 
90180   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
90181   ** handle rows (possibly in other tables) that refer via a foreign key
90182   ** to the row just deleted. */
90183   sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
90184 
90185   /* Invoke AFTER DELETE trigger programs. */
90186   sqlite3CodeRowTrigger(pParse, pTrigger,
90187       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
90188   );
90189 
90190   /* Jump here if the row had already been deleted before any BEFORE
90191   ** trigger programs were invoked. Or if a trigger program throws a
90192   ** RAISE(IGNORE) exception.  */
90193   sqlite3VdbeResolveLabel(v, iLabel);
90194   VdbeModuleComment((v, "END: GenRowDel()"));
90195 }
90196 
90197 /*
90198 ** This routine generates VDBE code that causes the deletion of all
90199 ** index entries associated with a single row of a single table, pTab
90200 **
90201 ** Preconditions:
90202 **
90203 **   1.  A read/write cursor "iDataCur" must be open on the canonical storage
90204 **       btree for the table pTab.  (This will be either the table itself
90205 **       for rowid tables or to the primary key index for WITHOUT ROWID
90206 **       tables.)
90207 **
90208 **   2.  Read/write cursors for all indices of pTab must be open as
90209 **       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
90210 **       index is the 0-th index.)
90211 **
90212 **   3.  The "iDataCur" cursor must be already be positioned on the row
90213 **       that is to be deleted.
90214 */
90215 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
90216   Parse *pParse,     /* Parsing and code generating context */
90217   Table *pTab,       /* Table containing the row to be deleted */
90218   int iDataCur,      /* Cursor of table holding data. */
90219   int iIdxCur,       /* First index cursor */
90220   int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
90221 ){
90222   int i;             /* Index loop counter */
90223   int r1 = -1;       /* Register holding an index key */
90224   int iPartIdxLabel; /* Jump destination for skipping partial index entries */
90225   Index *pIdx;       /* Current index */
90226   Index *pPrior = 0; /* Prior index */
90227   Vdbe *v;           /* The prepared statement under construction */
90228   Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
90229 
90230   v = pParse->pVdbe;
90231   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
90232   for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
90233     assert( iIdxCur+i!=iDataCur || pPk==pIdx );
90234     if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
90235     if( pIdx==pPk ) continue;
90236     VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
90237     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
90238                                  &iPartIdxLabel, pPrior, r1);
90239     sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
90240                       pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
90241     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
90242     pPrior = pIdx;
90243   }
90244 }
90245 
90246 /*
90247 ** Generate code that will assemble an index key and stores it in register
90248 ** regOut.  The key with be for index pIdx which is an index on pTab.
90249 ** iCur is the index of a cursor open on the pTab table and pointing to
90250 ** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
90251 ** iCur must be the cursor of the PRIMARY KEY index.
90252 **
90253 ** Return a register number which is the first in a block of
90254 ** registers that holds the elements of the index key.  The
90255 ** block of registers has already been deallocated by the time
90256 ** this routine returns.
90257 **
90258 ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
90259 ** to that label if pIdx is a partial index that should be skipped.
90260 ** A partial index should be skipped if its WHERE clause evaluates
90261 ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
90262 ** will be set to zero which is an empty label that is ignored by
90263 ** sqlite3VdbeResolveLabel().
90264 **
90265 ** The pPrior and regPrior parameters are used to implement a cache to
90266 ** avoid unnecessary register loads.  If pPrior is not NULL, then it is
90267 ** a pointer to a different index for which an index key has just been
90268 ** computed into register regPrior.  If the current pIdx index is generating
90269 ** its key into the same sequence of registers and if pPrior and pIdx share
90270 ** a column in common, then the register corresponding to that column already
90271 ** holds the correct value and the loading of that register is skipped.
90272 ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
90273 ** on a table with multiple indices, and especially with the ROWID or
90274 ** PRIMARY KEY columns of the index.
90275 */
90276 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
90277   Parse *pParse,       /* Parsing context */
90278   Index *pIdx,         /* The index for which to generate a key */
90279   int iDataCur,        /* Cursor number from which to take column data */
90280   int regOut,          /* Put the new key into this register if not 0 */
90281   int prefixOnly,      /* Compute only a unique prefix of the key */
90282   int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
90283   Index *pPrior,       /* Previously generated index key */
90284   int regPrior         /* Register holding previous generated key */
90285 ){
90286   Vdbe *v = pParse->pVdbe;
90287   int j;
90288   Table *pTab = pIdx->pTable;
90289   int regBase;
90290   int nCol;
90291 
90292   if( piPartIdxLabel ){
90293     if( pIdx->pPartIdxWhere ){
90294       *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
90295       pParse->iPartIdxTab = iDataCur;
90296       sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
90297                          SQLITE_JUMPIFNULL);
90298     }else{
90299       *piPartIdxLabel = 0;
90300     }
90301   }
90302   nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
90303   regBase = sqlite3GetTempRange(pParse, nCol);
90304   if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
90305   for(j=0; j<nCol; j++){
90306     if( pPrior && pPrior->aiColumn[j]==pIdx->aiColumn[j] ) continue;
90307     sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pIdx->aiColumn[j],
90308                                     regBase+j);
90309     /* If the column affinity is REAL but the number is an integer, then it
90310     ** might be stored in the table as an integer (using a compact
90311     ** representation) then converted to REAL by an OP_RealAffinity opcode.
90312     ** But we are getting ready to store this value back into an index, where
90313     ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
90314     ** opcode if it is present */
90315     sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
90316   }
90317   if( regOut ){
90318     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
90319   }
90320   sqlite3ReleaseTempRange(pParse, regBase, nCol);
90321   return regBase;
90322 }
90323 
90324 /************** End of delete.c **********************************************/
90325 /************** Begin file func.c ********************************************/
90326 /*
90327 ** 2002 February 23
90328 **
90329 ** The author disclaims copyright to this source code.  In place of
90330 ** a legal notice, here is a blessing:
90331 **
90332 **    May you do good and not evil.
90333 **    May you find forgiveness for yourself and forgive others.
90334 **    May you share freely, never taking more than you give.
90335 **
90336 *************************************************************************
90337 ** This file contains the C functions that implement various SQL
90338 ** functions of SQLite.
90339 **
90340 ** There is only one exported symbol in this file - the function
90341 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
90342 ** All other code has file scope.
90343 */
90344 /* #include <stdlib.h> */
90345 /* #include <assert.h> */
90346 
90347 /*
90348 ** Return the collating function associated with a function.
90349 */
90350 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
90351   return context->pColl;
90352 }
90353 
90354 /*
90355 ** Indicate that the accumulator load should be skipped on this
90356 ** iteration of the aggregate loop.
90357 */
90358 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
90359   context->skipFlag = 1;
90360 }
90361 
90362 /*
90363 ** Implementation of the non-aggregate min() and max() functions
90364 */
90365 static void minmaxFunc(
90366   sqlite3_context *context,
90367   int argc,
90368   sqlite3_value **argv
90369 ){
90370   int i;
90371   int mask;    /* 0 for min() or 0xffffffff for max() */
90372   int iBest;
90373   CollSeq *pColl;
90374 
90375   assert( argc>1 );
90376   mask = sqlite3_user_data(context)==0 ? 0 : -1;
90377   pColl = sqlite3GetFuncCollSeq(context);
90378   assert( pColl );
90379   assert( mask==-1 || mask==0 );
90380   iBest = 0;
90381   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
90382   for(i=1; i<argc; i++){
90383     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
90384     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
90385       testcase( mask==0 );
90386       iBest = i;
90387     }
90388   }
90389   sqlite3_result_value(context, argv[iBest]);
90390 }
90391 
90392 /*
90393 ** Return the type of the argument.
90394 */
90395 static void typeofFunc(
90396   sqlite3_context *context,
90397   int NotUsed,
90398   sqlite3_value **argv
90399 ){
90400   const char *z = 0;
90401   UNUSED_PARAMETER(NotUsed);
90402   switch( sqlite3_value_type(argv[0]) ){
90403     case SQLITE_INTEGER: z = "integer"; break;
90404     case SQLITE_TEXT:    z = "text";    break;
90405     case SQLITE_FLOAT:   z = "real";    break;
90406     case SQLITE_BLOB:    z = "blob";    break;
90407     default:             z = "null";    break;
90408   }
90409   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
90410 }
90411 
90412 
90413 /*
90414 ** Implementation of the length() function
90415 */
90416 static void lengthFunc(
90417   sqlite3_context *context,
90418   int argc,
90419   sqlite3_value **argv
90420 ){
90421   int len;
90422 
90423   assert( argc==1 );
90424   UNUSED_PARAMETER(argc);
90425   switch( sqlite3_value_type(argv[0]) ){
90426     case SQLITE_BLOB:
90427     case SQLITE_INTEGER:
90428     case SQLITE_FLOAT: {
90429       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
90430       break;
90431     }
90432     case SQLITE_TEXT: {
90433       const unsigned char *z = sqlite3_value_text(argv[0]);
90434       if( z==0 ) return;
90435       len = 0;
90436       while( *z ){
90437         len++;
90438         SQLITE_SKIP_UTF8(z);
90439       }
90440       sqlite3_result_int(context, len);
90441       break;
90442     }
90443     default: {
90444       sqlite3_result_null(context);
90445       break;
90446     }
90447   }
90448 }
90449 
90450 /*
90451 ** Implementation of the abs() function.
90452 **
90453 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
90454 ** the numeric argument X.
90455 */
90456 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90457   assert( argc==1 );
90458   UNUSED_PARAMETER(argc);
90459   switch( sqlite3_value_type(argv[0]) ){
90460     case SQLITE_INTEGER: {
90461       i64 iVal = sqlite3_value_int64(argv[0]);
90462       if( iVal<0 ){
90463         if( iVal==SMALLEST_INT64 ){
90464           /* IMP: R-31676-45509 If X is the integer -9223372036854775808
90465           ** then abs(X) throws an integer overflow error since there is no
90466           ** equivalent positive 64-bit two complement value. */
90467           sqlite3_result_error(context, "integer overflow", -1);
90468           return;
90469         }
90470         iVal = -iVal;
90471       }
90472       sqlite3_result_int64(context, iVal);
90473       break;
90474     }
90475     case SQLITE_NULL: {
90476       /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
90477       sqlite3_result_null(context);
90478       break;
90479     }
90480     default: {
90481       /* Because sqlite3_value_double() returns 0.0 if the argument is not
90482       ** something that can be converted into a number, we have:
90483       ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
90484       ** cannot be converted to a numeric value.
90485       */
90486       double rVal = sqlite3_value_double(argv[0]);
90487       if( rVal<0 ) rVal = -rVal;
90488       sqlite3_result_double(context, rVal);
90489       break;
90490     }
90491   }
90492 }
90493 
90494 /*
90495 ** Implementation of the instr() function.
90496 **
90497 ** instr(haystack,needle) finds the first occurrence of needle
90498 ** in haystack and returns the number of previous characters plus 1,
90499 ** or 0 if needle does not occur within haystack.
90500 **
90501 ** If both haystack and needle are BLOBs, then the result is one more than
90502 ** the number of bytes in haystack prior to the first occurrence of needle,
90503 ** or 0 if needle never occurs in haystack.
90504 */
90505 static void instrFunc(
90506   sqlite3_context *context,
90507   int argc,
90508   sqlite3_value **argv
90509 ){
90510   const unsigned char *zHaystack;
90511   const unsigned char *zNeedle;
90512   int nHaystack;
90513   int nNeedle;
90514   int typeHaystack, typeNeedle;
90515   int N = 1;
90516   int isText;
90517 
90518   UNUSED_PARAMETER(argc);
90519   typeHaystack = sqlite3_value_type(argv[0]);
90520   typeNeedle = sqlite3_value_type(argv[1]);
90521   if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
90522   nHaystack = sqlite3_value_bytes(argv[0]);
90523   nNeedle = sqlite3_value_bytes(argv[1]);
90524   if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
90525     zHaystack = sqlite3_value_blob(argv[0]);
90526     zNeedle = sqlite3_value_blob(argv[1]);
90527     isText = 0;
90528   }else{
90529     zHaystack = sqlite3_value_text(argv[0]);
90530     zNeedle = sqlite3_value_text(argv[1]);
90531     isText = 1;
90532   }
90533   while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
90534     N++;
90535     do{
90536       nHaystack--;
90537       zHaystack++;
90538     }while( isText && (zHaystack[0]&0xc0)==0x80 );
90539   }
90540   if( nNeedle>nHaystack ) N = 0;
90541   sqlite3_result_int(context, N);
90542 }
90543 
90544 /*
90545 ** Implementation of the printf() function.
90546 */
90547 static void printfFunc(
90548   sqlite3_context *context,
90549   int argc,
90550   sqlite3_value **argv
90551 ){
90552   PrintfArguments x;
90553   StrAccum str;
90554   const char *zFormat;
90555   int n;
90556 
90557   if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
90558     x.nArg = argc-1;
90559     x.nUsed = 0;
90560     x.apArg = argv+1;
90561     sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH);
90562     str.db = sqlite3_context_db_handle(context);
90563     sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
90564     n = str.nChar;
90565     sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
90566                         SQLITE_DYNAMIC);
90567   }
90568 }
90569 
90570 /*
90571 ** Implementation of the substr() function.
90572 **
90573 ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
90574 ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
90575 ** of x.  If x is text, then we actually count UTF-8 characters.
90576 ** If x is a blob, then we count bytes.
90577 **
90578 ** If p1 is negative, then we begin abs(p1) from the end of x[].
90579 **
90580 ** If p2 is negative, return the p2 characters preceding p1.
90581 */
90582 static void substrFunc(
90583   sqlite3_context *context,
90584   int argc,
90585   sqlite3_value **argv
90586 ){
90587   const unsigned char *z;
90588   const unsigned char *z2;
90589   int len;
90590   int p0type;
90591   i64 p1, p2;
90592   int negP2 = 0;
90593 
90594   assert( argc==3 || argc==2 );
90595   if( sqlite3_value_type(argv[1])==SQLITE_NULL
90596    || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
90597   ){
90598     return;
90599   }
90600   p0type = sqlite3_value_type(argv[0]);
90601   p1 = sqlite3_value_int(argv[1]);
90602   if( p0type==SQLITE_BLOB ){
90603     len = sqlite3_value_bytes(argv[0]);
90604     z = sqlite3_value_blob(argv[0]);
90605     if( z==0 ) return;
90606     assert( len==sqlite3_value_bytes(argv[0]) );
90607   }else{
90608     z = sqlite3_value_text(argv[0]);
90609     if( z==0 ) return;
90610     len = 0;
90611     if( p1<0 ){
90612       for(z2=z; *z2; len++){
90613         SQLITE_SKIP_UTF8(z2);
90614       }
90615     }
90616   }
90617   if( argc==3 ){
90618     p2 = sqlite3_value_int(argv[2]);
90619     if( p2<0 ){
90620       p2 = -p2;
90621       negP2 = 1;
90622     }
90623   }else{
90624     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
90625   }
90626   if( p1<0 ){
90627     p1 += len;
90628     if( p1<0 ){
90629       p2 += p1;
90630       if( p2<0 ) p2 = 0;
90631       p1 = 0;
90632     }
90633   }else if( p1>0 ){
90634     p1--;
90635   }else if( p2>0 ){
90636     p2--;
90637   }
90638   if( negP2 ){
90639     p1 -= p2;
90640     if( p1<0 ){
90641       p2 += p1;
90642       p1 = 0;
90643     }
90644   }
90645   assert( p1>=0 && p2>=0 );
90646   if( p0type!=SQLITE_BLOB ){
90647     while( *z && p1 ){
90648       SQLITE_SKIP_UTF8(z);
90649       p1--;
90650     }
90651     for(z2=z; *z2 && p2; p2--){
90652       SQLITE_SKIP_UTF8(z2);
90653     }
90654     sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
90655   }else{
90656     if( p1+p2>len ){
90657       p2 = len-p1;
90658       if( p2<0 ) p2 = 0;
90659     }
90660     sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
90661   }
90662 }
90663 
90664 /*
90665 ** Implementation of the round() function
90666 */
90667 #ifndef SQLITE_OMIT_FLOATING_POINT
90668 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90669   int n = 0;
90670   double r;
90671   char *zBuf;
90672   assert( argc==1 || argc==2 );
90673   if( argc==2 ){
90674     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
90675     n = sqlite3_value_int(argv[1]);
90676     if( n>30 ) n = 30;
90677     if( n<0 ) n = 0;
90678   }
90679   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
90680   r = sqlite3_value_double(argv[0]);
90681   /* If Y==0 and X will fit in a 64-bit int,
90682   ** handle the rounding directly,
90683   ** otherwise use printf.
90684   */
90685   if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
90686     r = (double)((sqlite_int64)(r+0.5));
90687   }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
90688     r = -(double)((sqlite_int64)((-r)+0.5));
90689   }else{
90690     zBuf = sqlite3_mprintf("%.*f",n,r);
90691     if( zBuf==0 ){
90692       sqlite3_result_error_nomem(context);
90693       return;
90694     }
90695     sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
90696     sqlite3_free(zBuf);
90697   }
90698   sqlite3_result_double(context, r);
90699 }
90700 #endif
90701 
90702 /*
90703 ** Allocate nByte bytes of space using sqlite3_malloc(). If the
90704 ** allocation fails, call sqlite3_result_error_nomem() to notify
90705 ** the database handle that malloc() has failed and return NULL.
90706 ** If nByte is larger than the maximum string or blob length, then
90707 ** raise an SQLITE_TOOBIG exception and return NULL.
90708 */
90709 static void *contextMalloc(sqlite3_context *context, i64 nByte){
90710   char *z;
90711   sqlite3 *db = sqlite3_context_db_handle(context);
90712   assert( nByte>0 );
90713   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
90714   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
90715   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
90716     sqlite3_result_error_toobig(context);
90717     z = 0;
90718   }else{
90719     z = sqlite3Malloc((int)nByte);
90720     if( !z ){
90721       sqlite3_result_error_nomem(context);
90722     }
90723   }
90724   return z;
90725 }
90726 
90727 /*
90728 ** Implementation of the upper() and lower() SQL functions.
90729 */
90730 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90731   char *z1;
90732   const char *z2;
90733   int i, n;
90734   UNUSED_PARAMETER(argc);
90735   z2 = (char*)sqlite3_value_text(argv[0]);
90736   n = sqlite3_value_bytes(argv[0]);
90737   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
90738   assert( z2==(char*)sqlite3_value_text(argv[0]) );
90739   if( z2 ){
90740     z1 = contextMalloc(context, ((i64)n)+1);
90741     if( z1 ){
90742       for(i=0; i<n; i++){
90743         z1[i] = (char)sqlite3Toupper(z2[i]);
90744       }
90745       sqlite3_result_text(context, z1, n, sqlite3_free);
90746     }
90747   }
90748 }
90749 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90750   char *z1;
90751   const char *z2;
90752   int i, n;
90753   UNUSED_PARAMETER(argc);
90754   z2 = (char*)sqlite3_value_text(argv[0]);
90755   n = sqlite3_value_bytes(argv[0]);
90756   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
90757   assert( z2==(char*)sqlite3_value_text(argv[0]) );
90758   if( z2 ){
90759     z1 = contextMalloc(context, ((i64)n)+1);
90760     if( z1 ){
90761       for(i=0; i<n; i++){
90762         z1[i] = sqlite3Tolower(z2[i]);
90763       }
90764       sqlite3_result_text(context, z1, n, sqlite3_free);
90765     }
90766   }
90767 }
90768 
90769 /*
90770 ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
90771 ** as VDBE code so that unused argument values do not have to be computed.
90772 ** However, we still need some kind of function implementation for this
90773 ** routines in the function table.  The noopFunc macro provides this.
90774 ** noopFunc will never be called so it doesn't matter what the implementation
90775 ** is.  We might as well use the "version()" function as a substitute.
90776 */
90777 #define noopFunc versionFunc   /* Substitute function - never called */
90778 
90779 /*
90780 ** Implementation of random().  Return a random integer.
90781 */
90782 static void randomFunc(
90783   sqlite3_context *context,
90784   int NotUsed,
90785   sqlite3_value **NotUsed2
90786 ){
90787   sqlite_int64 r;
90788   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90789   sqlite3_randomness(sizeof(r), &r);
90790   if( r<0 ){
90791     /* We need to prevent a random number of 0x8000000000000000
90792     ** (or -9223372036854775808) since when you do abs() of that
90793     ** number of you get the same value back again.  To do this
90794     ** in a way that is testable, mask the sign bit off of negative
90795     ** values, resulting in a positive value.  Then take the
90796     ** 2s complement of that positive value.  The end result can
90797     ** therefore be no less than -9223372036854775807.
90798     */
90799     r = -(r & LARGEST_INT64);
90800   }
90801   sqlite3_result_int64(context, r);
90802 }
90803 
90804 /*
90805 ** Implementation of randomblob(N).  Return a random blob
90806 ** that is N bytes long.
90807 */
90808 static void randomBlob(
90809   sqlite3_context *context,
90810   int argc,
90811   sqlite3_value **argv
90812 ){
90813   int n;
90814   unsigned char *p;
90815   assert( argc==1 );
90816   UNUSED_PARAMETER(argc);
90817   n = sqlite3_value_int(argv[0]);
90818   if( n<1 ){
90819     n = 1;
90820   }
90821   p = contextMalloc(context, n);
90822   if( p ){
90823     sqlite3_randomness(n, p);
90824     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
90825   }
90826 }
90827 
90828 /*
90829 ** Implementation of the last_insert_rowid() SQL function.  The return
90830 ** value is the same as the sqlite3_last_insert_rowid() API function.
90831 */
90832 static void last_insert_rowid(
90833   sqlite3_context *context,
90834   int NotUsed,
90835   sqlite3_value **NotUsed2
90836 ){
90837   sqlite3 *db = sqlite3_context_db_handle(context);
90838   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90839   /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
90840   ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
90841   ** function. */
90842   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
90843 }
90844 
90845 /*
90846 ** Implementation of the changes() SQL function.
90847 **
90848 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
90849 ** around the sqlite3_changes() C/C++ function and hence follows the same
90850 ** rules for counting changes.
90851 */
90852 static void changes(
90853   sqlite3_context *context,
90854   int NotUsed,
90855   sqlite3_value **NotUsed2
90856 ){
90857   sqlite3 *db = sqlite3_context_db_handle(context);
90858   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90859   sqlite3_result_int(context, sqlite3_changes(db));
90860 }
90861 
90862 /*
90863 ** Implementation of the total_changes() SQL function.  The return value is
90864 ** the same as the sqlite3_total_changes() API function.
90865 */
90866 static void total_changes(
90867   sqlite3_context *context,
90868   int NotUsed,
90869   sqlite3_value **NotUsed2
90870 ){
90871   sqlite3 *db = sqlite3_context_db_handle(context);
90872   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90873   /* IMP: R-52756-41993 This function is a wrapper around the
90874   ** sqlite3_total_changes() C/C++ interface. */
90875   sqlite3_result_int(context, sqlite3_total_changes(db));
90876 }
90877 
90878 /*
90879 ** A structure defining how to do GLOB-style comparisons.
90880 */
90881 struct compareInfo {
90882   u8 matchAll;
90883   u8 matchOne;
90884   u8 matchSet;
90885   u8 noCase;
90886 };
90887 
90888 /*
90889 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
90890 ** character is exactly one byte in size.  Also, all characters are
90891 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
90892 ** whereas only characters less than 0x80 do in ASCII.
90893 */
90894 #if defined(SQLITE_EBCDIC)
90895 # define sqlite3Utf8Read(A)    (*((*A)++))
90896 # define GlobUpperToLower(A)   A = sqlite3UpperToLower[A]
90897 #else
90898 # define GlobUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
90899 #endif
90900 
90901 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
90902 /* The correct SQL-92 behavior is for the LIKE operator to ignore
90903 ** case.  Thus  'a' LIKE 'A' would be true. */
90904 static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
90905 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
90906 ** is case sensitive causing 'a' LIKE 'A' to be false */
90907 static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
90908 
90909 /*
90910 ** Compare two UTF-8 strings for equality where the first string can
90911 ** potentially be a "glob" expression.  Return true (1) if they
90912 ** are the same and false (0) if they are different.
90913 **
90914 ** Globbing rules:
90915 **
90916 **      '*'       Matches any sequence of zero or more characters.
90917 **
90918 **      '?'       Matches exactly one character.
90919 **
90920 **     [...]      Matches one character from the enclosed list of
90921 **                characters.
90922 **
90923 **     [^...]     Matches one character not in the enclosed list.
90924 **
90925 ** With the [...] and [^...] matching, a ']' character can be included
90926 ** in the list by making it the first character after '[' or '^'.  A
90927 ** range of characters can be specified using '-'.  Example:
90928 ** "[a-z]" matches any single lower-case letter.  To match a '-', make
90929 ** it the last character in the list.
90930 **
90931 ** This routine is usually quick, but can be N**2 in the worst case.
90932 **
90933 ** Hints: to match '*' or '?', put them in "[]".  Like this:
90934 **
90935 **         abc[*]xyz        Matches "abc*xyz" only
90936 */
90937 static int patternCompare(
90938   const u8 *zPattern,              /* The glob pattern */
90939   const u8 *zString,               /* The string to compare against the glob */
90940   const struct compareInfo *pInfo, /* Information about how to do the compare */
90941   u32 esc                          /* The escape character */
90942 ){
90943   u32 c, c2;
90944   int invert;
90945   int seen;
90946   u8 matchOne = pInfo->matchOne;
90947   u8 matchAll = pInfo->matchAll;
90948   u8 matchSet = pInfo->matchSet;
90949   u8 noCase = pInfo->noCase;
90950   int prevEscape = 0;     /* True if the previous character was 'escape' */
90951 
90952   while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
90953     if( c==matchAll && !prevEscape ){
90954       while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
90955                || c == matchOne ){
90956         if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
90957           return 0;
90958         }
90959       }
90960       if( c==0 ){
90961         return 1;
90962       }else if( c==esc ){
90963         c = sqlite3Utf8Read(&zPattern);
90964         if( c==0 ){
90965           return 0;
90966         }
90967       }else if( c==matchSet ){
90968         assert( esc==0 );         /* This is GLOB, not LIKE */
90969         assert( matchSet<0x80 );  /* '[' is a single-byte character */
90970         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
90971           SQLITE_SKIP_UTF8(zString);
90972         }
90973         return *zString!=0;
90974       }
90975       while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
90976         if( noCase ){
90977           GlobUpperToLower(c2);
90978           GlobUpperToLower(c);
90979           while( c2 != 0 && c2 != c ){
90980             c2 = sqlite3Utf8Read(&zString);
90981             GlobUpperToLower(c2);
90982           }
90983         }else{
90984           while( c2 != 0 && c2 != c ){
90985             c2 = sqlite3Utf8Read(&zString);
90986           }
90987         }
90988         if( c2==0 ) return 0;
90989         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
90990       }
90991       return 0;
90992     }else if( c==matchOne && !prevEscape ){
90993       if( sqlite3Utf8Read(&zString)==0 ){
90994         return 0;
90995       }
90996     }else if( c==matchSet ){
90997       u32 prior_c = 0;
90998       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
90999       seen = 0;
91000       invert = 0;
91001       c = sqlite3Utf8Read(&zString);
91002       if( c==0 ) return 0;
91003       c2 = sqlite3Utf8Read(&zPattern);
91004       if( c2=='^' ){
91005         invert = 1;
91006         c2 = sqlite3Utf8Read(&zPattern);
91007       }
91008       if( c2==']' ){
91009         if( c==']' ) seen = 1;
91010         c2 = sqlite3Utf8Read(&zPattern);
91011       }
91012       while( c2 && c2!=']' ){
91013         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
91014           c2 = sqlite3Utf8Read(&zPattern);
91015           if( c>=prior_c && c<=c2 ) seen = 1;
91016           prior_c = 0;
91017         }else{
91018           if( c==c2 ){
91019             seen = 1;
91020           }
91021           prior_c = c2;
91022         }
91023         c2 = sqlite3Utf8Read(&zPattern);
91024       }
91025       if( c2==0 || (seen ^ invert)==0 ){
91026         return 0;
91027       }
91028     }else if( esc==c && !prevEscape ){
91029       prevEscape = 1;
91030     }else{
91031       c2 = sqlite3Utf8Read(&zString);
91032       if( noCase ){
91033         GlobUpperToLower(c);
91034         GlobUpperToLower(c2);
91035       }
91036       if( c!=c2 ){
91037         return 0;
91038       }
91039       prevEscape = 0;
91040     }
91041   }
91042   return *zString==0;
91043 }
91044 
91045 /*
91046 ** The sqlite3_strglob() interface.
91047 */
91048 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
91049   return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
91050 }
91051 
91052 /*
91053 ** Count the number of times that the LIKE operator (or GLOB which is
91054 ** just a variation of LIKE) gets called.  This is used for testing
91055 ** only.
91056 */
91057 #ifdef SQLITE_TEST
91058 SQLITE_API int sqlite3_like_count = 0;
91059 #endif
91060 
91061 
91062 /*
91063 ** Implementation of the like() SQL function.  This function implements
91064 ** the build-in LIKE operator.  The first argument to the function is the
91065 ** pattern and the second argument is the string.  So, the SQL statements:
91066 **
91067 **       A LIKE B
91068 **
91069 ** is implemented as like(B,A).
91070 **
91071 ** This same function (with a different compareInfo structure) computes
91072 ** the GLOB operator.
91073 */
91074 static void likeFunc(
91075   sqlite3_context *context,
91076   int argc,
91077   sqlite3_value **argv
91078 ){
91079   const unsigned char *zA, *zB;
91080   u32 escape = 0;
91081   int nPat;
91082   sqlite3 *db = sqlite3_context_db_handle(context);
91083 
91084   zB = sqlite3_value_text(argv[0]);
91085   zA = sqlite3_value_text(argv[1]);
91086 
91087   /* Limit the length of the LIKE or GLOB pattern to avoid problems
91088   ** of deep recursion and N*N behavior in patternCompare().
91089   */
91090   nPat = sqlite3_value_bytes(argv[0]);
91091   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
91092   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
91093   if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
91094     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
91095     return;
91096   }
91097   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
91098 
91099   if( argc==3 ){
91100     /* The escape character string must consist of a single UTF-8 character.
91101     ** Otherwise, return an error.
91102     */
91103     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
91104     if( zEsc==0 ) return;
91105     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
91106       sqlite3_result_error(context,
91107           "ESCAPE expression must be a single character", -1);
91108       return;
91109     }
91110     escape = sqlite3Utf8Read(&zEsc);
91111   }
91112   if( zA && zB ){
91113     struct compareInfo *pInfo = sqlite3_user_data(context);
91114 #ifdef SQLITE_TEST
91115     sqlite3_like_count++;
91116 #endif
91117 
91118     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
91119   }
91120 }
91121 
91122 /*
91123 ** Implementation of the NULLIF(x,y) function.  The result is the first
91124 ** argument if the arguments are different.  The result is NULL if the
91125 ** arguments are equal to each other.
91126 */
91127 static void nullifFunc(
91128   sqlite3_context *context,
91129   int NotUsed,
91130   sqlite3_value **argv
91131 ){
91132   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
91133   UNUSED_PARAMETER(NotUsed);
91134   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
91135     sqlite3_result_value(context, argv[0]);
91136   }
91137 }
91138 
91139 /*
91140 ** Implementation of the sqlite_version() function.  The result is the version
91141 ** of the SQLite library that is running.
91142 */
91143 static void versionFunc(
91144   sqlite3_context *context,
91145   int NotUsed,
91146   sqlite3_value **NotUsed2
91147 ){
91148   UNUSED_PARAMETER2(NotUsed, NotUsed2);
91149   /* IMP: R-48699-48617 This function is an SQL wrapper around the
91150   ** sqlite3_libversion() C-interface. */
91151   sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
91152 }
91153 
91154 /*
91155 ** Implementation of the sqlite_source_id() function. The result is a string
91156 ** that identifies the particular version of the source code used to build
91157 ** SQLite.
91158 */
91159 static void sourceidFunc(
91160   sqlite3_context *context,
91161   int NotUsed,
91162   sqlite3_value **NotUsed2
91163 ){
91164   UNUSED_PARAMETER2(NotUsed, NotUsed2);
91165   /* IMP: R-24470-31136 This function is an SQL wrapper around the
91166   ** sqlite3_sourceid() C interface. */
91167   sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
91168 }
91169 
91170 /*
91171 ** Implementation of the sqlite_log() function.  This is a wrapper around
91172 ** sqlite3_log().  The return value is NULL.  The function exists purely for
91173 ** its side-effects.
91174 */
91175 static void errlogFunc(
91176   sqlite3_context *context,
91177   int argc,
91178   sqlite3_value **argv
91179 ){
91180   UNUSED_PARAMETER(argc);
91181   UNUSED_PARAMETER(context);
91182   sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
91183 }
91184 
91185 /*
91186 ** Implementation of the sqlite_compileoption_used() function.
91187 ** The result is an integer that identifies if the compiler option
91188 ** was used to build SQLite.
91189 */
91190 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
91191 static void compileoptionusedFunc(
91192   sqlite3_context *context,
91193   int argc,
91194   sqlite3_value **argv
91195 ){
91196   const char *zOptName;
91197   assert( argc==1 );
91198   UNUSED_PARAMETER(argc);
91199   /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
91200   ** function is a wrapper around the sqlite3_compileoption_used() C/C++
91201   ** function.
91202   */
91203   if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
91204     sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
91205   }
91206 }
91207 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
91208 
91209 /*
91210 ** Implementation of the sqlite_compileoption_get() function.
91211 ** The result is a string that identifies the compiler options
91212 ** used to build SQLite.
91213 */
91214 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
91215 static void compileoptiongetFunc(
91216   sqlite3_context *context,
91217   int argc,
91218   sqlite3_value **argv
91219 ){
91220   int n;
91221   assert( argc==1 );
91222   UNUSED_PARAMETER(argc);
91223   /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
91224   ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
91225   */
91226   n = sqlite3_value_int(argv[0]);
91227   sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
91228 }
91229 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
91230 
91231 /* Array for converting from half-bytes (nybbles) into ASCII hex
91232 ** digits. */
91233 static const char hexdigits[] = {
91234   '0', '1', '2', '3', '4', '5', '6', '7',
91235   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
91236 };
91237 
91238 /*
91239 ** Implementation of the QUOTE() function.  This function takes a single
91240 ** argument.  If the argument is numeric, the return value is the same as
91241 ** the argument.  If the argument is NULL, the return value is the string
91242 ** "NULL".  Otherwise, the argument is enclosed in single quotes with
91243 ** single-quote escapes.
91244 */
91245 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
91246   assert( argc==1 );
91247   UNUSED_PARAMETER(argc);
91248   switch( sqlite3_value_type(argv[0]) ){
91249     case SQLITE_FLOAT: {
91250       double r1, r2;
91251       char zBuf[50];
91252       r1 = sqlite3_value_double(argv[0]);
91253       sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
91254       sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
91255       if( r1!=r2 ){
91256         sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
91257       }
91258       sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
91259       break;
91260     }
91261     case SQLITE_INTEGER: {
91262       sqlite3_result_value(context, argv[0]);
91263       break;
91264     }
91265     case SQLITE_BLOB: {
91266       char *zText = 0;
91267       char const *zBlob = sqlite3_value_blob(argv[0]);
91268       int nBlob = sqlite3_value_bytes(argv[0]);
91269       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
91270       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
91271       if( zText ){
91272         int i;
91273         for(i=0; i<nBlob; i++){
91274           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
91275           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
91276         }
91277         zText[(nBlob*2)+2] = '\'';
91278         zText[(nBlob*2)+3] = '\0';
91279         zText[0] = 'X';
91280         zText[1] = '\'';
91281         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
91282         sqlite3_free(zText);
91283       }
91284       break;
91285     }
91286     case SQLITE_TEXT: {
91287       int i,j;
91288       u64 n;
91289       const unsigned char *zArg = sqlite3_value_text(argv[0]);
91290       char *z;
91291 
91292       if( zArg==0 ) return;
91293       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
91294       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
91295       if( z ){
91296         z[0] = '\'';
91297         for(i=0, j=1; zArg[i]; i++){
91298           z[j++] = zArg[i];
91299           if( zArg[i]=='\'' ){
91300             z[j++] = '\'';
91301           }
91302         }
91303         z[j++] = '\'';
91304         z[j] = 0;
91305         sqlite3_result_text(context, z, j, sqlite3_free);
91306       }
91307       break;
91308     }
91309     default: {
91310       assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
91311       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
91312       break;
91313     }
91314   }
91315 }
91316 
91317 /*
91318 ** The unicode() function.  Return the integer unicode code-point value
91319 ** for the first character of the input string.
91320 */
91321 static void unicodeFunc(
91322   sqlite3_context *context,
91323   int argc,
91324   sqlite3_value **argv
91325 ){
91326   const unsigned char *z = sqlite3_value_text(argv[0]);
91327   (void)argc;
91328   if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
91329 }
91330 
91331 /*
91332 ** The char() function takes zero or more arguments, each of which is
91333 ** an integer.  It constructs a string where each character of the string
91334 ** is the unicode character for the corresponding integer argument.
91335 */
91336 static void charFunc(
91337   sqlite3_context *context,
91338   int argc,
91339   sqlite3_value **argv
91340 ){
91341   unsigned char *z, *zOut;
91342   int i;
91343   zOut = z = sqlite3_malloc( argc*4+1 );
91344   if( z==0 ){
91345     sqlite3_result_error_nomem(context);
91346     return;
91347   }
91348   for(i=0; i<argc; i++){
91349     sqlite3_int64 x;
91350     unsigned c;
91351     x = sqlite3_value_int64(argv[i]);
91352     if( x<0 || x>0x10ffff ) x = 0xfffd;
91353     c = (unsigned)(x & 0x1fffff);
91354     if( c<0x00080 ){
91355       *zOut++ = (u8)(c&0xFF);
91356     }else if( c<0x00800 ){
91357       *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
91358       *zOut++ = 0x80 + (u8)(c & 0x3F);
91359     }else if( c<0x10000 ){
91360       *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
91361       *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
91362       *zOut++ = 0x80 + (u8)(c & 0x3F);
91363     }else{
91364       *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
91365       *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
91366       *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
91367       *zOut++ = 0x80 + (u8)(c & 0x3F);
91368     }                                                    \
91369   }
91370   sqlite3_result_text(context, (char*)z, (int)(zOut-z), sqlite3_free);
91371 }
91372 
91373 /*
91374 ** The hex() function.  Interpret the argument as a blob.  Return
91375 ** a hexadecimal rendering as text.
91376 */
91377 static void hexFunc(
91378   sqlite3_context *context,
91379   int argc,
91380   sqlite3_value **argv
91381 ){
91382   int i, n;
91383   const unsigned char *pBlob;
91384   char *zHex, *z;
91385   assert( argc==1 );
91386   UNUSED_PARAMETER(argc);
91387   pBlob = sqlite3_value_blob(argv[0]);
91388   n = sqlite3_value_bytes(argv[0]);
91389   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
91390   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
91391   if( zHex ){
91392     for(i=0; i<n; i++, pBlob++){
91393       unsigned char c = *pBlob;
91394       *(z++) = hexdigits[(c>>4)&0xf];
91395       *(z++) = hexdigits[c&0xf];
91396     }
91397     *z = 0;
91398     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
91399   }
91400 }
91401 
91402 /*
91403 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
91404 */
91405 static void zeroblobFunc(
91406   sqlite3_context *context,
91407   int argc,
91408   sqlite3_value **argv
91409 ){
91410   i64 n;
91411   sqlite3 *db = sqlite3_context_db_handle(context);
91412   assert( argc==1 );
91413   UNUSED_PARAMETER(argc);
91414   n = sqlite3_value_int64(argv[0]);
91415   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
91416   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
91417   if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
91418     sqlite3_result_error_toobig(context);
91419   }else{
91420     sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
91421   }
91422 }
91423 
91424 /*
91425 ** The replace() function.  Three arguments are all strings: call
91426 ** them A, B, and C. The result is also a string which is derived
91427 ** from A by replacing every occurrence of B with C.  The match
91428 ** must be exact.  Collating sequences are not used.
91429 */
91430 static void replaceFunc(
91431   sqlite3_context *context,
91432   int argc,
91433   sqlite3_value **argv
91434 ){
91435   const unsigned char *zStr;        /* The input string A */
91436   const unsigned char *zPattern;    /* The pattern string B */
91437   const unsigned char *zRep;        /* The replacement string C */
91438   unsigned char *zOut;              /* The output */
91439   int nStr;                /* Size of zStr */
91440   int nPattern;            /* Size of zPattern */
91441   int nRep;                /* Size of zRep */
91442   i64 nOut;                /* Maximum size of zOut */
91443   int loopLimit;           /* Last zStr[] that might match zPattern[] */
91444   int i, j;                /* Loop counters */
91445 
91446   assert( argc==3 );
91447   UNUSED_PARAMETER(argc);
91448   zStr = sqlite3_value_text(argv[0]);
91449   if( zStr==0 ) return;
91450   nStr = sqlite3_value_bytes(argv[0]);
91451   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
91452   zPattern = sqlite3_value_text(argv[1]);
91453   if( zPattern==0 ){
91454     assert( sqlite3_value_type(argv[1])==SQLITE_NULL
91455             || sqlite3_context_db_handle(context)->mallocFailed );
91456     return;
91457   }
91458   if( zPattern[0]==0 ){
91459     assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
91460     sqlite3_result_value(context, argv[0]);
91461     return;
91462   }
91463   nPattern = sqlite3_value_bytes(argv[1]);
91464   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
91465   zRep = sqlite3_value_text(argv[2]);
91466   if( zRep==0 ) return;
91467   nRep = sqlite3_value_bytes(argv[2]);
91468   assert( zRep==sqlite3_value_text(argv[2]) );
91469   nOut = nStr + 1;
91470   assert( nOut<SQLITE_MAX_LENGTH );
91471   zOut = contextMalloc(context, (i64)nOut);
91472   if( zOut==0 ){
91473     return;
91474   }
91475   loopLimit = nStr - nPattern;
91476   for(i=j=0; i<=loopLimit; i++){
91477     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
91478       zOut[j++] = zStr[i];
91479     }else{
91480       u8 *zOld;
91481       sqlite3 *db = sqlite3_context_db_handle(context);
91482       nOut += nRep - nPattern;
91483       testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
91484       testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
91485       if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
91486         sqlite3_result_error_toobig(context);
91487         sqlite3_free(zOut);
91488         return;
91489       }
91490       zOld = zOut;
91491       zOut = sqlite3_realloc(zOut, (int)nOut);
91492       if( zOut==0 ){
91493         sqlite3_result_error_nomem(context);
91494         sqlite3_free(zOld);
91495         return;
91496       }
91497       memcpy(&zOut[j], zRep, nRep);
91498       j += nRep;
91499       i += nPattern-1;
91500     }
91501   }
91502   assert( j+nStr-i+1==nOut );
91503   memcpy(&zOut[j], &zStr[i], nStr-i);
91504   j += nStr - i;
91505   assert( j<=nOut );
91506   zOut[j] = 0;
91507   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
91508 }
91509 
91510 /*
91511 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
91512 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
91513 */
91514 static void trimFunc(
91515   sqlite3_context *context,
91516   int argc,
91517   sqlite3_value **argv
91518 ){
91519   const unsigned char *zIn;         /* Input string */
91520   const unsigned char *zCharSet;    /* Set of characters to trim */
91521   int nIn;                          /* Number of bytes in input */
91522   int flags;                        /* 1: trimleft  2: trimright  3: trim */
91523   int i;                            /* Loop counter */
91524   unsigned char *aLen = 0;          /* Length of each character in zCharSet */
91525   unsigned char **azChar = 0;       /* Individual characters in zCharSet */
91526   int nChar;                        /* Number of characters in zCharSet */
91527 
91528   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
91529     return;
91530   }
91531   zIn = sqlite3_value_text(argv[0]);
91532   if( zIn==0 ) return;
91533   nIn = sqlite3_value_bytes(argv[0]);
91534   assert( zIn==sqlite3_value_text(argv[0]) );
91535   if( argc==1 ){
91536     static const unsigned char lenOne[] = { 1 };
91537     static unsigned char * const azOne[] = { (u8*)" " };
91538     nChar = 1;
91539     aLen = (u8*)lenOne;
91540     azChar = (unsigned char **)azOne;
91541     zCharSet = 0;
91542   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
91543     return;
91544   }else{
91545     const unsigned char *z;
91546     for(z=zCharSet, nChar=0; *z; nChar++){
91547       SQLITE_SKIP_UTF8(z);
91548     }
91549     if( nChar>0 ){
91550       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
91551       if( azChar==0 ){
91552         return;
91553       }
91554       aLen = (unsigned char*)&azChar[nChar];
91555       for(z=zCharSet, nChar=0; *z; nChar++){
91556         azChar[nChar] = (unsigned char *)z;
91557         SQLITE_SKIP_UTF8(z);
91558         aLen[nChar] = (u8)(z - azChar[nChar]);
91559       }
91560     }
91561   }
91562   if( nChar>0 ){
91563     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
91564     if( flags & 1 ){
91565       while( nIn>0 ){
91566         int len = 0;
91567         for(i=0; i<nChar; i++){
91568           len = aLen[i];
91569           if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
91570         }
91571         if( i>=nChar ) break;
91572         zIn += len;
91573         nIn -= len;
91574       }
91575     }
91576     if( flags & 2 ){
91577       while( nIn>0 ){
91578         int len = 0;
91579         for(i=0; i<nChar; i++){
91580           len = aLen[i];
91581           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
91582         }
91583         if( i>=nChar ) break;
91584         nIn -= len;
91585       }
91586     }
91587     if( zCharSet ){
91588       sqlite3_free(azChar);
91589     }
91590   }
91591   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
91592 }
91593 
91594 
91595 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
91596 ** is only available if the SQLITE_SOUNDEX compile-time option is used
91597 ** when SQLite is built.
91598 */
91599 #ifdef SQLITE_SOUNDEX
91600 /*
91601 ** Compute the soundex encoding of a word.
91602 **
91603 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
91604 ** soundex encoding of the string X.
91605 */
91606 static void soundexFunc(
91607   sqlite3_context *context,
91608   int argc,
91609   sqlite3_value **argv
91610 ){
91611   char zResult[8];
91612   const u8 *zIn;
91613   int i, j;
91614   static const unsigned char iCode[] = {
91615     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91616     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91617     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91618     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91619     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
91620     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
91621     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
91622     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
91623   };
91624   assert( argc==1 );
91625   zIn = (u8*)sqlite3_value_text(argv[0]);
91626   if( zIn==0 ) zIn = (u8*)"";
91627   for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
91628   if( zIn[i] ){
91629     u8 prevcode = iCode[zIn[i]&0x7f];
91630     zResult[0] = sqlite3Toupper(zIn[i]);
91631     for(j=1; j<4 && zIn[i]; i++){
91632       int code = iCode[zIn[i]&0x7f];
91633       if( code>0 ){
91634         if( code!=prevcode ){
91635           prevcode = code;
91636           zResult[j++] = code + '0';
91637         }
91638       }else{
91639         prevcode = 0;
91640       }
91641     }
91642     while( j<4 ){
91643       zResult[j++] = '0';
91644     }
91645     zResult[j] = 0;
91646     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
91647   }else{
91648     /* IMP: R-64894-50321 The string "?000" is returned if the argument
91649     ** is NULL or contains no ASCII alphabetic characters. */
91650     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
91651   }
91652 }
91653 #endif /* SQLITE_SOUNDEX */
91654 
91655 #ifndef SQLITE_OMIT_LOAD_EXTENSION
91656 /*
91657 ** A function that loads a shared-library extension then returns NULL.
91658 */
91659 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
91660   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
91661   const char *zProc;
91662   sqlite3 *db = sqlite3_context_db_handle(context);
91663   char *zErrMsg = 0;
91664 
91665   if( argc==2 ){
91666     zProc = (const char *)sqlite3_value_text(argv[1]);
91667   }else{
91668     zProc = 0;
91669   }
91670   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
91671     sqlite3_result_error(context, zErrMsg, -1);
91672     sqlite3_free(zErrMsg);
91673   }
91674 }
91675 #endif
91676 
91677 
91678 /*
91679 ** An instance of the following structure holds the context of a
91680 ** sum() or avg() aggregate computation.
91681 */
91682 typedef struct SumCtx SumCtx;
91683 struct SumCtx {
91684   double rSum;      /* Floating point sum */
91685   i64 iSum;         /* Integer sum */
91686   i64 cnt;          /* Number of elements summed */
91687   u8 overflow;      /* True if integer overflow seen */
91688   u8 approx;        /* True if non-integer value was input to the sum */
91689 };
91690 
91691 /*
91692 ** Routines used to compute the sum, average, and total.
91693 **
91694 ** The SUM() function follows the (broken) SQL standard which means
91695 ** that it returns NULL if it sums over no inputs.  TOTAL returns
91696 ** 0.0 in that case.  In addition, TOTAL always returns a float where
91697 ** SUM might return an integer if it never encounters a floating point
91698 ** value.  TOTAL never fails, but SUM might through an exception if
91699 ** it overflows an integer.
91700 */
91701 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
91702   SumCtx *p;
91703   int type;
91704   assert( argc==1 );
91705   UNUSED_PARAMETER(argc);
91706   p = sqlite3_aggregate_context(context, sizeof(*p));
91707   type = sqlite3_value_numeric_type(argv[0]);
91708   if( p && type!=SQLITE_NULL ){
91709     p->cnt++;
91710     if( type==SQLITE_INTEGER ){
91711       i64 v = sqlite3_value_int64(argv[0]);
91712       p->rSum += v;
91713       if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
91714         p->overflow = 1;
91715       }
91716     }else{
91717       p->rSum += sqlite3_value_double(argv[0]);
91718       p->approx = 1;
91719     }
91720   }
91721 }
91722 static void sumFinalize(sqlite3_context *context){
91723   SumCtx *p;
91724   p = sqlite3_aggregate_context(context, 0);
91725   if( p && p->cnt>0 ){
91726     if( p->overflow ){
91727       sqlite3_result_error(context,"integer overflow",-1);
91728     }else if( p->approx ){
91729       sqlite3_result_double(context, p->rSum);
91730     }else{
91731       sqlite3_result_int64(context, p->iSum);
91732     }
91733   }
91734 }
91735 static void avgFinalize(sqlite3_context *context){
91736   SumCtx *p;
91737   p = sqlite3_aggregate_context(context, 0);
91738   if( p && p->cnt>0 ){
91739     sqlite3_result_double(context, p->rSum/(double)p->cnt);
91740   }
91741 }
91742 static void totalFinalize(sqlite3_context *context){
91743   SumCtx *p;
91744   p = sqlite3_aggregate_context(context, 0);
91745   /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
91746   sqlite3_result_double(context, p ? p->rSum : (double)0);
91747 }
91748 
91749 /*
91750 ** The following structure keeps track of state information for the
91751 ** count() aggregate function.
91752 */
91753 typedef struct CountCtx CountCtx;
91754 struct CountCtx {
91755   i64 n;
91756 };
91757 
91758 /*
91759 ** Routines to implement the count() aggregate function.
91760 */
91761 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
91762   CountCtx *p;
91763   p = sqlite3_aggregate_context(context, sizeof(*p));
91764   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
91765     p->n++;
91766   }
91767 
91768 #ifndef SQLITE_OMIT_DEPRECATED
91769   /* The sqlite3_aggregate_count() function is deprecated.  But just to make
91770   ** sure it still operates correctly, verify that its count agrees with our
91771   ** internal count when using count(*) and when the total count can be
91772   ** expressed as a 32-bit integer. */
91773   assert( argc==1 || p==0 || p->n>0x7fffffff
91774           || p->n==sqlite3_aggregate_count(context) );
91775 #endif
91776 }
91777 static void countFinalize(sqlite3_context *context){
91778   CountCtx *p;
91779   p = sqlite3_aggregate_context(context, 0);
91780   sqlite3_result_int64(context, p ? p->n : 0);
91781 }
91782 
91783 /*
91784 ** Routines to implement min() and max() aggregate functions.
91785 */
91786 static void minmaxStep(
91787   sqlite3_context *context,
91788   int NotUsed,
91789   sqlite3_value **argv
91790 ){
91791   Mem *pArg  = (Mem *)argv[0];
91792   Mem *pBest;
91793   UNUSED_PARAMETER(NotUsed);
91794 
91795   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
91796   if( !pBest ) return;
91797 
91798   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
91799     if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
91800   }else if( pBest->flags ){
91801     int max;
91802     int cmp;
91803     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
91804     /* This step function is used for both the min() and max() aggregates,
91805     ** the only difference between the two being that the sense of the
91806     ** comparison is inverted. For the max() aggregate, the
91807     ** sqlite3_user_data() function returns (void *)-1. For min() it
91808     ** returns (void *)db, where db is the sqlite3* database pointer.
91809     ** Therefore the next statement sets variable 'max' to 1 for the max()
91810     ** aggregate, or 0 for min().
91811     */
91812     max = sqlite3_user_data(context)!=0;
91813     cmp = sqlite3MemCompare(pBest, pArg, pColl);
91814     if( (max && cmp<0) || (!max && cmp>0) ){
91815       sqlite3VdbeMemCopy(pBest, pArg);
91816     }else{
91817       sqlite3SkipAccumulatorLoad(context);
91818     }
91819   }else{
91820     sqlite3VdbeMemCopy(pBest, pArg);
91821   }
91822 }
91823 static void minMaxFinalize(sqlite3_context *context){
91824   sqlite3_value *pRes;
91825   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
91826   if( pRes ){
91827     if( pRes->flags ){
91828       sqlite3_result_value(context, pRes);
91829     }
91830     sqlite3VdbeMemRelease(pRes);
91831   }
91832 }
91833 
91834 /*
91835 ** group_concat(EXPR, ?SEPARATOR?)
91836 */
91837 static void groupConcatStep(
91838   sqlite3_context *context,
91839   int argc,
91840   sqlite3_value **argv
91841 ){
91842   const char *zVal;
91843   StrAccum *pAccum;
91844   const char *zSep;
91845   int nVal, nSep;
91846   assert( argc==1 || argc==2 );
91847   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
91848   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
91849 
91850   if( pAccum ){
91851     sqlite3 *db = sqlite3_context_db_handle(context);
91852     int firstTerm = pAccum->useMalloc==0;
91853     pAccum->useMalloc = 2;
91854     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
91855     if( !firstTerm ){
91856       if( argc==2 ){
91857         zSep = (char*)sqlite3_value_text(argv[1]);
91858         nSep = sqlite3_value_bytes(argv[1]);
91859       }else{
91860         zSep = ",";
91861         nSep = 1;
91862       }
91863       if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
91864     }
91865     zVal = (char*)sqlite3_value_text(argv[0]);
91866     nVal = sqlite3_value_bytes(argv[0]);
91867     if( nVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
91868   }
91869 }
91870 static void groupConcatFinalize(sqlite3_context *context){
91871   StrAccum *pAccum;
91872   pAccum = sqlite3_aggregate_context(context, 0);
91873   if( pAccum ){
91874     if( pAccum->accError==STRACCUM_TOOBIG ){
91875       sqlite3_result_error_toobig(context);
91876     }else if( pAccum->accError==STRACCUM_NOMEM ){
91877       sqlite3_result_error_nomem(context);
91878     }else{
91879       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
91880                           sqlite3_free);
91881     }
91882   }
91883 }
91884 
91885 /*
91886 ** This routine does per-connection function registration.  Most
91887 ** of the built-in functions above are part of the global function set.
91888 ** This routine only deals with those that are not global.
91889 */
91890 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
91891   int rc = sqlite3_overload_function(db, "MATCH", 2);
91892   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
91893   if( rc==SQLITE_NOMEM ){
91894     db->mallocFailed = 1;
91895   }
91896 }
91897 
91898 /*
91899 ** Set the LIKEOPT flag on the 2-argument function with the given name.
91900 */
91901 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
91902   FuncDef *pDef;
91903   pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
91904                              2, SQLITE_UTF8, 0);
91905   if( ALWAYS(pDef) ){
91906     pDef->funcFlags |= flagVal;
91907   }
91908 }
91909 
91910 /*
91911 ** Register the built-in LIKE and GLOB functions.  The caseSensitive
91912 ** parameter determines whether or not the LIKE operator is case
91913 ** sensitive.  GLOB is always case sensitive.
91914 */
91915 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
91916   struct compareInfo *pInfo;
91917   if( caseSensitive ){
91918     pInfo = (struct compareInfo*)&likeInfoAlt;
91919   }else{
91920     pInfo = (struct compareInfo*)&likeInfoNorm;
91921   }
91922   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
91923   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
91924   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
91925       (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
91926   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
91927   setLikeOptFlag(db, "like",
91928       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
91929 }
91930 
91931 /*
91932 ** pExpr points to an expression which implements a function.  If
91933 ** it is appropriate to apply the LIKE optimization to that function
91934 ** then set aWc[0] through aWc[2] to the wildcard characters and
91935 ** return TRUE.  If the function is not a LIKE-style function then
91936 ** return FALSE.
91937 */
91938 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
91939   FuncDef *pDef;
91940   if( pExpr->op!=TK_FUNCTION
91941    || !pExpr->x.pList
91942    || pExpr->x.pList->nExpr!=2
91943   ){
91944     return 0;
91945   }
91946   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
91947   pDef = sqlite3FindFunction(db, pExpr->u.zToken,
91948                              sqlite3Strlen30(pExpr->u.zToken),
91949                              2, SQLITE_UTF8, 0);
91950   if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
91951     return 0;
91952   }
91953 
91954   /* The memcpy() statement assumes that the wildcard characters are
91955   ** the first three statements in the compareInfo structure.  The
91956   ** asserts() that follow verify that assumption
91957   */
91958   memcpy(aWc, pDef->pUserData, 3);
91959   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
91960   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
91961   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
91962   *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
91963   return 1;
91964 }
91965 
91966 /*
91967 ** All all of the FuncDef structures in the aBuiltinFunc[] array above
91968 ** to the global function hash table.  This occurs at start-time (as
91969 ** a consequence of calling sqlite3_initialize()).
91970 **
91971 ** After this routine runs
91972 */
91973 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
91974   /*
91975   ** The following array holds FuncDef structures for all of the functions
91976   ** defined in this file.
91977   **
91978   ** The array cannot be constant since changes are made to the
91979   ** FuncDef.pHash elements at start-time.  The elements of this array
91980   ** are read-only after initialization is complete.
91981   */
91982   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
91983     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
91984     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
91985     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
91986     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
91987     FUNCTION(trim,               1, 3, 0, trimFunc         ),
91988     FUNCTION(trim,               2, 3, 0, trimFunc         ),
91989     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
91990     FUNCTION(min,                0, 0, 1, 0                ),
91991     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
91992     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
91993     FUNCTION(max,                0, 1, 1, 0                ),
91994     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
91995     FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
91996     FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
91997     FUNCTION(instr,              2, 0, 0, instrFunc        ),
91998     FUNCTION(substr,             2, 0, 0, substrFunc       ),
91999     FUNCTION(substr,             3, 0, 0, substrFunc       ),
92000     FUNCTION(printf,            -1, 0, 0, printfFunc       ),
92001     FUNCTION(unicode,            1, 0, 0, unicodeFunc      ),
92002     FUNCTION(char,              -1, 0, 0, charFunc         ),
92003     FUNCTION(abs,                1, 0, 0, absFunc          ),
92004 #ifndef SQLITE_OMIT_FLOATING_POINT
92005     FUNCTION(round,              1, 0, 0, roundFunc        ),
92006     FUNCTION(round,              2, 0, 0, roundFunc        ),
92007 #endif
92008     FUNCTION(upper,              1, 0, 0, upperFunc        ),
92009     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
92010     FUNCTION(coalesce,           1, 0, 0, 0                ),
92011     FUNCTION(coalesce,           0, 0, 0, 0                ),
92012     FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
92013     FUNCTION(hex,                1, 0, 0, hexFunc          ),
92014     FUNCTION2(ifnull,            2, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
92015     FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
92016     FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
92017     VFUNCTION(random,            0, 0, 0, randomFunc       ),
92018     VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
92019     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
92020     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
92021     FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
92022     FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
92023 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
92024     FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
92025     FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
92026 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
92027     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
92028     VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
92029     VFUNCTION(changes,           0, 0, 0, changes          ),
92030     VFUNCTION(total_changes,     0, 0, 0, total_changes    ),
92031     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
92032     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
92033   #ifdef SQLITE_SOUNDEX
92034     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
92035   #endif
92036   #ifndef SQLITE_OMIT_LOAD_EXTENSION
92037     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
92038     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
92039   #endif
92040     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
92041     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
92042     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
92043  /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
92044     {0,SQLITE_UTF8|SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
92045     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
92046     AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
92047     AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
92048 
92049     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
92050   #ifdef SQLITE_CASE_SENSITIVE_LIKE
92051     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
92052     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
92053   #else
92054     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
92055     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
92056   #endif
92057   };
92058 
92059   int i;
92060   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
92061   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
92062 
92063   for(i=0; i<ArraySize(aBuiltinFunc); i++){
92064     sqlite3FuncDefInsert(pHash, &aFunc[i]);
92065   }
92066   sqlite3RegisterDateTimeFunctions();
92067 #ifndef SQLITE_OMIT_ALTERTABLE
92068   sqlite3AlterFunctions();
92069 #endif
92070 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
92071   sqlite3AnalyzeFunctions();
92072 #endif
92073 }
92074 
92075 /************** End of func.c ************************************************/
92076 /************** Begin file fkey.c ********************************************/
92077 /*
92078 **
92079 ** The author disclaims copyright to this source code.  In place of
92080 ** a legal notice, here is a blessing:
92081 **
92082 **    May you do good and not evil.
92083 **    May you find forgiveness for yourself and forgive others.
92084 **    May you share freely, never taking more than you give.
92085 **
92086 *************************************************************************
92087 ** This file contains code used by the compiler to add foreign key
92088 ** support to compiled SQL statements.
92089 */
92090 
92091 #ifndef SQLITE_OMIT_FOREIGN_KEY
92092 #ifndef SQLITE_OMIT_TRIGGER
92093 
92094 /*
92095 ** Deferred and Immediate FKs
92096 ** --------------------------
92097 **
92098 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
92099 ** If an immediate foreign key constraint is violated,
92100 ** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
92101 ** statement transaction rolled back. If a
92102 ** deferred foreign key constraint is violated, no action is taken
92103 ** immediately. However if the application attempts to commit the
92104 ** transaction before fixing the constraint violation, the attempt fails.
92105 **
92106 ** Deferred constraints are implemented using a simple counter associated
92107 ** with the database handle. The counter is set to zero each time a
92108 ** database transaction is opened. Each time a statement is executed
92109 ** that causes a foreign key violation, the counter is incremented. Each
92110 ** time a statement is executed that removes an existing violation from
92111 ** the database, the counter is decremented. When the transaction is
92112 ** committed, the commit fails if the current value of the counter is
92113 ** greater than zero. This scheme has two big drawbacks:
92114 **
92115 **   * When a commit fails due to a deferred foreign key constraint,
92116 **     there is no way to tell which foreign constraint is not satisfied,
92117 **     or which row it is not satisfied for.
92118 **
92119 **   * If the database contains foreign key violations when the
92120 **     transaction is opened, this may cause the mechanism to malfunction.
92121 **
92122 ** Despite these problems, this approach is adopted as it seems simpler
92123 ** than the alternatives.
92124 **
92125 ** INSERT operations:
92126 **
92127 **   I.1) For each FK for which the table is the child table, search
92128 **        the parent table for a match. If none is found increment the
92129 **        constraint counter.
92130 **
92131 **   I.2) For each FK for which the table is the parent table,
92132 **        search the child table for rows that correspond to the new
92133 **        row in the parent table. Decrement the counter for each row
92134 **        found (as the constraint is now satisfied).
92135 **
92136 ** DELETE operations:
92137 **
92138 **   D.1) For each FK for which the table is the child table,
92139 **        search the parent table for a row that corresponds to the
92140 **        deleted row in the child table. If such a row is not found,
92141 **        decrement the counter.
92142 **
92143 **   D.2) For each FK for which the table is the parent table, search
92144 **        the child table for rows that correspond to the deleted row
92145 **        in the parent table. For each found increment the counter.
92146 **
92147 ** UPDATE operations:
92148 **
92149 **   An UPDATE command requires that all 4 steps above are taken, but only
92150 **   for FK constraints for which the affected columns are actually
92151 **   modified (values must be compared at runtime).
92152 **
92153 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
92154 ** This simplifies the implementation a bit.
92155 **
92156 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
92157 ** resolution is considered to delete rows before the new row is inserted.
92158 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
92159 ** is thrown, even if the FK constraint would be satisfied after the new
92160 ** row is inserted.
92161 **
92162 ** Immediate constraints are usually handled similarly. The only difference
92163 ** is that the counter used is stored as part of each individual statement
92164 ** object (struct Vdbe). If, after the statement has run, its immediate
92165 ** constraint counter is greater than zero,
92166 ** it returns SQLITE_CONSTRAINT_FOREIGNKEY
92167 ** and the statement transaction is rolled back. An exception is an INSERT
92168 ** statement that inserts a single row only (no triggers). In this case,
92169 ** instead of using a counter, an exception is thrown immediately if the
92170 ** INSERT violates a foreign key constraint. This is necessary as such
92171 ** an INSERT does not open a statement transaction.
92172 **
92173 ** TODO: How should dropping a table be handled? How should renaming a
92174 ** table be handled?
92175 **
92176 **
92177 ** Query API Notes
92178 ** ---------------
92179 **
92180 ** Before coding an UPDATE or DELETE row operation, the code-generator
92181 ** for those two operations needs to know whether or not the operation
92182 ** requires any FK processing and, if so, which columns of the original
92183 ** row are required by the FK processing VDBE code (i.e. if FKs were
92184 ** implemented using triggers, which of the old.* columns would be
92185 ** accessed). No information is required by the code-generator before
92186 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
92187 ** generation code to query for this information are:
92188 **
92189 **   sqlite3FkRequired() - Test to see if FK processing is required.
92190 **   sqlite3FkOldmask()  - Query for the set of required old.* columns.
92191 **
92192 **
92193 ** Externally accessible module functions
92194 ** --------------------------------------
92195 **
92196 **   sqlite3FkCheck()    - Check for foreign key violations.
92197 **   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
92198 **   sqlite3FkDelete()   - Delete an FKey structure.
92199 */
92200 
92201 /*
92202 ** VDBE Calling Convention
92203 ** -----------------------
92204 **
92205 ** Example:
92206 **
92207 **   For the following INSERT statement:
92208 **
92209 **     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
92210 **     INSERT INTO t1 VALUES(1, 2, 3.1);
92211 **
92212 **   Register (x):        2    (type integer)
92213 **   Register (x+1):      1    (type integer)
92214 **   Register (x+2):      NULL (type NULL)
92215 **   Register (x+3):      3.1  (type real)
92216 */
92217 
92218 /*
92219 ** A foreign key constraint requires that the key columns in the parent
92220 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
92221 ** Given that pParent is the parent table for foreign key constraint pFKey,
92222 ** search the schema for a unique index on the parent key columns.
92223 **
92224 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
92225 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
92226 ** is set to point to the unique index.
92227 **
92228 ** If the parent key consists of a single column (the foreign key constraint
92229 ** is not a composite foreign key), output variable *paiCol is set to NULL.
92230 ** Otherwise, it is set to point to an allocated array of size N, where
92231 ** N is the number of columns in the parent key. The first element of the
92232 ** array is the index of the child table column that is mapped by the FK
92233 ** constraint to the parent table column stored in the left-most column
92234 ** of index *ppIdx. The second element of the array is the index of the
92235 ** child table column that corresponds to the second left-most column of
92236 ** *ppIdx, and so on.
92237 **
92238 ** If the required index cannot be found, either because:
92239 **
92240 **   1) The named parent key columns do not exist, or
92241 **
92242 **   2) The named parent key columns do exist, but are not subject to a
92243 **      UNIQUE or PRIMARY KEY constraint, or
92244 **
92245 **   3) No parent key columns were provided explicitly as part of the
92246 **      foreign key definition, and the parent table does not have a
92247 **      PRIMARY KEY, or
92248 **
92249 **   4) No parent key columns were provided explicitly as part of the
92250 **      foreign key definition, and the PRIMARY KEY of the parent table
92251 **      consists of a a different number of columns to the child key in
92252 **      the child table.
92253 **
92254 ** then non-zero is returned, and a "foreign key mismatch" error loaded
92255 ** into pParse. If an OOM error occurs, non-zero is returned and the
92256 ** pParse->db->mallocFailed flag is set.
92257 */
92258 SQLITE_PRIVATE int sqlite3FkLocateIndex(
92259   Parse *pParse,                  /* Parse context to store any error in */
92260   Table *pParent,                 /* Parent table of FK constraint pFKey */
92261   FKey *pFKey,                    /* Foreign key to find index for */
92262   Index **ppIdx,                  /* OUT: Unique index on parent table */
92263   int **paiCol                    /* OUT: Map of index columns in pFKey */
92264 ){
92265   Index *pIdx = 0;                    /* Value to return via *ppIdx */
92266   int *aiCol = 0;                     /* Value to return via *paiCol */
92267   int nCol = pFKey->nCol;             /* Number of columns in parent key */
92268   char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
92269 
92270   /* The caller is responsible for zeroing output parameters. */
92271   assert( ppIdx && *ppIdx==0 );
92272   assert( !paiCol || *paiCol==0 );
92273   assert( pParse );
92274 
92275   /* If this is a non-composite (single column) foreign key, check if it
92276   ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
92277   ** and *paiCol set to zero and return early.
92278   **
92279   ** Otherwise, for a composite foreign key (more than one column), allocate
92280   ** space for the aiCol array (returned via output parameter *paiCol).
92281   ** Non-composite foreign keys do not require the aiCol array.
92282   */
92283   if( nCol==1 ){
92284     /* The FK maps to the IPK if any of the following are true:
92285     **
92286     **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
92287     **      mapped to the primary key of table pParent, or
92288     **   2) The FK is explicitly mapped to a column declared as INTEGER
92289     **      PRIMARY KEY.
92290     */
92291     if( pParent->iPKey>=0 ){
92292       if( !zKey ) return 0;
92293       if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
92294     }
92295   }else if( paiCol ){
92296     assert( nCol>1 );
92297     aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
92298     if( !aiCol ) return 1;
92299     *paiCol = aiCol;
92300   }
92301 
92302   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
92303     if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){
92304       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
92305       ** of columns. If each indexed column corresponds to a foreign key
92306       ** column of pFKey, then this index is a winner.  */
92307 
92308       if( zKey==0 ){
92309         /* If zKey is NULL, then this foreign key is implicitly mapped to
92310         ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
92311         ** identified by the test (Index.autoIndex==2).  */
92312         if( pIdx->autoIndex==2 ){
92313           if( aiCol ){
92314             int i;
92315             for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
92316           }
92317           break;
92318         }
92319       }else{
92320         /* If zKey is non-NULL, then this foreign key was declared to
92321         ** map to an explicit list of columns in table pParent. Check if this
92322         ** index matches those columns. Also, check that the index uses
92323         ** the default collation sequences for each column. */
92324         int i, j;
92325         for(i=0; i<nCol; i++){
92326           i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
92327           char *zDfltColl;                  /* Def. collation for column */
92328           char *zIdxCol;                    /* Name of indexed column */
92329 
92330           /* If the index uses a collation sequence that is different from
92331           ** the default collation sequence for the column, this index is
92332           ** unusable. Bail out early in this case.  */
92333           zDfltColl = pParent->aCol[iCol].zColl;
92334           if( !zDfltColl ){
92335             zDfltColl = "BINARY";
92336           }
92337           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
92338 
92339           zIdxCol = pParent->aCol[iCol].zName;
92340           for(j=0; j<nCol; j++){
92341             if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
92342               if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
92343               break;
92344             }
92345           }
92346           if( j==nCol ) break;
92347         }
92348         if( i==nCol ) break;      /* pIdx is usable */
92349       }
92350     }
92351   }
92352 
92353   if( !pIdx ){
92354     if( !pParse->disableTriggers ){
92355       sqlite3ErrorMsg(pParse,
92356            "foreign key mismatch - \"%w\" referencing \"%w\"",
92357            pFKey->pFrom->zName, pFKey->zTo);
92358     }
92359     sqlite3DbFree(pParse->db, aiCol);
92360     return 1;
92361   }
92362 
92363   *ppIdx = pIdx;
92364   return 0;
92365 }
92366 
92367 /*
92368 ** This function is called when a row is inserted into or deleted from the
92369 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
92370 ** on the child table of pFKey, this function is invoked twice for each row
92371 ** affected - once to "delete" the old row, and then again to "insert" the
92372 ** new row.
92373 **
92374 ** Each time it is called, this function generates VDBE code to locate the
92375 ** row in the parent table that corresponds to the row being inserted into
92376 ** or deleted from the child table. If the parent row can be found, no
92377 ** special action is taken. Otherwise, if the parent row can *not* be
92378 ** found in the parent table:
92379 **
92380 **   Operation | FK type   | Action taken
92381 **   --------------------------------------------------------------------------
92382 **   INSERT      immediate   Increment the "immediate constraint counter".
92383 **
92384 **   DELETE      immediate   Decrement the "immediate constraint counter".
92385 **
92386 **   INSERT      deferred    Increment the "deferred constraint counter".
92387 **
92388 **   DELETE      deferred    Decrement the "deferred constraint counter".
92389 **
92390 ** These operations are identified in the comment at the top of this file
92391 ** (fkey.c) as "I.1" and "D.1".
92392 */
92393 static void fkLookupParent(
92394   Parse *pParse,        /* Parse context */
92395   int iDb,              /* Index of database housing pTab */
92396   Table *pTab,          /* Parent table of FK pFKey */
92397   Index *pIdx,          /* Unique index on parent key columns in pTab */
92398   FKey *pFKey,          /* Foreign key constraint */
92399   int *aiCol,           /* Map from parent key columns to child table columns */
92400   int regData,          /* Address of array containing child table row */
92401   int nIncr,            /* Increment constraint counter by this */
92402   int isIgnore          /* If true, pretend pTab contains all NULL values */
92403 ){
92404   int i;                                    /* Iterator variable */
92405   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
92406   int iCur = pParse->nTab - 1;              /* Cursor number to use */
92407   int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
92408 
92409   /* If nIncr is less than zero, then check at runtime if there are any
92410   ** outstanding constraints to resolve. If there are not, there is no need
92411   ** to check if deleting this row resolves any outstanding violations.
92412   **
92413   ** Check if any of the key columns in the child table row are NULL. If
92414   ** any are, then the constraint is considered satisfied. No need to
92415   ** search for a matching row in the parent table.  */
92416   if( nIncr<0 ){
92417     sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
92418     VdbeCoverage(v);
92419   }
92420   for(i=0; i<pFKey->nCol; i++){
92421     int iReg = aiCol[i] + regData + 1;
92422     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); VdbeCoverage(v);
92423   }
92424 
92425   if( isIgnore==0 ){
92426     if( pIdx==0 ){
92427       /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
92428       ** column of the parent table (table pTab).  */
92429       int iMustBeInt;               /* Address of MustBeInt instruction */
92430       int regTemp = sqlite3GetTempReg(pParse);
92431 
92432       /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
92433       ** apply the affinity of the parent key). If this fails, then there
92434       ** is no matching parent key. Before using MustBeInt, make a copy of
92435       ** the value. Otherwise, the value inserted into the child key column
92436       ** will have INTEGER affinity applied to it, which may not be correct.  */
92437       sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
92438       iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
92439       VdbeCoverage(v);
92440 
92441       /* If the parent table is the same as the child table, and we are about
92442       ** to increment the constraint-counter (i.e. this is an INSERT operation),
92443       ** then check if the row being inserted matches itself. If so, do not
92444       ** increment the constraint-counter.  */
92445       if( pTab==pFKey->pFrom && nIncr==1 ){
92446         sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); VdbeCoverage(v);
92447         sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
92448       }
92449 
92450       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
92451       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); VdbeCoverage(v);
92452       sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
92453       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
92454       sqlite3VdbeJumpHere(v, iMustBeInt);
92455       sqlite3ReleaseTempReg(pParse, regTemp);
92456     }else{
92457       int nCol = pFKey->nCol;
92458       int regTemp = sqlite3GetTempRange(pParse, nCol);
92459       int regRec = sqlite3GetTempReg(pParse);
92460 
92461       sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
92462       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
92463       for(i=0; i<nCol; i++){
92464         sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
92465       }
92466 
92467       /* If the parent table is the same as the child table, and we are about
92468       ** to increment the constraint-counter (i.e. this is an INSERT operation),
92469       ** then check if the row being inserted matches itself. If so, do not
92470       ** increment the constraint-counter.
92471       **
92472       ** If any of the parent-key values are NULL, then the row cannot match
92473       ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
92474       ** of the parent-key values are NULL (at this point it is known that
92475       ** none of the child key values are).
92476       */
92477       if( pTab==pFKey->pFrom && nIncr==1 ){
92478         int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
92479         for(i=0; i<nCol; i++){
92480           int iChild = aiCol[i]+1+regData;
92481           int iParent = pIdx->aiColumn[i]+1+regData;
92482           assert( aiCol[i]!=pTab->iPKey );
92483           if( pIdx->aiColumn[i]==pTab->iPKey ){
92484             /* The parent key is a composite key that includes the IPK column */
92485             iParent = regData;
92486           }
92487           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); VdbeCoverage(v);
92488           sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
92489         }
92490         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
92491       }
92492 
92493       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTemp, nCol, regRec,
92494                         sqlite3IndexAffinityStr(v,pIdx), nCol);
92495       sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); VdbeCoverage(v);
92496 
92497       sqlite3ReleaseTempReg(pParse, regRec);
92498       sqlite3ReleaseTempRange(pParse, regTemp, nCol);
92499     }
92500   }
92501 
92502   if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
92503    && !pParse->pToplevel
92504    && !pParse->isMultiWrite
92505   ){
92506     /* Special case: If this is an INSERT statement that will insert exactly
92507     ** one row into the table, raise a constraint immediately instead of
92508     ** incrementing a counter. This is necessary as the VM code is being
92509     ** generated for will not open a statement transaction.  */
92510     assert( nIncr==1 );
92511     sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
92512         OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
92513   }else{
92514     if( nIncr>0 && pFKey->isDeferred==0 ){
92515       sqlite3ParseToplevel(pParse)->mayAbort = 1;
92516     }
92517     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
92518   }
92519 
92520   sqlite3VdbeResolveLabel(v, iOk);
92521   sqlite3VdbeAddOp1(v, OP_Close, iCur);
92522 }
92523 
92524 
92525 /*
92526 ** Return an Expr object that refers to a memory register corresponding
92527 ** to column iCol of table pTab.
92528 **
92529 ** regBase is the first of an array of register that contains the data
92530 ** for pTab.  regBase itself holds the rowid.  regBase+1 holds the first
92531 ** column.  regBase+2 holds the second column, and so forth.
92532 */
92533 static Expr *exprTableRegister(
92534   Parse *pParse,     /* Parsing and code generating context */
92535   Table *pTab,       /* The table whose content is at r[regBase]... */
92536   int regBase,       /* Contents of table pTab */
92537   i16 iCol           /* Which column of pTab is desired */
92538 ){
92539   Expr *pExpr;
92540   Column *pCol;
92541   const char *zColl;
92542   sqlite3 *db = pParse->db;
92543 
92544   pExpr = sqlite3Expr(db, TK_REGISTER, 0);
92545   if( pExpr ){
92546     if( iCol>=0 && iCol!=pTab->iPKey ){
92547       pCol = &pTab->aCol[iCol];
92548       pExpr->iTable = regBase + iCol + 1;
92549       pExpr->affinity = pCol->affinity;
92550       zColl = pCol->zColl;
92551       if( zColl==0 ) zColl = db->pDfltColl->zName;
92552       pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
92553     }else{
92554       pExpr->iTable = regBase;
92555       pExpr->affinity = SQLITE_AFF_INTEGER;
92556     }
92557   }
92558   return pExpr;
92559 }
92560 
92561 /*
92562 ** Return an Expr object that refers to column iCol of table pTab which
92563 ** has cursor iCur.
92564 */
92565 static Expr *exprTableColumn(
92566   sqlite3 *db,      /* The database connection */
92567   Table *pTab,      /* The table whose column is desired */
92568   int iCursor,      /* The open cursor on the table */
92569   i16 iCol          /* The column that is wanted */
92570 ){
92571   Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
92572   if( pExpr ){
92573     pExpr->pTab = pTab;
92574     pExpr->iTable = iCursor;
92575     pExpr->iColumn = iCol;
92576   }
92577   return pExpr;
92578 }
92579 
92580 /*
92581 ** This function is called to generate code executed when a row is deleted
92582 ** from the parent table of foreign key constraint pFKey and, if pFKey is
92583 ** deferred, when a row is inserted into the same table. When generating
92584 ** code for an SQL UPDATE operation, this function may be called twice -
92585 ** once to "delete" the old row and once to "insert" the new row.
92586 **
92587 ** The code generated by this function scans through the rows in the child
92588 ** table that correspond to the parent table row being deleted or inserted.
92589 ** For each child row found, one of the following actions is taken:
92590 **
92591 **   Operation | FK type   | Action taken
92592 **   --------------------------------------------------------------------------
92593 **   DELETE      immediate   Increment the "immediate constraint counter".
92594 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
92595 **                           throw a "FOREIGN KEY constraint failed" exception.
92596 **
92597 **   INSERT      immediate   Decrement the "immediate constraint counter".
92598 **
92599 **   DELETE      deferred    Increment the "deferred constraint counter".
92600 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
92601 **                           throw a "FOREIGN KEY constraint failed" exception.
92602 **
92603 **   INSERT      deferred    Decrement the "deferred constraint counter".
92604 **
92605 ** These operations are identified in the comment at the top of this file
92606 ** (fkey.c) as "I.2" and "D.2".
92607 */
92608 static void fkScanChildren(
92609   Parse *pParse,                  /* Parse context */
92610   SrcList *pSrc,                  /* The child table to be scanned */
92611   Table *pTab,                    /* The parent table */
92612   Index *pIdx,                    /* Index on parent covering the foreign key */
92613   FKey *pFKey,                    /* The foreign key linking pSrc to pTab */
92614   int *aiCol,                     /* Map from pIdx cols to child table cols */
92615   int regData,                    /* Parent row data starts here */
92616   int nIncr                       /* Amount to increment deferred counter by */
92617 ){
92618   sqlite3 *db = pParse->db;       /* Database handle */
92619   int i;                          /* Iterator variable */
92620   Expr *pWhere = 0;               /* WHERE clause to scan with */
92621   NameContext sNameContext;       /* Context used to resolve WHERE clause */
92622   WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
92623   int iFkIfZero = 0;              /* Address of OP_FkIfZero */
92624   Vdbe *v = sqlite3GetVdbe(pParse);
92625 
92626   assert( pIdx==0 || pIdx->pTable==pTab );
92627   assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
92628   assert( pIdx!=0 || pFKey->nCol==1 );
92629   assert( pIdx!=0 || HasRowid(pTab) );
92630 
92631   if( nIncr<0 ){
92632     iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
92633     VdbeCoverage(v);
92634   }
92635 
92636   /* Create an Expr object representing an SQL expression like:
92637   **
92638   **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
92639   **
92640   ** The collation sequence used for the comparison should be that of
92641   ** the parent key columns. The affinity of the parent key column should
92642   ** be applied to each child key value before the comparison takes place.
92643   */
92644   for(i=0; i<pFKey->nCol; i++){
92645     Expr *pLeft;                  /* Value from parent table row */
92646     Expr *pRight;                 /* Column ref to child table */
92647     Expr *pEq;                    /* Expression (pLeft = pRight) */
92648     i16 iCol;                     /* Index of column in child table */
92649     const char *zCol;             /* Name of column in child table */
92650 
92651     iCol = pIdx ? pIdx->aiColumn[i] : -1;
92652     pLeft = exprTableRegister(pParse, pTab, regData, iCol);
92653     iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
92654     assert( iCol>=0 );
92655     zCol = pFKey->pFrom->aCol[iCol].zName;
92656     pRight = sqlite3Expr(db, TK_ID, zCol);
92657     pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
92658     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
92659   }
92660 
92661   /* If the child table is the same as the parent table, then add terms
92662   ** to the WHERE clause that prevent this entry from being scanned.
92663   ** The added WHERE clause terms are like this:
92664   **
92665   **     $current_rowid!=rowid
92666   **     NOT( $current_a==a AND $current_b==b AND ... )
92667   **
92668   ** The first form is used for rowid tables.  The second form is used
92669   ** for WITHOUT ROWID tables.  In the second form, the primary key is
92670   ** (a,b,...)
92671   */
92672   if( pTab==pFKey->pFrom && nIncr>0 ){
92673     Expr *pNe;                    /* Expression (pLeft != pRight) */
92674     Expr *pLeft;                  /* Value from parent table row */
92675     Expr *pRight;                 /* Column ref to child table */
92676     if( HasRowid(pTab) ){
92677       pLeft = exprTableRegister(pParse, pTab, regData, -1);
92678       pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
92679       pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
92680     }else{
92681       Expr *pEq, *pAll = 0;
92682       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
92683       assert( pIdx!=0 );
92684       for(i=0; i<pPk->nKeyCol; i++){
92685         i16 iCol = pIdx->aiColumn[i];
92686         pLeft = exprTableRegister(pParse, pTab, regData, iCol);
92687         pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
92688         pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
92689         pAll = sqlite3ExprAnd(db, pAll, pEq);
92690       }
92691       pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
92692     }
92693     pWhere = sqlite3ExprAnd(db, pWhere, pNe);
92694   }
92695 
92696   /* Resolve the references in the WHERE clause. */
92697   memset(&sNameContext, 0, sizeof(NameContext));
92698   sNameContext.pSrcList = pSrc;
92699   sNameContext.pParse = pParse;
92700   sqlite3ResolveExprNames(&sNameContext, pWhere);
92701 
92702   /* Create VDBE to loop through the entries in pSrc that match the WHERE
92703   ** clause. If the constraint is not deferred, throw an exception for
92704   ** each row found. Otherwise, for deferred constraints, increment the
92705   ** deferred constraint counter by nIncr for each row selected.  */
92706   pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
92707   if( nIncr>0 && pFKey->isDeferred==0 ){
92708     sqlite3ParseToplevel(pParse)->mayAbort = 1;
92709   }
92710   sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
92711   if( pWInfo ){
92712     sqlite3WhereEnd(pWInfo);
92713   }
92714 
92715   /* Clean up the WHERE clause constructed above. */
92716   sqlite3ExprDelete(db, pWhere);
92717   if( iFkIfZero ){
92718     sqlite3VdbeJumpHere(v, iFkIfZero);
92719   }
92720 }
92721 
92722 /*
92723 ** This function returns a linked list of FKey objects (connected by
92724 ** FKey.pNextTo) holding all children of table pTab.  For example,
92725 ** given the following schema:
92726 **
92727 **   CREATE TABLE t1(a PRIMARY KEY);
92728 **   CREATE TABLE t2(b REFERENCES t1(a);
92729 **
92730 ** Calling this function with table "t1" as an argument returns a pointer
92731 ** to the FKey structure representing the foreign key constraint on table
92732 ** "t2". Calling this function with "t2" as the argument would return a
92733 ** NULL pointer (as there are no FK constraints for which t2 is the parent
92734 ** table).
92735 */
92736 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
92737   int nName = sqlite3Strlen30(pTab->zName);
92738   return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
92739 }
92740 
92741 /*
92742 ** The second argument is a Trigger structure allocated by the
92743 ** fkActionTrigger() routine. This function deletes the Trigger structure
92744 ** and all of its sub-components.
92745 **
92746 ** The Trigger structure or any of its sub-components may be allocated from
92747 ** the lookaside buffer belonging to database handle dbMem.
92748 */
92749 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
92750   if( p ){
92751     TriggerStep *pStep = p->step_list;
92752     sqlite3ExprDelete(dbMem, pStep->pWhere);
92753     sqlite3ExprListDelete(dbMem, pStep->pExprList);
92754     sqlite3SelectDelete(dbMem, pStep->pSelect);
92755     sqlite3ExprDelete(dbMem, p->pWhen);
92756     sqlite3DbFree(dbMem, p);
92757   }
92758 }
92759 
92760 /*
92761 ** This function is called to generate code that runs when table pTab is
92762 ** being dropped from the database. The SrcList passed as the second argument
92763 ** to this function contains a single entry guaranteed to resolve to
92764 ** table pTab.
92765 **
92766 ** Normally, no code is required. However, if either
92767 **
92768 **   (a) The table is the parent table of a FK constraint, or
92769 **   (b) The table is the child table of a deferred FK constraint and it is
92770 **       determined at runtime that there are outstanding deferred FK
92771 **       constraint violations in the database,
92772 **
92773 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
92774 ** the table from the database. Triggers are disabled while running this
92775 ** DELETE, but foreign key actions are not.
92776 */
92777 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
92778   sqlite3 *db = pParse->db;
92779   if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
92780     int iSkip = 0;
92781     Vdbe *v = sqlite3GetVdbe(pParse);
92782 
92783     assert( v );                  /* VDBE has already been allocated */
92784     if( sqlite3FkReferences(pTab)==0 ){
92785       /* Search for a deferred foreign key constraint for which this table
92786       ** is the child table. If one cannot be found, return without
92787       ** generating any VDBE code. If one can be found, then jump over
92788       ** the entire DELETE if there are no outstanding deferred constraints
92789       ** when this statement is run.  */
92790       FKey *p;
92791       for(p=pTab->pFKey; p; p=p->pNextFrom){
92792         if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
92793       }
92794       if( !p ) return;
92795       iSkip = sqlite3VdbeMakeLabel(v);
92796       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); VdbeCoverage(v);
92797     }
92798 
92799     pParse->disableTriggers = 1;
92800     sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
92801     pParse->disableTriggers = 0;
92802 
92803     /* If the DELETE has generated immediate foreign key constraint
92804     ** violations, halt the VDBE and return an error at this point, before
92805     ** any modifications to the schema are made. This is because statement
92806     ** transactions are not able to rollback schema changes.
92807     **
92808     ** If the SQLITE_DeferFKs flag is set, then this is not required, as
92809     ** the statement transaction will not be rolled back even if FK
92810     ** constraints are violated.
92811     */
92812     if( (db->flags & SQLITE_DeferFKs)==0 ){
92813       sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
92814       VdbeCoverage(v);
92815       sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
92816           OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
92817     }
92818 
92819     if( iSkip ){
92820       sqlite3VdbeResolveLabel(v, iSkip);
92821     }
92822   }
92823 }
92824 
92825 
92826 /*
92827 ** The second argument points to an FKey object representing a foreign key
92828 ** for which pTab is the child table. An UPDATE statement against pTab
92829 ** is currently being processed. For each column of the table that is
92830 ** actually updated, the corresponding element in the aChange[] array
92831 ** is zero or greater (if a column is unmodified the corresponding element
92832 ** is set to -1). If the rowid column is modified by the UPDATE statement
92833 ** the bChngRowid argument is non-zero.
92834 **
92835 ** This function returns true if any of the columns that are part of the
92836 ** child key for FK constraint *p are modified.
92837 */
92838 static int fkChildIsModified(
92839   Table *pTab,                    /* Table being updated */
92840   FKey *p,                        /* Foreign key for which pTab is the child */
92841   int *aChange,                   /* Array indicating modified columns */
92842   int bChngRowid                  /* True if rowid is modified by this update */
92843 ){
92844   int i;
92845   for(i=0; i<p->nCol; i++){
92846     int iChildKey = p->aCol[i].iFrom;
92847     if( aChange[iChildKey]>=0 ) return 1;
92848     if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
92849   }
92850   return 0;
92851 }
92852 
92853 /*
92854 ** The second argument points to an FKey object representing a foreign key
92855 ** for which pTab is the parent table. An UPDATE statement against pTab
92856 ** is currently being processed. For each column of the table that is
92857 ** actually updated, the corresponding element in the aChange[] array
92858 ** is zero or greater (if a column is unmodified the corresponding element
92859 ** is set to -1). If the rowid column is modified by the UPDATE statement
92860 ** the bChngRowid argument is non-zero.
92861 **
92862 ** This function returns true if any of the columns that are part of the
92863 ** parent key for FK constraint *p are modified.
92864 */
92865 static int fkParentIsModified(
92866   Table *pTab,
92867   FKey *p,
92868   int *aChange,
92869   int bChngRowid
92870 ){
92871   int i;
92872   for(i=0; i<p->nCol; i++){
92873     char *zKey = p->aCol[i].zCol;
92874     int iKey;
92875     for(iKey=0; iKey<pTab->nCol; iKey++){
92876       if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
92877         Column *pCol = &pTab->aCol[iKey];
92878         if( zKey ){
92879           if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
92880         }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
92881           return 1;
92882         }
92883       }
92884     }
92885   }
92886   return 0;
92887 }
92888 
92889 /*
92890 ** This function is called when inserting, deleting or updating a row of
92891 ** table pTab to generate VDBE code to perform foreign key constraint
92892 ** processing for the operation.
92893 **
92894 ** For a DELETE operation, parameter regOld is passed the index of the
92895 ** first register in an array of (pTab->nCol+1) registers containing the
92896 ** rowid of the row being deleted, followed by each of the column values
92897 ** of the row being deleted, from left to right. Parameter regNew is passed
92898 ** zero in this case.
92899 **
92900 ** For an INSERT operation, regOld is passed zero and regNew is passed the
92901 ** first register of an array of (pTab->nCol+1) registers containing the new
92902 ** row data.
92903 **
92904 ** For an UPDATE operation, this function is called twice. Once before
92905 ** the original record is deleted from the table using the calling convention
92906 ** described for DELETE. Then again after the original record is deleted
92907 ** but before the new record is inserted using the INSERT convention.
92908 */
92909 SQLITE_PRIVATE void sqlite3FkCheck(
92910   Parse *pParse,                  /* Parse context */
92911   Table *pTab,                    /* Row is being deleted from this table */
92912   int regOld,                     /* Previous row data is stored here */
92913   int regNew,                     /* New row data is stored here */
92914   int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
92915   int bChngRowid                  /* True if rowid is UPDATEd */
92916 ){
92917   sqlite3 *db = pParse->db;       /* Database handle */
92918   FKey *pFKey;                    /* Used to iterate through FKs */
92919   int iDb;                        /* Index of database containing pTab */
92920   const char *zDb;                /* Name of database containing pTab */
92921   int isIgnoreErrors = pParse->disableTriggers;
92922 
92923   /* Exactly one of regOld and regNew should be non-zero. */
92924   assert( (regOld==0)!=(regNew==0) );
92925 
92926   /* If foreign-keys are disabled, this function is a no-op. */
92927   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
92928 
92929   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
92930   zDb = db->aDb[iDb].zName;
92931 
92932   /* Loop through all the foreign key constraints for which pTab is the
92933   ** child table (the table that the foreign key definition is part of).  */
92934   for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
92935     Table *pTo;                   /* Parent table of foreign key pFKey */
92936     Index *pIdx = 0;              /* Index on key columns in pTo */
92937     int *aiFree = 0;
92938     int *aiCol;
92939     int iCol;
92940     int i;
92941     int isIgnore = 0;
92942 
92943     if( aChange
92944      && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
92945      && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
92946     ){
92947       continue;
92948     }
92949 
92950     /* Find the parent table of this foreign key. Also find a unique index
92951     ** on the parent key columns in the parent table. If either of these
92952     ** schema items cannot be located, set an error in pParse and return
92953     ** early.  */
92954     if( pParse->disableTriggers ){
92955       pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
92956     }else{
92957       pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
92958     }
92959     if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
92960       assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
92961       if( !isIgnoreErrors || db->mallocFailed ) return;
92962       if( pTo==0 ){
92963         /* If isIgnoreErrors is true, then a table is being dropped. In this
92964         ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
92965         ** before actually dropping it in order to check FK constraints.
92966         ** If the parent table of an FK constraint on the current table is
92967         ** missing, behave as if it is empty. i.e. decrement the relevant
92968         ** FK counter for each row of the current table with non-NULL keys.
92969         */
92970         Vdbe *v = sqlite3GetVdbe(pParse);
92971         int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
92972         for(i=0; i<pFKey->nCol; i++){
92973           int iReg = pFKey->aCol[i].iFrom + regOld + 1;
92974           sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump); VdbeCoverage(v);
92975         }
92976         sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
92977       }
92978       continue;
92979     }
92980     assert( pFKey->nCol==1 || (aiFree && pIdx) );
92981 
92982     if( aiFree ){
92983       aiCol = aiFree;
92984     }else{
92985       iCol = pFKey->aCol[0].iFrom;
92986       aiCol = &iCol;
92987     }
92988     for(i=0; i<pFKey->nCol; i++){
92989       if( aiCol[i]==pTab->iPKey ){
92990         aiCol[i] = -1;
92991       }
92992 #ifndef SQLITE_OMIT_AUTHORIZATION
92993       /* Request permission to read the parent key columns. If the
92994       ** authorization callback returns SQLITE_IGNORE, behave as if any
92995       ** values read from the parent table are NULL. */
92996       if( db->xAuth ){
92997         int rcauth;
92998         char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
92999         rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
93000         isIgnore = (rcauth==SQLITE_IGNORE);
93001       }
93002 #endif
93003     }
93004 
93005     /* Take a shared-cache advisory read-lock on the parent table. Allocate
93006     ** a cursor to use to search the unique index on the parent key columns
93007     ** in the parent table.  */
93008     sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
93009     pParse->nTab++;
93010 
93011     if( regOld!=0 ){
93012       /* A row is being removed from the child table. Search for the parent.
93013       ** If the parent does not exist, removing the child row resolves an
93014       ** outstanding foreign key constraint violation. */
93015       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
93016     }
93017     if( regNew!=0 ){
93018       /* A row is being added to the child table. If a parent row cannot
93019       ** be found, adding the child row has violated the FK constraint. */
93020       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
93021     }
93022 
93023     sqlite3DbFree(db, aiFree);
93024   }
93025 
93026   /* Loop through all the foreign key constraints that refer to this table.
93027   ** (the "child" constraints) */
93028   for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
93029     Index *pIdx = 0;              /* Foreign key index for pFKey */
93030     SrcList *pSrc;
93031     int *aiCol = 0;
93032 
93033     if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
93034       continue;
93035     }
93036 
93037     if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
93038      && !pParse->pToplevel && !pParse->isMultiWrite
93039     ){
93040       assert( regOld==0 && regNew!=0 );
93041       /* Inserting a single row into a parent table cannot cause an immediate
93042       ** foreign key violation. So do nothing in this case.  */
93043       continue;
93044     }
93045 
93046     if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
93047       if( !isIgnoreErrors || db->mallocFailed ) return;
93048       continue;
93049     }
93050     assert( aiCol || pFKey->nCol==1 );
93051 
93052     /* Create a SrcList structure containing the child table.  We need the
93053     ** child table as a SrcList for sqlite3WhereBegin() */
93054     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
93055     if( pSrc ){
93056       struct SrcList_item *pItem = pSrc->a;
93057       pItem->pTab = pFKey->pFrom;
93058       pItem->zName = pFKey->pFrom->zName;
93059       pItem->pTab->nRef++;
93060       pItem->iCursor = pParse->nTab++;
93061 
93062       if( regNew!=0 ){
93063         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
93064       }
93065       if( regOld!=0 ){
93066         /* If there is a RESTRICT action configured for the current operation
93067         ** on the parent table of this FK, then throw an exception
93068         ** immediately if the FK constraint is violated, even if this is a
93069         ** deferred trigger. That's what RESTRICT means. To defer checking
93070         ** the constraint, the FK should specify NO ACTION (represented
93071         ** using OE_None). NO ACTION is the default.  */
93072         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
93073       }
93074       pItem->zName = 0;
93075       sqlite3SrcListDelete(db, pSrc);
93076     }
93077     sqlite3DbFree(db, aiCol);
93078   }
93079 }
93080 
93081 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
93082 
93083 /*
93084 ** This function is called before generating code to update or delete a
93085 ** row contained in table pTab.
93086 */
93087 SQLITE_PRIVATE u32 sqlite3FkOldmask(
93088   Parse *pParse,                  /* Parse context */
93089   Table *pTab                     /* Table being modified */
93090 ){
93091   u32 mask = 0;
93092   if( pParse->db->flags&SQLITE_ForeignKeys ){
93093     FKey *p;
93094     int i;
93095     for(p=pTab->pFKey; p; p=p->pNextFrom){
93096       for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
93097     }
93098     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
93099       Index *pIdx = 0;
93100       sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
93101       if( pIdx ){
93102         for(i=0; i<pIdx->nKeyCol; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
93103       }
93104     }
93105   }
93106   return mask;
93107 }
93108 
93109 
93110 /*
93111 ** This function is called before generating code to update or delete a
93112 ** row contained in table pTab. If the operation is a DELETE, then
93113 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
93114 ** to an array of size N, where N is the number of columns in table pTab.
93115 ** If the i'th column is not modified by the UPDATE, then the corresponding
93116 ** entry in the aChange[] array is set to -1. If the column is modified,
93117 ** the value is 0 or greater. Parameter chngRowid is set to true if the
93118 ** UPDATE statement modifies the rowid fields of the table.
93119 **
93120 ** If any foreign key processing will be required, this function returns
93121 ** true. If there is no foreign key related processing, this function
93122 ** returns false.
93123 */
93124 SQLITE_PRIVATE int sqlite3FkRequired(
93125   Parse *pParse,                  /* Parse context */
93126   Table *pTab,                    /* Table being modified */
93127   int *aChange,                   /* Non-NULL for UPDATE operations */
93128   int chngRowid                   /* True for UPDATE that affects rowid */
93129 ){
93130   if( pParse->db->flags&SQLITE_ForeignKeys ){
93131     if( !aChange ){
93132       /* A DELETE operation. Foreign key processing is required if the
93133       ** table in question is either the child or parent table for any
93134       ** foreign key constraint.  */
93135       return (sqlite3FkReferences(pTab) || pTab->pFKey);
93136     }else{
93137       /* This is an UPDATE. Foreign key processing is only required if the
93138       ** operation modifies one or more child or parent key columns. */
93139       FKey *p;
93140 
93141       /* Check if any child key columns are being modified. */
93142       for(p=pTab->pFKey; p; p=p->pNextFrom){
93143         if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1;
93144       }
93145 
93146       /* Check if any parent key columns are being modified. */
93147       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
93148         if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1;
93149       }
93150     }
93151   }
93152   return 0;
93153 }
93154 
93155 /*
93156 ** This function is called when an UPDATE or DELETE operation is being
93157 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
93158 ** If the current operation is an UPDATE, then the pChanges parameter is
93159 ** passed a pointer to the list of columns being modified. If it is a
93160 ** DELETE, pChanges is passed a NULL pointer.
93161 **
93162 ** It returns a pointer to a Trigger structure containing a trigger
93163 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
93164 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
93165 ** returned (these actions require no special handling by the triggers
93166 ** sub-system, code for them is created by fkScanChildren()).
93167 **
93168 ** For example, if pFKey is the foreign key and pTab is table "p" in
93169 ** the following schema:
93170 **
93171 **   CREATE TABLE p(pk PRIMARY KEY);
93172 **   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
93173 **
93174 ** then the returned trigger structure is equivalent to:
93175 **
93176 **   CREATE TRIGGER ... DELETE ON p BEGIN
93177 **     DELETE FROM c WHERE ck = old.pk;
93178 **   END;
93179 **
93180 ** The returned pointer is cached as part of the foreign key object. It
93181 ** is eventually freed along with the rest of the foreign key object by
93182 ** sqlite3FkDelete().
93183 */
93184 static Trigger *fkActionTrigger(
93185   Parse *pParse,                  /* Parse context */
93186   Table *pTab,                    /* Table being updated or deleted from */
93187   FKey *pFKey,                    /* Foreign key to get action for */
93188   ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
93189 ){
93190   sqlite3 *db = pParse->db;       /* Database handle */
93191   int action;                     /* One of OE_None, OE_Cascade etc. */
93192   Trigger *pTrigger;              /* Trigger definition to return */
93193   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
93194 
93195   action = pFKey->aAction[iAction];
93196   pTrigger = pFKey->apTrigger[iAction];
93197 
93198   if( action!=OE_None && !pTrigger ){
93199     u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
93200     char const *zFrom;            /* Name of child table */
93201     int nFrom;                    /* Length in bytes of zFrom */
93202     Index *pIdx = 0;              /* Parent key index for this FK */
93203     int *aiCol = 0;               /* child table cols -> parent key cols */
93204     TriggerStep *pStep = 0;        /* First (only) step of trigger program */
93205     Expr *pWhere = 0;             /* WHERE clause of trigger step */
93206     ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
93207     Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
93208     int i;                        /* Iterator variable */
93209     Expr *pWhen = 0;              /* WHEN clause for the trigger */
93210 
93211     if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
93212     assert( aiCol || pFKey->nCol==1 );
93213 
93214     for(i=0; i<pFKey->nCol; i++){
93215       Token tOld = { "old", 3 };  /* Literal "old" token */
93216       Token tNew = { "new", 3 };  /* Literal "new" token */
93217       Token tFromCol;             /* Name of column in child table */
93218       Token tToCol;               /* Name of column in parent table */
93219       int iFromCol;               /* Idx of column in child table */
93220       Expr *pEq;                  /* tFromCol = OLD.tToCol */
93221 
93222       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
93223       assert( iFromCol>=0 );
93224       tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
93225       tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
93226 
93227       tToCol.n = sqlite3Strlen30(tToCol.z);
93228       tFromCol.n = sqlite3Strlen30(tFromCol.z);
93229 
93230       /* Create the expression "OLD.zToCol = zFromCol". It is important
93231       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
93232       ** that the affinity and collation sequence associated with the
93233       ** parent table are used for the comparison. */
93234       pEq = sqlite3PExpr(pParse, TK_EQ,
93235           sqlite3PExpr(pParse, TK_DOT,
93236             sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
93237             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
93238           , 0),
93239           sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
93240       , 0);
93241       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
93242 
93243       /* For ON UPDATE, construct the next term of the WHEN clause.
93244       ** The final WHEN clause will be like this:
93245       **
93246       **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
93247       */
93248       if( pChanges ){
93249         pEq = sqlite3PExpr(pParse, TK_IS,
93250             sqlite3PExpr(pParse, TK_DOT,
93251               sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
93252               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
93253               0),
93254             sqlite3PExpr(pParse, TK_DOT,
93255               sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
93256               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
93257               0),
93258             0);
93259         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
93260       }
93261 
93262       if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
93263         Expr *pNew;
93264         if( action==OE_Cascade ){
93265           pNew = sqlite3PExpr(pParse, TK_DOT,
93266             sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
93267             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
93268           , 0);
93269         }else if( action==OE_SetDflt ){
93270           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
93271           if( pDflt ){
93272             pNew = sqlite3ExprDup(db, pDflt, 0);
93273           }else{
93274             pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
93275           }
93276         }else{
93277           pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
93278         }
93279         pList = sqlite3ExprListAppend(pParse, pList, pNew);
93280         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
93281       }
93282     }
93283     sqlite3DbFree(db, aiCol);
93284 
93285     zFrom = pFKey->pFrom->zName;
93286     nFrom = sqlite3Strlen30(zFrom);
93287 
93288     if( action==OE_Restrict ){
93289       Token tFrom;
93290       Expr *pRaise;
93291 
93292       tFrom.z = zFrom;
93293       tFrom.n = nFrom;
93294       pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
93295       if( pRaise ){
93296         pRaise->affinity = OE_Abort;
93297       }
93298       pSelect = sqlite3SelectNew(pParse,
93299           sqlite3ExprListAppend(pParse, 0, pRaise),
93300           sqlite3SrcListAppend(db, 0, &tFrom, 0),
93301           pWhere,
93302           0, 0, 0, 0, 0, 0
93303       );
93304       pWhere = 0;
93305     }
93306 
93307     /* Disable lookaside memory allocation */
93308     enableLookaside = db->lookaside.bEnabled;
93309     db->lookaside.bEnabled = 0;
93310 
93311     pTrigger = (Trigger *)sqlite3DbMallocZero(db,
93312         sizeof(Trigger) +         /* struct Trigger */
93313         sizeof(TriggerStep) +     /* Single step in trigger program */
93314         nFrom + 1                 /* Space for pStep->target.z */
93315     );
93316     if( pTrigger ){
93317       pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
93318       pStep->target.z = (char *)&pStep[1];
93319       pStep->target.n = nFrom;
93320       memcpy((char *)pStep->target.z, zFrom, nFrom);
93321 
93322       pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
93323       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
93324       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
93325       if( pWhen ){
93326         pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
93327         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
93328       }
93329     }
93330 
93331     /* Re-enable the lookaside buffer, if it was disabled earlier. */
93332     db->lookaside.bEnabled = enableLookaside;
93333 
93334     sqlite3ExprDelete(db, pWhere);
93335     sqlite3ExprDelete(db, pWhen);
93336     sqlite3ExprListDelete(db, pList);
93337     sqlite3SelectDelete(db, pSelect);
93338     if( db->mallocFailed==1 ){
93339       fkTriggerDelete(db, pTrigger);
93340       return 0;
93341     }
93342     assert( pStep!=0 );
93343 
93344     switch( action ){
93345       case OE_Restrict:
93346         pStep->op = TK_SELECT;
93347         break;
93348       case OE_Cascade:
93349         if( !pChanges ){
93350           pStep->op = TK_DELETE;
93351           break;
93352         }
93353       default:
93354         pStep->op = TK_UPDATE;
93355     }
93356     pStep->pTrig = pTrigger;
93357     pTrigger->pSchema = pTab->pSchema;
93358     pTrigger->pTabSchema = pTab->pSchema;
93359     pFKey->apTrigger[iAction] = pTrigger;
93360     pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
93361   }
93362 
93363   return pTrigger;
93364 }
93365 
93366 /*
93367 ** This function is called when deleting or updating a row to implement
93368 ** any required CASCADE, SET NULL or SET DEFAULT actions.
93369 */
93370 SQLITE_PRIVATE void sqlite3FkActions(
93371   Parse *pParse,                  /* Parse context */
93372   Table *pTab,                    /* Table being updated or deleted from */
93373   ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
93374   int regOld,                     /* Address of array containing old row */
93375   int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
93376   int bChngRowid                  /* True if rowid is UPDATEd */
93377 ){
93378   /* If foreign-key support is enabled, iterate through all FKs that
93379   ** refer to table pTab. If there is an action associated with the FK
93380   ** for this operation (either update or delete), invoke the associated
93381   ** trigger sub-program.  */
93382   if( pParse->db->flags&SQLITE_ForeignKeys ){
93383     FKey *pFKey;                  /* Iterator variable */
93384     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
93385       if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
93386         Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
93387         if( pAct ){
93388           sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
93389         }
93390       }
93391     }
93392   }
93393 }
93394 
93395 #endif /* ifndef SQLITE_OMIT_TRIGGER */
93396 
93397 /*
93398 ** Free all memory associated with foreign key definitions attached to
93399 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
93400 ** hash table.
93401 */
93402 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
93403   FKey *pFKey;                    /* Iterator variable */
93404   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
93405 
93406   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
93407   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
93408 
93409     /* Remove the FK from the fkeyHash hash table. */
93410     if( !db || db->pnBytesFreed==0 ){
93411       if( pFKey->pPrevTo ){
93412         pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
93413       }else{
93414         void *p = (void *)pFKey->pNextTo;
93415         const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
93416         sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
93417       }
93418       if( pFKey->pNextTo ){
93419         pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
93420       }
93421     }
93422 
93423     /* EV: R-30323-21917 Each foreign key constraint in SQLite is
93424     ** classified as either immediate or deferred.
93425     */
93426     assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
93427 
93428     /* Delete any triggers created to implement actions for this FK. */
93429 #ifndef SQLITE_OMIT_TRIGGER
93430     fkTriggerDelete(db, pFKey->apTrigger[0]);
93431     fkTriggerDelete(db, pFKey->apTrigger[1]);
93432 #endif
93433 
93434     pNext = pFKey->pNextFrom;
93435     sqlite3DbFree(db, pFKey);
93436   }
93437 }
93438 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
93439 
93440 /************** End of fkey.c ************************************************/
93441 /************** Begin file insert.c ******************************************/
93442 /*
93443 ** 2001 September 15
93444 **
93445 ** The author disclaims copyright to this source code.  In place of
93446 ** a legal notice, here is a blessing:
93447 **
93448 **    May you do good and not evil.
93449 **    May you find forgiveness for yourself and forgive others.
93450 **    May you share freely, never taking more than you give.
93451 **
93452 *************************************************************************
93453 ** This file contains C code routines that are called by the parser
93454 ** to handle INSERT statements in SQLite.
93455 */
93456 
93457 /*
93458 ** Generate code that will
93459 **
93460 **   (1) acquire a lock for table pTab then
93461 **   (2) open pTab as cursor iCur.
93462 **
93463 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
93464 ** for that table that is actually opened.
93465 */
93466 SQLITE_PRIVATE void sqlite3OpenTable(
93467   Parse *pParse,  /* Generate code into this VDBE */
93468   int iCur,       /* The cursor number of the table */
93469   int iDb,        /* The database index in sqlite3.aDb[] */
93470   Table *pTab,    /* The table to be opened */
93471   int opcode      /* OP_OpenRead or OP_OpenWrite */
93472 ){
93473   Vdbe *v;
93474   assert( !IsVirtual(pTab) );
93475   v = sqlite3GetVdbe(pParse);
93476   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
93477   sqlite3TableLock(pParse, iDb, pTab->tnum,
93478                    (opcode==OP_OpenWrite)?1:0, pTab->zName);
93479   if( HasRowid(pTab) ){
93480     sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
93481     VdbeComment((v, "%s", pTab->zName));
93482   }else{
93483     Index *pPk = sqlite3PrimaryKeyIndex(pTab);
93484     assert( pPk!=0 );
93485     assert( pPk->tnum=pTab->tnum );
93486     sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
93487     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
93488     VdbeComment((v, "%s", pTab->zName));
93489   }
93490 }
93491 
93492 /*
93493 ** Return a pointer to the column affinity string associated with index
93494 ** pIdx. A column affinity string has one character for each column in
93495 ** the table, according to the affinity of the column:
93496 **
93497 **  Character      Column affinity
93498 **  ------------------------------
93499 **  'a'            TEXT
93500 **  'b'            NONE
93501 **  'c'            NUMERIC
93502 **  'd'            INTEGER
93503 **  'e'            REAL
93504 **
93505 ** An extra 'd' is appended to the end of the string to cover the
93506 ** rowid that appears as the last column in every index.
93507 **
93508 ** Memory for the buffer containing the column index affinity string
93509 ** is managed along with the rest of the Index structure. It will be
93510 ** released when sqlite3DeleteIndex() is called.
93511 */
93512 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
93513   if( !pIdx->zColAff ){
93514     /* The first time a column affinity string for a particular index is
93515     ** required, it is allocated and populated here. It is then stored as
93516     ** a member of the Index structure for subsequent use.
93517     **
93518     ** The column affinity string will eventually be deleted by
93519     ** sqliteDeleteIndex() when the Index structure itself is cleaned
93520     ** up.
93521     */
93522     int n;
93523     Table *pTab = pIdx->pTable;
93524     sqlite3 *db = sqlite3VdbeDb(v);
93525     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
93526     if( !pIdx->zColAff ){
93527       db->mallocFailed = 1;
93528       return 0;
93529     }
93530     for(n=0; n<pIdx->nColumn; n++){
93531       i16 x = pIdx->aiColumn[n];
93532       pIdx->zColAff[n] = x<0 ? SQLITE_AFF_INTEGER : pTab->aCol[x].affinity;
93533     }
93534     pIdx->zColAff[n] = 0;
93535   }
93536 
93537   return pIdx->zColAff;
93538 }
93539 
93540 /*
93541 ** Compute the affinity string for table pTab, if it has not already been
93542 ** computed.  As an optimization, omit trailing SQLITE_AFF_NONE affinities.
93543 **
93544 ** If the affinity exists (if it is no entirely SQLITE_AFF_NONE values) and
93545 ** if iReg>0 then code an OP_Affinity opcode that will set the affinities
93546 ** for register iReg and following.  Or if affinities exists and iReg==0,
93547 ** then just set the P4 operand of the previous opcode (which should  be
93548 ** an OP_MakeRecord) to the affinity string.
93549 **
93550 ** A column affinity string has one character per column:
93551 **
93552 **  Character      Column affinity
93553 **  ------------------------------
93554 **  'a'            TEXT
93555 **  'b'            NONE
93556 **  'c'            NUMERIC
93557 **  'd'            INTEGER
93558 **  'e'            REAL
93559 */
93560 SQLITE_PRIVATE void sqlite3TableAffinity(Vdbe *v, Table *pTab, int iReg){
93561   int i;
93562   char *zColAff = pTab->zColAff;
93563   if( zColAff==0 ){
93564     sqlite3 *db = sqlite3VdbeDb(v);
93565     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
93566     if( !zColAff ){
93567       db->mallocFailed = 1;
93568       return;
93569     }
93570 
93571     for(i=0; i<pTab->nCol; i++){
93572       zColAff[i] = pTab->aCol[i].affinity;
93573     }
93574     do{
93575       zColAff[i--] = 0;
93576     }while( i>=0 && zColAff[i]==SQLITE_AFF_NONE );
93577     pTab->zColAff = zColAff;
93578   }
93579   i = sqlite3Strlen30(zColAff);
93580   if( i ){
93581     if( iReg ){
93582       sqlite3VdbeAddOp4(v, OP_Affinity, iReg, i, 0, zColAff, i);
93583     }else{
93584       sqlite3VdbeChangeP4(v, -1, zColAff, i);
93585     }
93586   }
93587 }
93588 
93589 /*
93590 ** Return non-zero if the table pTab in database iDb or any of its indices
93591 ** have been opened at any point in the VDBE program. This is used to see if
93592 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
93593 ** run without using a temporary table for the results of the SELECT.
93594 */
93595 static int readsTable(Parse *p, int iDb, Table *pTab){
93596   Vdbe *v = sqlite3GetVdbe(p);
93597   int i;
93598   int iEnd = sqlite3VdbeCurrentAddr(v);
93599 #ifndef SQLITE_OMIT_VIRTUALTABLE
93600   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
93601 #endif
93602 
93603   for(i=1; i<iEnd; i++){
93604     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
93605     assert( pOp!=0 );
93606     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
93607       Index *pIndex;
93608       int tnum = pOp->p2;
93609       if( tnum==pTab->tnum ){
93610         return 1;
93611       }
93612       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
93613         if( tnum==pIndex->tnum ){
93614           return 1;
93615         }
93616       }
93617     }
93618 #ifndef SQLITE_OMIT_VIRTUALTABLE
93619     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
93620       assert( pOp->p4.pVtab!=0 );
93621       assert( pOp->p4type==P4_VTAB );
93622       return 1;
93623     }
93624 #endif
93625   }
93626   return 0;
93627 }
93628 
93629 #ifndef SQLITE_OMIT_AUTOINCREMENT
93630 /*
93631 ** Locate or create an AutoincInfo structure associated with table pTab
93632 ** which is in database iDb.  Return the register number for the register
93633 ** that holds the maximum rowid.
93634 **
93635 ** There is at most one AutoincInfo structure per table even if the
93636 ** same table is autoincremented multiple times due to inserts within
93637 ** triggers.  A new AutoincInfo structure is created if this is the
93638 ** first use of table pTab.  On 2nd and subsequent uses, the original
93639 ** AutoincInfo structure is used.
93640 **
93641 ** Three memory locations are allocated:
93642 **
93643 **   (1)  Register to hold the name of the pTab table.
93644 **   (2)  Register to hold the maximum ROWID of pTab.
93645 **   (3)  Register to hold the rowid in sqlite_sequence of pTab
93646 **
93647 ** The 2nd register is the one that is returned.  That is all the
93648 ** insert routine needs to know about.
93649 */
93650 static int autoIncBegin(
93651   Parse *pParse,      /* Parsing context */
93652   int iDb,            /* Index of the database holding pTab */
93653   Table *pTab         /* The table we are writing to */
93654 ){
93655   int memId = 0;      /* Register holding maximum rowid */
93656   if( pTab->tabFlags & TF_Autoincrement ){
93657     Parse *pToplevel = sqlite3ParseToplevel(pParse);
93658     AutoincInfo *pInfo;
93659 
93660     pInfo = pToplevel->pAinc;
93661     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
93662     if( pInfo==0 ){
93663       pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
93664       if( pInfo==0 ) return 0;
93665       pInfo->pNext = pToplevel->pAinc;
93666       pToplevel->pAinc = pInfo;
93667       pInfo->pTab = pTab;
93668       pInfo->iDb = iDb;
93669       pToplevel->nMem++;                  /* Register to hold name of table */
93670       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
93671       pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
93672     }
93673     memId = pInfo->regCtr;
93674   }
93675   return memId;
93676 }
93677 
93678 /*
93679 ** This routine generates code that will initialize all of the
93680 ** register used by the autoincrement tracker.
93681 */
93682 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
93683   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
93684   sqlite3 *db = pParse->db;  /* The database connection */
93685   Db *pDb;                   /* Database only autoinc table */
93686   int memId;                 /* Register holding max rowid */
93687   int addr;                  /* A VDBE address */
93688   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
93689 
93690   /* This routine is never called during trigger-generation.  It is
93691   ** only called from the top-level */
93692   assert( pParse->pTriggerTab==0 );
93693   assert( pParse==sqlite3ParseToplevel(pParse) );
93694 
93695   assert( v );   /* We failed long ago if this is not so */
93696   for(p = pParse->pAinc; p; p = p->pNext){
93697     pDb = &db->aDb[p->iDb];
93698     memId = p->regCtr;
93699     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
93700     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
93701     sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
93702     addr = sqlite3VdbeCurrentAddr(v);
93703     sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
93704     sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
93705     sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
93706     sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
93707     sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
93708     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
93709     sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
93710     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
93711     sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
93712     sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
93713     sqlite3VdbeAddOp0(v, OP_Close);
93714   }
93715 }
93716 
93717 /*
93718 ** Update the maximum rowid for an autoincrement calculation.
93719 **
93720 ** This routine should be called when the top of the stack holds a
93721 ** new rowid that is about to be inserted.  If that new rowid is
93722 ** larger than the maximum rowid in the memId memory cell, then the
93723 ** memory cell is updated.  The stack is unchanged.
93724 */
93725 static void autoIncStep(Parse *pParse, int memId, int regRowid){
93726   if( memId>0 ){
93727     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
93728   }
93729 }
93730 
93731 /*
93732 ** This routine generates the code needed to write autoincrement
93733 ** maximum rowid values back into the sqlite_sequence register.
93734 ** Every statement that might do an INSERT into an autoincrement
93735 ** table (either directly or through triggers) needs to call this
93736 ** routine just before the "exit" code.
93737 */
93738 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
93739   AutoincInfo *p;
93740   Vdbe *v = pParse->pVdbe;
93741   sqlite3 *db = pParse->db;
93742 
93743   assert( v );
93744   for(p = pParse->pAinc; p; p = p->pNext){
93745     Db *pDb = &db->aDb[p->iDb];
93746     int j1;
93747     int iRec;
93748     int memId = p->regCtr;
93749 
93750     iRec = sqlite3GetTempReg(pParse);
93751     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
93752     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
93753     j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
93754     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
93755     sqlite3VdbeJumpHere(v, j1);
93756     sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
93757     sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
93758     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
93759     sqlite3VdbeAddOp0(v, OP_Close);
93760     sqlite3ReleaseTempReg(pParse, iRec);
93761   }
93762 }
93763 #else
93764 /*
93765 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
93766 ** above are all no-ops
93767 */
93768 # define autoIncBegin(A,B,C) (0)
93769 # define autoIncStep(A,B,C)
93770 #endif /* SQLITE_OMIT_AUTOINCREMENT */
93771 
93772 
93773 /* Forward declaration */
93774 static int xferOptimization(
93775   Parse *pParse,        /* Parser context */
93776   Table *pDest,         /* The table we are inserting into */
93777   Select *pSelect,      /* A SELECT statement to use as the data source */
93778   int onError,          /* How to handle constraint errors */
93779   int iDbDest           /* The database of pDest */
93780 );
93781 
93782 /*
93783 ** This routine is called to handle SQL of the following forms:
93784 **
93785 **    insert into TABLE (IDLIST) values(EXPRLIST)
93786 **    insert into TABLE (IDLIST) select
93787 **
93788 ** The IDLIST following the table name is always optional.  If omitted,
93789 ** then a list of all columns for the table is substituted.  The IDLIST
93790 ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
93791 **
93792 ** The pList parameter holds EXPRLIST in the first form of the INSERT
93793 ** statement above, and pSelect is NULL.  For the second form, pList is
93794 ** NULL and pSelect is a pointer to the select statement used to generate
93795 ** data for the insert.
93796 **
93797 ** The code generated follows one of four templates.  For a simple
93798 ** insert with data coming from a VALUES clause, the code executes
93799 ** once straight down through.  Pseudo-code follows (we call this
93800 ** the "1st template"):
93801 **
93802 **         open write cursor to <table> and its indices
93803 **         put VALUES clause expressions into registers
93804 **         write the resulting record into <table>
93805 **         cleanup
93806 **
93807 ** The three remaining templates assume the statement is of the form
93808 **
93809 **   INSERT INTO <table> SELECT ...
93810 **
93811 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
93812 ** in other words if the SELECT pulls all columns from a single table
93813 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
93814 ** if <table2> and <table1> are distinct tables but have identical
93815 ** schemas, including all the same indices, then a special optimization
93816 ** is invoked that copies raw records from <table2> over to <table1>.
93817 ** See the xferOptimization() function for the implementation of this
93818 ** template.  This is the 2nd template.
93819 **
93820 **         open a write cursor to <table>
93821 **         open read cursor on <table2>
93822 **         transfer all records in <table2> over to <table>
93823 **         close cursors
93824 **         foreach index on <table>
93825 **           open a write cursor on the <table> index
93826 **           open a read cursor on the corresponding <table2> index
93827 **           transfer all records from the read to the write cursors
93828 **           close cursors
93829 **         end foreach
93830 **
93831 ** The 3rd template is for when the second template does not apply
93832 ** and the SELECT clause does not read from <table> at any time.
93833 ** The generated code follows this template:
93834 **
93835 **         X <- A
93836 **         goto B
93837 **      A: setup for the SELECT
93838 **         loop over the rows in the SELECT
93839 **           load values into registers R..R+n
93840 **           yield X
93841 **         end loop
93842 **         cleanup after the SELECT
93843 **         end-coroutine X
93844 **      B: open write cursor to <table> and its indices
93845 **      C: yield X, at EOF goto D
93846 **         insert the select result into <table> from R..R+n
93847 **         goto C
93848 **      D: cleanup
93849 **
93850 ** The 4th template is used if the insert statement takes its
93851 ** values from a SELECT but the data is being inserted into a table
93852 ** that is also read as part of the SELECT.  In the third form,
93853 ** we have to use a intermediate table to store the results of
93854 ** the select.  The template is like this:
93855 **
93856 **         X <- A
93857 **         goto B
93858 **      A: setup for the SELECT
93859 **         loop over the tables in the SELECT
93860 **           load value into register R..R+n
93861 **           yield X
93862 **         end loop
93863 **         cleanup after the SELECT
93864 **         end co-routine R
93865 **      B: open temp table
93866 **      L: yield X, at EOF goto M
93867 **         insert row from R..R+n into temp table
93868 **         goto L
93869 **      M: open write cursor to <table> and its indices
93870 **         rewind temp table
93871 **      C: loop over rows of intermediate table
93872 **           transfer values form intermediate table into <table>
93873 **         end loop
93874 **      D: cleanup
93875 */
93876 SQLITE_PRIVATE void sqlite3Insert(
93877   Parse *pParse,        /* Parser context */
93878   SrcList *pTabList,    /* Name of table into which we are inserting */
93879   Select *pSelect,      /* A SELECT statement to use as the data source */
93880   IdList *pColumn,      /* Column names corresponding to IDLIST. */
93881   int onError           /* How to handle constraint errors */
93882 ){
93883   sqlite3 *db;          /* The main database structure */
93884   Table *pTab;          /* The table to insert into.  aka TABLE */
93885   char *zTab;           /* Name of the table into which we are inserting */
93886   const char *zDb;      /* Name of the database holding this table */
93887   int i, j, idx;        /* Loop counters */
93888   Vdbe *v;              /* Generate code into this virtual machine */
93889   Index *pIdx;          /* For looping over indices of the table */
93890   int nColumn;          /* Number of columns in the data */
93891   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
93892   int iDataCur = 0;     /* VDBE cursor that is the main data repository */
93893   int iIdxCur = 0;      /* First index cursor */
93894   int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
93895   int endOfLoop;        /* Label for the end of the insertion loop */
93896   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
93897   int addrInsTop = 0;   /* Jump to label "D" */
93898   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
93899   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
93900   int iDb;              /* Index of database holding TABLE */
93901   Db *pDb;              /* The database containing table being inserted into */
93902   u8 useTempTable = 0;  /* Store SELECT results in intermediate table */
93903   u8 appendFlag = 0;    /* True if the insert is likely to be an append */
93904   u8 withoutRowid;      /* 0 for normal table.  1 for WITHOUT ROWID table */
93905   u8 bIdListInOrder = 1; /* True if IDLIST is in table order */
93906   ExprList *pList = 0;  /* List of VALUES() to be inserted  */
93907 
93908   /* Register allocations */
93909   int regFromSelect = 0;/* Base register for data coming from SELECT */
93910   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
93911   int regRowCount = 0;  /* Memory cell used for the row counter */
93912   int regIns;           /* Block of regs holding rowid+data being inserted */
93913   int regRowid;         /* registers holding insert rowid */
93914   int regData;          /* register holding first column to insert */
93915   int *aRegIdx = 0;     /* One register allocated to each index */
93916 
93917 #ifndef SQLITE_OMIT_TRIGGER
93918   int isView;                 /* True if attempting to insert into a view */
93919   Trigger *pTrigger;          /* List of triggers on pTab, if required */
93920   int tmask;                  /* Mask of trigger times */
93921 #endif
93922 
93923   db = pParse->db;
93924   memset(&dest, 0, sizeof(dest));
93925   if( pParse->nErr || db->mallocFailed ){
93926     goto insert_cleanup;
93927   }
93928 
93929   /* If the Select object is really just a simple VALUES() list with a
93930   ** single row values (the common case) then keep that one row of values
93931   ** and go ahead and discard the Select object
93932   */
93933   if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
93934     pList = pSelect->pEList;
93935     pSelect->pEList = 0;
93936     sqlite3SelectDelete(db, pSelect);
93937     pSelect = 0;
93938   }
93939 
93940   /* Locate the table into which we will be inserting new information.
93941   */
93942   assert( pTabList->nSrc==1 );
93943   zTab = pTabList->a[0].zName;
93944   if( NEVER(zTab==0) ) goto insert_cleanup;
93945   pTab = sqlite3SrcListLookup(pParse, pTabList);
93946   if( pTab==0 ){
93947     goto insert_cleanup;
93948   }
93949   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
93950   assert( iDb<db->nDb );
93951   pDb = &db->aDb[iDb];
93952   zDb = pDb->zName;
93953   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
93954     goto insert_cleanup;
93955   }
93956   withoutRowid = !HasRowid(pTab);
93957 
93958   /* Figure out if we have any triggers and if the table being
93959   ** inserted into is a view
93960   */
93961 #ifndef SQLITE_OMIT_TRIGGER
93962   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
93963   isView = pTab->pSelect!=0;
93964 #else
93965 # define pTrigger 0
93966 # define tmask 0
93967 # define isView 0
93968 #endif
93969 #ifdef SQLITE_OMIT_VIEW
93970 # undef isView
93971 # define isView 0
93972 #endif
93973   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
93974 
93975   /* If pTab is really a view, make sure it has been initialized.
93976   ** ViewGetColumnNames() is a no-op if pTab is not a view.
93977   */
93978   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
93979     goto insert_cleanup;
93980   }
93981 
93982   /* Cannot insert into a read-only table.
93983   */
93984   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
93985     goto insert_cleanup;
93986   }
93987 
93988   /* Allocate a VDBE
93989   */
93990   v = sqlite3GetVdbe(pParse);
93991   if( v==0 ) goto insert_cleanup;
93992   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
93993   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
93994 
93995 #ifndef SQLITE_OMIT_XFER_OPT
93996   /* If the statement is of the form
93997   **
93998   **       INSERT INTO <table1> SELECT * FROM <table2>;
93999   **
94000   ** Then special optimizations can be applied that make the transfer
94001   ** very fast and which reduce fragmentation of indices.
94002   **
94003   ** This is the 2nd template.
94004   */
94005   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
94006     assert( !pTrigger );
94007     assert( pList==0 );
94008     goto insert_end;
94009   }
94010 #endif /* SQLITE_OMIT_XFER_OPT */
94011 
94012   /* If this is an AUTOINCREMENT table, look up the sequence number in the
94013   ** sqlite_sequence table and store it in memory cell regAutoinc.
94014   */
94015   regAutoinc = autoIncBegin(pParse, iDb, pTab);
94016 
94017   /* Allocate registers for holding the rowid of the new row,
94018   ** the content of the new row, and the assemblied row record.
94019   */
94020   regRowid = regIns = pParse->nMem+1;
94021   pParse->nMem += pTab->nCol + 1;
94022   if( IsVirtual(pTab) ){
94023     regRowid++;
94024     pParse->nMem++;
94025   }
94026   regData = regRowid+1;
94027 
94028   /* If the INSERT statement included an IDLIST term, then make sure
94029   ** all elements of the IDLIST really are columns of the table and
94030   ** remember the column indices.
94031   **
94032   ** If the table has an INTEGER PRIMARY KEY column and that column
94033   ** is named in the IDLIST, then record in the ipkColumn variable
94034   ** the index into IDLIST of the primary key column.  ipkColumn is
94035   ** the index of the primary key as it appears in IDLIST, not as
94036   ** is appears in the original table.  (The index of the INTEGER
94037   ** PRIMARY KEY in the original table is pTab->iPKey.)
94038   */
94039   if( pColumn ){
94040     for(i=0; i<pColumn->nId; i++){
94041       pColumn->a[i].idx = -1;
94042     }
94043     for(i=0; i<pColumn->nId; i++){
94044       for(j=0; j<pTab->nCol; j++){
94045         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
94046           pColumn->a[i].idx = j;
94047           if( i!=j ) bIdListInOrder = 0;
94048           if( j==pTab->iPKey ){
94049             ipkColumn = i;  assert( !withoutRowid );
94050           }
94051           break;
94052         }
94053       }
94054       if( j>=pTab->nCol ){
94055         if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
94056           ipkColumn = i;
94057         }else{
94058           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
94059               pTabList, 0, pColumn->a[i].zName);
94060           pParse->checkSchema = 1;
94061           goto insert_cleanup;
94062         }
94063       }
94064     }
94065   }
94066 
94067   /* Figure out how many columns of data are supplied.  If the data
94068   ** is coming from a SELECT statement, then generate a co-routine that
94069   ** produces a single row of the SELECT on each invocation.  The
94070   ** co-routine is the common header to the 3rd and 4th templates.
94071   */
94072   if( pSelect ){
94073     /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
94074     int regYield;       /* Register holding co-routine entry-point */
94075     int addrTop;        /* Top of the co-routine */
94076     int rc;             /* Result code */
94077 
94078     regYield = ++pParse->nMem;
94079     addrTop = sqlite3VdbeCurrentAddr(v) + 1;
94080     sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
94081     sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
94082     dest.iSdst = bIdListInOrder ? regData : 0;
94083     dest.nSdst = pTab->nCol;
94084     rc = sqlite3Select(pParse, pSelect, &dest);
94085     regFromSelect = dest.iSdst;
94086     assert( pParse->nErr==0 || rc );
94087     if( rc || db->mallocFailed ) goto insert_cleanup;
94088     sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
94089     sqlite3VdbeJumpHere(v, addrTop - 1);                       /* label B: */
94090     assert( pSelect->pEList );
94091     nColumn = pSelect->pEList->nExpr;
94092 
94093     /* Set useTempTable to TRUE if the result of the SELECT statement
94094     ** should be written into a temporary table (template 4).  Set to
94095     ** FALSE if each output row of the SELECT can be written directly into
94096     ** the destination table (template 3).
94097     **
94098     ** A temp table must be used if the table being updated is also one
94099     ** of the tables being read by the SELECT statement.  Also use a
94100     ** temp table in the case of row triggers.
94101     */
94102     if( pTrigger || readsTable(pParse, iDb, pTab) ){
94103       useTempTable = 1;
94104     }
94105 
94106     if( useTempTable ){
94107       /* Invoke the coroutine to extract information from the SELECT
94108       ** and add it to a transient table srcTab.  The code generated
94109       ** here is from the 4th template:
94110       **
94111       **      B: open temp table
94112       **      L: yield X, goto M at EOF
94113       **         insert row from R..R+n into temp table
94114       **         goto L
94115       **      M: ...
94116       */
94117       int regRec;          /* Register to hold packed record */
94118       int regTempRowid;    /* Register to hold temp table ROWID */
94119       int addrL;           /* Label "L" */
94120 
94121       srcTab = pParse->nTab++;
94122       regRec = sqlite3GetTempReg(pParse);
94123       regTempRowid = sqlite3GetTempReg(pParse);
94124       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
94125       addrL = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm); VdbeCoverage(v);
94126       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
94127       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
94128       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
94129       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrL);
94130       sqlite3VdbeJumpHere(v, addrL);
94131       sqlite3ReleaseTempReg(pParse, regRec);
94132       sqlite3ReleaseTempReg(pParse, regTempRowid);
94133     }
94134   }else{
94135     /* This is the case if the data for the INSERT is coming from a VALUES
94136     ** clause
94137     */
94138     NameContext sNC;
94139     memset(&sNC, 0, sizeof(sNC));
94140     sNC.pParse = pParse;
94141     srcTab = -1;
94142     assert( useTempTable==0 );
94143     nColumn = pList ? pList->nExpr : 0;
94144     for(i=0; i<nColumn; i++){
94145       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
94146         goto insert_cleanup;
94147       }
94148     }
94149   }
94150 
94151   /* If there is no IDLIST term but the table has an integer primary
94152   ** key, the set the ipkColumn variable to the integer primary key
94153   ** column index in the original table definition.
94154   */
94155   if( pColumn==0 && nColumn>0 ){
94156     ipkColumn = pTab->iPKey;
94157   }
94158 
94159   /* Make sure the number of columns in the source data matches the number
94160   ** of columns to be inserted into the table.
94161   */
94162   if( IsVirtual(pTab) ){
94163     for(i=0; i<pTab->nCol; i++){
94164       nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
94165     }
94166   }
94167   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
94168     sqlite3ErrorMsg(pParse,
94169        "table %S has %d columns but %d values were supplied",
94170        pTabList, 0, pTab->nCol-nHidden, nColumn);
94171     goto insert_cleanup;
94172   }
94173   if( pColumn!=0 && nColumn!=pColumn->nId ){
94174     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
94175     goto insert_cleanup;
94176   }
94177 
94178   /* Initialize the count of rows to be inserted
94179   */
94180   if( db->flags & SQLITE_CountRows ){
94181     regRowCount = ++pParse->nMem;
94182     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
94183   }
94184 
94185   /* If this is not a view, open the table and and all indices */
94186   if( !isView ){
94187     int nIdx;
94188     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
94189                                       &iDataCur, &iIdxCur);
94190     aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
94191     if( aRegIdx==0 ){
94192       goto insert_cleanup;
94193     }
94194     for(i=0; i<nIdx; i++){
94195       aRegIdx[i] = ++pParse->nMem;
94196     }
94197   }
94198 
94199   /* This is the top of the main insertion loop */
94200   if( useTempTable ){
94201     /* This block codes the top of loop only.  The complete loop is the
94202     ** following pseudocode (template 4):
94203     **
94204     **         rewind temp table, if empty goto D
94205     **      C: loop over rows of intermediate table
94206     **           transfer values form intermediate table into <table>
94207     **         end loop
94208     **      D: ...
94209     */
94210     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); VdbeCoverage(v);
94211     addrCont = sqlite3VdbeCurrentAddr(v);
94212   }else if( pSelect ){
94213     /* This block codes the top of loop only.  The complete loop is the
94214     ** following pseudocode (template 3):
94215     **
94216     **      C: yield X, at EOF goto D
94217     **         insert the select result into <table> from R..R+n
94218     **         goto C
94219     **      D: ...
94220     */
94221     addrInsTop = addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
94222     VdbeCoverage(v);
94223   }
94224 
94225   /* Run the BEFORE and INSTEAD OF triggers, if there are any
94226   */
94227   endOfLoop = sqlite3VdbeMakeLabel(v);
94228   if( tmask & TRIGGER_BEFORE ){
94229     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
94230 
94231     /* build the NEW.* reference row.  Note that if there is an INTEGER
94232     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
94233     ** translated into a unique ID for the row.  But on a BEFORE trigger,
94234     ** we do not know what the unique ID will be (because the insert has
94235     ** not happened yet) so we substitute a rowid of -1
94236     */
94237     if( ipkColumn<0 ){
94238       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
94239     }else{
94240       int j1;
94241       assert( !withoutRowid );
94242       if( useTempTable ){
94243         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
94244       }else{
94245         assert( pSelect==0 );  /* Otherwise useTempTable is true */
94246         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
94247       }
94248       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); VdbeCoverage(v);
94249       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
94250       sqlite3VdbeJumpHere(v, j1);
94251       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); VdbeCoverage(v);
94252     }
94253 
94254     /* Cannot have triggers on a virtual table. If it were possible,
94255     ** this block would have to account for hidden column.
94256     */
94257     assert( !IsVirtual(pTab) );
94258 
94259     /* Create the new column data
94260     */
94261     for(i=0; i<pTab->nCol; i++){
94262       if( pColumn==0 ){
94263         j = i;
94264       }else{
94265         for(j=0; j<pColumn->nId; j++){
94266           if( pColumn->a[j].idx==i ) break;
94267         }
94268       }
94269       if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
94270         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
94271       }else if( useTempTable ){
94272         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
94273       }else{
94274         assert( pSelect==0 ); /* Otherwise useTempTable is true */
94275         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
94276       }
94277     }
94278 
94279     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
94280     ** do not attempt any conversions before assembling the record.
94281     ** If this is a real table, attempt conversions as required by the
94282     ** table column affinities.
94283     */
94284     if( !isView ){
94285       sqlite3TableAffinity(v, pTab, regCols+1);
94286     }
94287 
94288     /* Fire BEFORE or INSTEAD OF triggers */
94289     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
94290         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
94291 
94292     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
94293   }
94294 
94295   /* Compute the content of the next row to insert into a range of
94296   ** registers beginning at regIns.
94297   */
94298   if( !isView ){
94299     if( IsVirtual(pTab) ){
94300       /* The row that the VUpdate opcode will delete: none */
94301       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
94302     }
94303     if( ipkColumn>=0 ){
94304       if( useTempTable ){
94305         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
94306       }else if( pSelect ){
94307         sqlite3VdbeAddOp2(v, OP_Copy, regFromSelect+ipkColumn, regRowid);
94308       }else{
94309         VdbeOp *pOp;
94310         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
94311         pOp = sqlite3VdbeGetOp(v, -1);
94312         if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
94313           appendFlag = 1;
94314           pOp->opcode = OP_NewRowid;
94315           pOp->p1 = iDataCur;
94316           pOp->p2 = regRowid;
94317           pOp->p3 = regAutoinc;
94318         }
94319       }
94320       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
94321       ** to generate a unique primary key value.
94322       */
94323       if( !appendFlag ){
94324         int j1;
94325         if( !IsVirtual(pTab) ){
94326           j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); VdbeCoverage(v);
94327           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
94328           sqlite3VdbeJumpHere(v, j1);
94329         }else{
94330           j1 = sqlite3VdbeCurrentAddr(v);
94331           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); VdbeCoverage(v);
94332         }
94333         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); VdbeCoverage(v);
94334       }
94335     }else if( IsVirtual(pTab) || withoutRowid ){
94336       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
94337     }else{
94338       sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
94339       appendFlag = 1;
94340     }
94341     autoIncStep(pParse, regAutoinc, regRowid);
94342 
94343     /* Compute data for all columns of the new entry, beginning
94344     ** with the first column.
94345     */
94346     nHidden = 0;
94347     for(i=0; i<pTab->nCol; i++){
94348       int iRegStore = regRowid+1+i;
94349       if( i==pTab->iPKey ){
94350         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
94351         ** Whenever this column is read, the rowid will be substituted
94352         ** in its place.  Hence, fill this column with a NULL to avoid
94353         ** taking up data space with information that will never be used.
94354         ** As there may be shallow copies of this value, make it a soft-NULL */
94355         sqlite3VdbeAddOp1(v, OP_SoftNull, iRegStore);
94356         continue;
94357       }
94358       if( pColumn==0 ){
94359         if( IsHiddenColumn(&pTab->aCol[i]) ){
94360           assert( IsVirtual(pTab) );
94361           j = -1;
94362           nHidden++;
94363         }else{
94364           j = i - nHidden;
94365         }
94366       }else{
94367         for(j=0; j<pColumn->nId; j++){
94368           if( pColumn->a[j].idx==i ) break;
94369         }
94370       }
94371       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
94372         sqlite3ExprCodeFactorable(pParse, pTab->aCol[i].pDflt, iRegStore);
94373       }else if( useTempTable ){
94374         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
94375       }else if( pSelect ){
94376         if( regFromSelect!=regData ){
94377           sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
94378         }
94379       }else{
94380         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
94381       }
94382     }
94383 
94384     /* Generate code to check constraints and generate index keys and
94385     ** do the insertion.
94386     */
94387 #ifndef SQLITE_OMIT_VIRTUALTABLE
94388     if( IsVirtual(pTab) ){
94389       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
94390       sqlite3VtabMakeWritable(pParse, pTab);
94391       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
94392       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
94393       sqlite3MayAbort(pParse);
94394     }else
94395 #endif
94396     {
94397       int isReplace;    /* Set to true if constraints may cause a replace */
94398       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
94399           regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
94400       );
94401       sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
94402       sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
94403                                regIns, aRegIdx, 0, appendFlag, isReplace==0);
94404     }
94405   }
94406 
94407   /* Update the count of rows that are inserted
94408   */
94409   if( (db->flags & SQLITE_CountRows)!=0 ){
94410     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
94411   }
94412 
94413   if( pTrigger ){
94414     /* Code AFTER triggers */
94415     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
94416         pTab, regData-2-pTab->nCol, onError, endOfLoop);
94417   }
94418 
94419   /* The bottom of the main insertion loop, if the data source
94420   ** is a SELECT statement.
94421   */
94422   sqlite3VdbeResolveLabel(v, endOfLoop);
94423   if( useTempTable ){
94424     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); VdbeCoverage(v);
94425     sqlite3VdbeJumpHere(v, addrInsTop);
94426     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
94427   }else if( pSelect ){
94428     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
94429     sqlite3VdbeJumpHere(v, addrInsTop);
94430   }
94431 
94432   if( !IsVirtual(pTab) && !isView ){
94433     /* Close all tables opened */
94434     if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
94435     for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
94436       sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
94437     }
94438   }
94439 
94440 insert_end:
94441   /* Update the sqlite_sequence table by storing the content of the
94442   ** maximum rowid counter values recorded while inserting into
94443   ** autoincrement tables.
94444   */
94445   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
94446     sqlite3AutoincrementEnd(pParse);
94447   }
94448 
94449   /*
94450   ** Return the number of rows inserted. If this routine is
94451   ** generating code because of a call to sqlite3NestedParse(), do not
94452   ** invoke the callback function.
94453   */
94454   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
94455     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
94456     sqlite3VdbeSetNumCols(v, 1);
94457     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
94458   }
94459 
94460 insert_cleanup:
94461   sqlite3SrcListDelete(db, pTabList);
94462   sqlite3ExprListDelete(db, pList);
94463   sqlite3SelectDelete(db, pSelect);
94464   sqlite3IdListDelete(db, pColumn);
94465   sqlite3DbFree(db, aRegIdx);
94466 }
94467 
94468 /* Make sure "isView" and other macros defined above are undefined. Otherwise
94469 ** thely may interfere with compilation of other functions in this file
94470 ** (or in another file, if this file becomes part of the amalgamation).  */
94471 #ifdef isView
94472  #undef isView
94473 #endif
94474 #ifdef pTrigger
94475  #undef pTrigger
94476 #endif
94477 #ifdef tmask
94478  #undef tmask
94479 #endif
94480 
94481 /*
94482 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
94483 ** on table pTab.
94484 **
94485 ** The regNewData parameter is the first register in a range that contains
94486 ** the data to be inserted or the data after the update.  There will be
94487 ** pTab->nCol+1 registers in this range.  The first register (the one
94488 ** that regNewData points to) will contain the new rowid, or NULL in the
94489 ** case of a WITHOUT ROWID table.  The second register in the range will
94490 ** contain the content of the first table column.  The third register will
94491 ** contain the content of the second table column.  And so forth.
94492 **
94493 ** The regOldData parameter is similar to regNewData except that it contains
94494 ** the data prior to an UPDATE rather than afterwards.  regOldData is zero
94495 ** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
94496 ** checking regOldData for zero.
94497 **
94498 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
94499 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
94500 ** might be modified by the UPDATE.  If pkChng is false, then the key of
94501 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
94502 **
94503 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
94504 ** was explicitly specified as part of the INSERT statement.  If pkChng
94505 ** is zero, it means that the either rowid is computed automatically or
94506 ** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
94507 ** pkChng will only be true if the INSERT statement provides an integer
94508 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
94509 **
94510 ** The code generated by this routine will store new index entries into
94511 ** registers identified by aRegIdx[].  No index entry is created for
94512 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
94513 ** the same as the order of indices on the linked list of indices
94514 ** at pTab->pIndex.
94515 **
94516 ** The caller must have already opened writeable cursors on the main
94517 ** table and all applicable indices (that is to say, all indices for which
94518 ** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
94519 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
94520 ** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
94521 ** for the first index in the pTab->pIndex list.  Cursors for other indices
94522 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
94523 **
94524 ** This routine also generates code to check constraints.  NOT NULL,
94525 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
94526 ** then the appropriate action is performed.  There are five possible
94527 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
94528 **
94529 **  Constraint type  Action       What Happens
94530 **  ---------------  ----------   ----------------------------------------
94531 **  any              ROLLBACK     The current transaction is rolled back and
94532 **                                sqlite3_step() returns immediately with a
94533 **                                return code of SQLITE_CONSTRAINT.
94534 **
94535 **  any              ABORT        Back out changes from the current command
94536 **                                only (do not do a complete rollback) then
94537 **                                cause sqlite3_step() to return immediately
94538 **                                with SQLITE_CONSTRAINT.
94539 **
94540 **  any              FAIL         Sqlite3_step() returns immediately with a
94541 **                                return code of SQLITE_CONSTRAINT.  The
94542 **                                transaction is not rolled back and any
94543 **                                changes to prior rows are retained.
94544 **
94545 **  any              IGNORE       The attempt in insert or update the current
94546 **                                row is skipped, without throwing an error.
94547 **                                Processing continues with the next row.
94548 **                                (There is an immediate jump to ignoreDest.)
94549 **
94550 **  NOT NULL         REPLACE      The NULL value is replace by the default
94551 **                                value for that column.  If the default value
94552 **                                is NULL, the action is the same as ABORT.
94553 **
94554 **  UNIQUE           REPLACE      The other row that conflicts with the row
94555 **                                being inserted is removed.
94556 **
94557 **  CHECK            REPLACE      Illegal.  The results in an exception.
94558 **
94559 ** Which action to take is determined by the overrideError parameter.
94560 ** Or if overrideError==OE_Default, then the pParse->onError parameter
94561 ** is used.  Or if pParse->onError==OE_Default then the onError value
94562 ** for the constraint is used.
94563 */
94564 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
94565   Parse *pParse,       /* The parser context */
94566   Table *pTab,         /* The table being inserted or updated */
94567   int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
94568   int iDataCur,        /* Canonical data cursor (main table or PK index) */
94569   int iIdxCur,         /* First index cursor */
94570   int regNewData,      /* First register in a range holding values to insert */
94571   int regOldData,      /* Previous content.  0 for INSERTs */
94572   u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
94573   u8 overrideError,    /* Override onError to this if not OE_Default */
94574   int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
94575   int *pbMayReplace    /* OUT: Set to true if constraint may cause a replace */
94576 ){
94577   Vdbe *v;             /* VDBE under constrution */
94578   Index *pIdx;         /* Pointer to one of the indices */
94579   Index *pPk = 0;      /* The PRIMARY KEY index */
94580   sqlite3 *db;         /* Database connection */
94581   int i;               /* loop counter */
94582   int ix;              /* Index loop counter */
94583   int nCol;            /* Number of columns */
94584   int onError;         /* Conflict resolution strategy */
94585   int j1;              /* Addresss of jump instruction */
94586   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
94587   int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
94588   int ipkTop = 0;      /* Top of the rowid change constraint check */
94589   int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
94590   u8 isUpdate;         /* True if this is an UPDATE operation */
94591   u8 bAffinityDone = 0;  /* True if the OP_Affinity operation has been run */
94592   int regRowid = -1;   /* Register holding ROWID value */
94593 
94594   isUpdate = regOldData!=0;
94595   db = pParse->db;
94596   v = sqlite3GetVdbe(pParse);
94597   assert( v!=0 );
94598   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
94599   nCol = pTab->nCol;
94600 
94601   /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
94602   ** normal rowid tables.  nPkField is the number of key fields in the
94603   ** pPk index or 1 for a rowid table.  In other words, nPkField is the
94604   ** number of fields in the true primary key of the table. */
94605   if( HasRowid(pTab) ){
94606     pPk = 0;
94607     nPkField = 1;
94608   }else{
94609     pPk = sqlite3PrimaryKeyIndex(pTab);
94610     nPkField = pPk->nKeyCol;
94611   }
94612 
94613   /* Record that this module has started */
94614   VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
94615                      iDataCur, iIdxCur, regNewData, regOldData, pkChng));
94616 
94617   /* Test all NOT NULL constraints.
94618   */
94619   for(i=0; i<nCol; i++){
94620     if( i==pTab->iPKey ){
94621       continue;
94622     }
94623     onError = pTab->aCol[i].notNull;
94624     if( onError==OE_None ) continue;
94625     if( overrideError!=OE_Default ){
94626       onError = overrideError;
94627     }else if( onError==OE_Default ){
94628       onError = OE_Abort;
94629     }
94630     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
94631       onError = OE_Abort;
94632     }
94633     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
94634         || onError==OE_Ignore || onError==OE_Replace );
94635     switch( onError ){
94636       case OE_Abort:
94637         sqlite3MayAbort(pParse);
94638         /* Fall through */
94639       case OE_Rollback:
94640       case OE_Fail: {
94641         char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
94642                                     pTab->aCol[i].zName);
94643         sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
94644                           regNewData+1+i, zMsg, P4_DYNAMIC);
94645         sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
94646         VdbeCoverage(v);
94647         break;
94648       }
94649       case OE_Ignore: {
94650         sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
94651         VdbeCoverage(v);
94652         break;
94653       }
94654       default: {
94655         assert( onError==OE_Replace );
94656         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i); VdbeCoverage(v);
94657         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
94658         sqlite3VdbeJumpHere(v, j1);
94659         break;
94660       }
94661     }
94662   }
94663 
94664   /* Test all CHECK constraints
94665   */
94666 #ifndef SQLITE_OMIT_CHECK
94667   if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
94668     ExprList *pCheck = pTab->pCheck;
94669     pParse->ckBase = regNewData+1;
94670     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
94671     for(i=0; i<pCheck->nExpr; i++){
94672       int allOk = sqlite3VdbeMakeLabel(v);
94673       sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
94674       if( onError==OE_Ignore ){
94675         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
94676       }else{
94677         char *zName = pCheck->a[i].zName;
94678         if( zName==0 ) zName = pTab->zName;
94679         if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
94680         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
94681                               onError, zName, P4_TRANSIENT,
94682                               P5_ConstraintCheck);
94683       }
94684       sqlite3VdbeResolveLabel(v, allOk);
94685     }
94686   }
94687 #endif /* !defined(SQLITE_OMIT_CHECK) */
94688 
94689   /* If rowid is changing, make sure the new rowid does not previously
94690   ** exist in the table.
94691   */
94692   if( pkChng && pPk==0 ){
94693     int addrRowidOk = sqlite3VdbeMakeLabel(v);
94694 
94695     /* Figure out what action to take in case of a rowid collision */
94696     onError = pTab->keyConf;
94697     if( overrideError!=OE_Default ){
94698       onError = overrideError;
94699     }else if( onError==OE_Default ){
94700       onError = OE_Abort;
94701     }
94702 
94703     if( isUpdate ){
94704       /* pkChng!=0 does not mean that the rowid has change, only that
94705       ** it might have changed.  Skip the conflict logic below if the rowid
94706       ** is unchanged. */
94707       sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
94708       sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
94709       VdbeCoverage(v);
94710     }
94711 
94712     /* If the response to a rowid conflict is REPLACE but the response
94713     ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
94714     ** to defer the running of the rowid conflict checking until after
94715     ** the UNIQUE constraints have run.
94716     */
94717     if( onError==OE_Replace && overrideError!=OE_Replace ){
94718       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
94719         if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
94720           ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
94721           break;
94722         }
94723       }
94724     }
94725 
94726     /* Check to see if the new rowid already exists in the table.  Skip
94727     ** the following conflict logic if it does not. */
94728     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
94729     VdbeCoverage(v);
94730 
94731     /* Generate code that deals with a rowid collision */
94732     switch( onError ){
94733       default: {
94734         onError = OE_Abort;
94735         /* Fall thru into the next case */
94736       }
94737       case OE_Rollback:
94738       case OE_Abort:
94739       case OE_Fail: {
94740         sqlite3RowidConstraint(pParse, onError, pTab);
94741         break;
94742       }
94743       case OE_Replace: {
94744         /* If there are DELETE triggers on this table and the
94745         ** recursive-triggers flag is set, call GenerateRowDelete() to
94746         ** remove the conflicting row from the table. This will fire
94747         ** the triggers and remove both the table and index b-tree entries.
94748         **
94749         ** Otherwise, if there are no triggers or the recursive-triggers
94750         ** flag is not set, but the table has one or more indexes, call
94751         ** GenerateRowIndexDelete(). This removes the index b-tree entries
94752         ** only. The table b-tree entry will be replaced by the new entry
94753         ** when it is inserted.
94754         **
94755         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
94756         ** also invoke MultiWrite() to indicate that this VDBE may require
94757         ** statement rollback (if the statement is aborted after the delete
94758         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
94759         ** but being more selective here allows statements like:
94760         **
94761         **   REPLACE INTO t(rowid) VALUES($newrowid)
94762         **
94763         ** to run without a statement journal if there are no indexes on the
94764         ** table.
94765         */
94766         Trigger *pTrigger = 0;
94767         if( db->flags&SQLITE_RecTriggers ){
94768           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
94769         }
94770         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
94771           sqlite3MultiWrite(pParse);
94772           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
94773                                    regNewData, 1, 0, OE_Replace, 1);
94774         }else if( pTab->pIndex ){
94775           sqlite3MultiWrite(pParse);
94776           sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
94777         }
94778         seenReplace = 1;
94779         break;
94780       }
94781       case OE_Ignore: {
94782         /*assert( seenReplace==0 );*/
94783         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
94784         break;
94785       }
94786     }
94787     sqlite3VdbeResolveLabel(v, addrRowidOk);
94788     if( ipkTop ){
94789       ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
94790       sqlite3VdbeJumpHere(v, ipkTop);
94791     }
94792   }
94793 
94794   /* Test all UNIQUE constraints by creating entries for each UNIQUE
94795   ** index and making sure that duplicate entries do not already exist.
94796   ** Compute the revised record entries for indices as we go.
94797   **
94798   ** This loop also handles the case of the PRIMARY KEY index for a
94799   ** WITHOUT ROWID table.
94800   */
94801   for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
94802     int regIdx;          /* Range of registers hold conent for pIdx */
94803     int regR;            /* Range of registers holding conflicting PK */
94804     int iThisCur;        /* Cursor for this UNIQUE index */
94805     int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
94806 
94807     if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
94808     if( bAffinityDone==0 ){
94809       sqlite3TableAffinity(v, pTab, regNewData+1);
94810       bAffinityDone = 1;
94811     }
94812     iThisCur = iIdxCur+ix;
94813     addrUniqueOk = sqlite3VdbeMakeLabel(v);
94814 
94815     /* Skip partial indices for which the WHERE clause is not true */
94816     if( pIdx->pPartIdxWhere ){
94817       sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
94818       pParse->ckBase = regNewData+1;
94819       sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
94820                          SQLITE_JUMPIFNULL);
94821       pParse->ckBase = 0;
94822     }
94823 
94824     /* Create a record for this index entry as it should appear after
94825     ** the insert or update.  Store that record in the aRegIdx[ix] register
94826     */
94827     regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
94828     for(i=0; i<pIdx->nColumn; i++){
94829       int iField = pIdx->aiColumn[i];
94830       int x;
94831       if( iField<0 || iField==pTab->iPKey ){
94832         if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
94833         x = regNewData;
94834         regRowid =  pIdx->pPartIdxWhere ? -1 : regIdx+i;
94835       }else{
94836         x = iField + regNewData + 1;
94837       }
94838       sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
94839       VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
94840     }
94841     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
94842     VdbeComment((v, "for %s", pIdx->zName));
94843     sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
94844 
94845     /* In an UPDATE operation, if this index is the PRIMARY KEY index
94846     ** of a WITHOUT ROWID table and there has been no change the
94847     ** primary key, then no collision is possible.  The collision detection
94848     ** logic below can all be skipped. */
94849     if( isUpdate && pPk==pIdx && pkChng==0 ){
94850       sqlite3VdbeResolveLabel(v, addrUniqueOk);
94851       continue;
94852     }
94853 
94854     /* Find out what action to take in case there is a uniqueness conflict */
94855     onError = pIdx->onError;
94856     if( onError==OE_None ){
94857       sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
94858       sqlite3VdbeResolveLabel(v, addrUniqueOk);
94859       continue;  /* pIdx is not a UNIQUE index */
94860     }
94861     if( overrideError!=OE_Default ){
94862       onError = overrideError;
94863     }else if( onError==OE_Default ){
94864       onError = OE_Abort;
94865     }
94866 
94867     /* Check to see if the new index entry will be unique */
94868     sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
94869                          regIdx, pIdx->nKeyCol); VdbeCoverage(v);
94870 
94871     /* Generate code to handle collisions */
94872     regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
94873     if( isUpdate || onError==OE_Replace ){
94874       if( HasRowid(pTab) ){
94875         sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
94876         /* Conflict only if the rowid of the existing index entry
94877         ** is different from old-rowid */
94878         if( isUpdate ){
94879           sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
94880           sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
94881           VdbeCoverage(v);
94882         }
94883       }else{
94884         int x;
94885         /* Extract the PRIMARY KEY from the end of the index entry and
94886         ** store it in registers regR..regR+nPk-1 */
94887         if( pIdx!=pPk ){
94888           for(i=0; i<pPk->nKeyCol; i++){
94889             x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
94890             sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
94891             VdbeComment((v, "%s.%s", pTab->zName,
94892                          pTab->aCol[pPk->aiColumn[i]].zName));
94893           }
94894         }
94895         if( isUpdate ){
94896           /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
94897           ** table, only conflict if the new PRIMARY KEY values are actually
94898           ** different from the old.
94899           **
94900           ** For a UNIQUE index, only conflict if the PRIMARY KEY values
94901           ** of the matched index row are different from the original PRIMARY
94902           ** KEY values of this row before the update.  */
94903           int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
94904           int op = OP_Ne;
94905           int regCmp = (pIdx->autoIndex==2 ? regIdx : regR);
94906 
94907           for(i=0; i<pPk->nKeyCol; i++){
94908             char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
94909             x = pPk->aiColumn[i];
94910             if( i==(pPk->nKeyCol-1) ){
94911               addrJump = addrUniqueOk;
94912               op = OP_Eq;
94913             }
94914             sqlite3VdbeAddOp4(v, op,
94915                 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
94916             );
94917             sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
94918             VdbeCoverageIf(v, op==OP_Eq);
94919             VdbeCoverageIf(v, op==OP_Ne);
94920           }
94921         }
94922       }
94923     }
94924 
94925     /* Generate code that executes if the new index entry is not unique */
94926     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
94927         || onError==OE_Ignore || onError==OE_Replace );
94928     switch( onError ){
94929       case OE_Rollback:
94930       case OE_Abort:
94931       case OE_Fail: {
94932         sqlite3UniqueConstraint(pParse, onError, pIdx);
94933         break;
94934       }
94935       case OE_Ignore: {
94936         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
94937         break;
94938       }
94939       default: {
94940         Trigger *pTrigger = 0;
94941         assert( onError==OE_Replace );
94942         sqlite3MultiWrite(pParse);
94943         if( db->flags&SQLITE_RecTriggers ){
94944           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
94945         }
94946         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
94947                                  regR, nPkField, 0, OE_Replace, pIdx==pPk);
94948         seenReplace = 1;
94949         break;
94950       }
94951     }
94952     sqlite3VdbeResolveLabel(v, addrUniqueOk);
94953     sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
94954     if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
94955   }
94956   if( ipkTop ){
94957     sqlite3VdbeAddOp2(v, OP_Goto, 0, ipkTop+1);
94958     sqlite3VdbeJumpHere(v, ipkBottom);
94959   }
94960 
94961   *pbMayReplace = seenReplace;
94962   VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
94963 }
94964 
94965 /*
94966 ** This routine generates code to finish the INSERT or UPDATE operation
94967 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
94968 ** A consecutive range of registers starting at regNewData contains the
94969 ** rowid and the content to be inserted.
94970 **
94971 ** The arguments to this routine should be the same as the first six
94972 ** arguments to sqlite3GenerateConstraintChecks.
94973 */
94974 SQLITE_PRIVATE void sqlite3CompleteInsertion(
94975   Parse *pParse,      /* The parser context */
94976   Table *pTab,        /* the table into which we are inserting */
94977   int iDataCur,       /* Cursor of the canonical data source */
94978   int iIdxCur,        /* First index cursor */
94979   int regNewData,     /* Range of content */
94980   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
94981   int isUpdate,       /* True for UPDATE, False for INSERT */
94982   int appendBias,     /* True if this is likely to be an append */
94983   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
94984 ){
94985   Vdbe *v;            /* Prepared statements under construction */
94986   Index *pIdx;        /* An index being inserted or updated */
94987   u8 pik_flags;       /* flag values passed to the btree insert */
94988   int regData;        /* Content registers (after the rowid) */
94989   int regRec;         /* Register holding assemblied record for the table */
94990   int i;              /* Loop counter */
94991   u8 bAffinityDone = 0; /* True if OP_Affinity has been run already */
94992 
94993   v = sqlite3GetVdbe(pParse);
94994   assert( v!=0 );
94995   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
94996   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
94997     if( aRegIdx[i]==0 ) continue;
94998     bAffinityDone = 1;
94999     if( pIdx->pPartIdxWhere ){
95000       sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
95001       VdbeCoverage(v);
95002     }
95003     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
95004     pik_flags = 0;
95005     if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
95006     if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
95007       assert( pParse->nested==0 );
95008       pik_flags |= OPFLAG_NCHANGE;
95009     }
95010     if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
95011   }
95012   if( !HasRowid(pTab) ) return;
95013   regData = regNewData + 1;
95014   regRec = sqlite3GetTempReg(pParse);
95015   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
95016   if( !bAffinityDone ) sqlite3TableAffinity(v, pTab, 0);
95017   sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
95018   if( pParse->nested ){
95019     pik_flags = 0;
95020   }else{
95021     pik_flags = OPFLAG_NCHANGE;
95022     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
95023   }
95024   if( appendBias ){
95025     pik_flags |= OPFLAG_APPEND;
95026   }
95027   if( useSeekResult ){
95028     pik_flags |= OPFLAG_USESEEKRESULT;
95029   }
95030   sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
95031   if( !pParse->nested ){
95032     sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
95033   }
95034   sqlite3VdbeChangeP5(v, pik_flags);
95035 }
95036 
95037 /*
95038 ** Allocate cursors for the pTab table and all its indices and generate
95039 ** code to open and initialized those cursors.
95040 **
95041 ** The cursor for the object that contains the complete data (normally
95042 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
95043 ** ROWID table) is returned in *piDataCur.  The first index cursor is
95044 ** returned in *piIdxCur.  The number of indices is returned.
95045 **
95046 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
95047 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
95048 ** If iBase is negative, then allocate the next available cursor.
95049 **
95050 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
95051 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
95052 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
95053 ** pTab->pIndex list.
95054 */
95055 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
95056   Parse *pParse,   /* Parsing context */
95057   Table *pTab,     /* Table to be opened */
95058   int op,          /* OP_OpenRead or OP_OpenWrite */
95059   int iBase,       /* Use this for the table cursor, if there is one */
95060   u8 *aToOpen,     /* If not NULL: boolean for each table and index */
95061   int *piDataCur,  /* Write the database source cursor number here */
95062   int *piIdxCur    /* Write the first index cursor number here */
95063 ){
95064   int i;
95065   int iDb;
95066   int iDataCur;
95067   Index *pIdx;
95068   Vdbe *v;
95069 
95070   assert( op==OP_OpenRead || op==OP_OpenWrite );
95071   if( IsVirtual(pTab) ){
95072     assert( aToOpen==0 );
95073     *piDataCur = 0;
95074     *piIdxCur = 1;
95075     return 0;
95076   }
95077   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
95078   v = sqlite3GetVdbe(pParse);
95079   assert( v!=0 );
95080   if( iBase<0 ) iBase = pParse->nTab;
95081   iDataCur = iBase++;
95082   if( piDataCur ) *piDataCur = iDataCur;
95083   if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
95084     sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
95085   }else{
95086     sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
95087   }
95088   if( piIdxCur ) *piIdxCur = iBase;
95089   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
95090     int iIdxCur = iBase++;
95091     assert( pIdx->pSchema==pTab->pSchema );
95092     if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
95093       *piDataCur = iIdxCur;
95094     }
95095     if( aToOpen==0 || aToOpen[i+1] ){
95096       sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
95097       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
95098       VdbeComment((v, "%s", pIdx->zName));
95099     }
95100   }
95101   if( iBase>pParse->nTab ) pParse->nTab = iBase;
95102   return i;
95103 }
95104 
95105 
95106 #ifdef SQLITE_TEST
95107 /*
95108 ** The following global variable is incremented whenever the
95109 ** transfer optimization is used.  This is used for testing
95110 ** purposes only - to make sure the transfer optimization really
95111 ** is happening when it is suppose to.
95112 */
95113 SQLITE_API int sqlite3_xferopt_count;
95114 #endif /* SQLITE_TEST */
95115 
95116 
95117 #ifndef SQLITE_OMIT_XFER_OPT
95118 /*
95119 ** Check to collation names to see if they are compatible.
95120 */
95121 static int xferCompatibleCollation(const char *z1, const char *z2){
95122   if( z1==0 ){
95123     return z2==0;
95124   }
95125   if( z2==0 ){
95126     return 0;
95127   }
95128   return sqlite3StrICmp(z1, z2)==0;
95129 }
95130 
95131 
95132 /*
95133 ** Check to see if index pSrc is compatible as a source of data
95134 ** for index pDest in an insert transfer optimization.  The rules
95135 ** for a compatible index:
95136 **
95137 **    *   The index is over the same set of columns
95138 **    *   The same DESC and ASC markings occurs on all columns
95139 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
95140 **    *   The same collating sequence on each column
95141 **    *   The index has the exact same WHERE clause
95142 */
95143 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
95144   int i;
95145   assert( pDest && pSrc );
95146   assert( pDest->pTable!=pSrc->pTable );
95147   if( pDest->nKeyCol!=pSrc->nKeyCol ){
95148     return 0;   /* Different number of columns */
95149   }
95150   if( pDest->onError!=pSrc->onError ){
95151     return 0;   /* Different conflict resolution strategies */
95152   }
95153   for(i=0; i<pSrc->nKeyCol; i++){
95154     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
95155       return 0;   /* Different columns indexed */
95156     }
95157     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
95158       return 0;   /* Different sort orders */
95159     }
95160     if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
95161       return 0;   /* Different collating sequences */
95162     }
95163   }
95164   if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
95165     return 0;     /* Different WHERE clauses */
95166   }
95167 
95168   /* If no test above fails then the indices must be compatible */
95169   return 1;
95170 }
95171 
95172 /*
95173 ** Attempt the transfer optimization on INSERTs of the form
95174 **
95175 **     INSERT INTO tab1 SELECT * FROM tab2;
95176 **
95177 ** The xfer optimization transfers raw records from tab2 over to tab1.
95178 ** Columns are not decoded and reassemblied, which greatly improves
95179 ** performance.  Raw index records are transferred in the same way.
95180 **
95181 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
95182 ** There are lots of rules for determining compatibility - see comments
95183 ** embedded in the code for details.
95184 **
95185 ** This routine returns TRUE if the optimization is guaranteed to be used.
95186 ** Sometimes the xfer optimization will only work if the destination table
95187 ** is empty - a factor that can only be determined at run-time.  In that
95188 ** case, this routine generates code for the xfer optimization but also
95189 ** does a test to see if the destination table is empty and jumps over the
95190 ** xfer optimization code if the test fails.  In that case, this routine
95191 ** returns FALSE so that the caller will know to go ahead and generate
95192 ** an unoptimized transfer.  This routine also returns FALSE if there
95193 ** is no chance that the xfer optimization can be applied.
95194 **
95195 ** This optimization is particularly useful at making VACUUM run faster.
95196 */
95197 static int xferOptimization(
95198   Parse *pParse,        /* Parser context */
95199   Table *pDest,         /* The table we are inserting into */
95200   Select *pSelect,      /* A SELECT statement to use as the data source */
95201   int onError,          /* How to handle constraint errors */
95202   int iDbDest           /* The database of pDest */
95203 ){
95204   ExprList *pEList;                /* The result set of the SELECT */
95205   Table *pSrc;                     /* The table in the FROM clause of SELECT */
95206   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
95207   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
95208   int i;                           /* Loop counter */
95209   int iDbSrc;                      /* The database of pSrc */
95210   int iSrc, iDest;                 /* Cursors from source and destination */
95211   int addr1, addr2;                /* Loop addresses */
95212   int emptyDestTest = 0;           /* Address of test for empty pDest */
95213   int emptySrcTest = 0;            /* Address of test for empty pSrc */
95214   Vdbe *v;                         /* The VDBE we are building */
95215   int regAutoinc;                  /* Memory register used by AUTOINC */
95216   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
95217   int regData, regRowid;           /* Registers holding data and rowid */
95218 
95219   if( pSelect==0 ){
95220     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
95221   }
95222   if( pParse->pWith || pSelect->pWith ){
95223     /* Do not attempt to process this query if there are an WITH clauses
95224     ** attached to it. Proceeding may generate a false "no such table: xxx"
95225     ** error if pSelect reads from a CTE named "xxx".  */
95226     return 0;
95227   }
95228   if( sqlite3TriggerList(pParse, pDest) ){
95229     return 0;   /* tab1 must not have triggers */
95230   }
95231 #ifndef SQLITE_OMIT_VIRTUALTABLE
95232   if( pDest->tabFlags & TF_Virtual ){
95233     return 0;   /* tab1 must not be a virtual table */
95234   }
95235 #endif
95236   if( onError==OE_Default ){
95237     if( pDest->iPKey>=0 ) onError = pDest->keyConf;
95238     if( onError==OE_Default ) onError = OE_Abort;
95239   }
95240   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
95241   if( pSelect->pSrc->nSrc!=1 ){
95242     return 0;   /* FROM clause must have exactly one term */
95243   }
95244   if( pSelect->pSrc->a[0].pSelect ){
95245     return 0;   /* FROM clause cannot contain a subquery */
95246   }
95247   if( pSelect->pWhere ){
95248     return 0;   /* SELECT may not have a WHERE clause */
95249   }
95250   if( pSelect->pOrderBy ){
95251     return 0;   /* SELECT may not have an ORDER BY clause */
95252   }
95253   /* Do not need to test for a HAVING clause.  If HAVING is present but
95254   ** there is no ORDER BY, we will get an error. */
95255   if( pSelect->pGroupBy ){
95256     return 0;   /* SELECT may not have a GROUP BY clause */
95257   }
95258   if( pSelect->pLimit ){
95259     return 0;   /* SELECT may not have a LIMIT clause */
95260   }
95261   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
95262   if( pSelect->pPrior ){
95263     return 0;   /* SELECT may not be a compound query */
95264   }
95265   if( pSelect->selFlags & SF_Distinct ){
95266     return 0;   /* SELECT may not be DISTINCT */
95267   }
95268   pEList = pSelect->pEList;
95269   assert( pEList!=0 );
95270   if( pEList->nExpr!=1 ){
95271     return 0;   /* The result set must have exactly one column */
95272   }
95273   assert( pEList->a[0].pExpr );
95274   if( pEList->a[0].pExpr->op!=TK_ALL ){
95275     return 0;   /* The result set must be the special operator "*" */
95276   }
95277 
95278   /* At this point we have established that the statement is of the
95279   ** correct syntactic form to participate in this optimization.  Now
95280   ** we have to check the semantics.
95281   */
95282   pItem = pSelect->pSrc->a;
95283   pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
95284   if( pSrc==0 ){
95285     return 0;   /* FROM clause does not contain a real table */
95286   }
95287   if( pSrc==pDest ){
95288     return 0;   /* tab1 and tab2 may not be the same table */
95289   }
95290   if( HasRowid(pDest)!=HasRowid(pSrc) ){
95291     return 0;   /* source and destination must both be WITHOUT ROWID or not */
95292   }
95293 #ifndef SQLITE_OMIT_VIRTUALTABLE
95294   if( pSrc->tabFlags & TF_Virtual ){
95295     return 0;   /* tab2 must not be a virtual table */
95296   }
95297 #endif
95298   if( pSrc->pSelect ){
95299     return 0;   /* tab2 may not be a view */
95300   }
95301   if( pDest->nCol!=pSrc->nCol ){
95302     return 0;   /* Number of columns must be the same in tab1 and tab2 */
95303   }
95304   if( pDest->iPKey!=pSrc->iPKey ){
95305     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
95306   }
95307   for(i=0; i<pDest->nCol; i++){
95308     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
95309       return 0;    /* Affinity must be the same on all columns */
95310     }
95311     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
95312       return 0;    /* Collating sequence must be the same on all columns */
95313     }
95314     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
95315       return 0;    /* tab2 must be NOT NULL if tab1 is */
95316     }
95317   }
95318   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
95319     if( pDestIdx->onError!=OE_None ){
95320       destHasUniqueIdx = 1;
95321     }
95322     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
95323       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
95324     }
95325     if( pSrcIdx==0 ){
95326       return 0;    /* pDestIdx has no corresponding index in pSrc */
95327     }
95328   }
95329 #ifndef SQLITE_OMIT_CHECK
95330   if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
95331     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
95332   }
95333 #endif
95334 #ifndef SQLITE_OMIT_FOREIGN_KEY
95335   /* Disallow the transfer optimization if the destination table constains
95336   ** any foreign key constraints.  This is more restrictive than necessary.
95337   ** But the main beneficiary of the transfer optimization is the VACUUM
95338   ** command, and the VACUUM command disables foreign key constraints.  So
95339   ** the extra complication to make this rule less restrictive is probably
95340   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
95341   */
95342   if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
95343     return 0;
95344   }
95345 #endif
95346   if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
95347     return 0;  /* xfer opt does not play well with PRAGMA count_changes */
95348   }
95349 
95350   /* If we get this far, it means that the xfer optimization is at
95351   ** least a possibility, though it might only work if the destination
95352   ** table (tab1) is initially empty.
95353   */
95354 #ifdef SQLITE_TEST
95355   sqlite3_xferopt_count++;
95356 #endif
95357   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
95358   v = sqlite3GetVdbe(pParse);
95359   sqlite3CodeVerifySchema(pParse, iDbSrc);
95360   iSrc = pParse->nTab++;
95361   iDest = pParse->nTab++;
95362   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
95363   regData = sqlite3GetTempReg(pParse);
95364   regRowid = sqlite3GetTempReg(pParse);
95365   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
95366   assert( HasRowid(pDest) || destHasUniqueIdx );
95367   if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
95368    || destHasUniqueIdx                              /* (2) */
95369    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
95370   ){
95371     /* In some circumstances, we are able to run the xfer optimization
95372     ** only if the destination table is initially empty.  This code makes
95373     ** that determination.  Conditions under which the destination must
95374     ** be empty:
95375     **
95376     ** (1) There is no INTEGER PRIMARY KEY but there are indices.
95377     **     (If the destination is not initially empty, the rowid fields
95378     **     of index entries might need to change.)
95379     **
95380     ** (2) The destination has a unique index.  (The xfer optimization
95381     **     is unable to test uniqueness.)
95382     **
95383     ** (3) onError is something other than OE_Abort and OE_Rollback.
95384     */
95385     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); VdbeCoverage(v);
95386     emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
95387     sqlite3VdbeJumpHere(v, addr1);
95388   }
95389   if( HasRowid(pSrc) ){
95390     sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
95391     emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
95392     if( pDest->iPKey>=0 ){
95393       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
95394       addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
95395       VdbeCoverage(v);
95396       sqlite3RowidConstraint(pParse, onError, pDest);
95397       sqlite3VdbeJumpHere(v, addr2);
95398       autoIncStep(pParse, regAutoinc, regRowid);
95399     }else if( pDest->pIndex==0 ){
95400       addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
95401     }else{
95402       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
95403       assert( (pDest->tabFlags & TF_Autoincrement)==0 );
95404     }
95405     sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
95406     sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
95407     sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
95408     sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
95409     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); VdbeCoverage(v);
95410     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
95411     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
95412   }else{
95413     sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
95414     sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
95415   }
95416   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
95417     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
95418       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
95419     }
95420     assert( pSrcIdx );
95421     sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
95422     sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
95423     VdbeComment((v, "%s", pSrcIdx->zName));
95424     sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
95425     sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
95426     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
95427     VdbeComment((v, "%s", pDestIdx->zName));
95428     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); VdbeCoverage(v);
95429     sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
95430     sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
95431     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); VdbeCoverage(v);
95432     sqlite3VdbeJumpHere(v, addr1);
95433     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
95434     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
95435   }
95436   if( emptySrcTest ) sqlite3VdbeJumpHere(v, emptySrcTest);
95437   sqlite3ReleaseTempReg(pParse, regRowid);
95438   sqlite3ReleaseTempReg(pParse, regData);
95439   if( emptyDestTest ){
95440     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
95441     sqlite3VdbeJumpHere(v, emptyDestTest);
95442     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
95443     return 0;
95444   }else{
95445     return 1;
95446   }
95447 }
95448 #endif /* SQLITE_OMIT_XFER_OPT */
95449 
95450 /************** End of insert.c **********************************************/
95451 /************** Begin file legacy.c ******************************************/
95452 /*
95453 ** 2001 September 15
95454 **
95455 ** The author disclaims copyright to this source code.  In place of
95456 ** a legal notice, here is a blessing:
95457 **
95458 **    May you do good and not evil.
95459 **    May you find forgiveness for yourself and forgive others.
95460 **    May you share freely, never taking more than you give.
95461 **
95462 *************************************************************************
95463 ** Main file for the SQLite library.  The routines in this file
95464 ** implement the programmer interface to the library.  Routines in
95465 ** other files are for internal use by SQLite and should not be
95466 ** accessed by users of the library.
95467 */
95468 
95469 
95470 /*
95471 ** Execute SQL code.  Return one of the SQLITE_ success/failure
95472 ** codes.  Also write an error message into memory obtained from
95473 ** malloc() and make *pzErrMsg point to that message.
95474 **
95475 ** If the SQL is a query, then for each row in the query result
95476 ** the xCallback() function is called.  pArg becomes the first
95477 ** argument to xCallback().  If xCallback=NULL then no callback
95478 ** is invoked, even for queries.
95479 */
95480 SQLITE_API int sqlite3_exec(
95481   sqlite3 *db,                /* The database on which the SQL executes */
95482   const char *zSql,           /* The SQL to be executed */
95483   sqlite3_callback xCallback, /* Invoke this callback routine */
95484   void *pArg,                 /* First argument to xCallback() */
95485   char **pzErrMsg             /* Write error messages here */
95486 ){
95487   int rc = SQLITE_OK;         /* Return code */
95488   const char *zLeftover;      /* Tail of unprocessed SQL */
95489   sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
95490   char **azCols = 0;          /* Names of result columns */
95491   int callbackIsInit;         /* True if callback data is initialized */
95492 
95493   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
95494   if( zSql==0 ) zSql = "";
95495 
95496   sqlite3_mutex_enter(db->mutex);
95497   sqlite3Error(db, SQLITE_OK, 0);
95498   while( rc==SQLITE_OK && zSql[0] ){
95499     int nCol;
95500     char **azVals = 0;
95501 
95502     pStmt = 0;
95503     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
95504     assert( rc==SQLITE_OK || pStmt==0 );
95505     if( rc!=SQLITE_OK ){
95506       continue;
95507     }
95508     if( !pStmt ){
95509       /* this happens for a comment or white-space */
95510       zSql = zLeftover;
95511       continue;
95512     }
95513 
95514     callbackIsInit = 0;
95515     nCol = sqlite3_column_count(pStmt);
95516 
95517     while( 1 ){
95518       int i;
95519       rc = sqlite3_step(pStmt);
95520 
95521       /* Invoke the callback function if required */
95522       if( xCallback && (SQLITE_ROW==rc ||
95523           (SQLITE_DONE==rc && !callbackIsInit
95524                            && db->flags&SQLITE_NullCallback)) ){
95525         if( !callbackIsInit ){
95526           azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
95527           if( azCols==0 ){
95528             goto exec_out;
95529           }
95530           for(i=0; i<nCol; i++){
95531             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
95532             /* sqlite3VdbeSetColName() installs column names as UTF8
95533             ** strings so there is no way for sqlite3_column_name() to fail. */
95534             assert( azCols[i]!=0 );
95535           }
95536           callbackIsInit = 1;
95537         }
95538         if( rc==SQLITE_ROW ){
95539           azVals = &azCols[nCol];
95540           for(i=0; i<nCol; i++){
95541             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
95542             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
95543               db->mallocFailed = 1;
95544               goto exec_out;
95545             }
95546           }
95547         }
95548         if( xCallback(pArg, nCol, azVals, azCols) ){
95549           rc = SQLITE_ABORT;
95550           sqlite3VdbeFinalize((Vdbe *)pStmt);
95551           pStmt = 0;
95552           sqlite3Error(db, SQLITE_ABORT, 0);
95553           goto exec_out;
95554         }
95555       }
95556 
95557       if( rc!=SQLITE_ROW ){
95558         rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
95559         pStmt = 0;
95560         zSql = zLeftover;
95561         while( sqlite3Isspace(zSql[0]) ) zSql++;
95562         break;
95563       }
95564     }
95565 
95566     sqlite3DbFree(db, azCols);
95567     azCols = 0;
95568   }
95569 
95570 exec_out:
95571   if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
95572   sqlite3DbFree(db, azCols);
95573 
95574   rc = sqlite3ApiExit(db, rc);
95575   if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
95576     int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
95577     *pzErrMsg = sqlite3Malloc(nErrMsg);
95578     if( *pzErrMsg ){
95579       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
95580     }else{
95581       rc = SQLITE_NOMEM;
95582       sqlite3Error(db, SQLITE_NOMEM, 0);
95583     }
95584   }else if( pzErrMsg ){
95585     *pzErrMsg = 0;
95586   }
95587 
95588   assert( (rc&db->errMask)==rc );
95589   sqlite3_mutex_leave(db->mutex);
95590   return rc;
95591 }
95592 
95593 /************** End of legacy.c **********************************************/
95594 /************** Begin file loadext.c *****************************************/
95595 /*
95596 ** 2006 June 7
95597 **
95598 ** The author disclaims copyright to this source code.  In place of
95599 ** a legal notice, here is a blessing:
95600 **
95601 **    May you do good and not evil.
95602 **    May you find forgiveness for yourself and forgive others.
95603 **    May you share freely, never taking more than you give.
95604 **
95605 *************************************************************************
95606 ** This file contains code used to dynamically load extensions into
95607 ** the SQLite library.
95608 */
95609 
95610 #ifndef SQLITE_CORE
95611   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
95612 #endif
95613 /************** Include sqlite3ext.h in the middle of loadext.c **************/
95614 /************** Begin file sqlite3ext.h **************************************/
95615 /*
95616 ** 2006 June 7
95617 **
95618 ** The author disclaims copyright to this source code.  In place of
95619 ** a legal notice, here is a blessing:
95620 **
95621 **    May you do good and not evil.
95622 **    May you find forgiveness for yourself and forgive others.
95623 **    May you share freely, never taking more than you give.
95624 **
95625 *************************************************************************
95626 ** This header file defines the SQLite interface for use by
95627 ** shared libraries that want to be imported as extensions into
95628 ** an SQLite instance.  Shared libraries that intend to be loaded
95629 ** as extensions by SQLite should #include this file instead of
95630 ** sqlite3.h.
95631 */
95632 #ifndef _SQLITE3EXT_H_
95633 #define _SQLITE3EXT_H_
95634 
95635 typedef struct sqlite3_api_routines sqlite3_api_routines;
95636 
95637 /*
95638 ** The following structure holds pointers to all of the SQLite API
95639 ** routines.
95640 **
95641 ** WARNING:  In order to maintain backwards compatibility, add new
95642 ** interfaces to the end of this structure only.  If you insert new
95643 ** interfaces in the middle of this structure, then older different
95644 ** versions of SQLite will not be able to load each others' shared
95645 ** libraries!
95646 */
95647 struct sqlite3_api_routines {
95648   void * (*aggregate_context)(sqlite3_context*,int nBytes);
95649   int  (*aggregate_count)(sqlite3_context*);
95650   int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
95651   int  (*bind_double)(sqlite3_stmt*,int,double);
95652   int  (*bind_int)(sqlite3_stmt*,int,int);
95653   int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
95654   int  (*bind_null)(sqlite3_stmt*,int);
95655   int  (*bind_parameter_count)(sqlite3_stmt*);
95656   int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
95657   const char * (*bind_parameter_name)(sqlite3_stmt*,int);
95658   int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
95659   int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
95660   int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
95661   int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
95662   int  (*busy_timeout)(sqlite3*,int ms);
95663   int  (*changes)(sqlite3*);
95664   int  (*close)(sqlite3*);
95665   int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
95666                            int eTextRep,const char*));
95667   int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
95668                              int eTextRep,const void*));
95669   const void * (*column_blob)(sqlite3_stmt*,int iCol);
95670   int  (*column_bytes)(sqlite3_stmt*,int iCol);
95671   int  (*column_bytes16)(sqlite3_stmt*,int iCol);
95672   int  (*column_count)(sqlite3_stmt*pStmt);
95673   const char * (*column_database_name)(sqlite3_stmt*,int);
95674   const void * (*column_database_name16)(sqlite3_stmt*,int);
95675   const char * (*column_decltype)(sqlite3_stmt*,int i);
95676   const void * (*column_decltype16)(sqlite3_stmt*,int);
95677   double  (*column_double)(sqlite3_stmt*,int iCol);
95678   int  (*column_int)(sqlite3_stmt*,int iCol);
95679   sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
95680   const char * (*column_name)(sqlite3_stmt*,int);
95681   const void * (*column_name16)(sqlite3_stmt*,int);
95682   const char * (*column_origin_name)(sqlite3_stmt*,int);
95683   const void * (*column_origin_name16)(sqlite3_stmt*,int);
95684   const char * (*column_table_name)(sqlite3_stmt*,int);
95685   const void * (*column_table_name16)(sqlite3_stmt*,int);
95686   const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
95687   const void * (*column_text16)(sqlite3_stmt*,int iCol);
95688   int  (*column_type)(sqlite3_stmt*,int iCol);
95689   sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
95690   void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
95691   int  (*complete)(const char*sql);
95692   int  (*complete16)(const void*sql);
95693   int  (*create_collation)(sqlite3*,const char*,int,void*,
95694                            int(*)(void*,int,const void*,int,const void*));
95695   int  (*create_collation16)(sqlite3*,const void*,int,void*,
95696                              int(*)(void*,int,const void*,int,const void*));
95697   int  (*create_function)(sqlite3*,const char*,int,int,void*,
95698                           void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
95699                           void (*xStep)(sqlite3_context*,int,sqlite3_value**),
95700                           void (*xFinal)(sqlite3_context*));
95701   int  (*create_function16)(sqlite3*,const void*,int,int,void*,
95702                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
95703                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
95704                             void (*xFinal)(sqlite3_context*));
95705   int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
95706   int  (*data_count)(sqlite3_stmt*pStmt);
95707   sqlite3 * (*db_handle)(sqlite3_stmt*);
95708   int (*declare_vtab)(sqlite3*,const char*);
95709   int  (*enable_shared_cache)(int);
95710   int  (*errcode)(sqlite3*db);
95711   const char * (*errmsg)(sqlite3*);
95712   const void * (*errmsg16)(sqlite3*);
95713   int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
95714   int  (*expired)(sqlite3_stmt*);
95715   int  (*finalize)(sqlite3_stmt*pStmt);
95716   void  (*free)(void*);
95717   void  (*free_table)(char**result);
95718   int  (*get_autocommit)(sqlite3*);
95719   void * (*get_auxdata)(sqlite3_context*,int);
95720   int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
95721   int  (*global_recover)(void);
95722   void  (*interruptx)(sqlite3*);
95723   sqlite_int64  (*last_insert_rowid)(sqlite3*);
95724   const char * (*libversion)(void);
95725   int  (*libversion_number)(void);
95726   void *(*malloc)(int);
95727   char * (*mprintf)(const char*,...);
95728   int  (*open)(const char*,sqlite3**);
95729   int  (*open16)(const void*,sqlite3**);
95730   int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
95731   int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
95732   void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
95733   void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
95734   void *(*realloc)(void*,int);
95735   int  (*reset)(sqlite3_stmt*pStmt);
95736   void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
95737   void  (*result_double)(sqlite3_context*,double);
95738   void  (*result_error)(sqlite3_context*,const char*,int);
95739   void  (*result_error16)(sqlite3_context*,const void*,int);
95740   void  (*result_int)(sqlite3_context*,int);
95741   void  (*result_int64)(sqlite3_context*,sqlite_int64);
95742   void  (*result_null)(sqlite3_context*);
95743   void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
95744   void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
95745   void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
95746   void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
95747   void  (*result_value)(sqlite3_context*,sqlite3_value*);
95748   void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
95749   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
95750                          const char*,const char*),void*);
95751   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
95752   char * (*snprintf)(int,char*,const char*,...);
95753   int  (*step)(sqlite3_stmt*);
95754   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
95755                                 char const**,char const**,int*,int*,int*);
95756   void  (*thread_cleanup)(void);
95757   int  (*total_changes)(sqlite3*);
95758   void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
95759   int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
95760   void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
95761                                          sqlite_int64),void*);
95762   void * (*user_data)(sqlite3_context*);
95763   const void * (*value_blob)(sqlite3_value*);
95764   int  (*value_bytes)(sqlite3_value*);
95765   int  (*value_bytes16)(sqlite3_value*);
95766   double  (*value_double)(sqlite3_value*);
95767   int  (*value_int)(sqlite3_value*);
95768   sqlite_int64  (*value_int64)(sqlite3_value*);
95769   int  (*value_numeric_type)(sqlite3_value*);
95770   const unsigned char * (*value_text)(sqlite3_value*);
95771   const void * (*value_text16)(sqlite3_value*);
95772   const void * (*value_text16be)(sqlite3_value*);
95773   const void * (*value_text16le)(sqlite3_value*);
95774   int  (*value_type)(sqlite3_value*);
95775   char *(*vmprintf)(const char*,va_list);
95776   /* Added ??? */
95777   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
95778   /* Added by 3.3.13 */
95779   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
95780   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
95781   int (*clear_bindings)(sqlite3_stmt*);
95782   /* Added by 3.4.1 */
95783   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
95784                           void (*xDestroy)(void *));
95785   /* Added by 3.5.0 */
95786   int (*bind_zeroblob)(sqlite3_stmt*,int,int);
95787   int (*blob_bytes)(sqlite3_blob*);
95788   int (*blob_close)(sqlite3_blob*);
95789   int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
95790                    int,sqlite3_blob**);
95791   int (*blob_read)(sqlite3_blob*,void*,int,int);
95792   int (*blob_write)(sqlite3_blob*,const void*,int,int);
95793   int (*create_collation_v2)(sqlite3*,const char*,int,void*,
95794                              int(*)(void*,int,const void*,int,const void*),
95795                              void(*)(void*));
95796   int (*file_control)(sqlite3*,const char*,int,void*);
95797   sqlite3_int64 (*memory_highwater)(int);
95798   sqlite3_int64 (*memory_used)(void);
95799   sqlite3_mutex *(*mutex_alloc)(int);
95800   void (*mutex_enter)(sqlite3_mutex*);
95801   void (*mutex_free)(sqlite3_mutex*);
95802   void (*mutex_leave)(sqlite3_mutex*);
95803   int (*mutex_try)(sqlite3_mutex*);
95804   int (*open_v2)(const char*,sqlite3**,int,const char*);
95805   int (*release_memory)(int);
95806   void (*result_error_nomem)(sqlite3_context*);
95807   void (*result_error_toobig)(sqlite3_context*);
95808   int (*sleep)(int);
95809   void (*soft_heap_limit)(int);
95810   sqlite3_vfs *(*vfs_find)(const char*);
95811   int (*vfs_register)(sqlite3_vfs*,int);
95812   int (*vfs_unregister)(sqlite3_vfs*);
95813   int (*xthreadsafe)(void);
95814   void (*result_zeroblob)(sqlite3_context*,int);
95815   void (*result_error_code)(sqlite3_context*,int);
95816   int (*test_control)(int, ...);
95817   void (*randomness)(int,void*);
95818   sqlite3 *(*context_db_handle)(sqlite3_context*);
95819   int (*extended_result_codes)(sqlite3*,int);
95820   int (*limit)(sqlite3*,int,int);
95821   sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
95822   const char *(*sql)(sqlite3_stmt*);
95823   int (*status)(int,int*,int*,int);
95824   int (*backup_finish)(sqlite3_backup*);
95825   sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
95826   int (*backup_pagecount)(sqlite3_backup*);
95827   int (*backup_remaining)(sqlite3_backup*);
95828   int (*backup_step)(sqlite3_backup*,int);
95829   const char *(*compileoption_get)(int);
95830   int (*compileoption_used)(const char*);
95831   int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
95832                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
95833                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
95834                             void (*xFinal)(sqlite3_context*),
95835                             void(*xDestroy)(void*));
95836   int (*db_config)(sqlite3*,int,...);
95837   sqlite3_mutex *(*db_mutex)(sqlite3*);
95838   int (*db_status)(sqlite3*,int,int*,int*,int);
95839   int (*extended_errcode)(sqlite3*);
95840   void (*log)(int,const char*,...);
95841   sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
95842   const char *(*sourceid)(void);
95843   int (*stmt_status)(sqlite3_stmt*,int,int);
95844   int (*strnicmp)(const char*,const char*,int);
95845   int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
95846   int (*wal_autocheckpoint)(sqlite3*,int);
95847   int (*wal_checkpoint)(sqlite3*,const char*);
95848   void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
95849   int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
95850   int (*vtab_config)(sqlite3*,int op,...);
95851   int (*vtab_on_conflict)(sqlite3*);
95852   /* Version 3.7.16 and later */
95853   int (*close_v2)(sqlite3*);
95854   const char *(*db_filename)(sqlite3*,const char*);
95855   int (*db_readonly)(sqlite3*,const char*);
95856   int (*db_release_memory)(sqlite3*);
95857   const char *(*errstr)(int);
95858   int (*stmt_busy)(sqlite3_stmt*);
95859   int (*stmt_readonly)(sqlite3_stmt*);
95860   int (*stricmp)(const char*,const char*);
95861   int (*uri_boolean)(const char*,const char*,int);
95862   sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
95863   const char *(*uri_parameter)(const char*,const char*);
95864   char *(*vsnprintf)(int,char*,const char*,va_list);
95865   int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
95866 };
95867 
95868 /*
95869 ** The following macros redefine the API routines so that they are
95870 ** redirected throught the global sqlite3_api structure.
95871 **
95872 ** This header file is also used by the loadext.c source file
95873 ** (part of the main SQLite library - not an extension) so that
95874 ** it can get access to the sqlite3_api_routines structure
95875 ** definition.  But the main library does not want to redefine
95876 ** the API.  So the redefinition macros are only valid if the
95877 ** SQLITE_CORE macros is undefined.
95878 */
95879 #ifndef SQLITE_CORE
95880 #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
95881 #ifndef SQLITE_OMIT_DEPRECATED
95882 #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
95883 #endif
95884 #define sqlite3_bind_blob              sqlite3_api->bind_blob
95885 #define sqlite3_bind_double            sqlite3_api->bind_double
95886 #define sqlite3_bind_int               sqlite3_api->bind_int
95887 #define sqlite3_bind_int64             sqlite3_api->bind_int64
95888 #define sqlite3_bind_null              sqlite3_api->bind_null
95889 #define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
95890 #define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
95891 #define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
95892 #define sqlite3_bind_text              sqlite3_api->bind_text
95893 #define sqlite3_bind_text16            sqlite3_api->bind_text16
95894 #define sqlite3_bind_value             sqlite3_api->bind_value
95895 #define sqlite3_busy_handler           sqlite3_api->busy_handler
95896 #define sqlite3_busy_timeout           sqlite3_api->busy_timeout
95897 #define sqlite3_changes                sqlite3_api->changes
95898 #define sqlite3_close                  sqlite3_api->close
95899 #define sqlite3_collation_needed       sqlite3_api->collation_needed
95900 #define sqlite3_collation_needed16     sqlite3_api->collation_needed16
95901 #define sqlite3_column_blob            sqlite3_api->column_blob
95902 #define sqlite3_column_bytes           sqlite3_api->column_bytes
95903 #define sqlite3_column_bytes16         sqlite3_api->column_bytes16
95904 #define sqlite3_column_count           sqlite3_api->column_count
95905 #define sqlite3_column_database_name   sqlite3_api->column_database_name
95906 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
95907 #define sqlite3_column_decltype        sqlite3_api->column_decltype
95908 #define sqlite3_column_decltype16      sqlite3_api->column_decltype16
95909 #define sqlite3_column_double          sqlite3_api->column_double
95910 #define sqlite3_column_int             sqlite3_api->column_int
95911 #define sqlite3_column_int64           sqlite3_api->column_int64
95912 #define sqlite3_column_name            sqlite3_api->column_name
95913 #define sqlite3_column_name16          sqlite3_api->column_name16
95914 #define sqlite3_column_origin_name     sqlite3_api->column_origin_name
95915 #define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
95916 #define sqlite3_column_table_name      sqlite3_api->column_table_name
95917 #define sqlite3_column_table_name16    sqlite3_api->column_table_name16
95918 #define sqlite3_column_text            sqlite3_api->column_text
95919 #define sqlite3_column_text16          sqlite3_api->column_text16
95920 #define sqlite3_column_type            sqlite3_api->column_type
95921 #define sqlite3_column_value           sqlite3_api->column_value
95922 #define sqlite3_commit_hook            sqlite3_api->commit_hook
95923 #define sqlite3_complete               sqlite3_api->complete
95924 #define sqlite3_complete16             sqlite3_api->complete16
95925 #define sqlite3_create_collation       sqlite3_api->create_collation
95926 #define sqlite3_create_collation16     sqlite3_api->create_collation16
95927 #define sqlite3_create_function        sqlite3_api->create_function
95928 #define sqlite3_create_function16      sqlite3_api->create_function16
95929 #define sqlite3_create_module          sqlite3_api->create_module
95930 #define sqlite3_create_module_v2       sqlite3_api->create_module_v2
95931 #define sqlite3_data_count             sqlite3_api->data_count
95932 #define sqlite3_db_handle              sqlite3_api->db_handle
95933 #define sqlite3_declare_vtab           sqlite3_api->declare_vtab
95934 #define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
95935 #define sqlite3_errcode                sqlite3_api->errcode
95936 #define sqlite3_errmsg                 sqlite3_api->errmsg
95937 #define sqlite3_errmsg16               sqlite3_api->errmsg16
95938 #define sqlite3_exec                   sqlite3_api->exec
95939 #ifndef SQLITE_OMIT_DEPRECATED
95940 #define sqlite3_expired                sqlite3_api->expired
95941 #endif
95942 #define sqlite3_finalize               sqlite3_api->finalize
95943 #define sqlite3_free                   sqlite3_api->free
95944 #define sqlite3_free_table             sqlite3_api->free_table
95945 #define sqlite3_get_autocommit         sqlite3_api->get_autocommit
95946 #define sqlite3_get_auxdata            sqlite3_api->get_auxdata
95947 #define sqlite3_get_table              sqlite3_api->get_table
95948 #ifndef SQLITE_OMIT_DEPRECATED
95949 #define sqlite3_global_recover         sqlite3_api->global_recover
95950 #endif
95951 #define sqlite3_interrupt              sqlite3_api->interruptx
95952 #define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
95953 #define sqlite3_libversion             sqlite3_api->libversion
95954 #define sqlite3_libversion_number      sqlite3_api->libversion_number
95955 #define sqlite3_malloc                 sqlite3_api->malloc
95956 #define sqlite3_mprintf                sqlite3_api->mprintf
95957 #define sqlite3_open                   sqlite3_api->open
95958 #define sqlite3_open16                 sqlite3_api->open16
95959 #define sqlite3_prepare                sqlite3_api->prepare
95960 #define sqlite3_prepare16              sqlite3_api->prepare16
95961 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
95962 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
95963 #define sqlite3_profile                sqlite3_api->profile
95964 #define sqlite3_progress_handler       sqlite3_api->progress_handler
95965 #define sqlite3_realloc                sqlite3_api->realloc
95966 #define sqlite3_reset                  sqlite3_api->reset
95967 #define sqlite3_result_blob            sqlite3_api->result_blob
95968 #define sqlite3_result_double          sqlite3_api->result_double
95969 #define sqlite3_result_error           sqlite3_api->result_error
95970 #define sqlite3_result_error16         sqlite3_api->result_error16
95971 #define sqlite3_result_int             sqlite3_api->result_int
95972 #define sqlite3_result_int64           sqlite3_api->result_int64
95973 #define sqlite3_result_null            sqlite3_api->result_null
95974 #define sqlite3_result_text            sqlite3_api->result_text
95975 #define sqlite3_result_text16          sqlite3_api->result_text16
95976 #define sqlite3_result_text16be        sqlite3_api->result_text16be
95977 #define sqlite3_result_text16le        sqlite3_api->result_text16le
95978 #define sqlite3_result_value           sqlite3_api->result_value
95979 #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
95980 #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
95981 #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
95982 #define sqlite3_snprintf               sqlite3_api->snprintf
95983 #define sqlite3_step                   sqlite3_api->step
95984 #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
95985 #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
95986 #define sqlite3_total_changes          sqlite3_api->total_changes
95987 #define sqlite3_trace                  sqlite3_api->trace
95988 #ifndef SQLITE_OMIT_DEPRECATED
95989 #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
95990 #endif
95991 #define sqlite3_update_hook            sqlite3_api->update_hook
95992 #define sqlite3_user_data              sqlite3_api->user_data
95993 #define sqlite3_value_blob             sqlite3_api->value_blob
95994 #define sqlite3_value_bytes            sqlite3_api->value_bytes
95995 #define sqlite3_value_bytes16          sqlite3_api->value_bytes16
95996 #define sqlite3_value_double           sqlite3_api->value_double
95997 #define sqlite3_value_int              sqlite3_api->value_int
95998 #define sqlite3_value_int64            sqlite3_api->value_int64
95999 #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
96000 #define sqlite3_value_text             sqlite3_api->value_text
96001 #define sqlite3_value_text16           sqlite3_api->value_text16
96002 #define sqlite3_value_text16be         sqlite3_api->value_text16be
96003 #define sqlite3_value_text16le         sqlite3_api->value_text16le
96004 #define sqlite3_value_type             sqlite3_api->value_type
96005 #define sqlite3_vmprintf               sqlite3_api->vmprintf
96006 #define sqlite3_overload_function      sqlite3_api->overload_function
96007 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
96008 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
96009 #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
96010 #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
96011 #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
96012 #define sqlite3_blob_close             sqlite3_api->blob_close
96013 #define sqlite3_blob_open              sqlite3_api->blob_open
96014 #define sqlite3_blob_read              sqlite3_api->blob_read
96015 #define sqlite3_blob_write             sqlite3_api->blob_write
96016 #define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
96017 #define sqlite3_file_control           sqlite3_api->file_control
96018 #define sqlite3_memory_highwater       sqlite3_api->memory_highwater
96019 #define sqlite3_memory_used            sqlite3_api->memory_used
96020 #define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
96021 #define sqlite3_mutex_enter            sqlite3_api->mutex_enter
96022 #define sqlite3_mutex_free             sqlite3_api->mutex_free
96023 #define sqlite3_mutex_leave            sqlite3_api->mutex_leave
96024 #define sqlite3_mutex_try              sqlite3_api->mutex_try
96025 #define sqlite3_open_v2                sqlite3_api->open_v2
96026 #define sqlite3_release_memory         sqlite3_api->release_memory
96027 #define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
96028 #define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
96029 #define sqlite3_sleep                  sqlite3_api->sleep
96030 #define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
96031 #define sqlite3_vfs_find               sqlite3_api->vfs_find
96032 #define sqlite3_vfs_register           sqlite3_api->vfs_register
96033 #define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
96034 #define sqlite3_threadsafe             sqlite3_api->xthreadsafe
96035 #define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
96036 #define sqlite3_result_error_code      sqlite3_api->result_error_code
96037 #define sqlite3_test_control           sqlite3_api->test_control
96038 #define sqlite3_randomness             sqlite3_api->randomness
96039 #define sqlite3_context_db_handle      sqlite3_api->context_db_handle
96040 #define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
96041 #define sqlite3_limit                  sqlite3_api->limit
96042 #define sqlite3_next_stmt              sqlite3_api->next_stmt
96043 #define sqlite3_sql                    sqlite3_api->sql
96044 #define sqlite3_status                 sqlite3_api->status
96045 #define sqlite3_backup_finish          sqlite3_api->backup_finish
96046 #define sqlite3_backup_init            sqlite3_api->backup_init
96047 #define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
96048 #define sqlite3_backup_remaining       sqlite3_api->backup_remaining
96049 #define sqlite3_backup_step            sqlite3_api->backup_step
96050 #define sqlite3_compileoption_get      sqlite3_api->compileoption_get
96051 #define sqlite3_compileoption_used     sqlite3_api->compileoption_used
96052 #define sqlite3_create_function_v2     sqlite3_api->create_function_v2
96053 #define sqlite3_db_config              sqlite3_api->db_config
96054 #define sqlite3_db_mutex               sqlite3_api->db_mutex
96055 #define sqlite3_db_status              sqlite3_api->db_status
96056 #define sqlite3_extended_errcode       sqlite3_api->extended_errcode
96057 #define sqlite3_log                    sqlite3_api->log
96058 #define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
96059 #define sqlite3_sourceid               sqlite3_api->sourceid
96060 #define sqlite3_stmt_status            sqlite3_api->stmt_status
96061 #define sqlite3_strnicmp               sqlite3_api->strnicmp
96062 #define sqlite3_unlock_notify          sqlite3_api->unlock_notify
96063 #define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
96064 #define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
96065 #define sqlite3_wal_hook               sqlite3_api->wal_hook
96066 #define sqlite3_blob_reopen            sqlite3_api->blob_reopen
96067 #define sqlite3_vtab_config            sqlite3_api->vtab_config
96068 #define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
96069 /* Version 3.7.16 and later */
96070 #define sqlite3_close_v2               sqlite3_api->close_v2
96071 #define sqlite3_db_filename            sqlite3_api->db_filename
96072 #define sqlite3_db_readonly            sqlite3_api->db_readonly
96073 #define sqlite3_db_release_memory      sqlite3_api->db_release_memory
96074 #define sqlite3_errstr                 sqlite3_api->errstr
96075 #define sqlite3_stmt_busy              sqlite3_api->stmt_busy
96076 #define sqlite3_stmt_readonly          sqlite3_api->stmt_readonly
96077 #define sqlite3_stricmp                sqlite3_api->stricmp
96078 #define sqlite3_uri_boolean            sqlite3_api->uri_boolean
96079 #define sqlite3_uri_int64              sqlite3_api->uri_int64
96080 #define sqlite3_uri_parameter          sqlite3_api->uri_parameter
96081 #define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
96082 #define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
96083 #endif /* SQLITE_CORE */
96084 
96085 #ifndef SQLITE_CORE
96086   /* This case when the file really is being compiled as a loadable
96087   ** extension */
96088 # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
96089 # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
96090 # define SQLITE_EXTENSION_INIT3     \
96091     extern const sqlite3_api_routines *sqlite3_api;
96092 #else
96093   /* This case when the file is being statically linked into the
96094   ** application */
96095 # define SQLITE_EXTENSION_INIT1     /*no-op*/
96096 # define SQLITE_EXTENSION_INIT2(v)  (void)v; /* unused parameter */
96097 # define SQLITE_EXTENSION_INIT3     /*no-op*/
96098 #endif
96099 
96100 #endif /* _SQLITE3EXT_H_ */
96101 
96102 /************** End of sqlite3ext.h ******************************************/
96103 /************** Continuing where we left off in loadext.c ********************/
96104 /* #include <string.h> */
96105 
96106 #ifndef SQLITE_OMIT_LOAD_EXTENSION
96107 
96108 /*
96109 ** Some API routines are omitted when various features are
96110 ** excluded from a build of SQLite.  Substitute a NULL pointer
96111 ** for any missing APIs.
96112 */
96113 #ifndef SQLITE_ENABLE_COLUMN_METADATA
96114 # define sqlite3_column_database_name   0
96115 # define sqlite3_column_database_name16 0
96116 # define sqlite3_column_table_name      0
96117 # define sqlite3_column_table_name16    0
96118 # define sqlite3_column_origin_name     0
96119 # define sqlite3_column_origin_name16   0
96120 # define sqlite3_table_column_metadata  0
96121 #endif
96122 
96123 #ifdef SQLITE_OMIT_AUTHORIZATION
96124 # define sqlite3_set_authorizer         0
96125 #endif
96126 
96127 #ifdef SQLITE_OMIT_UTF16
96128 # define sqlite3_bind_text16            0
96129 # define sqlite3_collation_needed16     0
96130 # define sqlite3_column_decltype16      0
96131 # define sqlite3_column_name16          0
96132 # define sqlite3_column_text16          0
96133 # define sqlite3_complete16             0
96134 # define sqlite3_create_collation16     0
96135 # define sqlite3_create_function16      0
96136 # define sqlite3_errmsg16               0
96137 # define sqlite3_open16                 0
96138 # define sqlite3_prepare16              0
96139 # define sqlite3_prepare16_v2           0
96140 # define sqlite3_result_error16         0
96141 # define sqlite3_result_text16          0
96142 # define sqlite3_result_text16be        0
96143 # define sqlite3_result_text16le        0
96144 # define sqlite3_value_text16           0
96145 # define sqlite3_value_text16be         0
96146 # define sqlite3_value_text16le         0
96147 # define sqlite3_column_database_name16 0
96148 # define sqlite3_column_table_name16    0
96149 # define sqlite3_column_origin_name16   0
96150 #endif
96151 
96152 #ifdef SQLITE_OMIT_COMPLETE
96153 # define sqlite3_complete 0
96154 # define sqlite3_complete16 0
96155 #endif
96156 
96157 #ifdef SQLITE_OMIT_DECLTYPE
96158 # define sqlite3_column_decltype16      0
96159 # define sqlite3_column_decltype        0
96160 #endif
96161 
96162 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
96163 # define sqlite3_progress_handler 0
96164 #endif
96165 
96166 #ifdef SQLITE_OMIT_VIRTUALTABLE
96167 # define sqlite3_create_module 0
96168 # define sqlite3_create_module_v2 0
96169 # define sqlite3_declare_vtab 0
96170 # define sqlite3_vtab_config 0
96171 # define sqlite3_vtab_on_conflict 0
96172 #endif
96173 
96174 #ifdef SQLITE_OMIT_SHARED_CACHE
96175 # define sqlite3_enable_shared_cache 0
96176 #endif
96177 
96178 #ifdef SQLITE_OMIT_TRACE
96179 # define sqlite3_profile       0
96180 # define sqlite3_trace         0
96181 #endif
96182 
96183 #ifdef SQLITE_OMIT_GET_TABLE
96184 # define sqlite3_free_table    0
96185 # define sqlite3_get_table     0
96186 #endif
96187 
96188 #ifdef SQLITE_OMIT_INCRBLOB
96189 #define sqlite3_bind_zeroblob  0
96190 #define sqlite3_blob_bytes     0
96191 #define sqlite3_blob_close     0
96192 #define sqlite3_blob_open      0
96193 #define sqlite3_blob_read      0
96194 #define sqlite3_blob_write     0
96195 #define sqlite3_blob_reopen    0
96196 #endif
96197 
96198 /*
96199 ** The following structure contains pointers to all SQLite API routines.
96200 ** A pointer to this structure is passed into extensions when they are
96201 ** loaded so that the extension can make calls back into the SQLite
96202 ** library.
96203 **
96204 ** When adding new APIs, add them to the bottom of this structure
96205 ** in order to preserve backwards compatibility.
96206 **
96207 ** Extensions that use newer APIs should first call the
96208 ** sqlite3_libversion_number() to make sure that the API they
96209 ** intend to use is supported by the library.  Extensions should
96210 ** also check to make sure that the pointer to the function is
96211 ** not NULL before calling it.
96212 */
96213 static const sqlite3_api_routines sqlite3Apis = {
96214   sqlite3_aggregate_context,
96215 #ifndef SQLITE_OMIT_DEPRECATED
96216   sqlite3_aggregate_count,
96217 #else
96218   0,
96219 #endif
96220   sqlite3_bind_blob,
96221   sqlite3_bind_double,
96222   sqlite3_bind_int,
96223   sqlite3_bind_int64,
96224   sqlite3_bind_null,
96225   sqlite3_bind_parameter_count,
96226   sqlite3_bind_parameter_index,
96227   sqlite3_bind_parameter_name,
96228   sqlite3_bind_text,
96229   sqlite3_bind_text16,
96230   sqlite3_bind_value,
96231   sqlite3_busy_handler,
96232   sqlite3_busy_timeout,
96233   sqlite3_changes,
96234   sqlite3_close,
96235   sqlite3_collation_needed,
96236   sqlite3_collation_needed16,
96237   sqlite3_column_blob,
96238   sqlite3_column_bytes,
96239   sqlite3_column_bytes16,
96240   sqlite3_column_count,
96241   sqlite3_column_database_name,
96242   sqlite3_column_database_name16,
96243   sqlite3_column_decltype,
96244   sqlite3_column_decltype16,
96245   sqlite3_column_double,
96246   sqlite3_column_int,
96247   sqlite3_column_int64,
96248   sqlite3_column_name,
96249   sqlite3_column_name16,
96250   sqlite3_column_origin_name,
96251   sqlite3_column_origin_name16,
96252   sqlite3_column_table_name,
96253   sqlite3_column_table_name16,
96254   sqlite3_column_text,
96255   sqlite3_column_text16,
96256   sqlite3_column_type,
96257   sqlite3_column_value,
96258   sqlite3_commit_hook,
96259   sqlite3_complete,
96260   sqlite3_complete16,
96261   sqlite3_create_collation,
96262   sqlite3_create_collation16,
96263   sqlite3_create_function,
96264   sqlite3_create_function16,
96265   sqlite3_create_module,
96266   sqlite3_data_count,
96267   sqlite3_db_handle,
96268   sqlite3_declare_vtab,
96269   sqlite3_enable_shared_cache,
96270   sqlite3_errcode,
96271   sqlite3_errmsg,
96272   sqlite3_errmsg16,
96273   sqlite3_exec,
96274 #ifndef SQLITE_OMIT_DEPRECATED
96275   sqlite3_expired,
96276 #else
96277   0,
96278 #endif
96279   sqlite3_finalize,
96280   sqlite3_free,
96281   sqlite3_free_table,
96282   sqlite3_get_autocommit,
96283   sqlite3_get_auxdata,
96284   sqlite3_get_table,
96285   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
96286   sqlite3_interrupt,
96287   sqlite3_last_insert_rowid,
96288   sqlite3_libversion,
96289   sqlite3_libversion_number,
96290   sqlite3_malloc,
96291   sqlite3_mprintf,
96292   sqlite3_open,
96293   sqlite3_open16,
96294   sqlite3_prepare,
96295   sqlite3_prepare16,
96296   sqlite3_profile,
96297   sqlite3_progress_handler,
96298   sqlite3_realloc,
96299   sqlite3_reset,
96300   sqlite3_result_blob,
96301   sqlite3_result_double,
96302   sqlite3_result_error,
96303   sqlite3_result_error16,
96304   sqlite3_result_int,
96305   sqlite3_result_int64,
96306   sqlite3_result_null,
96307   sqlite3_result_text,
96308   sqlite3_result_text16,
96309   sqlite3_result_text16be,
96310   sqlite3_result_text16le,
96311   sqlite3_result_value,
96312   sqlite3_rollback_hook,
96313   sqlite3_set_authorizer,
96314   sqlite3_set_auxdata,
96315   sqlite3_snprintf,
96316   sqlite3_step,
96317   sqlite3_table_column_metadata,
96318 #ifndef SQLITE_OMIT_DEPRECATED
96319   sqlite3_thread_cleanup,
96320 #else
96321   0,
96322 #endif
96323   sqlite3_total_changes,
96324   sqlite3_trace,
96325 #ifndef SQLITE_OMIT_DEPRECATED
96326   sqlite3_transfer_bindings,
96327 #else
96328   0,
96329 #endif
96330   sqlite3_update_hook,
96331   sqlite3_user_data,
96332   sqlite3_value_blob,
96333   sqlite3_value_bytes,
96334   sqlite3_value_bytes16,
96335   sqlite3_value_double,
96336   sqlite3_value_int,
96337   sqlite3_value_int64,
96338   sqlite3_value_numeric_type,
96339   sqlite3_value_text,
96340   sqlite3_value_text16,
96341   sqlite3_value_text16be,
96342   sqlite3_value_text16le,
96343   sqlite3_value_type,
96344   sqlite3_vmprintf,
96345   /*
96346   ** The original API set ends here.  All extensions can call any
96347   ** of the APIs above provided that the pointer is not NULL.  But
96348   ** before calling APIs that follow, extension should check the
96349   ** sqlite3_libversion_number() to make sure they are dealing with
96350   ** a library that is new enough to support that API.
96351   *************************************************************************
96352   */
96353   sqlite3_overload_function,
96354 
96355   /*
96356   ** Added after 3.3.13
96357   */
96358   sqlite3_prepare_v2,
96359   sqlite3_prepare16_v2,
96360   sqlite3_clear_bindings,
96361 
96362   /*
96363   ** Added for 3.4.1
96364   */
96365   sqlite3_create_module_v2,
96366 
96367   /*
96368   ** Added for 3.5.0
96369   */
96370   sqlite3_bind_zeroblob,
96371   sqlite3_blob_bytes,
96372   sqlite3_blob_close,
96373   sqlite3_blob_open,
96374   sqlite3_blob_read,
96375   sqlite3_blob_write,
96376   sqlite3_create_collation_v2,
96377   sqlite3_file_control,
96378   sqlite3_memory_highwater,
96379   sqlite3_memory_used,
96380 #ifdef SQLITE_MUTEX_OMIT
96381   0,
96382   0,
96383   0,
96384   0,
96385   0,
96386 #else
96387   sqlite3_mutex_alloc,
96388   sqlite3_mutex_enter,
96389   sqlite3_mutex_free,
96390   sqlite3_mutex_leave,
96391   sqlite3_mutex_try,
96392 #endif
96393   sqlite3_open_v2,
96394   sqlite3_release_memory,
96395   sqlite3_result_error_nomem,
96396   sqlite3_result_error_toobig,
96397   sqlite3_sleep,
96398   sqlite3_soft_heap_limit,
96399   sqlite3_vfs_find,
96400   sqlite3_vfs_register,
96401   sqlite3_vfs_unregister,
96402 
96403   /*
96404   ** Added for 3.5.8
96405   */
96406   sqlite3_threadsafe,
96407   sqlite3_result_zeroblob,
96408   sqlite3_result_error_code,
96409   sqlite3_test_control,
96410   sqlite3_randomness,
96411   sqlite3_context_db_handle,
96412 
96413   /*
96414   ** Added for 3.6.0
96415   */
96416   sqlite3_extended_result_codes,
96417   sqlite3_limit,
96418   sqlite3_next_stmt,
96419   sqlite3_sql,
96420   sqlite3_status,
96421 
96422   /*
96423   ** Added for 3.7.4
96424   */
96425   sqlite3_backup_finish,
96426   sqlite3_backup_init,
96427   sqlite3_backup_pagecount,
96428   sqlite3_backup_remaining,
96429   sqlite3_backup_step,
96430 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
96431   sqlite3_compileoption_get,
96432   sqlite3_compileoption_used,
96433 #else
96434   0,
96435   0,
96436 #endif
96437   sqlite3_create_function_v2,
96438   sqlite3_db_config,
96439   sqlite3_db_mutex,
96440   sqlite3_db_status,
96441   sqlite3_extended_errcode,
96442   sqlite3_log,
96443   sqlite3_soft_heap_limit64,
96444   sqlite3_sourceid,
96445   sqlite3_stmt_status,
96446   sqlite3_strnicmp,
96447 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
96448   sqlite3_unlock_notify,
96449 #else
96450   0,
96451 #endif
96452 #ifndef SQLITE_OMIT_WAL
96453   sqlite3_wal_autocheckpoint,
96454   sqlite3_wal_checkpoint,
96455   sqlite3_wal_hook,
96456 #else
96457   0,
96458   0,
96459   0,
96460 #endif
96461   sqlite3_blob_reopen,
96462   sqlite3_vtab_config,
96463   sqlite3_vtab_on_conflict,
96464   sqlite3_close_v2,
96465   sqlite3_db_filename,
96466   sqlite3_db_readonly,
96467   sqlite3_db_release_memory,
96468   sqlite3_errstr,
96469   sqlite3_stmt_busy,
96470   sqlite3_stmt_readonly,
96471   sqlite3_stricmp,
96472   sqlite3_uri_boolean,
96473   sqlite3_uri_int64,
96474   sqlite3_uri_parameter,
96475   sqlite3_vsnprintf,
96476   sqlite3_wal_checkpoint_v2
96477 };
96478 
96479 /*
96480 ** Attempt to load an SQLite extension library contained in the file
96481 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
96482 ** default entry point name (sqlite3_extension_init) is used.  Use
96483 ** of the default name is recommended.
96484 **
96485 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
96486 **
96487 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
96488 ** error message text.  The calling function should free this memory
96489 ** by calling sqlite3DbFree(db, ).
96490 */
96491 static int sqlite3LoadExtension(
96492   sqlite3 *db,          /* Load the extension into this database connection */
96493   const char *zFile,    /* Name of the shared library containing extension */
96494   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
96495   char **pzErrMsg       /* Put error message here if not 0 */
96496 ){
96497   sqlite3_vfs *pVfs = db->pVfs;
96498   void *handle;
96499   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
96500   char *zErrmsg = 0;
96501   const char *zEntry;
96502   char *zAltEntry = 0;
96503   void **aHandle;
96504   int nMsg = 300 + sqlite3Strlen30(zFile);
96505   int ii;
96506 
96507   /* Shared library endings to try if zFile cannot be loaded as written */
96508   static const char *azEndings[] = {
96509 #if SQLITE_OS_WIN
96510      "dll"
96511 #elif defined(__APPLE__)
96512      "dylib"
96513 #else
96514      "so"
96515 #endif
96516   };
96517 
96518 
96519   if( pzErrMsg ) *pzErrMsg = 0;
96520 
96521   /* Ticket #1863.  To avoid a creating security problems for older
96522   ** applications that relink against newer versions of SQLite, the
96523   ** ability to run load_extension is turned off by default.  One
96524   ** must call sqlite3_enable_load_extension() to turn on extension
96525   ** loading.  Otherwise you get the following error.
96526   */
96527   if( (db->flags & SQLITE_LoadExtension)==0 ){
96528     if( pzErrMsg ){
96529       *pzErrMsg = sqlite3_mprintf("not authorized");
96530     }
96531     return SQLITE_ERROR;
96532   }
96533 
96534   zEntry = zProc ? zProc : "sqlite3_extension_init";
96535 
96536   handle = sqlite3OsDlOpen(pVfs, zFile);
96537 #if SQLITE_OS_UNIX || SQLITE_OS_WIN
96538   for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
96539     char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
96540     if( zAltFile==0 ) return SQLITE_NOMEM;
96541     handle = sqlite3OsDlOpen(pVfs, zAltFile);
96542     sqlite3_free(zAltFile);
96543   }
96544 #endif
96545   if( handle==0 ){
96546     if( pzErrMsg ){
96547       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
96548       if( zErrmsg ){
96549         sqlite3_snprintf(nMsg, zErrmsg,
96550             "unable to open shared library [%s]", zFile);
96551         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
96552       }
96553     }
96554     return SQLITE_ERROR;
96555   }
96556   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
96557                    sqlite3OsDlSym(pVfs, handle, zEntry);
96558 
96559   /* If no entry point was specified and the default legacy
96560   ** entry point name "sqlite3_extension_init" was not found, then
96561   ** construct an entry point name "sqlite3_X_init" where the X is
96562   ** replaced by the lowercase value of every ASCII alphabetic
96563   ** character in the filename after the last "/" upto the first ".",
96564   ** and eliding the first three characters if they are "lib".
96565   ** Examples:
96566   **
96567   **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
96568   **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
96569   */
96570   if( xInit==0 && zProc==0 ){
96571     int iFile, iEntry, c;
96572     int ncFile = sqlite3Strlen30(zFile);
96573     zAltEntry = sqlite3_malloc(ncFile+30);
96574     if( zAltEntry==0 ){
96575       sqlite3OsDlClose(pVfs, handle);
96576       return SQLITE_NOMEM;
96577     }
96578     memcpy(zAltEntry, "sqlite3_", 8);
96579     for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
96580     iFile++;
96581     if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
96582     for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
96583       if( sqlite3Isalpha(c) ){
96584         zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
96585       }
96586     }
96587     memcpy(zAltEntry+iEntry, "_init", 6);
96588     zEntry = zAltEntry;
96589     xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
96590                      sqlite3OsDlSym(pVfs, handle, zEntry);
96591   }
96592   if( xInit==0 ){
96593     if( pzErrMsg ){
96594       nMsg += sqlite3Strlen30(zEntry);
96595       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
96596       if( zErrmsg ){
96597         sqlite3_snprintf(nMsg, zErrmsg,
96598             "no entry point [%s] in shared library [%s]", zEntry, zFile);
96599         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
96600       }
96601     }
96602     sqlite3OsDlClose(pVfs, handle);
96603     sqlite3_free(zAltEntry);
96604     return SQLITE_ERROR;
96605   }
96606   sqlite3_free(zAltEntry);
96607   if( xInit(db, &zErrmsg, &sqlite3Apis) ){
96608     if( pzErrMsg ){
96609       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
96610     }
96611     sqlite3_free(zErrmsg);
96612     sqlite3OsDlClose(pVfs, handle);
96613     return SQLITE_ERROR;
96614   }
96615 
96616   /* Append the new shared library handle to the db->aExtension array. */
96617   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
96618   if( aHandle==0 ){
96619     return SQLITE_NOMEM;
96620   }
96621   if( db->nExtension>0 ){
96622     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
96623   }
96624   sqlite3DbFree(db, db->aExtension);
96625   db->aExtension = aHandle;
96626 
96627   db->aExtension[db->nExtension++] = handle;
96628   return SQLITE_OK;
96629 }
96630 SQLITE_API int sqlite3_load_extension(
96631   sqlite3 *db,          /* Load the extension into this database connection */
96632   const char *zFile,    /* Name of the shared library containing extension */
96633   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
96634   char **pzErrMsg       /* Put error message here if not 0 */
96635 ){
96636   int rc;
96637   sqlite3_mutex_enter(db->mutex);
96638   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
96639   rc = sqlite3ApiExit(db, rc);
96640   sqlite3_mutex_leave(db->mutex);
96641   return rc;
96642 }
96643 
96644 /*
96645 ** Call this routine when the database connection is closing in order
96646 ** to clean up loaded extensions
96647 */
96648 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
96649   int i;
96650   assert( sqlite3_mutex_held(db->mutex) );
96651   for(i=0; i<db->nExtension; i++){
96652     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
96653   }
96654   sqlite3DbFree(db, db->aExtension);
96655 }
96656 
96657 /*
96658 ** Enable or disable extension loading.  Extension loading is disabled by
96659 ** default so as not to open security holes in older applications.
96660 */
96661 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
96662   sqlite3_mutex_enter(db->mutex);
96663   if( onoff ){
96664     db->flags |= SQLITE_LoadExtension;
96665   }else{
96666     db->flags &= ~SQLITE_LoadExtension;
96667   }
96668   sqlite3_mutex_leave(db->mutex);
96669   return SQLITE_OK;
96670 }
96671 
96672 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
96673 
96674 /*
96675 ** The auto-extension code added regardless of whether or not extension
96676 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
96677 ** code if regular extension loading is not available.  This is that
96678 ** dummy pointer.
96679 */
96680 #ifdef SQLITE_OMIT_LOAD_EXTENSION
96681 static const sqlite3_api_routines sqlite3Apis = { 0 };
96682 #endif
96683 
96684 
96685 /*
96686 ** The following object holds the list of automatically loaded
96687 ** extensions.
96688 **
96689 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
96690 ** mutex must be held while accessing this list.
96691 */
96692 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
96693 static SQLITE_WSD struct sqlite3AutoExtList {
96694   int nExt;              /* Number of entries in aExt[] */
96695   void (**aExt)(void);   /* Pointers to the extension init functions */
96696 } sqlite3Autoext = { 0, 0 };
96697 
96698 /* The "wsdAutoext" macro will resolve to the autoextension
96699 ** state vector.  If writable static data is unsupported on the target,
96700 ** we have to locate the state vector at run-time.  In the more common
96701 ** case where writable static data is supported, wsdStat can refer directly
96702 ** to the "sqlite3Autoext" state vector declared above.
96703 */
96704 #ifdef SQLITE_OMIT_WSD
96705 # define wsdAutoextInit \
96706   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
96707 # define wsdAutoext x[0]
96708 #else
96709 # define wsdAutoextInit
96710 # define wsdAutoext sqlite3Autoext
96711 #endif
96712 
96713 
96714 /*
96715 ** Register a statically linked extension that is automatically
96716 ** loaded by every new database connection.
96717 */
96718 SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
96719   int rc = SQLITE_OK;
96720 #ifndef SQLITE_OMIT_AUTOINIT
96721   rc = sqlite3_initialize();
96722   if( rc ){
96723     return rc;
96724   }else
96725 #endif
96726   {
96727     int i;
96728 #if SQLITE_THREADSAFE
96729     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96730 #endif
96731     wsdAutoextInit;
96732     sqlite3_mutex_enter(mutex);
96733     for(i=0; i<wsdAutoext.nExt; i++){
96734       if( wsdAutoext.aExt[i]==xInit ) break;
96735     }
96736     if( i==wsdAutoext.nExt ){
96737       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
96738       void (**aNew)(void);
96739       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
96740       if( aNew==0 ){
96741         rc = SQLITE_NOMEM;
96742       }else{
96743         wsdAutoext.aExt = aNew;
96744         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
96745         wsdAutoext.nExt++;
96746       }
96747     }
96748     sqlite3_mutex_leave(mutex);
96749     assert( (rc&0xff)==rc );
96750     return rc;
96751   }
96752 }
96753 
96754 /*
96755 ** Cancel a prior call to sqlite3_auto_extension.  Remove xInit from the
96756 ** set of routines that is invoked for each new database connection, if it
96757 ** is currently on the list.  If xInit is not on the list, then this
96758 ** routine is a no-op.
96759 **
96760 ** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
96761 ** was not on the list.
96762 */
96763 SQLITE_API int sqlite3_cancel_auto_extension(void (*xInit)(void)){
96764 #if SQLITE_THREADSAFE
96765   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96766 #endif
96767   int i;
96768   int n = 0;
96769   wsdAutoextInit;
96770   sqlite3_mutex_enter(mutex);
96771   for(i=wsdAutoext.nExt-1; i>=0; i--){
96772     if( wsdAutoext.aExt[i]==xInit ){
96773       wsdAutoext.nExt--;
96774       wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
96775       n++;
96776       break;
96777     }
96778   }
96779   sqlite3_mutex_leave(mutex);
96780   return n;
96781 }
96782 
96783 /*
96784 ** Reset the automatic extension loading mechanism.
96785 */
96786 SQLITE_API void sqlite3_reset_auto_extension(void){
96787 #ifndef SQLITE_OMIT_AUTOINIT
96788   if( sqlite3_initialize()==SQLITE_OK )
96789 #endif
96790   {
96791 #if SQLITE_THREADSAFE
96792     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96793 #endif
96794     wsdAutoextInit;
96795     sqlite3_mutex_enter(mutex);
96796     sqlite3_free(wsdAutoext.aExt);
96797     wsdAutoext.aExt = 0;
96798     wsdAutoext.nExt = 0;
96799     sqlite3_mutex_leave(mutex);
96800   }
96801 }
96802 
96803 /*
96804 ** Load all automatic extensions.
96805 **
96806 ** If anything goes wrong, set an error in the database connection.
96807 */
96808 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
96809   int i;
96810   int go = 1;
96811   int rc;
96812   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
96813 
96814   wsdAutoextInit;
96815   if( wsdAutoext.nExt==0 ){
96816     /* Common case: early out without every having to acquire a mutex */
96817     return;
96818   }
96819   for(i=0; go; i++){
96820     char *zErrmsg;
96821 #if SQLITE_THREADSAFE
96822     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96823 #endif
96824     sqlite3_mutex_enter(mutex);
96825     if( i>=wsdAutoext.nExt ){
96826       xInit = 0;
96827       go = 0;
96828     }else{
96829       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
96830               wsdAutoext.aExt[i];
96831     }
96832     sqlite3_mutex_leave(mutex);
96833     zErrmsg = 0;
96834     if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
96835       sqlite3Error(db, rc,
96836             "automatic extension loading failed: %s", zErrmsg);
96837       go = 0;
96838     }
96839     sqlite3_free(zErrmsg);
96840   }
96841 }
96842 
96843 /************** End of loadext.c *********************************************/
96844 /************** Begin file pragma.c ******************************************/
96845 /*
96846 ** 2003 April 6
96847 **
96848 ** The author disclaims copyright to this source code.  In place of
96849 ** a legal notice, here is a blessing:
96850 **
96851 **    May you do good and not evil.
96852 **    May you find forgiveness for yourself and forgive others.
96853 **    May you share freely, never taking more than you give.
96854 **
96855 *************************************************************************
96856 ** This file contains code used to implement the PRAGMA command.
96857 */
96858 
96859 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
96860 #  if defined(__APPLE__)
96861 #    define SQLITE_ENABLE_LOCKING_STYLE 1
96862 #  else
96863 #    define SQLITE_ENABLE_LOCKING_STYLE 0
96864 #  endif
96865 #endif
96866 
96867 /***************************************************************************
96868 ** The next block of code, including the PragTyp_XXXX macro definitions and
96869 ** the aPragmaName[] object is composed of generated code. DO NOT EDIT.
96870 **
96871 ** To add new pragmas, edit the code in ../tool/mkpragmatab.tcl and rerun
96872 ** that script.  Then copy/paste the output in place of the following:
96873 */
96874 #define PragTyp_HEADER_VALUE                   0
96875 #define PragTyp_AUTO_VACUUM                    1
96876 #define PragTyp_FLAG                           2
96877 #define PragTyp_BUSY_TIMEOUT                   3
96878 #define PragTyp_CACHE_SIZE                     4
96879 #define PragTyp_CASE_SENSITIVE_LIKE            5
96880 #define PragTyp_COLLATION_LIST                 6
96881 #define PragTyp_COMPILE_OPTIONS                7
96882 #define PragTyp_DATA_STORE_DIRECTORY           8
96883 #define PragTyp_DATABASE_LIST                  9
96884 #define PragTyp_DEFAULT_CACHE_SIZE            10
96885 #define PragTyp_ENCODING                      11
96886 #define PragTyp_FOREIGN_KEY_CHECK             12
96887 #define PragTyp_FOREIGN_KEY_LIST              13
96888 #define PragTyp_INCREMENTAL_VACUUM            14
96889 #define PragTyp_INDEX_INFO                    15
96890 #define PragTyp_INDEX_LIST                    16
96891 #define PragTyp_INTEGRITY_CHECK               17
96892 #define PragTyp_JOURNAL_MODE                  18
96893 #define PragTyp_JOURNAL_SIZE_LIMIT            19
96894 #define PragTyp_LOCK_PROXY_FILE               20
96895 #define PragTyp_LOCKING_MODE                  21
96896 #define PragTyp_PAGE_COUNT                    22
96897 #define PragTyp_MMAP_SIZE                     23
96898 #define PragTyp_PAGE_SIZE                     24
96899 #define PragTyp_SECURE_DELETE                 25
96900 #define PragTyp_SHRINK_MEMORY                 26
96901 #define PragTyp_SOFT_HEAP_LIMIT               27
96902 #define PragTyp_STATS                         28
96903 #define PragTyp_SYNCHRONOUS                   29
96904 #define PragTyp_TABLE_INFO                    30
96905 #define PragTyp_TEMP_STORE                    31
96906 #define PragTyp_TEMP_STORE_DIRECTORY          32
96907 #define PragTyp_WAL_AUTOCHECKPOINT            33
96908 #define PragTyp_WAL_CHECKPOINT                34
96909 #define PragTyp_ACTIVATE_EXTENSIONS           35
96910 #define PragTyp_HEXKEY                        36
96911 #define PragTyp_KEY                           37
96912 #define PragTyp_REKEY                         38
96913 #define PragTyp_LOCK_STATUS                   39
96914 #define PragTyp_PARSER_TRACE                  40
96915 #define PragFlag_NeedSchema           0x01
96916 static const struct sPragmaNames {
96917   const char *const zName;  /* Name of pragma */
96918   u8 ePragTyp;              /* PragTyp_XXX value */
96919   u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
96920   u32 iArg;                 /* Extra argument */
96921 } aPragmaNames[] = {
96922 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
96923   { /* zName:     */ "activate_extensions",
96924     /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
96925     /* ePragFlag: */ 0,
96926     /* iArg:      */ 0 },
96927 #endif
96928 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
96929   { /* zName:     */ "application_id",
96930     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
96931     /* ePragFlag: */ 0,
96932     /* iArg:      */ 0 },
96933 #endif
96934 #if !defined(SQLITE_OMIT_AUTOVACUUM)
96935   { /* zName:     */ "auto_vacuum",
96936     /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
96937     /* ePragFlag: */ PragFlag_NeedSchema,
96938     /* iArg:      */ 0 },
96939 #endif
96940 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96941 #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
96942   { /* zName:     */ "automatic_index",
96943     /* ePragTyp:  */ PragTyp_FLAG,
96944     /* ePragFlag: */ 0,
96945     /* iArg:      */ SQLITE_AutoIndex },
96946 #endif
96947 #endif
96948   { /* zName:     */ "busy_timeout",
96949     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
96950     /* ePragFlag: */ 0,
96951     /* iArg:      */ 0 },
96952 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
96953   { /* zName:     */ "cache_size",
96954     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
96955     /* ePragFlag: */ PragFlag_NeedSchema,
96956     /* iArg:      */ 0 },
96957 #endif
96958 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96959   { /* zName:     */ "cache_spill",
96960     /* ePragTyp:  */ PragTyp_FLAG,
96961     /* ePragFlag: */ 0,
96962     /* iArg:      */ SQLITE_CacheSpill },
96963 #endif
96964   { /* zName:     */ "case_sensitive_like",
96965     /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
96966     /* ePragFlag: */ 0,
96967     /* iArg:      */ 0 },
96968 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96969   { /* zName:     */ "checkpoint_fullfsync",
96970     /* ePragTyp:  */ PragTyp_FLAG,
96971     /* ePragFlag: */ 0,
96972     /* iArg:      */ SQLITE_CkptFullFSync },
96973 #endif
96974 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
96975   { /* zName:     */ "collation_list",
96976     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
96977     /* ePragFlag: */ 0,
96978     /* iArg:      */ 0 },
96979 #endif
96980 #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
96981   { /* zName:     */ "compile_options",
96982     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
96983     /* ePragFlag: */ 0,
96984     /* iArg:      */ 0 },
96985 #endif
96986 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96987   { /* zName:     */ "count_changes",
96988     /* ePragTyp:  */ PragTyp_FLAG,
96989     /* ePragFlag: */ 0,
96990     /* iArg:      */ SQLITE_CountRows },
96991 #endif
96992 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
96993   { /* zName:     */ "data_store_directory",
96994     /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
96995     /* ePragFlag: */ 0,
96996     /* iArg:      */ 0 },
96997 #endif
96998 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
96999   { /* zName:     */ "database_list",
97000     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
97001     /* ePragFlag: */ PragFlag_NeedSchema,
97002     /* iArg:      */ 0 },
97003 #endif
97004 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
97005   { /* zName:     */ "default_cache_size",
97006     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
97007     /* ePragFlag: */ PragFlag_NeedSchema,
97008     /* iArg:      */ 0 },
97009 #endif
97010 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97011 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
97012   { /* zName:     */ "defer_foreign_keys",
97013     /* ePragTyp:  */ PragTyp_FLAG,
97014     /* ePragFlag: */ 0,
97015     /* iArg:      */ SQLITE_DeferFKs },
97016 #endif
97017 #endif
97018 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97019   { /* zName:     */ "empty_result_callbacks",
97020     /* ePragTyp:  */ PragTyp_FLAG,
97021     /* ePragFlag: */ 0,
97022     /* iArg:      */ SQLITE_NullCallback },
97023 #endif
97024 #if !defined(SQLITE_OMIT_UTF16)
97025   { /* zName:     */ "encoding",
97026     /* ePragTyp:  */ PragTyp_ENCODING,
97027     /* ePragFlag: */ 0,
97028     /* iArg:      */ 0 },
97029 #endif
97030 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
97031   { /* zName:     */ "foreign_key_check",
97032     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
97033     /* ePragFlag: */ PragFlag_NeedSchema,
97034     /* iArg:      */ 0 },
97035 #endif
97036 #if !defined(SQLITE_OMIT_FOREIGN_KEY)
97037   { /* zName:     */ "foreign_key_list",
97038     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
97039     /* ePragFlag: */ PragFlag_NeedSchema,
97040     /* iArg:      */ 0 },
97041 #endif
97042 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97043 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
97044   { /* zName:     */ "foreign_keys",
97045     /* ePragTyp:  */ PragTyp_FLAG,
97046     /* ePragFlag: */ 0,
97047     /* iArg:      */ SQLITE_ForeignKeys },
97048 #endif
97049 #endif
97050 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
97051   { /* zName:     */ "freelist_count",
97052     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
97053     /* ePragFlag: */ 0,
97054     /* iArg:      */ 0 },
97055 #endif
97056 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97057   { /* zName:     */ "full_column_names",
97058     /* ePragTyp:  */ PragTyp_FLAG,
97059     /* ePragFlag: */ 0,
97060     /* iArg:      */ SQLITE_FullColNames },
97061   { /* zName:     */ "fullfsync",
97062     /* ePragTyp:  */ PragTyp_FLAG,
97063     /* ePragFlag: */ 0,
97064     /* iArg:      */ SQLITE_FullFSync },
97065 #endif
97066 #if defined(SQLITE_HAS_CODEC)
97067   { /* zName:     */ "hexkey",
97068     /* ePragTyp:  */ PragTyp_HEXKEY,
97069     /* ePragFlag: */ 0,
97070     /* iArg:      */ 0 },
97071   { /* zName:     */ "hexrekey",
97072     /* ePragTyp:  */ PragTyp_HEXKEY,
97073     /* ePragFlag: */ 0,
97074     /* iArg:      */ 0 },
97075 #endif
97076 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97077 #if !defined(SQLITE_OMIT_CHECK)
97078   { /* zName:     */ "ignore_check_constraints",
97079     /* ePragTyp:  */ PragTyp_FLAG,
97080     /* ePragFlag: */ 0,
97081     /* iArg:      */ SQLITE_IgnoreChecks },
97082 #endif
97083 #endif
97084 #if !defined(SQLITE_OMIT_AUTOVACUUM)
97085   { /* zName:     */ "incremental_vacuum",
97086     /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
97087     /* ePragFlag: */ PragFlag_NeedSchema,
97088     /* iArg:      */ 0 },
97089 #endif
97090 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
97091   { /* zName:     */ "index_info",
97092     /* ePragTyp:  */ PragTyp_INDEX_INFO,
97093     /* ePragFlag: */ PragFlag_NeedSchema,
97094     /* iArg:      */ 0 },
97095   { /* zName:     */ "index_list",
97096     /* ePragTyp:  */ PragTyp_INDEX_LIST,
97097     /* ePragFlag: */ PragFlag_NeedSchema,
97098     /* iArg:      */ 0 },
97099 #endif
97100 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
97101   { /* zName:     */ "integrity_check",
97102     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
97103     /* ePragFlag: */ PragFlag_NeedSchema,
97104     /* iArg:      */ 0 },
97105 #endif
97106 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
97107   { /* zName:     */ "journal_mode",
97108     /* ePragTyp:  */ PragTyp_JOURNAL_MODE,
97109     /* ePragFlag: */ PragFlag_NeedSchema,
97110     /* iArg:      */ 0 },
97111   { /* zName:     */ "journal_size_limit",
97112     /* ePragTyp:  */ PragTyp_JOURNAL_SIZE_LIMIT,
97113     /* ePragFlag: */ 0,
97114     /* iArg:      */ 0 },
97115 #endif
97116 #if defined(SQLITE_HAS_CODEC)
97117   { /* zName:     */ "key",
97118     /* ePragTyp:  */ PragTyp_KEY,
97119     /* ePragFlag: */ 0,
97120     /* iArg:      */ 0 },
97121 #endif
97122 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97123   { /* zName:     */ "legacy_file_format",
97124     /* ePragTyp:  */ PragTyp_FLAG,
97125     /* ePragFlag: */ 0,
97126     /* iArg:      */ SQLITE_LegacyFileFmt },
97127 #endif
97128 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
97129   { /* zName:     */ "lock_proxy_file",
97130     /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
97131     /* ePragFlag: */ 0,
97132     /* iArg:      */ 0 },
97133 #endif
97134 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
97135   { /* zName:     */ "lock_status",
97136     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
97137     /* ePragFlag: */ 0,
97138     /* iArg:      */ 0 },
97139 #endif
97140 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
97141   { /* zName:     */ "locking_mode",
97142     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
97143     /* ePragFlag: */ 0,
97144     /* iArg:      */ 0 },
97145   { /* zName:     */ "max_page_count",
97146     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
97147     /* ePragFlag: */ PragFlag_NeedSchema,
97148     /* iArg:      */ 0 },
97149   { /* zName:     */ "mmap_size",
97150     /* ePragTyp:  */ PragTyp_MMAP_SIZE,
97151     /* ePragFlag: */ 0,
97152     /* iArg:      */ 0 },
97153   { /* zName:     */ "page_count",
97154     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
97155     /* ePragFlag: */ PragFlag_NeedSchema,
97156     /* iArg:      */ 0 },
97157   { /* zName:     */ "page_size",
97158     /* ePragTyp:  */ PragTyp_PAGE_SIZE,
97159     /* ePragFlag: */ 0,
97160     /* iArg:      */ 0 },
97161 #endif
97162 #if defined(SQLITE_DEBUG)
97163   { /* zName:     */ "parser_trace",
97164     /* ePragTyp:  */ PragTyp_PARSER_TRACE,
97165     /* ePragFlag: */ 0,
97166     /* iArg:      */ 0 },
97167 #endif
97168 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97169   { /* zName:     */ "query_only",
97170     /* ePragTyp:  */ PragTyp_FLAG,
97171     /* ePragFlag: */ 0,
97172     /* iArg:      */ SQLITE_QueryOnly },
97173 #endif
97174 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
97175   { /* zName:     */ "quick_check",
97176     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
97177     /* ePragFlag: */ PragFlag_NeedSchema,
97178     /* iArg:      */ 0 },
97179 #endif
97180 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97181   { /* zName:     */ "read_uncommitted",
97182     /* ePragTyp:  */ PragTyp_FLAG,
97183     /* ePragFlag: */ 0,
97184     /* iArg:      */ SQLITE_ReadUncommitted },
97185   { /* zName:     */ "recursive_triggers",
97186     /* ePragTyp:  */ PragTyp_FLAG,
97187     /* ePragFlag: */ 0,
97188     /* iArg:      */ SQLITE_RecTriggers },
97189 #endif
97190 #if defined(SQLITE_HAS_CODEC)
97191   { /* zName:     */ "rekey",
97192     /* ePragTyp:  */ PragTyp_REKEY,
97193     /* ePragFlag: */ 0,
97194     /* iArg:      */ 0 },
97195 #endif
97196 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97197   { /* zName:     */ "reverse_unordered_selects",
97198     /* ePragTyp:  */ PragTyp_FLAG,
97199     /* ePragFlag: */ 0,
97200     /* iArg:      */ SQLITE_ReverseOrder },
97201 #endif
97202 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
97203   { /* zName:     */ "schema_version",
97204     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
97205     /* ePragFlag: */ 0,
97206     /* iArg:      */ 0 },
97207 #endif
97208 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
97209   { /* zName:     */ "secure_delete",
97210     /* ePragTyp:  */ PragTyp_SECURE_DELETE,
97211     /* ePragFlag: */ 0,
97212     /* iArg:      */ 0 },
97213 #endif
97214 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97215   { /* zName:     */ "short_column_names",
97216     /* ePragTyp:  */ PragTyp_FLAG,
97217     /* ePragFlag: */ 0,
97218     /* iArg:      */ SQLITE_ShortColNames },
97219 #endif
97220   { /* zName:     */ "shrink_memory",
97221     /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
97222     /* ePragFlag: */ 0,
97223     /* iArg:      */ 0 },
97224   { /* zName:     */ "soft_heap_limit",
97225     /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
97226     /* ePragFlag: */ 0,
97227     /* iArg:      */ 0 },
97228 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97229 #if defined(SQLITE_DEBUG)
97230   { /* zName:     */ "sql_trace",
97231     /* ePragTyp:  */ PragTyp_FLAG,
97232     /* ePragFlag: */ 0,
97233     /* iArg:      */ SQLITE_SqlTrace },
97234 #endif
97235 #endif
97236 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
97237   { /* zName:     */ "stats",
97238     /* ePragTyp:  */ PragTyp_STATS,
97239     /* ePragFlag: */ PragFlag_NeedSchema,
97240     /* iArg:      */ 0 },
97241 #endif
97242 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
97243   { /* zName:     */ "synchronous",
97244     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
97245     /* ePragFlag: */ PragFlag_NeedSchema,
97246     /* iArg:      */ 0 },
97247 #endif
97248 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
97249   { /* zName:     */ "table_info",
97250     /* ePragTyp:  */ PragTyp_TABLE_INFO,
97251     /* ePragFlag: */ PragFlag_NeedSchema,
97252     /* iArg:      */ 0 },
97253 #endif
97254 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
97255   { /* zName:     */ "temp_store",
97256     /* ePragTyp:  */ PragTyp_TEMP_STORE,
97257     /* ePragFlag: */ 0,
97258     /* iArg:      */ 0 },
97259   { /* zName:     */ "temp_store_directory",
97260     /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
97261     /* ePragFlag: */ 0,
97262     /* iArg:      */ 0 },
97263 #endif
97264 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
97265   { /* zName:     */ "user_version",
97266     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
97267     /* ePragFlag: */ 0,
97268     /* iArg:      */ 0 },
97269 #endif
97270 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97271 #if defined(SQLITE_DEBUG)
97272   { /* zName:     */ "vdbe_addoptrace",
97273     /* ePragTyp:  */ PragTyp_FLAG,
97274     /* ePragFlag: */ 0,
97275     /* iArg:      */ SQLITE_VdbeAddopTrace },
97276   { /* zName:     */ "vdbe_debug",
97277     /* ePragTyp:  */ PragTyp_FLAG,
97278     /* ePragFlag: */ 0,
97279     /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
97280   { /* zName:     */ "vdbe_eqp",
97281     /* ePragTyp:  */ PragTyp_FLAG,
97282     /* ePragFlag: */ 0,
97283     /* iArg:      */ SQLITE_VdbeEQP },
97284   { /* zName:     */ "vdbe_listing",
97285     /* ePragTyp:  */ PragTyp_FLAG,
97286     /* ePragFlag: */ 0,
97287     /* iArg:      */ SQLITE_VdbeListing },
97288   { /* zName:     */ "vdbe_trace",
97289     /* ePragTyp:  */ PragTyp_FLAG,
97290     /* ePragFlag: */ 0,
97291     /* iArg:      */ SQLITE_VdbeTrace },
97292 #endif
97293 #endif
97294 #if !defined(SQLITE_OMIT_WAL)
97295   { /* zName:     */ "wal_autocheckpoint",
97296     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
97297     /* ePragFlag: */ 0,
97298     /* iArg:      */ 0 },
97299   { /* zName:     */ "wal_checkpoint",
97300     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
97301     /* ePragFlag: */ PragFlag_NeedSchema,
97302     /* iArg:      */ 0 },
97303 #endif
97304 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
97305   { /* zName:     */ "writable_schema",
97306     /* ePragTyp:  */ PragTyp_FLAG,
97307     /* ePragFlag: */ 0,
97308     /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
97309 #endif
97310 };
97311 /* Number of pragmas: 56 on by default, 69 total. */
97312 /* End of the automatically generated pragma table.
97313 ***************************************************************************/
97314 
97315 /*
97316 ** Interpret the given string as a safety level.  Return 0 for OFF,
97317 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
97318 ** unrecognized string argument.  The FULL option is disallowed
97319 ** if the omitFull parameter it 1.
97320 **
97321 ** Note that the values returned are one less that the values that
97322 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
97323 ** to support legacy SQL code.  The safety level used to be boolean
97324 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
97325 */
97326 static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
97327                              /* 123456789 123456789 */
97328   static const char zText[] = "onoffalseyestruefull";
97329   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
97330   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
97331   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
97332   int i, n;
97333   if( sqlite3Isdigit(*z) ){
97334     return (u8)sqlite3Atoi(z);
97335   }
97336   n = sqlite3Strlen30(z);
97337   for(i=0; i<ArraySize(iLength)-omitFull; i++){
97338     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
97339       return iValue[i];
97340     }
97341   }
97342   return dflt;
97343 }
97344 
97345 /*
97346 ** Interpret the given string as a boolean value.
97347 */
97348 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, int dflt){
97349   return getSafetyLevel(z,1,dflt)!=0;
97350 }
97351 
97352 /* The sqlite3GetBoolean() function is used by other modules but the
97353 ** remainder of this file is specific to PRAGMA processing.  So omit
97354 ** the rest of the file if PRAGMAs are omitted from the build.
97355 */
97356 #if !defined(SQLITE_OMIT_PRAGMA)
97357 
97358 /*
97359 ** Interpret the given string as a locking mode value.
97360 */
97361 static int getLockingMode(const char *z){
97362   if( z ){
97363     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
97364     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
97365   }
97366   return PAGER_LOCKINGMODE_QUERY;
97367 }
97368 
97369 #ifndef SQLITE_OMIT_AUTOVACUUM
97370 /*
97371 ** Interpret the given string as an auto-vacuum mode value.
97372 **
97373 ** The following strings, "none", "full" and "incremental" are
97374 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
97375 */
97376 static int getAutoVacuum(const char *z){
97377   int i;
97378   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
97379   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
97380   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
97381   i = sqlite3Atoi(z);
97382   return (u8)((i>=0&&i<=2)?i:0);
97383 }
97384 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
97385 
97386 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
97387 /*
97388 ** Interpret the given string as a temp db location. Return 1 for file
97389 ** backed temporary databases, 2 for the Red-Black tree in memory database
97390 ** and 0 to use the compile-time default.
97391 */
97392 static int getTempStore(const char *z){
97393   if( z[0]>='0' && z[0]<='2' ){
97394     return z[0] - '0';
97395   }else if( sqlite3StrICmp(z, "file")==0 ){
97396     return 1;
97397   }else if( sqlite3StrICmp(z, "memory")==0 ){
97398     return 2;
97399   }else{
97400     return 0;
97401   }
97402 }
97403 #endif /* SQLITE_PAGER_PRAGMAS */
97404 
97405 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
97406 /*
97407 ** Invalidate temp storage, either when the temp storage is changed
97408 ** from default, or when 'file' and the temp_store_directory has changed
97409 */
97410 static int invalidateTempStorage(Parse *pParse){
97411   sqlite3 *db = pParse->db;
97412   if( db->aDb[1].pBt!=0 ){
97413     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
97414       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
97415         "from within a transaction");
97416       return SQLITE_ERROR;
97417     }
97418     sqlite3BtreeClose(db->aDb[1].pBt);
97419     db->aDb[1].pBt = 0;
97420     sqlite3ResetAllSchemasOfConnection(db);
97421   }
97422   return SQLITE_OK;
97423 }
97424 #endif /* SQLITE_PAGER_PRAGMAS */
97425 
97426 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
97427 /*
97428 ** If the TEMP database is open, close it and mark the database schema
97429 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
97430 ** or DEFAULT_TEMP_STORE pragmas.
97431 */
97432 static int changeTempStorage(Parse *pParse, const char *zStorageType){
97433   int ts = getTempStore(zStorageType);
97434   sqlite3 *db = pParse->db;
97435   if( db->temp_store==ts ) return SQLITE_OK;
97436   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
97437     return SQLITE_ERROR;
97438   }
97439   db->temp_store = (u8)ts;
97440   return SQLITE_OK;
97441 }
97442 #endif /* SQLITE_PAGER_PRAGMAS */
97443 
97444 /*
97445 ** Generate code to return a single integer value.
97446 */
97447 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
97448   Vdbe *v = sqlite3GetVdbe(pParse);
97449   int mem = ++pParse->nMem;
97450   i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
97451   if( pI64 ){
97452     memcpy(pI64, &value, sizeof(value));
97453   }
97454   sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
97455   sqlite3VdbeSetNumCols(v, 1);
97456   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
97457   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
97458 }
97459 
97460 
97461 /*
97462 ** Set the safety_level and pager flags for pager iDb.  Or if iDb<0
97463 ** set these values for all pagers.
97464 */
97465 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
97466 static void setAllPagerFlags(sqlite3 *db){
97467   if( db->autoCommit ){
97468     Db *pDb = db->aDb;
97469     int n = db->nDb;
97470     assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
97471     assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
97472     assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
97473     assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
97474              ==  PAGER_FLAGS_MASK );
97475     assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
97476     while( (n--) > 0 ){
97477       if( pDb->pBt ){
97478         sqlite3BtreeSetPagerFlags(pDb->pBt,
97479                  pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
97480       }
97481       pDb++;
97482     }
97483   }
97484 }
97485 #else
97486 # define setAllPagerFlags(X)  /* no-op */
97487 #endif
97488 
97489 
97490 /*
97491 ** Return a human-readable name for a constraint resolution action.
97492 */
97493 #ifndef SQLITE_OMIT_FOREIGN_KEY
97494 static const char *actionName(u8 action){
97495   const char *zName;
97496   switch( action ){
97497     case OE_SetNull:  zName = "SET NULL";        break;
97498     case OE_SetDflt:  zName = "SET DEFAULT";     break;
97499     case OE_Cascade:  zName = "CASCADE";         break;
97500     case OE_Restrict: zName = "RESTRICT";        break;
97501     default:          zName = "NO ACTION";
97502                       assert( action==OE_None ); break;
97503   }
97504   return zName;
97505 }
97506 #endif
97507 
97508 
97509 /*
97510 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
97511 ** defined in pager.h. This function returns the associated lowercase
97512 ** journal-mode name.
97513 */
97514 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
97515   static char * const azModeName[] = {
97516     "delete", "persist", "off", "truncate", "memory"
97517 #ifndef SQLITE_OMIT_WAL
97518      , "wal"
97519 #endif
97520   };
97521   assert( PAGER_JOURNALMODE_DELETE==0 );
97522   assert( PAGER_JOURNALMODE_PERSIST==1 );
97523   assert( PAGER_JOURNALMODE_OFF==2 );
97524   assert( PAGER_JOURNALMODE_TRUNCATE==3 );
97525   assert( PAGER_JOURNALMODE_MEMORY==4 );
97526   assert( PAGER_JOURNALMODE_WAL==5 );
97527   assert( eMode>=0 && eMode<=ArraySize(azModeName) );
97528 
97529   if( eMode==ArraySize(azModeName) ) return 0;
97530   return azModeName[eMode];
97531 }
97532 
97533 /*
97534 ** Process a pragma statement.
97535 **
97536 ** Pragmas are of this form:
97537 **
97538 **      PRAGMA [database.]id [= value]
97539 **
97540 ** The identifier might also be a string.  The value is a string, and
97541 ** identifier, or a number.  If minusFlag is true, then the value is
97542 ** a number that was preceded by a minus sign.
97543 **
97544 ** If the left side is "database.id" then pId1 is the database name
97545 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
97546 ** id and pId2 is any empty string.
97547 */
97548 SQLITE_PRIVATE void sqlite3Pragma(
97549   Parse *pParse,
97550   Token *pId1,        /* First part of [database.]id field */
97551   Token *pId2,        /* Second part of [database.]id field, or NULL */
97552   Token *pValue,      /* Token for <value>, or NULL */
97553   int minusFlag       /* True if a '-' sign preceded <value> */
97554 ){
97555   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
97556   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
97557   const char *zDb = 0;   /* The database name */
97558   Token *pId;            /* Pointer to <id> token */
97559   char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
97560   int iDb;               /* Database index for <database> */
97561   int lwr, upr, mid;           /* Binary search bounds */
97562   int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
97563   sqlite3 *db = pParse->db;    /* The database connection */
97564   Db *pDb;                     /* The specific database being pragmaed */
97565   Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
97566 
97567   if( v==0 ) return;
97568   sqlite3VdbeRunOnlyOnce(v);
97569   pParse->nMem = 2;
97570 
97571   /* Interpret the [database.] part of the pragma statement. iDb is the
97572   ** index of the database this pragma is being applied to in db.aDb[]. */
97573   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
97574   if( iDb<0 ) return;
97575   pDb = &db->aDb[iDb];
97576 
97577   /* If the temp database has been explicitly named as part of the
97578   ** pragma, make sure it is open.
97579   */
97580   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
97581     return;
97582   }
97583 
97584   zLeft = sqlite3NameFromToken(db, pId);
97585   if( !zLeft ) return;
97586   if( minusFlag ){
97587     zRight = sqlite3MPrintf(db, "-%T", pValue);
97588   }else{
97589     zRight = sqlite3NameFromToken(db, pValue);
97590   }
97591 
97592   assert( pId2 );
97593   zDb = pId2->n>0 ? pDb->zName : 0;
97594   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
97595     goto pragma_out;
97596   }
97597 
97598   /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
97599   ** connection.  If it returns SQLITE_OK, then assume that the VFS
97600   ** handled the pragma and generate a no-op prepared statement.
97601   */
97602   aFcntl[0] = 0;
97603   aFcntl[1] = zLeft;
97604   aFcntl[2] = zRight;
97605   aFcntl[3] = 0;
97606   db->busyHandler.nBusy = 0;
97607   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
97608   if( rc==SQLITE_OK ){
97609     if( aFcntl[0] ){
97610       int mem = ++pParse->nMem;
97611       sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
97612       sqlite3VdbeSetNumCols(v, 1);
97613       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
97614       sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
97615       sqlite3_free(aFcntl[0]);
97616     }
97617     goto pragma_out;
97618   }
97619   if( rc!=SQLITE_NOTFOUND ){
97620     if( aFcntl[0] ){
97621       sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
97622       sqlite3_free(aFcntl[0]);
97623     }
97624     pParse->nErr++;
97625     pParse->rc = rc;
97626     goto pragma_out;
97627   }
97628 
97629   /* Locate the pragma in the lookup table */
97630   lwr = 0;
97631   upr = ArraySize(aPragmaNames)-1;
97632   while( lwr<=upr ){
97633     mid = (lwr+upr)/2;
97634     rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
97635     if( rc==0 ) break;
97636     if( rc<0 ){
97637       upr = mid - 1;
97638     }else{
97639       lwr = mid + 1;
97640     }
97641   }
97642   if( lwr>upr ) goto pragma_out;
97643 
97644   /* Make sure the database schema is loaded if the pragma requires that */
97645   if( (aPragmaNames[mid].mPragFlag & PragFlag_NeedSchema)!=0 ){
97646     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
97647   }
97648 
97649   /* Jump to the appropriate pragma handler */
97650   switch( aPragmaNames[mid].ePragTyp ){
97651 
97652 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
97653   /*
97654   **  PRAGMA [database.]default_cache_size
97655   **  PRAGMA [database.]default_cache_size=N
97656   **
97657   ** The first form reports the current persistent setting for the
97658   ** page cache size.  The value returned is the maximum number of
97659   ** pages in the page cache.  The second form sets both the current
97660   ** page cache size value and the persistent page cache size value
97661   ** stored in the database file.
97662   **
97663   ** Older versions of SQLite would set the default cache size to a
97664   ** negative number to indicate synchronous=OFF.  These days, synchronous
97665   ** is always on by default regardless of the sign of the default cache
97666   ** size.  But continue to take the absolute value of the default cache
97667   ** size of historical compatibility.
97668   */
97669   case PragTyp_DEFAULT_CACHE_SIZE: {
97670     static const int iLn = VDBE_OFFSET_LINENO(2);
97671     static const VdbeOpList getCacheSize[] = {
97672       { OP_Transaction, 0, 0,        0},                         /* 0 */
97673       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
97674       { OP_IfPos,       1, 8,        0},
97675       { OP_Integer,     0, 2,        0},
97676       { OP_Subtract,    1, 2,        1},
97677       { OP_IfPos,       1, 8,        0},
97678       { OP_Integer,     0, 1,        0},                         /* 6 */
97679       { OP_Noop,        0, 0,        0},
97680       { OP_ResultRow,   1, 1,        0},
97681     };
97682     int addr;
97683     sqlite3VdbeUsesBtree(v, iDb);
97684     if( !zRight ){
97685       sqlite3VdbeSetNumCols(v, 1);
97686       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
97687       pParse->nMem += 2;
97688       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize,iLn);
97689       sqlite3VdbeChangeP1(v, addr, iDb);
97690       sqlite3VdbeChangeP1(v, addr+1, iDb);
97691       sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
97692     }else{
97693       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
97694       sqlite3BeginWriteOperation(pParse, 0, iDb);
97695       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
97696       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
97697       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97698       pDb->pSchema->cache_size = size;
97699       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
97700     }
97701     break;
97702   }
97703 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
97704 
97705 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
97706   /*
97707   **  PRAGMA [database.]page_size
97708   **  PRAGMA [database.]page_size=N
97709   **
97710   ** The first form reports the current setting for the
97711   ** database page size in bytes.  The second form sets the
97712   ** database page size value.  The value can only be set if
97713   ** the database has not yet been created.
97714   */
97715   case PragTyp_PAGE_SIZE: {
97716     Btree *pBt = pDb->pBt;
97717     assert( pBt!=0 );
97718     if( !zRight ){
97719       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
97720       returnSingleInt(pParse, "page_size", size);
97721     }else{
97722       /* Malloc may fail when setting the page-size, as there is an internal
97723       ** buffer that the pager module resizes using sqlite3_realloc().
97724       */
97725       db->nextPagesize = sqlite3Atoi(zRight);
97726       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
97727         db->mallocFailed = 1;
97728       }
97729     }
97730     break;
97731   }
97732 
97733   /*
97734   **  PRAGMA [database.]secure_delete
97735   **  PRAGMA [database.]secure_delete=ON/OFF
97736   **
97737   ** The first form reports the current setting for the
97738   ** secure_delete flag.  The second form changes the secure_delete
97739   ** flag setting and reports thenew value.
97740   */
97741   case PragTyp_SECURE_DELETE: {
97742     Btree *pBt = pDb->pBt;
97743     int b = -1;
97744     assert( pBt!=0 );
97745     if( zRight ){
97746       b = sqlite3GetBoolean(zRight, 0);
97747     }
97748     if( pId2->n==0 && b>=0 ){
97749       int ii;
97750       for(ii=0; ii<db->nDb; ii++){
97751         sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
97752       }
97753     }
97754     b = sqlite3BtreeSecureDelete(pBt, b);
97755     returnSingleInt(pParse, "secure_delete", b);
97756     break;
97757   }
97758 
97759   /*
97760   **  PRAGMA [database.]max_page_count
97761   **  PRAGMA [database.]max_page_count=N
97762   **
97763   ** The first form reports the current setting for the
97764   ** maximum number of pages in the database file.  The
97765   ** second form attempts to change this setting.  Both
97766   ** forms return the current setting.
97767   **
97768   ** The absolute value of N is used.  This is undocumented and might
97769   ** change.  The only purpose is to provide an easy way to test
97770   ** the sqlite3AbsInt32() function.
97771   **
97772   **  PRAGMA [database.]page_count
97773   **
97774   ** Return the number of pages in the specified database.
97775   */
97776   case PragTyp_PAGE_COUNT: {
97777     int iReg;
97778     sqlite3CodeVerifySchema(pParse, iDb);
97779     iReg = ++pParse->nMem;
97780     if( sqlite3Tolower(zLeft[0])=='p' ){
97781       sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
97782     }else{
97783       sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
97784                         sqlite3AbsInt32(sqlite3Atoi(zRight)));
97785     }
97786     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
97787     sqlite3VdbeSetNumCols(v, 1);
97788     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
97789     break;
97790   }
97791 
97792   /*
97793   **  PRAGMA [database.]locking_mode
97794   **  PRAGMA [database.]locking_mode = (normal|exclusive)
97795   */
97796   case PragTyp_LOCKING_MODE: {
97797     const char *zRet = "normal";
97798     int eMode = getLockingMode(zRight);
97799 
97800     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
97801       /* Simple "PRAGMA locking_mode;" statement. This is a query for
97802       ** the current default locking mode (which may be different to
97803       ** the locking-mode of the main database).
97804       */
97805       eMode = db->dfltLockMode;
97806     }else{
97807       Pager *pPager;
97808       if( pId2->n==0 ){
97809         /* This indicates that no database name was specified as part
97810         ** of the PRAGMA command. In this case the locking-mode must be
97811         ** set on all attached databases, as well as the main db file.
97812         **
97813         ** Also, the sqlite3.dfltLockMode variable is set so that
97814         ** any subsequently attached databases also use the specified
97815         ** locking mode.
97816         */
97817         int ii;
97818         assert(pDb==&db->aDb[0]);
97819         for(ii=2; ii<db->nDb; ii++){
97820           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
97821           sqlite3PagerLockingMode(pPager, eMode);
97822         }
97823         db->dfltLockMode = (u8)eMode;
97824       }
97825       pPager = sqlite3BtreePager(pDb->pBt);
97826       eMode = sqlite3PagerLockingMode(pPager, eMode);
97827     }
97828 
97829     assert( eMode==PAGER_LOCKINGMODE_NORMAL
97830             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
97831     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
97832       zRet = "exclusive";
97833     }
97834     sqlite3VdbeSetNumCols(v, 1);
97835     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
97836     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
97837     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
97838     break;
97839   }
97840 
97841   /*
97842   **  PRAGMA [database.]journal_mode
97843   **  PRAGMA [database.]journal_mode =
97844   **                      (delete|persist|off|truncate|memory|wal|off)
97845   */
97846   case PragTyp_JOURNAL_MODE: {
97847     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
97848     int ii;           /* Loop counter */
97849 
97850     sqlite3VdbeSetNumCols(v, 1);
97851     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
97852 
97853     if( zRight==0 ){
97854       /* If there is no "=MODE" part of the pragma, do a query for the
97855       ** current mode */
97856       eMode = PAGER_JOURNALMODE_QUERY;
97857     }else{
97858       const char *zMode;
97859       int n = sqlite3Strlen30(zRight);
97860       for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
97861         if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
97862       }
97863       if( !zMode ){
97864         /* If the "=MODE" part does not match any known journal mode,
97865         ** then do a query */
97866         eMode = PAGER_JOURNALMODE_QUERY;
97867       }
97868     }
97869     if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
97870       /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
97871       iDb = 0;
97872       pId2->n = 1;
97873     }
97874     for(ii=db->nDb-1; ii>=0; ii--){
97875       if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
97876         sqlite3VdbeUsesBtree(v, ii);
97877         sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
97878       }
97879     }
97880     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
97881     break;
97882   }
97883 
97884   /*
97885   **  PRAGMA [database.]journal_size_limit
97886   **  PRAGMA [database.]journal_size_limit=N
97887   **
97888   ** Get or set the size limit on rollback journal files.
97889   */
97890   case PragTyp_JOURNAL_SIZE_LIMIT: {
97891     Pager *pPager = sqlite3BtreePager(pDb->pBt);
97892     i64 iLimit = -2;
97893     if( zRight ){
97894       sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
97895       if( iLimit<-1 ) iLimit = -1;
97896     }
97897     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
97898     returnSingleInt(pParse, "journal_size_limit", iLimit);
97899     break;
97900   }
97901 
97902 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
97903 
97904   /*
97905   **  PRAGMA [database.]auto_vacuum
97906   **  PRAGMA [database.]auto_vacuum=N
97907   **
97908   ** Get or set the value of the database 'auto-vacuum' parameter.
97909   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
97910   */
97911 #ifndef SQLITE_OMIT_AUTOVACUUM
97912   case PragTyp_AUTO_VACUUM: {
97913     Btree *pBt = pDb->pBt;
97914     assert( pBt!=0 );
97915     if( !zRight ){
97916       returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
97917     }else{
97918       int eAuto = getAutoVacuum(zRight);
97919       assert( eAuto>=0 && eAuto<=2 );
97920       db->nextAutovac = (u8)eAuto;
97921       /* Call SetAutoVacuum() to set initialize the internal auto and
97922       ** incr-vacuum flags. This is required in case this connection
97923       ** creates the database file. It is important that it is created
97924       ** as an auto-vacuum capable db.
97925       */
97926       rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
97927       if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
97928         /* When setting the auto_vacuum mode to either "full" or
97929         ** "incremental", write the value of meta[6] in the database
97930         ** file. Before writing to meta[6], check that meta[3] indicates
97931         ** that this really is an auto-vacuum capable database.
97932         */
97933         static const int iLn = VDBE_OFFSET_LINENO(2);
97934         static const VdbeOpList setMeta6[] = {
97935           { OP_Transaction,    0,         1,                 0},    /* 0 */
97936           { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
97937           { OP_If,             1,         0,                 0},    /* 2 */
97938           { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
97939           { OP_Integer,        0,         1,                 0},    /* 4 */
97940           { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
97941         };
97942         int iAddr;
97943         iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
97944         sqlite3VdbeChangeP1(v, iAddr, iDb);
97945         sqlite3VdbeChangeP1(v, iAddr+1, iDb);
97946         sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
97947         sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
97948         sqlite3VdbeChangeP1(v, iAddr+5, iDb);
97949         sqlite3VdbeUsesBtree(v, iDb);
97950       }
97951     }
97952     break;
97953   }
97954 #endif
97955 
97956   /*
97957   **  PRAGMA [database.]incremental_vacuum(N)
97958   **
97959   ** Do N steps of incremental vacuuming on a database.
97960   */
97961 #ifndef SQLITE_OMIT_AUTOVACUUM
97962   case PragTyp_INCREMENTAL_VACUUM: {
97963     int iLimit, addr;
97964     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
97965       iLimit = 0x7fffffff;
97966     }
97967     sqlite3BeginWriteOperation(pParse, 0, iDb);
97968     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
97969     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); VdbeCoverage(v);
97970     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
97971     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
97972     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); VdbeCoverage(v);
97973     sqlite3VdbeJumpHere(v, addr);
97974     break;
97975   }
97976 #endif
97977 
97978 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
97979   /*
97980   **  PRAGMA [database.]cache_size
97981   **  PRAGMA [database.]cache_size=N
97982   **
97983   ** The first form reports the current local setting for the
97984   ** page cache size. The second form sets the local
97985   ** page cache size value.  If N is positive then that is the
97986   ** number of pages in the cache.  If N is negative, then the
97987   ** number of pages is adjusted so that the cache uses -N kibibytes
97988   ** of memory.
97989   */
97990   case PragTyp_CACHE_SIZE: {
97991     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97992     if( !zRight ){
97993       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
97994     }else{
97995       int size = sqlite3Atoi(zRight);
97996       pDb->pSchema->cache_size = size;
97997       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
97998     }
97999     break;
98000   }
98001 
98002   /*
98003   **  PRAGMA [database.]mmap_size(N)
98004   **
98005   ** Used to set mapping size limit. The mapping size limit is
98006   ** used to limit the aggregate size of all memory mapped regions of the
98007   ** database file. If this parameter is set to zero, then memory mapping
98008   ** is not used at all.  If N is negative, then the default memory map
98009   ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
98010   ** The parameter N is measured in bytes.
98011   **
98012   ** This value is advisory.  The underlying VFS is free to memory map
98013   ** as little or as much as it wants.  Except, if N is set to 0 then the
98014   ** upper layers will never invoke the xFetch interfaces to the VFS.
98015   */
98016   case PragTyp_MMAP_SIZE: {
98017     sqlite3_int64 sz;
98018 #if SQLITE_MAX_MMAP_SIZE>0
98019     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
98020     if( zRight ){
98021       int ii;
98022       sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
98023       if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
98024       if( pId2->n==0 ) db->szMmap = sz;
98025       for(ii=db->nDb-1; ii>=0; ii--){
98026         if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
98027           sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
98028         }
98029       }
98030     }
98031     sz = -1;
98032     rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
98033 #else
98034     sz = 0;
98035     rc = SQLITE_OK;
98036 #endif
98037     if( rc==SQLITE_OK ){
98038       returnSingleInt(pParse, "mmap_size", sz);
98039     }else if( rc!=SQLITE_NOTFOUND ){
98040       pParse->nErr++;
98041       pParse->rc = rc;
98042     }
98043     break;
98044   }
98045 
98046   /*
98047   **   PRAGMA temp_store
98048   **   PRAGMA temp_store = "default"|"memory"|"file"
98049   **
98050   ** Return or set the local value of the temp_store flag.  Changing
98051   ** the local value does not make changes to the disk file and the default
98052   ** value will be restored the next time the database is opened.
98053   **
98054   ** Note that it is possible for the library compile-time options to
98055   ** override this setting
98056   */
98057   case PragTyp_TEMP_STORE: {
98058     if( !zRight ){
98059       returnSingleInt(pParse, "temp_store", db->temp_store);
98060     }else{
98061       changeTempStorage(pParse, zRight);
98062     }
98063     break;
98064   }
98065 
98066   /*
98067   **   PRAGMA temp_store_directory
98068   **   PRAGMA temp_store_directory = ""|"directory_name"
98069   **
98070   ** Return or set the local value of the temp_store_directory flag.  Changing
98071   ** the value sets a specific directory to be used for temporary files.
98072   ** Setting to a null string reverts to the default temporary directory search.
98073   ** If temporary directory is changed, then invalidateTempStorage.
98074   **
98075   */
98076   case PragTyp_TEMP_STORE_DIRECTORY: {
98077     if( !zRight ){
98078       if( sqlite3_temp_directory ){
98079         sqlite3VdbeSetNumCols(v, 1);
98080         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
98081             "temp_store_directory", SQLITE_STATIC);
98082         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
98083         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
98084       }
98085     }else{
98086 #ifndef SQLITE_OMIT_WSD
98087       if( zRight[0] ){
98088         int res;
98089         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
98090         if( rc!=SQLITE_OK || res==0 ){
98091           sqlite3ErrorMsg(pParse, "not a writable directory");
98092           goto pragma_out;
98093         }
98094       }
98095       if( SQLITE_TEMP_STORE==0
98096        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
98097        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
98098       ){
98099         invalidateTempStorage(pParse);
98100       }
98101       sqlite3_free(sqlite3_temp_directory);
98102       if( zRight[0] ){
98103         sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
98104       }else{
98105         sqlite3_temp_directory = 0;
98106       }
98107 #endif /* SQLITE_OMIT_WSD */
98108     }
98109     break;
98110   }
98111 
98112 #if SQLITE_OS_WIN
98113   /*
98114   **   PRAGMA data_store_directory
98115   **   PRAGMA data_store_directory = ""|"directory_name"
98116   **
98117   ** Return or set the local value of the data_store_directory flag.  Changing
98118   ** the value sets a specific directory to be used for database files that
98119   ** were specified with a relative pathname.  Setting to a null string reverts
98120   ** to the default database directory, which for database files specified with
98121   ** a relative path will probably be based on the current directory for the
98122   ** process.  Database file specified with an absolute path are not impacted
98123   ** by this setting, regardless of its value.
98124   **
98125   */
98126   case PragTyp_DATA_STORE_DIRECTORY: {
98127     if( !zRight ){
98128       if( sqlite3_data_directory ){
98129         sqlite3VdbeSetNumCols(v, 1);
98130         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
98131             "data_store_directory", SQLITE_STATIC);
98132         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
98133         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
98134       }
98135     }else{
98136 #ifndef SQLITE_OMIT_WSD
98137       if( zRight[0] ){
98138         int res;
98139         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
98140         if( rc!=SQLITE_OK || res==0 ){
98141           sqlite3ErrorMsg(pParse, "not a writable directory");
98142           goto pragma_out;
98143         }
98144       }
98145       sqlite3_free(sqlite3_data_directory);
98146       if( zRight[0] ){
98147         sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
98148       }else{
98149         sqlite3_data_directory = 0;
98150       }
98151 #endif /* SQLITE_OMIT_WSD */
98152     }
98153     break;
98154   }
98155 #endif
98156 
98157 #if SQLITE_ENABLE_LOCKING_STYLE
98158   /*
98159   **   PRAGMA [database.]lock_proxy_file
98160   **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
98161   **
98162   ** Return or set the value of the lock_proxy_file flag.  Changing
98163   ** the value sets a specific file to be used for database access locks.
98164   **
98165   */
98166   case PragTyp_LOCK_PROXY_FILE: {
98167     if( !zRight ){
98168       Pager *pPager = sqlite3BtreePager(pDb->pBt);
98169       char *proxy_file_path = NULL;
98170       sqlite3_file *pFile = sqlite3PagerFile(pPager);
98171       sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
98172                            &proxy_file_path);
98173 
98174       if( proxy_file_path ){
98175         sqlite3VdbeSetNumCols(v, 1);
98176         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
98177                               "lock_proxy_file", SQLITE_STATIC);
98178         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
98179         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
98180       }
98181     }else{
98182       Pager *pPager = sqlite3BtreePager(pDb->pBt);
98183       sqlite3_file *pFile = sqlite3PagerFile(pPager);
98184       int res;
98185       if( zRight[0] ){
98186         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
98187                                      zRight);
98188       } else {
98189         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
98190                                      NULL);
98191       }
98192       if( res!=SQLITE_OK ){
98193         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
98194         goto pragma_out;
98195       }
98196     }
98197     break;
98198   }
98199 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
98200 
98201   /*
98202   **   PRAGMA [database.]synchronous
98203   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
98204   **
98205   ** Return or set the local value of the synchronous flag.  Changing
98206   ** the local value does not make changes to the disk file and the
98207   ** default value will be restored the next time the database is
98208   ** opened.
98209   */
98210   case PragTyp_SYNCHRONOUS: {
98211     if( !zRight ){
98212       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
98213     }else{
98214       if( !db->autoCommit ){
98215         sqlite3ErrorMsg(pParse,
98216             "Safety level may not be changed inside a transaction");
98217       }else{
98218         pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
98219         setAllPagerFlags(db);
98220       }
98221     }
98222     break;
98223   }
98224 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
98225 
98226 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
98227   case PragTyp_FLAG: {
98228     if( zRight==0 ){
98229       returnSingleInt(pParse, aPragmaNames[mid].zName,
98230                      (db->flags & aPragmaNames[mid].iArg)!=0 );
98231     }else{
98232       int mask = aPragmaNames[mid].iArg;    /* Mask of bits to set or clear. */
98233       if( db->autoCommit==0 ){
98234         /* Foreign key support may not be enabled or disabled while not
98235         ** in auto-commit mode.  */
98236         mask &= ~(SQLITE_ForeignKeys);
98237       }
98238 
98239       if( sqlite3GetBoolean(zRight, 0) ){
98240         db->flags |= mask;
98241       }else{
98242         db->flags &= ~mask;
98243         if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
98244       }
98245 
98246       /* Many of the flag-pragmas modify the code generated by the SQL
98247       ** compiler (eg. count_changes). So add an opcode to expire all
98248       ** compiled SQL statements after modifying a pragma value.
98249       */
98250       sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
98251       setAllPagerFlags(db);
98252     }
98253     break;
98254   }
98255 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
98256 
98257 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
98258   /*
98259   **   PRAGMA table_info(<table>)
98260   **
98261   ** Return a single row for each column of the named table. The columns of
98262   ** the returned data set are:
98263   **
98264   ** cid:        Column id (numbered from left to right, starting at 0)
98265   ** name:       Column name
98266   ** type:       Column declaration type.
98267   ** notnull:    True if 'NOT NULL' is part of column declaration
98268   ** dflt_value: The default value for the column, if any.
98269   */
98270   case PragTyp_TABLE_INFO: if( zRight ){
98271     Table *pTab;
98272     pTab = sqlite3FindTable(db, zRight, zDb);
98273     if( pTab ){
98274       int i, k;
98275       int nHidden = 0;
98276       Column *pCol;
98277       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
98278       sqlite3VdbeSetNumCols(v, 6);
98279       pParse->nMem = 6;
98280       sqlite3CodeVerifySchema(pParse, iDb);
98281       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
98282       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
98283       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
98284       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
98285       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
98286       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
98287       sqlite3ViewGetColumnNames(pParse, pTab);
98288       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
98289         if( IsHiddenColumn(pCol) ){
98290           nHidden++;
98291           continue;
98292         }
98293         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
98294         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
98295         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
98296            pCol->zType ? pCol->zType : "", 0);
98297         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
98298         if( pCol->zDflt ){
98299           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
98300         }else{
98301           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
98302         }
98303         if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
98304           k = 0;
98305         }else if( pPk==0 ){
98306           k = 1;
98307         }else{
98308           for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
98309         }
98310         sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
98311         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
98312       }
98313     }
98314   }
98315   break;
98316 
98317   case PragTyp_STATS: {
98318     Index *pIdx;
98319     HashElem *i;
98320     v = sqlite3GetVdbe(pParse);
98321     sqlite3VdbeSetNumCols(v, 4);
98322     pParse->nMem = 4;
98323     sqlite3CodeVerifySchema(pParse, iDb);
98324     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
98325     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
98326     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
98327     sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
98328     for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
98329       Table *pTab = sqliteHashData(i);
98330       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
98331       sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
98332       sqlite3VdbeAddOp2(v, OP_Integer,
98333                            (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
98334       sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
98335       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
98336       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98337         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
98338         sqlite3VdbeAddOp2(v, OP_Integer,
98339                              (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
98340         sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
98341         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
98342       }
98343     }
98344   }
98345   break;
98346 
98347   case PragTyp_INDEX_INFO: if( zRight ){
98348     Index *pIdx;
98349     Table *pTab;
98350     pIdx = sqlite3FindIndex(db, zRight, zDb);
98351     if( pIdx ){
98352       int i;
98353       pTab = pIdx->pTable;
98354       sqlite3VdbeSetNumCols(v, 3);
98355       pParse->nMem = 3;
98356       sqlite3CodeVerifySchema(pParse, iDb);
98357       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
98358       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
98359       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
98360       for(i=0; i<pIdx->nKeyCol; i++){
98361         i16 cnum = pIdx->aiColumn[i];
98362         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
98363         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
98364         assert( pTab->nCol>cnum );
98365         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
98366         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
98367       }
98368     }
98369   }
98370   break;
98371 
98372   case PragTyp_INDEX_LIST: if( zRight ){
98373     Index *pIdx;
98374     Table *pTab;
98375     int i;
98376     pTab = sqlite3FindTable(db, zRight, zDb);
98377     if( pTab ){
98378       v = sqlite3GetVdbe(pParse);
98379       sqlite3VdbeSetNumCols(v, 3);
98380       pParse->nMem = 3;
98381       sqlite3CodeVerifySchema(pParse, iDb);
98382       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
98383       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
98384       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
98385       for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
98386         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
98387         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
98388         sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
98389         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
98390       }
98391     }
98392   }
98393   break;
98394 
98395   case PragTyp_DATABASE_LIST: {
98396     int i;
98397     sqlite3VdbeSetNumCols(v, 3);
98398     pParse->nMem = 3;
98399     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
98400     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
98401     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
98402     for(i=0; i<db->nDb; i++){
98403       if( db->aDb[i].pBt==0 ) continue;
98404       assert( db->aDb[i].zName!=0 );
98405       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
98406       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
98407       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
98408            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
98409       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
98410     }
98411   }
98412   break;
98413 
98414   case PragTyp_COLLATION_LIST: {
98415     int i = 0;
98416     HashElem *p;
98417     sqlite3VdbeSetNumCols(v, 2);
98418     pParse->nMem = 2;
98419     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
98420     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
98421     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
98422       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
98423       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
98424       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
98425       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
98426     }
98427   }
98428   break;
98429 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
98430 
98431 #ifndef SQLITE_OMIT_FOREIGN_KEY
98432   case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
98433     FKey *pFK;
98434     Table *pTab;
98435     pTab = sqlite3FindTable(db, zRight, zDb);
98436     if( pTab ){
98437       v = sqlite3GetVdbe(pParse);
98438       pFK = pTab->pFKey;
98439       if( pFK ){
98440         int i = 0;
98441         sqlite3VdbeSetNumCols(v, 8);
98442         pParse->nMem = 8;
98443         sqlite3CodeVerifySchema(pParse, iDb);
98444         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
98445         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
98446         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
98447         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
98448         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
98449         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
98450         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
98451         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
98452         while(pFK){
98453           int j;
98454           for(j=0; j<pFK->nCol; j++){
98455             char *zCol = pFK->aCol[j].zCol;
98456             char *zOnDelete = (char *)actionName(pFK->aAction[0]);
98457             char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
98458             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
98459             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
98460             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
98461             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
98462                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
98463             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
98464             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
98465             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
98466             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
98467             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
98468           }
98469           ++i;
98470           pFK = pFK->pNextFrom;
98471         }
98472       }
98473     }
98474   }
98475   break;
98476 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
98477 
98478 #ifndef SQLITE_OMIT_FOREIGN_KEY
98479 #ifndef SQLITE_OMIT_TRIGGER
98480   case PragTyp_FOREIGN_KEY_CHECK: {
98481     FKey *pFK;             /* A foreign key constraint */
98482     Table *pTab;           /* Child table contain "REFERENCES" keyword */
98483     Table *pParent;        /* Parent table that child points to */
98484     Index *pIdx;           /* Index in the parent table */
98485     int i;                 /* Loop counter:  Foreign key number for pTab */
98486     int j;                 /* Loop counter:  Field of the foreign key */
98487     HashElem *k;           /* Loop counter:  Next table in schema */
98488     int x;                 /* result variable */
98489     int regResult;         /* 3 registers to hold a result row */
98490     int regKey;            /* Register to hold key for checking the FK */
98491     int regRow;            /* Registers to hold a row from pTab */
98492     int addrTop;           /* Top of a loop checking foreign keys */
98493     int addrOk;            /* Jump here if the key is OK */
98494     int *aiCols;           /* child to parent column mapping */
98495 
98496     regResult = pParse->nMem+1;
98497     pParse->nMem += 4;
98498     regKey = ++pParse->nMem;
98499     regRow = ++pParse->nMem;
98500     v = sqlite3GetVdbe(pParse);
98501     sqlite3VdbeSetNumCols(v, 4);
98502     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
98503     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
98504     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
98505     sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
98506     sqlite3CodeVerifySchema(pParse, iDb);
98507     k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
98508     while( k ){
98509       if( zRight ){
98510         pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
98511         k = 0;
98512       }else{
98513         pTab = (Table*)sqliteHashData(k);
98514         k = sqliteHashNext(k);
98515       }
98516       if( pTab==0 || pTab->pFKey==0 ) continue;
98517       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
98518       if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
98519       sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
98520       sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
98521                         P4_TRANSIENT);
98522       for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
98523         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
98524         if( pParent==0 ) continue;
98525         pIdx = 0;
98526         sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
98527         x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
98528         if( x==0 ){
98529           if( pIdx==0 ){
98530             sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
98531           }else{
98532             sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
98533             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
98534           }
98535         }else{
98536           k = 0;
98537           break;
98538         }
98539       }
98540       assert( pParse->nErr>0 || pFK==0 );
98541       if( pFK ) break;
98542       if( pParse->nTab<i ) pParse->nTab = i;
98543       addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0); VdbeCoverage(v);
98544       for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
98545         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
98546         pIdx = 0;
98547         aiCols = 0;
98548         if( pParent ){
98549           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
98550           assert( x==0 );
98551         }
98552         addrOk = sqlite3VdbeMakeLabel(v);
98553         if( pParent && pIdx==0 ){
98554           int iKey = pFK->aCol[0].iFrom;
98555           assert( iKey>=0 && iKey<pTab->nCol );
98556           if( iKey!=pTab->iPKey ){
98557             sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
98558             sqlite3ColumnDefault(v, pTab, iKey, regRow);
98559             sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk); VdbeCoverage(v);
98560             sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
98561                sqlite3VdbeCurrentAddr(v)+3); VdbeCoverage(v);
98562           }else{
98563             sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
98564           }
98565           sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow); VdbeCoverage(v);
98566           sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
98567           sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
98568         }else{
98569           for(j=0; j<pFK->nCol; j++){
98570             sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
98571                             aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
98572             sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk); VdbeCoverage(v);
98573           }
98574           if( pParent ){
98575             sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, pFK->nCol, regKey,
98576                               sqlite3IndexAffinityStr(v,pIdx), pFK->nCol);
98577             sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
98578             VdbeCoverage(v);
98579           }
98580         }
98581         sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
98582         sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
98583                           pFK->zTo, P4_TRANSIENT);
98584         sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
98585         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
98586         sqlite3VdbeResolveLabel(v, addrOk);
98587         sqlite3DbFree(db, aiCols);
98588       }
98589       sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1); VdbeCoverage(v);
98590       sqlite3VdbeJumpHere(v, addrTop);
98591     }
98592   }
98593   break;
98594 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
98595 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
98596 
98597 #ifndef NDEBUG
98598   case PragTyp_PARSER_TRACE: {
98599     if( zRight ){
98600       if( sqlite3GetBoolean(zRight, 0) ){
98601         sqlite3ParserTrace(stderr, "parser: ");
98602       }else{
98603         sqlite3ParserTrace(0, 0);
98604       }
98605     }
98606   }
98607   break;
98608 #endif
98609 
98610   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
98611   ** used will be case sensitive or not depending on the RHS.
98612   */
98613   case PragTyp_CASE_SENSITIVE_LIKE: {
98614     if( zRight ){
98615       sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
98616     }
98617   }
98618   break;
98619 
98620 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
98621 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
98622 #endif
98623 
98624 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
98625   /* Pragma "quick_check" is reduced version of
98626   ** integrity_check designed to detect most database corruption
98627   ** without most of the overhead of a full integrity-check.
98628   */
98629   case PragTyp_INTEGRITY_CHECK: {
98630     int i, j, addr, mxErr;
98631 
98632     /* Code that appears at the end of the integrity check.  If no error
98633     ** messages have been generated, output OK.  Otherwise output the
98634     ** error message
98635     */
98636     static const int iLn = VDBE_OFFSET_LINENO(2);
98637     static const VdbeOpList endCode[] = {
98638       { OP_AddImm,      1, 0,        0},    /* 0 */
98639       { OP_IfNeg,       1, 0,        0},    /* 1 */
98640       { OP_String8,     0, 3,        0},    /* 2 */
98641       { OP_ResultRow,   3, 1,        0},
98642     };
98643 
98644     int isQuick = (sqlite3Tolower(zLeft[0])=='q');
98645 
98646     /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
98647     ** then iDb is set to the index of the database identified by <db>.
98648     ** In this case, the integrity of database iDb only is verified by
98649     ** the VDBE created below.
98650     **
98651     ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
98652     ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
98653     ** to -1 here, to indicate that the VDBE should verify the integrity
98654     ** of all attached databases.  */
98655     assert( iDb>=0 );
98656     assert( iDb==0 || pId2->z );
98657     if( pId2->z==0 ) iDb = -1;
98658 
98659     /* Initialize the VDBE program */
98660     pParse->nMem = 6;
98661     sqlite3VdbeSetNumCols(v, 1);
98662     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
98663 
98664     /* Set the maximum error count */
98665     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
98666     if( zRight ){
98667       sqlite3GetInt32(zRight, &mxErr);
98668       if( mxErr<=0 ){
98669         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
98670       }
98671     }
98672     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
98673 
98674     /* Do an integrity check on each database file */
98675     for(i=0; i<db->nDb; i++){
98676       HashElem *x;
98677       Hash *pTbls;
98678       int cnt = 0;
98679 
98680       if( OMIT_TEMPDB && i==1 ) continue;
98681       if( iDb>=0 && i!=iDb ) continue;
98682 
98683       sqlite3CodeVerifySchema(pParse, i);
98684       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
98685       VdbeCoverage(v);
98686       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98687       sqlite3VdbeJumpHere(v, addr);
98688 
98689       /* Do an integrity check of the B-Tree
98690       **
98691       ** Begin by filling registers 2, 3, ... with the root pages numbers
98692       ** for all tables and indices in the database.
98693       */
98694       assert( sqlite3SchemaMutexHeld(db, i, 0) );
98695       pTbls = &db->aDb[i].pSchema->tblHash;
98696       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
98697         Table *pTab = sqliteHashData(x);
98698         Index *pIdx;
98699         if( HasRowid(pTab) ){
98700           sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
98701           VdbeComment((v, "%s", pTab->zName));
98702           cnt++;
98703         }
98704         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98705           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
98706           VdbeComment((v, "%s", pIdx->zName));
98707           cnt++;
98708         }
98709       }
98710 
98711       /* Make sure sufficient number of registers have been allocated */
98712       pParse->nMem = MAX( pParse->nMem, cnt+8 );
98713 
98714       /* Do the b-tree integrity checks */
98715       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
98716       sqlite3VdbeChangeP5(v, (u8)i);
98717       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); VdbeCoverage(v);
98718       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
98719          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
98720          P4_DYNAMIC);
98721       sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
98722       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
98723       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
98724       sqlite3VdbeJumpHere(v, addr);
98725 
98726       /* Make sure all the indices are constructed correctly.
98727       */
98728       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
98729         Table *pTab = sqliteHashData(x);
98730         Index *pIdx, *pPk;
98731         Index *pPrior = 0;
98732         int loopTop;
98733         int iDataCur, iIdxCur;
98734         int r1 = -1;
98735 
98736         if( pTab->pIndex==0 ) continue;
98737         pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
98738         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
98739         VdbeCoverage(v);
98740         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98741         sqlite3VdbeJumpHere(v, addr);
98742         sqlite3ExprCacheClear(pParse);
98743         sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
98744                                    1, 0, &iDataCur, &iIdxCur);
98745         sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
98746         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98747           sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
98748         }
98749         pParse->nMem = MAX(pParse->nMem, 8+j);
98750         sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0); VdbeCoverage(v);
98751         loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
98752         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98753           int jmp2, jmp3, jmp4;
98754           if( pPk==pIdx ) continue;
98755           r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
98756                                        pPrior, r1);
98757           pPrior = pIdx;
98758           sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
98759           jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
98760                                       pIdx->nColumn); VdbeCoverage(v);
98761           sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
98762           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
98763           sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
98764           sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
98765                             P4_STATIC);
98766           sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
98767           sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
98768           sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
98769           sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
98770           jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1); VdbeCoverage(v);
98771           sqlite3VdbeAddOp0(v, OP_Halt);
98772           sqlite3VdbeJumpHere(v, jmp4);
98773           sqlite3VdbeJumpHere(v, jmp2);
98774           sqlite3VdbeResolveLabel(v, jmp3);
98775         }
98776         sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop); VdbeCoverage(v);
98777         sqlite3VdbeJumpHere(v, loopTop-1);
98778 #ifndef SQLITE_OMIT_BTREECOUNT
98779         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
98780                      "wrong # of entries in index ", P4_STATIC);
98781         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98782           if( pPk==pIdx ) continue;
98783           addr = sqlite3VdbeCurrentAddr(v);
98784           sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2); VdbeCoverage(v);
98785           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98786           sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
98787           sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3); VdbeCoverage(v);
98788           sqlite3VdbeChangeP5(v, SQLITE_NOTNULL);
98789           sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
98790           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
98791           sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
98792           sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
98793         }
98794 #endif /* SQLITE_OMIT_BTREECOUNT */
98795       }
98796     }
98797     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
98798     sqlite3VdbeChangeP2(v, addr, -mxErr);
98799     sqlite3VdbeJumpHere(v, addr+1);
98800     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
98801   }
98802   break;
98803 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
98804 
98805 #ifndef SQLITE_OMIT_UTF16
98806   /*
98807   **   PRAGMA encoding
98808   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
98809   **
98810   ** In its first form, this pragma returns the encoding of the main
98811   ** database. If the database is not initialized, it is initialized now.
98812   **
98813   ** The second form of this pragma is a no-op if the main database file
98814   ** has not already been initialized. In this case it sets the default
98815   ** encoding that will be used for the main database file if a new file
98816   ** is created. If an existing main database file is opened, then the
98817   ** default text encoding for the existing database is used.
98818   **
98819   ** In all cases new databases created using the ATTACH command are
98820   ** created to use the same default text encoding as the main database. If
98821   ** the main database has not been initialized and/or created when ATTACH
98822   ** is executed, this is done before the ATTACH operation.
98823   **
98824   ** In the second form this pragma sets the text encoding to be used in
98825   ** new database files created using this database handle. It is only
98826   ** useful if invoked immediately after the main database i
98827   */
98828   case PragTyp_ENCODING: {
98829     static const struct EncName {
98830       char *zName;
98831       u8 enc;
98832     } encnames[] = {
98833       { "UTF8",     SQLITE_UTF8        },
98834       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
98835       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
98836       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
98837       { "UTF16le",  SQLITE_UTF16LE     },
98838       { "UTF16be",  SQLITE_UTF16BE     },
98839       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
98840       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
98841       { 0, 0 }
98842     };
98843     const struct EncName *pEnc;
98844     if( !zRight ){    /* "PRAGMA encoding" */
98845       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
98846       sqlite3VdbeSetNumCols(v, 1);
98847       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
98848       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
98849       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
98850       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
98851       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
98852       sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
98853       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
98854     }else{                        /* "PRAGMA encoding = XXX" */
98855       /* Only change the value of sqlite.enc if the database handle is not
98856       ** initialized. If the main database exists, the new sqlite.enc value
98857       ** will be overwritten when the schema is next loaded. If it does not
98858       ** already exists, it will be created to use the new encoding value.
98859       */
98860       if(
98861         !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
98862         DbHasProperty(db, 0, DB_Empty)
98863       ){
98864         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
98865           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
98866             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
98867             break;
98868           }
98869         }
98870         if( !pEnc->zName ){
98871           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
98872         }
98873       }
98874     }
98875   }
98876   break;
98877 #endif /* SQLITE_OMIT_UTF16 */
98878 
98879 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
98880   /*
98881   **   PRAGMA [database.]schema_version
98882   **   PRAGMA [database.]schema_version = <integer>
98883   **
98884   **   PRAGMA [database.]user_version
98885   **   PRAGMA [database.]user_version = <integer>
98886   **
98887   **   PRAGMA [database.]freelist_count = <integer>
98888   **
98889   **   PRAGMA [database.]application_id
98890   **   PRAGMA [database.]application_id = <integer>
98891   **
98892   ** The pragma's schema_version and user_version are used to set or get
98893   ** the value of the schema-version and user-version, respectively. Both
98894   ** the schema-version and the user-version are 32-bit signed integers
98895   ** stored in the database header.
98896   **
98897   ** The schema-cookie is usually only manipulated internally by SQLite. It
98898   ** is incremented by SQLite whenever the database schema is modified (by
98899   ** creating or dropping a table or index). The schema version is used by
98900   ** SQLite each time a query is executed to ensure that the internal cache
98901   ** of the schema used when compiling the SQL query matches the schema of
98902   ** the database against which the compiled query is actually executed.
98903   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
98904   ** the schema-version is potentially dangerous and may lead to program
98905   ** crashes or database corruption. Use with caution!
98906   **
98907   ** The user-version is not used internally by SQLite. It may be used by
98908   ** applications for any purpose.
98909   */
98910   case PragTyp_HEADER_VALUE: {
98911     int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
98912     sqlite3VdbeUsesBtree(v, iDb);
98913     switch( zLeft[0] ){
98914       case 'a': case 'A':
98915         iCookie = BTREE_APPLICATION_ID;
98916         break;
98917       case 'f': case 'F':
98918         iCookie = BTREE_FREE_PAGE_COUNT;
98919         break;
98920       case 's': case 'S':
98921         iCookie = BTREE_SCHEMA_VERSION;
98922         break;
98923       default:
98924         iCookie = BTREE_USER_VERSION;
98925         break;
98926     }
98927 
98928     if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
98929       /* Write the specified cookie value */
98930       static const VdbeOpList setCookie[] = {
98931         { OP_Transaction,    0,  1,  0},    /* 0 */
98932         { OP_Integer,        0,  1,  0},    /* 1 */
98933         { OP_SetCookie,      0,  0,  1},    /* 2 */
98934       };
98935       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
98936       sqlite3VdbeChangeP1(v, addr, iDb);
98937       sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
98938       sqlite3VdbeChangeP1(v, addr+2, iDb);
98939       sqlite3VdbeChangeP2(v, addr+2, iCookie);
98940     }else{
98941       /* Read the specified cookie value */
98942       static const VdbeOpList readCookie[] = {
98943         { OP_Transaction,     0,  0,  0},    /* 0 */
98944         { OP_ReadCookie,      0,  1,  0},    /* 1 */
98945         { OP_ResultRow,       1,  1,  0}
98946       };
98947       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie, 0);
98948       sqlite3VdbeChangeP1(v, addr, iDb);
98949       sqlite3VdbeChangeP1(v, addr+1, iDb);
98950       sqlite3VdbeChangeP3(v, addr+1, iCookie);
98951       sqlite3VdbeSetNumCols(v, 1);
98952       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
98953     }
98954   }
98955   break;
98956 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
98957 
98958 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
98959   /*
98960   **   PRAGMA compile_options
98961   **
98962   ** Return the names of all compile-time options used in this build,
98963   ** one option per row.
98964   */
98965   case PragTyp_COMPILE_OPTIONS: {
98966     int i = 0;
98967     const char *zOpt;
98968     sqlite3VdbeSetNumCols(v, 1);
98969     pParse->nMem = 1;
98970     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
98971     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
98972       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
98973       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
98974     }
98975   }
98976   break;
98977 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
98978 
98979 #ifndef SQLITE_OMIT_WAL
98980   /*
98981   **   PRAGMA [database.]wal_checkpoint = passive|full|restart
98982   **
98983   ** Checkpoint the database.
98984   */
98985   case PragTyp_WAL_CHECKPOINT: {
98986     int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
98987     int eMode = SQLITE_CHECKPOINT_PASSIVE;
98988     if( zRight ){
98989       if( sqlite3StrICmp(zRight, "full")==0 ){
98990         eMode = SQLITE_CHECKPOINT_FULL;
98991       }else if( sqlite3StrICmp(zRight, "restart")==0 ){
98992         eMode = SQLITE_CHECKPOINT_RESTART;
98993       }
98994     }
98995     sqlite3VdbeSetNumCols(v, 3);
98996     pParse->nMem = 3;
98997     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
98998     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
98999     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
99000 
99001     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
99002     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
99003   }
99004   break;
99005 
99006   /*
99007   **   PRAGMA wal_autocheckpoint
99008   **   PRAGMA wal_autocheckpoint = N
99009   **
99010   ** Configure a database connection to automatically checkpoint a database
99011   ** after accumulating N frames in the log. Or query for the current value
99012   ** of N.
99013   */
99014   case PragTyp_WAL_AUTOCHECKPOINT: {
99015     if( zRight ){
99016       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
99017     }
99018     returnSingleInt(pParse, "wal_autocheckpoint",
99019        db->xWalCallback==sqlite3WalDefaultHook ?
99020            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
99021   }
99022   break;
99023 #endif
99024 
99025   /*
99026   **  PRAGMA shrink_memory
99027   **
99028   ** This pragma attempts to free as much memory as possible from the
99029   ** current database connection.
99030   */
99031   case PragTyp_SHRINK_MEMORY: {
99032     sqlite3_db_release_memory(db);
99033     break;
99034   }
99035 
99036   /*
99037   **   PRAGMA busy_timeout
99038   **   PRAGMA busy_timeout = N
99039   **
99040   ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
99041   ** if one is set.  If no busy handler or a different busy handler is set
99042   ** then 0 is returned.  Setting the busy_timeout to 0 or negative
99043   ** disables the timeout.
99044   */
99045   /*case PragTyp_BUSY_TIMEOUT*/ default: {
99046     assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
99047     if( zRight ){
99048       sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
99049     }
99050     returnSingleInt(pParse, "timeout",  db->busyTimeout);
99051     break;
99052   }
99053 
99054   /*
99055   **   PRAGMA soft_heap_limit
99056   **   PRAGMA soft_heap_limit = N
99057   **
99058   ** Call sqlite3_soft_heap_limit64(N).  Return the result.  If N is omitted,
99059   ** use -1.
99060   */
99061   case PragTyp_SOFT_HEAP_LIMIT: {
99062     sqlite3_int64 N;
99063     if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
99064       sqlite3_soft_heap_limit64(N);
99065     }
99066     returnSingleInt(pParse, "soft_heap_limit",  sqlite3_soft_heap_limit64(-1));
99067     break;
99068   }
99069 
99070 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
99071   /*
99072   ** Report the current state of file logs for all databases
99073   */
99074   case PragTyp_LOCK_STATUS: {
99075     static const char *const azLockName[] = {
99076       "unlocked", "shared", "reserved", "pending", "exclusive"
99077     };
99078     int i;
99079     sqlite3VdbeSetNumCols(v, 2);
99080     pParse->nMem = 2;
99081     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
99082     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
99083     for(i=0; i<db->nDb; i++){
99084       Btree *pBt;
99085       const char *zState = "unknown";
99086       int j;
99087       if( db->aDb[i].zName==0 ) continue;
99088       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
99089       pBt = db->aDb[i].pBt;
99090       if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
99091         zState = "closed";
99092       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
99093                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
99094          zState = azLockName[j];
99095       }
99096       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
99097       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
99098     }
99099     break;
99100   }
99101 #endif
99102 
99103 #ifdef SQLITE_HAS_CODEC
99104   case PragTyp_KEY: {
99105     if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
99106     break;
99107   }
99108   case PragTyp_REKEY: {
99109     if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
99110     break;
99111   }
99112   case PragTyp_HEXKEY: {
99113     if( zRight ){
99114       u8 iByte;
99115       int i;
99116       char zKey[40];
99117       for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
99118         iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
99119         if( (i&1)!=0 ) zKey[i/2] = iByte;
99120       }
99121       if( (zLeft[3] & 0xf)==0xb ){
99122         sqlite3_key_v2(db, zDb, zKey, i/2);
99123       }else{
99124         sqlite3_rekey_v2(db, zDb, zKey, i/2);
99125       }
99126     }
99127     break;
99128   }
99129 #endif
99130 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
99131   case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
99132 #ifdef SQLITE_HAS_CODEC
99133     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
99134       sqlite3_activate_see(&zRight[4]);
99135     }
99136 #endif
99137 #ifdef SQLITE_ENABLE_CEROD
99138     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
99139       sqlite3_activate_cerod(&zRight[6]);
99140     }
99141 #endif
99142   }
99143   break;
99144 #endif
99145 
99146   } /* End of the PRAGMA switch */
99147 
99148 pragma_out:
99149   sqlite3DbFree(db, zLeft);
99150   sqlite3DbFree(db, zRight);
99151 }
99152 
99153 #endif /* SQLITE_OMIT_PRAGMA */
99154 
99155 /************** End of pragma.c **********************************************/
99156 /************** Begin file prepare.c *****************************************/
99157 /*
99158 ** 2005 May 25
99159 **
99160 ** The author disclaims copyright to this source code.  In place of
99161 ** a legal notice, here is a blessing:
99162 **
99163 **    May you do good and not evil.
99164 **    May you find forgiveness for yourself and forgive others.
99165 **    May you share freely, never taking more than you give.
99166 **
99167 *************************************************************************
99168 ** This file contains the implementation of the sqlite3_prepare()
99169 ** interface, and routines that contribute to loading the database schema
99170 ** from disk.
99171 */
99172 
99173 /*
99174 ** Fill the InitData structure with an error message that indicates
99175 ** that the database is corrupt.
99176 */
99177 static void corruptSchema(
99178   InitData *pData,     /* Initialization context */
99179   const char *zObj,    /* Object being parsed at the point of error */
99180   const char *zExtra   /* Error information */
99181 ){
99182   sqlite3 *db = pData->db;
99183   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
99184     if( zObj==0 ) zObj = "?";
99185     sqlite3SetString(pData->pzErrMsg, db,
99186       "malformed database schema (%s)", zObj);
99187     if( zExtra ){
99188       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
99189                                  "%s - %s", *pData->pzErrMsg, zExtra);
99190     }
99191   }
99192   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
99193 }
99194 
99195 /*
99196 ** This is the callback routine for the code that initializes the
99197 ** database.  See sqlite3Init() below for additional information.
99198 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
99199 **
99200 ** Each callback contains the following information:
99201 **
99202 **     argv[0] = name of thing being created
99203 **     argv[1] = root page number for table or index. 0 for trigger or view.
99204 **     argv[2] = SQL text for the CREATE statement.
99205 **
99206 */
99207 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
99208   InitData *pData = (InitData*)pInit;
99209   sqlite3 *db = pData->db;
99210   int iDb = pData->iDb;
99211 
99212   assert( argc==3 );
99213   UNUSED_PARAMETER2(NotUsed, argc);
99214   assert( sqlite3_mutex_held(db->mutex) );
99215   DbClearProperty(db, iDb, DB_Empty);
99216   if( db->mallocFailed ){
99217     corruptSchema(pData, argv[0], 0);
99218     return 1;
99219   }
99220 
99221   assert( iDb>=0 && iDb<db->nDb );
99222   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
99223   if( argv[1]==0 ){
99224     corruptSchema(pData, argv[0], 0);
99225   }else if( argv[2] && argv[2][0] ){
99226     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
99227     ** But because db->init.busy is set to 1, no VDBE code is generated
99228     ** or executed.  All the parser does is build the internal data
99229     ** structures that describe the table, index, or view.
99230     */
99231     int rc;
99232     sqlite3_stmt *pStmt;
99233     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
99234 
99235     assert( db->init.busy );
99236     db->init.iDb = iDb;
99237     db->init.newTnum = sqlite3Atoi(argv[1]);
99238     db->init.orphanTrigger = 0;
99239     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
99240     rc = db->errCode;
99241     assert( (rc&0xFF)==(rcp&0xFF) );
99242     db->init.iDb = 0;
99243     if( SQLITE_OK!=rc ){
99244       if( db->init.orphanTrigger ){
99245         assert( iDb==1 );
99246       }else{
99247         pData->rc = rc;
99248         if( rc==SQLITE_NOMEM ){
99249           db->mallocFailed = 1;
99250         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
99251           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
99252         }
99253       }
99254     }
99255     sqlite3_finalize(pStmt);
99256   }else if( argv[0]==0 ){
99257     corruptSchema(pData, 0, 0);
99258   }else{
99259     /* If the SQL column is blank it means this is an index that
99260     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
99261     ** constraint for a CREATE TABLE.  The index should have already
99262     ** been created when we processed the CREATE TABLE.  All we have
99263     ** to do here is record the root page number for that index.
99264     */
99265     Index *pIndex;
99266     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
99267     if( pIndex==0 ){
99268       /* This can occur if there exists an index on a TEMP table which
99269       ** has the same name as another index on a permanent index.  Since
99270       ** the permanent table is hidden by the TEMP table, we can also
99271       ** safely ignore the index on the permanent table.
99272       */
99273       /* Do Nothing */;
99274     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
99275       corruptSchema(pData, argv[0], "invalid rootpage");
99276     }
99277   }
99278   return 0;
99279 }
99280 
99281 /*
99282 ** Attempt to read the database schema and initialize internal
99283 ** data structures for a single database file.  The index of the
99284 ** database file is given by iDb.  iDb==0 is used for the main
99285 ** database.  iDb==1 should never be used.  iDb>=2 is used for
99286 ** auxiliary databases.  Return one of the SQLITE_ error codes to
99287 ** indicate success or failure.
99288 */
99289 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
99290   int rc;
99291   int i;
99292 #ifndef SQLITE_OMIT_DEPRECATED
99293   int size;
99294 #endif
99295   Table *pTab;
99296   Db *pDb;
99297   char const *azArg[4];
99298   int meta[5];
99299   InitData initData;
99300   char const *zMasterSchema;
99301   char const *zMasterName;
99302   int openedTransaction = 0;
99303 
99304   /*
99305   ** The master database table has a structure like this
99306   */
99307   static const char master_schema[] =
99308      "CREATE TABLE sqlite_master(\n"
99309      "  type text,\n"
99310      "  name text,\n"
99311      "  tbl_name text,\n"
99312      "  rootpage integer,\n"
99313      "  sql text\n"
99314      ")"
99315   ;
99316 #ifndef SQLITE_OMIT_TEMPDB
99317   static const char temp_master_schema[] =
99318      "CREATE TEMP TABLE sqlite_temp_master(\n"
99319      "  type text,\n"
99320      "  name text,\n"
99321      "  tbl_name text,\n"
99322      "  rootpage integer,\n"
99323      "  sql text\n"
99324      ")"
99325   ;
99326 #else
99327   #define temp_master_schema 0
99328 #endif
99329 
99330   assert( iDb>=0 && iDb<db->nDb );
99331   assert( db->aDb[iDb].pSchema );
99332   assert( sqlite3_mutex_held(db->mutex) );
99333   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
99334 
99335   /* zMasterSchema and zInitScript are set to point at the master schema
99336   ** and initialisation script appropriate for the database being
99337   ** initialized. zMasterName is the name of the master table.
99338   */
99339   if( !OMIT_TEMPDB && iDb==1 ){
99340     zMasterSchema = temp_master_schema;
99341   }else{
99342     zMasterSchema = master_schema;
99343   }
99344   zMasterName = SCHEMA_TABLE(iDb);
99345 
99346   /* Construct the schema tables.  */
99347   azArg[0] = zMasterName;
99348   azArg[1] = "1";
99349   azArg[2] = zMasterSchema;
99350   azArg[3] = 0;
99351   initData.db = db;
99352   initData.iDb = iDb;
99353   initData.rc = SQLITE_OK;
99354   initData.pzErrMsg = pzErrMsg;
99355   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
99356   if( initData.rc ){
99357     rc = initData.rc;
99358     goto error_out;
99359   }
99360   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
99361   if( ALWAYS(pTab) ){
99362     pTab->tabFlags |= TF_Readonly;
99363   }
99364 
99365   /* Create a cursor to hold the database open
99366   */
99367   pDb = &db->aDb[iDb];
99368   if( pDb->pBt==0 ){
99369     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
99370       DbSetProperty(db, 1, DB_SchemaLoaded);
99371     }
99372     return SQLITE_OK;
99373   }
99374 
99375   /* If there is not already a read-only (or read-write) transaction opened
99376   ** on the b-tree database, open one now. If a transaction is opened, it
99377   ** will be closed before this function returns.  */
99378   sqlite3BtreeEnter(pDb->pBt);
99379   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
99380     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
99381     if( rc!=SQLITE_OK ){
99382       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
99383       goto initone_error_out;
99384     }
99385     openedTransaction = 1;
99386   }
99387 
99388   /* Get the database meta information.
99389   **
99390   ** Meta values are as follows:
99391   **    meta[0]   Schema cookie.  Changes with each schema change.
99392   **    meta[1]   File format of schema layer.
99393   **    meta[2]   Size of the page cache.
99394   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
99395   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
99396   **    meta[5]   User version
99397   **    meta[6]   Incremental vacuum mode
99398   **    meta[7]   unused
99399   **    meta[8]   unused
99400   **    meta[9]   unused
99401   **
99402   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
99403   ** the possible values of meta[4].
99404   */
99405   for(i=0; i<ArraySize(meta); i++){
99406     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
99407   }
99408   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
99409 
99410   /* If opening a non-empty database, check the text encoding. For the
99411   ** main database, set sqlite3.enc to the encoding of the main database.
99412   ** For an attached db, it is an error if the encoding is not the same
99413   ** as sqlite3.enc.
99414   */
99415   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
99416     if( iDb==0 ){
99417 #ifndef SQLITE_OMIT_UTF16
99418       u8 encoding;
99419       /* If opening the main database, set ENC(db). */
99420       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
99421       if( encoding==0 ) encoding = SQLITE_UTF8;
99422       ENC(db) = encoding;
99423 #else
99424       ENC(db) = SQLITE_UTF8;
99425 #endif
99426     }else{
99427       /* If opening an attached database, the encoding much match ENC(db) */
99428       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
99429         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
99430             " text encoding as main database");
99431         rc = SQLITE_ERROR;
99432         goto initone_error_out;
99433       }
99434     }
99435   }else{
99436     DbSetProperty(db, iDb, DB_Empty);
99437   }
99438   pDb->pSchema->enc = ENC(db);
99439 
99440   if( pDb->pSchema->cache_size==0 ){
99441 #ifndef SQLITE_OMIT_DEPRECATED
99442     size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
99443     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
99444     pDb->pSchema->cache_size = size;
99445 #else
99446     pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
99447 #endif
99448     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
99449   }
99450 
99451   /*
99452   ** file_format==1    Version 3.0.0.
99453   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
99454   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
99455   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
99456   */
99457   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
99458   if( pDb->pSchema->file_format==0 ){
99459     pDb->pSchema->file_format = 1;
99460   }
99461   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
99462     sqlite3SetString(pzErrMsg, db, "unsupported file format");
99463     rc = SQLITE_ERROR;
99464     goto initone_error_out;
99465   }
99466 
99467   /* Ticket #2804:  When we open a database in the newer file format,
99468   ** clear the legacy_file_format pragma flag so that a VACUUM will
99469   ** not downgrade the database and thus invalidate any descending
99470   ** indices that the user might have created.
99471   */
99472   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
99473     db->flags &= ~SQLITE_LegacyFileFmt;
99474   }
99475 
99476   /* Read the schema information out of the schema tables
99477   */
99478   assert( db->init.busy );
99479   {
99480     char *zSql;
99481     zSql = sqlite3MPrintf(db,
99482         "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
99483         db->aDb[iDb].zName, zMasterName);
99484 #ifndef SQLITE_OMIT_AUTHORIZATION
99485     {
99486       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
99487       xAuth = db->xAuth;
99488       db->xAuth = 0;
99489 #endif
99490       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
99491 #ifndef SQLITE_OMIT_AUTHORIZATION
99492       db->xAuth = xAuth;
99493     }
99494 #endif
99495     if( rc==SQLITE_OK ) rc = initData.rc;
99496     sqlite3DbFree(db, zSql);
99497 #ifndef SQLITE_OMIT_ANALYZE
99498     if( rc==SQLITE_OK ){
99499       sqlite3AnalysisLoad(db, iDb);
99500     }
99501 #endif
99502   }
99503   if( db->mallocFailed ){
99504     rc = SQLITE_NOMEM;
99505     sqlite3ResetAllSchemasOfConnection(db);
99506   }
99507   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
99508     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
99509     ** the schema loaded, even if errors occurred. In this situation the
99510     ** current sqlite3_prepare() operation will fail, but the following one
99511     ** will attempt to compile the supplied statement against whatever subset
99512     ** of the schema was loaded before the error occurred. The primary
99513     ** purpose of this is to allow access to the sqlite_master table
99514     ** even when its contents have been corrupted.
99515     */
99516     DbSetProperty(db, iDb, DB_SchemaLoaded);
99517     rc = SQLITE_OK;
99518   }
99519 
99520   /* Jump here for an error that occurs after successfully allocating
99521   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
99522   ** before that point, jump to error_out.
99523   */
99524 initone_error_out:
99525   if( openedTransaction ){
99526     sqlite3BtreeCommit(pDb->pBt);
99527   }
99528   sqlite3BtreeLeave(pDb->pBt);
99529 
99530 error_out:
99531   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
99532     db->mallocFailed = 1;
99533   }
99534   return rc;
99535 }
99536 
99537 /*
99538 ** Initialize all database files - the main database file, the file
99539 ** used to store temporary tables, and any additional database files
99540 ** created using ATTACH statements.  Return a success code.  If an
99541 ** error occurs, write an error message into *pzErrMsg.
99542 **
99543 ** After a database is initialized, the DB_SchemaLoaded bit is set
99544 ** bit is set in the flags field of the Db structure. If the database
99545 ** file was of zero-length, then the DB_Empty flag is also set.
99546 */
99547 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
99548   int i, rc;
99549   int commit_internal = !(db->flags&SQLITE_InternChanges);
99550 
99551   assert( sqlite3_mutex_held(db->mutex) );
99552   rc = SQLITE_OK;
99553   db->init.busy = 1;
99554   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
99555     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
99556     rc = sqlite3InitOne(db, i, pzErrMsg);
99557     if( rc ){
99558       sqlite3ResetOneSchema(db, i);
99559     }
99560   }
99561 
99562   /* Once all the other databases have been initialized, load the schema
99563   ** for the TEMP database. This is loaded last, as the TEMP database
99564   ** schema may contain references to objects in other databases.
99565   */
99566 #ifndef SQLITE_OMIT_TEMPDB
99567   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
99568                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
99569     rc = sqlite3InitOne(db, 1, pzErrMsg);
99570     if( rc ){
99571       sqlite3ResetOneSchema(db, 1);
99572     }
99573   }
99574 #endif
99575 
99576   db->init.busy = 0;
99577   if( rc==SQLITE_OK && commit_internal ){
99578     sqlite3CommitInternalChanges(db);
99579   }
99580 
99581   return rc;
99582 }
99583 
99584 /*
99585 ** This routine is a no-op if the database schema is already initialized.
99586 ** Otherwise, the schema is loaded. An error code is returned.
99587 */
99588 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
99589   int rc = SQLITE_OK;
99590   sqlite3 *db = pParse->db;
99591   assert( sqlite3_mutex_held(db->mutex) );
99592   if( !db->init.busy ){
99593     rc = sqlite3Init(db, &pParse->zErrMsg);
99594   }
99595   if( rc!=SQLITE_OK ){
99596     pParse->rc = rc;
99597     pParse->nErr++;
99598   }
99599   return rc;
99600 }
99601 
99602 
99603 /*
99604 ** Check schema cookies in all databases.  If any cookie is out
99605 ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
99606 ** make no changes to pParse->rc.
99607 */
99608 static void schemaIsValid(Parse *pParse){
99609   sqlite3 *db = pParse->db;
99610   int iDb;
99611   int rc;
99612   int cookie;
99613 
99614   assert( pParse->checkSchema );
99615   assert( sqlite3_mutex_held(db->mutex) );
99616   for(iDb=0; iDb<db->nDb; iDb++){
99617     int openedTransaction = 0;         /* True if a transaction is opened */
99618     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
99619     if( pBt==0 ) continue;
99620 
99621     /* If there is not already a read-only (or read-write) transaction opened
99622     ** on the b-tree database, open one now. If a transaction is opened, it
99623     ** will be closed immediately after reading the meta-value. */
99624     if( !sqlite3BtreeIsInReadTrans(pBt) ){
99625       rc = sqlite3BtreeBeginTrans(pBt, 0);
99626       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
99627         db->mallocFailed = 1;
99628       }
99629       if( rc!=SQLITE_OK ) return;
99630       openedTransaction = 1;
99631     }
99632 
99633     /* Read the schema cookie from the database. If it does not match the
99634     ** value stored as part of the in-memory schema representation,
99635     ** set Parse.rc to SQLITE_SCHEMA. */
99636     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
99637     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
99638     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
99639       sqlite3ResetOneSchema(db, iDb);
99640       pParse->rc = SQLITE_SCHEMA;
99641     }
99642 
99643     /* Close the transaction, if one was opened. */
99644     if( openedTransaction ){
99645       sqlite3BtreeCommit(pBt);
99646     }
99647   }
99648 }
99649 
99650 /*
99651 ** Convert a schema pointer into the iDb index that indicates
99652 ** which database file in db->aDb[] the schema refers to.
99653 **
99654 ** If the same database is attached more than once, the first
99655 ** attached database is returned.
99656 */
99657 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
99658   int i = -1000000;
99659 
99660   /* If pSchema is NULL, then return -1000000. This happens when code in
99661   ** expr.c is trying to resolve a reference to a transient table (i.e. one
99662   ** created by a sub-select). In this case the return value of this
99663   ** function should never be used.
99664   **
99665   ** We return -1000000 instead of the more usual -1 simply because using
99666   ** -1000000 as the incorrect index into db->aDb[] is much
99667   ** more likely to cause a segfault than -1 (of course there are assert()
99668   ** statements too, but it never hurts to play the odds).
99669   */
99670   assert( sqlite3_mutex_held(db->mutex) );
99671   if( pSchema ){
99672     for(i=0; ALWAYS(i<db->nDb); i++){
99673       if( db->aDb[i].pSchema==pSchema ){
99674         break;
99675       }
99676     }
99677     assert( i>=0 && i<db->nDb );
99678   }
99679   return i;
99680 }
99681 
99682 /*
99683 ** Free all memory allocations in the pParse object
99684 */
99685 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
99686   if( pParse ){
99687     sqlite3 *db = pParse->db;
99688     sqlite3DbFree(db, pParse->aLabel);
99689     sqlite3ExprListDelete(db, pParse->pConstExpr);
99690   }
99691 }
99692 
99693 /*
99694 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
99695 */
99696 static int sqlite3Prepare(
99697   sqlite3 *db,              /* Database handle. */
99698   const char *zSql,         /* UTF-8 encoded SQL statement. */
99699   int nBytes,               /* Length of zSql in bytes. */
99700   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
99701   Vdbe *pReprepare,         /* VM being reprepared */
99702   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99703   const char **pzTail       /* OUT: End of parsed string */
99704 ){
99705   Parse *pParse;            /* Parsing context */
99706   char *zErrMsg = 0;        /* Error message */
99707   int rc = SQLITE_OK;       /* Result code */
99708   int i;                    /* Loop counter */
99709 
99710   /* Allocate the parsing context */
99711   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
99712   if( pParse==0 ){
99713     rc = SQLITE_NOMEM;
99714     goto end_prepare;
99715   }
99716   pParse->pReprepare = pReprepare;
99717   assert( ppStmt && *ppStmt==0 );
99718   assert( !db->mallocFailed );
99719   assert( sqlite3_mutex_held(db->mutex) );
99720 
99721   /* Check to verify that it is possible to get a read lock on all
99722   ** database schemas.  The inability to get a read lock indicates that
99723   ** some other database connection is holding a write-lock, which in
99724   ** turn means that the other connection has made uncommitted changes
99725   ** to the schema.
99726   **
99727   ** Were we to proceed and prepare the statement against the uncommitted
99728   ** schema changes and if those schema changes are subsequently rolled
99729   ** back and different changes are made in their place, then when this
99730   ** prepared statement goes to run the schema cookie would fail to detect
99731   ** the schema change.  Disaster would follow.
99732   **
99733   ** This thread is currently holding mutexes on all Btrees (because
99734   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
99735   ** is not possible for another thread to start a new schema change
99736   ** while this routine is running.  Hence, we do not need to hold
99737   ** locks on the schema, we just need to make sure nobody else is
99738   ** holding them.
99739   **
99740   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
99741   ** but it does *not* override schema lock detection, so this all still
99742   ** works even if READ_UNCOMMITTED is set.
99743   */
99744   for(i=0; i<db->nDb; i++) {
99745     Btree *pBt = db->aDb[i].pBt;
99746     if( pBt ){
99747       assert( sqlite3BtreeHoldsMutex(pBt) );
99748       rc = sqlite3BtreeSchemaLocked(pBt);
99749       if( rc ){
99750         const char *zDb = db->aDb[i].zName;
99751         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
99752         testcase( db->flags & SQLITE_ReadUncommitted );
99753         goto end_prepare;
99754       }
99755     }
99756   }
99757 
99758   sqlite3VtabUnlockList(db);
99759 
99760   pParse->db = db;
99761   pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
99762   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
99763     char *zSqlCopy;
99764     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
99765     testcase( nBytes==mxLen );
99766     testcase( nBytes==mxLen+1 );
99767     if( nBytes>mxLen ){
99768       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
99769       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
99770       goto end_prepare;
99771     }
99772     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
99773     if( zSqlCopy ){
99774       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
99775       sqlite3DbFree(db, zSqlCopy);
99776       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
99777     }else{
99778       pParse->zTail = &zSql[nBytes];
99779     }
99780   }else{
99781     sqlite3RunParser(pParse, zSql, &zErrMsg);
99782   }
99783   assert( 0==pParse->nQueryLoop );
99784 
99785   if( db->mallocFailed ){
99786     pParse->rc = SQLITE_NOMEM;
99787   }
99788   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
99789   if( pParse->checkSchema ){
99790     schemaIsValid(pParse);
99791   }
99792   if( db->mallocFailed ){
99793     pParse->rc = SQLITE_NOMEM;
99794   }
99795   if( pzTail ){
99796     *pzTail = pParse->zTail;
99797   }
99798   rc = pParse->rc;
99799 
99800 #ifndef SQLITE_OMIT_EXPLAIN
99801   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
99802     static const char * const azColName[] = {
99803        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
99804        "selectid", "order", "from", "detail"
99805     };
99806     int iFirst, mx;
99807     if( pParse->explain==2 ){
99808       sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
99809       iFirst = 8;
99810       mx = 12;
99811     }else{
99812       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
99813       iFirst = 0;
99814       mx = 8;
99815     }
99816     for(i=iFirst; i<mx; i++){
99817       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
99818                             azColName[i], SQLITE_STATIC);
99819     }
99820   }
99821 #endif
99822 
99823   if( db->init.busy==0 ){
99824     Vdbe *pVdbe = pParse->pVdbe;
99825     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
99826   }
99827   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
99828     sqlite3VdbeFinalize(pParse->pVdbe);
99829     assert(!(*ppStmt));
99830   }else{
99831     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
99832   }
99833 
99834   if( zErrMsg ){
99835     sqlite3Error(db, rc, "%s", zErrMsg);
99836     sqlite3DbFree(db, zErrMsg);
99837   }else{
99838     sqlite3Error(db, rc, 0);
99839   }
99840 
99841   /* Delete any TriggerPrg structures allocated while parsing this statement. */
99842   while( pParse->pTriggerPrg ){
99843     TriggerPrg *pT = pParse->pTriggerPrg;
99844     pParse->pTriggerPrg = pT->pNext;
99845     sqlite3DbFree(db, pT);
99846   }
99847 
99848 end_prepare:
99849 
99850   sqlite3ParserReset(pParse);
99851   sqlite3StackFree(db, pParse);
99852   rc = sqlite3ApiExit(db, rc);
99853   assert( (rc&db->errMask)==rc );
99854   return rc;
99855 }
99856 static int sqlite3LockAndPrepare(
99857   sqlite3 *db,              /* Database handle. */
99858   const char *zSql,         /* UTF-8 encoded SQL statement. */
99859   int nBytes,               /* Length of zSql in bytes. */
99860   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
99861   Vdbe *pOld,               /* VM being reprepared */
99862   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99863   const char **pzTail       /* OUT: End of parsed string */
99864 ){
99865   int rc;
99866   assert( ppStmt!=0 );
99867   *ppStmt = 0;
99868   if( !sqlite3SafetyCheckOk(db) ){
99869     return SQLITE_MISUSE_BKPT;
99870   }
99871   sqlite3_mutex_enter(db->mutex);
99872   sqlite3BtreeEnterAll(db);
99873   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
99874   if( rc==SQLITE_SCHEMA ){
99875     sqlite3_finalize(*ppStmt);
99876     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
99877   }
99878   sqlite3BtreeLeaveAll(db);
99879   sqlite3_mutex_leave(db->mutex);
99880   assert( rc==SQLITE_OK || *ppStmt==0 );
99881   return rc;
99882 }
99883 
99884 /*
99885 ** Rerun the compilation of a statement after a schema change.
99886 **
99887 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
99888 ** if the statement cannot be recompiled because another connection has
99889 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
99890 ** occurs, return SQLITE_SCHEMA.
99891 */
99892 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
99893   int rc;
99894   sqlite3_stmt *pNew;
99895   const char *zSql;
99896   sqlite3 *db;
99897 
99898   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
99899   zSql = sqlite3_sql((sqlite3_stmt *)p);
99900   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
99901   db = sqlite3VdbeDb(p);
99902   assert( sqlite3_mutex_held(db->mutex) );
99903   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
99904   if( rc ){
99905     if( rc==SQLITE_NOMEM ){
99906       db->mallocFailed = 1;
99907     }
99908     assert( pNew==0 );
99909     return rc;
99910   }else{
99911     assert( pNew!=0 );
99912   }
99913   sqlite3VdbeSwap((Vdbe*)pNew, p);
99914   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
99915   sqlite3VdbeResetStepResult((Vdbe*)pNew);
99916   sqlite3VdbeFinalize((Vdbe*)pNew);
99917   return SQLITE_OK;
99918 }
99919 
99920 
99921 /*
99922 ** Two versions of the official API.  Legacy and new use.  In the legacy
99923 ** version, the original SQL text is not saved in the prepared statement
99924 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
99925 ** sqlite3_step().  In the new version, the original SQL text is retained
99926 ** and the statement is automatically recompiled if an schema change
99927 ** occurs.
99928 */
99929 SQLITE_API int sqlite3_prepare(
99930   sqlite3 *db,              /* Database handle. */
99931   const char *zSql,         /* UTF-8 encoded SQL statement. */
99932   int nBytes,               /* Length of zSql in bytes. */
99933   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99934   const char **pzTail       /* OUT: End of parsed string */
99935 ){
99936   int rc;
99937   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
99938   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
99939   return rc;
99940 }
99941 SQLITE_API int sqlite3_prepare_v2(
99942   sqlite3 *db,              /* Database handle. */
99943   const char *zSql,         /* UTF-8 encoded SQL statement. */
99944   int nBytes,               /* Length of zSql in bytes. */
99945   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99946   const char **pzTail       /* OUT: End of parsed string */
99947 ){
99948   int rc;
99949   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
99950   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
99951   return rc;
99952 }
99953 
99954 
99955 #ifndef SQLITE_OMIT_UTF16
99956 /*
99957 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
99958 */
99959 static int sqlite3Prepare16(
99960   sqlite3 *db,              /* Database handle. */
99961   const void *zSql,         /* UTF-16 encoded SQL statement. */
99962   int nBytes,               /* Length of zSql in bytes. */
99963   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
99964   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99965   const void **pzTail       /* OUT: End of parsed string */
99966 ){
99967   /* This function currently works by first transforming the UTF-16
99968   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
99969   ** tricky bit is figuring out the pointer to return in *pzTail.
99970   */
99971   char *zSql8;
99972   const char *zTail8 = 0;
99973   int rc = SQLITE_OK;
99974 
99975   assert( ppStmt );
99976   *ppStmt = 0;
99977   if( !sqlite3SafetyCheckOk(db) ){
99978     return SQLITE_MISUSE_BKPT;
99979   }
99980   if( nBytes>=0 ){
99981     int sz;
99982     const char *z = (const char*)zSql;
99983     for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
99984     nBytes = sz;
99985   }
99986   sqlite3_mutex_enter(db->mutex);
99987   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
99988   if( zSql8 ){
99989     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
99990   }
99991 
99992   if( zTail8 && pzTail ){
99993     /* If sqlite3_prepare returns a tail pointer, we calculate the
99994     ** equivalent pointer into the UTF-16 string by counting the unicode
99995     ** characters between zSql8 and zTail8, and then returning a pointer
99996     ** the same number of characters into the UTF-16 string.
99997     */
99998     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
99999     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
100000   }
100001   sqlite3DbFree(db, zSql8);
100002   rc = sqlite3ApiExit(db, rc);
100003   sqlite3_mutex_leave(db->mutex);
100004   return rc;
100005 }
100006 
100007 /*
100008 ** Two versions of the official API.  Legacy and new use.  In the legacy
100009 ** version, the original SQL text is not saved in the prepared statement
100010 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
100011 ** sqlite3_step().  In the new version, the original SQL text is retained
100012 ** and the statement is automatically recompiled if an schema change
100013 ** occurs.
100014 */
100015 SQLITE_API int sqlite3_prepare16(
100016   sqlite3 *db,              /* Database handle. */
100017   const void *zSql,         /* UTF-16 encoded SQL statement. */
100018   int nBytes,               /* Length of zSql in bytes. */
100019   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
100020   const void **pzTail       /* OUT: End of parsed string */
100021 ){
100022   int rc;
100023   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
100024   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
100025   return rc;
100026 }
100027 SQLITE_API int sqlite3_prepare16_v2(
100028   sqlite3 *db,              /* Database handle. */
100029   const void *zSql,         /* UTF-16 encoded SQL statement. */
100030   int nBytes,               /* Length of zSql in bytes. */
100031   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
100032   const void **pzTail       /* OUT: End of parsed string */
100033 ){
100034   int rc;
100035   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
100036   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
100037   return rc;
100038 }
100039 
100040 #endif /* SQLITE_OMIT_UTF16 */
100041 
100042 /************** End of prepare.c *********************************************/
100043 /************** Begin file select.c ******************************************/
100044 /*
100045 ** 2001 September 15
100046 **
100047 ** The author disclaims copyright to this source code.  In place of
100048 ** a legal notice, here is a blessing:
100049 **
100050 **    May you do good and not evil.
100051 **    May you find forgiveness for yourself and forgive others.
100052 **    May you share freely, never taking more than you give.
100053 **
100054 *************************************************************************
100055 ** This file contains C code routines that are called by the parser
100056 ** to handle SELECT statements in SQLite.
100057 */
100058 
100059 
100060 /*
100061 ** Delete all the content of a Select structure but do not deallocate
100062 ** the select structure itself.
100063 */
100064 static void clearSelect(sqlite3 *db, Select *p){
100065   sqlite3ExprListDelete(db, p->pEList);
100066   sqlite3SrcListDelete(db, p->pSrc);
100067   sqlite3ExprDelete(db, p->pWhere);
100068   sqlite3ExprListDelete(db, p->pGroupBy);
100069   sqlite3ExprDelete(db, p->pHaving);
100070   sqlite3ExprListDelete(db, p->pOrderBy);
100071   sqlite3SelectDelete(db, p->pPrior);
100072   sqlite3ExprDelete(db, p->pLimit);
100073   sqlite3ExprDelete(db, p->pOffset);
100074   sqlite3WithDelete(db, p->pWith);
100075 }
100076 
100077 /*
100078 ** Initialize a SelectDest structure.
100079 */
100080 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
100081   pDest->eDest = (u8)eDest;
100082   pDest->iSDParm = iParm;
100083   pDest->affSdst = 0;
100084   pDest->iSdst = 0;
100085   pDest->nSdst = 0;
100086 }
100087 
100088 
100089 /*
100090 ** Allocate a new Select structure and return a pointer to that
100091 ** structure.
100092 */
100093 SQLITE_PRIVATE Select *sqlite3SelectNew(
100094   Parse *pParse,        /* Parsing context */
100095   ExprList *pEList,     /* which columns to include in the result */
100096   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
100097   Expr *pWhere,         /* the WHERE clause */
100098   ExprList *pGroupBy,   /* the GROUP BY clause */
100099   Expr *pHaving,        /* the HAVING clause */
100100   ExprList *pOrderBy,   /* the ORDER BY clause */
100101   u16 selFlags,         /* Flag parameters, such as SF_Distinct */
100102   Expr *pLimit,         /* LIMIT value.  NULL means not used */
100103   Expr *pOffset         /* OFFSET value.  NULL means no offset */
100104 ){
100105   Select *pNew;
100106   Select standin;
100107   sqlite3 *db = pParse->db;
100108   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
100109   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
100110   if( pNew==0 ){
100111     assert( db->mallocFailed );
100112     pNew = &standin;
100113     memset(pNew, 0, sizeof(*pNew));
100114   }
100115   if( pEList==0 ){
100116     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
100117   }
100118   pNew->pEList = pEList;
100119   if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
100120   pNew->pSrc = pSrc;
100121   pNew->pWhere = pWhere;
100122   pNew->pGroupBy = pGroupBy;
100123   pNew->pHaving = pHaving;
100124   pNew->pOrderBy = pOrderBy;
100125   pNew->selFlags = selFlags;
100126   pNew->op = TK_SELECT;
100127   pNew->pLimit = pLimit;
100128   pNew->pOffset = pOffset;
100129   assert( pOffset==0 || pLimit!=0 );
100130   pNew->addrOpenEphm[0] = -1;
100131   pNew->addrOpenEphm[1] = -1;
100132   pNew->addrOpenEphm[2] = -1;
100133   if( db->mallocFailed ) {
100134     clearSelect(db, pNew);
100135     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
100136     pNew = 0;
100137   }else{
100138     assert( pNew->pSrc!=0 || pParse->nErr>0 );
100139   }
100140   assert( pNew!=&standin );
100141   return pNew;
100142 }
100143 
100144 /*
100145 ** Delete the given Select structure and all of its substructures.
100146 */
100147 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
100148   if( p ){
100149     clearSelect(db, p);
100150     sqlite3DbFree(db, p);
100151   }
100152 }
100153 
100154 /*
100155 ** Return a pointer to the right-most SELECT statement in a compound.
100156 */
100157 static Select *findRightmost(Select *p){
100158   while( p->pNext ) p = p->pNext;
100159   return p;
100160 }
100161 
100162 /*
100163 ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
100164 ** type of join.  Return an integer constant that expresses that type
100165 ** in terms of the following bit values:
100166 **
100167 **     JT_INNER
100168 **     JT_CROSS
100169 **     JT_OUTER
100170 **     JT_NATURAL
100171 **     JT_LEFT
100172 **     JT_RIGHT
100173 **
100174 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
100175 **
100176 ** If an illegal or unsupported join type is seen, then still return
100177 ** a join type, but put an error in the pParse structure.
100178 */
100179 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
100180   int jointype = 0;
100181   Token *apAll[3];
100182   Token *p;
100183                              /*   0123456789 123456789 123456789 123 */
100184   static const char zKeyText[] = "naturaleftouterightfullinnercross";
100185   static const struct {
100186     u8 i;        /* Beginning of keyword text in zKeyText[] */
100187     u8 nChar;    /* Length of the keyword in characters */
100188     u8 code;     /* Join type mask */
100189   } aKeyword[] = {
100190     /* natural */ { 0,  7, JT_NATURAL                },
100191     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
100192     /* outer   */ { 10, 5, JT_OUTER                  },
100193     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
100194     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
100195     /* inner   */ { 23, 5, JT_INNER                  },
100196     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
100197   };
100198   int i, j;
100199   apAll[0] = pA;
100200   apAll[1] = pB;
100201   apAll[2] = pC;
100202   for(i=0; i<3 && apAll[i]; i++){
100203     p = apAll[i];
100204     for(j=0; j<ArraySize(aKeyword); j++){
100205       if( p->n==aKeyword[j].nChar
100206           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
100207         jointype |= aKeyword[j].code;
100208         break;
100209       }
100210     }
100211     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
100212     if( j>=ArraySize(aKeyword) ){
100213       jointype |= JT_ERROR;
100214       break;
100215     }
100216   }
100217   if(
100218      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
100219      (jointype & JT_ERROR)!=0
100220   ){
100221     const char *zSp = " ";
100222     assert( pB!=0 );
100223     if( pC==0 ){ zSp++; }
100224     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
100225        "%T %T%s%T", pA, pB, zSp, pC);
100226     jointype = JT_INNER;
100227   }else if( (jointype & JT_OUTER)!=0
100228          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
100229     sqlite3ErrorMsg(pParse,
100230       "RIGHT and FULL OUTER JOINs are not currently supported");
100231     jointype = JT_INNER;
100232   }
100233   return jointype;
100234 }
100235 
100236 /*
100237 ** Return the index of a column in a table.  Return -1 if the column
100238 ** is not contained in the table.
100239 */
100240 static int columnIndex(Table *pTab, const char *zCol){
100241   int i;
100242   for(i=0; i<pTab->nCol; i++){
100243     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
100244   }
100245   return -1;
100246 }
100247 
100248 /*
100249 ** Search the first N tables in pSrc, from left to right, looking for a
100250 ** table that has a column named zCol.
100251 **
100252 ** When found, set *piTab and *piCol to the table index and column index
100253 ** of the matching column and return TRUE.
100254 **
100255 ** If not found, return FALSE.
100256 */
100257 static int tableAndColumnIndex(
100258   SrcList *pSrc,       /* Array of tables to search */
100259   int N,               /* Number of tables in pSrc->a[] to search */
100260   const char *zCol,    /* Name of the column we are looking for */
100261   int *piTab,          /* Write index of pSrc->a[] here */
100262   int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
100263 ){
100264   int i;               /* For looping over tables in pSrc */
100265   int iCol;            /* Index of column matching zCol */
100266 
100267   assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
100268   for(i=0; i<N; i++){
100269     iCol = columnIndex(pSrc->a[i].pTab, zCol);
100270     if( iCol>=0 ){
100271       if( piTab ){
100272         *piTab = i;
100273         *piCol = iCol;
100274       }
100275       return 1;
100276     }
100277   }
100278   return 0;
100279 }
100280 
100281 /*
100282 ** This function is used to add terms implied by JOIN syntax to the
100283 ** WHERE clause expression of a SELECT statement. The new term, which
100284 ** is ANDed with the existing WHERE clause, is of the form:
100285 **
100286 **    (tab1.col1 = tab2.col2)
100287 **
100288 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
100289 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
100290 ** column iColRight of tab2.
100291 */
100292 static void addWhereTerm(
100293   Parse *pParse,                  /* Parsing context */
100294   SrcList *pSrc,                  /* List of tables in FROM clause */
100295   int iLeft,                      /* Index of first table to join in pSrc */
100296   int iColLeft,                   /* Index of column in first table */
100297   int iRight,                     /* Index of second table in pSrc */
100298   int iColRight,                  /* Index of column in second table */
100299   int isOuterJoin,                /* True if this is an OUTER join */
100300   Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
100301 ){
100302   sqlite3 *db = pParse->db;
100303   Expr *pE1;
100304   Expr *pE2;
100305   Expr *pEq;
100306 
100307   assert( iLeft<iRight );
100308   assert( pSrc->nSrc>iRight );
100309   assert( pSrc->a[iLeft].pTab );
100310   assert( pSrc->a[iRight].pTab );
100311 
100312   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
100313   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
100314 
100315   pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
100316   if( pEq && isOuterJoin ){
100317     ExprSetProperty(pEq, EP_FromJoin);
100318     assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
100319     ExprSetVVAProperty(pEq, EP_NoReduce);
100320     pEq->iRightJoinTable = (i16)pE2->iTable;
100321   }
100322   *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
100323 }
100324 
100325 /*
100326 ** Set the EP_FromJoin property on all terms of the given expression.
100327 ** And set the Expr.iRightJoinTable to iTable for every term in the
100328 ** expression.
100329 **
100330 ** The EP_FromJoin property is used on terms of an expression to tell
100331 ** the LEFT OUTER JOIN processing logic that this term is part of the
100332 ** join restriction specified in the ON or USING clause and not a part
100333 ** of the more general WHERE clause.  These terms are moved over to the
100334 ** WHERE clause during join processing but we need to remember that they
100335 ** originated in the ON or USING clause.
100336 **
100337 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
100338 ** expression depends on table iRightJoinTable even if that table is not
100339 ** explicitly mentioned in the expression.  That information is needed
100340 ** for cases like this:
100341 **
100342 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
100343 **
100344 ** The where clause needs to defer the handling of the t1.x=5
100345 ** term until after the t2 loop of the join.  In that way, a
100346 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
100347 ** defer the handling of t1.x=5, it will be processed immediately
100348 ** after the t1 loop and rows with t1.x!=5 will never appear in
100349 ** the output, which is incorrect.
100350 */
100351 static void setJoinExpr(Expr *p, int iTable){
100352   while( p ){
100353     ExprSetProperty(p, EP_FromJoin);
100354     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
100355     ExprSetVVAProperty(p, EP_NoReduce);
100356     p->iRightJoinTable = (i16)iTable;
100357     setJoinExpr(p->pLeft, iTable);
100358     p = p->pRight;
100359   }
100360 }
100361 
100362 /*
100363 ** This routine processes the join information for a SELECT statement.
100364 ** ON and USING clauses are converted into extra terms of the WHERE clause.
100365 ** NATURAL joins also create extra WHERE clause terms.
100366 **
100367 ** The terms of a FROM clause are contained in the Select.pSrc structure.
100368 ** The left most table is the first entry in Select.pSrc.  The right-most
100369 ** table is the last entry.  The join operator is held in the entry to
100370 ** the left.  Thus entry 0 contains the join operator for the join between
100371 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
100372 ** also attached to the left entry.
100373 **
100374 ** This routine returns the number of errors encountered.
100375 */
100376 static int sqliteProcessJoin(Parse *pParse, Select *p){
100377   SrcList *pSrc;                  /* All tables in the FROM clause */
100378   int i, j;                       /* Loop counters */
100379   struct SrcList_item *pLeft;     /* Left table being joined */
100380   struct SrcList_item *pRight;    /* Right table being joined */
100381 
100382   pSrc = p->pSrc;
100383   pLeft = &pSrc->a[0];
100384   pRight = &pLeft[1];
100385   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
100386     Table *pLeftTab = pLeft->pTab;
100387     Table *pRightTab = pRight->pTab;
100388     int isOuter;
100389 
100390     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
100391     isOuter = (pRight->jointype & JT_OUTER)!=0;
100392 
100393     /* When the NATURAL keyword is present, add WHERE clause terms for
100394     ** every column that the two tables have in common.
100395     */
100396     if( pRight->jointype & JT_NATURAL ){
100397       if( pRight->pOn || pRight->pUsing ){
100398         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
100399            "an ON or USING clause", 0);
100400         return 1;
100401       }
100402       for(j=0; j<pRightTab->nCol; j++){
100403         char *zName;   /* Name of column in the right table */
100404         int iLeft;     /* Matching left table */
100405         int iLeftCol;  /* Matching column in the left table */
100406 
100407         zName = pRightTab->aCol[j].zName;
100408         if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
100409           addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
100410                        isOuter, &p->pWhere);
100411         }
100412       }
100413     }
100414 
100415     /* Disallow both ON and USING clauses in the same join
100416     */
100417     if( pRight->pOn && pRight->pUsing ){
100418       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
100419         "clauses in the same join");
100420       return 1;
100421     }
100422 
100423     /* Add the ON clause to the end of the WHERE clause, connected by
100424     ** an AND operator.
100425     */
100426     if( pRight->pOn ){
100427       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
100428       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
100429       pRight->pOn = 0;
100430     }
100431 
100432     /* Create extra terms on the WHERE clause for each column named
100433     ** in the USING clause.  Example: If the two tables to be joined are
100434     ** A and B and the USING clause names X, Y, and Z, then add this
100435     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
100436     ** Report an error if any column mentioned in the USING clause is
100437     ** not contained in both tables to be joined.
100438     */
100439     if( pRight->pUsing ){
100440       IdList *pList = pRight->pUsing;
100441       for(j=0; j<pList->nId; j++){
100442         char *zName;     /* Name of the term in the USING clause */
100443         int iLeft;       /* Table on the left with matching column name */
100444         int iLeftCol;    /* Column number of matching column on the left */
100445         int iRightCol;   /* Column number of matching column on the right */
100446 
100447         zName = pList->a[j].zName;
100448         iRightCol = columnIndex(pRightTab, zName);
100449         if( iRightCol<0
100450          || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
100451         ){
100452           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
100453             "not present in both tables", zName);
100454           return 1;
100455         }
100456         addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
100457                      isOuter, &p->pWhere);
100458       }
100459     }
100460   }
100461   return 0;
100462 }
100463 
100464 /*
100465 ** Insert code into "v" that will push the record on the top of the
100466 ** stack into the sorter.
100467 */
100468 static void pushOntoSorter(
100469   Parse *pParse,         /* Parser context */
100470   ExprList *pOrderBy,    /* The ORDER BY clause */
100471   Select *pSelect,       /* The whole SELECT statement */
100472   int regData            /* Register holding data to be sorted */
100473 ){
100474   Vdbe *v = pParse->pVdbe;
100475   int nExpr = pOrderBy->nExpr;
100476   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
100477   int regRecord = sqlite3GetTempReg(pParse);
100478   int op;
100479   sqlite3ExprCacheClear(pParse);
100480   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
100481   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
100482   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
100483   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
100484   if( pSelect->selFlags & SF_UseSorter ){
100485     op = OP_SorterInsert;
100486   }else{
100487     op = OP_IdxInsert;
100488   }
100489   sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
100490   sqlite3ReleaseTempReg(pParse, regRecord);
100491   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
100492   if( pSelect->iLimit ){
100493     int addr1, addr2;
100494     int iLimit;
100495     if( pSelect->iOffset ){
100496       iLimit = pSelect->iOffset+1;
100497     }else{
100498       iLimit = pSelect->iLimit;
100499     }
100500     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); VdbeCoverage(v);
100501     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
100502     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
100503     sqlite3VdbeJumpHere(v, addr1);
100504     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
100505     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
100506     sqlite3VdbeJumpHere(v, addr2);
100507   }
100508 }
100509 
100510 /*
100511 ** Add code to implement the OFFSET
100512 */
100513 static void codeOffset(
100514   Vdbe *v,          /* Generate code into this VM */
100515   int iOffset,      /* Register holding the offset counter */
100516   int iContinue     /* Jump here to skip the current record */
100517 ){
100518   if( iOffset>0 && iContinue!=0 ){
100519     int addr;
100520     sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
100521     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset); VdbeCoverage(v);
100522     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
100523     VdbeComment((v, "skip OFFSET records"));
100524     sqlite3VdbeJumpHere(v, addr);
100525   }
100526 }
100527 
100528 /*
100529 ** Add code that will check to make sure the N registers starting at iMem
100530 ** form a distinct entry.  iTab is a sorting index that holds previously
100531 ** seen combinations of the N values.  A new entry is made in iTab
100532 ** if the current N values are new.
100533 **
100534 ** A jump to addrRepeat is made and the N+1 values are popped from the
100535 ** stack if the top N elements are not distinct.
100536 */
100537 static void codeDistinct(
100538   Parse *pParse,     /* Parsing and code generating context */
100539   int iTab,          /* A sorting index used to test for distinctness */
100540   int addrRepeat,    /* Jump to here if not distinct */
100541   int N,             /* Number of elements */
100542   int iMem           /* First element */
100543 ){
100544   Vdbe *v;
100545   int r1;
100546 
100547   v = pParse->pVdbe;
100548   r1 = sqlite3GetTempReg(pParse);
100549   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); VdbeCoverage(v);
100550   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
100551   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
100552   sqlite3ReleaseTempReg(pParse, r1);
100553 }
100554 
100555 #ifndef SQLITE_OMIT_SUBQUERY
100556 /*
100557 ** Generate an error message when a SELECT is used within a subexpression
100558 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
100559 ** column.  We do this in a subroutine because the error used to occur
100560 ** in multiple places.  (The error only occurs in one place now, but we
100561 ** retain the subroutine to minimize code disruption.)
100562 */
100563 static int checkForMultiColumnSelectError(
100564   Parse *pParse,       /* Parse context. */
100565   SelectDest *pDest,   /* Destination of SELECT results */
100566   int nExpr            /* Number of result columns returned by SELECT */
100567 ){
100568   int eDest = pDest->eDest;
100569   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
100570     sqlite3ErrorMsg(pParse, "only a single result allowed for "
100571        "a SELECT that is part of an expression");
100572     return 1;
100573   }else{
100574     return 0;
100575   }
100576 }
100577 #endif
100578 
100579 /*
100580 ** An instance of the following object is used to record information about
100581 ** how to process the DISTINCT keyword, to simplify passing that information
100582 ** into the selectInnerLoop() routine.
100583 */
100584 typedef struct DistinctCtx DistinctCtx;
100585 struct DistinctCtx {
100586   u8 isTnct;      /* True if the DISTINCT keyword is present */
100587   u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
100588   int tabTnct;    /* Ephemeral table used for DISTINCT processing */
100589   int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
100590 };
100591 
100592 /*
100593 ** This routine generates the code for the inside of the inner loop
100594 ** of a SELECT.
100595 **
100596 ** If srcTab is negative, then the pEList expressions
100597 ** are evaluated in order to get the data for this row.  If srcTab is
100598 ** zero or more, then data is pulled from srcTab and pEList is used only
100599 ** to get number columns and the datatype for each column.
100600 */
100601 static void selectInnerLoop(
100602   Parse *pParse,          /* The parser context */
100603   Select *p,              /* The complete select statement being coded */
100604   ExprList *pEList,       /* List of values being extracted */
100605   int srcTab,             /* Pull data from this table */
100606   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
100607   DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
100608   SelectDest *pDest,      /* How to dispose of the results */
100609   int iContinue,          /* Jump here to continue with next row */
100610   int iBreak              /* Jump here to break out of the inner loop */
100611 ){
100612   Vdbe *v = pParse->pVdbe;
100613   int i;
100614   int hasDistinct;        /* True if the DISTINCT keyword is present */
100615   int regResult;              /* Start of memory holding result set */
100616   int eDest = pDest->eDest;   /* How to dispose of results */
100617   int iParm = pDest->iSDParm; /* First argument to disposal method */
100618   int nResultCol;             /* Number of result columns */
100619 
100620   assert( v );
100621   assert( pEList!=0 );
100622   hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
100623   if( pOrderBy==0 && !hasDistinct ){
100624     codeOffset(v, p->iOffset, iContinue);
100625   }
100626 
100627   /* Pull the requested columns.
100628   */
100629   nResultCol = pEList->nExpr;
100630 
100631   if( pDest->iSdst==0 ){
100632     pDest->iSdst = pParse->nMem+1;
100633     pParse->nMem += nResultCol;
100634   }else if( pDest->iSdst+nResultCol > pParse->nMem ){
100635     /* This is an error condition that can result, for example, when a SELECT
100636     ** on the right-hand side of an INSERT contains more result columns than
100637     ** there are columns in the table on the left.  The error will be caught
100638     ** and reported later.  But we need to make sure enough memory is allocated
100639     ** to avoid other spurious errors in the meantime. */
100640     pParse->nMem += nResultCol;
100641   }
100642   pDest->nSdst = nResultCol;
100643   regResult = pDest->iSdst;
100644   if( srcTab>=0 ){
100645     for(i=0; i<nResultCol; i++){
100646       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
100647       VdbeComment((v, "%s", pEList->a[i].zName));
100648     }
100649   }else if( eDest!=SRT_Exists ){
100650     /* If the destination is an EXISTS(...) expression, the actual
100651     ** values returned by the SELECT are not required.
100652     */
100653     sqlite3ExprCodeExprList(pParse, pEList, regResult,
100654                   (eDest==SRT_Output||eDest==SRT_Coroutine)?SQLITE_ECEL_DUP:0);
100655   }
100656 
100657   /* If the DISTINCT keyword was present on the SELECT statement
100658   ** and this row has been seen before, then do not make this row
100659   ** part of the result.
100660   */
100661   if( hasDistinct ){
100662     switch( pDistinct->eTnctType ){
100663       case WHERE_DISTINCT_ORDERED: {
100664         VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
100665         int iJump;              /* Jump destination */
100666         int regPrev;            /* Previous row content */
100667 
100668         /* Allocate space for the previous row */
100669         regPrev = pParse->nMem+1;
100670         pParse->nMem += nResultCol;
100671 
100672         /* Change the OP_OpenEphemeral coded earlier to an OP_Null
100673         ** sets the MEM_Cleared bit on the first register of the
100674         ** previous value.  This will cause the OP_Ne below to always
100675         ** fail on the first iteration of the loop even if the first
100676         ** row is all NULLs.
100677         */
100678         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
100679         pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
100680         pOp->opcode = OP_Null;
100681         pOp->p1 = 1;
100682         pOp->p2 = regPrev;
100683 
100684         iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
100685         for(i=0; i<nResultCol; i++){
100686           CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
100687           if( i<nResultCol-1 ){
100688             sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
100689             VdbeCoverage(v);
100690           }else{
100691             sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
100692             VdbeCoverage(v);
100693            }
100694           sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
100695           sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
100696         }
100697         assert( sqlite3VdbeCurrentAddr(v)==iJump );
100698         sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
100699         break;
100700       }
100701 
100702       case WHERE_DISTINCT_UNIQUE: {
100703         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
100704         break;
100705       }
100706 
100707       default: {
100708         assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
100709         codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
100710         break;
100711       }
100712     }
100713     if( pOrderBy==0 ){
100714       codeOffset(v, p->iOffset, iContinue);
100715     }
100716   }
100717 
100718   switch( eDest ){
100719     /* In this mode, write each query result to the key of the temporary
100720     ** table iParm.
100721     */
100722 #ifndef SQLITE_OMIT_COMPOUND_SELECT
100723     case SRT_Union: {
100724       int r1;
100725       r1 = sqlite3GetTempReg(pParse);
100726       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100727       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
100728       sqlite3ReleaseTempReg(pParse, r1);
100729       break;
100730     }
100731 
100732     /* Construct a record from the query result, but instead of
100733     ** saving that record, use it as a key to delete elements from
100734     ** the temporary table iParm.
100735     */
100736     case SRT_Except: {
100737       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
100738       break;
100739     }
100740 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
100741 
100742     /* Store the result as data using a unique key.
100743     */
100744     case SRT_DistTable:
100745     case SRT_Table:
100746     case SRT_EphemTab: {
100747       int r1 = sqlite3GetTempReg(pParse);
100748       testcase( eDest==SRT_Table );
100749       testcase( eDest==SRT_EphemTab );
100750       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100751 #ifndef SQLITE_OMIT_CTE
100752       if( eDest==SRT_DistTable ){
100753         /* If the destination is DistTable, then cursor (iParm+1) is open
100754         ** on an ephemeral index. If the current row is already present
100755         ** in the index, do not write it to the output. If not, add the
100756         ** current row to the index and proceed with writing it to the
100757         ** output table as well.  */
100758         int addr = sqlite3VdbeCurrentAddr(v) + 4;
100759         sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0); VdbeCoverage(v);
100760         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
100761         assert( pOrderBy==0 );
100762       }
100763 #endif
100764       if( pOrderBy ){
100765         pushOntoSorter(pParse, pOrderBy, p, r1);
100766       }else{
100767         int r2 = sqlite3GetTempReg(pParse);
100768         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
100769         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
100770         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
100771         sqlite3ReleaseTempReg(pParse, r2);
100772       }
100773       sqlite3ReleaseTempReg(pParse, r1);
100774       break;
100775     }
100776 
100777 #ifndef SQLITE_OMIT_SUBQUERY
100778     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
100779     ** then there should be a single item on the stack.  Write this
100780     ** item into the set table with bogus data.
100781     */
100782     case SRT_Set: {
100783       assert( nResultCol==1 );
100784       pDest->affSdst =
100785                   sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
100786       if( pOrderBy ){
100787         /* At first glance you would think we could optimize out the
100788         ** ORDER BY in this case since the order of entries in the set
100789         ** does not matter.  But there might be a LIMIT clause, in which
100790         ** case the order does matter */
100791         pushOntoSorter(pParse, pOrderBy, p, regResult);
100792       }else{
100793         int r1 = sqlite3GetTempReg(pParse);
100794         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
100795         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
100796         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
100797         sqlite3ReleaseTempReg(pParse, r1);
100798       }
100799       break;
100800     }
100801 
100802     /* If any row exist in the result set, record that fact and abort.
100803     */
100804     case SRT_Exists: {
100805       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
100806       /* The LIMIT clause will terminate the loop for us */
100807       break;
100808     }
100809 
100810     /* If this is a scalar select that is part of an expression, then
100811     ** store the results in the appropriate memory cell and break out
100812     ** of the scan loop.
100813     */
100814     case SRT_Mem: {
100815       assert( nResultCol==1 );
100816       if( pOrderBy ){
100817         pushOntoSorter(pParse, pOrderBy, p, regResult);
100818       }else{
100819         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
100820         /* The LIMIT clause will jump out of the loop for us */
100821       }
100822       break;
100823     }
100824 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
100825 
100826     case SRT_Coroutine:       /* Send data to a co-routine */
100827     case SRT_Output: {        /* Return the results */
100828       testcase( eDest==SRT_Coroutine );
100829       testcase( eDest==SRT_Output );
100830       if( pOrderBy ){
100831         int r1 = sqlite3GetTempReg(pParse);
100832         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100833         pushOntoSorter(pParse, pOrderBy, p, r1);
100834         sqlite3ReleaseTempReg(pParse, r1);
100835       }else if( eDest==SRT_Coroutine ){
100836         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
100837       }else{
100838         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
100839         sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
100840       }
100841       break;
100842     }
100843 
100844 #ifndef SQLITE_OMIT_CTE
100845     /* Write the results into a priority queue that is order according to
100846     ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
100847     ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
100848     ** pSO->nExpr columns, then make sure all keys are unique by adding a
100849     ** final OP_Sequence column.  The last column is the record as a blob.
100850     */
100851     case SRT_DistQueue:
100852     case SRT_Queue: {
100853       int nKey;
100854       int r1, r2, r3;
100855       int addrTest = 0;
100856       ExprList *pSO;
100857       pSO = pDest->pOrderBy;
100858       assert( pSO );
100859       nKey = pSO->nExpr;
100860       r1 = sqlite3GetTempReg(pParse);
100861       r2 = sqlite3GetTempRange(pParse, nKey+2);
100862       r3 = r2+nKey+1;
100863       if( eDest==SRT_DistQueue ){
100864         /* If the destination is DistQueue, then cursor (iParm+1) is open
100865         ** on a second ephemeral index that holds all values every previously
100866         ** added to the queue. */
100867         addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0,
100868                                         regResult, nResultCol);
100869         VdbeCoverage(v);
100870       }
100871       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
100872       if( eDest==SRT_DistQueue ){
100873         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
100874         sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
100875       }
100876       for(i=0; i<nKey; i++){
100877         sqlite3VdbeAddOp2(v, OP_SCopy,
100878                           regResult + pSO->a[i].u.x.iOrderByCol - 1,
100879                           r2+i);
100880       }
100881       sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
100882       sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
100883       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
100884       if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
100885       sqlite3ReleaseTempReg(pParse, r1);
100886       sqlite3ReleaseTempRange(pParse, r2, nKey+2);
100887       break;
100888     }
100889 #endif /* SQLITE_OMIT_CTE */
100890 
100891 
100892 
100893 #if !defined(SQLITE_OMIT_TRIGGER)
100894     /* Discard the results.  This is used for SELECT statements inside
100895     ** the body of a TRIGGER.  The purpose of such selects is to call
100896     ** user-defined functions that have side effects.  We do not care
100897     ** about the actual results of the select.
100898     */
100899     default: {
100900       assert( eDest==SRT_Discard );
100901       break;
100902     }
100903 #endif
100904   }
100905 
100906   /* Jump to the end of the loop if the LIMIT is reached.  Except, if
100907   ** there is a sorter, in which case the sorter has already limited
100908   ** the output for us.
100909   */
100910   if( pOrderBy==0 && p->iLimit ){
100911     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
100912   }
100913 }
100914 
100915 /*
100916 ** Allocate a KeyInfo object sufficient for an index of N key columns and
100917 ** X extra columns.
100918 */
100919 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
100920   KeyInfo *p = sqlite3DbMallocZero(0,
100921                    sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
100922   if( p ){
100923     p->aSortOrder = (u8*)&p->aColl[N+X];
100924     p->nField = (u16)N;
100925     p->nXField = (u16)X;
100926     p->enc = ENC(db);
100927     p->db = db;
100928     p->nRef = 1;
100929   }else{
100930     db->mallocFailed = 1;
100931   }
100932   return p;
100933 }
100934 
100935 /*
100936 ** Deallocate a KeyInfo object
100937 */
100938 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
100939   if( p ){
100940     assert( p->nRef>0 );
100941     p->nRef--;
100942     if( p->nRef==0 ) sqlite3DbFree(0, p);
100943   }
100944 }
100945 
100946 /*
100947 ** Make a new pointer to a KeyInfo object
100948 */
100949 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
100950   if( p ){
100951     assert( p->nRef>0 );
100952     p->nRef++;
100953   }
100954   return p;
100955 }
100956 
100957 #ifdef SQLITE_DEBUG
100958 /*
100959 ** Return TRUE if a KeyInfo object can be change.  The KeyInfo object
100960 ** can only be changed if this is just a single reference to the object.
100961 **
100962 ** This routine is used only inside of assert() statements.
100963 */
100964 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
100965 #endif /* SQLITE_DEBUG */
100966 
100967 /*
100968 ** Given an expression list, generate a KeyInfo structure that records
100969 ** the collating sequence for each expression in that expression list.
100970 **
100971 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
100972 ** KeyInfo structure is appropriate for initializing a virtual index to
100973 ** implement that clause.  If the ExprList is the result set of a SELECT
100974 ** then the KeyInfo structure is appropriate for initializing a virtual
100975 ** index to implement a DISTINCT test.
100976 **
100977 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
100978 ** function is responsible for seeing that this structure is eventually
100979 ** freed.
100980 */
100981 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList, int nExtra){
100982   int nExpr;
100983   KeyInfo *pInfo;
100984   struct ExprList_item *pItem;
100985   sqlite3 *db = pParse->db;
100986   int i;
100987 
100988   nExpr = pList->nExpr;
100989   pInfo = sqlite3KeyInfoAlloc(db, nExpr+nExtra, 1);
100990   if( pInfo ){
100991     assert( sqlite3KeyInfoIsWriteable(pInfo) );
100992     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
100993       CollSeq *pColl;
100994       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
100995       if( !pColl ) pColl = db->pDfltColl;
100996       pInfo->aColl[i] = pColl;
100997       pInfo->aSortOrder[i] = pItem->sortOrder;
100998     }
100999   }
101000   return pInfo;
101001 }
101002 
101003 #ifndef SQLITE_OMIT_COMPOUND_SELECT
101004 /*
101005 ** Name of the connection operator, used for error messages.
101006 */
101007 static const char *selectOpName(int id){
101008   char *z;
101009   switch( id ){
101010     case TK_ALL:       z = "UNION ALL";   break;
101011     case TK_INTERSECT: z = "INTERSECT";   break;
101012     case TK_EXCEPT:    z = "EXCEPT";      break;
101013     default:           z = "UNION";       break;
101014   }
101015   return z;
101016 }
101017 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
101018 
101019 #ifndef SQLITE_OMIT_EXPLAIN
101020 /*
101021 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
101022 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
101023 ** where the caption is of the form:
101024 **
101025 **   "USE TEMP B-TREE FOR xxx"
101026 **
101027 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
101028 ** is determined by the zUsage argument.
101029 */
101030 static void explainTempTable(Parse *pParse, const char *zUsage){
101031   if( pParse->explain==2 ){
101032     Vdbe *v = pParse->pVdbe;
101033     char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
101034     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
101035   }
101036 }
101037 
101038 /*
101039 ** Assign expression b to lvalue a. A second, no-op, version of this macro
101040 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
101041 ** in sqlite3Select() to assign values to structure member variables that
101042 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
101043 ** code with #ifndef directives.
101044 */
101045 # define explainSetInteger(a, b) a = b
101046 
101047 #else
101048 /* No-op versions of the explainXXX() functions and macros. */
101049 # define explainTempTable(y,z)
101050 # define explainSetInteger(y,z)
101051 #endif
101052 
101053 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
101054 /*
101055 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
101056 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
101057 ** where the caption is of one of the two forms:
101058 **
101059 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
101060 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
101061 **
101062 ** where iSub1 and iSub2 are the integers passed as the corresponding
101063 ** function parameters, and op is the text representation of the parameter
101064 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
101065 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
101066 ** false, or the second form if it is true.
101067 */
101068 static void explainComposite(
101069   Parse *pParse,                  /* Parse context */
101070   int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
101071   int iSub1,                      /* Subquery id 1 */
101072   int iSub2,                      /* Subquery id 2 */
101073   int bUseTmp                     /* True if a temp table was used */
101074 ){
101075   assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
101076   if( pParse->explain==2 ){
101077     Vdbe *v = pParse->pVdbe;
101078     char *zMsg = sqlite3MPrintf(
101079         pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
101080         bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
101081     );
101082     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
101083   }
101084 }
101085 #else
101086 /* No-op versions of the explainXXX() functions and macros. */
101087 # define explainComposite(v,w,x,y,z)
101088 #endif
101089 
101090 /*
101091 ** If the inner loop was generated using a non-null pOrderBy argument,
101092 ** then the results were placed in a sorter.  After the loop is terminated
101093 ** we need to run the sorter and output the results.  The following
101094 ** routine generates the code needed to do that.
101095 */
101096 static void generateSortTail(
101097   Parse *pParse,    /* Parsing context */
101098   Select *p,        /* The SELECT statement */
101099   Vdbe *v,          /* Generate code into this VDBE */
101100   int nColumn,      /* Number of columns of data */
101101   SelectDest *pDest /* Write the sorted results here */
101102 ){
101103   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
101104   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
101105   int addr;
101106   int iTab;
101107   int pseudoTab = 0;
101108   ExprList *pOrderBy = p->pOrderBy;
101109 
101110   int eDest = pDest->eDest;
101111   int iParm = pDest->iSDParm;
101112 
101113   int regRow;
101114   int regRowid;
101115 
101116   iTab = pOrderBy->iECursor;
101117   regRow = sqlite3GetTempReg(pParse);
101118   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
101119     pseudoTab = pParse->nTab++;
101120     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
101121     regRowid = 0;
101122   }else{
101123     regRowid = sqlite3GetTempReg(pParse);
101124   }
101125   if( p->selFlags & SF_UseSorter ){
101126     int regSortOut = ++pParse->nMem;
101127     int ptab2 = pParse->nTab++;
101128     sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
101129     addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
101130     VdbeCoverage(v);
101131     codeOffset(v, p->iOffset, addrContinue);
101132     sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
101133     sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
101134     sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
101135   }else{
101136     addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); VdbeCoverage(v);
101137     codeOffset(v, p->iOffset, addrContinue);
101138     sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
101139   }
101140   switch( eDest ){
101141     case SRT_Table:
101142     case SRT_EphemTab: {
101143       testcase( eDest==SRT_Table );
101144       testcase( eDest==SRT_EphemTab );
101145       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
101146       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
101147       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
101148       break;
101149     }
101150 #ifndef SQLITE_OMIT_SUBQUERY
101151     case SRT_Set: {
101152       assert( nColumn==1 );
101153       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
101154                         &pDest->affSdst, 1);
101155       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
101156       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
101157       break;
101158     }
101159     case SRT_Mem: {
101160       assert( nColumn==1 );
101161       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
101162       /* The LIMIT clause will terminate the loop for us */
101163       break;
101164     }
101165 #endif
101166     default: {
101167       int i;
101168       assert( eDest==SRT_Output || eDest==SRT_Coroutine );
101169       testcase( eDest==SRT_Output );
101170       testcase( eDest==SRT_Coroutine );
101171       for(i=0; i<nColumn; i++){
101172         assert( regRow!=pDest->iSdst+i );
101173         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
101174         if( i==0 ){
101175           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
101176         }
101177       }
101178       if( eDest==SRT_Output ){
101179         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
101180         sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
101181       }else{
101182         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
101183       }
101184       break;
101185     }
101186   }
101187   sqlite3ReleaseTempReg(pParse, regRow);
101188   sqlite3ReleaseTempReg(pParse, regRowid);
101189 
101190   /* The bottom of the loop
101191   */
101192   sqlite3VdbeResolveLabel(v, addrContinue);
101193   if( p->selFlags & SF_UseSorter ){
101194     sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr); VdbeCoverage(v);
101195   }else{
101196     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); VdbeCoverage(v);
101197   }
101198   sqlite3VdbeResolveLabel(v, addrBreak);
101199   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
101200     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
101201   }
101202 }
101203 
101204 /*
101205 ** Return a pointer to a string containing the 'declaration type' of the
101206 ** expression pExpr. The string may be treated as static by the caller.
101207 **
101208 ** Also try to estimate the size of the returned value and return that
101209 ** result in *pEstWidth.
101210 **
101211 ** The declaration type is the exact datatype definition extracted from the
101212 ** original CREATE TABLE statement if the expression is a column. The
101213 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
101214 ** is considered a column can be complex in the presence of subqueries. The
101215 ** result-set expression in all of the following SELECT statements is
101216 ** considered a column by this function.
101217 **
101218 **   SELECT col FROM tbl;
101219 **   SELECT (SELECT col FROM tbl;
101220 **   SELECT (SELECT col FROM tbl);
101221 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
101222 **
101223 ** The declaration type for any expression other than a column is NULL.
101224 **
101225 ** This routine has either 3 or 6 parameters depending on whether or not
101226 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
101227 */
101228 #ifdef SQLITE_ENABLE_COLUMN_METADATA
101229 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
101230 static const char *columnTypeImpl(
101231   NameContext *pNC,
101232   Expr *pExpr,
101233   const char **pzOrigDb,
101234   const char **pzOrigTab,
101235   const char **pzOrigCol,
101236   u8 *pEstWidth
101237 ){
101238   char const *zOrigDb = 0;
101239   char const *zOrigTab = 0;
101240   char const *zOrigCol = 0;
101241 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
101242 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
101243 static const char *columnTypeImpl(
101244   NameContext *pNC,
101245   Expr *pExpr,
101246   u8 *pEstWidth
101247 ){
101248 #endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
101249   char const *zType = 0;
101250   int j;
101251   u8 estWidth = 1;
101252 
101253   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
101254   switch( pExpr->op ){
101255     case TK_AGG_COLUMN:
101256     case TK_COLUMN: {
101257       /* The expression is a column. Locate the table the column is being
101258       ** extracted from in NameContext.pSrcList. This table may be real
101259       ** database table or a subquery.
101260       */
101261       Table *pTab = 0;            /* Table structure column is extracted from */
101262       Select *pS = 0;             /* Select the column is extracted from */
101263       int iCol = pExpr->iColumn;  /* Index of column in pTab */
101264       testcase( pExpr->op==TK_AGG_COLUMN );
101265       testcase( pExpr->op==TK_COLUMN );
101266       while( pNC && !pTab ){
101267         SrcList *pTabList = pNC->pSrcList;
101268         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
101269         if( j<pTabList->nSrc ){
101270           pTab = pTabList->a[j].pTab;
101271           pS = pTabList->a[j].pSelect;
101272         }else{
101273           pNC = pNC->pNext;
101274         }
101275       }
101276 
101277       if( pTab==0 ){
101278         /* At one time, code such as "SELECT new.x" within a trigger would
101279         ** cause this condition to run.  Since then, we have restructured how
101280         ** trigger code is generated and so this condition is no longer
101281         ** possible. However, it can still be true for statements like
101282         ** the following:
101283         **
101284         **   CREATE TABLE t1(col INTEGER);
101285         **   SELECT (SELECT t1.col) FROM FROM t1;
101286         **
101287         ** when columnType() is called on the expression "t1.col" in the
101288         ** sub-select. In this case, set the column type to NULL, even
101289         ** though it should really be "INTEGER".
101290         **
101291         ** This is not a problem, as the column type of "t1.col" is never
101292         ** used. When columnType() is called on the expression
101293         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
101294         ** branch below.  */
101295         break;
101296       }
101297 
101298       assert( pTab && pExpr->pTab==pTab );
101299       if( pS ){
101300         /* The "table" is actually a sub-select or a view in the FROM clause
101301         ** of the SELECT statement. Return the declaration type and origin
101302         ** data for the result-set column of the sub-select.
101303         */
101304         if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
101305           /* If iCol is less than zero, then the expression requests the
101306           ** rowid of the sub-select or view. This expression is legal (see
101307           ** test case misc2.2.2) - it always evaluates to NULL.
101308           */
101309           NameContext sNC;
101310           Expr *p = pS->pEList->a[iCol].pExpr;
101311           sNC.pSrcList = pS->pSrc;
101312           sNC.pNext = pNC;
101313           sNC.pParse = pNC->pParse;
101314           zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth);
101315         }
101316       }else if( pTab->pSchema ){
101317         /* A real table */
101318         assert( !pS );
101319         if( iCol<0 ) iCol = pTab->iPKey;
101320         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
101321 #ifdef SQLITE_ENABLE_COLUMN_METADATA
101322         if( iCol<0 ){
101323           zType = "INTEGER";
101324           zOrigCol = "rowid";
101325         }else{
101326           zType = pTab->aCol[iCol].zType;
101327           zOrigCol = pTab->aCol[iCol].zName;
101328           estWidth = pTab->aCol[iCol].szEst;
101329         }
101330         zOrigTab = pTab->zName;
101331         if( pNC->pParse ){
101332           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
101333           zOrigDb = pNC->pParse->db->aDb[iDb].zName;
101334         }
101335 #else
101336         if( iCol<0 ){
101337           zType = "INTEGER";
101338         }else{
101339           zType = pTab->aCol[iCol].zType;
101340           estWidth = pTab->aCol[iCol].szEst;
101341         }
101342 #endif
101343       }
101344       break;
101345     }
101346 #ifndef SQLITE_OMIT_SUBQUERY
101347     case TK_SELECT: {
101348       /* The expression is a sub-select. Return the declaration type and
101349       ** origin info for the single column in the result set of the SELECT
101350       ** statement.
101351       */
101352       NameContext sNC;
101353       Select *pS = pExpr->x.pSelect;
101354       Expr *p = pS->pEList->a[0].pExpr;
101355       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
101356       sNC.pSrcList = pS->pSrc;
101357       sNC.pNext = pNC;
101358       sNC.pParse = pNC->pParse;
101359       zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth);
101360       break;
101361     }
101362 #endif
101363   }
101364 
101365 #ifdef SQLITE_ENABLE_COLUMN_METADATA
101366   if( pzOrigDb ){
101367     assert( pzOrigTab && pzOrigCol );
101368     *pzOrigDb = zOrigDb;
101369     *pzOrigTab = zOrigTab;
101370     *pzOrigCol = zOrigCol;
101371   }
101372 #endif
101373   if( pEstWidth ) *pEstWidth = estWidth;
101374   return zType;
101375 }
101376 
101377 /*
101378 ** Generate code that will tell the VDBE the declaration types of columns
101379 ** in the result set.
101380 */
101381 static void generateColumnTypes(
101382   Parse *pParse,      /* Parser context */
101383   SrcList *pTabList,  /* List of tables */
101384   ExprList *pEList    /* Expressions defining the result set */
101385 ){
101386 #ifndef SQLITE_OMIT_DECLTYPE
101387   Vdbe *v = pParse->pVdbe;
101388   int i;
101389   NameContext sNC;
101390   sNC.pSrcList = pTabList;
101391   sNC.pParse = pParse;
101392   for(i=0; i<pEList->nExpr; i++){
101393     Expr *p = pEList->a[i].pExpr;
101394     const char *zType;
101395 #ifdef SQLITE_ENABLE_COLUMN_METADATA
101396     const char *zOrigDb = 0;
101397     const char *zOrigTab = 0;
101398     const char *zOrigCol = 0;
101399     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
101400 
101401     /* The vdbe must make its own copy of the column-type and other
101402     ** column specific strings, in case the schema is reset before this
101403     ** virtual machine is deleted.
101404     */
101405     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
101406     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
101407     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
101408 #else
101409     zType = columnType(&sNC, p, 0, 0, 0, 0);
101410 #endif
101411     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
101412   }
101413 #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
101414 }
101415 
101416 /*
101417 ** Generate code that will tell the VDBE the names of columns
101418 ** in the result set.  This information is used to provide the
101419 ** azCol[] values in the callback.
101420 */
101421 static void generateColumnNames(
101422   Parse *pParse,      /* Parser context */
101423   SrcList *pTabList,  /* List of tables */
101424   ExprList *pEList    /* Expressions defining the result set */
101425 ){
101426   Vdbe *v = pParse->pVdbe;
101427   int i, j;
101428   sqlite3 *db = pParse->db;
101429   int fullNames, shortNames;
101430 
101431 #ifndef SQLITE_OMIT_EXPLAIN
101432   /* If this is an EXPLAIN, skip this step */
101433   if( pParse->explain ){
101434     return;
101435   }
101436 #endif
101437 
101438   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
101439   pParse->colNamesSet = 1;
101440   fullNames = (db->flags & SQLITE_FullColNames)!=0;
101441   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
101442   sqlite3VdbeSetNumCols(v, pEList->nExpr);
101443   for(i=0; i<pEList->nExpr; i++){
101444     Expr *p;
101445     p = pEList->a[i].pExpr;
101446     if( NEVER(p==0) ) continue;
101447     if( pEList->a[i].zName ){
101448       char *zName = pEList->a[i].zName;
101449       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
101450     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
101451       Table *pTab;
101452       char *zCol;
101453       int iCol = p->iColumn;
101454       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
101455         if( pTabList->a[j].iCursor==p->iTable ) break;
101456       }
101457       assert( j<pTabList->nSrc );
101458       pTab = pTabList->a[j].pTab;
101459       if( iCol<0 ) iCol = pTab->iPKey;
101460       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
101461       if( iCol<0 ){
101462         zCol = "rowid";
101463       }else{
101464         zCol = pTab->aCol[iCol].zName;
101465       }
101466       if( !shortNames && !fullNames ){
101467         sqlite3VdbeSetColName(v, i, COLNAME_NAME,
101468             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
101469       }else if( fullNames ){
101470         char *zName = 0;
101471         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
101472         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
101473       }else{
101474         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
101475       }
101476     }else{
101477       const char *z = pEList->a[i].zSpan;
101478       z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
101479       sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
101480     }
101481   }
101482   generateColumnTypes(pParse, pTabList, pEList);
101483 }
101484 
101485 /*
101486 ** Given a an expression list (which is really the list of expressions
101487 ** that form the result set of a SELECT statement) compute appropriate
101488 ** column names for a table that would hold the expression list.
101489 **
101490 ** All column names will be unique.
101491 **
101492 ** Only the column names are computed.  Column.zType, Column.zColl,
101493 ** and other fields of Column are zeroed.
101494 **
101495 ** Return SQLITE_OK on success.  If a memory allocation error occurs,
101496 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
101497 */
101498 static int selectColumnsFromExprList(
101499   Parse *pParse,          /* Parsing context */
101500   ExprList *pEList,       /* Expr list from which to derive column names */
101501   i16 *pnCol,             /* Write the number of columns here */
101502   Column **paCol          /* Write the new column list here */
101503 ){
101504   sqlite3 *db = pParse->db;   /* Database connection */
101505   int i, j;                   /* Loop counters */
101506   int cnt;                    /* Index added to make the name unique */
101507   Column *aCol, *pCol;        /* For looping over result columns */
101508   int nCol;                   /* Number of columns in the result set */
101509   Expr *p;                    /* Expression for a single result column */
101510   char *zName;                /* Column name */
101511   int nName;                  /* Size of name in zName[] */
101512 
101513   if( pEList ){
101514     nCol = pEList->nExpr;
101515     aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
101516     testcase( aCol==0 );
101517   }else{
101518     nCol = 0;
101519     aCol = 0;
101520   }
101521   *pnCol = nCol;
101522   *paCol = aCol;
101523 
101524   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
101525     /* Get an appropriate name for the column
101526     */
101527     p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
101528     if( (zName = pEList->a[i].zName)!=0 ){
101529       /* If the column contains an "AS <name>" phrase, use <name> as the name */
101530       zName = sqlite3DbStrDup(db, zName);
101531     }else{
101532       Expr *pColExpr = p;  /* The expression that is the result column name */
101533       Table *pTab;         /* Table associated with this expression */
101534       while( pColExpr->op==TK_DOT ){
101535         pColExpr = pColExpr->pRight;
101536         assert( pColExpr!=0 );
101537       }
101538       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
101539         /* For columns use the column name name */
101540         int iCol = pColExpr->iColumn;
101541         pTab = pColExpr->pTab;
101542         if( iCol<0 ) iCol = pTab->iPKey;
101543         zName = sqlite3MPrintf(db, "%s",
101544                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
101545       }else if( pColExpr->op==TK_ID ){
101546         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
101547         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
101548       }else{
101549         /* Use the original text of the column expression as its name */
101550         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
101551       }
101552     }
101553     if( db->mallocFailed ){
101554       sqlite3DbFree(db, zName);
101555       break;
101556     }
101557 
101558     /* Make sure the column name is unique.  If the name is not unique,
101559     ** append a integer to the name so that it becomes unique.
101560     */
101561     nName = sqlite3Strlen30(zName);
101562     for(j=cnt=0; j<i; j++){
101563       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
101564         char *zNewName;
101565         int k;
101566         for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
101567         if( k>=0 && zName[k]==':' ) nName = k;
101568         zName[nName] = 0;
101569         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
101570         sqlite3DbFree(db, zName);
101571         zName = zNewName;
101572         j = -1;
101573         if( zName==0 ) break;
101574       }
101575     }
101576     pCol->zName = zName;
101577   }
101578   if( db->mallocFailed ){
101579     for(j=0; j<i; j++){
101580       sqlite3DbFree(db, aCol[j].zName);
101581     }
101582     sqlite3DbFree(db, aCol);
101583     *paCol = 0;
101584     *pnCol = 0;
101585     return SQLITE_NOMEM;
101586   }
101587   return SQLITE_OK;
101588 }
101589 
101590 /*
101591 ** Add type and collation information to a column list based on
101592 ** a SELECT statement.
101593 **
101594 ** The column list presumably came from selectColumnNamesFromExprList().
101595 ** The column list has only names, not types or collations.  This
101596 ** routine goes through and adds the types and collations.
101597 **
101598 ** This routine requires that all identifiers in the SELECT
101599 ** statement be resolved.
101600 */
101601 static void selectAddColumnTypeAndCollation(
101602   Parse *pParse,        /* Parsing contexts */
101603   Table *pTab,          /* Add column type information to this table */
101604   Select *pSelect       /* SELECT used to determine types and collations */
101605 ){
101606   sqlite3 *db = pParse->db;
101607   NameContext sNC;
101608   Column *pCol;
101609   CollSeq *pColl;
101610   int i;
101611   Expr *p;
101612   struct ExprList_item *a;
101613   u64 szAll = 0;
101614 
101615   assert( pSelect!=0 );
101616   assert( (pSelect->selFlags & SF_Resolved)!=0 );
101617   assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
101618   if( db->mallocFailed ) return;
101619   memset(&sNC, 0, sizeof(sNC));
101620   sNC.pSrcList = pSelect->pSrc;
101621   a = pSelect->pEList->a;
101622   for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
101623     p = a[i].pExpr;
101624     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
101625     szAll += pCol->szEst;
101626     pCol->affinity = sqlite3ExprAffinity(p);
101627     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
101628     pColl = sqlite3ExprCollSeq(pParse, p);
101629     if( pColl ){
101630       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
101631     }
101632   }
101633   pTab->szTabRow = sqlite3LogEst(szAll*4);
101634 }
101635 
101636 /*
101637 ** Given a SELECT statement, generate a Table structure that describes
101638 ** the result set of that SELECT.
101639 */
101640 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
101641   Table *pTab;
101642   sqlite3 *db = pParse->db;
101643   int savedFlags;
101644 
101645   savedFlags = db->flags;
101646   db->flags &= ~SQLITE_FullColNames;
101647   db->flags |= SQLITE_ShortColNames;
101648   sqlite3SelectPrep(pParse, pSelect, 0);
101649   if( pParse->nErr ) return 0;
101650   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
101651   db->flags = savedFlags;
101652   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
101653   if( pTab==0 ){
101654     return 0;
101655   }
101656   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
101657   ** is disabled */
101658   assert( db->lookaside.bEnabled==0 );
101659   pTab->nRef = 1;
101660   pTab->zName = 0;
101661   pTab->nRowEst = 1048576;
101662   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
101663   selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
101664   pTab->iPKey = -1;
101665   if( db->mallocFailed ){
101666     sqlite3DeleteTable(db, pTab);
101667     return 0;
101668   }
101669   return pTab;
101670 }
101671 
101672 /*
101673 ** Get a VDBE for the given parser context.  Create a new one if necessary.
101674 ** If an error occurs, return NULL and leave a message in pParse.
101675 */
101676 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
101677   Vdbe *v = pParse->pVdbe;
101678   if( v==0 ){
101679     v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
101680     if( v ) sqlite3VdbeAddOp0(v, OP_Init);
101681     if( pParse->pToplevel==0
101682      && OptimizationEnabled(pParse->db,SQLITE_FactorOutConst)
101683     ){
101684       pParse->okConstFactor = 1;
101685     }
101686 
101687   }
101688   return v;
101689 }
101690 
101691 
101692 /*
101693 ** Compute the iLimit and iOffset fields of the SELECT based on the
101694 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
101695 ** that appear in the original SQL statement after the LIMIT and OFFSET
101696 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
101697 ** are the integer memory register numbers for counters used to compute
101698 ** the limit and offset.  If there is no limit and/or offset, then
101699 ** iLimit and iOffset are negative.
101700 **
101701 ** This routine changes the values of iLimit and iOffset only if
101702 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
101703 ** iOffset should have been preset to appropriate default values (zero)
101704 ** prior to calling this routine.
101705 **
101706 ** The iOffset register (if it exists) is initialized to the value
101707 ** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
101708 ** iOffset+1 is initialized to LIMIT+OFFSET.
101709 **
101710 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
101711 ** redefined.  The UNION ALL operator uses this property to force
101712 ** the reuse of the same limit and offset registers across multiple
101713 ** SELECT statements.
101714 */
101715 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
101716   Vdbe *v = 0;
101717   int iLimit = 0;
101718   int iOffset;
101719   int addr1, n;
101720   if( p->iLimit ) return;
101721 
101722   /*
101723   ** "LIMIT -1" always shows all rows.  There is some
101724   ** controversy about what the correct behavior should be.
101725   ** The current implementation interprets "LIMIT 0" to mean
101726   ** no rows.
101727   */
101728   sqlite3ExprCacheClear(pParse);
101729   assert( p->pOffset==0 || p->pLimit!=0 );
101730   if( p->pLimit ){
101731     p->iLimit = iLimit = ++pParse->nMem;
101732     v = sqlite3GetVdbe(pParse);
101733     assert( v!=0 );
101734     if( sqlite3ExprIsInteger(p->pLimit, &n) ){
101735       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
101736       VdbeComment((v, "LIMIT counter"));
101737       if( n==0 ){
101738         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
101739       }else if( n>=0 && p->nSelectRow>(u64)n ){
101740         p->nSelectRow = n;
101741       }
101742     }else{
101743       sqlite3ExprCode(pParse, p->pLimit, iLimit);
101744       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); VdbeCoverage(v);
101745       VdbeComment((v, "LIMIT counter"));
101746       sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak); VdbeCoverage(v);
101747     }
101748     if( p->pOffset ){
101749       p->iOffset = iOffset = ++pParse->nMem;
101750       pParse->nMem++;   /* Allocate an extra register for limit+offset */
101751       sqlite3ExprCode(pParse, p->pOffset, iOffset);
101752       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
101753       VdbeComment((v, "OFFSET counter"));
101754       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); VdbeCoverage(v);
101755       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
101756       sqlite3VdbeJumpHere(v, addr1);
101757       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
101758       VdbeComment((v, "LIMIT+OFFSET"));
101759       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); VdbeCoverage(v);
101760       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
101761       sqlite3VdbeJumpHere(v, addr1);
101762     }
101763   }
101764 }
101765 
101766 #ifndef SQLITE_OMIT_COMPOUND_SELECT
101767 /*
101768 ** Return the appropriate collating sequence for the iCol-th column of
101769 ** the result set for the compound-select statement "p".  Return NULL if
101770 ** the column has no default collating sequence.
101771 **
101772 ** The collating sequence for the compound select is taken from the
101773 ** left-most term of the select that has a collating sequence.
101774 */
101775 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
101776   CollSeq *pRet;
101777   if( p->pPrior ){
101778     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
101779   }else{
101780     pRet = 0;
101781   }
101782   assert( iCol>=0 );
101783   if( pRet==0 && iCol<p->pEList->nExpr ){
101784     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
101785   }
101786   return pRet;
101787 }
101788 
101789 /*
101790 ** The select statement passed as the second parameter is a compound SELECT
101791 ** with an ORDER BY clause. This function allocates and returns a KeyInfo
101792 ** structure suitable for implementing the ORDER BY.
101793 **
101794 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
101795 ** function is responsible for ensuring that this structure is eventually
101796 ** freed.
101797 */
101798 static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
101799   ExprList *pOrderBy = p->pOrderBy;
101800   int nOrderBy = p->pOrderBy->nExpr;
101801   sqlite3 *db = pParse->db;
101802   KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
101803   if( pRet ){
101804     int i;
101805     for(i=0; i<nOrderBy; i++){
101806       struct ExprList_item *pItem = &pOrderBy->a[i];
101807       Expr *pTerm = pItem->pExpr;
101808       CollSeq *pColl;
101809 
101810       if( pTerm->flags & EP_Collate ){
101811         pColl = sqlite3ExprCollSeq(pParse, pTerm);
101812       }else{
101813         pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
101814         if( pColl==0 ) pColl = db->pDfltColl;
101815         pOrderBy->a[i].pExpr =
101816           sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
101817       }
101818       assert( sqlite3KeyInfoIsWriteable(pRet) );
101819       pRet->aColl[i] = pColl;
101820       pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
101821     }
101822   }
101823 
101824   return pRet;
101825 }
101826 
101827 #ifndef SQLITE_OMIT_CTE
101828 /*
101829 ** This routine generates VDBE code to compute the content of a WITH RECURSIVE
101830 ** query of the form:
101831 **
101832 **   <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
101833 **                         \___________/             \_______________/
101834 **                           p->pPrior                      p
101835 **
101836 **
101837 ** There is exactly one reference to the recursive-table in the FROM clause
101838 ** of recursive-query, marked with the SrcList->a[].isRecursive flag.
101839 **
101840 ** The setup-query runs once to generate an initial set of rows that go
101841 ** into a Queue table.  Rows are extracted from the Queue table one by
101842 ** one.  Each row extracted from Queue is output to pDest.  Then the single
101843 ** extracted row (now in the iCurrent table) becomes the content of the
101844 ** recursive-table for a recursive-query run.  The output of the recursive-query
101845 ** is added back into the Queue table.  Then another row is extracted from Queue
101846 ** and the iteration continues until the Queue table is empty.
101847 **
101848 ** If the compound query operator is UNION then no duplicate rows are ever
101849 ** inserted into the Queue table.  The iDistinct table keeps a copy of all rows
101850 ** that have ever been inserted into Queue and causes duplicates to be
101851 ** discarded.  If the operator is UNION ALL, then duplicates are allowed.
101852 **
101853 ** If the query has an ORDER BY, then entries in the Queue table are kept in
101854 ** ORDER BY order and the first entry is extracted for each cycle.  Without
101855 ** an ORDER BY, the Queue table is just a FIFO.
101856 **
101857 ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
101858 ** have been output to pDest.  A LIMIT of zero means to output no rows and a
101859 ** negative LIMIT means to output all rows.  If there is also an OFFSET clause
101860 ** with a positive value, then the first OFFSET outputs are discarded rather
101861 ** than being sent to pDest.  The LIMIT count does not begin until after OFFSET
101862 ** rows have been skipped.
101863 */
101864 static void generateWithRecursiveQuery(
101865   Parse *pParse,        /* Parsing context */
101866   Select *p,            /* The recursive SELECT to be coded */
101867   SelectDest *pDest     /* What to do with query results */
101868 ){
101869   SrcList *pSrc = p->pSrc;      /* The FROM clause of the recursive query */
101870   int nCol = p->pEList->nExpr;  /* Number of columns in the recursive table */
101871   Vdbe *v = pParse->pVdbe;      /* The prepared statement under construction */
101872   Select *pSetup = p->pPrior;   /* The setup query */
101873   int addrTop;                  /* Top of the loop */
101874   int addrCont, addrBreak;      /* CONTINUE and BREAK addresses */
101875   int iCurrent = 0;             /* The Current table */
101876   int regCurrent;               /* Register holding Current table */
101877   int iQueue;                   /* The Queue table */
101878   int iDistinct = 0;            /* To ensure unique results if UNION */
101879   int eDest = SRT_Table;        /* How to write to Queue */
101880   SelectDest destQueue;         /* SelectDest targetting the Queue table */
101881   int i;                        /* Loop counter */
101882   int rc;                       /* Result code */
101883   ExprList *pOrderBy;           /* The ORDER BY clause */
101884   Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
101885   int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
101886 
101887   /* Obtain authorization to do a recursive query */
101888   if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
101889 
101890   /* Process the LIMIT and OFFSET clauses, if they exist */
101891   addrBreak = sqlite3VdbeMakeLabel(v);
101892   computeLimitRegisters(pParse, p, addrBreak);
101893   pLimit = p->pLimit;
101894   pOffset = p->pOffset;
101895   regLimit = p->iLimit;
101896   regOffset = p->iOffset;
101897   p->pLimit = p->pOffset = 0;
101898   p->iLimit = p->iOffset = 0;
101899   pOrderBy = p->pOrderBy;
101900 
101901   /* Locate the cursor number of the Current table */
101902   for(i=0; ALWAYS(i<pSrc->nSrc); i++){
101903     if( pSrc->a[i].isRecursive ){
101904       iCurrent = pSrc->a[i].iCursor;
101905       break;
101906     }
101907   }
101908 
101909   /* Allocate cursors numbers for Queue and Distinct.  The cursor number for
101910   ** the Distinct table must be exactly one greater than Queue in order
101911   ** for the SRT_DistTable and SRT_DistQueue destinations to work. */
101912   iQueue = pParse->nTab++;
101913   if( p->op==TK_UNION ){
101914     eDest = pOrderBy ? SRT_DistQueue : SRT_DistTable;
101915     iDistinct = pParse->nTab++;
101916   }else{
101917     eDest = pOrderBy ? SRT_Queue : SRT_Table;
101918   }
101919   sqlite3SelectDestInit(&destQueue, eDest, iQueue);
101920 
101921   /* Allocate cursors for Current, Queue, and Distinct. */
101922   regCurrent = ++pParse->nMem;
101923   sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
101924   if( pOrderBy ){
101925     KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
101926     sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
101927                       (char*)pKeyInfo, P4_KEYINFO);
101928     destQueue.pOrderBy = pOrderBy;
101929   }else{
101930     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
101931   }
101932   VdbeComment((v, "Queue table"));
101933   if( iDistinct ){
101934     p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
101935     p->selFlags |= SF_UsesEphemeral;
101936   }
101937 
101938   /* Detach the ORDER BY clause from the compound SELECT */
101939   p->pOrderBy = 0;
101940 
101941   /* Store the results of the setup-query in Queue. */
101942   pSetup->pNext = 0;
101943   rc = sqlite3Select(pParse, pSetup, &destQueue);
101944   pSetup->pNext = p;
101945   if( rc ) goto end_of_recursive_query;
101946 
101947   /* Find the next row in the Queue and output that row */
101948   addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak); VdbeCoverage(v);
101949 
101950   /* Transfer the next row in Queue over to Current */
101951   sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
101952   if( pOrderBy ){
101953     sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
101954   }else{
101955     sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
101956   }
101957   sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
101958 
101959   /* Output the single row in Current */
101960   addrCont = sqlite3VdbeMakeLabel(v);
101961   codeOffset(v, regOffset, addrCont);
101962   selectInnerLoop(pParse, p, p->pEList, iCurrent,
101963       0, 0, pDest, addrCont, addrBreak);
101964   if( regLimit ){
101965     sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
101966     VdbeCoverage(v);
101967   }
101968   sqlite3VdbeResolveLabel(v, addrCont);
101969 
101970   /* Execute the recursive SELECT taking the single row in Current as
101971   ** the value for the recursive-table. Store the results in the Queue.
101972   */
101973   p->pPrior = 0;
101974   sqlite3Select(pParse, p, &destQueue);
101975   assert( p->pPrior==0 );
101976   p->pPrior = pSetup;
101977 
101978   /* Keep running the loop until the Queue is empty */
101979   sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
101980   sqlite3VdbeResolveLabel(v, addrBreak);
101981 
101982 end_of_recursive_query:
101983   p->pOrderBy = pOrderBy;
101984   p->pLimit = pLimit;
101985   p->pOffset = pOffset;
101986   return;
101987 }
101988 #endif /* SQLITE_OMIT_CTE */
101989 
101990 /* Forward references */
101991 static int multiSelectOrderBy(
101992   Parse *pParse,        /* Parsing context */
101993   Select *p,            /* The right-most of SELECTs to be coded */
101994   SelectDest *pDest     /* What to do with query results */
101995 );
101996 
101997 
101998 /*
101999 ** This routine is called to process a compound query form from
102000 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
102001 ** INTERSECT
102002 **
102003 ** "p" points to the right-most of the two queries.  the query on the
102004 ** left is p->pPrior.  The left query could also be a compound query
102005 ** in which case this routine will be called recursively.
102006 **
102007 ** The results of the total query are to be written into a destination
102008 ** of type eDest with parameter iParm.
102009 **
102010 ** Example 1:  Consider a three-way compound SQL statement.
102011 **
102012 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
102013 **
102014 ** This statement is parsed up as follows:
102015 **
102016 **     SELECT c FROM t3
102017 **      |
102018 **      `----->  SELECT b FROM t2
102019 **                |
102020 **                `------>  SELECT a FROM t1
102021 **
102022 ** The arrows in the diagram above represent the Select.pPrior pointer.
102023 ** So if this routine is called with p equal to the t3 query, then
102024 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
102025 **
102026 ** Notice that because of the way SQLite parses compound SELECTs, the
102027 ** individual selects always group from left to right.
102028 */
102029 static int multiSelect(
102030   Parse *pParse,        /* Parsing context */
102031   Select *p,            /* The right-most of SELECTs to be coded */
102032   SelectDest *pDest     /* What to do with query results */
102033 ){
102034   int rc = SQLITE_OK;   /* Success code from a subroutine */
102035   Select *pPrior;       /* Another SELECT immediately to our left */
102036   Vdbe *v;              /* Generate code to this VDBE */
102037   SelectDest dest;      /* Alternative data destination */
102038   Select *pDelete = 0;  /* Chain of simple selects to delete */
102039   sqlite3 *db;          /* Database connection */
102040 #ifndef SQLITE_OMIT_EXPLAIN
102041   int iSub1 = 0;        /* EQP id of left-hand query */
102042   int iSub2 = 0;        /* EQP id of right-hand query */
102043 #endif
102044 
102045   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
102046   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
102047   */
102048   assert( p && p->pPrior );  /* Calling function guarantees this much */
102049   assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
102050   db = pParse->db;
102051   pPrior = p->pPrior;
102052   dest = *pDest;
102053   if( pPrior->pOrderBy ){
102054     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
102055       selectOpName(p->op));
102056     rc = 1;
102057     goto multi_select_end;
102058   }
102059   if( pPrior->pLimit ){
102060     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
102061       selectOpName(p->op));
102062     rc = 1;
102063     goto multi_select_end;
102064   }
102065 
102066   v = sqlite3GetVdbe(pParse);
102067   assert( v!=0 );  /* The VDBE already created by calling function */
102068 
102069   /* Create the destination temporary table if necessary
102070   */
102071   if( dest.eDest==SRT_EphemTab ){
102072     assert( p->pEList );
102073     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
102074     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
102075     dest.eDest = SRT_Table;
102076   }
102077 
102078   /* Make sure all SELECTs in the statement have the same number of elements
102079   ** in their result sets.
102080   */
102081   assert( p->pEList && pPrior->pEList );
102082   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
102083     if( p->selFlags & SF_Values ){
102084       sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
102085     }else{
102086       sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
102087         " do not have the same number of result columns", selectOpName(p->op));
102088     }
102089     rc = 1;
102090     goto multi_select_end;
102091   }
102092 
102093 #ifndef SQLITE_OMIT_CTE
102094   if( p->selFlags & SF_Recursive ){
102095     generateWithRecursiveQuery(pParse, p, &dest);
102096   }else
102097 #endif
102098 
102099   /* Compound SELECTs that have an ORDER BY clause are handled separately.
102100   */
102101   if( p->pOrderBy ){
102102     return multiSelectOrderBy(pParse, p, pDest);
102103   }else
102104 
102105   /* Generate code for the left and right SELECT statements.
102106   */
102107   switch( p->op ){
102108     case TK_ALL: {
102109       int addr = 0;
102110       int nLimit;
102111       assert( !pPrior->pLimit );
102112       pPrior->iLimit = p->iLimit;
102113       pPrior->iOffset = p->iOffset;
102114       pPrior->pLimit = p->pLimit;
102115       pPrior->pOffset = p->pOffset;
102116       explainSetInteger(iSub1, pParse->iNextSelectId);
102117       rc = sqlite3Select(pParse, pPrior, &dest);
102118       p->pLimit = 0;
102119       p->pOffset = 0;
102120       if( rc ){
102121         goto multi_select_end;
102122       }
102123       p->pPrior = 0;
102124       p->iLimit = pPrior->iLimit;
102125       p->iOffset = pPrior->iOffset;
102126       if( p->iLimit ){
102127         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit); VdbeCoverage(v);
102128         VdbeComment((v, "Jump ahead if LIMIT reached"));
102129       }
102130       explainSetInteger(iSub2, pParse->iNextSelectId);
102131       rc = sqlite3Select(pParse, p, &dest);
102132       testcase( rc!=SQLITE_OK );
102133       pDelete = p->pPrior;
102134       p->pPrior = pPrior;
102135       p->nSelectRow += pPrior->nSelectRow;
102136       if( pPrior->pLimit
102137        && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
102138        && nLimit>0 && p->nSelectRow > (u64)nLimit
102139       ){
102140         p->nSelectRow = nLimit;
102141       }
102142       if( addr ){
102143         sqlite3VdbeJumpHere(v, addr);
102144       }
102145       break;
102146     }
102147     case TK_EXCEPT:
102148     case TK_UNION: {
102149       int unionTab;    /* Cursor number of the temporary table holding result */
102150       u8 op = 0;       /* One of the SRT_ operations to apply to self */
102151       int priorOp;     /* The SRT_ operation to apply to prior selects */
102152       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
102153       int addr;
102154       SelectDest uniondest;
102155 
102156       testcase( p->op==TK_EXCEPT );
102157       testcase( p->op==TK_UNION );
102158       priorOp = SRT_Union;
102159       if( dest.eDest==priorOp ){
102160         /* We can reuse a temporary table generated by a SELECT to our
102161         ** right.
102162         */
102163         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
102164         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
102165         unionTab = dest.iSDParm;
102166       }else{
102167         /* We will need to create our own temporary table to hold the
102168         ** intermediate results.
102169         */
102170         unionTab = pParse->nTab++;
102171         assert( p->pOrderBy==0 );
102172         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
102173         assert( p->addrOpenEphm[0] == -1 );
102174         p->addrOpenEphm[0] = addr;
102175         findRightmost(p)->selFlags |= SF_UsesEphemeral;
102176         assert( p->pEList );
102177       }
102178 
102179       /* Code the SELECT statements to our left
102180       */
102181       assert( !pPrior->pOrderBy );
102182       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
102183       explainSetInteger(iSub1, pParse->iNextSelectId);
102184       rc = sqlite3Select(pParse, pPrior, &uniondest);
102185       if( rc ){
102186         goto multi_select_end;
102187       }
102188 
102189       /* Code the current SELECT statement
102190       */
102191       if( p->op==TK_EXCEPT ){
102192         op = SRT_Except;
102193       }else{
102194         assert( p->op==TK_UNION );
102195         op = SRT_Union;
102196       }
102197       p->pPrior = 0;
102198       pLimit = p->pLimit;
102199       p->pLimit = 0;
102200       pOffset = p->pOffset;
102201       p->pOffset = 0;
102202       uniondest.eDest = op;
102203       explainSetInteger(iSub2, pParse->iNextSelectId);
102204       rc = sqlite3Select(pParse, p, &uniondest);
102205       testcase( rc!=SQLITE_OK );
102206       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
102207       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
102208       sqlite3ExprListDelete(db, p->pOrderBy);
102209       pDelete = p->pPrior;
102210       p->pPrior = pPrior;
102211       p->pOrderBy = 0;
102212       if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
102213       sqlite3ExprDelete(db, p->pLimit);
102214       p->pLimit = pLimit;
102215       p->pOffset = pOffset;
102216       p->iLimit = 0;
102217       p->iOffset = 0;
102218 
102219       /* Convert the data in the temporary table into whatever form
102220       ** it is that we currently need.
102221       */
102222       assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
102223       if( dest.eDest!=priorOp ){
102224         int iCont, iBreak, iStart;
102225         assert( p->pEList );
102226         if( dest.eDest==SRT_Output ){
102227           Select *pFirst = p;
102228           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
102229           generateColumnNames(pParse, 0, pFirst->pEList);
102230         }
102231         iBreak = sqlite3VdbeMakeLabel(v);
102232         iCont = sqlite3VdbeMakeLabel(v);
102233         computeLimitRegisters(pParse, p, iBreak);
102234         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); VdbeCoverage(v);
102235         iStart = sqlite3VdbeCurrentAddr(v);
102236         selectInnerLoop(pParse, p, p->pEList, unionTab,
102237                         0, 0, &dest, iCont, iBreak);
102238         sqlite3VdbeResolveLabel(v, iCont);
102239         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); VdbeCoverage(v);
102240         sqlite3VdbeResolveLabel(v, iBreak);
102241         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
102242       }
102243       break;
102244     }
102245     default: assert( p->op==TK_INTERSECT ); {
102246       int tab1, tab2;
102247       int iCont, iBreak, iStart;
102248       Expr *pLimit, *pOffset;
102249       int addr;
102250       SelectDest intersectdest;
102251       int r1;
102252 
102253       /* INTERSECT is different from the others since it requires
102254       ** two temporary tables.  Hence it has its own case.  Begin
102255       ** by allocating the tables we will need.
102256       */
102257       tab1 = pParse->nTab++;
102258       tab2 = pParse->nTab++;
102259       assert( p->pOrderBy==0 );
102260 
102261       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
102262       assert( p->addrOpenEphm[0] == -1 );
102263       p->addrOpenEphm[0] = addr;
102264       findRightmost(p)->selFlags |= SF_UsesEphemeral;
102265       assert( p->pEList );
102266 
102267       /* Code the SELECTs to our left into temporary table "tab1".
102268       */
102269       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
102270       explainSetInteger(iSub1, pParse->iNextSelectId);
102271       rc = sqlite3Select(pParse, pPrior, &intersectdest);
102272       if( rc ){
102273         goto multi_select_end;
102274       }
102275 
102276       /* Code the current SELECT into temporary table "tab2"
102277       */
102278       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
102279       assert( p->addrOpenEphm[1] == -1 );
102280       p->addrOpenEphm[1] = addr;
102281       p->pPrior = 0;
102282       pLimit = p->pLimit;
102283       p->pLimit = 0;
102284       pOffset = p->pOffset;
102285       p->pOffset = 0;
102286       intersectdest.iSDParm = tab2;
102287       explainSetInteger(iSub2, pParse->iNextSelectId);
102288       rc = sqlite3Select(pParse, p, &intersectdest);
102289       testcase( rc!=SQLITE_OK );
102290       pDelete = p->pPrior;
102291       p->pPrior = pPrior;
102292       if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
102293       sqlite3ExprDelete(db, p->pLimit);
102294       p->pLimit = pLimit;
102295       p->pOffset = pOffset;
102296 
102297       /* Generate code to take the intersection of the two temporary
102298       ** tables.
102299       */
102300       assert( p->pEList );
102301       if( dest.eDest==SRT_Output ){
102302         Select *pFirst = p;
102303         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
102304         generateColumnNames(pParse, 0, pFirst->pEList);
102305       }
102306       iBreak = sqlite3VdbeMakeLabel(v);
102307       iCont = sqlite3VdbeMakeLabel(v);
102308       computeLimitRegisters(pParse, p, iBreak);
102309       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
102310       r1 = sqlite3GetTempReg(pParse);
102311       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
102312       sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); VdbeCoverage(v);
102313       sqlite3ReleaseTempReg(pParse, r1);
102314       selectInnerLoop(pParse, p, p->pEList, tab1,
102315                       0, 0, &dest, iCont, iBreak);
102316       sqlite3VdbeResolveLabel(v, iCont);
102317       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
102318       sqlite3VdbeResolveLabel(v, iBreak);
102319       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
102320       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
102321       break;
102322     }
102323   }
102324 
102325   explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
102326 
102327   /* Compute collating sequences used by
102328   ** temporary tables needed to implement the compound select.
102329   ** Attach the KeyInfo structure to all temporary tables.
102330   **
102331   ** This section is run by the right-most SELECT statement only.
102332   ** SELECT statements to the left always skip this part.  The right-most
102333   ** SELECT might also skip this part if it has no ORDER BY clause and
102334   ** no temp tables are required.
102335   */
102336   if( p->selFlags & SF_UsesEphemeral ){
102337     int i;                        /* Loop counter */
102338     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
102339     Select *pLoop;                /* For looping through SELECT statements */
102340     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
102341     int nCol;                     /* Number of columns in result set */
102342 
102343     assert( p->pNext==0 );
102344     nCol = p->pEList->nExpr;
102345     pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
102346     if( !pKeyInfo ){
102347       rc = SQLITE_NOMEM;
102348       goto multi_select_end;
102349     }
102350     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
102351       *apColl = multiSelectCollSeq(pParse, p, i);
102352       if( 0==*apColl ){
102353         *apColl = db->pDfltColl;
102354       }
102355     }
102356 
102357     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
102358       for(i=0; i<2; i++){
102359         int addr = pLoop->addrOpenEphm[i];
102360         if( addr<0 ){
102361           /* If [0] is unused then [1] is also unused.  So we can
102362           ** always safely abort as soon as the first unused slot is found */
102363           assert( pLoop->addrOpenEphm[1]<0 );
102364           break;
102365         }
102366         sqlite3VdbeChangeP2(v, addr, nCol);
102367         sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
102368                             P4_KEYINFO);
102369         pLoop->addrOpenEphm[i] = -1;
102370       }
102371     }
102372     sqlite3KeyInfoUnref(pKeyInfo);
102373   }
102374 
102375 multi_select_end:
102376   pDest->iSdst = dest.iSdst;
102377   pDest->nSdst = dest.nSdst;
102378   sqlite3SelectDelete(db, pDelete);
102379   return rc;
102380 }
102381 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
102382 
102383 /*
102384 ** Code an output subroutine for a coroutine implementation of a
102385 ** SELECT statment.
102386 **
102387 ** The data to be output is contained in pIn->iSdst.  There are
102388 ** pIn->nSdst columns to be output.  pDest is where the output should
102389 ** be sent.
102390 **
102391 ** regReturn is the number of the register holding the subroutine
102392 ** return address.
102393 **
102394 ** If regPrev>0 then it is the first register in a vector that
102395 ** records the previous output.  mem[regPrev] is a flag that is false
102396 ** if there has been no previous output.  If regPrev>0 then code is
102397 ** generated to suppress duplicates.  pKeyInfo is used for comparing
102398 ** keys.
102399 **
102400 ** If the LIMIT found in p->iLimit is reached, jump immediately to
102401 ** iBreak.
102402 */
102403 static int generateOutputSubroutine(
102404   Parse *pParse,          /* Parsing context */
102405   Select *p,              /* The SELECT statement */
102406   SelectDest *pIn,        /* Coroutine supplying data */
102407   SelectDest *pDest,      /* Where to send the data */
102408   int regReturn,          /* The return address register */
102409   int regPrev,            /* Previous result register.  No uniqueness if 0 */
102410   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
102411   int iBreak              /* Jump here if we hit the LIMIT */
102412 ){
102413   Vdbe *v = pParse->pVdbe;
102414   int iContinue;
102415   int addr;
102416 
102417   addr = sqlite3VdbeCurrentAddr(v);
102418   iContinue = sqlite3VdbeMakeLabel(v);
102419 
102420   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
102421   */
102422   if( regPrev ){
102423     int j1, j2;
102424     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); VdbeCoverage(v);
102425     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
102426                               (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
102427     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2); VdbeCoverage(v);
102428     sqlite3VdbeJumpHere(v, j1);
102429     sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
102430     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
102431   }
102432   if( pParse->db->mallocFailed ) return 0;
102433 
102434   /* Suppress the first OFFSET entries if there is an OFFSET clause
102435   */
102436   codeOffset(v, p->iOffset, iContinue);
102437 
102438   switch( pDest->eDest ){
102439     /* Store the result as data using a unique key.
102440     */
102441     case SRT_Table:
102442     case SRT_EphemTab: {
102443       int r1 = sqlite3GetTempReg(pParse);
102444       int r2 = sqlite3GetTempReg(pParse);
102445       testcase( pDest->eDest==SRT_Table );
102446       testcase( pDest->eDest==SRT_EphemTab );
102447       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
102448       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
102449       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
102450       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
102451       sqlite3ReleaseTempReg(pParse, r2);
102452       sqlite3ReleaseTempReg(pParse, r1);
102453       break;
102454     }
102455 
102456 #ifndef SQLITE_OMIT_SUBQUERY
102457     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
102458     ** then there should be a single item on the stack.  Write this
102459     ** item into the set table with bogus data.
102460     */
102461     case SRT_Set: {
102462       int r1;
102463       assert( pIn->nSdst==1 );
102464       pDest->affSdst =
102465          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
102466       r1 = sqlite3GetTempReg(pParse);
102467       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
102468       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
102469       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
102470       sqlite3ReleaseTempReg(pParse, r1);
102471       break;
102472     }
102473 
102474 #if 0  /* Never occurs on an ORDER BY query */
102475     /* If any row exist in the result set, record that fact and abort.
102476     */
102477     case SRT_Exists: {
102478       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
102479       /* The LIMIT clause will terminate the loop for us */
102480       break;
102481     }
102482 #endif
102483 
102484     /* If this is a scalar select that is part of an expression, then
102485     ** store the results in the appropriate memory cell and break out
102486     ** of the scan loop.
102487     */
102488     case SRT_Mem: {
102489       assert( pIn->nSdst==1 );
102490       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
102491       /* The LIMIT clause will jump out of the loop for us */
102492       break;
102493     }
102494 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
102495 
102496     /* The results are stored in a sequence of registers
102497     ** starting at pDest->iSdst.  Then the co-routine yields.
102498     */
102499     case SRT_Coroutine: {
102500       if( pDest->iSdst==0 ){
102501         pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
102502         pDest->nSdst = pIn->nSdst;
102503       }
102504       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
102505       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
102506       break;
102507     }
102508 
102509     /* If none of the above, then the result destination must be
102510     ** SRT_Output.  This routine is never called with any other
102511     ** destination other than the ones handled above or SRT_Output.
102512     **
102513     ** For SRT_Output, results are stored in a sequence of registers.
102514     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
102515     ** return the next row of result.
102516     */
102517     default: {
102518       assert( pDest->eDest==SRT_Output );
102519       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
102520       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
102521       break;
102522     }
102523   }
102524 
102525   /* Jump to the end of the loop if the LIMIT is reached.
102526   */
102527   if( p->iLimit ){
102528     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); VdbeCoverage(v);
102529   }
102530 
102531   /* Generate the subroutine return
102532   */
102533   sqlite3VdbeResolveLabel(v, iContinue);
102534   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
102535 
102536   return addr;
102537 }
102538 
102539 /*
102540 ** Alternative compound select code generator for cases when there
102541 ** is an ORDER BY clause.
102542 **
102543 ** We assume a query of the following form:
102544 **
102545 **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
102546 **
102547 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
102548 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
102549 ** co-routines.  Then run the co-routines in parallel and merge the results
102550 ** into the output.  In addition to the two coroutines (called selectA and
102551 ** selectB) there are 7 subroutines:
102552 **
102553 **    outA:    Move the output of the selectA coroutine into the output
102554 **             of the compound query.
102555 **
102556 **    outB:    Move the output of the selectB coroutine into the output
102557 **             of the compound query.  (Only generated for UNION and
102558 **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
102559 **             appears only in B.)
102560 **
102561 **    AltB:    Called when there is data from both coroutines and A<B.
102562 **
102563 **    AeqB:    Called when there is data from both coroutines and A==B.
102564 **
102565 **    AgtB:    Called when there is data from both coroutines and A>B.
102566 **
102567 **    EofA:    Called when data is exhausted from selectA.
102568 **
102569 **    EofB:    Called when data is exhausted from selectB.
102570 **
102571 ** The implementation of the latter five subroutines depend on which
102572 ** <operator> is used:
102573 **
102574 **
102575 **             UNION ALL         UNION            EXCEPT          INTERSECT
102576 **          -------------  -----------------  --------------  -----------------
102577 **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
102578 **
102579 **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
102580 **
102581 **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
102582 **
102583 **   EofA:   outB, nextB      outB, nextB          halt             halt
102584 **
102585 **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
102586 **
102587 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
102588 ** causes an immediate jump to EofA and an EOF on B following nextB causes
102589 ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
102590 ** following nextX causes a jump to the end of the select processing.
102591 **
102592 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
102593 ** within the output subroutine.  The regPrev register set holds the previously
102594 ** output value.  A comparison is made against this value and the output
102595 ** is skipped if the next results would be the same as the previous.
102596 **
102597 ** The implementation plan is to implement the two coroutines and seven
102598 ** subroutines first, then put the control logic at the bottom.  Like this:
102599 **
102600 **          goto Init
102601 **     coA: coroutine for left query (A)
102602 **     coB: coroutine for right query (B)
102603 **    outA: output one row of A
102604 **    outB: output one row of B (UNION and UNION ALL only)
102605 **    EofA: ...
102606 **    EofB: ...
102607 **    AltB: ...
102608 **    AeqB: ...
102609 **    AgtB: ...
102610 **    Init: initialize coroutine registers
102611 **          yield coA
102612 **          if eof(A) goto EofA
102613 **          yield coB
102614 **          if eof(B) goto EofB
102615 **    Cmpr: Compare A, B
102616 **          Jump AltB, AeqB, AgtB
102617 **     End: ...
102618 **
102619 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
102620 ** actually called using Gosub and they do not Return.  EofA and EofB loop
102621 ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
102622 ** and AgtB jump to either L2 or to one of EofA or EofB.
102623 */
102624 #ifndef SQLITE_OMIT_COMPOUND_SELECT
102625 static int multiSelectOrderBy(
102626   Parse *pParse,        /* Parsing context */
102627   Select *p,            /* The right-most of SELECTs to be coded */
102628   SelectDest *pDest     /* What to do with query results */
102629 ){
102630   int i, j;             /* Loop counters */
102631   Select *pPrior;       /* Another SELECT immediately to our left */
102632   Vdbe *v;              /* Generate code to this VDBE */
102633   SelectDest destA;     /* Destination for coroutine A */
102634   SelectDest destB;     /* Destination for coroutine B */
102635   int regAddrA;         /* Address register for select-A coroutine */
102636   int regAddrB;         /* Address register for select-B coroutine */
102637   int addrSelectA;      /* Address of the select-A coroutine */
102638   int addrSelectB;      /* Address of the select-B coroutine */
102639   int regOutA;          /* Address register for the output-A subroutine */
102640   int regOutB;          /* Address register for the output-B subroutine */
102641   int addrOutA;         /* Address of the output-A subroutine */
102642   int addrOutB = 0;     /* Address of the output-B subroutine */
102643   int addrEofA;         /* Address of the select-A-exhausted subroutine */
102644   int addrEofA_noB;     /* Alternate addrEofA if B is uninitialized */
102645   int addrEofB;         /* Address of the select-B-exhausted subroutine */
102646   int addrAltB;         /* Address of the A<B subroutine */
102647   int addrAeqB;         /* Address of the A==B subroutine */
102648   int addrAgtB;         /* Address of the A>B subroutine */
102649   int regLimitA;        /* Limit register for select-A */
102650   int regLimitB;        /* Limit register for select-A */
102651   int regPrev;          /* A range of registers to hold previous output */
102652   int savedLimit;       /* Saved value of p->iLimit */
102653   int savedOffset;      /* Saved value of p->iOffset */
102654   int labelCmpr;        /* Label for the start of the merge algorithm */
102655   int labelEnd;         /* Label for the end of the overall SELECT stmt */
102656   int j1;               /* Jump instructions that get retargetted */
102657   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
102658   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
102659   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
102660   sqlite3 *db;          /* Database connection */
102661   ExprList *pOrderBy;   /* The ORDER BY clause */
102662   int nOrderBy;         /* Number of terms in the ORDER BY clause */
102663   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
102664 #ifndef SQLITE_OMIT_EXPLAIN
102665   int iSub1;            /* EQP id of left-hand query */
102666   int iSub2;            /* EQP id of right-hand query */
102667 #endif
102668 
102669   assert( p->pOrderBy!=0 );
102670   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
102671   db = pParse->db;
102672   v = pParse->pVdbe;
102673   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
102674   labelEnd = sqlite3VdbeMakeLabel(v);
102675   labelCmpr = sqlite3VdbeMakeLabel(v);
102676 
102677 
102678   /* Patch up the ORDER BY clause
102679   */
102680   op = p->op;
102681   pPrior = p->pPrior;
102682   assert( pPrior->pOrderBy==0 );
102683   pOrderBy = p->pOrderBy;
102684   assert( pOrderBy );
102685   nOrderBy = pOrderBy->nExpr;
102686 
102687   /* For operators other than UNION ALL we have to make sure that
102688   ** the ORDER BY clause covers every term of the result set.  Add
102689   ** terms to the ORDER BY clause as necessary.
102690   */
102691   if( op!=TK_ALL ){
102692     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
102693       struct ExprList_item *pItem;
102694       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
102695         assert( pItem->u.x.iOrderByCol>0 );
102696         if( pItem->u.x.iOrderByCol==i ) break;
102697       }
102698       if( j==nOrderBy ){
102699         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
102700         if( pNew==0 ) return SQLITE_NOMEM;
102701         pNew->flags |= EP_IntValue;
102702         pNew->u.iValue = i;
102703         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
102704         if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
102705       }
102706     }
102707   }
102708 
102709   /* Compute the comparison permutation and keyinfo that is used with
102710   ** the permutation used to determine if the next
102711   ** row of results comes from selectA or selectB.  Also add explicit
102712   ** collations to the ORDER BY clause terms so that when the subqueries
102713   ** to the right and the left are evaluated, they use the correct
102714   ** collation.
102715   */
102716   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
102717   if( aPermute ){
102718     struct ExprList_item *pItem;
102719     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
102720       assert( pItem->u.x.iOrderByCol>0
102721           && pItem->u.x.iOrderByCol<=p->pEList->nExpr );
102722       aPermute[i] = pItem->u.x.iOrderByCol - 1;
102723     }
102724     pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
102725   }else{
102726     pKeyMerge = 0;
102727   }
102728 
102729   /* Reattach the ORDER BY clause to the query.
102730   */
102731   p->pOrderBy = pOrderBy;
102732   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
102733 
102734   /* Allocate a range of temporary registers and the KeyInfo needed
102735   ** for the logic that removes duplicate result rows when the
102736   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
102737   */
102738   if( op==TK_ALL ){
102739     regPrev = 0;
102740   }else{
102741     int nExpr = p->pEList->nExpr;
102742     assert( nOrderBy>=nExpr || db->mallocFailed );
102743     regPrev = pParse->nMem+1;
102744     pParse->nMem += nExpr+1;
102745     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
102746     pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
102747     if( pKeyDup ){
102748       assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
102749       for(i=0; i<nExpr; i++){
102750         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
102751         pKeyDup->aSortOrder[i] = 0;
102752       }
102753     }
102754   }
102755 
102756   /* Separate the left and the right query from one another
102757   */
102758   p->pPrior = 0;
102759   pPrior->pNext = 0;
102760   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
102761   if( pPrior->pPrior==0 ){
102762     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
102763   }
102764 
102765   /* Compute the limit registers */
102766   computeLimitRegisters(pParse, p, labelEnd);
102767   if( p->iLimit && op==TK_ALL ){
102768     regLimitA = ++pParse->nMem;
102769     regLimitB = ++pParse->nMem;
102770     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
102771                                   regLimitA);
102772     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
102773   }else{
102774     regLimitA = regLimitB = 0;
102775   }
102776   sqlite3ExprDelete(db, p->pLimit);
102777   p->pLimit = 0;
102778   sqlite3ExprDelete(db, p->pOffset);
102779   p->pOffset = 0;
102780 
102781   regAddrA = ++pParse->nMem;
102782   regAddrB = ++pParse->nMem;
102783   regOutA = ++pParse->nMem;
102784   regOutB = ++pParse->nMem;
102785   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
102786   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
102787 
102788   /* Generate a coroutine to evaluate the SELECT statement to the
102789   ** left of the compound operator - the "A" select.
102790   */
102791   addrSelectA = sqlite3VdbeCurrentAddr(v) + 1;
102792   j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
102793   VdbeComment((v, "left SELECT"));
102794   pPrior->iLimit = regLimitA;
102795   explainSetInteger(iSub1, pParse->iNextSelectId);
102796   sqlite3Select(pParse, pPrior, &destA);
102797   sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
102798   sqlite3VdbeJumpHere(v, j1);
102799 
102800   /* Generate a coroutine to evaluate the SELECT statement on
102801   ** the right - the "B" select
102802   */
102803   addrSelectB = sqlite3VdbeCurrentAddr(v) + 1;
102804   j1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrB, 0, addrSelectB);
102805   VdbeComment((v, "right SELECT"));
102806   savedLimit = p->iLimit;
102807   savedOffset = p->iOffset;
102808   p->iLimit = regLimitB;
102809   p->iOffset = 0;
102810   explainSetInteger(iSub2, pParse->iNextSelectId);
102811   sqlite3Select(pParse, p, &destB);
102812   p->iLimit = savedLimit;
102813   p->iOffset = savedOffset;
102814   sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
102815 
102816   /* Generate a subroutine that outputs the current row of the A
102817   ** select as the next output row of the compound select.
102818   */
102819   VdbeNoopComment((v, "Output routine for A"));
102820   addrOutA = generateOutputSubroutine(pParse,
102821                  p, &destA, pDest, regOutA,
102822                  regPrev, pKeyDup, labelEnd);
102823 
102824   /* Generate a subroutine that outputs the current row of the B
102825   ** select as the next output row of the compound select.
102826   */
102827   if( op==TK_ALL || op==TK_UNION ){
102828     VdbeNoopComment((v, "Output routine for B"));
102829     addrOutB = generateOutputSubroutine(pParse,
102830                  p, &destB, pDest, regOutB,
102831                  regPrev, pKeyDup, labelEnd);
102832   }
102833   sqlite3KeyInfoUnref(pKeyDup);
102834 
102835   /* Generate a subroutine to run when the results from select A
102836   ** are exhausted and only data in select B remains.
102837   */
102838   if( op==TK_EXCEPT || op==TK_INTERSECT ){
102839     addrEofA_noB = addrEofA = labelEnd;
102840   }else{
102841     VdbeNoopComment((v, "eof-A subroutine"));
102842     addrEofA = sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
102843     addrEofA_noB = sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, labelEnd);
102844                                      VdbeCoverage(v);
102845     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
102846     p->nSelectRow += pPrior->nSelectRow;
102847   }
102848 
102849   /* Generate a subroutine to run when the results from select B
102850   ** are exhausted and only data in select A remains.
102851   */
102852   if( op==TK_INTERSECT ){
102853     addrEofB = addrEofA;
102854     if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
102855   }else{
102856     VdbeNoopComment((v, "eof-B subroutine"));
102857     addrEofB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
102858     sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, labelEnd); VdbeCoverage(v);
102859     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
102860   }
102861 
102862   /* Generate code to handle the case of A<B
102863   */
102864   VdbeNoopComment((v, "A-lt-B subroutine"));
102865   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
102866   sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
102867   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102868 
102869   /* Generate code to handle the case of A==B
102870   */
102871   if( op==TK_ALL ){
102872     addrAeqB = addrAltB;
102873   }else if( op==TK_INTERSECT ){
102874     addrAeqB = addrAltB;
102875     addrAltB++;
102876   }else{
102877     VdbeNoopComment((v, "A-eq-B subroutine"));
102878     addrAeqB =
102879     sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA); VdbeCoverage(v);
102880     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102881   }
102882 
102883   /* Generate code to handle the case of A>B
102884   */
102885   VdbeNoopComment((v, "A-gt-B subroutine"));
102886   addrAgtB = sqlite3VdbeCurrentAddr(v);
102887   if( op==TK_ALL || op==TK_UNION ){
102888     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
102889   }
102890   sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
102891   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102892 
102893   /* This code runs once to initialize everything.
102894   */
102895   sqlite3VdbeJumpHere(v, j1);
102896   sqlite3VdbeAddOp2(v, OP_Yield, regAddrA, addrEofA_noB); VdbeCoverage(v);
102897   sqlite3VdbeAddOp2(v, OP_Yield, regAddrB, addrEofB); VdbeCoverage(v);
102898 
102899   /* Implement the main merge loop
102900   */
102901   sqlite3VdbeResolveLabel(v, labelCmpr);
102902   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
102903   sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
102904                          (char*)pKeyMerge, P4_KEYINFO);
102905   sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
102906   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); VdbeCoverage(v);
102907 
102908   /* Jump to the this point in order to terminate the query.
102909   */
102910   sqlite3VdbeResolveLabel(v, labelEnd);
102911 
102912   /* Set the number of output columns
102913   */
102914   if( pDest->eDest==SRT_Output ){
102915     Select *pFirst = pPrior;
102916     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
102917     generateColumnNames(pParse, 0, pFirst->pEList);
102918   }
102919 
102920   /* Reassembly the compound query so that it will be freed correctly
102921   ** by the calling function */
102922   if( p->pPrior ){
102923     sqlite3SelectDelete(db, p->pPrior);
102924   }
102925   p->pPrior = pPrior;
102926   pPrior->pNext = p;
102927 
102928   /*** TBD:  Insert subroutine calls to close cursors on incomplete
102929   **** subqueries ****/
102930   explainComposite(pParse, p->op, iSub1, iSub2, 0);
102931   return SQLITE_OK;
102932 }
102933 #endif
102934 
102935 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
102936 /* Forward Declarations */
102937 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
102938 static void substSelect(sqlite3*, Select *, int, ExprList *);
102939 
102940 /*
102941 ** Scan through the expression pExpr.  Replace every reference to
102942 ** a column in table number iTable with a copy of the iColumn-th
102943 ** entry in pEList.  (But leave references to the ROWID column
102944 ** unchanged.)
102945 **
102946 ** This routine is part of the flattening procedure.  A subquery
102947 ** whose result set is defined by pEList appears as entry in the
102948 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
102949 ** FORM clause entry is iTable.  This routine make the necessary
102950 ** changes to pExpr so that it refers directly to the source table
102951 ** of the subquery rather the result set of the subquery.
102952 */
102953 static Expr *substExpr(
102954   sqlite3 *db,        /* Report malloc errors to this connection */
102955   Expr *pExpr,        /* Expr in which substitution occurs */
102956   int iTable,         /* Table to be substituted */
102957   ExprList *pEList    /* Substitute expressions */
102958 ){
102959   if( pExpr==0 ) return 0;
102960   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
102961     if( pExpr->iColumn<0 ){
102962       pExpr->op = TK_NULL;
102963     }else{
102964       Expr *pNew;
102965       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
102966       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
102967       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
102968       sqlite3ExprDelete(db, pExpr);
102969       pExpr = pNew;
102970     }
102971   }else{
102972     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
102973     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
102974     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102975       substSelect(db, pExpr->x.pSelect, iTable, pEList);
102976     }else{
102977       substExprList(db, pExpr->x.pList, iTable, pEList);
102978     }
102979   }
102980   return pExpr;
102981 }
102982 static void substExprList(
102983   sqlite3 *db,         /* Report malloc errors here */
102984   ExprList *pList,     /* List to scan and in which to make substitutes */
102985   int iTable,          /* Table to be substituted */
102986   ExprList *pEList     /* Substitute values */
102987 ){
102988   int i;
102989   if( pList==0 ) return;
102990   for(i=0; i<pList->nExpr; i++){
102991     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
102992   }
102993 }
102994 static void substSelect(
102995   sqlite3 *db,         /* Report malloc errors here */
102996   Select *p,           /* SELECT statement in which to make substitutions */
102997   int iTable,          /* Table to be replaced */
102998   ExprList *pEList     /* Substitute values */
102999 ){
103000   SrcList *pSrc;
103001   struct SrcList_item *pItem;
103002   int i;
103003   if( !p ) return;
103004   substExprList(db, p->pEList, iTable, pEList);
103005   substExprList(db, p->pGroupBy, iTable, pEList);
103006   substExprList(db, p->pOrderBy, iTable, pEList);
103007   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
103008   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
103009   substSelect(db, p->pPrior, iTable, pEList);
103010   pSrc = p->pSrc;
103011   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
103012   if( ALWAYS(pSrc) ){
103013     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
103014       substSelect(db, pItem->pSelect, iTable, pEList);
103015     }
103016   }
103017 }
103018 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
103019 
103020 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
103021 /*
103022 ** This routine attempts to flatten subqueries as a performance optimization.
103023 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
103024 **
103025 ** To understand the concept of flattening, consider the following
103026 ** query:
103027 **
103028 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
103029 **
103030 ** The default way of implementing this query is to execute the
103031 ** subquery first and store the results in a temporary table, then
103032 ** run the outer query on that temporary table.  This requires two
103033 ** passes over the data.  Furthermore, because the temporary table
103034 ** has no indices, the WHERE clause on the outer query cannot be
103035 ** optimized.
103036 **
103037 ** This routine attempts to rewrite queries such as the above into
103038 ** a single flat select, like this:
103039 **
103040 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
103041 **
103042 ** The code generated for this simpification gives the same result
103043 ** but only has to scan the data once.  And because indices might
103044 ** exist on the table t1, a complete scan of the data might be
103045 ** avoided.
103046 **
103047 ** Flattening is only attempted if all of the following are true:
103048 **
103049 **   (1)  The subquery and the outer query do not both use aggregates.
103050 **
103051 **   (2)  The subquery is not an aggregate or the outer query is not a join.
103052 **
103053 **   (3)  The subquery is not the right operand of a left outer join
103054 **        (Originally ticket #306.  Strengthened by ticket #3300)
103055 **
103056 **   (4)  The subquery is not DISTINCT.
103057 **
103058 **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
103059 **        sub-queries that were excluded from this optimization. Restriction
103060 **        (4) has since been expanded to exclude all DISTINCT subqueries.
103061 **
103062 **   (6)  The subquery does not use aggregates or the outer query is not
103063 **        DISTINCT.
103064 **
103065 **   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
103066 **        A FROM clause, consider adding a FROM close with the special
103067 **        table sqlite_once that consists of a single row containing a
103068 **        single NULL.
103069 **
103070 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
103071 **
103072 **   (9)  The subquery does not use LIMIT or the outer query does not use
103073 **        aggregates.
103074 **
103075 **  (10)  The subquery does not use aggregates or the outer query does not
103076 **        use LIMIT.
103077 **
103078 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
103079 **
103080 **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
103081 **        a separate restriction deriving from ticket #350.
103082 **
103083 **  (13)  The subquery and outer query do not both use LIMIT.
103084 **
103085 **  (14)  The subquery does not use OFFSET.
103086 **
103087 **  (15)  The outer query is not part of a compound select or the
103088 **        subquery does not have a LIMIT clause.
103089 **        (See ticket #2339 and ticket [02a8e81d44]).
103090 **
103091 **  (16)  The outer query is not an aggregate or the subquery does
103092 **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
103093 **        until we introduced the group_concat() function.
103094 **
103095 **  (17)  The sub-query is not a compound select, or it is a UNION ALL
103096 **        compound clause made up entirely of non-aggregate queries, and
103097 **        the parent query:
103098 **
103099 **          * is not itself part of a compound select,
103100 **          * is not an aggregate or DISTINCT query, and
103101 **          * is not a join
103102 **
103103 **        The parent and sub-query may contain WHERE clauses. Subject to
103104 **        rules (11), (13) and (14), they may also contain ORDER BY,
103105 **        LIMIT and OFFSET clauses.  The subquery cannot use any compound
103106 **        operator other than UNION ALL because all the other compound
103107 **        operators have an implied DISTINCT which is disallowed by
103108 **        restriction (4).
103109 **
103110 **        Also, each component of the sub-query must return the same number
103111 **        of result columns. This is actually a requirement for any compound
103112 **        SELECT statement, but all the code here does is make sure that no
103113 **        such (illegal) sub-query is flattened. The caller will detect the
103114 **        syntax error and return a detailed message.
103115 **
103116 **  (18)  If the sub-query is a compound select, then all terms of the
103117 **        ORDER by clause of the parent must be simple references to
103118 **        columns of the sub-query.
103119 **
103120 **  (19)  The subquery does not use LIMIT or the outer query does not
103121 **        have a WHERE clause.
103122 **
103123 **  (20)  If the sub-query is a compound select, then it must not use
103124 **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
103125 **        somewhat by saying that the terms of the ORDER BY clause must
103126 **        appear as unmodified result columns in the outer query.  But we
103127 **        have other optimizations in mind to deal with that case.
103128 **
103129 **  (21)  The subquery does not use LIMIT or the outer query is not
103130 **        DISTINCT.  (See ticket [752e1646fc]).
103131 **
103132 **  (22)  The subquery is not a recursive CTE.
103133 **
103134 **  (23)  The parent is not a recursive CTE, or the sub-query is not a
103135 **        compound query. This restriction is because transforming the
103136 **        parent to a compound query confuses the code that handles
103137 **        recursive queries in multiSelect().
103138 **
103139 **
103140 ** In this routine, the "p" parameter is a pointer to the outer query.
103141 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
103142 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
103143 **
103144 ** If flattening is not attempted, this routine is a no-op and returns 0.
103145 ** If flattening is attempted this routine returns 1.
103146 **
103147 ** All of the expression analysis must occur on both the outer query and
103148 ** the subquery before this routine runs.
103149 */
103150 static int flattenSubquery(
103151   Parse *pParse,       /* Parsing context */
103152   Select *p,           /* The parent or outer SELECT statement */
103153   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
103154   int isAgg,           /* True if outer SELECT uses aggregate functions */
103155   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
103156 ){
103157   const char *zSavedAuthContext = pParse->zAuthContext;
103158   Select *pParent;
103159   Select *pSub;       /* The inner query or "subquery" */
103160   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
103161   SrcList *pSrc;      /* The FROM clause of the outer query */
103162   SrcList *pSubSrc;   /* The FROM clause of the subquery */
103163   ExprList *pList;    /* The result set of the outer query */
103164   int iParent;        /* VDBE cursor number of the pSub result set temp table */
103165   int i;              /* Loop counter */
103166   Expr *pWhere;                    /* The WHERE clause */
103167   struct SrcList_item *pSubitem;   /* The subquery */
103168   sqlite3 *db = pParse->db;
103169 
103170   /* Check to see if flattening is permitted.  Return 0 if not.
103171   */
103172   assert( p!=0 );
103173   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
103174   if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
103175   pSrc = p->pSrc;
103176   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
103177   pSubitem = &pSrc->a[iFrom];
103178   iParent = pSubitem->iCursor;
103179   pSub = pSubitem->pSelect;
103180   assert( pSub!=0 );
103181   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
103182   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
103183   pSubSrc = pSub->pSrc;
103184   assert( pSubSrc );
103185   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
103186   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
103187   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
103188   ** became arbitrary expressions, we were forced to add restrictions (13)
103189   ** and (14). */
103190   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
103191   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
103192   if( (p->selFlags & SF_Compound)!=0 && pSub->pLimit ){
103193     return 0;                                            /* Restriction (15) */
103194   }
103195   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
103196   if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
103197   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
103198      return 0;         /* Restrictions (8)(9) */
103199   }
103200   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
103201      return 0;         /* Restriction (6)  */
103202   }
103203   if( p->pOrderBy && pSub->pOrderBy ){
103204      return 0;                                           /* Restriction (11) */
103205   }
103206   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
103207   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
103208   if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
103209      return 0;         /* Restriction (21) */
103210   }
103211   if( pSub->selFlags & SF_Recursive ) return 0;          /* Restriction (22)  */
103212   if( (p->selFlags & SF_Recursive) && pSub->pPrior ) return 0;       /* (23)  */
103213 
103214   /* OBSOLETE COMMENT 1:
103215   ** Restriction 3:  If the subquery is a join, make sure the subquery is
103216   ** not used as the right operand of an outer join.  Examples of why this
103217   ** is not allowed:
103218   **
103219   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
103220   **
103221   ** If we flatten the above, we would get
103222   **
103223   **         (t1 LEFT OUTER JOIN t2) JOIN t3
103224   **
103225   ** which is not at all the same thing.
103226   **
103227   ** OBSOLETE COMMENT 2:
103228   ** Restriction 12:  If the subquery is the right operand of a left outer
103229   ** join, make sure the subquery has no WHERE clause.
103230   ** An examples of why this is not allowed:
103231   **
103232   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
103233   **
103234   ** If we flatten the above, we would get
103235   **
103236   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
103237   **
103238   ** But the t2.x>0 test will always fail on a NULL row of t2, which
103239   ** effectively converts the OUTER JOIN into an INNER JOIN.
103240   **
103241   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
103242   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
103243   ** is fraught with danger.  Best to avoid the whole thing.  If the
103244   ** subquery is the right term of a LEFT JOIN, then do not flatten.
103245   */
103246   if( (pSubitem->jointype & JT_OUTER)!=0 ){
103247     return 0;
103248   }
103249 
103250   /* Restriction 17: If the sub-query is a compound SELECT, then it must
103251   ** use only the UNION ALL operator. And none of the simple select queries
103252   ** that make up the compound SELECT are allowed to be aggregate or distinct
103253   ** queries.
103254   */
103255   if( pSub->pPrior ){
103256     if( pSub->pOrderBy ){
103257       return 0;  /* Restriction 20 */
103258     }
103259     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
103260       return 0;
103261     }
103262     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
103263       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
103264       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
103265       assert( pSub->pSrc!=0 );
103266       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
103267        || (pSub1->pPrior && pSub1->op!=TK_ALL)
103268        || pSub1->pSrc->nSrc<1
103269        || pSub->pEList->nExpr!=pSub1->pEList->nExpr
103270       ){
103271         return 0;
103272       }
103273       testcase( pSub1->pSrc->nSrc>1 );
103274     }
103275 
103276     /* Restriction 18. */
103277     if( p->pOrderBy ){
103278       int ii;
103279       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
103280         if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
103281       }
103282     }
103283   }
103284 
103285   /***** If we reach this point, flattening is permitted. *****/
103286 
103287   /* Authorize the subquery */
103288   pParse->zAuthContext = pSubitem->zName;
103289   TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
103290   testcase( i==SQLITE_DENY );
103291   pParse->zAuthContext = zSavedAuthContext;
103292 
103293   /* If the sub-query is a compound SELECT statement, then (by restrictions
103294   ** 17 and 18 above) it must be a UNION ALL and the parent query must
103295   ** be of the form:
103296   **
103297   **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
103298   **
103299   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
103300   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
103301   ** OFFSET clauses and joins them to the left-hand-side of the original
103302   ** using UNION ALL operators. In this case N is the number of simple
103303   ** select statements in the compound sub-query.
103304   **
103305   ** Example:
103306   **
103307   **     SELECT a+1 FROM (
103308   **        SELECT x FROM tab
103309   **        UNION ALL
103310   **        SELECT y FROM tab
103311   **        UNION ALL
103312   **        SELECT abs(z*2) FROM tab2
103313   **     ) WHERE a!=5 ORDER BY 1
103314   **
103315   ** Transformed into:
103316   **
103317   **     SELECT x+1 FROM tab WHERE x+1!=5
103318   **     UNION ALL
103319   **     SELECT y+1 FROM tab WHERE y+1!=5
103320   **     UNION ALL
103321   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
103322   **     ORDER BY 1
103323   **
103324   ** We call this the "compound-subquery flattening".
103325   */
103326   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
103327     Select *pNew;
103328     ExprList *pOrderBy = p->pOrderBy;
103329     Expr *pLimit = p->pLimit;
103330     Expr *pOffset = p->pOffset;
103331     Select *pPrior = p->pPrior;
103332     p->pOrderBy = 0;
103333     p->pSrc = 0;
103334     p->pPrior = 0;
103335     p->pLimit = 0;
103336     p->pOffset = 0;
103337     pNew = sqlite3SelectDup(db, p, 0);
103338     p->pOffset = pOffset;
103339     p->pLimit = pLimit;
103340     p->pOrderBy = pOrderBy;
103341     p->pSrc = pSrc;
103342     p->op = TK_ALL;
103343     if( pNew==0 ){
103344       p->pPrior = pPrior;
103345     }else{
103346       pNew->pPrior = pPrior;
103347       if( pPrior ) pPrior->pNext = pNew;
103348       pNew->pNext = p;
103349       p->pPrior = pNew;
103350     }
103351     if( db->mallocFailed ) return 1;
103352   }
103353 
103354   /* Begin flattening the iFrom-th entry of the FROM clause
103355   ** in the outer query.
103356   */
103357   pSub = pSub1 = pSubitem->pSelect;
103358 
103359   /* Delete the transient table structure associated with the
103360   ** subquery
103361   */
103362   sqlite3DbFree(db, pSubitem->zDatabase);
103363   sqlite3DbFree(db, pSubitem->zName);
103364   sqlite3DbFree(db, pSubitem->zAlias);
103365   pSubitem->zDatabase = 0;
103366   pSubitem->zName = 0;
103367   pSubitem->zAlias = 0;
103368   pSubitem->pSelect = 0;
103369 
103370   /* Defer deleting the Table object associated with the
103371   ** subquery until code generation is
103372   ** complete, since there may still exist Expr.pTab entries that
103373   ** refer to the subquery even after flattening.  Ticket #3346.
103374   **
103375   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
103376   */
103377   if( ALWAYS(pSubitem->pTab!=0) ){
103378     Table *pTabToDel = pSubitem->pTab;
103379     if( pTabToDel->nRef==1 ){
103380       Parse *pToplevel = sqlite3ParseToplevel(pParse);
103381       pTabToDel->pNextZombie = pToplevel->pZombieTab;
103382       pToplevel->pZombieTab = pTabToDel;
103383     }else{
103384       pTabToDel->nRef--;
103385     }
103386     pSubitem->pTab = 0;
103387   }
103388 
103389   /* The following loop runs once for each term in a compound-subquery
103390   ** flattening (as described above).  If we are doing a different kind
103391   ** of flattening - a flattening other than a compound-subquery flattening -
103392   ** then this loop only runs once.
103393   **
103394   ** This loop moves all of the FROM elements of the subquery into the
103395   ** the FROM clause of the outer query.  Before doing this, remember
103396   ** the cursor number for the original outer query FROM element in
103397   ** iParent.  The iParent cursor will never be used.  Subsequent code
103398   ** will scan expressions looking for iParent references and replace
103399   ** those references with expressions that resolve to the subquery FROM
103400   ** elements we are now copying in.
103401   */
103402   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
103403     int nSubSrc;
103404     u8 jointype = 0;
103405     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
103406     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
103407     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
103408 
103409     if( pSrc ){
103410       assert( pParent==p );  /* First time through the loop */
103411       jointype = pSubitem->jointype;
103412     }else{
103413       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
103414       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
103415       if( pSrc==0 ){
103416         assert( db->mallocFailed );
103417         break;
103418       }
103419     }
103420 
103421     /* The subquery uses a single slot of the FROM clause of the outer
103422     ** query.  If the subquery has more than one element in its FROM clause,
103423     ** then expand the outer query to make space for it to hold all elements
103424     ** of the subquery.
103425     **
103426     ** Example:
103427     **
103428     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
103429     **
103430     ** The outer query has 3 slots in its FROM clause.  One slot of the
103431     ** outer query (the middle slot) is used by the subquery.  The next
103432     ** block of code will expand the out query to 4 slots.  The middle
103433     ** slot is expanded to two slots in order to make space for the
103434     ** two elements in the FROM clause of the subquery.
103435     */
103436     if( nSubSrc>1 ){
103437       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
103438       if( db->mallocFailed ){
103439         break;
103440       }
103441     }
103442 
103443     /* Transfer the FROM clause terms from the subquery into the
103444     ** outer query.
103445     */
103446     for(i=0; i<nSubSrc; i++){
103447       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
103448       pSrc->a[i+iFrom] = pSubSrc->a[i];
103449       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
103450     }
103451     pSrc->a[iFrom].jointype = jointype;
103452 
103453     /* Now begin substituting subquery result set expressions for
103454     ** references to the iParent in the outer query.
103455     **
103456     ** Example:
103457     **
103458     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
103459     **   \                     \_____________ subquery __________/          /
103460     **    \_____________________ outer query ______________________________/
103461     **
103462     ** We look at every expression in the outer query and every place we see
103463     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
103464     */
103465     pList = pParent->pEList;
103466     for(i=0; i<pList->nExpr; i++){
103467       if( pList->a[i].zName==0 ){
103468         char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
103469         sqlite3Dequote(zName);
103470         pList->a[i].zName = zName;
103471       }
103472     }
103473     substExprList(db, pParent->pEList, iParent, pSub->pEList);
103474     if( isAgg ){
103475       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
103476       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
103477     }
103478     if( pSub->pOrderBy ){
103479       assert( pParent->pOrderBy==0 );
103480       pParent->pOrderBy = pSub->pOrderBy;
103481       pSub->pOrderBy = 0;
103482     }else if( pParent->pOrderBy ){
103483       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
103484     }
103485     if( pSub->pWhere ){
103486       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
103487     }else{
103488       pWhere = 0;
103489     }
103490     if( subqueryIsAgg ){
103491       assert( pParent->pHaving==0 );
103492       pParent->pHaving = pParent->pWhere;
103493       pParent->pWhere = pWhere;
103494       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
103495       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
103496                                   sqlite3ExprDup(db, pSub->pHaving, 0));
103497       assert( pParent->pGroupBy==0 );
103498       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
103499     }else{
103500       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
103501       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
103502     }
103503 
103504     /* The flattened query is distinct if either the inner or the
103505     ** outer query is distinct.
103506     */
103507     pParent->selFlags |= pSub->selFlags & SF_Distinct;
103508 
103509     /*
103510     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
103511     **
103512     ** One is tempted to try to add a and b to combine the limits.  But this
103513     ** does not work if either limit is negative.
103514     */
103515     if( pSub->pLimit ){
103516       pParent->pLimit = pSub->pLimit;
103517       pSub->pLimit = 0;
103518     }
103519   }
103520 
103521   /* Finially, delete what is left of the subquery and return
103522   ** success.
103523   */
103524   sqlite3SelectDelete(db, pSub1);
103525 
103526   return 1;
103527 }
103528 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
103529 
103530 /*
103531 ** Based on the contents of the AggInfo structure indicated by the first
103532 ** argument, this function checks if the following are true:
103533 **
103534 **    * the query contains just a single aggregate function,
103535 **    * the aggregate function is either min() or max(), and
103536 **    * the argument to the aggregate function is a column value.
103537 **
103538 ** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
103539 ** is returned as appropriate. Also, *ppMinMax is set to point to the
103540 ** list of arguments passed to the aggregate before returning.
103541 **
103542 ** Or, if the conditions above are not met, *ppMinMax is set to 0 and
103543 ** WHERE_ORDERBY_NORMAL is returned.
103544 */
103545 static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
103546   int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
103547 
103548   *ppMinMax = 0;
103549   if( pAggInfo->nFunc==1 ){
103550     Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
103551     ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
103552 
103553     assert( pExpr->op==TK_AGG_FUNCTION );
103554     if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
103555       const char *zFunc = pExpr->u.zToken;
103556       if( sqlite3StrICmp(zFunc, "min")==0 ){
103557         eRet = WHERE_ORDERBY_MIN;
103558         *ppMinMax = pEList;
103559       }else if( sqlite3StrICmp(zFunc, "max")==0 ){
103560         eRet = WHERE_ORDERBY_MAX;
103561         *ppMinMax = pEList;
103562       }
103563     }
103564   }
103565 
103566   assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
103567   return eRet;
103568 }
103569 
103570 /*
103571 ** The select statement passed as the first argument is an aggregate query.
103572 ** The second argment is the associated aggregate-info object. This
103573 ** function tests if the SELECT is of the form:
103574 **
103575 **   SELECT count(*) FROM <tbl>
103576 **
103577 ** where table is a database table, not a sub-select or view. If the query
103578 ** does match this pattern, then a pointer to the Table object representing
103579 ** <tbl> is returned. Otherwise, 0 is returned.
103580 */
103581 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
103582   Table *pTab;
103583   Expr *pExpr;
103584 
103585   assert( !p->pGroupBy );
103586 
103587   if( p->pWhere || p->pEList->nExpr!=1
103588    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
103589   ){
103590     return 0;
103591   }
103592   pTab = p->pSrc->a[0].pTab;
103593   pExpr = p->pEList->a[0].pExpr;
103594   assert( pTab && !pTab->pSelect && pExpr );
103595 
103596   if( IsVirtual(pTab) ) return 0;
103597   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
103598   if( NEVER(pAggInfo->nFunc==0) ) return 0;
103599   if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
103600   if( pExpr->flags&EP_Distinct ) return 0;
103601 
103602   return pTab;
103603 }
103604 
103605 /*
103606 ** If the source-list item passed as an argument was augmented with an
103607 ** INDEXED BY clause, then try to locate the specified index. If there
103608 ** was such a clause and the named index cannot be found, return
103609 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
103610 ** pFrom->pIndex and return SQLITE_OK.
103611 */
103612 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
103613   if( pFrom->pTab && pFrom->zIndex ){
103614     Table *pTab = pFrom->pTab;
103615     char *zIndex = pFrom->zIndex;
103616     Index *pIdx;
103617     for(pIdx=pTab->pIndex;
103618         pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
103619         pIdx=pIdx->pNext
103620     );
103621     if( !pIdx ){
103622       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
103623       pParse->checkSchema = 1;
103624       return SQLITE_ERROR;
103625     }
103626     pFrom->pIndex = pIdx;
103627   }
103628   return SQLITE_OK;
103629 }
103630 /*
103631 ** Detect compound SELECT statements that use an ORDER BY clause with
103632 ** an alternative collating sequence.
103633 **
103634 **    SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
103635 **
103636 ** These are rewritten as a subquery:
103637 **
103638 **    SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
103639 **     ORDER BY ... COLLATE ...
103640 **
103641 ** This transformation is necessary because the multiSelectOrderBy() routine
103642 ** above that generates the code for a compound SELECT with an ORDER BY clause
103643 ** uses a merge algorithm that requires the same collating sequence on the
103644 ** result columns as on the ORDER BY clause.  See ticket
103645 ** http://www.sqlite.org/src/info/6709574d2a
103646 **
103647 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
103648 ** The UNION ALL operator works fine with multiSelectOrderBy() even when
103649 ** there are COLLATE terms in the ORDER BY.
103650 */
103651 static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
103652   int i;
103653   Select *pNew;
103654   Select *pX;
103655   sqlite3 *db;
103656   struct ExprList_item *a;
103657   SrcList *pNewSrc;
103658   Parse *pParse;
103659   Token dummy;
103660 
103661   if( p->pPrior==0 ) return WRC_Continue;
103662   if( p->pOrderBy==0 ) return WRC_Continue;
103663   for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
103664   if( pX==0 ) return WRC_Continue;
103665   a = p->pOrderBy->a;
103666   for(i=p->pOrderBy->nExpr-1; i>=0; i--){
103667     if( a[i].pExpr->flags & EP_Collate ) break;
103668   }
103669   if( i<0 ) return WRC_Continue;
103670 
103671   /* If we reach this point, that means the transformation is required. */
103672 
103673   pParse = pWalker->pParse;
103674   db = pParse->db;
103675   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
103676   if( pNew==0 ) return WRC_Abort;
103677   memset(&dummy, 0, sizeof(dummy));
103678   pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
103679   if( pNewSrc==0 ) return WRC_Abort;
103680   *pNew = *p;
103681   p->pSrc = pNewSrc;
103682   p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
103683   p->op = TK_SELECT;
103684   p->pWhere = 0;
103685   pNew->pGroupBy = 0;
103686   pNew->pHaving = 0;
103687   pNew->pOrderBy = 0;
103688   p->pPrior = 0;
103689   p->pNext = 0;
103690   p->selFlags &= ~SF_Compound;
103691   assert( pNew->pPrior!=0 );
103692   pNew->pPrior->pNext = pNew;
103693   pNew->pLimit = 0;
103694   pNew->pOffset = 0;
103695   return WRC_Continue;
103696 }
103697 
103698 #ifndef SQLITE_OMIT_CTE
103699 /*
103700 ** Argument pWith (which may be NULL) points to a linked list of nested
103701 ** WITH contexts, from inner to outermost. If the table identified by
103702 ** FROM clause element pItem is really a common-table-expression (CTE)
103703 ** then return a pointer to the CTE definition for that table. Otherwise
103704 ** return NULL.
103705 **
103706 ** If a non-NULL value is returned, set *ppContext to point to the With
103707 ** object that the returned CTE belongs to.
103708 */
103709 static struct Cte *searchWith(
103710   With *pWith,                    /* Current outermost WITH clause */
103711   struct SrcList_item *pItem,     /* FROM clause element to resolve */
103712   With **ppContext                /* OUT: WITH clause return value belongs to */
103713 ){
103714   const char *zName;
103715   if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
103716     With *p;
103717     for(p=pWith; p; p=p->pOuter){
103718       int i;
103719       for(i=0; i<p->nCte; i++){
103720         if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
103721           *ppContext = p;
103722           return &p->a[i];
103723         }
103724       }
103725     }
103726   }
103727   return 0;
103728 }
103729 
103730 /* The code generator maintains a stack of active WITH clauses
103731 ** with the inner-most WITH clause being at the top of the stack.
103732 **
103733 ** This routine pushes the WITH clause passed as the second argument
103734 ** onto the top of the stack. If argument bFree is true, then this
103735 ** WITH clause will never be popped from the stack. In this case it
103736 ** should be freed along with the Parse object. In other cases, when
103737 ** bFree==0, the With object will be freed along with the SELECT
103738 ** statement with which it is associated.
103739 */
103740 SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
103741   assert( bFree==0 || pParse->pWith==0 );
103742   if( pWith ){
103743     pWith->pOuter = pParse->pWith;
103744     pParse->pWith = pWith;
103745     pParse->bFreeWith = bFree;
103746   }
103747 }
103748 
103749 /*
103750 ** This function checks if argument pFrom refers to a CTE declared by
103751 ** a WITH clause on the stack currently maintained by the parser. And,
103752 ** if currently processing a CTE expression, if it is a recursive
103753 ** reference to the current CTE.
103754 **
103755 ** If pFrom falls into either of the two categories above, pFrom->pTab
103756 ** and other fields are populated accordingly. The caller should check
103757 ** (pFrom->pTab!=0) to determine whether or not a successful match
103758 ** was found.
103759 **
103760 ** Whether or not a match is found, SQLITE_OK is returned if no error
103761 ** occurs. If an error does occur, an error message is stored in the
103762 ** parser and some error code other than SQLITE_OK returned.
103763 */
103764 static int withExpand(
103765   Walker *pWalker,
103766   struct SrcList_item *pFrom
103767 ){
103768   Parse *pParse = pWalker->pParse;
103769   sqlite3 *db = pParse->db;
103770   struct Cte *pCte;               /* Matched CTE (or NULL if no match) */
103771   With *pWith;                    /* WITH clause that pCte belongs to */
103772 
103773   assert( pFrom->pTab==0 );
103774 
103775   pCte = searchWith(pParse->pWith, pFrom, &pWith);
103776   if( pCte ){
103777     Table *pTab;
103778     ExprList *pEList;
103779     Select *pSel;
103780     Select *pLeft;                /* Left-most SELECT statement */
103781     int bMayRecursive;            /* True if compound joined by UNION [ALL] */
103782     With *pSavedWith;             /* Initial value of pParse->pWith */
103783 
103784     /* If pCte->zErr is non-NULL at this point, then this is an illegal
103785     ** recursive reference to CTE pCte. Leave an error in pParse and return
103786     ** early. If pCte->zErr is NULL, then this is not a recursive reference.
103787     ** In this case, proceed.  */
103788     if( pCte->zErr ){
103789       sqlite3ErrorMsg(pParse, pCte->zErr, pCte->zName);
103790       return SQLITE_ERROR;
103791     }
103792 
103793     assert( pFrom->pTab==0 );
103794     pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
103795     if( pTab==0 ) return WRC_Abort;
103796     pTab->nRef = 1;
103797     pTab->zName = sqlite3DbStrDup(db, pCte->zName);
103798     pTab->iPKey = -1;
103799     pTab->nRowEst = 1048576;
103800     pTab->tabFlags |= TF_Ephemeral;
103801     pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
103802     if( db->mallocFailed ) return SQLITE_NOMEM;
103803     assert( pFrom->pSelect );
103804 
103805     /* Check if this is a recursive CTE. */
103806     pSel = pFrom->pSelect;
103807     bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
103808     if( bMayRecursive ){
103809       int i;
103810       SrcList *pSrc = pFrom->pSelect->pSrc;
103811       for(i=0; i<pSrc->nSrc; i++){
103812         struct SrcList_item *pItem = &pSrc->a[i];
103813         if( pItem->zDatabase==0
103814          && pItem->zName!=0
103815          && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
103816           ){
103817           pItem->pTab = pTab;
103818           pItem->isRecursive = 1;
103819           pTab->nRef++;
103820           pSel->selFlags |= SF_Recursive;
103821         }
103822       }
103823     }
103824 
103825     /* Only one recursive reference is permitted. */
103826     if( pTab->nRef>2 ){
103827       sqlite3ErrorMsg(
103828           pParse, "multiple references to recursive table: %s", pCte->zName
103829       );
103830       return SQLITE_ERROR;
103831     }
103832     assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 ));
103833 
103834     pCte->zErr = "circular reference: %s";
103835     pSavedWith = pParse->pWith;
103836     pParse->pWith = pWith;
103837     sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
103838 
103839     for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
103840     pEList = pLeft->pEList;
103841     if( pCte->pCols ){
103842       if( pEList->nExpr!=pCte->pCols->nExpr ){
103843         sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
103844             pCte->zName, pEList->nExpr, pCte->pCols->nExpr
103845         );
103846         pParse->pWith = pSavedWith;
103847         return SQLITE_ERROR;
103848       }
103849       pEList = pCte->pCols;
103850     }
103851 
103852     selectColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
103853     if( bMayRecursive ){
103854       if( pSel->selFlags & SF_Recursive ){
103855         pCte->zErr = "multiple recursive references: %s";
103856       }else{
103857         pCte->zErr = "recursive reference in a subquery: %s";
103858       }
103859       sqlite3WalkSelect(pWalker, pSel);
103860     }
103861     pCte->zErr = 0;
103862     pParse->pWith = pSavedWith;
103863   }
103864 
103865   return SQLITE_OK;
103866 }
103867 #endif
103868 
103869 #ifndef SQLITE_OMIT_CTE
103870 /*
103871 ** If the SELECT passed as the second argument has an associated WITH
103872 ** clause, pop it from the stack stored as part of the Parse object.
103873 **
103874 ** This function is used as the xSelectCallback2() callback by
103875 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
103876 ** names and other FROM clause elements.
103877 */
103878 static void selectPopWith(Walker *pWalker, Select *p){
103879   Parse *pParse = pWalker->pParse;
103880   With *pWith = findRightmost(p)->pWith;
103881   if( pWith!=0 ){
103882     assert( pParse->pWith==pWith );
103883     pParse->pWith = pWith->pOuter;
103884   }
103885 }
103886 #else
103887 #define selectPopWith 0
103888 #endif
103889 
103890 /*
103891 ** This routine is a Walker callback for "expanding" a SELECT statement.
103892 ** "Expanding" means to do the following:
103893 **
103894 **    (1)  Make sure VDBE cursor numbers have been assigned to every
103895 **         element of the FROM clause.
103896 **
103897 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
103898 **         defines FROM clause.  When views appear in the FROM clause,
103899 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
103900 **         that implements the view.  A copy is made of the view's SELECT
103901 **         statement so that we can freely modify or delete that statement
103902 **         without worrying about messing up the presistent representation
103903 **         of the view.
103904 **
103905 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
103906 **         on joins and the ON and USING clause of joins.
103907 **
103908 **    (4)  Scan the list of columns in the result set (pEList) looking
103909 **         for instances of the "*" operator or the TABLE.* operator.
103910 **         If found, expand each "*" to be every column in every table
103911 **         and TABLE.* to be every column in TABLE.
103912 **
103913 */
103914 static int selectExpander(Walker *pWalker, Select *p){
103915   Parse *pParse = pWalker->pParse;
103916   int i, j, k;
103917   SrcList *pTabList;
103918   ExprList *pEList;
103919   struct SrcList_item *pFrom;
103920   sqlite3 *db = pParse->db;
103921   Expr *pE, *pRight, *pExpr;
103922   u16 selFlags = p->selFlags;
103923 
103924   p->selFlags |= SF_Expanded;
103925   if( db->mallocFailed  ){
103926     return WRC_Abort;
103927   }
103928   if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
103929     return WRC_Prune;
103930   }
103931   pTabList = p->pSrc;
103932   pEList = p->pEList;
103933   sqlite3WithPush(pParse, findRightmost(p)->pWith, 0);
103934 
103935   /* Make sure cursor numbers have been assigned to all entries in
103936   ** the FROM clause of the SELECT statement.
103937   */
103938   sqlite3SrcListAssignCursors(pParse, pTabList);
103939 
103940   /* Look up every table named in the FROM clause of the select.  If
103941   ** an entry of the FROM clause is a subquery instead of a table or view,
103942   ** then create a transient table structure to describe the subquery.
103943   */
103944   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
103945     Table *pTab;
103946     assert( pFrom->isRecursive==0 || pFrom->pTab );
103947     if( pFrom->isRecursive ) continue;
103948     if( pFrom->pTab!=0 ){
103949       /* This statement has already been prepared.  There is no need
103950       ** to go further. */
103951       assert( i==0 );
103952 #ifndef SQLITE_OMIT_CTE
103953       selectPopWith(pWalker, p);
103954 #endif
103955       return WRC_Prune;
103956     }
103957 #ifndef SQLITE_OMIT_CTE
103958     if( withExpand(pWalker, pFrom) ) return WRC_Abort;
103959     if( pFrom->pTab ) {} else
103960 #endif
103961     if( pFrom->zName==0 ){
103962 #ifndef SQLITE_OMIT_SUBQUERY
103963       Select *pSel = pFrom->pSelect;
103964       /* A sub-query in the FROM clause of a SELECT */
103965       assert( pSel!=0 );
103966       assert( pFrom->pTab==0 );
103967       sqlite3WalkSelect(pWalker, pSel);
103968       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
103969       if( pTab==0 ) return WRC_Abort;
103970       pTab->nRef = 1;
103971       pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
103972       while( pSel->pPrior ){ pSel = pSel->pPrior; }
103973       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
103974       pTab->iPKey = -1;
103975       pTab->nRowEst = 1048576;
103976       pTab->tabFlags |= TF_Ephemeral;
103977 #endif
103978     }else{
103979       /* An ordinary table or view name in the FROM clause */
103980       assert( pFrom->pTab==0 );
103981       pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
103982       if( pTab==0 ) return WRC_Abort;
103983       if( pTab->nRef==0xffff ){
103984         sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
103985            pTab->zName);
103986         pFrom->pTab = 0;
103987         return WRC_Abort;
103988       }
103989       pTab->nRef++;
103990 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
103991       if( pTab->pSelect || IsVirtual(pTab) ){
103992         /* We reach here if the named table is a really a view */
103993         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
103994         assert( pFrom->pSelect==0 );
103995         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
103996         sqlite3WalkSelect(pWalker, pFrom->pSelect);
103997       }
103998 #endif
103999     }
104000 
104001     /* Locate the index named by the INDEXED BY clause, if any. */
104002     if( sqlite3IndexedByLookup(pParse, pFrom) ){
104003       return WRC_Abort;
104004     }
104005   }
104006 
104007   /* Process NATURAL keywords, and ON and USING clauses of joins.
104008   */
104009   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
104010     return WRC_Abort;
104011   }
104012 
104013   /* For every "*" that occurs in the column list, insert the names of
104014   ** all columns in all tables.  And for every TABLE.* insert the names
104015   ** of all columns in TABLE.  The parser inserted a special expression
104016   ** with the TK_ALL operator for each "*" that it found in the column list.
104017   ** The following code just has to locate the TK_ALL expressions and expand
104018   ** each one to the list of all columns in all tables.
104019   **
104020   ** The first loop just checks to see if there are any "*" operators
104021   ** that need expanding.
104022   */
104023   for(k=0; k<pEList->nExpr; k++){
104024     pE = pEList->a[k].pExpr;
104025     if( pE->op==TK_ALL ) break;
104026     assert( pE->op!=TK_DOT || pE->pRight!=0 );
104027     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
104028     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
104029   }
104030   if( k<pEList->nExpr ){
104031     /*
104032     ** If we get here it means the result set contains one or more "*"
104033     ** operators that need to be expanded.  Loop through each expression
104034     ** in the result set and expand them one by one.
104035     */
104036     struct ExprList_item *a = pEList->a;
104037     ExprList *pNew = 0;
104038     int flags = pParse->db->flags;
104039     int longNames = (flags & SQLITE_FullColNames)!=0
104040                       && (flags & SQLITE_ShortColNames)==0;
104041 
104042     /* When processing FROM-clause subqueries, it is always the case
104043     ** that full_column_names=OFF and short_column_names=ON.  The
104044     ** sqlite3ResultSetOfSelect() routine makes it so. */
104045     assert( (p->selFlags & SF_NestedFrom)==0
104046           || ((flags & SQLITE_FullColNames)==0 &&
104047               (flags & SQLITE_ShortColNames)!=0) );
104048 
104049     for(k=0; k<pEList->nExpr; k++){
104050       pE = a[k].pExpr;
104051       pRight = pE->pRight;
104052       assert( pE->op!=TK_DOT || pRight!=0 );
104053       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
104054         /* This particular expression does not need to be expanded.
104055         */
104056         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
104057         if( pNew ){
104058           pNew->a[pNew->nExpr-1].zName = a[k].zName;
104059           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
104060           a[k].zName = 0;
104061           a[k].zSpan = 0;
104062         }
104063         a[k].pExpr = 0;
104064       }else{
104065         /* This expression is a "*" or a "TABLE.*" and needs to be
104066         ** expanded. */
104067         int tableSeen = 0;      /* Set to 1 when TABLE matches */
104068         char *zTName = 0;       /* text of name of TABLE */
104069         if( pE->op==TK_DOT ){
104070           assert( pE->pLeft!=0 );
104071           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
104072           zTName = pE->pLeft->u.zToken;
104073         }
104074         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
104075           Table *pTab = pFrom->pTab;
104076           Select *pSub = pFrom->pSelect;
104077           char *zTabName = pFrom->zAlias;
104078           const char *zSchemaName = 0;
104079           int iDb;
104080           if( zTabName==0 ){
104081             zTabName = pTab->zName;
104082           }
104083           if( db->mallocFailed ) break;
104084           if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
104085             pSub = 0;
104086             if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
104087               continue;
104088             }
104089             iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
104090             zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
104091           }
104092           for(j=0; j<pTab->nCol; j++){
104093             char *zName = pTab->aCol[j].zName;
104094             char *zColname;  /* The computed column name */
104095             char *zToFree;   /* Malloced string that needs to be freed */
104096             Token sColname;  /* Computed column name as a token */
104097 
104098             assert( zName );
104099             if( zTName && pSub
104100              && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
104101             ){
104102               continue;
104103             }
104104 
104105             /* If a column is marked as 'hidden' (currently only possible
104106             ** for virtual tables), do not include it in the expanded
104107             ** result-set list.
104108             */
104109             if( IsHiddenColumn(&pTab->aCol[j]) ){
104110               assert(IsVirtual(pTab));
104111               continue;
104112             }
104113             tableSeen = 1;
104114 
104115             if( i>0 && zTName==0 ){
104116               if( (pFrom->jointype & JT_NATURAL)!=0
104117                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
104118               ){
104119                 /* In a NATURAL join, omit the join columns from the
104120                 ** table to the right of the join */
104121                 continue;
104122               }
104123               if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
104124                 /* In a join with a USING clause, omit columns in the
104125                 ** using clause from the table on the right. */
104126                 continue;
104127               }
104128             }
104129             pRight = sqlite3Expr(db, TK_ID, zName);
104130             zColname = zName;
104131             zToFree = 0;
104132             if( longNames || pTabList->nSrc>1 ){
104133               Expr *pLeft;
104134               pLeft = sqlite3Expr(db, TK_ID, zTabName);
104135               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
104136               if( zSchemaName ){
104137                 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
104138                 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
104139               }
104140               if( longNames ){
104141                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
104142                 zToFree = zColname;
104143               }
104144             }else{
104145               pExpr = pRight;
104146             }
104147             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
104148             sColname.z = zColname;
104149             sColname.n = sqlite3Strlen30(zColname);
104150             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
104151             if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
104152               struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
104153               if( pSub ){
104154                 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
104155                 testcase( pX->zSpan==0 );
104156               }else{
104157                 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
104158                                            zSchemaName, zTabName, zColname);
104159                 testcase( pX->zSpan==0 );
104160               }
104161               pX->bSpanIsTab = 1;
104162             }
104163             sqlite3DbFree(db, zToFree);
104164           }
104165         }
104166         if( !tableSeen ){
104167           if( zTName ){
104168             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
104169           }else{
104170             sqlite3ErrorMsg(pParse, "no tables specified");
104171           }
104172         }
104173       }
104174     }
104175     sqlite3ExprListDelete(db, pEList);
104176     p->pEList = pNew;
104177   }
104178 #if SQLITE_MAX_COLUMN
104179   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
104180     sqlite3ErrorMsg(pParse, "too many columns in result set");
104181   }
104182 #endif
104183   return WRC_Continue;
104184 }
104185 
104186 /*
104187 ** No-op routine for the parse-tree walker.
104188 **
104189 ** When this routine is the Walker.xExprCallback then expression trees
104190 ** are walked without any actions being taken at each node.  Presumably,
104191 ** when this routine is used for Walker.xExprCallback then
104192 ** Walker.xSelectCallback is set to do something useful for every
104193 ** subquery in the parser tree.
104194 */
104195 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
104196   UNUSED_PARAMETER2(NotUsed, NotUsed2);
104197   return WRC_Continue;
104198 }
104199 
104200 /*
104201 ** This routine "expands" a SELECT statement and all of its subqueries.
104202 ** For additional information on what it means to "expand" a SELECT
104203 ** statement, see the comment on the selectExpand worker callback above.
104204 **
104205 ** Expanding a SELECT statement is the first step in processing a
104206 ** SELECT statement.  The SELECT statement must be expanded before
104207 ** name resolution is performed.
104208 **
104209 ** If anything goes wrong, an error message is written into pParse.
104210 ** The calling function can detect the problem by looking at pParse->nErr
104211 ** and/or pParse->db->mallocFailed.
104212 */
104213 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
104214   Walker w;
104215   memset(&w, 0, sizeof(w));
104216   w.xExprCallback = exprWalkNoop;
104217   w.pParse = pParse;
104218   if( pParse->hasCompound ){
104219     w.xSelectCallback = convertCompoundSelectToSubquery;
104220     sqlite3WalkSelect(&w, pSelect);
104221   }
104222   w.xSelectCallback = selectExpander;
104223   w.xSelectCallback2 = selectPopWith;
104224   sqlite3WalkSelect(&w, pSelect);
104225 }
104226 
104227 
104228 #ifndef SQLITE_OMIT_SUBQUERY
104229 /*
104230 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
104231 ** interface.
104232 **
104233 ** For each FROM-clause subquery, add Column.zType and Column.zColl
104234 ** information to the Table structure that represents the result set
104235 ** of that subquery.
104236 **
104237 ** The Table structure that represents the result set was constructed
104238 ** by selectExpander() but the type and collation information was omitted
104239 ** at that point because identifiers had not yet been resolved.  This
104240 ** routine is called after identifier resolution.
104241 */
104242 static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
104243   Parse *pParse;
104244   int i;
104245   SrcList *pTabList;
104246   struct SrcList_item *pFrom;
104247 
104248   assert( p->selFlags & SF_Resolved );
104249   if( (p->selFlags & SF_HasTypeInfo)==0 ){
104250     p->selFlags |= SF_HasTypeInfo;
104251     pParse = pWalker->pParse;
104252     pTabList = p->pSrc;
104253     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
104254       Table *pTab = pFrom->pTab;
104255       if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
104256         /* A sub-query in the FROM clause of a SELECT */
104257         Select *pSel = pFrom->pSelect;
104258         if( pSel ){
104259           while( pSel->pPrior ) pSel = pSel->pPrior;
104260           selectAddColumnTypeAndCollation(pParse, pTab, pSel);
104261         }
104262       }
104263     }
104264   }
104265 }
104266 #endif
104267 
104268 
104269 /*
104270 ** This routine adds datatype and collating sequence information to
104271 ** the Table structures of all FROM-clause subqueries in a
104272 ** SELECT statement.
104273 **
104274 ** Use this routine after name resolution.
104275 */
104276 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
104277 #ifndef SQLITE_OMIT_SUBQUERY
104278   Walker w;
104279   memset(&w, 0, sizeof(w));
104280   w.xSelectCallback2 = selectAddSubqueryTypeInfo;
104281   w.xExprCallback = exprWalkNoop;
104282   w.pParse = pParse;
104283   sqlite3WalkSelect(&w, pSelect);
104284 #endif
104285 }
104286 
104287 
104288 /*
104289 ** This routine sets up a SELECT statement for processing.  The
104290 ** following is accomplished:
104291 **
104292 **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
104293 **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
104294 **     *  ON and USING clauses are shifted into WHERE statements
104295 **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
104296 **     *  Identifiers in expression are matched to tables.
104297 **
104298 ** This routine acts recursively on all subqueries within the SELECT.
104299 */
104300 SQLITE_PRIVATE void sqlite3SelectPrep(
104301   Parse *pParse,         /* The parser context */
104302   Select *p,             /* The SELECT statement being coded. */
104303   NameContext *pOuterNC  /* Name context for container */
104304 ){
104305   sqlite3 *db;
104306   if( NEVER(p==0) ) return;
104307   db = pParse->db;
104308   if( db->mallocFailed ) return;
104309   if( p->selFlags & SF_HasTypeInfo ) return;
104310   sqlite3SelectExpand(pParse, p);
104311   if( pParse->nErr || db->mallocFailed ) return;
104312   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
104313   if( pParse->nErr || db->mallocFailed ) return;
104314   sqlite3SelectAddTypeInfo(pParse, p);
104315 }
104316 
104317 /*
104318 ** Reset the aggregate accumulator.
104319 **
104320 ** The aggregate accumulator is a set of memory cells that hold
104321 ** intermediate results while calculating an aggregate.  This
104322 ** routine generates code that stores NULLs in all of those memory
104323 ** cells.
104324 */
104325 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
104326   Vdbe *v = pParse->pVdbe;
104327   int i;
104328   struct AggInfo_func *pFunc;
104329   int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
104330   if( nReg==0 ) return;
104331 #ifdef SQLITE_DEBUG
104332   /* Verify that all AggInfo registers are within the range specified by
104333   ** AggInfo.mnReg..AggInfo.mxReg */
104334   assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
104335   for(i=0; i<pAggInfo->nColumn; i++){
104336     assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
104337          && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
104338   }
104339   for(i=0; i<pAggInfo->nFunc; i++){
104340     assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
104341          && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
104342   }
104343 #endif
104344   sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
104345   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
104346     if( pFunc->iDistinct>=0 ){
104347       Expr *pE = pFunc->pExpr;
104348       assert( !ExprHasProperty(pE, EP_xIsSelect) );
104349       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
104350         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
104351            "argument");
104352         pFunc->iDistinct = -1;
104353       }else{
104354         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0);
104355         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
104356                           (char*)pKeyInfo, P4_KEYINFO);
104357       }
104358     }
104359   }
104360 }
104361 
104362 /*
104363 ** Invoke the OP_AggFinalize opcode for every aggregate function
104364 ** in the AggInfo structure.
104365 */
104366 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
104367   Vdbe *v = pParse->pVdbe;
104368   int i;
104369   struct AggInfo_func *pF;
104370   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
104371     ExprList *pList = pF->pExpr->x.pList;
104372     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
104373     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
104374                       (void*)pF->pFunc, P4_FUNCDEF);
104375   }
104376 }
104377 
104378 /*
104379 ** Update the accumulator memory cells for an aggregate based on
104380 ** the current cursor position.
104381 */
104382 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
104383   Vdbe *v = pParse->pVdbe;
104384   int i;
104385   int regHit = 0;
104386   int addrHitTest = 0;
104387   struct AggInfo_func *pF;
104388   struct AggInfo_col *pC;
104389 
104390   pAggInfo->directMode = 1;
104391   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
104392     int nArg;
104393     int addrNext = 0;
104394     int regAgg;
104395     ExprList *pList = pF->pExpr->x.pList;
104396     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
104397     if( pList ){
104398       nArg = pList->nExpr;
104399       regAgg = sqlite3GetTempRange(pParse, nArg);
104400       sqlite3ExprCodeExprList(pParse, pList, regAgg, SQLITE_ECEL_DUP);
104401     }else{
104402       nArg = 0;
104403       regAgg = 0;
104404     }
104405     if( pF->iDistinct>=0 ){
104406       addrNext = sqlite3VdbeMakeLabel(v);
104407       assert( nArg==1 );
104408       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
104409     }
104410     if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
104411       CollSeq *pColl = 0;
104412       struct ExprList_item *pItem;
104413       int j;
104414       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
104415       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
104416         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
104417       }
104418       if( !pColl ){
104419         pColl = pParse->db->pDfltColl;
104420       }
104421       if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
104422       sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
104423     }
104424     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
104425                       (void*)pF->pFunc, P4_FUNCDEF);
104426     sqlite3VdbeChangeP5(v, (u8)nArg);
104427     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
104428     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
104429     if( addrNext ){
104430       sqlite3VdbeResolveLabel(v, addrNext);
104431       sqlite3ExprCacheClear(pParse);
104432     }
104433   }
104434 
104435   /* Before populating the accumulator registers, clear the column cache.
104436   ** Otherwise, if any of the required column values are already present
104437   ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
104438   ** to pC->iMem. But by the time the value is used, the original register
104439   ** may have been used, invalidating the underlying buffer holding the
104440   ** text or blob value. See ticket [883034dcb5].
104441   **
104442   ** Another solution would be to change the OP_SCopy used to copy cached
104443   ** values to an OP_Copy.
104444   */
104445   if( regHit ){
104446     addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit); VdbeCoverage(v);
104447   }
104448   sqlite3ExprCacheClear(pParse);
104449   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
104450     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
104451   }
104452   pAggInfo->directMode = 0;
104453   sqlite3ExprCacheClear(pParse);
104454   if( addrHitTest ){
104455     sqlite3VdbeJumpHere(v, addrHitTest);
104456   }
104457 }
104458 
104459 /*
104460 ** Add a single OP_Explain instruction to the VDBE to explain a simple
104461 ** count(*) query ("SELECT count(*) FROM pTab").
104462 */
104463 #ifndef SQLITE_OMIT_EXPLAIN
104464 static void explainSimpleCount(
104465   Parse *pParse,                  /* Parse context */
104466   Table *pTab,                    /* Table being queried */
104467   Index *pIdx                     /* Index used to optimize scan, or NULL */
104468 ){
104469   if( pParse->explain==2 ){
104470     char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
104471         pTab->zName,
104472         pIdx ? " USING COVERING INDEX " : "",
104473         pIdx ? pIdx->zName : ""
104474     );
104475     sqlite3VdbeAddOp4(
104476         pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
104477     );
104478   }
104479 }
104480 #else
104481 # define explainSimpleCount(a,b,c)
104482 #endif
104483 
104484 /*
104485 ** Generate code for the SELECT statement given in the p argument.
104486 **
104487 ** The results are returned according to the SelectDest structure.
104488 ** See comments in sqliteInt.h for further information.
104489 **
104490 ** This routine returns the number of errors.  If any errors are
104491 ** encountered, then an appropriate error message is left in
104492 ** pParse->zErrMsg.
104493 **
104494 ** This routine does NOT free the Select structure passed in.  The
104495 ** calling function needs to do that.
104496 */
104497 SQLITE_PRIVATE int sqlite3Select(
104498   Parse *pParse,         /* The parser context */
104499   Select *p,             /* The SELECT statement being coded. */
104500   SelectDest *pDest      /* What to do with the query results */
104501 ){
104502   int i, j;              /* Loop counters */
104503   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
104504   Vdbe *v;               /* The virtual machine under construction */
104505   int isAgg;             /* True for select lists like "count(*)" */
104506   ExprList *pEList;      /* List of columns to extract. */
104507   SrcList *pTabList;     /* List of tables to select from */
104508   Expr *pWhere;          /* The WHERE clause.  May be NULL */
104509   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
104510   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
104511   Expr *pHaving;         /* The HAVING clause.  May be NULL */
104512   int rc = 1;            /* Value to return from this function */
104513   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
104514   DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
104515   AggInfo sAggInfo;      /* Information used by aggregate queries */
104516   int iEnd;              /* Address of the end of the query */
104517   sqlite3 *db;           /* The database connection */
104518 
104519 #ifndef SQLITE_OMIT_EXPLAIN
104520   int iRestoreSelectId = pParse->iSelectId;
104521   pParse->iSelectId = pParse->iNextSelectId++;
104522 #endif
104523 
104524   db = pParse->db;
104525   if( p==0 || db->mallocFailed || pParse->nErr ){
104526     return 1;
104527   }
104528   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
104529   memset(&sAggInfo, 0, sizeof(sAggInfo));
104530 
104531   if( IgnorableOrderby(pDest) ){
104532     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
104533            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
104534     /* If ORDER BY makes no difference in the output then neither does
104535     ** DISTINCT so it can be removed too. */
104536     sqlite3ExprListDelete(db, p->pOrderBy);
104537     p->pOrderBy = 0;
104538     p->selFlags &= ~SF_Distinct;
104539   }
104540   sqlite3SelectPrep(pParse, p, 0);
104541   pOrderBy = p->pOrderBy;
104542   pTabList = p->pSrc;
104543   pEList = p->pEList;
104544   if( pParse->nErr || db->mallocFailed ){
104545     goto select_end;
104546   }
104547   isAgg = (p->selFlags & SF_Aggregate)!=0;
104548   assert( pEList!=0 );
104549 
104550   /* Begin generating code.
104551   */
104552   v = sqlite3GetVdbe(pParse);
104553   if( v==0 ) goto select_end;
104554 
104555   /* If writing to memory or generating a set
104556   ** only a single column may be output.
104557   */
104558 #ifndef SQLITE_OMIT_SUBQUERY
104559   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
104560     goto select_end;
104561   }
104562 #endif
104563 
104564   /* Generate code for all sub-queries in the FROM clause
104565   */
104566 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
104567   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
104568     struct SrcList_item *pItem = &pTabList->a[i];
104569     SelectDest dest;
104570     Select *pSub = pItem->pSelect;
104571     int isAggSub;
104572 
104573     if( pSub==0 ) continue;
104574 
104575     /* Sometimes the code for a subquery will be generated more than
104576     ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
104577     ** for example.  In that case, do not regenerate the code to manifest
104578     ** a view or the co-routine to implement a view.  The first instance
104579     ** is sufficient, though the subroutine to manifest the view does need
104580     ** to be invoked again. */
104581     if( pItem->addrFillSub ){
104582       if( pItem->viaCoroutine==0 ){
104583         sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
104584       }
104585       continue;
104586     }
104587 
104588     /* Increment Parse.nHeight by the height of the largest expression
104589     ** tree referred to by this, the parent select. The child select
104590     ** may contain expression trees of at most
104591     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
104592     ** more conservative than necessary, but much easier than enforcing
104593     ** an exact limit.
104594     */
104595     pParse->nHeight += sqlite3SelectExprHeight(p);
104596 
104597     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
104598     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
104599       /* This subquery can be absorbed into its parent. */
104600       if( isAggSub ){
104601         isAgg = 1;
104602         p->selFlags |= SF_Aggregate;
104603       }
104604       i = -1;
104605     }else if( pTabList->nSrc==1
104606            && OptimizationEnabled(db, SQLITE_SubqCoroutine)
104607     ){
104608       /* Implement a co-routine that will return a single row of the result
104609       ** set on each invocation.
104610       */
104611       int addrTop = sqlite3VdbeCurrentAddr(v)+1;
104612       pItem->regReturn = ++pParse->nMem;
104613       sqlite3VdbeAddOp3(v, OP_InitCoroutine, pItem->regReturn, 0, addrTop);
104614       VdbeComment((v, "%s", pItem->pTab->zName));
104615       pItem->addrFillSub = addrTop;
104616       sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
104617       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104618       sqlite3Select(pParse, pSub, &dest);
104619       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104620       pItem->viaCoroutine = 1;
104621       pItem->regResult = dest.iSdst;
104622       sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
104623       sqlite3VdbeJumpHere(v, addrTop-1);
104624       sqlite3ClearTempRegCache(pParse);
104625     }else{
104626       /* Generate a subroutine that will fill an ephemeral table with
104627       ** the content of this subquery.  pItem->addrFillSub will point
104628       ** to the address of the generated subroutine.  pItem->regReturn
104629       ** is a register allocated to hold the subroutine return address
104630       */
104631       int topAddr;
104632       int onceAddr = 0;
104633       int retAddr;
104634       assert( pItem->addrFillSub==0 );
104635       pItem->regReturn = ++pParse->nMem;
104636       topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
104637       pItem->addrFillSub = topAddr+1;
104638       if( pItem->isCorrelated==0 ){
104639         /* If the subquery is not correlated and if we are not inside of
104640         ** a trigger, then we only need to compute the value of the subquery
104641         ** once. */
104642         onceAddr = sqlite3CodeOnce(pParse); VdbeCoverage(v);
104643         VdbeComment((v, "materialize \"%s\"", pItem->pTab->zName));
104644       }else{
104645         VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
104646       }
104647       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
104648       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104649       sqlite3Select(pParse, pSub, &dest);
104650       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104651       if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
104652       retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
104653       VdbeComment((v, "end %s", pItem->pTab->zName));
104654       sqlite3VdbeChangeP1(v, topAddr, retAddr);
104655       sqlite3ClearTempRegCache(pParse);
104656     }
104657     if( /*pParse->nErr ||*/ db->mallocFailed ){
104658       goto select_end;
104659     }
104660     pParse->nHeight -= sqlite3SelectExprHeight(p);
104661     pTabList = p->pSrc;
104662     if( !IgnorableOrderby(pDest) ){
104663       pOrderBy = p->pOrderBy;
104664     }
104665   }
104666   pEList = p->pEList;
104667 #endif
104668   pWhere = p->pWhere;
104669   pGroupBy = p->pGroupBy;
104670   pHaving = p->pHaving;
104671   sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
104672 
104673 #ifndef SQLITE_OMIT_COMPOUND_SELECT
104674   /* If there is are a sequence of queries, do the earlier ones first.
104675   */
104676   if( p->pPrior ){
104677     rc = multiSelect(pParse, p, pDest);
104678     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
104679     return rc;
104680   }
104681 #endif
104682 
104683   /* If there is both a GROUP BY and an ORDER BY clause and they are
104684   ** identical, then disable the ORDER BY clause since the GROUP BY
104685   ** will cause elements to come out in the correct order.  This is
104686   ** an optimization - the correct answer should result regardless.
104687   ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
104688   ** to disable this optimization for testing purposes.
104689   */
104690   if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy, -1)==0
104691          && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
104692     pOrderBy = 0;
104693   }
104694 
104695   /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
104696   ** if the select-list is the same as the ORDER BY list, then this query
104697   ** can be rewritten as a GROUP BY. In other words, this:
104698   **
104699   **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
104700   **
104701   ** is transformed to:
104702   **
104703   **     SELECT xyz FROM ... GROUP BY xyz
104704   **
104705   ** The second form is preferred as a single index (or temp-table) may be
104706   ** used for both the ORDER BY and DISTINCT processing. As originally
104707   ** written the query must use a temp-table for at least one of the ORDER
104708   ** BY and DISTINCT, and an index or separate temp-table for the other.
104709   */
104710   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
104711    && sqlite3ExprListCompare(pOrderBy, p->pEList, -1)==0
104712   ){
104713     p->selFlags &= ~SF_Distinct;
104714     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
104715     pGroupBy = p->pGroupBy;
104716     pOrderBy = 0;
104717     /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
104718     ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
104719     ** original setting of the SF_Distinct flag, not the current setting */
104720     assert( sDistinct.isTnct );
104721   }
104722 
104723   /* If there is an ORDER BY clause, then this sorting
104724   ** index might end up being unused if the data can be
104725   ** extracted in pre-sorted order.  If that is the case, then the
104726   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
104727   ** we figure out that the sorting index is not needed.  The addrSortIndex
104728   ** variable is used to facilitate that change.
104729   */
104730   if( pOrderBy ){
104731     KeyInfo *pKeyInfo;
104732     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy, 0);
104733     pOrderBy->iECursor = pParse->nTab++;
104734     p->addrOpenEphm[2] = addrSortIndex =
104735       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
104736                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
104737                            (char*)pKeyInfo, P4_KEYINFO);
104738   }else{
104739     addrSortIndex = -1;
104740   }
104741 
104742   /* If the output is destined for a temporary table, open that table.
104743   */
104744   if( pDest->eDest==SRT_EphemTab ){
104745     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
104746   }
104747 
104748   /* Set the limiter.
104749   */
104750   iEnd = sqlite3VdbeMakeLabel(v);
104751   p->nSelectRow = LARGEST_INT64;
104752   computeLimitRegisters(pParse, p, iEnd);
104753   if( p->iLimit==0 && addrSortIndex>=0 ){
104754     sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
104755     p->selFlags |= SF_UseSorter;
104756   }
104757 
104758   /* Open a virtual index to use for the distinct set.
104759   */
104760   if( p->selFlags & SF_Distinct ){
104761     sDistinct.tabTnct = pParse->nTab++;
104762     sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
104763                                 sDistinct.tabTnct, 0, 0,
104764                                 (char*)keyInfoFromExprList(pParse, p->pEList, 0),
104765                                 P4_KEYINFO);
104766     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
104767     sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
104768   }else{
104769     sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
104770   }
104771 
104772   if( !isAgg && pGroupBy==0 ){
104773     /* No aggregate functions and no GROUP BY clause */
104774     u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
104775 
104776     /* Begin the database scan. */
104777     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, p->pEList,
104778                                wctrlFlags, 0);
104779     if( pWInfo==0 ) goto select_end;
104780     if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
104781       p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
104782     }
104783     if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
104784       sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
104785     }
104786     if( pOrderBy && sqlite3WhereIsOrdered(pWInfo) ) pOrderBy = 0;
104787 
104788     /* If sorting index that was created by a prior OP_OpenEphemeral
104789     ** instruction ended up not being needed, then change the OP_OpenEphemeral
104790     ** into an OP_Noop.
104791     */
104792     if( addrSortIndex>=0 && pOrderBy==0 ){
104793       sqlite3VdbeChangeToNoop(v, addrSortIndex);
104794       p->addrOpenEphm[2] = -1;
104795     }
104796 
104797     /* Use the standard inner loop. */
104798     selectInnerLoop(pParse, p, pEList, -1, pOrderBy, &sDistinct, pDest,
104799                     sqlite3WhereContinueLabel(pWInfo),
104800                     sqlite3WhereBreakLabel(pWInfo));
104801 
104802     /* End the database scan loop.
104803     */
104804     sqlite3WhereEnd(pWInfo);
104805   }else{
104806     /* This case when there exist aggregate functions or a GROUP BY clause
104807     ** or both */
104808     NameContext sNC;    /* Name context for processing aggregate information */
104809     int iAMem;          /* First Mem address for storing current GROUP BY */
104810     int iBMem;          /* First Mem address for previous GROUP BY */
104811     int iUseFlag;       /* Mem address holding flag indicating that at least
104812                         ** one row of the input to the aggregator has been
104813                         ** processed */
104814     int iAbortFlag;     /* Mem address which causes query abort if positive */
104815     int groupBySort;    /* Rows come from source in GROUP BY order */
104816     int addrEnd;        /* End of processing for this SELECT */
104817     int sortPTab = 0;   /* Pseudotable used to decode sorting results */
104818     int sortOut = 0;    /* Output register from the sorter */
104819 
104820     /* Remove any and all aliases between the result set and the
104821     ** GROUP BY clause.
104822     */
104823     if( pGroupBy ){
104824       int k;                        /* Loop counter */
104825       struct ExprList_item *pItem;  /* For looping over expression in a list */
104826 
104827       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
104828         pItem->u.x.iAlias = 0;
104829       }
104830       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
104831         pItem->u.x.iAlias = 0;
104832       }
104833       if( p->nSelectRow>100 ) p->nSelectRow = 100;
104834     }else{
104835       p->nSelectRow = 1;
104836     }
104837 
104838 
104839     /* Create a label to jump to when we want to abort the query */
104840     addrEnd = sqlite3VdbeMakeLabel(v);
104841 
104842     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
104843     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
104844     ** SELECT statement.
104845     */
104846     memset(&sNC, 0, sizeof(sNC));
104847     sNC.pParse = pParse;
104848     sNC.pSrcList = pTabList;
104849     sNC.pAggInfo = &sAggInfo;
104850     sAggInfo.mnReg = pParse->nMem+1;
104851     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
104852     sAggInfo.pGroupBy = pGroupBy;
104853     sqlite3ExprAnalyzeAggList(&sNC, pEList);
104854     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
104855     if( pHaving ){
104856       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
104857     }
104858     sAggInfo.nAccumulator = sAggInfo.nColumn;
104859     for(i=0; i<sAggInfo.nFunc; i++){
104860       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
104861       sNC.ncFlags |= NC_InAggFunc;
104862       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
104863       sNC.ncFlags &= ~NC_InAggFunc;
104864     }
104865     sAggInfo.mxReg = pParse->nMem;
104866     if( db->mallocFailed ) goto select_end;
104867 
104868     /* Processing for aggregates with GROUP BY is very different and
104869     ** much more complex than aggregates without a GROUP BY.
104870     */
104871     if( pGroupBy ){
104872       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
104873       int j1;             /* A-vs-B comparision jump */
104874       int addrOutputRow;  /* Start of subroutine that outputs a result row */
104875       int regOutputRow;   /* Return address register for output subroutine */
104876       int addrSetAbort;   /* Set the abort flag and return */
104877       int addrTopOfLoop;  /* Top of the input loop */
104878       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
104879       int addrReset;      /* Subroutine for resetting the accumulator */
104880       int regReset;       /* Return address register for reset subroutine */
104881 
104882       /* If there is a GROUP BY clause we might need a sorting index to
104883       ** implement it.  Allocate that sorting index now.  If it turns out
104884       ** that we do not need it after all, the OP_SorterOpen instruction
104885       ** will be converted into a Noop.
104886       */
104887       sAggInfo.sortingIdx = pParse->nTab++;
104888       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0);
104889       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
104890           sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
104891           0, (char*)pKeyInfo, P4_KEYINFO);
104892 
104893       /* Initialize memory locations used by GROUP BY aggregate processing
104894       */
104895       iUseFlag = ++pParse->nMem;
104896       iAbortFlag = ++pParse->nMem;
104897       regOutputRow = ++pParse->nMem;
104898       addrOutputRow = sqlite3VdbeMakeLabel(v);
104899       regReset = ++pParse->nMem;
104900       addrReset = sqlite3VdbeMakeLabel(v);
104901       iAMem = pParse->nMem + 1;
104902       pParse->nMem += pGroupBy->nExpr;
104903       iBMem = pParse->nMem + 1;
104904       pParse->nMem += pGroupBy->nExpr;
104905       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
104906       VdbeComment((v, "clear abort flag"));
104907       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
104908       VdbeComment((v, "indicate accumulator empty"));
104909       sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
104910 
104911       /* Begin a loop that will extract all source rows in GROUP BY order.
104912       ** This might involve two separate loops with an OP_Sort in between, or
104913       ** it might be a single loop that uses an index to extract information
104914       ** in the right order to begin with.
104915       */
104916       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
104917       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
104918                                  WHERE_GROUPBY, 0);
104919       if( pWInfo==0 ) goto select_end;
104920       if( sqlite3WhereIsOrdered(pWInfo) ){
104921         /* The optimizer is able to deliver rows in group by order so
104922         ** we do not have to sort.  The OP_OpenEphemeral table will be
104923         ** cancelled later because we still need to use the pKeyInfo
104924         */
104925         groupBySort = 0;
104926       }else{
104927         /* Rows are coming out in undetermined order.  We have to push
104928         ** each row into a sorting index, terminate the first loop,
104929         ** then loop over the sorting index in order to get the output
104930         ** in sorted order
104931         */
104932         int regBase;
104933         int regRecord;
104934         int nCol;
104935         int nGroupBy;
104936 
104937         explainTempTable(pParse,
104938             (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
104939                     "DISTINCT" : "GROUP BY");
104940 
104941         groupBySort = 1;
104942         nGroupBy = pGroupBy->nExpr;
104943         nCol = nGroupBy + 1;
104944         j = nGroupBy+1;
104945         for(i=0; i<sAggInfo.nColumn; i++){
104946           if( sAggInfo.aCol[i].iSorterColumn>=j ){
104947             nCol++;
104948             j++;
104949           }
104950         }
104951         regBase = sqlite3GetTempRange(pParse, nCol);
104952         sqlite3ExprCacheClear(pParse);
104953         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
104954         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
104955         j = nGroupBy+1;
104956         for(i=0; i<sAggInfo.nColumn; i++){
104957           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
104958           if( pCol->iSorterColumn>=j ){
104959             int r1 = j + regBase;
104960             int r2;
104961 
104962             r2 = sqlite3ExprCodeGetColumn(pParse,
104963                                pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
104964             if( r1!=r2 ){
104965               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
104966             }
104967             j++;
104968           }
104969         }
104970         regRecord = sqlite3GetTempReg(pParse);
104971         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
104972         sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
104973         sqlite3ReleaseTempReg(pParse, regRecord);
104974         sqlite3ReleaseTempRange(pParse, regBase, nCol);
104975         sqlite3WhereEnd(pWInfo);
104976         sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
104977         sortOut = sqlite3GetTempReg(pParse);
104978         sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
104979         sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
104980         VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
104981         sAggInfo.useSortingIdx = 1;
104982         sqlite3ExprCacheClear(pParse);
104983       }
104984 
104985       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
104986       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
104987       ** Then compare the current GROUP BY terms against the GROUP BY terms
104988       ** from the previous row currently stored in a0, a1, a2...
104989       */
104990       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
104991       sqlite3ExprCacheClear(pParse);
104992       if( groupBySort ){
104993         sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
104994       }
104995       for(j=0; j<pGroupBy->nExpr; j++){
104996         if( groupBySort ){
104997           sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
104998           if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
104999         }else{
105000           sAggInfo.directMode = 1;
105001           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
105002         }
105003       }
105004       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
105005                           (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
105006       j1 = sqlite3VdbeCurrentAddr(v);
105007       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1); VdbeCoverage(v);
105008 
105009       /* Generate code that runs whenever the GROUP BY changes.
105010       ** Changes in the GROUP BY are detected by the previous code
105011       ** block.  If there were no changes, this block is skipped.
105012       **
105013       ** This code copies current group by terms in b0,b1,b2,...
105014       ** over to a0,a1,a2.  It then calls the output subroutine
105015       ** and resets the aggregate accumulator registers in preparation
105016       ** for the next GROUP BY batch.
105017       */
105018       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
105019       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
105020       VdbeComment((v, "output one row"));
105021       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); VdbeCoverage(v);
105022       VdbeComment((v, "check abort flag"));
105023       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
105024       VdbeComment((v, "reset accumulator"));
105025 
105026       /* Update the aggregate accumulators based on the content of
105027       ** the current row
105028       */
105029       sqlite3VdbeJumpHere(v, j1);
105030       updateAccumulator(pParse, &sAggInfo);
105031       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
105032       VdbeComment((v, "indicate data in accumulator"));
105033 
105034       /* End of the loop
105035       */
105036       if( groupBySort ){
105037         sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
105038         VdbeCoverage(v);
105039       }else{
105040         sqlite3WhereEnd(pWInfo);
105041         sqlite3VdbeChangeToNoop(v, addrSortingIdx);
105042       }
105043 
105044       /* Output the final row of result
105045       */
105046       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
105047       VdbeComment((v, "output final row"));
105048 
105049       /* Jump over the subroutines
105050       */
105051       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
105052 
105053       /* Generate a subroutine that outputs a single row of the result
105054       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
105055       ** is less than or equal to zero, the subroutine is a no-op.  If
105056       ** the processing calls for the query to abort, this subroutine
105057       ** increments the iAbortFlag memory location before returning in
105058       ** order to signal the caller to abort.
105059       */
105060       addrSetAbort = sqlite3VdbeCurrentAddr(v);
105061       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
105062       VdbeComment((v, "set abort flag"));
105063       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
105064       sqlite3VdbeResolveLabel(v, addrOutputRow);
105065       addrOutputRow = sqlite3VdbeCurrentAddr(v);
105066       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); VdbeCoverage(v);
105067       VdbeComment((v, "Groupby result generator entry point"));
105068       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
105069       finalizeAggFunctions(pParse, &sAggInfo);
105070       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
105071       selectInnerLoop(pParse, p, p->pEList, -1, pOrderBy,
105072                       &sDistinct, pDest,
105073                       addrOutputRow+1, addrSetAbort);
105074       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
105075       VdbeComment((v, "end groupby result generator"));
105076 
105077       /* Generate a subroutine that will reset the group-by accumulator
105078       */
105079       sqlite3VdbeResolveLabel(v, addrReset);
105080       resetAccumulator(pParse, &sAggInfo);
105081       sqlite3VdbeAddOp1(v, OP_Return, regReset);
105082 
105083     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
105084     else {
105085       ExprList *pDel = 0;
105086 #ifndef SQLITE_OMIT_BTREECOUNT
105087       Table *pTab;
105088       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
105089         /* If isSimpleCount() returns a pointer to a Table structure, then
105090         ** the SQL statement is of the form:
105091         **
105092         **   SELECT count(*) FROM <tbl>
105093         **
105094         ** where the Table structure returned represents table <tbl>.
105095         **
105096         ** This statement is so common that it is optimized specially. The
105097         ** OP_Count instruction is executed either on the intkey table that
105098         ** contains the data for table <tbl> or on one of its indexes. It
105099         ** is better to execute the op on an index, as indexes are almost
105100         ** always spread across less pages than their corresponding tables.
105101         */
105102         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
105103         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
105104         Index *pIdx;                         /* Iterator variable */
105105         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
105106         Index *pBest = 0;                    /* Best index found so far */
105107         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
105108 
105109         sqlite3CodeVerifySchema(pParse, iDb);
105110         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
105111 
105112         /* Search for the index that has the lowest scan cost.
105113         **
105114         ** (2011-04-15) Do not do a full scan of an unordered index.
105115         **
105116         ** (2013-10-03) Do not count the entries in a partial index.
105117         **
105118         ** In practice the KeyInfo structure will not be used. It is only
105119         ** passed to keep OP_OpenRead happy.
105120         */
105121         if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
105122         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
105123           if( pIdx->bUnordered==0
105124            && pIdx->szIdxRow<pTab->szTabRow
105125            && pIdx->pPartIdxWhere==0
105126            && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
105127           ){
105128             pBest = pIdx;
105129           }
105130         }
105131         if( pBest ){
105132           iRoot = pBest->tnum;
105133           pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
105134         }
105135 
105136         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
105137         sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
105138         if( pKeyInfo ){
105139           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
105140         }
105141         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
105142         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
105143         explainSimpleCount(pParse, pTab, pBest);
105144       }else
105145 #endif /* SQLITE_OMIT_BTREECOUNT */
105146       {
105147         /* Check if the query is of one of the following forms:
105148         **
105149         **   SELECT min(x) FROM ...
105150         **   SELECT max(x) FROM ...
105151         **
105152         ** If it is, then ask the code in where.c to attempt to sort results
105153         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
105154         ** If where.c is able to produce results sorted in this order, then
105155         ** add vdbe code to break out of the processing loop after the
105156         ** first iteration (since the first iteration of the loop is
105157         ** guaranteed to operate on the row with the minimum or maximum
105158         ** value of x, the only row required).
105159         **
105160         ** A special flag must be passed to sqlite3WhereBegin() to slightly
105161         ** modify behavior as follows:
105162         **
105163         **   + If the query is a "SELECT min(x)", then the loop coded by
105164         **     where.c should not iterate over any values with a NULL value
105165         **     for x.
105166         **
105167         **   + The optimizer code in where.c (the thing that decides which
105168         **     index or indices to use) should place a different priority on
105169         **     satisfying the 'ORDER BY' clause than it does in other cases.
105170         **     Refer to code and comments in where.c for details.
105171         */
105172         ExprList *pMinMax = 0;
105173         u8 flag = WHERE_ORDERBY_NORMAL;
105174 
105175         assert( p->pGroupBy==0 );
105176         assert( flag==0 );
105177         if( p->pHaving==0 ){
105178           flag = minMaxQuery(&sAggInfo, &pMinMax);
105179         }
105180         assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
105181 
105182         if( flag ){
105183           pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
105184           pDel = pMinMax;
105185           if( pMinMax && !db->mallocFailed ){
105186             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
105187             pMinMax->a[0].pExpr->op = TK_COLUMN;
105188           }
105189         }
105190 
105191         /* This case runs if the aggregate has no GROUP BY clause.  The
105192         ** processing is much simpler since there is only a single row
105193         ** of output.
105194         */
105195         resetAccumulator(pParse, &sAggInfo);
105196         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
105197         if( pWInfo==0 ){
105198           sqlite3ExprListDelete(db, pDel);
105199           goto select_end;
105200         }
105201         updateAccumulator(pParse, &sAggInfo);
105202         assert( pMinMax==0 || pMinMax->nExpr==1 );
105203         if( sqlite3WhereIsOrdered(pWInfo) ){
105204           sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
105205           VdbeComment((v, "%s() by index",
105206                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
105207         }
105208         sqlite3WhereEnd(pWInfo);
105209         finalizeAggFunctions(pParse, &sAggInfo);
105210       }
105211 
105212       pOrderBy = 0;
105213       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
105214       selectInnerLoop(pParse, p, p->pEList, -1, 0, 0,
105215                       pDest, addrEnd, addrEnd);
105216       sqlite3ExprListDelete(db, pDel);
105217     }
105218     sqlite3VdbeResolveLabel(v, addrEnd);
105219 
105220   } /* endif aggregate query */
105221 
105222   if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
105223     explainTempTable(pParse, "DISTINCT");
105224   }
105225 
105226   /* If there is an ORDER BY clause, then we need to sort the results
105227   ** and send them to the callback one by one.
105228   */
105229   if( pOrderBy ){
105230     explainTempTable(pParse, "ORDER BY");
105231     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
105232   }
105233 
105234   /* Jump here to skip this query
105235   */
105236   sqlite3VdbeResolveLabel(v, iEnd);
105237 
105238   /* The SELECT was successfully coded.   Set the return code to 0
105239   ** to indicate no errors.
105240   */
105241   rc = 0;
105242 
105243   /* Control jumps to here if an error is encountered above, or upon
105244   ** successful coding of the SELECT.
105245   */
105246 select_end:
105247   explainSetInteger(pParse->iSelectId, iRestoreSelectId);
105248 
105249   /* Identify column names if results of the SELECT are to be output.
105250   */
105251   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
105252     generateColumnNames(pParse, pTabList, pEList);
105253   }
105254 
105255   sqlite3DbFree(db, sAggInfo.aCol);
105256   sqlite3DbFree(db, sAggInfo.aFunc);
105257   return rc;
105258 }
105259 
105260 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
105261 /*
105262 ** Generate a human-readable description of a the Select object.
105263 */
105264 static void explainOneSelect(Vdbe *pVdbe, Select *p){
105265   sqlite3ExplainPrintf(pVdbe, "SELECT ");
105266   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
105267     if( p->selFlags & SF_Distinct ){
105268       sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
105269     }
105270     if( p->selFlags & SF_Aggregate ){
105271       sqlite3ExplainPrintf(pVdbe, "agg_flag ");
105272     }
105273     sqlite3ExplainNL(pVdbe);
105274     sqlite3ExplainPrintf(pVdbe, "   ");
105275   }
105276   sqlite3ExplainExprList(pVdbe, p->pEList);
105277   sqlite3ExplainNL(pVdbe);
105278   if( p->pSrc && p->pSrc->nSrc ){
105279     int i;
105280     sqlite3ExplainPrintf(pVdbe, "FROM ");
105281     sqlite3ExplainPush(pVdbe);
105282     for(i=0; i<p->pSrc->nSrc; i++){
105283       struct SrcList_item *pItem = &p->pSrc->a[i];
105284       sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
105285       if( pItem->pSelect ){
105286         sqlite3ExplainSelect(pVdbe, pItem->pSelect);
105287         if( pItem->pTab ){
105288           sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
105289         }
105290       }else if( pItem->zName ){
105291         sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
105292       }
105293       if( pItem->zAlias ){
105294         sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
105295       }
105296       if( pItem->jointype & JT_LEFT ){
105297         sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
105298       }
105299       sqlite3ExplainNL(pVdbe);
105300     }
105301     sqlite3ExplainPop(pVdbe);
105302   }
105303   if( p->pWhere ){
105304     sqlite3ExplainPrintf(pVdbe, "WHERE ");
105305     sqlite3ExplainExpr(pVdbe, p->pWhere);
105306     sqlite3ExplainNL(pVdbe);
105307   }
105308   if( p->pGroupBy ){
105309     sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
105310     sqlite3ExplainExprList(pVdbe, p->pGroupBy);
105311     sqlite3ExplainNL(pVdbe);
105312   }
105313   if( p->pHaving ){
105314     sqlite3ExplainPrintf(pVdbe, "HAVING ");
105315     sqlite3ExplainExpr(pVdbe, p->pHaving);
105316     sqlite3ExplainNL(pVdbe);
105317   }
105318   if( p->pOrderBy ){
105319     sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
105320     sqlite3ExplainExprList(pVdbe, p->pOrderBy);
105321     sqlite3ExplainNL(pVdbe);
105322   }
105323   if( p->pLimit ){
105324     sqlite3ExplainPrintf(pVdbe, "LIMIT ");
105325     sqlite3ExplainExpr(pVdbe, p->pLimit);
105326     sqlite3ExplainNL(pVdbe);
105327   }
105328   if( p->pOffset ){
105329     sqlite3ExplainPrintf(pVdbe, "OFFSET ");
105330     sqlite3ExplainExpr(pVdbe, p->pOffset);
105331     sqlite3ExplainNL(pVdbe);
105332   }
105333 }
105334 SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
105335   if( p==0 ){
105336     sqlite3ExplainPrintf(pVdbe, "(null-select)");
105337     return;
105338   }
105339   sqlite3ExplainPush(pVdbe);
105340   while( p ){
105341     explainOneSelect(pVdbe, p);
105342     p = p->pNext;
105343     if( p==0 ) break;
105344     sqlite3ExplainNL(pVdbe);
105345     sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
105346   }
105347   sqlite3ExplainPrintf(pVdbe, "END");
105348   sqlite3ExplainPop(pVdbe);
105349 }
105350 
105351 /* End of the structure debug printing code
105352 *****************************************************************************/
105353 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
105354 
105355 /************** End of select.c **********************************************/
105356 /************** Begin file table.c *******************************************/
105357 /*
105358 ** 2001 September 15
105359 **
105360 ** The author disclaims copyright to this source code.  In place of
105361 ** a legal notice, here is a blessing:
105362 **
105363 **    May you do good and not evil.
105364 **    May you find forgiveness for yourself and forgive others.
105365 **    May you share freely, never taking more than you give.
105366 **
105367 *************************************************************************
105368 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
105369 ** interface routines.  These are just wrappers around the main
105370 ** interface routine of sqlite3_exec().
105371 **
105372 ** These routines are in a separate files so that they will not be linked
105373 ** if they are not used.
105374 */
105375 /* #include <stdlib.h> */
105376 /* #include <string.h> */
105377 
105378 #ifndef SQLITE_OMIT_GET_TABLE
105379 
105380 /*
105381 ** This structure is used to pass data from sqlite3_get_table() through
105382 ** to the callback function is uses to build the result.
105383 */
105384 typedef struct TabResult {
105385   char **azResult;   /* Accumulated output */
105386   char *zErrMsg;     /* Error message text, if an error occurs */
105387   int nAlloc;        /* Slots allocated for azResult[] */
105388   int nRow;          /* Number of rows in the result */
105389   int nColumn;       /* Number of columns in the result */
105390   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
105391   int rc;            /* Return code from sqlite3_exec() */
105392 } TabResult;
105393 
105394 /*
105395 ** This routine is called once for each row in the result table.  Its job
105396 ** is to fill in the TabResult structure appropriately, allocating new
105397 ** memory as necessary.
105398 */
105399 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
105400   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
105401   int need;                         /* Slots needed in p->azResult[] */
105402   int i;                            /* Loop counter */
105403   char *z;                          /* A single column of result */
105404 
105405   /* Make sure there is enough space in p->azResult to hold everything
105406   ** we need to remember from this invocation of the callback.
105407   */
105408   if( p->nRow==0 && argv!=0 ){
105409     need = nCol*2;
105410   }else{
105411     need = nCol;
105412   }
105413   if( p->nData + need > p->nAlloc ){
105414     char **azNew;
105415     p->nAlloc = p->nAlloc*2 + need;
105416     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
105417     if( azNew==0 ) goto malloc_failed;
105418     p->azResult = azNew;
105419   }
105420 
105421   /* If this is the first row, then generate an extra row containing
105422   ** the names of all columns.
105423   */
105424   if( p->nRow==0 ){
105425     p->nColumn = nCol;
105426     for(i=0; i<nCol; i++){
105427       z = sqlite3_mprintf("%s", colv[i]);
105428       if( z==0 ) goto malloc_failed;
105429       p->azResult[p->nData++] = z;
105430     }
105431   }else if( p->nColumn!=nCol ){
105432     sqlite3_free(p->zErrMsg);
105433     p->zErrMsg = sqlite3_mprintf(
105434        "sqlite3_get_table() called with two or more incompatible queries"
105435     );
105436     p->rc = SQLITE_ERROR;
105437     return 1;
105438   }
105439 
105440   /* Copy over the row data
105441   */
105442   if( argv!=0 ){
105443     for(i=0; i<nCol; i++){
105444       if( argv[i]==0 ){
105445         z = 0;
105446       }else{
105447         int n = sqlite3Strlen30(argv[i])+1;
105448         z = sqlite3_malloc( n );
105449         if( z==0 ) goto malloc_failed;
105450         memcpy(z, argv[i], n);
105451       }
105452       p->azResult[p->nData++] = z;
105453     }
105454     p->nRow++;
105455   }
105456   return 0;
105457 
105458 malloc_failed:
105459   p->rc = SQLITE_NOMEM;
105460   return 1;
105461 }
105462 
105463 /*
105464 ** Query the database.  But instead of invoking a callback for each row,
105465 ** malloc() for space to hold the result and return the entire results
105466 ** at the conclusion of the call.
105467 **
105468 ** The result that is written to ***pazResult is held in memory obtained
105469 ** from malloc().  But the caller cannot free this memory directly.
105470 ** Instead, the entire table should be passed to sqlite3_free_table() when
105471 ** the calling procedure is finished using it.
105472 */
105473 SQLITE_API int sqlite3_get_table(
105474   sqlite3 *db,                /* The database on which the SQL executes */
105475   const char *zSql,           /* The SQL to be executed */
105476   char ***pazResult,          /* Write the result table here */
105477   int *pnRow,                 /* Write the number of rows in the result here */
105478   int *pnColumn,              /* Write the number of columns of result here */
105479   char **pzErrMsg             /* Write error messages here */
105480 ){
105481   int rc;
105482   TabResult res;
105483 
105484   *pazResult = 0;
105485   if( pnColumn ) *pnColumn = 0;
105486   if( pnRow ) *pnRow = 0;
105487   if( pzErrMsg ) *pzErrMsg = 0;
105488   res.zErrMsg = 0;
105489   res.nRow = 0;
105490   res.nColumn = 0;
105491   res.nData = 1;
105492   res.nAlloc = 20;
105493   res.rc = SQLITE_OK;
105494   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
105495   if( res.azResult==0 ){
105496      db->errCode = SQLITE_NOMEM;
105497      return SQLITE_NOMEM;
105498   }
105499   res.azResult[0] = 0;
105500   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
105501   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
105502   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
105503   if( (rc&0xff)==SQLITE_ABORT ){
105504     sqlite3_free_table(&res.azResult[1]);
105505     if( res.zErrMsg ){
105506       if( pzErrMsg ){
105507         sqlite3_free(*pzErrMsg);
105508         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
105509       }
105510       sqlite3_free(res.zErrMsg);
105511     }
105512     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
105513     return res.rc;
105514   }
105515   sqlite3_free(res.zErrMsg);
105516   if( rc!=SQLITE_OK ){
105517     sqlite3_free_table(&res.azResult[1]);
105518     return rc;
105519   }
105520   if( res.nAlloc>res.nData ){
105521     char **azNew;
105522     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
105523     if( azNew==0 ){
105524       sqlite3_free_table(&res.azResult[1]);
105525       db->errCode = SQLITE_NOMEM;
105526       return SQLITE_NOMEM;
105527     }
105528     res.azResult = azNew;
105529   }
105530   *pazResult = &res.azResult[1];
105531   if( pnColumn ) *pnColumn = res.nColumn;
105532   if( pnRow ) *pnRow = res.nRow;
105533   return rc;
105534 }
105535 
105536 /*
105537 ** This routine frees the space the sqlite3_get_table() malloced.
105538 */
105539 SQLITE_API void sqlite3_free_table(
105540   char **azResult            /* Result returned from from sqlite3_get_table() */
105541 ){
105542   if( azResult ){
105543     int i, n;
105544     azResult--;
105545     assert( azResult!=0 );
105546     n = SQLITE_PTR_TO_INT(azResult[0]);
105547     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
105548     sqlite3_free(azResult);
105549   }
105550 }
105551 
105552 #endif /* SQLITE_OMIT_GET_TABLE */
105553 
105554 /************** End of table.c ***********************************************/
105555 /************** Begin file trigger.c *****************************************/
105556 /*
105557 **
105558 ** The author disclaims copyright to this source code.  In place of
105559 ** a legal notice, here is a blessing:
105560 **
105561 **    May you do good and not evil.
105562 **    May you find forgiveness for yourself and forgive others.
105563 **    May you share freely, never taking more than you give.
105564 **
105565 *************************************************************************
105566 ** This file contains the implementation for TRIGGERs
105567 */
105568 
105569 #ifndef SQLITE_OMIT_TRIGGER
105570 /*
105571 ** Delete a linked list of TriggerStep structures.
105572 */
105573 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
105574   while( pTriggerStep ){
105575     TriggerStep * pTmp = pTriggerStep;
105576     pTriggerStep = pTriggerStep->pNext;
105577 
105578     sqlite3ExprDelete(db, pTmp->pWhere);
105579     sqlite3ExprListDelete(db, pTmp->pExprList);
105580     sqlite3SelectDelete(db, pTmp->pSelect);
105581     sqlite3IdListDelete(db, pTmp->pIdList);
105582 
105583     sqlite3DbFree(db, pTmp);
105584   }
105585 }
105586 
105587 /*
105588 ** Given table pTab, return a list of all the triggers attached to
105589 ** the table. The list is connected by Trigger.pNext pointers.
105590 **
105591 ** All of the triggers on pTab that are in the same database as pTab
105592 ** are already attached to pTab->pTrigger.  But there might be additional
105593 ** triggers on pTab in the TEMP schema.  This routine prepends all
105594 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
105595 ** and returns the combined list.
105596 **
105597 ** To state it another way:  This routine returns a list of all triggers
105598 ** that fire off of pTab.  The list will include any TEMP triggers on
105599 ** pTab as well as the triggers lised in pTab->pTrigger.
105600 */
105601 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
105602   Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
105603   Trigger *pList = 0;                  /* List of triggers to return */
105604 
105605   if( pParse->disableTriggers ){
105606     return 0;
105607   }
105608 
105609   if( pTmpSchema!=pTab->pSchema ){
105610     HashElem *p;
105611     assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
105612     for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
105613       Trigger *pTrig = (Trigger *)sqliteHashData(p);
105614       if( pTrig->pTabSchema==pTab->pSchema
105615        && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
105616       ){
105617         pTrig->pNext = (pList ? pList : pTab->pTrigger);
105618         pList = pTrig;
105619       }
105620     }
105621   }
105622 
105623   return (pList ? pList : pTab->pTrigger);
105624 }
105625 
105626 /*
105627 ** This is called by the parser when it sees a CREATE TRIGGER statement
105628 ** up to the point of the BEGIN before the trigger actions.  A Trigger
105629 ** structure is generated based on the information available and stored
105630 ** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
105631 ** sqlite3FinishTrigger() function is called to complete the trigger
105632 ** construction process.
105633 */
105634 SQLITE_PRIVATE void sqlite3BeginTrigger(
105635   Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
105636   Token *pName1,      /* The name of the trigger */
105637   Token *pName2,      /* The name of the trigger */
105638   int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
105639   int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
105640   IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
105641   SrcList *pTableName,/* The name of the table/view the trigger applies to */
105642   Expr *pWhen,        /* WHEN clause */
105643   int isTemp,         /* True if the TEMPORARY keyword is present */
105644   int noErr           /* Suppress errors if the trigger already exists */
105645 ){
105646   Trigger *pTrigger = 0;  /* The new trigger */
105647   Table *pTab;            /* Table that the trigger fires off of */
105648   char *zName = 0;        /* Name of the trigger */
105649   sqlite3 *db = pParse->db;  /* The database connection */
105650   int iDb;                /* The database to store the trigger in */
105651   Token *pName;           /* The unqualified db name */
105652   DbFixer sFix;           /* State vector for the DB fixer */
105653   int iTabDb;             /* Index of the database holding pTab */
105654 
105655   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
105656   assert( pName2!=0 );
105657   assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
105658   assert( op>0 && op<0xff );
105659   if( isTemp ){
105660     /* If TEMP was specified, then the trigger name may not be qualified. */
105661     if( pName2->n>0 ){
105662       sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
105663       goto trigger_cleanup;
105664     }
105665     iDb = 1;
105666     pName = pName1;
105667   }else{
105668     /* Figure out the db that the trigger will be created in */
105669     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
105670     if( iDb<0 ){
105671       goto trigger_cleanup;
105672     }
105673   }
105674   if( !pTableName || db->mallocFailed ){
105675     goto trigger_cleanup;
105676   }
105677 
105678   /* A long-standing parser bug is that this syntax was allowed:
105679   **
105680   **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
105681   **                                                 ^^^^^^^^
105682   **
105683   ** To maintain backwards compatibility, ignore the database
105684   ** name on pTableName if we are reparsing our of SQLITE_MASTER.
105685   */
105686   if( db->init.busy && iDb!=1 ){
105687     sqlite3DbFree(db, pTableName->a[0].zDatabase);
105688     pTableName->a[0].zDatabase = 0;
105689   }
105690 
105691   /* If the trigger name was unqualified, and the table is a temp table,
105692   ** then set iDb to 1 to create the trigger in the temporary database.
105693   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
105694   ** exist, the error is caught by the block below.
105695   */
105696   pTab = sqlite3SrcListLookup(pParse, pTableName);
105697   if( db->init.busy==0 && pName2->n==0 && pTab
105698         && pTab->pSchema==db->aDb[1].pSchema ){
105699     iDb = 1;
105700   }
105701 
105702   /* Ensure the table name matches database name and that the table exists */
105703   if( db->mallocFailed ) goto trigger_cleanup;
105704   assert( pTableName->nSrc==1 );
105705   sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
105706   if( sqlite3FixSrcList(&sFix, pTableName) ){
105707     goto trigger_cleanup;
105708   }
105709   pTab = sqlite3SrcListLookup(pParse, pTableName);
105710   if( !pTab ){
105711     /* The table does not exist. */
105712     if( db->init.iDb==1 ){
105713       /* Ticket #3810.
105714       ** Normally, whenever a table is dropped, all associated triggers are
105715       ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
105716       ** and the table is dropped by a different database connection, the
105717       ** trigger is not visible to the database connection that does the
105718       ** drop so the trigger cannot be dropped.  This results in an
105719       ** "orphaned trigger" - a trigger whose associated table is missing.
105720       */
105721       db->init.orphanTrigger = 1;
105722     }
105723     goto trigger_cleanup;
105724   }
105725   if( IsVirtual(pTab) ){
105726     sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
105727     goto trigger_cleanup;
105728   }
105729 
105730   /* Check that the trigger name is not reserved and that no trigger of the
105731   ** specified name exists */
105732   zName = sqlite3NameFromToken(db, pName);
105733   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
105734     goto trigger_cleanup;
105735   }
105736   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105737   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
105738                       zName, sqlite3Strlen30(zName)) ){
105739     if( !noErr ){
105740       sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
105741     }else{
105742       assert( !db->init.busy );
105743       sqlite3CodeVerifySchema(pParse, iDb);
105744     }
105745     goto trigger_cleanup;
105746   }
105747 
105748   /* Do not create a trigger on a system table */
105749   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
105750     sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
105751     pParse->nErr++;
105752     goto trigger_cleanup;
105753   }
105754 
105755   /* INSTEAD of triggers are only for views and views only support INSTEAD
105756   ** of triggers.
105757   */
105758   if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
105759     sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
105760         (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
105761     goto trigger_cleanup;
105762   }
105763   if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
105764     sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
105765         " trigger on table: %S", pTableName, 0);
105766     goto trigger_cleanup;
105767   }
105768   iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
105769 
105770 #ifndef SQLITE_OMIT_AUTHORIZATION
105771   {
105772     int code = SQLITE_CREATE_TRIGGER;
105773     const char *zDb = db->aDb[iTabDb].zName;
105774     const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
105775     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
105776     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
105777       goto trigger_cleanup;
105778     }
105779     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
105780       goto trigger_cleanup;
105781     }
105782   }
105783 #endif
105784 
105785   /* INSTEAD OF triggers can only appear on views and BEFORE triggers
105786   ** cannot appear on views.  So we might as well translate every
105787   ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
105788   ** elsewhere.
105789   */
105790   if (tr_tm == TK_INSTEAD){
105791     tr_tm = TK_BEFORE;
105792   }
105793 
105794   /* Build the Trigger object */
105795   pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
105796   if( pTrigger==0 ) goto trigger_cleanup;
105797   pTrigger->zName = zName;
105798   zName = 0;
105799   pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
105800   pTrigger->pSchema = db->aDb[iDb].pSchema;
105801   pTrigger->pTabSchema = pTab->pSchema;
105802   pTrigger->op = (u8)op;
105803   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
105804   pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
105805   pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
105806   assert( pParse->pNewTrigger==0 );
105807   pParse->pNewTrigger = pTrigger;
105808 
105809 trigger_cleanup:
105810   sqlite3DbFree(db, zName);
105811   sqlite3SrcListDelete(db, pTableName);
105812   sqlite3IdListDelete(db, pColumns);
105813   sqlite3ExprDelete(db, pWhen);
105814   if( !pParse->pNewTrigger ){
105815     sqlite3DeleteTrigger(db, pTrigger);
105816   }else{
105817     assert( pParse->pNewTrigger==pTrigger );
105818   }
105819 }
105820 
105821 /*
105822 ** This routine is called after all of the trigger actions have been parsed
105823 ** in order to complete the process of building the trigger.
105824 */
105825 SQLITE_PRIVATE void sqlite3FinishTrigger(
105826   Parse *pParse,          /* Parser context */
105827   TriggerStep *pStepList, /* The triggered program */
105828   Token *pAll             /* Token that describes the complete CREATE TRIGGER */
105829 ){
105830   Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
105831   char *zName;                            /* Name of trigger */
105832   sqlite3 *db = pParse->db;               /* The database */
105833   DbFixer sFix;                           /* Fixer object */
105834   int iDb;                                /* Database containing the trigger */
105835   Token nameToken;                        /* Trigger name for error reporting */
105836 
105837   pParse->pNewTrigger = 0;
105838   if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
105839   zName = pTrig->zName;
105840   iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
105841   pTrig->step_list = pStepList;
105842   while( pStepList ){
105843     pStepList->pTrig = pTrig;
105844     pStepList = pStepList->pNext;
105845   }
105846   nameToken.z = pTrig->zName;
105847   nameToken.n = sqlite3Strlen30(nameToken.z);
105848   sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
105849   if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
105850    || sqlite3FixExpr(&sFix, pTrig->pWhen)
105851   ){
105852     goto triggerfinish_cleanup;
105853   }
105854 
105855   /* if we are not initializing,
105856   ** build the sqlite_master entry
105857   */
105858   if( !db->init.busy ){
105859     Vdbe *v;
105860     char *z;
105861 
105862     /* Make an entry in the sqlite_master table */
105863     v = sqlite3GetVdbe(pParse);
105864     if( v==0 ) goto triggerfinish_cleanup;
105865     sqlite3BeginWriteOperation(pParse, 0, iDb);
105866     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
105867     sqlite3NestedParse(pParse,
105868        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
105869        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
105870        pTrig->table, z);
105871     sqlite3DbFree(db, z);
105872     sqlite3ChangeCookie(pParse, iDb);
105873     sqlite3VdbeAddParseSchemaOp(v, iDb,
105874         sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
105875   }
105876 
105877   if( db->init.busy ){
105878     Trigger *pLink = pTrig;
105879     Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
105880     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105881     pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
105882     if( pTrig ){
105883       db->mallocFailed = 1;
105884     }else if( pLink->pSchema==pLink->pTabSchema ){
105885       Table *pTab;
105886       int n = sqlite3Strlen30(pLink->table);
105887       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
105888       assert( pTab!=0 );
105889       pLink->pNext = pTab->pTrigger;
105890       pTab->pTrigger = pLink;
105891     }
105892   }
105893 
105894 triggerfinish_cleanup:
105895   sqlite3DeleteTrigger(db, pTrig);
105896   assert( !pParse->pNewTrigger );
105897   sqlite3DeleteTriggerStep(db, pStepList);
105898 }
105899 
105900 /*
105901 ** Turn a SELECT statement (that the pSelect parameter points to) into
105902 ** a trigger step.  Return a pointer to a TriggerStep structure.
105903 **
105904 ** The parser calls this routine when it finds a SELECT statement in
105905 ** body of a TRIGGER.
105906 */
105907 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
105908   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
105909   if( pTriggerStep==0 ) {
105910     sqlite3SelectDelete(db, pSelect);
105911     return 0;
105912   }
105913   pTriggerStep->op = TK_SELECT;
105914   pTriggerStep->pSelect = pSelect;
105915   pTriggerStep->orconf = OE_Default;
105916   return pTriggerStep;
105917 }
105918 
105919 /*
105920 ** Allocate space to hold a new trigger step.  The allocated space
105921 ** holds both the TriggerStep object and the TriggerStep.target.z string.
105922 **
105923 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
105924 */
105925 static TriggerStep *triggerStepAllocate(
105926   sqlite3 *db,                /* Database connection */
105927   u8 op,                      /* Trigger opcode */
105928   Token *pName                /* The target name */
105929 ){
105930   TriggerStep *pTriggerStep;
105931 
105932   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
105933   if( pTriggerStep ){
105934     char *z = (char*)&pTriggerStep[1];
105935     memcpy(z, pName->z, pName->n);
105936     pTriggerStep->target.z = z;
105937     pTriggerStep->target.n = pName->n;
105938     pTriggerStep->op = op;
105939   }
105940   return pTriggerStep;
105941 }
105942 
105943 /*
105944 ** Build a trigger step out of an INSERT statement.  Return a pointer
105945 ** to the new trigger step.
105946 **
105947 ** The parser calls this routine when it sees an INSERT inside the
105948 ** body of a trigger.
105949 */
105950 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
105951   sqlite3 *db,        /* The database connection */
105952   Token *pTableName,  /* Name of the table into which we insert */
105953   IdList *pColumn,    /* List of columns in pTableName to insert into */
105954   Select *pSelect,    /* A SELECT statement that supplies values */
105955   u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
105956 ){
105957   TriggerStep *pTriggerStep;
105958 
105959   assert(pSelect != 0 || db->mallocFailed);
105960 
105961   pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
105962   if( pTriggerStep ){
105963     pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
105964     pTriggerStep->pIdList = pColumn;
105965     pTriggerStep->orconf = orconf;
105966   }else{
105967     sqlite3IdListDelete(db, pColumn);
105968   }
105969   sqlite3SelectDelete(db, pSelect);
105970 
105971   return pTriggerStep;
105972 }
105973 
105974 /*
105975 ** Construct a trigger step that implements an UPDATE statement and return
105976 ** a pointer to that trigger step.  The parser calls this routine when it
105977 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
105978 */
105979 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
105980   sqlite3 *db,         /* The database connection */
105981   Token *pTableName,   /* Name of the table to be updated */
105982   ExprList *pEList,    /* The SET clause: list of column and new values */
105983   Expr *pWhere,        /* The WHERE clause */
105984   u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
105985 ){
105986   TriggerStep *pTriggerStep;
105987 
105988   pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
105989   if( pTriggerStep ){
105990     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
105991     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
105992     pTriggerStep->orconf = orconf;
105993   }
105994   sqlite3ExprListDelete(db, pEList);
105995   sqlite3ExprDelete(db, pWhere);
105996   return pTriggerStep;
105997 }
105998 
105999 /*
106000 ** Construct a trigger step that implements a DELETE statement and return
106001 ** a pointer to that trigger step.  The parser calls this routine when it
106002 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
106003 */
106004 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
106005   sqlite3 *db,            /* Database connection */
106006   Token *pTableName,      /* The table from which rows are deleted */
106007   Expr *pWhere            /* The WHERE clause */
106008 ){
106009   TriggerStep *pTriggerStep;
106010 
106011   pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
106012   if( pTriggerStep ){
106013     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
106014     pTriggerStep->orconf = OE_Default;
106015   }
106016   sqlite3ExprDelete(db, pWhere);
106017   return pTriggerStep;
106018 }
106019 
106020 /*
106021 ** Recursively delete a Trigger structure
106022 */
106023 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
106024   if( pTrigger==0 ) return;
106025   sqlite3DeleteTriggerStep(db, pTrigger->step_list);
106026   sqlite3DbFree(db, pTrigger->zName);
106027   sqlite3DbFree(db, pTrigger->table);
106028   sqlite3ExprDelete(db, pTrigger->pWhen);
106029   sqlite3IdListDelete(db, pTrigger->pColumns);
106030   sqlite3DbFree(db, pTrigger);
106031 }
106032 
106033 /*
106034 ** This function is called to drop a trigger from the database schema.
106035 **
106036 ** This may be called directly from the parser and therefore identifies
106037 ** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
106038 ** same job as this routine except it takes a pointer to the trigger
106039 ** instead of the trigger name.
106040 **/
106041 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
106042   Trigger *pTrigger = 0;
106043   int i;
106044   const char *zDb;
106045   const char *zName;
106046   int nName;
106047   sqlite3 *db = pParse->db;
106048 
106049   if( db->mallocFailed ) goto drop_trigger_cleanup;
106050   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
106051     goto drop_trigger_cleanup;
106052   }
106053 
106054   assert( pName->nSrc==1 );
106055   zDb = pName->a[0].zDatabase;
106056   zName = pName->a[0].zName;
106057   nName = sqlite3Strlen30(zName);
106058   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
106059   for(i=OMIT_TEMPDB; i<db->nDb; i++){
106060     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
106061     if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
106062     assert( sqlite3SchemaMutexHeld(db, j, 0) );
106063     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
106064     if( pTrigger ) break;
106065   }
106066   if( !pTrigger ){
106067     if( !noErr ){
106068       sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
106069     }else{
106070       sqlite3CodeVerifyNamedSchema(pParse, zDb);
106071     }
106072     pParse->checkSchema = 1;
106073     goto drop_trigger_cleanup;
106074   }
106075   sqlite3DropTriggerPtr(pParse, pTrigger);
106076 
106077 drop_trigger_cleanup:
106078   sqlite3SrcListDelete(db, pName);
106079 }
106080 
106081 /*
106082 ** Return a pointer to the Table structure for the table that a trigger
106083 ** is set on.
106084 */
106085 static Table *tableOfTrigger(Trigger *pTrigger){
106086   int n = sqlite3Strlen30(pTrigger->table);
106087   return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
106088 }
106089 
106090 
106091 /*
106092 ** Drop a trigger given a pointer to that trigger.
106093 */
106094 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
106095   Table   *pTable;
106096   Vdbe *v;
106097   sqlite3 *db = pParse->db;
106098   int iDb;
106099 
106100   iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
106101   assert( iDb>=0 && iDb<db->nDb );
106102   pTable = tableOfTrigger(pTrigger);
106103   assert( pTable );
106104   assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
106105 #ifndef SQLITE_OMIT_AUTHORIZATION
106106   {
106107     int code = SQLITE_DROP_TRIGGER;
106108     const char *zDb = db->aDb[iDb].zName;
106109     const char *zTab = SCHEMA_TABLE(iDb);
106110     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
106111     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
106112       sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
106113       return;
106114     }
106115   }
106116 #endif
106117 
106118   /* Generate code to destroy the database record of the trigger.
106119   */
106120   assert( pTable!=0 );
106121   if( (v = sqlite3GetVdbe(pParse))!=0 ){
106122     int base;
106123     static const int iLn = VDBE_OFFSET_LINENO(2);
106124     static const VdbeOpList dropTrigger[] = {
106125       { OP_Rewind,     0, ADDR(9),  0},
106126       { OP_String8,    0, 1,        0}, /* 1 */
106127       { OP_Column,     0, 1,        2},
106128       { OP_Ne,         2, ADDR(8),  1},
106129       { OP_String8,    0, 1,        0}, /* 4: "trigger" */
106130       { OP_Column,     0, 0,        2},
106131       { OP_Ne,         2, ADDR(8),  1},
106132       { OP_Delete,     0, 0,        0},
106133       { OP_Next,       0, ADDR(1),  0}, /* 8 */
106134     };
106135 
106136     sqlite3BeginWriteOperation(pParse, 0, iDb);
106137     sqlite3OpenMasterTable(pParse, iDb);
106138     base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger, iLn);
106139     sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
106140     sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
106141     sqlite3ChangeCookie(pParse, iDb);
106142     sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
106143     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
106144     if( pParse->nMem<3 ){
106145       pParse->nMem = 3;
106146     }
106147   }
106148 }
106149 
106150 /*
106151 ** Remove a trigger from the hash tables of the sqlite* pointer.
106152 */
106153 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
106154   Trigger *pTrigger;
106155   Hash *pHash;
106156 
106157   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
106158   pHash = &(db->aDb[iDb].pSchema->trigHash);
106159   pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
106160   if( ALWAYS(pTrigger) ){
106161     if( pTrigger->pSchema==pTrigger->pTabSchema ){
106162       Table *pTab = tableOfTrigger(pTrigger);
106163       Trigger **pp;
106164       for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
106165       *pp = (*pp)->pNext;
106166     }
106167     sqlite3DeleteTrigger(db, pTrigger);
106168     db->flags |= SQLITE_InternChanges;
106169   }
106170 }
106171 
106172 /*
106173 ** pEList is the SET clause of an UPDATE statement.  Each entry
106174 ** in pEList is of the format <id>=<expr>.  If any of the entries
106175 ** in pEList have an <id> which matches an identifier in pIdList,
106176 ** then return TRUE.  If pIdList==NULL, then it is considered a
106177 ** wildcard that matches anything.  Likewise if pEList==NULL then
106178 ** it matches anything so always return true.  Return false only
106179 ** if there is no match.
106180 */
106181 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
106182   int e;
106183   if( pIdList==0 || NEVER(pEList==0) ) return 1;
106184   for(e=0; e<pEList->nExpr; e++){
106185     if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
106186   }
106187   return 0;
106188 }
106189 
106190 /*
106191 ** Return a list of all triggers on table pTab if there exists at least
106192 ** one trigger that must be fired when an operation of type 'op' is
106193 ** performed on the table, and, if that operation is an UPDATE, if at
106194 ** least one of the columns in pChanges is being modified.
106195 */
106196 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
106197   Parse *pParse,          /* Parse context */
106198   Table *pTab,            /* The table the contains the triggers */
106199   int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
106200   ExprList *pChanges,     /* Columns that change in an UPDATE statement */
106201   int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
106202 ){
106203   int mask = 0;
106204   Trigger *pList = 0;
106205   Trigger *p;
106206 
106207   if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
106208     pList = sqlite3TriggerList(pParse, pTab);
106209   }
106210   assert( pList==0 || IsVirtual(pTab)==0 );
106211   for(p=pList; p; p=p->pNext){
106212     if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
106213       mask |= p->tr_tm;
106214     }
106215   }
106216   if( pMask ){
106217     *pMask = mask;
106218   }
106219   return (mask ? pList : 0);
106220 }
106221 
106222 /*
106223 ** Convert the pStep->target token into a SrcList and return a pointer
106224 ** to that SrcList.
106225 **
106226 ** This routine adds a specific database name, if needed, to the target when
106227 ** forming the SrcList.  This prevents a trigger in one database from
106228 ** referring to a target in another database.  An exception is when the
106229 ** trigger is in TEMP in which case it can refer to any other database it
106230 ** wants.
106231 */
106232 static SrcList *targetSrcList(
106233   Parse *pParse,       /* The parsing context */
106234   TriggerStep *pStep   /* The trigger containing the target token */
106235 ){
106236   int iDb;             /* Index of the database to use */
106237   SrcList *pSrc;       /* SrcList to be returned */
106238 
106239   pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
106240   if( pSrc ){
106241     assert( pSrc->nSrc>0 );
106242     assert( pSrc->a!=0 );
106243     iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
106244     if( iDb==0 || iDb>=2 ){
106245       sqlite3 *db = pParse->db;
106246       assert( iDb<pParse->db->nDb );
106247       pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
106248     }
106249   }
106250   return pSrc;
106251 }
106252 
106253 /*
106254 ** Generate VDBE code for the statements inside the body of a single
106255 ** trigger.
106256 */
106257 static int codeTriggerProgram(
106258   Parse *pParse,            /* The parser context */
106259   TriggerStep *pStepList,   /* List of statements inside the trigger body */
106260   int orconf                /* Conflict algorithm. (OE_Abort, etc) */
106261 ){
106262   TriggerStep *pStep;
106263   Vdbe *v = pParse->pVdbe;
106264   sqlite3 *db = pParse->db;
106265 
106266   assert( pParse->pTriggerTab && pParse->pToplevel );
106267   assert( pStepList );
106268   assert( v!=0 );
106269   for(pStep=pStepList; pStep; pStep=pStep->pNext){
106270     /* Figure out the ON CONFLICT policy that will be used for this step
106271     ** of the trigger program. If the statement that caused this trigger
106272     ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
106273     ** the ON CONFLICT policy that was specified as part of the trigger
106274     ** step statement. Example:
106275     **
106276     **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
106277     **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
106278     **   END;
106279     **
106280     **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
106281     **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
106282     */
106283     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
106284     assert( pParse->okConstFactor==0 );
106285 
106286     switch( pStep->op ){
106287       case TK_UPDATE: {
106288         sqlite3Update(pParse,
106289           targetSrcList(pParse, pStep),
106290           sqlite3ExprListDup(db, pStep->pExprList, 0),
106291           sqlite3ExprDup(db, pStep->pWhere, 0),
106292           pParse->eOrconf
106293         );
106294         break;
106295       }
106296       case TK_INSERT: {
106297         sqlite3Insert(pParse,
106298           targetSrcList(pParse, pStep),
106299           sqlite3SelectDup(db, pStep->pSelect, 0),
106300           sqlite3IdListDup(db, pStep->pIdList),
106301           pParse->eOrconf
106302         );
106303         break;
106304       }
106305       case TK_DELETE: {
106306         sqlite3DeleteFrom(pParse,
106307           targetSrcList(pParse, pStep),
106308           sqlite3ExprDup(db, pStep->pWhere, 0)
106309         );
106310         break;
106311       }
106312       default: assert( pStep->op==TK_SELECT ); {
106313         SelectDest sDest;
106314         Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
106315         sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
106316         sqlite3Select(pParse, pSelect, &sDest);
106317         sqlite3SelectDelete(db, pSelect);
106318         break;
106319       }
106320     }
106321     if( pStep->op!=TK_SELECT ){
106322       sqlite3VdbeAddOp0(v, OP_ResetCount);
106323     }
106324   }
106325 
106326   return 0;
106327 }
106328 
106329 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
106330 /*
106331 ** This function is used to add VdbeComment() annotations to a VDBE
106332 ** program. It is not used in production code, only for debugging.
106333 */
106334 static const char *onErrorText(int onError){
106335   switch( onError ){
106336     case OE_Abort:    return "abort";
106337     case OE_Rollback: return "rollback";
106338     case OE_Fail:     return "fail";
106339     case OE_Replace:  return "replace";
106340     case OE_Ignore:   return "ignore";
106341     case OE_Default:  return "default";
106342   }
106343   return "n/a";
106344 }
106345 #endif
106346 
106347 /*
106348 ** Parse context structure pFrom has just been used to create a sub-vdbe
106349 ** (trigger program). If an error has occurred, transfer error information
106350 ** from pFrom to pTo.
106351 */
106352 static void transferParseError(Parse *pTo, Parse *pFrom){
106353   assert( pFrom->zErrMsg==0 || pFrom->nErr );
106354   assert( pTo->zErrMsg==0 || pTo->nErr );
106355   if( pTo->nErr==0 ){
106356     pTo->zErrMsg = pFrom->zErrMsg;
106357     pTo->nErr = pFrom->nErr;
106358   }else{
106359     sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
106360   }
106361 }
106362 
106363 /*
106364 ** Create and populate a new TriggerPrg object with a sub-program
106365 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
106366 */
106367 static TriggerPrg *codeRowTrigger(
106368   Parse *pParse,       /* Current parse context */
106369   Trigger *pTrigger,   /* Trigger to code */
106370   Table *pTab,         /* The table pTrigger is attached to */
106371   int orconf           /* ON CONFLICT policy to code trigger program with */
106372 ){
106373   Parse *pTop = sqlite3ParseToplevel(pParse);
106374   sqlite3 *db = pParse->db;   /* Database handle */
106375   TriggerPrg *pPrg;           /* Value to return */
106376   Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
106377   Vdbe *v;                    /* Temporary VM */
106378   NameContext sNC;            /* Name context for sub-vdbe */
106379   SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
106380   Parse *pSubParse;           /* Parse context for sub-vdbe */
106381   int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
106382 
106383   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
106384   assert( pTop->pVdbe );
106385 
106386   /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
106387   ** are freed if an error occurs, link them into the Parse.pTriggerPrg
106388   ** list of the top-level Parse object sooner rather than later.  */
106389   pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
106390   if( !pPrg ) return 0;
106391   pPrg->pNext = pTop->pTriggerPrg;
106392   pTop->pTriggerPrg = pPrg;
106393   pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
106394   if( !pProgram ) return 0;
106395   sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
106396   pPrg->pTrigger = pTrigger;
106397   pPrg->orconf = orconf;
106398   pPrg->aColmask[0] = 0xffffffff;
106399   pPrg->aColmask[1] = 0xffffffff;
106400 
106401   /* Allocate and populate a new Parse context to use for coding the
106402   ** trigger sub-program.  */
106403   pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
106404   if( !pSubParse ) return 0;
106405   memset(&sNC, 0, sizeof(sNC));
106406   sNC.pParse = pSubParse;
106407   pSubParse->db = db;
106408   pSubParse->pTriggerTab = pTab;
106409   pSubParse->pToplevel = pTop;
106410   pSubParse->zAuthContext = pTrigger->zName;
106411   pSubParse->eTriggerOp = pTrigger->op;
106412   pSubParse->nQueryLoop = pParse->nQueryLoop;
106413 
106414   v = sqlite3GetVdbe(pSubParse);
106415   if( v ){
106416     VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
106417       pTrigger->zName, onErrorText(orconf),
106418       (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
106419         (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
106420         (pTrigger->op==TK_INSERT ? "INSERT" : ""),
106421         (pTrigger->op==TK_DELETE ? "DELETE" : ""),
106422       pTab->zName
106423     ));
106424 #ifndef SQLITE_OMIT_TRACE
106425     sqlite3VdbeChangeP4(v, -1,
106426       sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
106427     );
106428 #endif
106429 
106430     /* If one was specified, code the WHEN clause. If it evaluates to false
106431     ** (or NULL) the sub-vdbe is immediately halted by jumping to the
106432     ** OP_Halt inserted at the end of the program.  */
106433     if( pTrigger->pWhen ){
106434       pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
106435       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
106436        && db->mallocFailed==0
106437       ){
106438         iEndTrigger = sqlite3VdbeMakeLabel(v);
106439         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
106440       }
106441       sqlite3ExprDelete(db, pWhen);
106442     }
106443 
106444     /* Code the trigger program into the sub-vdbe. */
106445     codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
106446 
106447     /* Insert an OP_Halt at the end of the sub-program. */
106448     if( iEndTrigger ){
106449       sqlite3VdbeResolveLabel(v, iEndTrigger);
106450     }
106451     sqlite3VdbeAddOp0(v, OP_Halt);
106452     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
106453 
106454     transferParseError(pParse, pSubParse);
106455     if( db->mallocFailed==0 ){
106456       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
106457     }
106458     pProgram->nMem = pSubParse->nMem;
106459     pProgram->nCsr = pSubParse->nTab;
106460     pProgram->nOnce = pSubParse->nOnce;
106461     pProgram->token = (void *)pTrigger;
106462     pPrg->aColmask[0] = pSubParse->oldmask;
106463     pPrg->aColmask[1] = pSubParse->newmask;
106464     sqlite3VdbeDelete(v);
106465   }
106466 
106467   assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
106468   assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
106469   sqlite3ParserReset(pSubParse);
106470   sqlite3StackFree(db, pSubParse);
106471 
106472   return pPrg;
106473 }
106474 
106475 /*
106476 ** Return a pointer to a TriggerPrg object containing the sub-program for
106477 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
106478 ** TriggerPrg object exists, a new object is allocated and populated before
106479 ** being returned.
106480 */
106481 static TriggerPrg *getRowTrigger(
106482   Parse *pParse,       /* Current parse context */
106483   Trigger *pTrigger,   /* Trigger to code */
106484   Table *pTab,         /* The table trigger pTrigger is attached to */
106485   int orconf           /* ON CONFLICT algorithm. */
106486 ){
106487   Parse *pRoot = sqlite3ParseToplevel(pParse);
106488   TriggerPrg *pPrg;
106489 
106490   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
106491 
106492   /* It may be that this trigger has already been coded (or is in the
106493   ** process of being coded). If this is the case, then an entry with
106494   ** a matching TriggerPrg.pTrigger field will be present somewhere
106495   ** in the Parse.pTriggerPrg list. Search for such an entry.  */
106496   for(pPrg=pRoot->pTriggerPrg;
106497       pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
106498       pPrg=pPrg->pNext
106499   );
106500 
106501   /* If an existing TriggerPrg could not be located, create a new one. */
106502   if( !pPrg ){
106503     pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
106504   }
106505 
106506   return pPrg;
106507 }
106508 
106509 /*
106510 ** Generate code for the trigger program associated with trigger p on
106511 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
106512 ** function are the same as those described in the header function for
106513 ** sqlite3CodeRowTrigger()
106514 */
106515 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
106516   Parse *pParse,       /* Parse context */
106517   Trigger *p,          /* Trigger to code */
106518   Table *pTab,         /* The table to code triggers from */
106519   int reg,             /* Reg array containing OLD.* and NEW.* values */
106520   int orconf,          /* ON CONFLICT policy */
106521   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
106522 ){
106523   Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
106524   TriggerPrg *pPrg;
106525   pPrg = getRowTrigger(pParse, p, pTab, orconf);
106526   assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
106527 
106528   /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
106529   ** is a pointer to the sub-vdbe containing the trigger program.  */
106530   if( pPrg ){
106531     int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
106532 
106533     sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
106534     sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
106535     VdbeComment(
106536         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
106537 
106538     /* Set the P5 operand of the OP_Program instruction to non-zero if
106539     ** recursive invocation of this trigger program is disallowed. Recursive
106540     ** invocation is disallowed if (a) the sub-program is really a trigger,
106541     ** not a foreign key action, and (b) the flag to enable recursive triggers
106542     ** is clear.  */
106543     sqlite3VdbeChangeP5(v, (u8)bRecursive);
106544   }
106545 }
106546 
106547 /*
106548 ** This is called to code the required FOR EACH ROW triggers for an operation
106549 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
106550 ** is given by the op parameter. The tr_tm parameter determines whether the
106551 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
106552 ** parameter pChanges is passed the list of columns being modified.
106553 **
106554 ** If there are no triggers that fire at the specified time for the specified
106555 ** operation on pTab, this function is a no-op.
106556 **
106557 ** The reg argument is the address of the first in an array of registers
106558 ** that contain the values substituted for the new.* and old.* references
106559 ** in the trigger program. If N is the number of columns in table pTab
106560 ** (a copy of pTab->nCol), then registers are populated as follows:
106561 **
106562 **   Register       Contains
106563 **   ------------------------------------------------------
106564 **   reg+0          OLD.rowid
106565 **   reg+1          OLD.* value of left-most column of pTab
106566 **   ...            ...
106567 **   reg+N          OLD.* value of right-most column of pTab
106568 **   reg+N+1        NEW.rowid
106569 **   reg+N+2        OLD.* value of left-most column of pTab
106570 **   ...            ...
106571 **   reg+N+N+1      NEW.* value of right-most column of pTab
106572 **
106573 ** For ON DELETE triggers, the registers containing the NEW.* values will
106574 ** never be accessed by the trigger program, so they are not allocated or
106575 ** populated by the caller (there is no data to populate them with anyway).
106576 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
106577 ** are never accessed, and so are not allocated by the caller. So, for an
106578 ** ON INSERT trigger, the value passed to this function as parameter reg
106579 ** is not a readable register, although registers (reg+N) through
106580 ** (reg+N+N+1) are.
106581 **
106582 ** Parameter orconf is the default conflict resolution algorithm for the
106583 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
106584 ** is the instruction that control should jump to if a trigger program
106585 ** raises an IGNORE exception.
106586 */
106587 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
106588   Parse *pParse,       /* Parse context */
106589   Trigger *pTrigger,   /* List of triggers on table pTab */
106590   int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
106591   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
106592   int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
106593   Table *pTab,         /* The table to code triggers from */
106594   int reg,             /* The first in an array of registers (see above) */
106595   int orconf,          /* ON CONFLICT policy */
106596   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
106597 ){
106598   Trigger *p;          /* Used to iterate through pTrigger list */
106599 
106600   assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
106601   assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
106602   assert( (op==TK_UPDATE)==(pChanges!=0) );
106603 
106604   for(p=pTrigger; p; p=p->pNext){
106605 
106606     /* Sanity checking:  The schema for the trigger and for the table are
106607     ** always defined.  The trigger must be in the same schema as the table
106608     ** or else it must be a TEMP trigger. */
106609     assert( p->pSchema!=0 );
106610     assert( p->pTabSchema!=0 );
106611     assert( p->pSchema==p->pTabSchema
106612          || p->pSchema==pParse->db->aDb[1].pSchema );
106613 
106614     /* Determine whether we should code this trigger */
106615     if( p->op==op
106616      && p->tr_tm==tr_tm
106617      && checkColumnOverlap(p->pColumns, pChanges)
106618     ){
106619       sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
106620     }
106621   }
106622 }
106623 
106624 /*
106625 ** Triggers may access values stored in the old.* or new.* pseudo-table.
106626 ** This function returns a 32-bit bitmask indicating which columns of the
106627 ** old.* or new.* tables actually are used by triggers. This information
106628 ** may be used by the caller, for example, to avoid having to load the entire
106629 ** old.* record into memory when executing an UPDATE or DELETE command.
106630 **
106631 ** Bit 0 of the returned mask is set if the left-most column of the
106632 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
106633 ** the second leftmost column value is required, and so on. If there
106634 ** are more than 32 columns in the table, and at least one of the columns
106635 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
106636 **
106637 ** It is not possible to determine if the old.rowid or new.rowid column is
106638 ** accessed by triggers. The caller must always assume that it is.
106639 **
106640 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
106641 ** applies to the old.* table. If 1, the new.* table.
106642 **
106643 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
106644 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
106645 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
106646 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
106647 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
106648 */
106649 SQLITE_PRIVATE u32 sqlite3TriggerColmask(
106650   Parse *pParse,       /* Parse context */
106651   Trigger *pTrigger,   /* List of triggers on table pTab */
106652   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
106653   int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
106654   int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
106655   Table *pTab,         /* The table to code triggers from */
106656   int orconf           /* Default ON CONFLICT policy for trigger steps */
106657 ){
106658   const int op = pChanges ? TK_UPDATE : TK_DELETE;
106659   u32 mask = 0;
106660   Trigger *p;
106661 
106662   assert( isNew==1 || isNew==0 );
106663   for(p=pTrigger; p; p=p->pNext){
106664     if( p->op==op && (tr_tm&p->tr_tm)
106665      && checkColumnOverlap(p->pColumns,pChanges)
106666     ){
106667       TriggerPrg *pPrg;
106668       pPrg = getRowTrigger(pParse, p, pTab, orconf);
106669       if( pPrg ){
106670         mask |= pPrg->aColmask[isNew];
106671       }
106672     }
106673   }
106674 
106675   return mask;
106676 }
106677 
106678 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
106679 
106680 /************** End of trigger.c *********************************************/
106681 /************** Begin file update.c ******************************************/
106682 /*
106683 ** 2001 September 15
106684 **
106685 ** The author disclaims copyright to this source code.  In place of
106686 ** a legal notice, here is a blessing:
106687 **
106688 **    May you do good and not evil.
106689 **    May you find forgiveness for yourself and forgive others.
106690 **    May you share freely, never taking more than you give.
106691 **
106692 *************************************************************************
106693 ** This file contains C code routines that are called by the parser
106694 ** to handle UPDATE statements.
106695 */
106696 
106697 #ifndef SQLITE_OMIT_VIRTUALTABLE
106698 /* Forward declaration */
106699 static void updateVirtualTable(
106700   Parse *pParse,       /* The parsing context */
106701   SrcList *pSrc,       /* The virtual table to be modified */
106702   Table *pTab,         /* The virtual table */
106703   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
106704   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
106705   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
106706   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
106707   int onError          /* ON CONFLICT strategy */
106708 );
106709 #endif /* SQLITE_OMIT_VIRTUALTABLE */
106710 
106711 /*
106712 ** The most recently coded instruction was an OP_Column to retrieve the
106713 ** i-th column of table pTab. This routine sets the P4 parameter of the
106714 ** OP_Column to the default value, if any.
106715 **
106716 ** The default value of a column is specified by a DEFAULT clause in the
106717 ** column definition. This was either supplied by the user when the table
106718 ** was created, or added later to the table definition by an ALTER TABLE
106719 ** command. If the latter, then the row-records in the table btree on disk
106720 ** may not contain a value for the column and the default value, taken
106721 ** from the P4 parameter of the OP_Column instruction, is returned instead.
106722 ** If the former, then all row-records are guaranteed to include a value
106723 ** for the column and the P4 value is not required.
106724 **
106725 ** Column definitions created by an ALTER TABLE command may only have
106726 ** literal default values specified: a number, null or a string. (If a more
106727 ** complicated default expression value was provided, it is evaluated
106728 ** when the ALTER TABLE is executed and one of the literal values written
106729 ** into the sqlite_master table.)
106730 **
106731 ** Therefore, the P4 parameter is only required if the default value for
106732 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
106733 ** function is capable of transforming these types of expressions into
106734 ** sqlite3_value objects.
106735 **
106736 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
106737 ** on register iReg. This is used when an equivalent integer value is
106738 ** stored in place of an 8-byte floating point value in order to save
106739 ** space.
106740 */
106741 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
106742   assert( pTab!=0 );
106743   if( !pTab->pSelect ){
106744     sqlite3_value *pValue = 0;
106745     u8 enc = ENC(sqlite3VdbeDb(v));
106746     Column *pCol = &pTab->aCol[i];
106747     VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
106748     assert( i<pTab->nCol );
106749     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
106750                          pCol->affinity, &pValue);
106751     if( pValue ){
106752       sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
106753     }
106754 #ifndef SQLITE_OMIT_FLOATING_POINT
106755     if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
106756       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
106757     }
106758 #endif
106759   }
106760 }
106761 
106762 /*
106763 ** Process an UPDATE statement.
106764 **
106765 **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
106766 **          \_______/ \________/     \______/       \________________/
106767 *            onError   pTabList      pChanges             pWhere
106768 */
106769 SQLITE_PRIVATE void sqlite3Update(
106770   Parse *pParse,         /* The parser context */
106771   SrcList *pTabList,     /* The table in which we should change things */
106772   ExprList *pChanges,    /* Things to be changed */
106773   Expr *pWhere,          /* The WHERE clause.  May be null */
106774   int onError            /* How to handle constraint errors */
106775 ){
106776   int i, j;              /* Loop counters */
106777   Table *pTab;           /* The table to be updated */
106778   int addrTop = 0;       /* VDBE instruction address of the start of the loop */
106779   WhereInfo *pWInfo;     /* Information about the WHERE clause */
106780   Vdbe *v;               /* The virtual database engine */
106781   Index *pIdx;           /* For looping over indices */
106782   Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
106783   int nIdx;              /* Number of indices that need updating */
106784   int iBaseCur;          /* Base cursor number */
106785   int iDataCur;          /* Cursor for the canonical data btree */
106786   int iIdxCur;           /* Cursor for the first index */
106787   sqlite3 *db;           /* The database structure */
106788   int *aRegIdx = 0;      /* One register assigned to each index to be updated */
106789   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
106790                          ** an expression for the i-th column of the table.
106791                          ** aXRef[i]==-1 if the i-th column is not changed. */
106792   u8 *aToOpen;           /* 1 for tables and indices to be opened */
106793   u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
106794   u8 chngRowid;          /* Rowid changed in a normal table */
106795   u8 chngKey;            /* Either chngPk or chngRowid */
106796   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
106797   AuthContext sContext;  /* The authorization context */
106798   NameContext sNC;       /* The name-context to resolve expressions in */
106799   int iDb;               /* Database containing the table being updated */
106800   int okOnePass;         /* True for one-pass algorithm without the FIFO */
106801   int hasFK;             /* True if foreign key processing is required */
106802   int labelBreak;        /* Jump here to break out of UPDATE loop */
106803   int labelContinue;     /* Jump here to continue next step of UPDATE loop */
106804 
106805 #ifndef SQLITE_OMIT_TRIGGER
106806   int isView;            /* True when updating a view (INSTEAD OF trigger) */
106807   Trigger *pTrigger;     /* List of triggers on pTab, if required */
106808   int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
106809 #endif
106810   int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
106811   int iEph = 0;          /* Ephemeral table holding all primary key values */
106812   int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
106813   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
106814 
106815   /* Register Allocations */
106816   int regRowCount = 0;   /* A count of rows changed */
106817   int regOldRowid;       /* The old rowid */
106818   int regNewRowid;       /* The new rowid */
106819   int regNew;            /* Content of the NEW.* table in triggers */
106820   int regOld = 0;        /* Content of OLD.* table in triggers */
106821   int regRowSet = 0;     /* Rowset of rows to be updated */
106822   int regKey = 0;        /* composite PRIMARY KEY value */
106823 
106824   memset(&sContext, 0, sizeof(sContext));
106825   db = pParse->db;
106826   if( pParse->nErr || db->mallocFailed ){
106827     goto update_cleanup;
106828   }
106829   assert( pTabList->nSrc==1 );
106830 
106831   /* Locate the table which we want to update.
106832   */
106833   pTab = sqlite3SrcListLookup(pParse, pTabList);
106834   if( pTab==0 ) goto update_cleanup;
106835   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
106836 
106837   /* Figure out if we have any triggers and if the table being
106838   ** updated is a view.
106839   */
106840 #ifndef SQLITE_OMIT_TRIGGER
106841   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
106842   isView = pTab->pSelect!=0;
106843   assert( pTrigger || tmask==0 );
106844 #else
106845 # define pTrigger 0
106846 # define isView 0
106847 # define tmask 0
106848 #endif
106849 #ifdef SQLITE_OMIT_VIEW
106850 # undef isView
106851 # define isView 0
106852 #endif
106853 
106854   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
106855     goto update_cleanup;
106856   }
106857   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
106858     goto update_cleanup;
106859   }
106860 
106861   /* Allocate a cursors for the main database table and for all indices.
106862   ** The index cursors might not be used, but if they are used they
106863   ** need to occur right after the database cursor.  So go ahead and
106864   ** allocate enough space, just in case.
106865   */
106866   pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
106867   iIdxCur = iDataCur+1;
106868   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
106869   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
106870     if( pIdx->autoIndex==2 && pPk!=0 ){
106871       iDataCur = pParse->nTab;
106872       pTabList->a[0].iCursor = iDataCur;
106873     }
106874     pParse->nTab++;
106875   }
106876 
106877   /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
106878   ** Initialize aXRef[] and aToOpen[] to their default values.
106879   */
106880   aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
106881   if( aXRef==0 ) goto update_cleanup;
106882   aRegIdx = aXRef+pTab->nCol;
106883   aToOpen = (u8*)(aRegIdx+nIdx);
106884   memset(aToOpen, 1, nIdx+1);
106885   aToOpen[nIdx+1] = 0;
106886   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
106887 
106888   /* Initialize the name-context */
106889   memset(&sNC, 0, sizeof(sNC));
106890   sNC.pParse = pParse;
106891   sNC.pSrcList = pTabList;
106892 
106893   /* Resolve the column names in all the expressions of the
106894   ** of the UPDATE statement.  Also find the column index
106895   ** for each column to be updated in the pChanges array.  For each
106896   ** column to be updated, make sure we have authorization to change
106897   ** that column.
106898   */
106899   chngRowid = chngPk = 0;
106900   for(i=0; i<pChanges->nExpr; i++){
106901     if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
106902       goto update_cleanup;
106903     }
106904     for(j=0; j<pTab->nCol; j++){
106905       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
106906         if( j==pTab->iPKey ){
106907           chngRowid = 1;
106908           pRowidExpr = pChanges->a[i].pExpr;
106909         }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
106910           chngPk = 1;
106911         }
106912         aXRef[j] = i;
106913         break;
106914       }
106915     }
106916     if( j>=pTab->nCol ){
106917       if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
106918         j = -1;
106919         chngRowid = 1;
106920         pRowidExpr = pChanges->a[i].pExpr;
106921       }else{
106922         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
106923         pParse->checkSchema = 1;
106924         goto update_cleanup;
106925       }
106926     }
106927 #ifndef SQLITE_OMIT_AUTHORIZATION
106928     {
106929       int rc;
106930       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
106931                             j<0 ? "ROWID" : pTab->aCol[j].zName,
106932                             db->aDb[iDb].zName);
106933       if( rc==SQLITE_DENY ){
106934         goto update_cleanup;
106935       }else if( rc==SQLITE_IGNORE ){
106936         aXRef[j] = -1;
106937       }
106938     }
106939 #endif
106940   }
106941   assert( (chngRowid & chngPk)==0 );
106942   assert( chngRowid==0 || chngRowid==1 );
106943   assert( chngPk==0 || chngPk==1 );
106944   chngKey = chngRowid + chngPk;
106945 
106946   /* The SET expressions are not actually used inside the WHERE loop.
106947   ** So reset the colUsed mask
106948   */
106949   pTabList->a[0].colUsed = 0;
106950 
106951   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
106952 
106953   /* There is one entry in the aRegIdx[] array for each index on the table
106954   ** being updated.  Fill in aRegIdx[] with a register number that will hold
106955   ** the key for accessing each index.
106956   */
106957   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
106958     int reg;
106959     if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
106960       reg = ++pParse->nMem;
106961     }else{
106962       reg = 0;
106963       for(i=0; i<pIdx->nKeyCol; i++){
106964         if( aXRef[pIdx->aiColumn[i]]>=0 ){
106965           reg = ++pParse->nMem;
106966           break;
106967         }
106968       }
106969     }
106970     if( reg==0 ) aToOpen[j+1] = 0;
106971     aRegIdx[j] = reg;
106972   }
106973 
106974   /* Begin generating code. */
106975   v = sqlite3GetVdbe(pParse);
106976   if( v==0 ) goto update_cleanup;
106977   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
106978   sqlite3BeginWriteOperation(pParse, 1, iDb);
106979 
106980 #ifndef SQLITE_OMIT_VIRTUALTABLE
106981   /* Virtual tables must be handled separately */
106982   if( IsVirtual(pTab) ){
106983     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
106984                        pWhere, onError);
106985     pWhere = 0;
106986     pTabList = 0;
106987     goto update_cleanup;
106988   }
106989 #endif
106990 
106991   /* Allocate required registers. */
106992   regRowSet = ++pParse->nMem;
106993   regOldRowid = regNewRowid = ++pParse->nMem;
106994   if( chngPk || pTrigger || hasFK ){
106995     regOld = pParse->nMem + 1;
106996     pParse->nMem += pTab->nCol;
106997   }
106998   if( chngKey || pTrigger || hasFK ){
106999     regNewRowid = ++pParse->nMem;
107000   }
107001   regNew = pParse->nMem + 1;
107002   pParse->nMem += pTab->nCol;
107003 
107004   /* Start the view context. */
107005   if( isView ){
107006     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
107007   }
107008 
107009   /* If we are trying to update a view, realize that view into
107010   ** a ephemeral table.
107011   */
107012 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
107013   if( isView ){
107014     sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
107015   }
107016 #endif
107017 
107018   /* Resolve the column names in all the expressions in the
107019   ** WHERE clause.
107020   */
107021   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
107022     goto update_cleanup;
107023   }
107024 
107025   /* Begin the database scan
107026   */
107027   if( HasRowid(pTab) ){
107028     sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
107029     pWInfo = sqlite3WhereBegin(
107030         pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
107031     );
107032     if( pWInfo==0 ) goto update_cleanup;
107033     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
107034 
107035     /* Remember the rowid of every item to be updated.
107036     */
107037     sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
107038     if( !okOnePass ){
107039       sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
107040     }
107041 
107042     /* End the database scan loop.
107043     */
107044     sqlite3WhereEnd(pWInfo);
107045   }else{
107046     int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
107047     i16 nPk;         /* Number of components of the PRIMARY KEY */
107048     int addrOpen;    /* Address of the OpenEphemeral instruction */
107049 
107050     assert( pPk!=0 );
107051     nPk = pPk->nKeyCol;
107052     iPk = pParse->nMem+1;
107053     pParse->nMem += nPk;
107054     regKey = ++pParse->nMem;
107055     iEph = pParse->nTab++;
107056     sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
107057     addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
107058     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
107059     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
107060                                WHERE_ONEPASS_DESIRED, iIdxCur);
107061     if( pWInfo==0 ) goto update_cleanup;
107062     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
107063     for(i=0; i<nPk; i++){
107064       sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
107065                                       iPk+i);
107066     }
107067     if( okOnePass ){
107068       sqlite3VdbeChangeToNoop(v, addrOpen);
107069       nKey = nPk;
107070       regKey = iPk;
107071     }else{
107072       sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
107073                         sqlite3IndexAffinityStr(v, pPk), nPk);
107074       sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
107075     }
107076     sqlite3WhereEnd(pWInfo);
107077   }
107078 
107079   /* Initialize the count of updated rows
107080   */
107081   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
107082     regRowCount = ++pParse->nMem;
107083     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
107084   }
107085 
107086   labelBreak = sqlite3VdbeMakeLabel(v);
107087   if( !isView ){
107088     /*
107089     ** Open every index that needs updating.  Note that if any
107090     ** index could potentially invoke a REPLACE conflict resolution
107091     ** action, then we need to open all indices because we might need
107092     ** to be deleting some records.
107093     */
107094     if( onError==OE_Replace ){
107095       memset(aToOpen, 1, nIdx+1);
107096     }else{
107097       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
107098         if( pIdx->onError==OE_Replace ){
107099           memset(aToOpen, 1, nIdx+1);
107100           break;
107101         }
107102       }
107103     }
107104     if( okOnePass ){
107105       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
107106       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
107107     }
107108     sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
107109                                0, 0);
107110   }
107111 
107112   /* Top of the update loop */
107113   if( okOnePass ){
107114     if( aToOpen[iDataCur-iBaseCur] ){
107115       assert( pPk!=0 );
107116       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
107117       VdbeCoverageNeverTaken(v);
107118     }
107119     labelContinue = labelBreak;
107120     sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
107121     VdbeCoverage(v);
107122   }else if( pPk ){
107123     labelContinue = sqlite3VdbeMakeLabel(v);
107124     sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak); VdbeCoverage(v);
107125     addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
107126     sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
107127     VdbeCoverage(v);
107128   }else{
107129     labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
107130                              regOldRowid);
107131     VdbeCoverage(v);
107132     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
107133     VdbeCoverage(v);
107134   }
107135 
107136   /* If the record number will change, set register regNewRowid to
107137   ** contain the new value. If the record number is not being modified,
107138   ** then regNewRowid is the same register as regOldRowid, which is
107139   ** already populated.  */
107140   assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
107141   if( chngRowid ){
107142     sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
107143     sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); VdbeCoverage(v);
107144   }
107145 
107146   /* Compute the old pre-UPDATE content of the row being changed, if that
107147   ** information is needed */
107148   if( chngPk || hasFK || pTrigger ){
107149     u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
107150     oldmask |= sqlite3TriggerColmask(pParse,
107151         pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
107152     );
107153     for(i=0; i<pTab->nCol; i++){
107154       if( oldmask==0xffffffff
107155        || (i<32 && (oldmask & MASKBIT32(i))!=0)
107156        || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
107157       ){
107158         testcase(  oldmask!=0xffffffff && i==31 );
107159         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
107160       }else{
107161         sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
107162       }
107163     }
107164     if( chngRowid==0 && pPk==0 ){
107165       sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
107166     }
107167   }
107168 
107169   /* Populate the array of registers beginning at regNew with the new
107170   ** row data. This array is used to check constaints, create the new
107171   ** table and index records, and as the values for any new.* references
107172   ** made by triggers.
107173   **
107174   ** If there are one or more BEFORE triggers, then do not populate the
107175   ** registers associated with columns that are (a) not modified by
107176   ** this UPDATE statement and (b) not accessed by new.* references. The
107177   ** values for registers not modified by the UPDATE must be reloaded from
107178   ** the database after the BEFORE triggers are fired anyway (as the trigger
107179   ** may have modified them). So not loading those that are not going to
107180   ** be used eliminates some redundant opcodes.
107181   */
107182   newmask = sqlite3TriggerColmask(
107183       pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
107184   );
107185   /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/
107186   for(i=0; i<pTab->nCol; i++){
107187     if( i==pTab->iPKey ){
107188       sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
107189     }else{
107190       j = aXRef[i];
107191       if( j>=0 ){
107192         sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
107193       }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
107194         /* This branch loads the value of a column that will not be changed
107195         ** into a register. This is done if there are no BEFORE triggers, or
107196         ** if there are one or more BEFORE triggers that use this value via
107197         ** a new.* reference in a trigger program.
107198         */
107199         testcase( i==31 );
107200         testcase( i==32 );
107201         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
107202       }else{
107203         sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
107204       }
107205     }
107206   }
107207 
107208   /* Fire any BEFORE UPDATE triggers. This happens before constraints are
107209   ** verified. One could argue that this is wrong.
107210   */
107211   if( tmask&TRIGGER_BEFORE ){
107212     sqlite3TableAffinity(v, pTab, regNew);
107213     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
107214         TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
107215 
107216     /* The row-trigger may have deleted the row being updated. In this
107217     ** case, jump to the next row. No updates or AFTER triggers are
107218     ** required. This behavior - what happens when the row being updated
107219     ** is deleted or renamed by a BEFORE trigger - is left undefined in the
107220     ** documentation.
107221     */
107222     if( pPk ){
107223       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
107224       VdbeCoverage(v);
107225     }else{
107226       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
107227       VdbeCoverage(v);
107228     }
107229 
107230     /* If it did not delete it, the row-trigger may still have modified
107231     ** some of the columns of the row being updated. Load the values for
107232     ** all columns not modified by the update statement into their
107233     ** registers in case this has happened.
107234     */
107235     for(i=0; i<pTab->nCol; i++){
107236       if( aXRef[i]<0 && i!=pTab->iPKey ){
107237         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
107238       }
107239     }
107240   }
107241 
107242   if( !isView ){
107243     int j1 = 0;           /* Address of jump instruction */
107244     int bReplace = 0;     /* True if REPLACE conflict resolution might happen */
107245 
107246     /* Do constraint checks. */
107247     assert( regOldRowid>0 );
107248     sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
107249         regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
107250 
107251     /* Do FK constraint checks. */
107252     if( hasFK ){
107253       sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
107254     }
107255 
107256     /* Delete the index entries associated with the current record.  */
107257     if( bReplace || chngKey ){
107258       if( pPk ){
107259         j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
107260       }else{
107261         j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
107262       }
107263       VdbeCoverageNeverTaken(v);
107264     }
107265     sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx);
107266 
107267     /* If changing the record number, delete the old record.  */
107268     if( hasFK || chngKey || pPk!=0 ){
107269       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
107270     }
107271     if( bReplace || chngKey ){
107272       sqlite3VdbeJumpHere(v, j1);
107273     }
107274 
107275     if( hasFK ){
107276       sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
107277     }
107278 
107279     /* Insert the new index entries and the new record. */
107280     sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
107281                              regNewRowid, aRegIdx, 1, 0, 0);
107282 
107283     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
107284     ** handle rows (possibly in other tables) that refer via a foreign key
107285     ** to the row just updated. */
107286     if( hasFK ){
107287       sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
107288     }
107289   }
107290 
107291   /* Increment the row counter
107292   */
107293   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
107294     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
107295   }
107296 
107297   sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
107298       TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
107299 
107300   /* Repeat the above with the next record to be updated, until
107301   ** all record selected by the WHERE clause have been updated.
107302   */
107303   if( okOnePass ){
107304     /* Nothing to do at end-of-loop for a single-pass */
107305   }else if( pPk ){
107306     sqlite3VdbeResolveLabel(v, labelContinue);
107307     sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop); VdbeCoverage(v);
107308   }else{
107309     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
107310   }
107311   sqlite3VdbeResolveLabel(v, labelBreak);
107312 
107313   /* Close all tables */
107314   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
107315     assert( aRegIdx );
107316     if( aToOpen[i+1] ){
107317       sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
107318     }
107319   }
107320   if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
107321 
107322   /* Update the sqlite_sequence table by storing the content of the
107323   ** maximum rowid counter values recorded while inserting into
107324   ** autoincrement tables.
107325   */
107326   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
107327     sqlite3AutoincrementEnd(pParse);
107328   }
107329 
107330   /*
107331   ** Return the number of rows that were changed. If this routine is
107332   ** generating code because of a call to sqlite3NestedParse(), do not
107333   ** invoke the callback function.
107334   */
107335   if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
107336     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
107337     sqlite3VdbeSetNumCols(v, 1);
107338     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
107339   }
107340 
107341 update_cleanup:
107342   sqlite3AuthContextPop(&sContext);
107343   sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
107344   sqlite3SrcListDelete(db, pTabList);
107345   sqlite3ExprListDelete(db, pChanges);
107346   sqlite3ExprDelete(db, pWhere);
107347   return;
107348 }
107349 /* Make sure "isView" and other macros defined above are undefined. Otherwise
107350 ** thely may interfere with compilation of other functions in this file
107351 ** (or in another file, if this file becomes part of the amalgamation).  */
107352 #ifdef isView
107353  #undef isView
107354 #endif
107355 #ifdef pTrigger
107356  #undef pTrigger
107357 #endif
107358 
107359 #ifndef SQLITE_OMIT_VIRTUALTABLE
107360 /*
107361 ** Generate code for an UPDATE of a virtual table.
107362 **
107363 ** The strategy is that we create an ephemerial table that contains
107364 ** for each row to be changed:
107365 **
107366 **   (A)  The original rowid of that row.
107367 **   (B)  The revised rowid for the row. (note1)
107368 **   (C)  The content of every column in the row.
107369 **
107370 ** Then we loop over this ephemeral table and for each row in
107371 ** the ephermeral table call VUpdate.
107372 **
107373 ** When finished, drop the ephemeral table.
107374 **
107375 ** (note1) Actually, if we know in advance that (A) is always the same
107376 ** as (B) we only store (A), then duplicate (A) when pulling
107377 ** it out of the ephemeral table before calling VUpdate.
107378 */
107379 static void updateVirtualTable(
107380   Parse *pParse,       /* The parsing context */
107381   SrcList *pSrc,       /* The virtual table to be modified */
107382   Table *pTab,         /* The virtual table */
107383   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
107384   Expr *pRowid,        /* Expression used to recompute the rowid */
107385   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
107386   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
107387   int onError          /* ON CONFLICT strategy */
107388 ){
107389   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
107390   ExprList *pEList = 0;     /* The result set of the SELECT statement */
107391   Select *pSelect = 0;      /* The SELECT statement */
107392   Expr *pExpr;              /* Temporary expression */
107393   int ephemTab;             /* Table holding the result of the SELECT */
107394   int i;                    /* Loop counter */
107395   int addr;                 /* Address of top of loop */
107396   int iReg;                 /* First register in set passed to OP_VUpdate */
107397   sqlite3 *db = pParse->db; /* Database connection */
107398   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
107399   SelectDest dest;
107400 
107401   /* Construct the SELECT statement that will find the new values for
107402   ** all updated rows.
107403   */
107404   pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
107405   if( pRowid ){
107406     pEList = sqlite3ExprListAppend(pParse, pEList,
107407                                    sqlite3ExprDup(db, pRowid, 0));
107408   }
107409   assert( pTab->iPKey<0 );
107410   for(i=0; i<pTab->nCol; i++){
107411     if( aXRef[i]>=0 ){
107412       pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
107413     }else{
107414       pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
107415     }
107416     pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
107417   }
107418   pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
107419 
107420   /* Create the ephemeral table into which the update results will
107421   ** be stored.
107422   */
107423   assert( v );
107424   ephemTab = pParse->nTab++;
107425   sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
107426   sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
107427 
107428   /* fill the ephemeral table
107429   */
107430   sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
107431   sqlite3Select(pParse, pSelect, &dest);
107432 
107433   /* Generate code to scan the ephemeral table and call VUpdate. */
107434   iReg = ++pParse->nMem;
107435   pParse->nMem += pTab->nCol+1;
107436   addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); VdbeCoverage(v);
107437   sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
107438   sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
107439   for(i=0; i<pTab->nCol; i++){
107440     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
107441   }
107442   sqlite3VtabMakeWritable(pParse, pTab);
107443   sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
107444   sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
107445   sqlite3MayAbort(pParse);
107446   sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); VdbeCoverage(v);
107447   sqlite3VdbeJumpHere(v, addr);
107448   sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
107449 
107450   /* Cleanup */
107451   sqlite3SelectDelete(db, pSelect);
107452 }
107453 #endif /* SQLITE_OMIT_VIRTUALTABLE */
107454 
107455 /************** End of update.c **********************************************/
107456 /************** Begin file vacuum.c ******************************************/
107457 /*
107458 ** 2003 April 6
107459 **
107460 ** The author disclaims copyright to this source code.  In place of
107461 ** a legal notice, here is a blessing:
107462 **
107463 **    May you do good and not evil.
107464 **    May you find forgiveness for yourself and forgive others.
107465 **    May you share freely, never taking more than you give.
107466 **
107467 *************************************************************************
107468 ** This file contains code used to implement the VACUUM command.
107469 **
107470 ** Most of the code in this file may be omitted by defining the
107471 ** SQLITE_OMIT_VACUUM macro.
107472 */
107473 
107474 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
107475 /*
107476 ** Finalize a prepared statement.  If there was an error, store the
107477 ** text of the error message in *pzErrMsg.  Return the result code.
107478 */
107479 static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
107480   int rc;
107481   rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
107482   if( rc ){
107483     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
107484   }
107485   return rc;
107486 }
107487 
107488 /*
107489 ** Execute zSql on database db. Return an error code.
107490 */
107491 static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
107492   sqlite3_stmt *pStmt;
107493   VVA_ONLY( int rc; )
107494   if( !zSql ){
107495     return SQLITE_NOMEM;
107496   }
107497   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
107498     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
107499     return sqlite3_errcode(db);
107500   }
107501   VVA_ONLY( rc = ) sqlite3_step(pStmt);
107502   assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
107503   return vacuumFinalize(db, pStmt, pzErrMsg);
107504 }
107505 
107506 /*
107507 ** Execute zSql on database db. The statement returns exactly
107508 ** one column. Execute this as SQL on the same database.
107509 */
107510 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
107511   sqlite3_stmt *pStmt;
107512   int rc;
107513 
107514   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
107515   if( rc!=SQLITE_OK ) return rc;
107516 
107517   while( SQLITE_ROW==sqlite3_step(pStmt) ){
107518     rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
107519     if( rc!=SQLITE_OK ){
107520       vacuumFinalize(db, pStmt, pzErrMsg);
107521       return rc;
107522     }
107523   }
107524 
107525   return vacuumFinalize(db, pStmt, pzErrMsg);
107526 }
107527 
107528 /*
107529 ** The VACUUM command is used to clean up the database,
107530 ** collapse free space, etc.  It is modelled after the VACUUM command
107531 ** in PostgreSQL.  The VACUUM command works as follows:
107532 **
107533 **   (1)  Create a new transient database file
107534 **   (2)  Copy all content from the database being vacuumed into
107535 **        the new transient database file
107536 **   (3)  Copy content from the transient database back into the
107537 **        original database.
107538 **
107539 ** The transient database requires temporary disk space approximately
107540 ** equal to the size of the original database.  The copy operation of
107541 ** step (3) requires additional temporary disk space approximately equal
107542 ** to the size of the original database for the rollback journal.
107543 ** Hence, temporary disk space that is approximately 2x the size of the
107544 ** orginal database is required.  Every page of the database is written
107545 ** approximately 3 times:  Once for step (2) and twice for step (3).
107546 ** Two writes per page are required in step (3) because the original
107547 ** database content must be written into the rollback journal prior to
107548 ** overwriting the database with the vacuumed content.
107549 **
107550 ** Only 1x temporary space and only 1x writes would be required if
107551 ** the copy of step (3) were replace by deleting the original database
107552 ** and renaming the transient database as the original.  But that will
107553 ** not work if other processes are attached to the original database.
107554 ** And a power loss in between deleting the original and renaming the
107555 ** transient would cause the database file to appear to be deleted
107556 ** following reboot.
107557 */
107558 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
107559   Vdbe *v = sqlite3GetVdbe(pParse);
107560   if( v ){
107561     sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
107562     sqlite3VdbeUsesBtree(v, 0);
107563   }
107564   return;
107565 }
107566 
107567 /*
107568 ** This routine implements the OP_Vacuum opcode of the VDBE.
107569 */
107570 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
107571   int rc = SQLITE_OK;     /* Return code from service routines */
107572   Btree *pMain;           /* The database being vacuumed */
107573   Btree *pTemp;           /* The temporary database we vacuum into */
107574   char *zSql = 0;         /* SQL statements */
107575   int saved_flags;        /* Saved value of the db->flags */
107576   int saved_nChange;      /* Saved value of db->nChange */
107577   int saved_nTotalChange; /* Saved value of db->nTotalChange */
107578   void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
107579   Db *pDb = 0;            /* Database to detach at end of vacuum */
107580   int isMemDb;            /* True if vacuuming a :memory: database */
107581   int nRes;               /* Bytes of reserved space at the end of each page */
107582   int nDb;                /* Number of attached databases */
107583 
107584   if( !db->autoCommit ){
107585     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
107586     return SQLITE_ERROR;
107587   }
107588   if( db->nVdbeActive>1 ){
107589     sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
107590     return SQLITE_ERROR;
107591   }
107592 
107593   /* Save the current value of the database flags so that it can be
107594   ** restored before returning. Then set the writable-schema flag, and
107595   ** disable CHECK and foreign key constraints.  */
107596   saved_flags = db->flags;
107597   saved_nChange = db->nChange;
107598   saved_nTotalChange = db->nTotalChange;
107599   saved_xTrace = db->xTrace;
107600   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
107601   db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
107602   db->xTrace = 0;
107603 
107604   pMain = db->aDb[0].pBt;
107605   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
107606 
107607   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
107608   ** can be set to 'off' for this file, as it is not recovered if a crash
107609   ** occurs anyway. The integrity of the database is maintained by a
107610   ** (possibly synchronous) transaction opened on the main database before
107611   ** sqlite3BtreeCopyFile() is called.
107612   **
107613   ** An optimisation would be to use a non-journaled pager.
107614   ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
107615   ** that actually made the VACUUM run slower.  Very little journalling
107616   ** actually occurs when doing a vacuum since the vacuum_db is initially
107617   ** empty.  Only the journal header is written.  Apparently it takes more
107618   ** time to parse and run the PRAGMA to turn journalling off than it does
107619   ** to write the journal header file.
107620   */
107621   nDb = db->nDb;
107622   if( sqlite3TempInMemory(db) ){
107623     zSql = "ATTACH ':memory:' AS vacuum_db;";
107624   }else{
107625     zSql = "ATTACH '' AS vacuum_db;";
107626   }
107627   rc = execSql(db, pzErrMsg, zSql);
107628   if( db->nDb>nDb ){
107629     pDb = &db->aDb[db->nDb-1];
107630     assert( strcmp(pDb->zName,"vacuum_db")==0 );
107631   }
107632   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107633   pTemp = db->aDb[db->nDb-1].pBt;
107634 
107635   /* The call to execSql() to attach the temp database has left the file
107636   ** locked (as there was more than one active statement when the transaction
107637   ** to read the schema was concluded. Unlock it here so that this doesn't
107638   ** cause problems for the call to BtreeSetPageSize() below.  */
107639   sqlite3BtreeCommit(pTemp);
107640 
107641   nRes = sqlite3BtreeGetReserve(pMain);
107642 
107643   /* A VACUUM cannot change the pagesize of an encrypted database. */
107644 #ifdef SQLITE_HAS_CODEC
107645   if( db->nextPagesize ){
107646     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
107647     int nKey;
107648     char *zKey;
107649     sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
107650     if( nKey ) db->nextPagesize = 0;
107651   }
107652 #endif
107653 
107654   rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
107655   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107656 
107657   /* Begin a transaction and take an exclusive lock on the main database
107658   ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
107659   ** to ensure that we do not try to change the page-size on a WAL database.
107660   */
107661   rc = execSql(db, pzErrMsg, "BEGIN;");
107662   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107663   rc = sqlite3BtreeBeginTrans(pMain, 2);
107664   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107665 
107666   /* Do not attempt to change the page size for a WAL database */
107667   if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
107668                                                ==PAGER_JOURNALMODE_WAL ){
107669     db->nextPagesize = 0;
107670   }
107671 
107672   if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
107673    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
107674    || NEVER(db->mallocFailed)
107675   ){
107676     rc = SQLITE_NOMEM;
107677     goto end_of_vacuum;
107678   }
107679 
107680 #ifndef SQLITE_OMIT_AUTOVACUUM
107681   sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
107682                                            sqlite3BtreeGetAutoVacuum(pMain));
107683 #endif
107684 
107685   /* Query the schema of the main database. Create a mirror schema
107686   ** in the temporary database.
107687   */
107688   rc = execExecSql(db, pzErrMsg,
107689       "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
107690       "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
107691       "   AND coalesce(rootpage,1)>0"
107692   );
107693   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107694   rc = execExecSql(db, pzErrMsg,
107695       "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
107696       "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
107697   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107698   rc = execExecSql(db, pzErrMsg,
107699       "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
107700       "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
107701   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107702 
107703   /* Loop through the tables in the main database. For each, do
107704   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
107705   ** the contents to the temporary database.
107706   */
107707   rc = execExecSql(db, pzErrMsg,
107708       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
107709       "|| ' SELECT * FROM main.' || quote(name) || ';'"
107710       "FROM main.sqlite_master "
107711       "WHERE type = 'table' AND name!='sqlite_sequence' "
107712       "  AND coalesce(rootpage,1)>0"
107713   );
107714   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107715 
107716   /* Copy over the sequence table
107717   */
107718   rc = execExecSql(db, pzErrMsg,
107719       "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
107720       "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
107721   );
107722   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107723   rc = execExecSql(db, pzErrMsg,
107724       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
107725       "|| ' SELECT * FROM main.' || quote(name) || ';' "
107726       "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
107727   );
107728   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107729 
107730 
107731   /* Copy the triggers, views, and virtual tables from the main database
107732   ** over to the temporary database.  None of these objects has any
107733   ** associated storage, so all we have to do is copy their entries
107734   ** from the SQLITE_MASTER table.
107735   */
107736   rc = execSql(db, pzErrMsg,
107737       "INSERT INTO vacuum_db.sqlite_master "
107738       "  SELECT type, name, tbl_name, rootpage, sql"
107739       "    FROM main.sqlite_master"
107740       "   WHERE type='view' OR type='trigger'"
107741       "      OR (type='table' AND rootpage=0)"
107742   );
107743   if( rc ) goto end_of_vacuum;
107744 
107745   /* At this point, there is a write transaction open on both the
107746   ** vacuum database and the main database. Assuming no error occurs,
107747   ** both transactions are closed by this block - the main database
107748   ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
107749   ** call to sqlite3BtreeCommit().
107750   */
107751   {
107752     u32 meta;
107753     int i;
107754 
107755     /* This array determines which meta meta values are preserved in the
107756     ** vacuum.  Even entries are the meta value number and odd entries
107757     ** are an increment to apply to the meta value after the vacuum.
107758     ** The increment is used to increase the schema cookie so that other
107759     ** connections to the same database will know to reread the schema.
107760     */
107761     static const unsigned char aCopy[] = {
107762        BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
107763        BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
107764        BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
107765        BTREE_USER_VERSION,       0,  /* Preserve the user version */
107766        BTREE_APPLICATION_ID,     0,  /* Preserve the application id */
107767     };
107768 
107769     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
107770     assert( 1==sqlite3BtreeIsInTrans(pMain) );
107771 
107772     /* Copy Btree meta values */
107773     for(i=0; i<ArraySize(aCopy); i+=2){
107774       /* GetMeta() and UpdateMeta() cannot fail in this context because
107775       ** we already have page 1 loaded into cache and marked dirty. */
107776       sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
107777       rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
107778       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
107779     }
107780 
107781     rc = sqlite3BtreeCopyFile(pMain, pTemp);
107782     if( rc!=SQLITE_OK ) goto end_of_vacuum;
107783     rc = sqlite3BtreeCommit(pTemp);
107784     if( rc!=SQLITE_OK ) goto end_of_vacuum;
107785 #ifndef SQLITE_OMIT_AUTOVACUUM
107786     sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
107787 #endif
107788   }
107789 
107790   assert( rc==SQLITE_OK );
107791   rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
107792 
107793 end_of_vacuum:
107794   /* Restore the original value of db->flags */
107795   db->flags = saved_flags;
107796   db->nChange = saved_nChange;
107797   db->nTotalChange = saved_nTotalChange;
107798   db->xTrace = saved_xTrace;
107799   sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
107800 
107801   /* Currently there is an SQL level transaction open on the vacuum
107802   ** database. No locks are held on any other files (since the main file
107803   ** was committed at the btree level). So it safe to end the transaction
107804   ** by manually setting the autoCommit flag to true and detaching the
107805   ** vacuum database. The vacuum_db journal file is deleted when the pager
107806   ** is closed by the DETACH.
107807   */
107808   db->autoCommit = 1;
107809 
107810   if( pDb ){
107811     sqlite3BtreeClose(pDb->pBt);
107812     pDb->pBt = 0;
107813     pDb->pSchema = 0;
107814   }
107815 
107816   /* This both clears the schemas and reduces the size of the db->aDb[]
107817   ** array. */
107818   sqlite3ResetAllSchemasOfConnection(db);
107819 
107820   return rc;
107821 }
107822 
107823 #endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
107824 
107825 /************** End of vacuum.c **********************************************/
107826 /************** Begin file vtab.c ********************************************/
107827 /*
107828 ** 2006 June 10
107829 **
107830 ** The author disclaims copyright to this source code.  In place of
107831 ** a legal notice, here is a blessing:
107832 **
107833 **    May you do good and not evil.
107834 **    May you find forgiveness for yourself and forgive others.
107835 **    May you share freely, never taking more than you give.
107836 **
107837 *************************************************************************
107838 ** This file contains code used to help implement virtual tables.
107839 */
107840 #ifndef SQLITE_OMIT_VIRTUALTABLE
107841 
107842 /*
107843 ** Before a virtual table xCreate() or xConnect() method is invoked, the
107844 ** sqlite3.pVtabCtx member variable is set to point to an instance of
107845 ** this struct allocated on the stack. It is used by the implementation of
107846 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
107847 ** are invoked only from within xCreate and xConnect methods.
107848 */
107849 struct VtabCtx {
107850   VTable *pVTable;    /* The virtual table being constructed */
107851   Table *pTab;        /* The Table object to which the virtual table belongs */
107852 };
107853 
107854 /*
107855 ** The actual function that does the work of creating a new module.
107856 ** This function implements the sqlite3_create_module() and
107857 ** sqlite3_create_module_v2() interfaces.
107858 */
107859 static int createModule(
107860   sqlite3 *db,                    /* Database in which module is registered */
107861   const char *zName,              /* Name assigned to this module */
107862   const sqlite3_module *pModule,  /* The definition of the module */
107863   void *pAux,                     /* Context pointer for xCreate/xConnect */
107864   void (*xDestroy)(void *)        /* Module destructor function */
107865 ){
107866   int rc = SQLITE_OK;
107867   int nName;
107868 
107869   sqlite3_mutex_enter(db->mutex);
107870   nName = sqlite3Strlen30(zName);
107871   if( sqlite3HashFind(&db->aModule, zName, nName) ){
107872     rc = SQLITE_MISUSE_BKPT;
107873   }else{
107874     Module *pMod;
107875     pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
107876     if( pMod ){
107877       Module *pDel;
107878       char *zCopy = (char *)(&pMod[1]);
107879       memcpy(zCopy, zName, nName+1);
107880       pMod->zName = zCopy;
107881       pMod->pModule = pModule;
107882       pMod->pAux = pAux;
107883       pMod->xDestroy = xDestroy;
107884       pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,nName,(void*)pMod);
107885       assert( pDel==0 || pDel==pMod );
107886       if( pDel ){
107887         db->mallocFailed = 1;
107888         sqlite3DbFree(db, pDel);
107889       }
107890     }
107891   }
107892   rc = sqlite3ApiExit(db, rc);
107893   if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
107894 
107895   sqlite3_mutex_leave(db->mutex);
107896   return rc;
107897 }
107898 
107899 
107900 /*
107901 ** External API function used to create a new virtual-table module.
107902 */
107903 SQLITE_API int sqlite3_create_module(
107904   sqlite3 *db,                    /* Database in which module is registered */
107905   const char *zName,              /* Name assigned to this module */
107906   const sqlite3_module *pModule,  /* The definition of the module */
107907   void *pAux                      /* Context pointer for xCreate/xConnect */
107908 ){
107909   return createModule(db, zName, pModule, pAux, 0);
107910 }
107911 
107912 /*
107913 ** External API function used to create a new virtual-table module.
107914 */
107915 SQLITE_API int sqlite3_create_module_v2(
107916   sqlite3 *db,                    /* Database in which module is registered */
107917   const char *zName,              /* Name assigned to this module */
107918   const sqlite3_module *pModule,  /* The definition of the module */
107919   void *pAux,                     /* Context pointer for xCreate/xConnect */
107920   void (*xDestroy)(void *)        /* Module destructor function */
107921 ){
107922   return createModule(db, zName, pModule, pAux, xDestroy);
107923 }
107924 
107925 /*
107926 ** Lock the virtual table so that it cannot be disconnected.
107927 ** Locks nest.  Every lock should have a corresponding unlock.
107928 ** If an unlock is omitted, resources leaks will occur.
107929 **
107930 ** If a disconnect is attempted while a virtual table is locked,
107931 ** the disconnect is deferred until all locks have been removed.
107932 */
107933 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
107934   pVTab->nRef++;
107935 }
107936 
107937 
107938 /*
107939 ** pTab is a pointer to a Table structure representing a virtual-table.
107940 ** Return a pointer to the VTable object used by connection db to access
107941 ** this virtual-table, if one has been created, or NULL otherwise.
107942 */
107943 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
107944   VTable *pVtab;
107945   assert( IsVirtual(pTab) );
107946   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
107947   return pVtab;
107948 }
107949 
107950 /*
107951 ** Decrement the ref-count on a virtual table object. When the ref-count
107952 ** reaches zero, call the xDisconnect() method to delete the object.
107953 */
107954 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
107955   sqlite3 *db = pVTab->db;
107956 
107957   assert( db );
107958   assert( pVTab->nRef>0 );
107959   assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
107960 
107961   pVTab->nRef--;
107962   if( pVTab->nRef==0 ){
107963     sqlite3_vtab *p = pVTab->pVtab;
107964     if( p ){
107965       p->pModule->xDisconnect(p);
107966     }
107967     sqlite3DbFree(db, pVTab);
107968   }
107969 }
107970 
107971 /*
107972 ** Table p is a virtual table. This function moves all elements in the
107973 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
107974 ** database connections to be disconnected at the next opportunity.
107975 ** Except, if argument db is not NULL, then the entry associated with
107976 ** connection db is left in the p->pVTable list.
107977 */
107978 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
107979   VTable *pRet = 0;
107980   VTable *pVTable = p->pVTable;
107981   p->pVTable = 0;
107982 
107983   /* Assert that the mutex (if any) associated with the BtShared database
107984   ** that contains table p is held by the caller. See header comments
107985   ** above function sqlite3VtabUnlockList() for an explanation of why
107986   ** this makes it safe to access the sqlite3.pDisconnect list of any
107987   ** database connection that may have an entry in the p->pVTable list.
107988   */
107989   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
107990 
107991   while( pVTable ){
107992     sqlite3 *db2 = pVTable->db;
107993     VTable *pNext = pVTable->pNext;
107994     assert( db2 );
107995     if( db2==db ){
107996       pRet = pVTable;
107997       p->pVTable = pRet;
107998       pRet->pNext = 0;
107999     }else{
108000       pVTable->pNext = db2->pDisconnect;
108001       db2->pDisconnect = pVTable;
108002     }
108003     pVTable = pNext;
108004   }
108005 
108006   assert( !db || pRet );
108007   return pRet;
108008 }
108009 
108010 /*
108011 ** Table *p is a virtual table. This function removes the VTable object
108012 ** for table *p associated with database connection db from the linked
108013 ** list in p->pVTab. It also decrements the VTable ref count. This is
108014 ** used when closing database connection db to free all of its VTable
108015 ** objects without disturbing the rest of the Schema object (which may
108016 ** be being used by other shared-cache connections).
108017 */
108018 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
108019   VTable **ppVTab;
108020 
108021   assert( IsVirtual(p) );
108022   assert( sqlite3BtreeHoldsAllMutexes(db) );
108023   assert( sqlite3_mutex_held(db->mutex) );
108024 
108025   for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
108026     if( (*ppVTab)->db==db  ){
108027       VTable *pVTab = *ppVTab;
108028       *ppVTab = pVTab->pNext;
108029       sqlite3VtabUnlock(pVTab);
108030       break;
108031     }
108032   }
108033 }
108034 
108035 
108036 /*
108037 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
108038 **
108039 ** This function may only be called when the mutexes associated with all
108040 ** shared b-tree databases opened using connection db are held by the
108041 ** caller. This is done to protect the sqlite3.pDisconnect list. The
108042 ** sqlite3.pDisconnect list is accessed only as follows:
108043 **
108044 **   1) By this function. In this case, all BtShared mutexes and the mutex
108045 **      associated with the database handle itself must be held.
108046 **
108047 **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
108048 **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
108049 **      associated with the database the virtual table is stored in is held
108050 **      or, if the virtual table is stored in a non-sharable database, then
108051 **      the database handle mutex is held.
108052 **
108053 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
108054 ** by multiple threads. It is thread-safe.
108055 */
108056 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
108057   VTable *p = db->pDisconnect;
108058   db->pDisconnect = 0;
108059 
108060   assert( sqlite3BtreeHoldsAllMutexes(db) );
108061   assert( sqlite3_mutex_held(db->mutex) );
108062 
108063   if( p ){
108064     sqlite3ExpirePreparedStatements(db);
108065     do {
108066       VTable *pNext = p->pNext;
108067       sqlite3VtabUnlock(p);
108068       p = pNext;
108069     }while( p );
108070   }
108071 }
108072 
108073 /*
108074 ** Clear any and all virtual-table information from the Table record.
108075 ** This routine is called, for example, just before deleting the Table
108076 ** record.
108077 **
108078 ** Since it is a virtual-table, the Table structure contains a pointer
108079 ** to the head of a linked list of VTable structures. Each VTable
108080 ** structure is associated with a single sqlite3* user of the schema.
108081 ** The reference count of the VTable structure associated with database
108082 ** connection db is decremented immediately (which may lead to the
108083 ** structure being xDisconnected and free). Any other VTable structures
108084 ** in the list are moved to the sqlite3.pDisconnect list of the associated
108085 ** database connection.
108086 */
108087 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
108088   if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
108089   if( p->azModuleArg ){
108090     int i;
108091     for(i=0; i<p->nModuleArg; i++){
108092       if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
108093     }
108094     sqlite3DbFree(db, p->azModuleArg);
108095   }
108096 }
108097 
108098 /*
108099 ** Add a new module argument to pTable->azModuleArg[].
108100 ** The string is not copied - the pointer is stored.  The
108101 ** string will be freed automatically when the table is
108102 ** deleted.
108103 */
108104 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
108105   int i = pTable->nModuleArg++;
108106   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
108107   char **azModuleArg;
108108   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
108109   if( azModuleArg==0 ){
108110     int j;
108111     for(j=0; j<i; j++){
108112       sqlite3DbFree(db, pTable->azModuleArg[j]);
108113     }
108114     sqlite3DbFree(db, zArg);
108115     sqlite3DbFree(db, pTable->azModuleArg);
108116     pTable->nModuleArg = 0;
108117   }else{
108118     azModuleArg[i] = zArg;
108119     azModuleArg[i+1] = 0;
108120   }
108121   pTable->azModuleArg = azModuleArg;
108122 }
108123 
108124 /*
108125 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
108126 ** statement.  The module name has been parsed, but the optional list
108127 ** of parameters that follow the module name are still pending.
108128 */
108129 SQLITE_PRIVATE void sqlite3VtabBeginParse(
108130   Parse *pParse,        /* Parsing context */
108131   Token *pName1,        /* Name of new table, or database name */
108132   Token *pName2,        /* Name of new table or NULL */
108133   Token *pModuleName,   /* Name of the module for the virtual table */
108134   int ifNotExists       /* No error if the table already exists */
108135 ){
108136   int iDb;              /* The database the table is being created in */
108137   Table *pTable;        /* The new virtual table */
108138   sqlite3 *db;          /* Database connection */
108139 
108140   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
108141   pTable = pParse->pNewTable;
108142   if( pTable==0 ) return;
108143   assert( 0==pTable->pIndex );
108144 
108145   db = pParse->db;
108146   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
108147   assert( iDb>=0 );
108148 
108149   pTable->tabFlags |= TF_Virtual;
108150   pTable->nModuleArg = 0;
108151   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
108152   addModuleArgument(db, pTable, 0);
108153   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
108154   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
108155 
108156 #ifndef SQLITE_OMIT_AUTHORIZATION
108157   /* Creating a virtual table invokes the authorization callback twice.
108158   ** The first invocation, to obtain permission to INSERT a row into the
108159   ** sqlite_master table, has already been made by sqlite3StartTable().
108160   ** The second call, to obtain permission to create the table, is made now.
108161   */
108162   if( pTable->azModuleArg ){
108163     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
108164             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
108165   }
108166 #endif
108167 }
108168 
108169 /*
108170 ** This routine takes the module argument that has been accumulating
108171 ** in pParse->zArg[] and appends it to the list of arguments on the
108172 ** virtual table currently under construction in pParse->pTable.
108173 */
108174 static void addArgumentToVtab(Parse *pParse){
108175   if( pParse->sArg.z && pParse->pNewTable ){
108176     const char *z = (const char*)pParse->sArg.z;
108177     int n = pParse->sArg.n;
108178     sqlite3 *db = pParse->db;
108179     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
108180   }
108181 }
108182 
108183 /*
108184 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
108185 ** has been completely parsed.
108186 */
108187 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
108188   Table *pTab = pParse->pNewTable;  /* The table being constructed */
108189   sqlite3 *db = pParse->db;         /* The database connection */
108190 
108191   if( pTab==0 ) return;
108192   addArgumentToVtab(pParse);
108193   pParse->sArg.z = 0;
108194   if( pTab->nModuleArg<1 ) return;
108195 
108196   /* If the CREATE VIRTUAL TABLE statement is being entered for the
108197   ** first time (in other words if the virtual table is actually being
108198   ** created now instead of just being read out of sqlite_master) then
108199   ** do additional initialization work and store the statement text
108200   ** in the sqlite_master table.
108201   */
108202   if( !db->init.busy ){
108203     char *zStmt;
108204     char *zWhere;
108205     int iDb;
108206     Vdbe *v;
108207 
108208     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
108209     if( pEnd ){
108210       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
108211     }
108212     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
108213 
108214     /* A slot for the record has already been allocated in the
108215     ** SQLITE_MASTER table.  We just need to update that slot with all
108216     ** the information we've collected.
108217     **
108218     ** The VM register number pParse->regRowid holds the rowid of an
108219     ** entry in the sqlite_master table tht was created for this vtab
108220     ** by sqlite3StartTable().
108221     */
108222     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108223     sqlite3NestedParse(pParse,
108224       "UPDATE %Q.%s "
108225          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
108226        "WHERE rowid=#%d",
108227       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
108228       pTab->zName,
108229       pTab->zName,
108230       zStmt,
108231       pParse->regRowid
108232     );
108233     sqlite3DbFree(db, zStmt);
108234     v = sqlite3GetVdbe(pParse);
108235     sqlite3ChangeCookie(pParse, iDb);
108236 
108237     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
108238     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
108239     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
108240     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
108241                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
108242   }
108243 
108244   /* If we are rereading the sqlite_master table create the in-memory
108245   ** record of the table. The xConnect() method is not called until
108246   ** the first time the virtual table is used in an SQL statement. This
108247   ** allows a schema that contains virtual tables to be loaded before
108248   ** the required virtual table implementations are registered.  */
108249   else {
108250     Table *pOld;
108251     Schema *pSchema = pTab->pSchema;
108252     const char *zName = pTab->zName;
108253     int nName = sqlite3Strlen30(zName);
108254     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
108255     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
108256     if( pOld ){
108257       db->mallocFailed = 1;
108258       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
108259       return;
108260     }
108261     pParse->pNewTable = 0;
108262   }
108263 }
108264 
108265 /*
108266 ** The parser calls this routine when it sees the first token
108267 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
108268 */
108269 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
108270   addArgumentToVtab(pParse);
108271   pParse->sArg.z = 0;
108272   pParse->sArg.n = 0;
108273 }
108274 
108275 /*
108276 ** The parser calls this routine for each token after the first token
108277 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
108278 */
108279 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
108280   Token *pArg = &pParse->sArg;
108281   if( pArg->z==0 ){
108282     pArg->z = p->z;
108283     pArg->n = p->n;
108284   }else{
108285     assert(pArg->z < p->z);
108286     pArg->n = (int)(&p->z[p->n] - pArg->z);
108287   }
108288 }
108289 
108290 /*
108291 ** Invoke a virtual table constructor (either xCreate or xConnect). The
108292 ** pointer to the function to invoke is passed as the fourth parameter
108293 ** to this procedure.
108294 */
108295 static int vtabCallConstructor(
108296   sqlite3 *db,
108297   Table *pTab,
108298   Module *pMod,
108299   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
108300   char **pzErr
108301 ){
108302   VtabCtx sCtx, *pPriorCtx;
108303   VTable *pVTable;
108304   int rc;
108305   const char *const*azArg = (const char *const*)pTab->azModuleArg;
108306   int nArg = pTab->nModuleArg;
108307   char *zErr = 0;
108308   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
108309   int iDb;
108310 
108311   if( !zModuleName ){
108312     return SQLITE_NOMEM;
108313   }
108314 
108315   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
108316   if( !pVTable ){
108317     sqlite3DbFree(db, zModuleName);
108318     return SQLITE_NOMEM;
108319   }
108320   pVTable->db = db;
108321   pVTable->pMod = pMod;
108322 
108323   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
108324   pTab->azModuleArg[1] = db->aDb[iDb].zName;
108325 
108326   /* Invoke the virtual table constructor */
108327   assert( &db->pVtabCtx );
108328   assert( xConstruct );
108329   sCtx.pTab = pTab;
108330   sCtx.pVTable = pVTable;
108331   pPriorCtx = db->pVtabCtx;
108332   db->pVtabCtx = &sCtx;
108333   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
108334   db->pVtabCtx = pPriorCtx;
108335   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
108336 
108337   if( SQLITE_OK!=rc ){
108338     if( zErr==0 ){
108339       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
108340     }else {
108341       *pzErr = sqlite3MPrintf(db, "%s", zErr);
108342       sqlite3_free(zErr);
108343     }
108344     sqlite3DbFree(db, pVTable);
108345   }else if( ALWAYS(pVTable->pVtab) ){
108346     /* Justification of ALWAYS():  A correct vtab constructor must allocate
108347     ** the sqlite3_vtab object if successful.  */
108348     pVTable->pVtab->pModule = pMod->pModule;
108349     pVTable->nRef = 1;
108350     if( sCtx.pTab ){
108351       const char *zFormat = "vtable constructor did not declare schema: %s";
108352       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
108353       sqlite3VtabUnlock(pVTable);
108354       rc = SQLITE_ERROR;
108355     }else{
108356       int iCol;
108357       /* If everything went according to plan, link the new VTable structure
108358       ** into the linked list headed by pTab->pVTable. Then loop through the
108359       ** columns of the table to see if any of them contain the token "hidden".
108360       ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
108361       ** the type string.  */
108362       pVTable->pNext = pTab->pVTable;
108363       pTab->pVTable = pVTable;
108364 
108365       for(iCol=0; iCol<pTab->nCol; iCol++){
108366         char *zType = pTab->aCol[iCol].zType;
108367         int nType;
108368         int i = 0;
108369         if( !zType ) continue;
108370         nType = sqlite3Strlen30(zType);
108371         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
108372           for(i=0; i<nType; i++){
108373             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
108374              && (zType[i+7]=='\0' || zType[i+7]==' ')
108375             ){
108376               i++;
108377               break;
108378             }
108379           }
108380         }
108381         if( i<nType ){
108382           int j;
108383           int nDel = 6 + (zType[i+6] ? 1 : 0);
108384           for(j=i; (j+nDel)<=nType; j++){
108385             zType[j] = zType[j+nDel];
108386           }
108387           if( zType[i]=='\0' && i>0 ){
108388             assert(zType[i-1]==' ');
108389             zType[i-1] = '\0';
108390           }
108391           pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
108392         }
108393       }
108394     }
108395   }
108396 
108397   sqlite3DbFree(db, zModuleName);
108398   return rc;
108399 }
108400 
108401 /*
108402 ** This function is invoked by the parser to call the xConnect() method
108403 ** of the virtual table pTab. If an error occurs, an error code is returned
108404 ** and an error left in pParse.
108405 **
108406 ** This call is a no-op if table pTab is not a virtual table.
108407 */
108408 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
108409   sqlite3 *db = pParse->db;
108410   const char *zMod;
108411   Module *pMod;
108412   int rc;
108413 
108414   assert( pTab );
108415   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
108416     return SQLITE_OK;
108417   }
108418 
108419   /* Locate the required virtual table module */
108420   zMod = pTab->azModuleArg[0];
108421   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
108422 
108423   if( !pMod ){
108424     const char *zModule = pTab->azModuleArg[0];
108425     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
108426     rc = SQLITE_ERROR;
108427   }else{
108428     char *zErr = 0;
108429     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
108430     if( rc!=SQLITE_OK ){
108431       sqlite3ErrorMsg(pParse, "%s", zErr);
108432     }
108433     sqlite3DbFree(db, zErr);
108434   }
108435 
108436   return rc;
108437 }
108438 /*
108439 ** Grow the db->aVTrans[] array so that there is room for at least one
108440 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
108441 */
108442 static int growVTrans(sqlite3 *db){
108443   const int ARRAY_INCR = 5;
108444 
108445   /* Grow the sqlite3.aVTrans array if required */
108446   if( (db->nVTrans%ARRAY_INCR)==0 ){
108447     VTable **aVTrans;
108448     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
108449     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
108450     if( !aVTrans ){
108451       return SQLITE_NOMEM;
108452     }
108453     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
108454     db->aVTrans = aVTrans;
108455   }
108456 
108457   return SQLITE_OK;
108458 }
108459 
108460 /*
108461 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
108462 ** have already been reserved using growVTrans().
108463 */
108464 static void addToVTrans(sqlite3 *db, VTable *pVTab){
108465   /* Add pVtab to the end of sqlite3.aVTrans */
108466   db->aVTrans[db->nVTrans++] = pVTab;
108467   sqlite3VtabLock(pVTab);
108468 }
108469 
108470 /*
108471 ** This function is invoked by the vdbe to call the xCreate method
108472 ** of the virtual table named zTab in database iDb.
108473 **
108474 ** If an error occurs, *pzErr is set to point an an English language
108475 ** description of the error and an SQLITE_XXX error code is returned.
108476 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
108477 */
108478 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
108479   int rc = SQLITE_OK;
108480   Table *pTab;
108481   Module *pMod;
108482   const char *zMod;
108483 
108484   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
108485   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
108486 
108487   /* Locate the required virtual table module */
108488   zMod = pTab->azModuleArg[0];
108489   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
108490 
108491   /* If the module has been registered and includes a Create method,
108492   ** invoke it now. If the module has not been registered, return an
108493   ** error. Otherwise, do nothing.
108494   */
108495   if( !pMod ){
108496     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
108497     rc = SQLITE_ERROR;
108498   }else{
108499     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
108500   }
108501 
108502   /* Justification of ALWAYS():  The xConstructor method is required to
108503   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
108504   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
108505     rc = growVTrans(db);
108506     if( rc==SQLITE_OK ){
108507       addToVTrans(db, sqlite3GetVTable(db, pTab));
108508     }
108509   }
108510 
108511   return rc;
108512 }
108513 
108514 /*
108515 ** This function is used to set the schema of a virtual table.  It is only
108516 ** valid to call this function from within the xCreate() or xConnect() of a
108517 ** virtual table module.
108518 */
108519 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
108520   Parse *pParse;
108521 
108522   int rc = SQLITE_OK;
108523   Table *pTab;
108524   char *zErr = 0;
108525 
108526   sqlite3_mutex_enter(db->mutex);
108527   if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
108528     sqlite3Error(db, SQLITE_MISUSE, 0);
108529     sqlite3_mutex_leave(db->mutex);
108530     return SQLITE_MISUSE_BKPT;
108531   }
108532   assert( (pTab->tabFlags & TF_Virtual)!=0 );
108533 
108534   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
108535   if( pParse==0 ){
108536     rc = SQLITE_NOMEM;
108537   }else{
108538     pParse->declareVtab = 1;
108539     pParse->db = db;
108540     pParse->nQueryLoop = 1;
108541 
108542     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
108543      && pParse->pNewTable
108544      && !db->mallocFailed
108545      && !pParse->pNewTable->pSelect
108546      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
108547     ){
108548       if( !pTab->aCol ){
108549         pTab->aCol = pParse->pNewTable->aCol;
108550         pTab->nCol = pParse->pNewTable->nCol;
108551         pParse->pNewTable->nCol = 0;
108552         pParse->pNewTable->aCol = 0;
108553       }
108554       db->pVtabCtx->pTab = 0;
108555     }else{
108556       sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
108557       sqlite3DbFree(db, zErr);
108558       rc = SQLITE_ERROR;
108559     }
108560     pParse->declareVtab = 0;
108561 
108562     if( pParse->pVdbe ){
108563       sqlite3VdbeFinalize(pParse->pVdbe);
108564     }
108565     sqlite3DeleteTable(db, pParse->pNewTable);
108566     sqlite3ParserReset(pParse);
108567     sqlite3StackFree(db, pParse);
108568   }
108569 
108570   assert( (rc&0xff)==rc );
108571   rc = sqlite3ApiExit(db, rc);
108572   sqlite3_mutex_leave(db->mutex);
108573   return rc;
108574 }
108575 
108576 /*
108577 ** This function is invoked by the vdbe to call the xDestroy method
108578 ** of the virtual table named zTab in database iDb. This occurs
108579 ** when a DROP TABLE is mentioned.
108580 **
108581 ** This call is a no-op if zTab is not a virtual table.
108582 */
108583 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
108584   int rc = SQLITE_OK;
108585   Table *pTab;
108586 
108587   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
108588   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
108589     VTable *p = vtabDisconnectAll(db, pTab);
108590 
108591     assert( rc==SQLITE_OK );
108592     rc = p->pMod->pModule->xDestroy(p->pVtab);
108593 
108594     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
108595     if( rc==SQLITE_OK ){
108596       assert( pTab->pVTable==p && p->pNext==0 );
108597       p->pVtab = 0;
108598       pTab->pVTable = 0;
108599       sqlite3VtabUnlock(p);
108600     }
108601   }
108602 
108603   return rc;
108604 }
108605 
108606 /*
108607 ** This function invokes either the xRollback or xCommit method
108608 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
108609 ** called is identified by the second argument, "offset", which is
108610 ** the offset of the method to call in the sqlite3_module structure.
108611 **
108612 ** The array is cleared after invoking the callbacks.
108613 */
108614 static void callFinaliser(sqlite3 *db, int offset){
108615   int i;
108616   if( db->aVTrans ){
108617     for(i=0; i<db->nVTrans; i++){
108618       VTable *pVTab = db->aVTrans[i];
108619       sqlite3_vtab *p = pVTab->pVtab;
108620       if( p ){
108621         int (*x)(sqlite3_vtab *);
108622         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
108623         if( x ) x(p);
108624       }
108625       pVTab->iSavepoint = 0;
108626       sqlite3VtabUnlock(pVTab);
108627     }
108628     sqlite3DbFree(db, db->aVTrans);
108629     db->nVTrans = 0;
108630     db->aVTrans = 0;
108631   }
108632 }
108633 
108634 /*
108635 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
108636 ** array. Return the error code for the first error that occurs, or
108637 ** SQLITE_OK if all xSync operations are successful.
108638 **
108639 ** If an error message is available, leave it in p->zErrMsg.
108640 */
108641 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
108642   int i;
108643   int rc = SQLITE_OK;
108644   VTable **aVTrans = db->aVTrans;
108645 
108646   db->aVTrans = 0;
108647   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
108648     int (*x)(sqlite3_vtab *);
108649     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
108650     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
108651       rc = x(pVtab);
108652       sqlite3VtabImportErrmsg(p, pVtab);
108653     }
108654   }
108655   db->aVTrans = aVTrans;
108656   return rc;
108657 }
108658 
108659 /*
108660 ** Invoke the xRollback method of all virtual tables in the
108661 ** sqlite3.aVTrans array. Then clear the array itself.
108662 */
108663 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
108664   callFinaliser(db, offsetof(sqlite3_module,xRollback));
108665   return SQLITE_OK;
108666 }
108667 
108668 /*
108669 ** Invoke the xCommit method of all virtual tables in the
108670 ** sqlite3.aVTrans array. Then clear the array itself.
108671 */
108672 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
108673   callFinaliser(db, offsetof(sqlite3_module,xCommit));
108674   return SQLITE_OK;
108675 }
108676 
108677 /*
108678 ** If the virtual table pVtab supports the transaction interface
108679 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
108680 ** not currently open, invoke the xBegin method now.
108681 **
108682 ** If the xBegin call is successful, place the sqlite3_vtab pointer
108683 ** in the sqlite3.aVTrans array.
108684 */
108685 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
108686   int rc = SQLITE_OK;
108687   const sqlite3_module *pModule;
108688 
108689   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
108690   ** than zero, then this function is being called from within a
108691   ** virtual module xSync() callback. It is illegal to write to
108692   ** virtual module tables in this case, so return SQLITE_LOCKED.
108693   */
108694   if( sqlite3VtabInSync(db) ){
108695     return SQLITE_LOCKED;
108696   }
108697   if( !pVTab ){
108698     return SQLITE_OK;
108699   }
108700   pModule = pVTab->pVtab->pModule;
108701 
108702   if( pModule->xBegin ){
108703     int i;
108704 
108705     /* If pVtab is already in the aVTrans array, return early */
108706     for(i=0; i<db->nVTrans; i++){
108707       if( db->aVTrans[i]==pVTab ){
108708         return SQLITE_OK;
108709       }
108710     }
108711 
108712     /* Invoke the xBegin method. If successful, add the vtab to the
108713     ** sqlite3.aVTrans[] array. */
108714     rc = growVTrans(db);
108715     if( rc==SQLITE_OK ){
108716       rc = pModule->xBegin(pVTab->pVtab);
108717       if( rc==SQLITE_OK ){
108718         addToVTrans(db, pVTab);
108719       }
108720     }
108721   }
108722   return rc;
108723 }
108724 
108725 /*
108726 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
108727 ** virtual tables that currently have an open transaction. Pass iSavepoint
108728 ** as the second argument to the virtual table method invoked.
108729 **
108730 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
108731 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
108732 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
108733 ** an open transaction is invoked.
108734 **
108735 ** If any virtual table method returns an error code other than SQLITE_OK,
108736 ** processing is abandoned and the error returned to the caller of this
108737 ** function immediately. If all calls to virtual table methods are successful,
108738 ** SQLITE_OK is returned.
108739 */
108740 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
108741   int rc = SQLITE_OK;
108742 
108743   assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
108744   assert( iSavepoint>=0 );
108745   if( db->aVTrans ){
108746     int i;
108747     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
108748       VTable *pVTab = db->aVTrans[i];
108749       const sqlite3_module *pMod = pVTab->pMod->pModule;
108750       if( pVTab->pVtab && pMod->iVersion>=2 ){
108751         int (*xMethod)(sqlite3_vtab *, int);
108752         switch( op ){
108753           case SAVEPOINT_BEGIN:
108754             xMethod = pMod->xSavepoint;
108755             pVTab->iSavepoint = iSavepoint+1;
108756             break;
108757           case SAVEPOINT_ROLLBACK:
108758             xMethod = pMod->xRollbackTo;
108759             break;
108760           default:
108761             xMethod = pMod->xRelease;
108762             break;
108763         }
108764         if( xMethod && pVTab->iSavepoint>iSavepoint ){
108765           rc = xMethod(pVTab->pVtab, iSavepoint);
108766         }
108767       }
108768     }
108769   }
108770   return rc;
108771 }
108772 
108773 /*
108774 ** The first parameter (pDef) is a function implementation.  The
108775 ** second parameter (pExpr) is the first argument to this function.
108776 ** If pExpr is a column in a virtual table, then let the virtual
108777 ** table implementation have an opportunity to overload the function.
108778 **
108779 ** This routine is used to allow virtual table implementations to
108780 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
108781 **
108782 ** Return either the pDef argument (indicating no change) or a
108783 ** new FuncDef structure that is marked as ephemeral using the
108784 ** SQLITE_FUNC_EPHEM flag.
108785 */
108786 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
108787   sqlite3 *db,    /* Database connection for reporting malloc problems */
108788   FuncDef *pDef,  /* Function to possibly overload */
108789   int nArg,       /* Number of arguments to the function */
108790   Expr *pExpr     /* First argument to the function */
108791 ){
108792   Table *pTab;
108793   sqlite3_vtab *pVtab;
108794   sqlite3_module *pMod;
108795   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
108796   void *pArg = 0;
108797   FuncDef *pNew;
108798   int rc = 0;
108799   char *zLowerName;
108800   unsigned char *z;
108801 
108802 
108803   /* Check to see the left operand is a column in a virtual table */
108804   if( NEVER(pExpr==0) ) return pDef;
108805   if( pExpr->op!=TK_COLUMN ) return pDef;
108806   pTab = pExpr->pTab;
108807   if( NEVER(pTab==0) ) return pDef;
108808   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
108809   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
108810   assert( pVtab!=0 );
108811   assert( pVtab->pModule!=0 );
108812   pMod = (sqlite3_module *)pVtab->pModule;
108813   if( pMod->xFindFunction==0 ) return pDef;
108814 
108815   /* Call the xFindFunction method on the virtual table implementation
108816   ** to see if the implementation wants to overload this function
108817   */
108818   zLowerName = sqlite3DbStrDup(db, pDef->zName);
108819   if( zLowerName ){
108820     for(z=(unsigned char*)zLowerName; *z; z++){
108821       *z = sqlite3UpperToLower[*z];
108822     }
108823     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
108824     sqlite3DbFree(db, zLowerName);
108825   }
108826   if( rc==0 ){
108827     return pDef;
108828   }
108829 
108830   /* Create a new ephemeral function definition for the overloaded
108831   ** function */
108832   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
108833                              + sqlite3Strlen30(pDef->zName) + 1);
108834   if( pNew==0 ){
108835     return pDef;
108836   }
108837   *pNew = *pDef;
108838   pNew->zName = (char *)&pNew[1];
108839   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
108840   pNew->xFunc = xFunc;
108841   pNew->pUserData = pArg;
108842   pNew->funcFlags |= SQLITE_FUNC_EPHEM;
108843   return pNew;
108844 }
108845 
108846 /*
108847 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
108848 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
108849 ** array if it is missing.  If pTab is already in the array, this routine
108850 ** is a no-op.
108851 */
108852 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
108853   Parse *pToplevel = sqlite3ParseToplevel(pParse);
108854   int i, n;
108855   Table **apVtabLock;
108856 
108857   assert( IsVirtual(pTab) );
108858   for(i=0; i<pToplevel->nVtabLock; i++){
108859     if( pTab==pToplevel->apVtabLock[i] ) return;
108860   }
108861   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
108862   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
108863   if( apVtabLock ){
108864     pToplevel->apVtabLock = apVtabLock;
108865     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
108866   }else{
108867     pToplevel->db->mallocFailed = 1;
108868   }
108869 }
108870 
108871 /*
108872 ** Return the ON CONFLICT resolution mode in effect for the virtual
108873 ** table update operation currently in progress.
108874 **
108875 ** The results of this routine are undefined unless it is called from
108876 ** within an xUpdate method.
108877 */
108878 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
108879   static const unsigned char aMap[] = {
108880     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
108881   };
108882   assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
108883   assert( OE_Ignore==4 && OE_Replace==5 );
108884   assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
108885   return (int)aMap[db->vtabOnConflict-1];
108886 }
108887 
108888 /*
108889 ** Call from within the xCreate() or xConnect() methods to provide
108890 ** the SQLite core with additional information about the behavior
108891 ** of the virtual table being implemented.
108892 */
108893 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
108894   va_list ap;
108895   int rc = SQLITE_OK;
108896 
108897   sqlite3_mutex_enter(db->mutex);
108898 
108899   va_start(ap, op);
108900   switch( op ){
108901     case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
108902       VtabCtx *p = db->pVtabCtx;
108903       if( !p ){
108904         rc = SQLITE_MISUSE_BKPT;
108905       }else{
108906         assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
108907         p->pVTable->bConstraint = (u8)va_arg(ap, int);
108908       }
108909       break;
108910     }
108911     default:
108912       rc = SQLITE_MISUSE_BKPT;
108913       break;
108914   }
108915   va_end(ap);
108916 
108917   if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
108918   sqlite3_mutex_leave(db->mutex);
108919   return rc;
108920 }
108921 
108922 #endif /* SQLITE_OMIT_VIRTUALTABLE */
108923 
108924 /************** End of vtab.c ************************************************/
108925 /************** Begin file where.c *******************************************/
108926 /*
108927 ** 2001 September 15
108928 **
108929 ** The author disclaims copyright to this source code.  In place of
108930 ** a legal notice, here is a blessing:
108931 **
108932 **    May you do good and not evil.
108933 **    May you find forgiveness for yourself and forgive others.
108934 **    May you share freely, never taking more than you give.
108935 **
108936 *************************************************************************
108937 ** This module contains C code that generates VDBE code used to process
108938 ** the WHERE clause of SQL statements.  This module is responsible for
108939 ** generating the code that loops through a table looking for applicable
108940 ** rows.  Indices are selected and used to speed the search when doing
108941 ** so is applicable.  Because this module is responsible for selecting
108942 ** indices, you might also think of this module as the "query optimizer".
108943 */
108944 /************** Include whereInt.h in the middle of where.c ******************/
108945 /************** Begin file whereInt.h ****************************************/
108946 /*
108947 ** 2013-11-12
108948 **
108949 ** The author disclaims copyright to this source code.  In place of
108950 ** a legal notice, here is a blessing:
108951 **
108952 **    May you do good and not evil.
108953 **    May you find forgiveness for yourself and forgive others.
108954 **    May you share freely, never taking more than you give.
108955 **
108956 *************************************************************************
108957 **
108958 ** This file contains structure and macro definitions for the query
108959 ** planner logic in "where.c".  These definitions are broken out into
108960 ** a separate source file for easier editing.
108961 */
108962 
108963 /*
108964 ** Trace output macros
108965 */
108966 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
108967 /***/ int sqlite3WhereTrace = 0;
108968 #endif
108969 #if defined(SQLITE_DEBUG) \
108970     && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
108971 # define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
108972 # define WHERETRACE_ENABLED 1
108973 #else
108974 # define WHERETRACE(K,X)
108975 #endif
108976 
108977 /* Forward references
108978 */
108979 typedef struct WhereClause WhereClause;
108980 typedef struct WhereMaskSet WhereMaskSet;
108981 typedef struct WhereOrInfo WhereOrInfo;
108982 typedef struct WhereAndInfo WhereAndInfo;
108983 typedef struct WhereLevel WhereLevel;
108984 typedef struct WhereLoop WhereLoop;
108985 typedef struct WherePath WherePath;
108986 typedef struct WhereTerm WhereTerm;
108987 typedef struct WhereLoopBuilder WhereLoopBuilder;
108988 typedef struct WhereScan WhereScan;
108989 typedef struct WhereOrCost WhereOrCost;
108990 typedef struct WhereOrSet WhereOrSet;
108991 
108992 /*
108993 ** This object contains information needed to implement a single nested
108994 ** loop in WHERE clause.
108995 **
108996 ** Contrast this object with WhereLoop.  This object describes the
108997 ** implementation of the loop.  WhereLoop describes the algorithm.
108998 ** This object contains a pointer to the WhereLoop algorithm as one of
108999 ** its elements.
109000 **
109001 ** The WhereInfo object contains a single instance of this object for
109002 ** each term in the FROM clause (which is to say, for each of the
109003 ** nested loops as implemented).  The order of WhereLevel objects determines
109004 ** the loop nested order, with WhereInfo.a[0] being the outer loop and
109005 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
109006 */
109007 struct WhereLevel {
109008   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
109009   int iTabCur;          /* The VDBE cursor used to access the table */
109010   int iIdxCur;          /* The VDBE cursor used to access pIdx */
109011   int addrBrk;          /* Jump here to break out of the loop */
109012   int addrNxt;          /* Jump here to start the next IN combination */
109013   int addrSkip;         /* Jump here for next iteration of skip-scan */
109014   int addrCont;         /* Jump here to continue with the next loop cycle */
109015   int addrFirst;        /* First instruction of interior of the loop */
109016   int addrBody;         /* Beginning of the body of this loop */
109017   u8 iFrom;             /* Which entry in the FROM clause */
109018   u8 op, p3, p5;        /* Opcode, P3 & P5 of the opcode that ends the loop */
109019   int p1, p2;           /* Operands of the opcode used to ends the loop */
109020   union {               /* Information that depends on pWLoop->wsFlags */
109021     struct {
109022       int nIn;              /* Number of entries in aInLoop[] */
109023       struct InLoop {
109024         int iCur;              /* The VDBE cursor used by this IN operator */
109025         int addrInTop;         /* Top of the IN loop */
109026         u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
109027       } *aInLoop;           /* Information about each nested IN operator */
109028     } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
109029     Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
109030   } u;
109031   struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
109032   Bitmask notReady;          /* FROM entries not usable at this level */
109033 };
109034 
109035 /*
109036 ** Each instance of this object represents an algorithm for evaluating one
109037 ** term of a join.  Every term of the FROM clause will have at least
109038 ** one corresponding WhereLoop object (unless INDEXED BY constraints
109039 ** prevent a query solution - which is an error) and many terms of the
109040 ** FROM clause will have multiple WhereLoop objects, each describing a
109041 ** potential way of implementing that FROM-clause term, together with
109042 ** dependencies and cost estimates for using the chosen algorithm.
109043 **
109044 ** Query planning consists of building up a collection of these WhereLoop
109045 ** objects, then computing a particular sequence of WhereLoop objects, with
109046 ** one WhereLoop object per FROM clause term, that satisfy all dependencies
109047 ** and that minimize the overall cost.
109048 */
109049 struct WhereLoop {
109050   Bitmask prereq;       /* Bitmask of other loops that must run first */
109051   Bitmask maskSelf;     /* Bitmask identifying table iTab */
109052 #ifdef SQLITE_DEBUG
109053   char cId;             /* Symbolic ID of this loop for debugging use */
109054 #endif
109055   u8 iTab;              /* Position in FROM clause of table for this loop */
109056   u8 iSortIdx;          /* Sorting index number.  0==None */
109057   LogEst rSetup;        /* One-time setup cost (ex: create transient index) */
109058   LogEst rRun;          /* Cost of running each loop */
109059   LogEst nOut;          /* Estimated number of output rows */
109060   union {
109061     struct {               /* Information for internal btree tables */
109062       u16 nEq;               /* Number of equality constraints */
109063       u16 nSkip;             /* Number of initial index columns to skip */
109064       Index *pIndex;         /* Index used, or NULL */
109065     } btree;
109066     struct {               /* Information for virtual tables */
109067       int idxNum;            /* Index number */
109068       u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
109069       u8 isOrdered;          /* True if satisfies ORDER BY */
109070       u16 omitMask;          /* Terms that may be omitted */
109071       char *idxStr;          /* Index identifier string */
109072     } vtab;
109073   } u;
109074   u32 wsFlags;          /* WHERE_* flags describing the plan */
109075   u16 nLTerm;           /* Number of entries in aLTerm[] */
109076   /**** whereLoopXfer() copies fields above ***********************/
109077 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
109078   u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
109079   WhereTerm **aLTerm;   /* WhereTerms used */
109080   WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
109081   WhereTerm *aLTermSpace[4];  /* Initial aLTerm[] space */
109082 };
109083 
109084 /* This object holds the prerequisites and the cost of running a
109085 ** subquery on one operand of an OR operator in the WHERE clause.
109086 ** See WhereOrSet for additional information
109087 */
109088 struct WhereOrCost {
109089   Bitmask prereq;     /* Prerequisites */
109090   LogEst rRun;        /* Cost of running this subquery */
109091   LogEst nOut;        /* Number of outputs for this subquery */
109092 };
109093 
109094 /* The WhereOrSet object holds a set of possible WhereOrCosts that
109095 ** correspond to the subquery(s) of OR-clause processing.  Only the
109096 ** best N_OR_COST elements are retained.
109097 */
109098 #define N_OR_COST 3
109099 struct WhereOrSet {
109100   u16 n;                      /* Number of valid a[] entries */
109101   WhereOrCost a[N_OR_COST];   /* Set of best costs */
109102 };
109103 
109104 
109105 /* Forward declaration of methods */
109106 static int whereLoopResize(sqlite3*, WhereLoop*, int);
109107 
109108 /*
109109 ** Each instance of this object holds a sequence of WhereLoop objects
109110 ** that implement some or all of a query plan.
109111 **
109112 ** Think of each WhereLoop object as a node in a graph with arcs
109113 ** showing dependencies and costs for travelling between nodes.  (That is
109114 ** not a completely accurate description because WhereLoop costs are a
109115 ** vector, not a scalar, and because dependencies are many-to-one, not
109116 ** one-to-one as are graph nodes.  But it is a useful visualization aid.)
109117 ** Then a WherePath object is a path through the graph that visits some
109118 ** or all of the WhereLoop objects once.
109119 **
109120 ** The "solver" works by creating the N best WherePath objects of length
109121 ** 1.  Then using those as a basis to compute the N best WherePath objects
109122 ** of length 2.  And so forth until the length of WherePaths equals the
109123 ** number of nodes in the FROM clause.  The best (lowest cost) WherePath
109124 ** at the end is the choosen query plan.
109125 */
109126 struct WherePath {
109127   Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
109128   Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
109129   LogEst nRow;          /* Estimated number of rows generated by this path */
109130   LogEst rCost;         /* Total cost of this path */
109131   u8 isOrdered;         /* True if this path satisfies ORDER BY */
109132   u8 isOrderedValid;    /* True if the isOrdered field is valid */
109133   WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
109134 };
109135 
109136 /*
109137 ** The query generator uses an array of instances of this structure to
109138 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
109139 ** clause subexpression is separated from the others by AND operators,
109140 ** usually, or sometimes subexpressions separated by OR.
109141 **
109142 ** All WhereTerms are collected into a single WhereClause structure.
109143 ** The following identity holds:
109144 **
109145 **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
109146 **
109147 ** When a term is of the form:
109148 **
109149 **              X <op> <expr>
109150 **
109151 ** where X is a column name and <op> is one of certain operators,
109152 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
109153 ** cursor number and column number for X.  WhereTerm.eOperator records
109154 ** the <op> using a bitmask encoding defined by WO_xxx below.  The
109155 ** use of a bitmask encoding for the operator allows us to search
109156 ** quickly for terms that match any of several different operators.
109157 **
109158 ** A WhereTerm might also be two or more subterms connected by OR:
109159 **
109160 **         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
109161 **
109162 ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
109163 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
109164 ** is collected about the OR clause.
109165 **
109166 ** If a term in the WHERE clause does not match either of the two previous
109167 ** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
109168 ** to the original subexpression content and wtFlags is set up appropriately
109169 ** but no other fields in the WhereTerm object are meaningful.
109170 **
109171 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
109172 ** but they do so indirectly.  A single WhereMaskSet structure translates
109173 ** cursor number into bits and the translated bit is stored in the prereq
109174 ** fields.  The translation is used in order to maximize the number of
109175 ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
109176 ** spread out over the non-negative integers.  For example, the cursor
109177 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
109178 ** translates these sparse cursor numbers into consecutive integers
109179 ** beginning with 0 in order to make the best possible use of the available
109180 ** bits in the Bitmask.  So, in the example above, the cursor numbers
109181 ** would be mapped into integers 0 through 7.
109182 **
109183 ** The number of terms in a join is limited by the number of bits
109184 ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
109185 ** is only able to process joins with 64 or fewer tables.
109186 */
109187 struct WhereTerm {
109188   Expr *pExpr;            /* Pointer to the subexpression that is this term */
109189   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
109190   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
109191   union {
109192     int leftColumn;         /* Column number of X in "X <op> <expr>" */
109193     WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
109194     WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
109195   } u;
109196   LogEst truthProb;       /* Probability of truth for this expression */
109197   u16 eOperator;          /* A WO_xx value describing <op> */
109198   u8 wtFlags;             /* TERM_xxx bit flags.  See below */
109199   u8 nChild;              /* Number of children that must disable us */
109200   WhereClause *pWC;       /* The clause this term is part of */
109201   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
109202   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
109203 };
109204 
109205 /*
109206 ** Allowed values of WhereTerm.wtFlags
109207 */
109208 #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
109209 #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
109210 #define TERM_CODED      0x04   /* This term is already coded */
109211 #define TERM_COPIED     0x08   /* Has a child */
109212 #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
109213 #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
109214 #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
109215 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109216 #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
109217 #else
109218 #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
109219 #endif
109220 
109221 /*
109222 ** An instance of the WhereScan object is used as an iterator for locating
109223 ** terms in the WHERE clause that are useful to the query planner.
109224 */
109225 struct WhereScan {
109226   WhereClause *pOrigWC;      /* Original, innermost WhereClause */
109227   WhereClause *pWC;          /* WhereClause currently being scanned */
109228   char *zCollName;           /* Required collating sequence, if not NULL */
109229   char idxaff;               /* Must match this affinity, if zCollName!=NULL */
109230   unsigned char nEquiv;      /* Number of entries in aEquiv[] */
109231   unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
109232   u32 opMask;                /* Acceptable operators */
109233   int k;                     /* Resume scanning at this->pWC->a[this->k] */
109234   int aEquiv[22];            /* Cursor,Column pairs for equivalence classes */
109235 };
109236 
109237 /*
109238 ** An instance of the following structure holds all information about a
109239 ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
109240 **
109241 ** Explanation of pOuter:  For a WHERE clause of the form
109242 **
109243 **           a AND ((b AND c) OR (d AND e)) AND f
109244 **
109245 ** There are separate WhereClause objects for the whole clause and for
109246 ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
109247 ** subclauses points to the WhereClause object for the whole clause.
109248 */
109249 struct WhereClause {
109250   WhereInfo *pWInfo;       /* WHERE clause processing context */
109251   WhereClause *pOuter;     /* Outer conjunction */
109252   u8 op;                   /* Split operator.  TK_AND or TK_OR */
109253   int nTerm;               /* Number of terms */
109254   int nSlot;               /* Number of entries in a[] */
109255   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
109256 #if defined(SQLITE_SMALL_STACK)
109257   WhereTerm aStatic[1];    /* Initial static space for a[] */
109258 #else
109259   WhereTerm aStatic[8];    /* Initial static space for a[] */
109260 #endif
109261 };
109262 
109263 /*
109264 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
109265 ** a dynamically allocated instance of the following structure.
109266 */
109267 struct WhereOrInfo {
109268   WhereClause wc;          /* Decomposition into subterms */
109269   Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
109270 };
109271 
109272 /*
109273 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
109274 ** a dynamically allocated instance of the following structure.
109275 */
109276 struct WhereAndInfo {
109277   WhereClause wc;          /* The subexpression broken out */
109278 };
109279 
109280 /*
109281 ** An instance of the following structure keeps track of a mapping
109282 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
109283 **
109284 ** The VDBE cursor numbers are small integers contained in
109285 ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE
109286 ** clause, the cursor numbers might not begin with 0 and they might
109287 ** contain gaps in the numbering sequence.  But we want to make maximum
109288 ** use of the bits in our bitmasks.  This structure provides a mapping
109289 ** from the sparse cursor numbers into consecutive integers beginning
109290 ** with 0.
109291 **
109292 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
109293 ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
109294 **
109295 ** For example, if the WHERE clause expression used these VDBE
109296 ** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
109297 ** would map those cursor numbers into bits 0 through 5.
109298 **
109299 ** Note that the mapping is not necessarily ordered.  In the example
109300 ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
109301 ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
109302 ** does not really matter.  What is important is that sparse cursor
109303 ** numbers all get mapped into bit numbers that begin with 0 and contain
109304 ** no gaps.
109305 */
109306 struct WhereMaskSet {
109307   int n;                        /* Number of assigned cursor values */
109308   int ix[BMS];                  /* Cursor assigned to each bit */
109309 };
109310 
109311 /*
109312 ** This object is a convenience wrapper holding all information needed
109313 ** to construct WhereLoop objects for a particular query.
109314 */
109315 struct WhereLoopBuilder {
109316   WhereInfo *pWInfo;        /* Information about this WHERE */
109317   WhereClause *pWC;         /* WHERE clause terms */
109318   ExprList *pOrderBy;       /* ORDER BY clause */
109319   WhereLoop *pNew;          /* Template WhereLoop */
109320   WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
109321 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109322   UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
109323   int nRecValid;            /* Number of valid fields currently in pRec */
109324 #endif
109325 };
109326 
109327 /*
109328 ** The WHERE clause processing routine has two halves.  The
109329 ** first part does the start of the WHERE loop and the second
109330 ** half does the tail of the WHERE loop.  An instance of
109331 ** this structure is returned by the first half and passed
109332 ** into the second half to give some continuity.
109333 **
109334 ** An instance of this object holds the complete state of the query
109335 ** planner.
109336 */
109337 struct WhereInfo {
109338   Parse *pParse;            /* Parsing and code generating context */
109339   SrcList *pTabList;        /* List of tables in the join */
109340   ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
109341   ExprList *pResultSet;     /* Result set. DISTINCT operates on these */
109342   WhereLoop *pLoops;        /* List of all WhereLoop objects */
109343   Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
109344   LogEst nRowOut;           /* Estimated number of output rows */
109345   u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
109346   u8 bOBSat;                /* ORDER BY satisfied by indices */
109347   u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
109348   u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
109349   u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
109350   u8 nLevel;                /* Number of nested loop */
109351   int iTop;                 /* The very beginning of the WHERE loop */
109352   int iContinue;            /* Jump here to continue with next record */
109353   int iBreak;               /* Jump here to break out of the loop */
109354   int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
109355   int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
109356   WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
109357   WhereClause sWC;          /* Decomposition of the WHERE clause */
109358   WhereLevel a[1];          /* Information about each nest loop in WHERE */
109359 };
109360 
109361 /*
109362 ** Bitmasks for the operators on WhereTerm objects.  These are all
109363 ** operators that are of interest to the query planner.  An
109364 ** OR-ed combination of these values can be used when searching for
109365 ** particular WhereTerms within a WhereClause.
109366 */
109367 #define WO_IN     0x001
109368 #define WO_EQ     0x002
109369 #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
109370 #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
109371 #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
109372 #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
109373 #define WO_MATCH  0x040
109374 #define WO_ISNULL 0x080
109375 #define WO_OR     0x100       /* Two or more OR-connected terms */
109376 #define WO_AND    0x200       /* Two or more AND-connected terms */
109377 #define WO_EQUIV  0x400       /* Of the form A==B, both columns */
109378 #define WO_NOOP   0x800       /* This term does not restrict search space */
109379 
109380 #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
109381 #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
109382 
109383 /*
109384 ** These are definitions of bits in the WhereLoop.wsFlags field.
109385 ** The particular combination of bits in each WhereLoop help to
109386 ** determine the algorithm that WhereLoop represents.
109387 */
109388 #define WHERE_COLUMN_EQ    0x00000001  /* x=EXPR */
109389 #define WHERE_COLUMN_RANGE 0x00000002  /* x<EXPR and/or x>EXPR */
109390 #define WHERE_COLUMN_IN    0x00000004  /* x IN (...) */
109391 #define WHERE_COLUMN_NULL  0x00000008  /* x IS NULL */
109392 #define WHERE_CONSTRAINT   0x0000000f  /* Any of the WHERE_COLUMN_xxx values */
109393 #define WHERE_TOP_LIMIT    0x00000010  /* x<EXPR or x<=EXPR constraint */
109394 #define WHERE_BTM_LIMIT    0x00000020  /* x>EXPR or x>=EXPR constraint */
109395 #define WHERE_BOTH_LIMIT   0x00000030  /* Both x>EXPR and x<EXPR */
109396 #define WHERE_IDX_ONLY     0x00000040  /* Use index only - omit table */
109397 #define WHERE_IPK          0x00000100  /* x is the INTEGER PRIMARY KEY */
109398 #define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
109399 #define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
109400 #define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
109401 #define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
109402 #define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
109403 #define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
109404 #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
109405 #define WHERE_UNQ_WANTED   0x00010000  /* WHERE_ONEROW would have been helpful*/
109406 
109407 /************** End of whereInt.h ********************************************/
109408 /************** Continuing where we left off in where.c **********************/
109409 
109410 /*
109411 ** Return the estimated number of output rows from a WHERE clause
109412 */
109413 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
109414   return sqlite3LogEstToInt(pWInfo->nRowOut);
109415 }
109416 
109417 /*
109418 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
109419 ** WHERE clause returns outputs for DISTINCT processing.
109420 */
109421 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
109422   return pWInfo->eDistinct;
109423 }
109424 
109425 /*
109426 ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
109427 ** Return FALSE if the output needs to be sorted.
109428 */
109429 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
109430   return pWInfo->bOBSat!=0;
109431 }
109432 
109433 /*
109434 ** Return the VDBE address or label to jump to in order to continue
109435 ** immediately with the next row of a WHERE clause.
109436 */
109437 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
109438   return pWInfo->iContinue;
109439 }
109440 
109441 /*
109442 ** Return the VDBE address or label to jump to in order to break
109443 ** out of a WHERE loop.
109444 */
109445 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
109446   return pWInfo->iBreak;
109447 }
109448 
109449 /*
109450 ** Return TRUE if an UPDATE or DELETE statement can operate directly on
109451 ** the rowids returned by a WHERE clause.  Return FALSE if doing an
109452 ** UPDATE or DELETE might change subsequent WHERE clause results.
109453 **
109454 ** If the ONEPASS optimization is used (if this routine returns true)
109455 ** then also write the indices of open cursors used by ONEPASS
109456 ** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
109457 ** table and iaCur[1] gets the cursor used by an auxiliary index.
109458 ** Either value may be -1, indicating that cursor is not used.
109459 ** Any cursors returned will have been opened for writing.
109460 **
109461 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
109462 ** unable to use the ONEPASS optimization.
109463 */
109464 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
109465   memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
109466   return pWInfo->okOnePass;
109467 }
109468 
109469 /*
109470 ** Move the content of pSrc into pDest
109471 */
109472 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
109473   pDest->n = pSrc->n;
109474   memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
109475 }
109476 
109477 /*
109478 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
109479 **
109480 ** The new entry might overwrite an existing entry, or it might be
109481 ** appended, or it might be discarded.  Do whatever is the right thing
109482 ** so that pSet keeps the N_OR_COST best entries seen so far.
109483 */
109484 static int whereOrInsert(
109485   WhereOrSet *pSet,      /* The WhereOrSet to be updated */
109486   Bitmask prereq,        /* Prerequisites of the new entry */
109487   LogEst rRun,           /* Run-cost of the new entry */
109488   LogEst nOut            /* Number of outputs for the new entry */
109489 ){
109490   u16 i;
109491   WhereOrCost *p;
109492   for(i=pSet->n, p=pSet->a; i>0; i--, p++){
109493     if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
109494       goto whereOrInsert_done;
109495     }
109496     if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
109497       return 0;
109498     }
109499   }
109500   if( pSet->n<N_OR_COST ){
109501     p = &pSet->a[pSet->n++];
109502     p->nOut = nOut;
109503   }else{
109504     p = pSet->a;
109505     for(i=1; i<pSet->n; i++){
109506       if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
109507     }
109508     if( p->rRun<=rRun ) return 0;
109509   }
109510 whereOrInsert_done:
109511   p->prereq = prereq;
109512   p->rRun = rRun;
109513   if( p->nOut>nOut ) p->nOut = nOut;
109514   return 1;
109515 }
109516 
109517 /*
109518 ** Initialize a preallocated WhereClause structure.
109519 */
109520 static void whereClauseInit(
109521   WhereClause *pWC,        /* The WhereClause to be initialized */
109522   WhereInfo *pWInfo        /* The WHERE processing context */
109523 ){
109524   pWC->pWInfo = pWInfo;
109525   pWC->pOuter = 0;
109526   pWC->nTerm = 0;
109527   pWC->nSlot = ArraySize(pWC->aStatic);
109528   pWC->a = pWC->aStatic;
109529 }
109530 
109531 /* Forward reference */
109532 static void whereClauseClear(WhereClause*);
109533 
109534 /*
109535 ** Deallocate all memory associated with a WhereOrInfo object.
109536 */
109537 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
109538   whereClauseClear(&p->wc);
109539   sqlite3DbFree(db, p);
109540 }
109541 
109542 /*
109543 ** Deallocate all memory associated with a WhereAndInfo object.
109544 */
109545 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
109546   whereClauseClear(&p->wc);
109547   sqlite3DbFree(db, p);
109548 }
109549 
109550 /*
109551 ** Deallocate a WhereClause structure.  The WhereClause structure
109552 ** itself is not freed.  This routine is the inverse of whereClauseInit().
109553 */
109554 static void whereClauseClear(WhereClause *pWC){
109555   int i;
109556   WhereTerm *a;
109557   sqlite3 *db = pWC->pWInfo->pParse->db;
109558   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
109559     if( a->wtFlags & TERM_DYNAMIC ){
109560       sqlite3ExprDelete(db, a->pExpr);
109561     }
109562     if( a->wtFlags & TERM_ORINFO ){
109563       whereOrInfoDelete(db, a->u.pOrInfo);
109564     }else if( a->wtFlags & TERM_ANDINFO ){
109565       whereAndInfoDelete(db, a->u.pAndInfo);
109566     }
109567   }
109568   if( pWC->a!=pWC->aStatic ){
109569     sqlite3DbFree(db, pWC->a);
109570   }
109571 }
109572 
109573 /*
109574 ** Add a single new WhereTerm entry to the WhereClause object pWC.
109575 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
109576 ** The index in pWC->a[] of the new WhereTerm is returned on success.
109577 ** 0 is returned if the new WhereTerm could not be added due to a memory
109578 ** allocation error.  The memory allocation failure will be recorded in
109579 ** the db->mallocFailed flag so that higher-level functions can detect it.
109580 **
109581 ** This routine will increase the size of the pWC->a[] array as necessary.
109582 **
109583 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
109584 ** for freeing the expression p is assumed by the WhereClause object pWC.
109585 ** This is true even if this routine fails to allocate a new WhereTerm.
109586 **
109587 ** WARNING:  This routine might reallocate the space used to store
109588 ** WhereTerms.  All pointers to WhereTerms should be invalidated after
109589 ** calling this routine.  Such pointers may be reinitialized by referencing
109590 ** the pWC->a[] array.
109591 */
109592 static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
109593   WhereTerm *pTerm;
109594   int idx;
109595   testcase( wtFlags & TERM_VIRTUAL );
109596   if( pWC->nTerm>=pWC->nSlot ){
109597     WhereTerm *pOld = pWC->a;
109598     sqlite3 *db = pWC->pWInfo->pParse->db;
109599     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
109600     if( pWC->a==0 ){
109601       if( wtFlags & TERM_DYNAMIC ){
109602         sqlite3ExprDelete(db, p);
109603       }
109604       pWC->a = pOld;
109605       return 0;
109606     }
109607     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
109608     if( pOld!=pWC->aStatic ){
109609       sqlite3DbFree(db, pOld);
109610     }
109611     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
109612   }
109613   pTerm = &pWC->a[idx = pWC->nTerm++];
109614   if( p && ExprHasProperty(p, EP_Unlikely) ){
109615     pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
109616   }else{
109617     pTerm->truthProb = -1;
109618   }
109619   pTerm->pExpr = sqlite3ExprSkipCollate(p);
109620   pTerm->wtFlags = wtFlags;
109621   pTerm->pWC = pWC;
109622   pTerm->iParent = -1;
109623   return idx;
109624 }
109625 
109626 /*
109627 ** This routine identifies subexpressions in the WHERE clause where
109628 ** each subexpression is separated by the AND operator or some other
109629 ** operator specified in the op parameter.  The WhereClause structure
109630 ** is filled with pointers to subexpressions.  For example:
109631 **
109632 **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
109633 **           \________/     \_______________/     \________________/
109634 **            slot[0]            slot[1]               slot[2]
109635 **
109636 ** The original WHERE clause in pExpr is unaltered.  All this routine
109637 ** does is make slot[] entries point to substructure within pExpr.
109638 **
109639 ** In the previous sentence and in the diagram, "slot[]" refers to
109640 ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
109641 ** all terms of the WHERE clause.
109642 */
109643 static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
109644   pWC->op = op;
109645   if( pExpr==0 ) return;
109646   if( pExpr->op!=op ){
109647     whereClauseInsert(pWC, pExpr, 0);
109648   }else{
109649     whereSplit(pWC, pExpr->pLeft, op);
109650     whereSplit(pWC, pExpr->pRight, op);
109651   }
109652 }
109653 
109654 /*
109655 ** Initialize a WhereMaskSet object
109656 */
109657 #define initMaskSet(P)  (P)->n=0
109658 
109659 /*
109660 ** Return the bitmask for the given cursor number.  Return 0 if
109661 ** iCursor is not in the set.
109662 */
109663 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
109664   int i;
109665   assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
109666   for(i=0; i<pMaskSet->n; i++){
109667     if( pMaskSet->ix[i]==iCursor ){
109668       return MASKBIT(i);
109669     }
109670   }
109671   return 0;
109672 }
109673 
109674 /*
109675 ** Create a new mask for cursor iCursor.
109676 **
109677 ** There is one cursor per table in the FROM clause.  The number of
109678 ** tables in the FROM clause is limited by a test early in the
109679 ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
109680 ** array will never overflow.
109681 */
109682 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
109683   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
109684   pMaskSet->ix[pMaskSet->n++] = iCursor;
109685 }
109686 
109687 /*
109688 ** These routines walk (recursively) an expression tree and generate
109689 ** a bitmask indicating which tables are used in that expression
109690 ** tree.
109691 */
109692 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
109693 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
109694 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
109695   Bitmask mask = 0;
109696   if( p==0 ) return 0;
109697   if( p->op==TK_COLUMN ){
109698     mask = getMask(pMaskSet, p->iTable);
109699     return mask;
109700   }
109701   mask = exprTableUsage(pMaskSet, p->pRight);
109702   mask |= exprTableUsage(pMaskSet, p->pLeft);
109703   if( ExprHasProperty(p, EP_xIsSelect) ){
109704     mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
109705   }else{
109706     mask |= exprListTableUsage(pMaskSet, p->x.pList);
109707   }
109708   return mask;
109709 }
109710 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
109711   int i;
109712   Bitmask mask = 0;
109713   if( pList ){
109714     for(i=0; i<pList->nExpr; i++){
109715       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
109716     }
109717   }
109718   return mask;
109719 }
109720 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
109721   Bitmask mask = 0;
109722   while( pS ){
109723     SrcList *pSrc = pS->pSrc;
109724     mask |= exprListTableUsage(pMaskSet, pS->pEList);
109725     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
109726     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
109727     mask |= exprTableUsage(pMaskSet, pS->pWhere);
109728     mask |= exprTableUsage(pMaskSet, pS->pHaving);
109729     if( ALWAYS(pSrc!=0) ){
109730       int i;
109731       for(i=0; i<pSrc->nSrc; i++){
109732         mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
109733         mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
109734       }
109735     }
109736     pS = pS->pPrior;
109737   }
109738   return mask;
109739 }
109740 
109741 /*
109742 ** Return TRUE if the given operator is one of the operators that is
109743 ** allowed for an indexable WHERE clause term.  The allowed operators are
109744 ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
109745 */
109746 static int allowedOp(int op){
109747   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
109748   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
109749   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
109750   assert( TK_GE==TK_EQ+4 );
109751   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
109752 }
109753 
109754 /*
109755 ** Swap two objects of type TYPE.
109756 */
109757 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
109758 
109759 /*
109760 ** Commute a comparison operator.  Expressions of the form "X op Y"
109761 ** are converted into "Y op X".
109762 **
109763 ** If left/right precedence rules come into play when determining the
109764 ** collating sequence, then COLLATE operators are adjusted to ensure
109765 ** that the collating sequence does not change.  For example:
109766 ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
109767 ** the left hand side of a comparison overrides any collation sequence
109768 ** attached to the right. For the same reason the EP_Collate flag
109769 ** is not commuted.
109770 */
109771 static void exprCommute(Parse *pParse, Expr *pExpr){
109772   u16 expRight = (pExpr->pRight->flags & EP_Collate);
109773   u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
109774   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
109775   if( expRight==expLeft ){
109776     /* Either X and Y both have COLLATE operator or neither do */
109777     if( expRight ){
109778       /* Both X and Y have COLLATE operators.  Make sure X is always
109779       ** used by clearing the EP_Collate flag from Y. */
109780       pExpr->pRight->flags &= ~EP_Collate;
109781     }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
109782       /* Neither X nor Y have COLLATE operators, but X has a non-default
109783       ** collating sequence.  So add the EP_Collate marker on X to cause
109784       ** it to be searched first. */
109785       pExpr->pLeft->flags |= EP_Collate;
109786     }
109787   }
109788   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
109789   if( pExpr->op>=TK_GT ){
109790     assert( TK_LT==TK_GT+2 );
109791     assert( TK_GE==TK_LE+2 );
109792     assert( TK_GT>TK_EQ );
109793     assert( TK_GT<TK_LE );
109794     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
109795     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
109796   }
109797 }
109798 
109799 /*
109800 ** Translate from TK_xx operator to WO_xx bitmask.
109801 */
109802 static u16 operatorMask(int op){
109803   u16 c;
109804   assert( allowedOp(op) );
109805   if( op==TK_IN ){
109806     c = WO_IN;
109807   }else if( op==TK_ISNULL ){
109808     c = WO_ISNULL;
109809   }else{
109810     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
109811     c = (u16)(WO_EQ<<(op-TK_EQ));
109812   }
109813   assert( op!=TK_ISNULL || c==WO_ISNULL );
109814   assert( op!=TK_IN || c==WO_IN );
109815   assert( op!=TK_EQ || c==WO_EQ );
109816   assert( op!=TK_LT || c==WO_LT );
109817   assert( op!=TK_LE || c==WO_LE );
109818   assert( op!=TK_GT || c==WO_GT );
109819   assert( op!=TK_GE || c==WO_GE );
109820   return c;
109821 }
109822 
109823 /*
109824 ** Advance to the next WhereTerm that matches according to the criteria
109825 ** established when the pScan object was initialized by whereScanInit().
109826 ** Return NULL if there are no more matching WhereTerms.
109827 */
109828 static WhereTerm *whereScanNext(WhereScan *pScan){
109829   int iCur;            /* The cursor on the LHS of the term */
109830   int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
109831   Expr *pX;            /* An expression being tested */
109832   WhereClause *pWC;    /* Shorthand for pScan->pWC */
109833   WhereTerm *pTerm;    /* The term being tested */
109834   int k = pScan->k;    /* Where to start scanning */
109835 
109836   while( pScan->iEquiv<=pScan->nEquiv ){
109837     iCur = pScan->aEquiv[pScan->iEquiv-2];
109838     iColumn = pScan->aEquiv[pScan->iEquiv-1];
109839     while( (pWC = pScan->pWC)!=0 ){
109840       for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
109841         if( pTerm->leftCursor==iCur
109842          && pTerm->u.leftColumn==iColumn
109843          && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
109844         ){
109845           if( (pTerm->eOperator & WO_EQUIV)!=0
109846            && pScan->nEquiv<ArraySize(pScan->aEquiv)
109847           ){
109848             int j;
109849             pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
109850             assert( pX->op==TK_COLUMN );
109851             for(j=0; j<pScan->nEquiv; j+=2){
109852               if( pScan->aEquiv[j]==pX->iTable
109853                && pScan->aEquiv[j+1]==pX->iColumn ){
109854                   break;
109855               }
109856             }
109857             if( j==pScan->nEquiv ){
109858               pScan->aEquiv[j] = pX->iTable;
109859               pScan->aEquiv[j+1] = pX->iColumn;
109860               pScan->nEquiv += 2;
109861             }
109862           }
109863           if( (pTerm->eOperator & pScan->opMask)!=0 ){
109864             /* Verify the affinity and collating sequence match */
109865             if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
109866               CollSeq *pColl;
109867               Parse *pParse = pWC->pWInfo->pParse;
109868               pX = pTerm->pExpr;
109869               if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
109870                 continue;
109871               }
109872               assert(pX->pLeft);
109873               pColl = sqlite3BinaryCompareCollSeq(pParse,
109874                                                   pX->pLeft, pX->pRight);
109875               if( pColl==0 ) pColl = pParse->db->pDfltColl;
109876               if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
109877                 continue;
109878               }
109879             }
109880             if( (pTerm->eOperator & WO_EQ)!=0
109881              && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
109882              && pX->iTable==pScan->aEquiv[0]
109883              && pX->iColumn==pScan->aEquiv[1]
109884             ){
109885               continue;
109886             }
109887             pScan->k = k+1;
109888             return pTerm;
109889           }
109890         }
109891       }
109892       pScan->pWC = pScan->pWC->pOuter;
109893       k = 0;
109894     }
109895     pScan->pWC = pScan->pOrigWC;
109896     k = 0;
109897     pScan->iEquiv += 2;
109898   }
109899   return 0;
109900 }
109901 
109902 /*
109903 ** Initialize a WHERE clause scanner object.  Return a pointer to the
109904 ** first match.  Return NULL if there are no matches.
109905 **
109906 ** The scanner will be searching the WHERE clause pWC.  It will look
109907 ** for terms of the form "X <op> <expr>" where X is column iColumn of table
109908 ** iCur.  The <op> must be one of the operators described by opMask.
109909 **
109910 ** If the search is for X and the WHERE clause contains terms of the
109911 ** form X=Y then this routine might also return terms of the form
109912 ** "Y <op> <expr>".  The number of levels of transitivity is limited,
109913 ** but is enough to handle most commonly occurring SQL statements.
109914 **
109915 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with
109916 ** index pIdx.
109917 */
109918 static WhereTerm *whereScanInit(
109919   WhereScan *pScan,       /* The WhereScan object being initialized */
109920   WhereClause *pWC,       /* The WHERE clause to be scanned */
109921   int iCur,               /* Cursor to scan for */
109922   int iColumn,            /* Column to scan for */
109923   u32 opMask,             /* Operator(s) to scan for */
109924   Index *pIdx             /* Must be compatible with this index */
109925 ){
109926   int j;
109927 
109928   /* memset(pScan, 0, sizeof(*pScan)); */
109929   pScan->pOrigWC = pWC;
109930   pScan->pWC = pWC;
109931   if( pIdx && iColumn>=0 ){
109932     pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
109933     for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
109934       if( NEVER(j>=pIdx->nKeyCol) ) return 0;
109935     }
109936     pScan->zCollName = pIdx->azColl[j];
109937   }else{
109938     pScan->idxaff = 0;
109939     pScan->zCollName = 0;
109940   }
109941   pScan->opMask = opMask;
109942   pScan->k = 0;
109943   pScan->aEquiv[0] = iCur;
109944   pScan->aEquiv[1] = iColumn;
109945   pScan->nEquiv = 2;
109946   pScan->iEquiv = 2;
109947   return whereScanNext(pScan);
109948 }
109949 
109950 /*
109951 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
109952 ** where X is a reference to the iColumn of table iCur and <op> is one of
109953 ** the WO_xx operator codes specified by the op parameter.
109954 ** Return a pointer to the term.  Return 0 if not found.
109955 **
109956 ** The term returned might by Y=<expr> if there is another constraint in
109957 ** the WHERE clause that specifies that X=Y.  Any such constraints will be
109958 ** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
109959 ** aEquiv[] array holds X and all its equivalents, with each SQL variable
109960 ** taking up two slots in aEquiv[].  The first slot is for the cursor number
109961 ** and the second is for the column number.  There are 22 slots in aEquiv[]
109962 ** so that means we can look for X plus up to 10 other equivalent values.
109963 ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
109964 ** and ... and A9=A10 and A10=<expr>.
109965 **
109966 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
109967 ** then try for the one with no dependencies on <expr> - in other words where
109968 ** <expr> is a constant expression of some kind.  Only return entries of
109969 ** the form "X <op> Y" where Y is a column in another table if no terms of
109970 ** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
109971 ** exist, try to return a term that does not use WO_EQUIV.
109972 */
109973 static WhereTerm *findTerm(
109974   WhereClause *pWC,     /* The WHERE clause to be searched */
109975   int iCur,             /* Cursor number of LHS */
109976   int iColumn,          /* Column number of LHS */
109977   Bitmask notReady,     /* RHS must not overlap with this mask */
109978   u32 op,               /* Mask of WO_xx values describing operator */
109979   Index *pIdx           /* Must be compatible with this index, if not NULL */
109980 ){
109981   WhereTerm *pResult = 0;
109982   WhereTerm *p;
109983   WhereScan scan;
109984 
109985   p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
109986   while( p ){
109987     if( (p->prereqRight & notReady)==0 ){
109988       if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
109989         return p;
109990       }
109991       if( pResult==0 ) pResult = p;
109992     }
109993     p = whereScanNext(&scan);
109994   }
109995   return pResult;
109996 }
109997 
109998 /* Forward reference */
109999 static void exprAnalyze(SrcList*, WhereClause*, int);
110000 
110001 /*
110002 ** Call exprAnalyze on all terms in a WHERE clause.
110003 */
110004 static void exprAnalyzeAll(
110005   SrcList *pTabList,       /* the FROM clause */
110006   WhereClause *pWC         /* the WHERE clause to be analyzed */
110007 ){
110008   int i;
110009   for(i=pWC->nTerm-1; i>=0; i--){
110010     exprAnalyze(pTabList, pWC, i);
110011   }
110012 }
110013 
110014 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
110015 /*
110016 ** Check to see if the given expression is a LIKE or GLOB operator that
110017 ** can be optimized using inequality constraints.  Return TRUE if it is
110018 ** so and false if not.
110019 **
110020 ** In order for the operator to be optimizible, the RHS must be a string
110021 ** literal that does not begin with a wildcard.
110022 */
110023 static int isLikeOrGlob(
110024   Parse *pParse,    /* Parsing and code generating context */
110025   Expr *pExpr,      /* Test this expression */
110026   Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
110027   int *pisComplete, /* True if the only wildcard is % in the last character */
110028   int *pnoCase      /* True if uppercase is equivalent to lowercase */
110029 ){
110030   const char *z = 0;         /* String on RHS of LIKE operator */
110031   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
110032   ExprList *pList;           /* List of operands to the LIKE operator */
110033   int c;                     /* One character in z[] */
110034   int cnt;                   /* Number of non-wildcard prefix characters */
110035   char wc[3];                /* Wildcard characters */
110036   sqlite3 *db = pParse->db;  /* Database connection */
110037   sqlite3_value *pVal = 0;
110038   int op;                    /* Opcode of pRight */
110039 
110040   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
110041     return 0;
110042   }
110043 #ifdef SQLITE_EBCDIC
110044   if( *pnoCase ) return 0;
110045 #endif
110046   pList = pExpr->x.pList;
110047   pLeft = pList->a[1].pExpr;
110048   if( pLeft->op!=TK_COLUMN
110049    || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
110050    || IsVirtual(pLeft->pTab)
110051   ){
110052     /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
110053     ** be the name of an indexed column with TEXT affinity. */
110054     return 0;
110055   }
110056   assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
110057 
110058   pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
110059   op = pRight->op;
110060   if( op==TK_VARIABLE ){
110061     Vdbe *pReprepare = pParse->pReprepare;
110062     int iCol = pRight->iColumn;
110063     pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
110064     if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
110065       z = (char *)sqlite3_value_text(pVal);
110066     }
110067     sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
110068     assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
110069   }else if( op==TK_STRING ){
110070     z = pRight->u.zToken;
110071   }
110072   if( z ){
110073     cnt = 0;
110074     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
110075       cnt++;
110076     }
110077     if( cnt!=0 && 255!=(u8)z[cnt-1] ){
110078       Expr *pPrefix;
110079       *pisComplete = c==wc[0] && z[cnt+1]==0;
110080       pPrefix = sqlite3Expr(db, TK_STRING, z);
110081       if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
110082       *ppPrefix = pPrefix;
110083       if( op==TK_VARIABLE ){
110084         Vdbe *v = pParse->pVdbe;
110085         sqlite3VdbeSetVarmask(v, pRight->iColumn);
110086         if( *pisComplete && pRight->u.zToken[1] ){
110087           /* If the rhs of the LIKE expression is a variable, and the current
110088           ** value of the variable means there is no need to invoke the LIKE
110089           ** function, then no OP_Variable will be added to the program.
110090           ** This causes problems for the sqlite3_bind_parameter_name()
110091           ** API. To workaround them, add a dummy OP_Variable here.
110092           */
110093           int r1 = sqlite3GetTempReg(pParse);
110094           sqlite3ExprCodeTarget(pParse, pRight, r1);
110095           sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
110096           sqlite3ReleaseTempReg(pParse, r1);
110097         }
110098       }
110099     }else{
110100       z = 0;
110101     }
110102   }
110103 
110104   sqlite3ValueFree(pVal);
110105   return (z!=0);
110106 }
110107 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
110108 
110109 
110110 #ifndef SQLITE_OMIT_VIRTUALTABLE
110111 /*
110112 ** Check to see if the given expression is of the form
110113 **
110114 **         column MATCH expr
110115 **
110116 ** If it is then return TRUE.  If not, return FALSE.
110117 */
110118 static int isMatchOfColumn(
110119   Expr *pExpr      /* Test this expression */
110120 ){
110121   ExprList *pList;
110122 
110123   if( pExpr->op!=TK_FUNCTION ){
110124     return 0;
110125   }
110126   if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
110127     return 0;
110128   }
110129   pList = pExpr->x.pList;
110130   if( pList->nExpr!=2 ){
110131     return 0;
110132   }
110133   if( pList->a[1].pExpr->op != TK_COLUMN ){
110134     return 0;
110135   }
110136   return 1;
110137 }
110138 #endif /* SQLITE_OMIT_VIRTUALTABLE */
110139 
110140 /*
110141 ** If the pBase expression originated in the ON or USING clause of
110142 ** a join, then transfer the appropriate markings over to derived.
110143 */
110144 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
110145   if( pDerived ){
110146     pDerived->flags |= pBase->flags & EP_FromJoin;
110147     pDerived->iRightJoinTable = pBase->iRightJoinTable;
110148   }
110149 }
110150 
110151 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
110152 /*
110153 ** Analyze a term that consists of two or more OR-connected
110154 ** subterms.  So in:
110155 **
110156 **     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
110157 **                          ^^^^^^^^^^^^^^^^^^^^
110158 **
110159 ** This routine analyzes terms such as the middle term in the above example.
110160 ** A WhereOrTerm object is computed and attached to the term under
110161 ** analysis, regardless of the outcome of the analysis.  Hence:
110162 **
110163 **     WhereTerm.wtFlags   |=  TERM_ORINFO
110164 **     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
110165 **
110166 ** The term being analyzed must have two or more of OR-connected subterms.
110167 ** A single subterm might be a set of AND-connected sub-subterms.
110168 ** Examples of terms under analysis:
110169 **
110170 **     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
110171 **     (B)     x=expr1 OR expr2=x OR x=expr3
110172 **     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
110173 **     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
110174 **     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
110175 **
110176 ** CASE 1:
110177 **
110178 ** If all subterms are of the form T.C=expr for some single column of C and
110179 ** a single table T (as shown in example B above) then create a new virtual
110180 ** term that is an equivalent IN expression.  In other words, if the term
110181 ** being analyzed is:
110182 **
110183 **      x = expr1  OR  expr2 = x  OR  x = expr3
110184 **
110185 ** then create a new virtual term like this:
110186 **
110187 **      x IN (expr1,expr2,expr3)
110188 **
110189 ** CASE 2:
110190 **
110191 ** If all subterms are indexable by a single table T, then set
110192 **
110193 **     WhereTerm.eOperator              =  WO_OR
110194 **     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
110195 **
110196 ** A subterm is "indexable" if it is of the form
110197 ** "T.C <op> <expr>" where C is any column of table T and
110198 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
110199 ** A subterm is also indexable if it is an AND of two or more
110200 ** subsubterms at least one of which is indexable.  Indexable AND
110201 ** subterms have their eOperator set to WO_AND and they have
110202 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
110203 **
110204 ** From another point of view, "indexable" means that the subterm could
110205 ** potentially be used with an index if an appropriate index exists.
110206 ** This analysis does not consider whether or not the index exists; that
110207 ** is decided elsewhere.  This analysis only looks at whether subterms
110208 ** appropriate for indexing exist.
110209 **
110210 ** All examples A through E above satisfy case 2.  But if a term
110211 ** also statisfies case 1 (such as B) we know that the optimizer will
110212 ** always prefer case 1, so in that case we pretend that case 2 is not
110213 ** satisfied.
110214 **
110215 ** It might be the case that multiple tables are indexable.  For example,
110216 ** (E) above is indexable on tables P, Q, and R.
110217 **
110218 ** Terms that satisfy case 2 are candidates for lookup by using
110219 ** separate indices to find rowids for each subterm and composing
110220 ** the union of all rowids using a RowSet object.  This is similar
110221 ** to "bitmap indices" in other database engines.
110222 **
110223 ** OTHERWISE:
110224 **
110225 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
110226 ** zero.  This term is not useful for search.
110227 */
110228 static void exprAnalyzeOrTerm(
110229   SrcList *pSrc,            /* the FROM clause */
110230   WhereClause *pWC,         /* the complete WHERE clause */
110231   int idxTerm               /* Index of the OR-term to be analyzed */
110232 ){
110233   WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
110234   Parse *pParse = pWInfo->pParse;         /* Parser context */
110235   sqlite3 *db = pParse->db;               /* Database connection */
110236   WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
110237   Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
110238   int i;                                  /* Loop counters */
110239   WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
110240   WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
110241   WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
110242   Bitmask chngToIN;         /* Tables that might satisfy case 1 */
110243   Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
110244 
110245   /*
110246   ** Break the OR clause into its separate subterms.  The subterms are
110247   ** stored in a WhereClause structure containing within the WhereOrInfo
110248   ** object that is attached to the original OR clause term.
110249   */
110250   assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
110251   assert( pExpr->op==TK_OR );
110252   pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
110253   if( pOrInfo==0 ) return;
110254   pTerm->wtFlags |= TERM_ORINFO;
110255   pOrWc = &pOrInfo->wc;
110256   whereClauseInit(pOrWc, pWInfo);
110257   whereSplit(pOrWc, pExpr, TK_OR);
110258   exprAnalyzeAll(pSrc, pOrWc);
110259   if( db->mallocFailed ) return;
110260   assert( pOrWc->nTerm>=2 );
110261 
110262   /*
110263   ** Compute the set of tables that might satisfy cases 1 or 2.
110264   */
110265   indexable = ~(Bitmask)0;
110266   chngToIN = ~(Bitmask)0;
110267   for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
110268     if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
110269       WhereAndInfo *pAndInfo;
110270       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
110271       chngToIN = 0;
110272       pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
110273       if( pAndInfo ){
110274         WhereClause *pAndWC;
110275         WhereTerm *pAndTerm;
110276         int j;
110277         Bitmask b = 0;
110278         pOrTerm->u.pAndInfo = pAndInfo;
110279         pOrTerm->wtFlags |= TERM_ANDINFO;
110280         pOrTerm->eOperator = WO_AND;
110281         pAndWC = &pAndInfo->wc;
110282         whereClauseInit(pAndWC, pWC->pWInfo);
110283         whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
110284         exprAnalyzeAll(pSrc, pAndWC);
110285         pAndWC->pOuter = pWC;
110286         testcase( db->mallocFailed );
110287         if( !db->mallocFailed ){
110288           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
110289             assert( pAndTerm->pExpr );
110290             if( allowedOp(pAndTerm->pExpr->op) ){
110291               b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
110292             }
110293           }
110294         }
110295         indexable &= b;
110296       }
110297     }else if( pOrTerm->wtFlags & TERM_COPIED ){
110298       /* Skip this term for now.  We revisit it when we process the
110299       ** corresponding TERM_VIRTUAL term */
110300     }else{
110301       Bitmask b;
110302       b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
110303       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
110304         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
110305         b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
110306       }
110307       indexable &= b;
110308       if( (pOrTerm->eOperator & WO_EQ)==0 ){
110309         chngToIN = 0;
110310       }else{
110311         chngToIN &= b;
110312       }
110313     }
110314   }
110315 
110316   /*
110317   ** Record the set of tables that satisfy case 2.  The set might be
110318   ** empty.
110319   */
110320   pOrInfo->indexable = indexable;
110321   pTerm->eOperator = indexable==0 ? 0 : WO_OR;
110322 
110323   /*
110324   ** chngToIN holds a set of tables that *might* satisfy case 1.  But
110325   ** we have to do some additional checking to see if case 1 really
110326   ** is satisfied.
110327   **
110328   ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
110329   ** that there is no possibility of transforming the OR clause into an
110330   ** IN operator because one or more terms in the OR clause contain
110331   ** something other than == on a column in the single table.  The 1-bit
110332   ** case means that every term of the OR clause is of the form
110333   ** "table.column=expr" for some single table.  The one bit that is set
110334   ** will correspond to the common table.  We still need to check to make
110335   ** sure the same column is used on all terms.  The 2-bit case is when
110336   ** the all terms are of the form "table1.column=table2.column".  It
110337   ** might be possible to form an IN operator with either table1.column
110338   ** or table2.column as the LHS if either is common to every term of
110339   ** the OR clause.
110340   **
110341   ** Note that terms of the form "table.column1=table.column2" (the
110342   ** same table on both sizes of the ==) cannot be optimized.
110343   */
110344   if( chngToIN ){
110345     int okToChngToIN = 0;     /* True if the conversion to IN is valid */
110346     int iColumn = -1;         /* Column index on lhs of IN operator */
110347     int iCursor = -1;         /* Table cursor common to all terms */
110348     int j = 0;                /* Loop counter */
110349 
110350     /* Search for a table and column that appears on one side or the
110351     ** other of the == operator in every subterm.  That table and column
110352     ** will be recorded in iCursor and iColumn.  There might not be any
110353     ** such table and column.  Set okToChngToIN if an appropriate table
110354     ** and column is found but leave okToChngToIN false if not found.
110355     */
110356     for(j=0; j<2 && !okToChngToIN; j++){
110357       pOrTerm = pOrWc->a;
110358       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
110359         assert( pOrTerm->eOperator & WO_EQ );
110360         pOrTerm->wtFlags &= ~TERM_OR_OK;
110361         if( pOrTerm->leftCursor==iCursor ){
110362           /* This is the 2-bit case and we are on the second iteration and
110363           ** current term is from the first iteration.  So skip this term. */
110364           assert( j==1 );
110365           continue;
110366         }
110367         if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
110368           /* This term must be of the form t1.a==t2.b where t2 is in the
110369           ** chngToIN set but t1 is not.  This term will be either preceeded
110370           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term
110371           ** and use its inversion. */
110372           testcase( pOrTerm->wtFlags & TERM_COPIED );
110373           testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
110374           assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
110375           continue;
110376         }
110377         iColumn = pOrTerm->u.leftColumn;
110378         iCursor = pOrTerm->leftCursor;
110379         break;
110380       }
110381       if( i<0 ){
110382         /* No candidate table+column was found.  This can only occur
110383         ** on the second iteration */
110384         assert( j==1 );
110385         assert( IsPowerOfTwo(chngToIN) );
110386         assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
110387         break;
110388       }
110389       testcase( j==1 );
110390 
110391       /* We have found a candidate table and column.  Check to see if that
110392       ** table and column is common to every term in the OR clause */
110393       okToChngToIN = 1;
110394       for(; i>=0 && okToChngToIN; i--, pOrTerm++){
110395         assert( pOrTerm->eOperator & WO_EQ );
110396         if( pOrTerm->leftCursor!=iCursor ){
110397           pOrTerm->wtFlags &= ~TERM_OR_OK;
110398         }else if( pOrTerm->u.leftColumn!=iColumn ){
110399           okToChngToIN = 0;
110400         }else{
110401           int affLeft, affRight;
110402           /* If the right-hand side is also a column, then the affinities
110403           ** of both right and left sides must be such that no type
110404           ** conversions are required on the right.  (Ticket #2249)
110405           */
110406           affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
110407           affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
110408           if( affRight!=0 && affRight!=affLeft ){
110409             okToChngToIN = 0;
110410           }else{
110411             pOrTerm->wtFlags |= TERM_OR_OK;
110412           }
110413         }
110414       }
110415     }
110416 
110417     /* At this point, okToChngToIN is true if original pTerm satisfies
110418     ** case 1.  In that case, construct a new virtual term that is
110419     ** pTerm converted into an IN operator.
110420     */
110421     if( okToChngToIN ){
110422       Expr *pDup;            /* A transient duplicate expression */
110423       ExprList *pList = 0;   /* The RHS of the IN operator */
110424       Expr *pLeft = 0;       /* The LHS of the IN operator */
110425       Expr *pNew;            /* The complete IN operator */
110426 
110427       for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
110428         if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
110429         assert( pOrTerm->eOperator & WO_EQ );
110430         assert( pOrTerm->leftCursor==iCursor );
110431         assert( pOrTerm->u.leftColumn==iColumn );
110432         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
110433         pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
110434         pLeft = pOrTerm->pExpr->pLeft;
110435       }
110436       assert( pLeft!=0 );
110437       pDup = sqlite3ExprDup(db, pLeft, 0);
110438       pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
110439       if( pNew ){
110440         int idxNew;
110441         transferJoinMarkings(pNew, pExpr);
110442         assert( !ExprHasProperty(pNew, EP_xIsSelect) );
110443         pNew->x.pList = pList;
110444         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
110445         testcase( idxNew==0 );
110446         exprAnalyze(pSrc, pWC, idxNew);
110447         pTerm = &pWC->a[idxTerm];
110448         pWC->a[idxNew].iParent = idxTerm;
110449         pTerm->nChild = 1;
110450       }else{
110451         sqlite3ExprListDelete(db, pList);
110452       }
110453       pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
110454     }
110455   }
110456 }
110457 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
110458 
110459 /*
110460 ** The input to this routine is an WhereTerm structure with only the
110461 ** "pExpr" field filled in.  The job of this routine is to analyze the
110462 ** subexpression and populate all the other fields of the WhereTerm
110463 ** structure.
110464 **
110465 ** If the expression is of the form "<expr> <op> X" it gets commuted
110466 ** to the standard form of "X <op> <expr>".
110467 **
110468 ** If the expression is of the form "X <op> Y" where both X and Y are
110469 ** columns, then the original expression is unchanged and a new virtual
110470 ** term of the form "Y <op> X" is added to the WHERE clause and
110471 ** analyzed separately.  The original term is marked with TERM_COPIED
110472 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
110473 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
110474 ** is a commuted copy of a prior term.)  The original term has nChild=1
110475 ** and the copy has idxParent set to the index of the original term.
110476 */
110477 static void exprAnalyze(
110478   SrcList *pSrc,            /* the FROM clause */
110479   WhereClause *pWC,         /* the WHERE clause */
110480   int idxTerm               /* Index of the term to be analyzed */
110481 ){
110482   WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
110483   WhereTerm *pTerm;                /* The term to be analyzed */
110484   WhereMaskSet *pMaskSet;          /* Set of table index masks */
110485   Expr *pExpr;                     /* The expression to be analyzed */
110486   Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
110487   Bitmask prereqAll;               /* Prerequesites of pExpr */
110488   Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
110489   Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
110490   int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
110491   int noCase = 0;                  /* LIKE/GLOB distinguishes case */
110492   int op;                          /* Top-level operator.  pExpr->op */
110493   Parse *pParse = pWInfo->pParse;  /* Parsing context */
110494   sqlite3 *db = pParse->db;        /* Database connection */
110495 
110496   if( db->mallocFailed ){
110497     return;
110498   }
110499   pTerm = &pWC->a[idxTerm];
110500   pMaskSet = &pWInfo->sMaskSet;
110501   pExpr = pTerm->pExpr;
110502   assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
110503   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
110504   op = pExpr->op;
110505   if( op==TK_IN ){
110506     assert( pExpr->pRight==0 );
110507     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
110508       pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
110509     }else{
110510       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
110511     }
110512   }else if( op==TK_ISNULL ){
110513     pTerm->prereqRight = 0;
110514   }else{
110515     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
110516   }
110517   prereqAll = exprTableUsage(pMaskSet, pExpr);
110518   if( ExprHasProperty(pExpr, EP_FromJoin) ){
110519     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
110520     prereqAll |= x;
110521     extraRight = x-1;  /* ON clause terms may not be used with an index
110522                        ** on left table of a LEFT JOIN.  Ticket #3015 */
110523   }
110524   pTerm->prereqAll = prereqAll;
110525   pTerm->leftCursor = -1;
110526   pTerm->iParent = -1;
110527   pTerm->eOperator = 0;
110528   if( allowedOp(op) ){
110529     Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
110530     Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
110531     u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
110532     if( pLeft->op==TK_COLUMN ){
110533       pTerm->leftCursor = pLeft->iTable;
110534       pTerm->u.leftColumn = pLeft->iColumn;
110535       pTerm->eOperator = operatorMask(op) & opMask;
110536     }
110537     if( pRight && pRight->op==TK_COLUMN ){
110538       WhereTerm *pNew;
110539       Expr *pDup;
110540       u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
110541       if( pTerm->leftCursor>=0 ){
110542         int idxNew;
110543         pDup = sqlite3ExprDup(db, pExpr, 0);
110544         if( db->mallocFailed ){
110545           sqlite3ExprDelete(db, pDup);
110546           return;
110547         }
110548         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
110549         if( idxNew==0 ) return;
110550         pNew = &pWC->a[idxNew];
110551         pNew->iParent = idxTerm;
110552         pTerm = &pWC->a[idxTerm];
110553         pTerm->nChild = 1;
110554         pTerm->wtFlags |= TERM_COPIED;
110555         if( pExpr->op==TK_EQ
110556          && !ExprHasProperty(pExpr, EP_FromJoin)
110557          && OptimizationEnabled(db, SQLITE_Transitive)
110558         ){
110559           pTerm->eOperator |= WO_EQUIV;
110560           eExtraOp = WO_EQUIV;
110561         }
110562       }else{
110563         pDup = pExpr;
110564         pNew = pTerm;
110565       }
110566       exprCommute(pParse, pDup);
110567       pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
110568       pNew->leftCursor = pLeft->iTable;
110569       pNew->u.leftColumn = pLeft->iColumn;
110570       testcase( (prereqLeft | extraRight) != prereqLeft );
110571       pNew->prereqRight = prereqLeft | extraRight;
110572       pNew->prereqAll = prereqAll;
110573       pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
110574     }
110575   }
110576 
110577 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
110578   /* If a term is the BETWEEN operator, create two new virtual terms
110579   ** that define the range that the BETWEEN implements.  For example:
110580   **
110581   **      a BETWEEN b AND c
110582   **
110583   ** is converted into:
110584   **
110585   **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
110586   **
110587   ** The two new terms are added onto the end of the WhereClause object.
110588   ** The new terms are "dynamic" and are children of the original BETWEEN
110589   ** term.  That means that if the BETWEEN term is coded, the children are
110590   ** skipped.  Or, if the children are satisfied by an index, the original
110591   ** BETWEEN term is skipped.
110592   */
110593   else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
110594     ExprList *pList = pExpr->x.pList;
110595     int i;
110596     static const u8 ops[] = {TK_GE, TK_LE};
110597     assert( pList!=0 );
110598     assert( pList->nExpr==2 );
110599     for(i=0; i<2; i++){
110600       Expr *pNewExpr;
110601       int idxNew;
110602       pNewExpr = sqlite3PExpr(pParse, ops[i],
110603                              sqlite3ExprDup(db, pExpr->pLeft, 0),
110604                              sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
110605       transferJoinMarkings(pNewExpr, pExpr);
110606       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
110607       testcase( idxNew==0 );
110608       exprAnalyze(pSrc, pWC, idxNew);
110609       pTerm = &pWC->a[idxTerm];
110610       pWC->a[idxNew].iParent = idxTerm;
110611     }
110612     pTerm->nChild = 2;
110613   }
110614 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
110615 
110616 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
110617   /* Analyze a term that is composed of two or more subterms connected by
110618   ** an OR operator.
110619   */
110620   else if( pExpr->op==TK_OR ){
110621     assert( pWC->op==TK_AND );
110622     exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
110623     pTerm = &pWC->a[idxTerm];
110624   }
110625 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
110626 
110627 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
110628   /* Add constraints to reduce the search space on a LIKE or GLOB
110629   ** operator.
110630   **
110631   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
110632   **
110633   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
110634   **
110635   ** The last character of the prefix "abc" is incremented to form the
110636   ** termination condition "abd".
110637   */
110638   if( pWC->op==TK_AND
110639    && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
110640   ){
110641     Expr *pLeft;       /* LHS of LIKE/GLOB operator */
110642     Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
110643     Expr *pNewExpr1;
110644     Expr *pNewExpr2;
110645     int idxNew1;
110646     int idxNew2;
110647     Token sCollSeqName;  /* Name of collating sequence */
110648 
110649     pLeft = pExpr->x.pList->a[1].pExpr;
110650     pStr2 = sqlite3ExprDup(db, pStr1, 0);
110651     if( !db->mallocFailed ){
110652       u8 c, *pC;       /* Last character before the first wildcard */
110653       pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
110654       c = *pC;
110655       if( noCase ){
110656         /* The point is to increment the last character before the first
110657         ** wildcard.  But if we increment '@', that will push it into the
110658         ** alphabetic range where case conversions will mess up the
110659         ** inequality.  To avoid this, make sure to also run the full
110660         ** LIKE on all candidate expressions by clearing the isComplete flag
110661         */
110662         if( c=='A'-1 ) isComplete = 0;
110663         c = sqlite3UpperToLower[c];
110664       }
110665       *pC = c + 1;
110666     }
110667     sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
110668     sCollSeqName.n = 6;
110669     pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
110670     pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
110671            sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
110672            pStr1, 0);
110673     transferJoinMarkings(pNewExpr1, pExpr);
110674     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
110675     testcase( idxNew1==0 );
110676     exprAnalyze(pSrc, pWC, idxNew1);
110677     pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
110678     pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
110679            sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
110680            pStr2, 0);
110681     transferJoinMarkings(pNewExpr2, pExpr);
110682     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
110683     testcase( idxNew2==0 );
110684     exprAnalyze(pSrc, pWC, idxNew2);
110685     pTerm = &pWC->a[idxTerm];
110686     if( isComplete ){
110687       pWC->a[idxNew1].iParent = idxTerm;
110688       pWC->a[idxNew2].iParent = idxTerm;
110689       pTerm->nChild = 2;
110690     }
110691   }
110692 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
110693 
110694 #ifndef SQLITE_OMIT_VIRTUALTABLE
110695   /* Add a WO_MATCH auxiliary term to the constraint set if the
110696   ** current expression is of the form:  column MATCH expr.
110697   ** This information is used by the xBestIndex methods of
110698   ** virtual tables.  The native query optimizer does not attempt
110699   ** to do anything with MATCH functions.
110700   */
110701   if( isMatchOfColumn(pExpr) ){
110702     int idxNew;
110703     Expr *pRight, *pLeft;
110704     WhereTerm *pNewTerm;
110705     Bitmask prereqColumn, prereqExpr;
110706 
110707     pRight = pExpr->x.pList->a[0].pExpr;
110708     pLeft = pExpr->x.pList->a[1].pExpr;
110709     prereqExpr = exprTableUsage(pMaskSet, pRight);
110710     prereqColumn = exprTableUsage(pMaskSet, pLeft);
110711     if( (prereqExpr & prereqColumn)==0 ){
110712       Expr *pNewExpr;
110713       pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
110714                               0, sqlite3ExprDup(db, pRight, 0), 0);
110715       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
110716       testcase( idxNew==0 );
110717       pNewTerm = &pWC->a[idxNew];
110718       pNewTerm->prereqRight = prereqExpr;
110719       pNewTerm->leftCursor = pLeft->iTable;
110720       pNewTerm->u.leftColumn = pLeft->iColumn;
110721       pNewTerm->eOperator = WO_MATCH;
110722       pNewTerm->iParent = idxTerm;
110723       pTerm = &pWC->a[idxTerm];
110724       pTerm->nChild = 1;
110725       pTerm->wtFlags |= TERM_COPIED;
110726       pNewTerm->prereqAll = pTerm->prereqAll;
110727     }
110728   }
110729 #endif /* SQLITE_OMIT_VIRTUALTABLE */
110730 
110731 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110732   /* When sqlite_stat3 histogram data is available an operator of the
110733   ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
110734   ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
110735   ** virtual term of that form.
110736   **
110737   ** Note that the virtual term must be tagged with TERM_VNULL.  This
110738   ** TERM_VNULL tag will suppress the not-null check at the beginning
110739   ** of the loop.  Without the TERM_VNULL flag, the not-null check at
110740   ** the start of the loop will prevent any results from being returned.
110741   */
110742   if( pExpr->op==TK_NOTNULL
110743    && pExpr->pLeft->op==TK_COLUMN
110744    && pExpr->pLeft->iColumn>=0
110745    && OptimizationEnabled(db, SQLITE_Stat3)
110746   ){
110747     Expr *pNewExpr;
110748     Expr *pLeft = pExpr->pLeft;
110749     int idxNew;
110750     WhereTerm *pNewTerm;
110751 
110752     pNewExpr = sqlite3PExpr(pParse, TK_GT,
110753                             sqlite3ExprDup(db, pLeft, 0),
110754                             sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
110755 
110756     idxNew = whereClauseInsert(pWC, pNewExpr,
110757                               TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
110758     if( idxNew ){
110759       pNewTerm = &pWC->a[idxNew];
110760       pNewTerm->prereqRight = 0;
110761       pNewTerm->leftCursor = pLeft->iTable;
110762       pNewTerm->u.leftColumn = pLeft->iColumn;
110763       pNewTerm->eOperator = WO_GT;
110764       pNewTerm->iParent = idxTerm;
110765       pTerm = &pWC->a[idxTerm];
110766       pTerm->nChild = 1;
110767       pTerm->wtFlags |= TERM_COPIED;
110768       pNewTerm->prereqAll = pTerm->prereqAll;
110769     }
110770   }
110771 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
110772 
110773   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
110774   ** an index for tables to the left of the join.
110775   */
110776   pTerm->prereqRight |= extraRight;
110777 }
110778 
110779 /*
110780 ** This function searches pList for a entry that matches the iCol-th column
110781 ** of index pIdx.
110782 **
110783 ** If such an expression is found, its index in pList->a[] is returned. If
110784 ** no expression is found, -1 is returned.
110785 */
110786 static int findIndexCol(
110787   Parse *pParse,                  /* Parse context */
110788   ExprList *pList,                /* Expression list to search */
110789   int iBase,                      /* Cursor for table associated with pIdx */
110790   Index *pIdx,                    /* Index to match column of */
110791   int iCol                        /* Column of index to match */
110792 ){
110793   int i;
110794   const char *zColl = pIdx->azColl[iCol];
110795 
110796   for(i=0; i<pList->nExpr; i++){
110797     Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
110798     if( p->op==TK_COLUMN
110799      && p->iColumn==pIdx->aiColumn[iCol]
110800      && p->iTable==iBase
110801     ){
110802       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
110803       if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
110804         return i;
110805       }
110806     }
110807   }
110808 
110809   return -1;
110810 }
110811 
110812 /*
110813 ** Return true if the DISTINCT expression-list passed as the third argument
110814 ** is redundant.
110815 **
110816 ** A DISTINCT list is redundant if the database contains some subset of
110817 ** columns that are unique and non-null.
110818 */
110819 static int isDistinctRedundant(
110820   Parse *pParse,            /* Parsing context */
110821   SrcList *pTabList,        /* The FROM clause */
110822   WhereClause *pWC,         /* The WHERE clause */
110823   ExprList *pDistinct       /* The result set that needs to be DISTINCT */
110824 ){
110825   Table *pTab;
110826   Index *pIdx;
110827   int i;
110828   int iBase;
110829 
110830   /* If there is more than one table or sub-select in the FROM clause of
110831   ** this query, then it will not be possible to show that the DISTINCT
110832   ** clause is redundant. */
110833   if( pTabList->nSrc!=1 ) return 0;
110834   iBase = pTabList->a[0].iCursor;
110835   pTab = pTabList->a[0].pTab;
110836 
110837   /* If any of the expressions is an IPK column on table iBase, then return
110838   ** true. Note: The (p->iTable==iBase) part of this test may be false if the
110839   ** current SELECT is a correlated sub-query.
110840   */
110841   for(i=0; i<pDistinct->nExpr; i++){
110842     Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
110843     if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
110844   }
110845 
110846   /* Loop through all indices on the table, checking each to see if it makes
110847   ** the DISTINCT qualifier redundant. It does so if:
110848   **
110849   **   1. The index is itself UNIQUE, and
110850   **
110851   **   2. All of the columns in the index are either part of the pDistinct
110852   **      list, or else the WHERE clause contains a term of the form "col=X",
110853   **      where X is a constant value. The collation sequences of the
110854   **      comparison and select-list expressions must match those of the index.
110855   **
110856   **   3. All of those index columns for which the WHERE clause does not
110857   **      contain a "col=X" term are subject to a NOT NULL constraint.
110858   */
110859   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
110860     if( pIdx->onError==OE_None ) continue;
110861     for(i=0; i<pIdx->nKeyCol; i++){
110862       i16 iCol = pIdx->aiColumn[i];
110863       if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
110864         int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
110865         if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
110866           break;
110867         }
110868       }
110869     }
110870     if( i==pIdx->nKeyCol ){
110871       /* This index implies that the DISTINCT qualifier is redundant. */
110872       return 1;
110873     }
110874   }
110875 
110876   return 0;
110877 }
110878 
110879 
110880 /*
110881 ** Estimate the logarithm of the input value to base 2.
110882 */
110883 static LogEst estLog(LogEst N){
110884   LogEst x = sqlite3LogEst(N);
110885   return x>33 ? x - 33 : 0;
110886 }
110887 
110888 /*
110889 ** Two routines for printing the content of an sqlite3_index_info
110890 ** structure.  Used for testing and debugging only.  If neither
110891 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
110892 ** are no-ops.
110893 */
110894 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
110895 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
110896   int i;
110897   if( !sqlite3WhereTrace ) return;
110898   for(i=0; i<p->nConstraint; i++){
110899     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
110900        i,
110901        p->aConstraint[i].iColumn,
110902        p->aConstraint[i].iTermOffset,
110903        p->aConstraint[i].op,
110904        p->aConstraint[i].usable);
110905   }
110906   for(i=0; i<p->nOrderBy; i++){
110907     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
110908        i,
110909        p->aOrderBy[i].iColumn,
110910        p->aOrderBy[i].desc);
110911   }
110912 }
110913 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
110914   int i;
110915   if( !sqlite3WhereTrace ) return;
110916   for(i=0; i<p->nConstraint; i++){
110917     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
110918        i,
110919        p->aConstraintUsage[i].argvIndex,
110920        p->aConstraintUsage[i].omit);
110921   }
110922   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
110923   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
110924   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
110925   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
110926   sqlite3DebugPrintf("  estimatedRows=%lld\n", p->estimatedRows);
110927 }
110928 #else
110929 #define TRACE_IDX_INPUTS(A)
110930 #define TRACE_IDX_OUTPUTS(A)
110931 #endif
110932 
110933 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110934 /*
110935 ** Return TRUE if the WHERE clause term pTerm is of a form where it
110936 ** could be used with an index to access pSrc, assuming an appropriate
110937 ** index existed.
110938 */
110939 static int termCanDriveIndex(
110940   WhereTerm *pTerm,              /* WHERE clause term to check */
110941   struct SrcList_item *pSrc,     /* Table we are trying to access */
110942   Bitmask notReady               /* Tables in outer loops of the join */
110943 ){
110944   char aff;
110945   if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
110946   if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
110947   if( (pTerm->prereqRight & notReady)!=0 ) return 0;
110948   if( pTerm->u.leftColumn<0 ) return 0;
110949   aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
110950   if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
110951   return 1;
110952 }
110953 #endif
110954 
110955 
110956 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110957 /*
110958 ** Generate code to construct the Index object for an automatic index
110959 ** and to set up the WhereLevel object pLevel so that the code generator
110960 ** makes use of the automatic index.
110961 */
110962 static void constructAutomaticIndex(
110963   Parse *pParse,              /* The parsing context */
110964   WhereClause *pWC,           /* The WHERE clause */
110965   struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
110966   Bitmask notReady,           /* Mask of cursors that are not available */
110967   WhereLevel *pLevel          /* Write new index here */
110968 ){
110969   int nKeyCol;                /* Number of columns in the constructed index */
110970   WhereTerm *pTerm;           /* A single term of the WHERE clause */
110971   WhereTerm *pWCEnd;          /* End of pWC->a[] */
110972   Index *pIdx;                /* Object describing the transient index */
110973   Vdbe *v;                    /* Prepared statement under construction */
110974   int addrInit;               /* Address of the initialization bypass jump */
110975   Table *pTable;              /* The table being indexed */
110976   int addrTop;                /* Top of the index fill loop */
110977   int regRecord;              /* Register holding an index record */
110978   int n;                      /* Column counter */
110979   int i;                      /* Loop counter */
110980   int mxBitCol;               /* Maximum column in pSrc->colUsed */
110981   CollSeq *pColl;             /* Collating sequence to on a column */
110982   WhereLoop *pLoop;           /* The Loop object */
110983   char *zNotUsed;             /* Extra space on the end of pIdx */
110984   Bitmask idxCols;            /* Bitmap of columns used for indexing */
110985   Bitmask extraCols;          /* Bitmap of additional columns */
110986   u8 sentWarning = 0;         /* True if a warnning has been issued */
110987 
110988   /* Generate code to skip over the creation and initialization of the
110989   ** transient index on 2nd and subsequent iterations of the loop. */
110990   v = pParse->pVdbe;
110991   assert( v!=0 );
110992   addrInit = sqlite3CodeOnce(pParse); VdbeCoverage(v);
110993 
110994   /* Count the number of columns that will be added to the index
110995   ** and used to match WHERE clause constraints */
110996   nKeyCol = 0;
110997   pTable = pSrc->pTab;
110998   pWCEnd = &pWC->a[pWC->nTerm];
110999   pLoop = pLevel->pWLoop;
111000   idxCols = 0;
111001   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
111002     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
111003       int iCol = pTerm->u.leftColumn;
111004       Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
111005       testcase( iCol==BMS );
111006       testcase( iCol==BMS-1 );
111007       if( !sentWarning ){
111008         sqlite3_log(SQLITE_WARNING_AUTOINDEX,
111009             "automatic index on %s(%s)", pTable->zName,
111010             pTable->aCol[iCol].zName);
111011         sentWarning = 1;
111012       }
111013       if( (idxCols & cMask)==0 ){
111014         if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return;
111015         pLoop->aLTerm[nKeyCol++] = pTerm;
111016         idxCols |= cMask;
111017       }
111018     }
111019   }
111020   assert( nKeyCol>0 );
111021   pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
111022   pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
111023                      | WHERE_AUTO_INDEX;
111024 
111025   /* Count the number of additional columns needed to create a
111026   ** covering index.  A "covering index" is an index that contains all
111027   ** columns that are needed by the query.  With a covering index, the
111028   ** original table never needs to be accessed.  Automatic indices must
111029   ** be a covering index because the index will not be updated if the
111030   ** original table changes and the index and table cannot both be used
111031   ** if they go out of sync.
111032   */
111033   extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
111034   mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
111035   testcase( pTable->nCol==BMS-1 );
111036   testcase( pTable->nCol==BMS-2 );
111037   for(i=0; i<mxBitCol; i++){
111038     if( extraCols & MASKBIT(i) ) nKeyCol++;
111039   }
111040   if( pSrc->colUsed & MASKBIT(BMS-1) ){
111041     nKeyCol += pTable->nCol - BMS + 1;
111042   }
111043   pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
111044 
111045   /* Construct the Index object to describe this index */
111046   pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
111047   if( pIdx==0 ) return;
111048   pLoop->u.btree.pIndex = pIdx;
111049   pIdx->zName = "auto-index";
111050   pIdx->pTable = pTable;
111051   n = 0;
111052   idxCols = 0;
111053   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
111054     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
111055       int iCol = pTerm->u.leftColumn;
111056       Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
111057       testcase( iCol==BMS-1 );
111058       testcase( iCol==BMS );
111059       if( (idxCols & cMask)==0 ){
111060         Expr *pX = pTerm->pExpr;
111061         idxCols |= cMask;
111062         pIdx->aiColumn[n] = pTerm->u.leftColumn;
111063         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
111064         pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
111065         n++;
111066       }
111067     }
111068   }
111069   assert( (u32)n==pLoop->u.btree.nEq );
111070 
111071   /* Add additional columns needed to make the automatic index into
111072   ** a covering index */
111073   for(i=0; i<mxBitCol; i++){
111074     if( extraCols & MASKBIT(i) ){
111075       pIdx->aiColumn[n] = i;
111076       pIdx->azColl[n] = "BINARY";
111077       n++;
111078     }
111079   }
111080   if( pSrc->colUsed & MASKBIT(BMS-1) ){
111081     for(i=BMS-1; i<pTable->nCol; i++){
111082       pIdx->aiColumn[n] = i;
111083       pIdx->azColl[n] = "BINARY";
111084       n++;
111085     }
111086   }
111087   assert( n==nKeyCol );
111088   pIdx->aiColumn[n] = -1;
111089   pIdx->azColl[n] = "BINARY";
111090 
111091   /* Create the automatic index */
111092   assert( pLevel->iIdxCur>=0 );
111093   pLevel->iIdxCur = pParse->nTab++;
111094   sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
111095   sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
111096   VdbeComment((v, "for %s", pTable->zName));
111097 
111098   /* Fill the automatic index with content */
111099   addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); VdbeCoverage(v);
111100   regRecord = sqlite3GetTempReg(pParse);
111101   sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
111102   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
111103   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
111104   sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); VdbeCoverage(v);
111105   sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
111106   sqlite3VdbeJumpHere(v, addrTop);
111107   sqlite3ReleaseTempReg(pParse, regRecord);
111108 
111109   /* Jump here when skipping the initialization */
111110   sqlite3VdbeJumpHere(v, addrInit);
111111 }
111112 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
111113 
111114 #ifndef SQLITE_OMIT_VIRTUALTABLE
111115 /*
111116 ** Allocate and populate an sqlite3_index_info structure. It is the
111117 ** responsibility of the caller to eventually release the structure
111118 ** by passing the pointer returned by this function to sqlite3_free().
111119 */
111120 static sqlite3_index_info *allocateIndexInfo(
111121   Parse *pParse,
111122   WhereClause *pWC,
111123   struct SrcList_item *pSrc,
111124   ExprList *pOrderBy
111125 ){
111126   int i, j;
111127   int nTerm;
111128   struct sqlite3_index_constraint *pIdxCons;
111129   struct sqlite3_index_orderby *pIdxOrderBy;
111130   struct sqlite3_index_constraint_usage *pUsage;
111131   WhereTerm *pTerm;
111132   int nOrderBy;
111133   sqlite3_index_info *pIdxInfo;
111134 
111135   /* Count the number of possible WHERE clause constraints referring
111136   ** to this virtual table */
111137   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
111138     if( pTerm->leftCursor != pSrc->iCursor ) continue;
111139     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
111140     testcase( pTerm->eOperator & WO_IN );
111141     testcase( pTerm->eOperator & WO_ISNULL );
111142     testcase( pTerm->eOperator & WO_ALL );
111143     if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
111144     if( pTerm->wtFlags & TERM_VNULL ) continue;
111145     nTerm++;
111146   }
111147 
111148   /* If the ORDER BY clause contains only columns in the current
111149   ** virtual table then allocate space for the aOrderBy part of
111150   ** the sqlite3_index_info structure.
111151   */
111152   nOrderBy = 0;
111153   if( pOrderBy ){
111154     int n = pOrderBy->nExpr;
111155     for(i=0; i<n; i++){
111156       Expr *pExpr = pOrderBy->a[i].pExpr;
111157       if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
111158     }
111159     if( i==n){
111160       nOrderBy = n;
111161     }
111162   }
111163 
111164   /* Allocate the sqlite3_index_info structure
111165   */
111166   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
111167                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
111168                            + sizeof(*pIdxOrderBy)*nOrderBy );
111169   if( pIdxInfo==0 ){
111170     sqlite3ErrorMsg(pParse, "out of memory");
111171     return 0;
111172   }
111173 
111174   /* Initialize the structure.  The sqlite3_index_info structure contains
111175   ** many fields that are declared "const" to prevent xBestIndex from
111176   ** changing them.  We have to do some funky casting in order to
111177   ** initialize those fields.
111178   */
111179   pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
111180   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
111181   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
111182   *(int*)&pIdxInfo->nConstraint = nTerm;
111183   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
111184   *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
111185   *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
111186   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
111187                                                                    pUsage;
111188 
111189   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
111190     u8 op;
111191     if( pTerm->leftCursor != pSrc->iCursor ) continue;
111192     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
111193     testcase( pTerm->eOperator & WO_IN );
111194     testcase( pTerm->eOperator & WO_ISNULL );
111195     testcase( pTerm->eOperator & WO_ALL );
111196     if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
111197     if( pTerm->wtFlags & TERM_VNULL ) continue;
111198     pIdxCons[j].iColumn = pTerm->u.leftColumn;
111199     pIdxCons[j].iTermOffset = i;
111200     op = (u8)pTerm->eOperator & WO_ALL;
111201     if( op==WO_IN ) op = WO_EQ;
111202     pIdxCons[j].op = op;
111203     /* The direct assignment in the previous line is possible only because
111204     ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
111205     ** following asserts verify this fact. */
111206     assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
111207     assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
111208     assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
111209     assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
111210     assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
111211     assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
111212     assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
111213     j++;
111214   }
111215   for(i=0; i<nOrderBy; i++){
111216     Expr *pExpr = pOrderBy->a[i].pExpr;
111217     pIdxOrderBy[i].iColumn = pExpr->iColumn;
111218     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
111219   }
111220 
111221   return pIdxInfo;
111222 }
111223 
111224 /*
111225 ** The table object reference passed as the second argument to this function
111226 ** must represent a virtual table. This function invokes the xBestIndex()
111227 ** method of the virtual table with the sqlite3_index_info object that
111228 ** comes in as the 3rd argument to this function.
111229 **
111230 ** If an error occurs, pParse is populated with an error message and a
111231 ** non-zero value is returned. Otherwise, 0 is returned and the output
111232 ** part of the sqlite3_index_info structure is left populated.
111233 **
111234 ** Whether or not an error is returned, it is the responsibility of the
111235 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
111236 ** that this is required.
111237 */
111238 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
111239   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
111240   int i;
111241   int rc;
111242 
111243   TRACE_IDX_INPUTS(p);
111244   rc = pVtab->pModule->xBestIndex(pVtab, p);
111245   TRACE_IDX_OUTPUTS(p);
111246 
111247   if( rc!=SQLITE_OK ){
111248     if( rc==SQLITE_NOMEM ){
111249       pParse->db->mallocFailed = 1;
111250     }else if( !pVtab->zErrMsg ){
111251       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
111252     }else{
111253       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
111254     }
111255   }
111256   sqlite3_free(pVtab->zErrMsg);
111257   pVtab->zErrMsg = 0;
111258 
111259   for(i=0; i<p->nConstraint; i++){
111260     if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
111261       sqlite3ErrorMsg(pParse,
111262           "table %s: xBestIndex returned an invalid plan", pTab->zName);
111263     }
111264   }
111265 
111266   return pParse->nErr;
111267 }
111268 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
111269 
111270 
111271 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
111272 /*
111273 ** Estimate the location of a particular key among all keys in an
111274 ** index.  Store the results in aStat as follows:
111275 **
111276 **    aStat[0]      Est. number of rows less than pVal
111277 **    aStat[1]      Est. number of rows equal to pVal
111278 **
111279 ** Return SQLITE_OK on success.
111280 */
111281 static void whereKeyStats(
111282   Parse *pParse,              /* Database connection */
111283   Index *pIdx,                /* Index to consider domain of */
111284   UnpackedRecord *pRec,       /* Vector of values to consider */
111285   int roundUp,                /* Round up if true.  Round down if false */
111286   tRowcnt *aStat              /* OUT: stats written here */
111287 ){
111288   IndexSample *aSample = pIdx->aSample;
111289   int iCol;                   /* Index of required stats in anEq[] etc. */
111290   int iMin = 0;               /* Smallest sample not yet tested */
111291   int i = pIdx->nSample;      /* Smallest sample larger than or equal to pRec */
111292   int iTest;                  /* Next sample to test */
111293   int res;                    /* Result of comparison operation */
111294 
111295 #ifndef SQLITE_DEBUG
111296   UNUSED_PARAMETER( pParse );
111297 #endif
111298   assert( pRec!=0 );
111299   iCol = pRec->nField - 1;
111300   assert( pIdx->nSample>0 );
111301   assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
111302   do{
111303     iTest = (iMin+i)/2;
111304     res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec, 0);
111305     if( res<0 ){
111306       iMin = iTest+1;
111307     }else{
111308       i = iTest;
111309     }
111310   }while( res && iMin<i );
111311 
111312 #ifdef SQLITE_DEBUG
111313   /* The following assert statements check that the binary search code
111314   ** above found the right answer. This block serves no purpose other
111315   ** than to invoke the asserts.  */
111316   if( res==0 ){
111317     /* If (res==0) is true, then sample $i must be equal to pRec */
111318     assert( i<pIdx->nSample );
111319     assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)
111320          || pParse->db->mallocFailed );
111321   }else{
111322     /* Otherwise, pRec must be smaller than sample $i and larger than
111323     ** sample ($i-1).  */
111324     assert( i==pIdx->nSample
111325          || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec, 0)>0
111326          || pParse->db->mallocFailed );
111327     assert( i==0
111328          || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec, 0)<0
111329          || pParse->db->mallocFailed );
111330   }
111331 #endif /* ifdef SQLITE_DEBUG */
111332 
111333   /* At this point, aSample[i] is the first sample that is greater than
111334   ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
111335   ** than pVal.  If aSample[i]==pVal, then res==0.
111336   */
111337   if( res==0 ){
111338     aStat[0] = aSample[i].anLt[iCol];
111339     aStat[1] = aSample[i].anEq[iCol];
111340   }else{
111341     tRowcnt iLower, iUpper, iGap;
111342     if( i==0 ){
111343       iLower = 0;
111344       iUpper = aSample[0].anLt[iCol];
111345     }else{
111346       iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
111347       iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
111348     }
111349     aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
111350     if( iLower>=iUpper ){
111351       iGap = 0;
111352     }else{
111353       iGap = iUpper - iLower;
111354     }
111355     if( roundUp ){
111356       iGap = (iGap*2)/3;
111357     }else{
111358       iGap = iGap/3;
111359     }
111360     aStat[0] = iLower + iGap;
111361   }
111362 }
111363 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
111364 
111365 /*
111366 ** This function is used to estimate the number of rows that will be visited
111367 ** by scanning an index for a range of values. The range may have an upper
111368 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
111369 ** and lower bounds are represented by pLower and pUpper respectively. For
111370 ** example, assuming that index p is on t1(a):
111371 **
111372 **   ... FROM t1 WHERE a > ? AND a < ? ...
111373 **                    |_____|   |_____|
111374 **                       |         |
111375 **                     pLower    pUpper
111376 **
111377 ** If either of the upper or lower bound is not present, then NULL is passed in
111378 ** place of the corresponding WhereTerm.
111379 **
111380 ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
111381 ** column subject to the range constraint. Or, equivalently, the number of
111382 ** equality constraints optimized by the proposed index scan. For example,
111383 ** assuming index p is on t1(a, b), and the SQL query is:
111384 **
111385 **   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
111386 **
111387 ** then nEq is set to 1 (as the range restricted column, b, is the second
111388 ** left-most column of the index). Or, if the query is:
111389 **
111390 **   ... FROM t1 WHERE a > ? AND a < ? ...
111391 **
111392 ** then nEq is set to 0.
111393 **
111394 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
111395 ** number of rows that the index scan is expected to visit without
111396 ** considering the range constraints. If nEq is 0, this is the number of
111397 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
111398 ** to account for the range contraints pLower and pUpper.
111399 **
111400 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
111401 ** used, each range inequality reduces the search space by a factor of 4.
111402 ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
111403 ** rows visited by a factor of 16.
111404 */
111405 static int whereRangeScanEst(
111406   Parse *pParse,       /* Parsing & code generating context */
111407   WhereLoopBuilder *pBuilder,
111408   WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
111409   WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
111410   WhereLoop *pLoop     /* Modify the .nOut and maybe .rRun fields */
111411 ){
111412   int rc = SQLITE_OK;
111413   int nOut = pLoop->nOut;
111414   LogEst nNew;
111415 
111416 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
111417   Index *p = pLoop->u.btree.pIndex;
111418   int nEq = pLoop->u.btree.nEq;
111419 
111420   if( p->nSample>0
111421    && nEq==pBuilder->nRecValid
111422    && nEq<p->nSampleCol
111423    && OptimizationEnabled(pParse->db, SQLITE_Stat3)
111424   ){
111425     UnpackedRecord *pRec = pBuilder->pRec;
111426     tRowcnt a[2];
111427     u8 aff;
111428 
111429     /* Variable iLower will be set to the estimate of the number of rows in
111430     ** the index that are less than the lower bound of the range query. The
111431     ** lower bound being the concatenation of $P and $L, where $P is the
111432     ** key-prefix formed by the nEq values matched against the nEq left-most
111433     ** columns of the index, and $L is the value in pLower.
111434     **
111435     ** Or, if pLower is NULL or $L cannot be extracted from it (because it
111436     ** is not a simple variable or literal value), the lower bound of the
111437     ** range is $P. Due to a quirk in the way whereKeyStats() works, even
111438     ** if $L is available, whereKeyStats() is called for both ($P) and
111439     ** ($P:$L) and the larger of the two returned values used.
111440     **
111441     ** Similarly, iUpper is to be set to the estimate of the number of rows
111442     ** less than the upper bound of the range query. Where the upper bound
111443     ** is either ($P) or ($P:$U). Again, even if $U is available, both values
111444     ** of iUpper are requested of whereKeyStats() and the smaller used.
111445     */
111446     tRowcnt iLower;
111447     tRowcnt iUpper;
111448 
111449     if( nEq==p->nKeyCol ){
111450       aff = SQLITE_AFF_INTEGER;
111451     }else{
111452       aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
111453     }
111454     /* Determine iLower and iUpper using ($P) only. */
111455     if( nEq==0 ){
111456       iLower = 0;
111457       iUpper = p->aiRowEst[0];
111458     }else{
111459       /* Note: this call could be optimized away - since the same values must
111460       ** have been requested when testing key $P in whereEqualScanEst().  */
111461       whereKeyStats(pParse, p, pRec, 0, a);
111462       iLower = a[0];
111463       iUpper = a[0] + a[1];
111464     }
111465 
111466     /* If possible, improve on the iLower estimate using ($P:$L). */
111467     if( pLower ){
111468       int bOk;                    /* True if value is extracted from pExpr */
111469       Expr *pExpr = pLower->pExpr->pRight;
111470       assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
111471       rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
111472       if( rc==SQLITE_OK && bOk ){
111473         tRowcnt iNew;
111474         whereKeyStats(pParse, p, pRec, 0, a);
111475         iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
111476         if( iNew>iLower ) iLower = iNew;
111477         nOut--;
111478       }
111479     }
111480 
111481     /* If possible, improve on the iUpper estimate using ($P:$U). */
111482     if( pUpper ){
111483       int bOk;                    /* True if value is extracted from pExpr */
111484       Expr *pExpr = pUpper->pExpr->pRight;
111485       assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
111486       rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
111487       if( rc==SQLITE_OK && bOk ){
111488         tRowcnt iNew;
111489         whereKeyStats(pParse, p, pRec, 1, a);
111490         iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
111491         if( iNew<iUpper ) iUpper = iNew;
111492         nOut--;
111493       }
111494     }
111495 
111496     pBuilder->pRec = pRec;
111497     if( rc==SQLITE_OK ){
111498       if( iUpper>iLower ){
111499         nNew = sqlite3LogEst(iUpper - iLower);
111500       }else{
111501         nNew = 10;        assert( 10==sqlite3LogEst(2) );
111502       }
111503       if( nNew<nOut ){
111504         nOut = nNew;
111505       }
111506       pLoop->nOut = (LogEst)nOut;
111507       WHERETRACE(0x10, ("range scan regions: %u..%u  est=%d\n",
111508                          (u32)iLower, (u32)iUpper, nOut));
111509       return SQLITE_OK;
111510     }
111511   }
111512 #else
111513   UNUSED_PARAMETER(pParse);
111514   UNUSED_PARAMETER(pBuilder);
111515 #endif
111516   assert( pLower || pUpper );
111517   /* TUNING:  Each inequality constraint reduces the search space 4-fold.
111518   ** A BETWEEN operator, therefore, reduces the search space 16-fold */
111519   nNew = nOut;
111520   if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
111521     nNew -= 20;        assert( 20==sqlite3LogEst(4) );
111522     nOut--;
111523   }
111524   if( pUpper ){
111525     nNew -= 20;        assert( 20==sqlite3LogEst(4) );
111526     nOut--;
111527   }
111528   if( nNew<10 ) nNew = 10;
111529   if( nNew<nOut ) nOut = nNew;
111530   pLoop->nOut = (LogEst)nOut;
111531   return rc;
111532 }
111533 
111534 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
111535 /*
111536 ** Estimate the number of rows that will be returned based on
111537 ** an equality constraint x=VALUE and where that VALUE occurs in
111538 ** the histogram data.  This only works when x is the left-most
111539 ** column of an index and sqlite_stat3 histogram data is available
111540 ** for that index.  When pExpr==NULL that means the constraint is
111541 ** "x IS NULL" instead of "x=VALUE".
111542 **
111543 ** Write the estimated row count into *pnRow and return SQLITE_OK.
111544 ** If unable to make an estimate, leave *pnRow unchanged and return
111545 ** non-zero.
111546 **
111547 ** This routine can fail if it is unable to load a collating sequence
111548 ** required for string comparison, or if unable to allocate memory
111549 ** for a UTF conversion required for comparison.  The error is stored
111550 ** in the pParse structure.
111551 */
111552 static int whereEqualScanEst(
111553   Parse *pParse,       /* Parsing & code generating context */
111554   WhereLoopBuilder *pBuilder,
111555   Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
111556   tRowcnt *pnRow       /* Write the revised row estimate here */
111557 ){
111558   Index *p = pBuilder->pNew->u.btree.pIndex;
111559   int nEq = pBuilder->pNew->u.btree.nEq;
111560   UnpackedRecord *pRec = pBuilder->pRec;
111561   u8 aff;                   /* Column affinity */
111562   int rc;                   /* Subfunction return code */
111563   tRowcnt a[2];             /* Statistics */
111564   int bOk;
111565 
111566   assert( nEq>=1 );
111567   assert( nEq<=(p->nKeyCol+1) );
111568   assert( p->aSample!=0 );
111569   assert( p->nSample>0 );
111570   assert( pBuilder->nRecValid<nEq );
111571 
111572   /* If values are not available for all fields of the index to the left
111573   ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
111574   if( pBuilder->nRecValid<(nEq-1) ){
111575     return SQLITE_NOTFOUND;
111576   }
111577 
111578   /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
111579   ** below would return the same value.  */
111580   if( nEq>p->nKeyCol ){
111581     *pnRow = 1;
111582     return SQLITE_OK;
111583   }
111584 
111585   aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
111586   rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
111587   pBuilder->pRec = pRec;
111588   if( rc!=SQLITE_OK ) return rc;
111589   if( bOk==0 ) return SQLITE_NOTFOUND;
111590   pBuilder->nRecValid = nEq;
111591 
111592   whereKeyStats(pParse, p, pRec, 0, a);
111593   WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1]));
111594   *pnRow = a[1];
111595 
111596   return rc;
111597 }
111598 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
111599 
111600 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
111601 /*
111602 ** Estimate the number of rows that will be returned based on
111603 ** an IN constraint where the right-hand side of the IN operator
111604 ** is a list of values.  Example:
111605 **
111606 **        WHERE x IN (1,2,3,4)
111607 **
111608 ** Write the estimated row count into *pnRow and return SQLITE_OK.
111609 ** If unable to make an estimate, leave *pnRow unchanged and return
111610 ** non-zero.
111611 **
111612 ** This routine can fail if it is unable to load a collating sequence
111613 ** required for string comparison, or if unable to allocate memory
111614 ** for a UTF conversion required for comparison.  The error is stored
111615 ** in the pParse structure.
111616 */
111617 static int whereInScanEst(
111618   Parse *pParse,       /* Parsing & code generating context */
111619   WhereLoopBuilder *pBuilder,
111620   ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
111621   tRowcnt *pnRow       /* Write the revised row estimate here */
111622 ){
111623   Index *p = pBuilder->pNew->u.btree.pIndex;
111624   int nRecValid = pBuilder->nRecValid;
111625   int rc = SQLITE_OK;     /* Subfunction return code */
111626   tRowcnt nEst;           /* Number of rows for a single term */
111627   tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
111628   int i;                  /* Loop counter */
111629 
111630   assert( p->aSample!=0 );
111631   for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
111632     nEst = p->aiRowEst[0];
111633     rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
111634     nRowEst += nEst;
111635     pBuilder->nRecValid = nRecValid;
111636   }
111637 
111638   if( rc==SQLITE_OK ){
111639     if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
111640     *pnRow = nRowEst;
111641     WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
111642   }
111643   assert( pBuilder->nRecValid==nRecValid );
111644   return rc;
111645 }
111646 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
111647 
111648 /*
111649 ** Disable a term in the WHERE clause.  Except, do not disable the term
111650 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
111651 ** or USING clause of that join.
111652 **
111653 ** Consider the term t2.z='ok' in the following queries:
111654 **
111655 **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
111656 **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
111657 **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
111658 **
111659 ** The t2.z='ok' is disabled in the in (2) because it originates
111660 ** in the ON clause.  The term is disabled in (3) because it is not part
111661 ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
111662 **
111663 ** Disabling a term causes that term to not be tested in the inner loop
111664 ** of the join.  Disabling is an optimization.  When terms are satisfied
111665 ** by indices, we disable them to prevent redundant tests in the inner
111666 ** loop.  We would get the correct results if nothing were ever disabled,
111667 ** but joins might run a little slower.  The trick is to disable as much
111668 ** as we can without disabling too much.  If we disabled in (1), we'd get
111669 ** the wrong answer.  See ticket #813.
111670 */
111671 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
111672   if( pTerm
111673       && (pTerm->wtFlags & TERM_CODED)==0
111674       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
111675       && (pLevel->notReady & pTerm->prereqAll)==0
111676   ){
111677     pTerm->wtFlags |= TERM_CODED;
111678     if( pTerm->iParent>=0 ){
111679       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
111680       if( (--pOther->nChild)==0 ){
111681         disableTerm(pLevel, pOther);
111682       }
111683     }
111684   }
111685 }
111686 
111687 /*
111688 ** Code an OP_Affinity opcode to apply the column affinity string zAff
111689 ** to the n registers starting at base.
111690 **
111691 ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
111692 ** beginning and end of zAff are ignored.  If all entries in zAff are
111693 ** SQLITE_AFF_NONE, then no code gets generated.
111694 **
111695 ** This routine makes its own copy of zAff so that the caller is free
111696 ** to modify zAff after this routine returns.
111697 */
111698 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
111699   Vdbe *v = pParse->pVdbe;
111700   if( zAff==0 ){
111701     assert( pParse->db->mallocFailed );
111702     return;
111703   }
111704   assert( v!=0 );
111705 
111706   /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
111707   ** and end of the affinity string.
111708   */
111709   while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
111710     n--;
111711     base++;
111712     zAff++;
111713   }
111714   while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
111715     n--;
111716   }
111717 
111718   /* Code the OP_Affinity opcode if there is anything left to do. */
111719   if( n>0 ){
111720     sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
111721     sqlite3VdbeChangeP4(v, -1, zAff, n);
111722     sqlite3ExprCacheAffinityChange(pParse, base, n);
111723   }
111724 }
111725 
111726 
111727 /*
111728 ** Generate code for a single equality term of the WHERE clause.  An equality
111729 ** term can be either X=expr or X IN (...).   pTerm is the term to be
111730 ** coded.
111731 **
111732 ** The current value for the constraint is left in register iReg.
111733 **
111734 ** For a constraint of the form X=expr, the expression is evaluated and its
111735 ** result is left on the stack.  For constraints of the form X IN (...)
111736 ** this routine sets up a loop that will iterate over all values of X.
111737 */
111738 static int codeEqualityTerm(
111739   Parse *pParse,      /* The parsing context */
111740   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
111741   WhereLevel *pLevel, /* The level of the FROM clause we are working on */
111742   int iEq,            /* Index of the equality term within this level */
111743   int bRev,           /* True for reverse-order IN operations */
111744   int iTarget         /* Attempt to leave results in this register */
111745 ){
111746   Expr *pX = pTerm->pExpr;
111747   Vdbe *v = pParse->pVdbe;
111748   int iReg;                  /* Register holding results */
111749 
111750   assert( iTarget>0 );
111751   if( pX->op==TK_EQ ){
111752     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
111753   }else if( pX->op==TK_ISNULL ){
111754     iReg = iTarget;
111755     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
111756 #ifndef SQLITE_OMIT_SUBQUERY
111757   }else{
111758     int eType;
111759     int iTab;
111760     struct InLoop *pIn;
111761     WhereLoop *pLoop = pLevel->pWLoop;
111762 
111763     if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
111764       && pLoop->u.btree.pIndex!=0
111765       && pLoop->u.btree.pIndex->aSortOrder[iEq]
111766     ){
111767       testcase( iEq==0 );
111768       testcase( bRev );
111769       bRev = !bRev;
111770     }
111771     assert( pX->op==TK_IN );
111772     iReg = iTarget;
111773     eType = sqlite3FindInIndex(pParse, pX, 0);
111774     if( eType==IN_INDEX_INDEX_DESC ){
111775       testcase( bRev );
111776       bRev = !bRev;
111777     }
111778     iTab = pX->iTable;
111779     sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
111780     VdbeCoverageIf(v, bRev);
111781     VdbeCoverageIf(v, !bRev);
111782     assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
111783     pLoop->wsFlags |= WHERE_IN_ABLE;
111784     if( pLevel->u.in.nIn==0 ){
111785       pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
111786     }
111787     pLevel->u.in.nIn++;
111788     pLevel->u.in.aInLoop =
111789        sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
111790                               sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
111791     pIn = pLevel->u.in.aInLoop;
111792     if( pIn ){
111793       pIn += pLevel->u.in.nIn - 1;
111794       pIn->iCur = iTab;
111795       if( eType==IN_INDEX_ROWID ){
111796         pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
111797       }else{
111798         pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
111799       }
111800       pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
111801       sqlite3VdbeAddOp1(v, OP_IsNull, iReg); VdbeCoverage(v);
111802     }else{
111803       pLevel->u.in.nIn = 0;
111804     }
111805 #endif
111806   }
111807   disableTerm(pLevel, pTerm);
111808   return iReg;
111809 }
111810 
111811 /*
111812 ** Generate code that will evaluate all == and IN constraints for an
111813 ** index scan.
111814 **
111815 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
111816 ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
111817 ** The index has as many as three equality constraints, but in this
111818 ** example, the third "c" value is an inequality.  So only two
111819 ** constraints are coded.  This routine will generate code to evaluate
111820 ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
111821 ** in consecutive registers and the index of the first register is returned.
111822 **
111823 ** In the example above nEq==2.  But this subroutine works for any value
111824 ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
111825 ** The only thing it does is allocate the pLevel->iMem memory cell and
111826 ** compute the affinity string.
111827 **
111828 ** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
111829 ** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
111830 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
111831 ** occurs after the nEq quality constraints.
111832 **
111833 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
111834 ** the index of the first memory cell in that range. The code that
111835 ** calls this routine will use that memory range to store keys for
111836 ** start and termination conditions of the loop.
111837 ** key value of the loop.  If one or more IN operators appear, then
111838 ** this routine allocates an additional nEq memory cells for internal
111839 ** use.
111840 **
111841 ** Before returning, *pzAff is set to point to a buffer containing a
111842 ** copy of the column affinity string of the index allocated using
111843 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
111844 ** with equality constraints that use NONE affinity are set to
111845 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
111846 **
111847 **   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
111848 **   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
111849 **
111850 ** In the example above, the index on t1(a) has TEXT affinity. But since
111851 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
111852 ** no conversion should be attempted before using a t2.b value as part of
111853 ** a key to search the index. Hence the first byte in the returned affinity
111854 ** string in this example would be set to SQLITE_AFF_NONE.
111855 */
111856 static int codeAllEqualityTerms(
111857   Parse *pParse,        /* Parsing context */
111858   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
111859   int bRev,             /* Reverse the order of IN operators */
111860   int nExtraReg,        /* Number of extra registers to allocate */
111861   char **pzAff          /* OUT: Set to point to affinity string */
111862 ){
111863   u16 nEq;                      /* The number of == or IN constraints to code */
111864   u16 nSkip;                    /* Number of left-most columns to skip */
111865   Vdbe *v = pParse->pVdbe;      /* The vm under construction */
111866   Index *pIdx;                  /* The index being used for this loop */
111867   WhereTerm *pTerm;             /* A single constraint term */
111868   WhereLoop *pLoop;             /* The WhereLoop object */
111869   int j;                        /* Loop counter */
111870   int regBase;                  /* Base register */
111871   int nReg;                     /* Number of registers to allocate */
111872   char *zAff;                   /* Affinity string to return */
111873 
111874   /* This module is only called on query plans that use an index. */
111875   pLoop = pLevel->pWLoop;
111876   assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
111877   nEq = pLoop->u.btree.nEq;
111878   nSkip = pLoop->u.btree.nSkip;
111879   pIdx = pLoop->u.btree.pIndex;
111880   assert( pIdx!=0 );
111881 
111882   /* Figure out how many memory cells we will need then allocate them.
111883   */
111884   regBase = pParse->nMem + 1;
111885   nReg = pLoop->u.btree.nEq + nExtraReg;
111886   pParse->nMem += nReg;
111887 
111888   zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
111889   if( !zAff ){
111890     pParse->db->mallocFailed = 1;
111891   }
111892 
111893   if( nSkip ){
111894     int iIdxCur = pLevel->iIdxCur;
111895     sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
111896     VdbeCoverageIf(v, bRev==0);
111897     VdbeCoverageIf(v, bRev!=0);
111898     VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
111899     j = sqlite3VdbeAddOp0(v, OP_Goto);
111900     pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLT:OP_SeekGT),
111901                             iIdxCur, 0, regBase, nSkip);
111902     VdbeCoverageIf(v, bRev==0);
111903     VdbeCoverageIf(v, bRev!=0);
111904     sqlite3VdbeJumpHere(v, j);
111905     for(j=0; j<nSkip; j++){
111906       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
111907       assert( pIdx->aiColumn[j]>=0 );
111908       VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
111909     }
111910   }
111911 
111912   /* Evaluate the equality constraints
111913   */
111914   assert( zAff==0 || (int)strlen(zAff)>=nEq );
111915   for(j=nSkip; j<nEq; j++){
111916     int r1;
111917     pTerm = pLoop->aLTerm[j];
111918     assert( pTerm!=0 );
111919     /* The following testcase is true for indices with redundant columns.
111920     ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
111921     testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
111922     testcase( pTerm->wtFlags & TERM_VIRTUAL );
111923     r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
111924     if( r1!=regBase+j ){
111925       if( nReg==1 ){
111926         sqlite3ReleaseTempReg(pParse, regBase);
111927         regBase = r1;
111928       }else{
111929         sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
111930       }
111931     }
111932     testcase( pTerm->eOperator & WO_ISNULL );
111933     testcase( pTerm->eOperator & WO_IN );
111934     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
111935       Expr *pRight = pTerm->pExpr->pRight;
111936       if( sqlite3ExprCanBeNull(pRight) ){
111937         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk);
111938         VdbeCoverage(v);
111939       }
111940       if( zAff ){
111941         if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
111942           zAff[j] = SQLITE_AFF_NONE;
111943         }
111944         if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
111945           zAff[j] = SQLITE_AFF_NONE;
111946         }
111947       }
111948     }
111949   }
111950   *pzAff = zAff;
111951   return regBase;
111952 }
111953 
111954 #ifndef SQLITE_OMIT_EXPLAIN
111955 /*
111956 ** This routine is a helper for explainIndexRange() below
111957 **
111958 ** pStr holds the text of an expression that we are building up one term
111959 ** at a time.  This routine adds a new term to the end of the expression.
111960 ** Terms are separated by AND so add the "AND" text for second and subsequent
111961 ** terms only.
111962 */
111963 static void explainAppendTerm(
111964   StrAccum *pStr,             /* The text expression being built */
111965   int iTerm,                  /* Index of this term.  First is zero */
111966   const char *zColumn,        /* Name of the column */
111967   const char *zOp             /* Name of the operator */
111968 ){
111969   if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
111970   sqlite3StrAccumAppendAll(pStr, zColumn);
111971   sqlite3StrAccumAppend(pStr, zOp, 1);
111972   sqlite3StrAccumAppend(pStr, "?", 1);
111973 }
111974 
111975 /*
111976 ** Argument pLevel describes a strategy for scanning table pTab. This
111977 ** function returns a pointer to a string buffer containing a description
111978 ** of the subset of table rows scanned by the strategy in the form of an
111979 ** SQL expression. Or, if all rows are scanned, NULL is returned.
111980 **
111981 ** For example, if the query:
111982 **
111983 **   SELECT * FROM t1 WHERE a=1 AND b>2;
111984 **
111985 ** is run and there is an index on (a, b), then this function returns a
111986 ** string similar to:
111987 **
111988 **   "a=? AND b>?"
111989 **
111990 ** The returned pointer points to memory obtained from sqlite3DbMalloc().
111991 ** It is the responsibility of the caller to free the buffer when it is
111992 ** no longer required.
111993 */
111994 static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){
111995   Index *pIndex = pLoop->u.btree.pIndex;
111996   u16 nEq = pLoop->u.btree.nEq;
111997   u16 nSkip = pLoop->u.btree.nSkip;
111998   int i, j;
111999   Column *aCol = pTab->aCol;
112000   i16 *aiColumn = pIndex->aiColumn;
112001   StrAccum txt;
112002 
112003   if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
112004     return 0;
112005   }
112006   sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
112007   txt.db = db;
112008   sqlite3StrAccumAppend(&txt, " (", 2);
112009   for(i=0; i<nEq; i++){
112010     char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName;
112011     if( i>=nSkip ){
112012       explainAppendTerm(&txt, i, z, "=");
112013     }else{
112014       if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
112015       sqlite3StrAccumAppend(&txt, "ANY(", 4);
112016       sqlite3StrAccumAppendAll(&txt, z);
112017       sqlite3StrAccumAppend(&txt, ")", 1);
112018     }
112019   }
112020 
112021   j = i;
112022   if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
112023     char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
112024     explainAppendTerm(&txt, i++, z, ">");
112025   }
112026   if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
112027     char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
112028     explainAppendTerm(&txt, i, z, "<");
112029   }
112030   sqlite3StrAccumAppend(&txt, ")", 1);
112031   return sqlite3StrAccumFinish(&txt);
112032 }
112033 
112034 /*
112035 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
112036 ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
112037 ** record is added to the output to describe the table scan strategy in
112038 ** pLevel.
112039 */
112040 static void explainOneScan(
112041   Parse *pParse,                  /* Parse context */
112042   SrcList *pTabList,              /* Table list this loop refers to */
112043   WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
112044   int iLevel,                     /* Value for "level" column of output */
112045   int iFrom,                      /* Value for "from" column of output */
112046   u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
112047 ){
112048 #ifndef SQLITE_DEBUG
112049   if( pParse->explain==2 )
112050 #endif
112051   {
112052     struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
112053     Vdbe *v = pParse->pVdbe;      /* VM being constructed */
112054     sqlite3 *db = pParse->db;     /* Database handle */
112055     char *zMsg;                   /* Text to add to EQP output */
112056     int iId = pParse->iSelectId;  /* Select id (left-most output column) */
112057     int isSearch;                 /* True for a SEARCH. False for SCAN. */
112058     WhereLoop *pLoop;             /* The controlling WhereLoop object */
112059     u32 flags;                    /* Flags that describe this loop */
112060 
112061     pLoop = pLevel->pWLoop;
112062     flags = pLoop->wsFlags;
112063     if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
112064 
112065     isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
112066             || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
112067             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
112068 
112069     zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
112070     if( pItem->pSelect ){
112071       zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
112072     }else{
112073       zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
112074     }
112075 
112076     if( pItem->zAlias ){
112077       zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
112078     }
112079     if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
112080      && ALWAYS(pLoop->u.btree.pIndex!=0)
112081     ){
112082       char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
112083       zMsg = sqlite3MAppendf(db, zMsg,
112084                ((flags & WHERE_AUTO_INDEX) ?
112085                    "%s USING AUTOMATIC %sINDEX%.0s%s" :
112086                    "%s USING %sINDEX %s%s"),
112087                zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""),
112088                pLoop->u.btree.pIndex->zName, zWhere);
112089       sqlite3DbFree(db, zWhere);
112090     }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
112091       zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
112092 
112093       if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
112094         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
112095       }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
112096         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
112097       }else if( flags&WHERE_BTM_LIMIT ){
112098         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
112099       }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){
112100         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
112101       }
112102     }
112103 #ifndef SQLITE_OMIT_VIRTUALTABLE
112104     else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
112105       zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
112106                   pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
112107     }
112108 #endif
112109     zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg);
112110     sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
112111   }
112112 }
112113 #else
112114 # define explainOneScan(u,v,w,x,y,z)
112115 #endif /* SQLITE_OMIT_EXPLAIN */
112116 
112117 
112118 /*
112119 ** Generate code for the start of the iLevel-th loop in the WHERE clause
112120 ** implementation described by pWInfo.
112121 */
112122 static Bitmask codeOneLoopStart(
112123   WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
112124   int iLevel,          /* Which level of pWInfo->a[] should be coded */
112125   Bitmask notReady     /* Which tables are currently available */
112126 ){
112127   int j, k;            /* Loop counters */
112128   int iCur;            /* The VDBE cursor for the table */
112129   int addrNxt;         /* Where to jump to continue with the next IN case */
112130   int omitTable;       /* True if we use the index only */
112131   int bRev;            /* True if we need to scan in reverse order */
112132   WhereLevel *pLevel;  /* The where level to be coded */
112133   WhereLoop *pLoop;    /* The WhereLoop object being coded */
112134   WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
112135   WhereTerm *pTerm;               /* A WHERE clause term */
112136   Parse *pParse;                  /* Parsing context */
112137   sqlite3 *db;                    /* Database connection */
112138   Vdbe *v;                        /* The prepared stmt under constructions */
112139   struct SrcList_item *pTabItem;  /* FROM clause term being coded */
112140   int addrBrk;                    /* Jump here to break out of the loop */
112141   int addrCont;                   /* Jump here to continue with next cycle */
112142   int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
112143   int iReleaseReg = 0;      /* Temp register to free before returning */
112144 
112145   pParse = pWInfo->pParse;
112146   v = pParse->pVdbe;
112147   pWC = &pWInfo->sWC;
112148   db = pParse->db;
112149   pLevel = &pWInfo->a[iLevel];
112150   pLoop = pLevel->pWLoop;
112151   pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
112152   iCur = pTabItem->iCursor;
112153   pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
112154   bRev = (pWInfo->revMask>>iLevel)&1;
112155   omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
112156            && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
112157   VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
112158 
112159   /* Create labels for the "break" and "continue" instructions
112160   ** for the current loop.  Jump to addrBrk to break out of a loop.
112161   ** Jump to cont to go immediately to the next iteration of the
112162   ** loop.
112163   **
112164   ** When there is an IN operator, we also have a "addrNxt" label that
112165   ** means to continue with the next IN value combination.  When
112166   ** there are no IN operators in the constraints, the "addrNxt" label
112167   ** is the same as "addrBrk".
112168   */
112169   addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
112170   addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
112171 
112172   /* If this is the right table of a LEFT OUTER JOIN, allocate and
112173   ** initialize a memory cell that records if this table matches any
112174   ** row of the left table of the join.
112175   */
112176   if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
112177     pLevel->iLeftJoin = ++pParse->nMem;
112178     sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
112179     VdbeComment((v, "init LEFT JOIN no-match flag"));
112180   }
112181 
112182   /* Special case of a FROM clause subquery implemented as a co-routine */
112183   if( pTabItem->viaCoroutine ){
112184     int regYield = pTabItem->regReturn;
112185     sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, pTabItem->addrFillSub);
112186     pLevel->p2 =  sqlite3VdbeAddOp2(v, OP_Yield, regYield, addrBrk);
112187     VdbeCoverage(v);
112188     VdbeComment((v, "next row of \"%s\"", pTabItem->pTab->zName));
112189     pLevel->op = OP_Goto;
112190   }else
112191 
112192 #ifndef SQLITE_OMIT_VIRTUALTABLE
112193   if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
112194     /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
112195     **          to access the data.
112196     */
112197     int iReg;   /* P3 Value for OP_VFilter */
112198     int addrNotFound;
112199     int nConstraint = pLoop->nLTerm;
112200 
112201     sqlite3ExprCachePush(pParse);
112202     iReg = sqlite3GetTempRange(pParse, nConstraint+2);
112203     addrNotFound = pLevel->addrBrk;
112204     for(j=0; j<nConstraint; j++){
112205       int iTarget = iReg+j+2;
112206       pTerm = pLoop->aLTerm[j];
112207       if( pTerm==0 ) continue;
112208       if( pTerm->eOperator & WO_IN ){
112209         codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
112210         addrNotFound = pLevel->addrNxt;
112211       }else{
112212         sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
112213       }
112214     }
112215     sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
112216     sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
112217     sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
112218                       pLoop->u.vtab.idxStr,
112219                       pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
112220     VdbeCoverage(v);
112221     pLoop->u.vtab.needFree = 0;
112222     for(j=0; j<nConstraint && j<16; j++){
112223       if( (pLoop->u.vtab.omitMask>>j)&1 ){
112224         disableTerm(pLevel, pLoop->aLTerm[j]);
112225       }
112226     }
112227     pLevel->op = OP_VNext;
112228     pLevel->p1 = iCur;
112229     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
112230     sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
112231     sqlite3ExprCachePop(pParse, 1);
112232   }else
112233 #endif /* SQLITE_OMIT_VIRTUALTABLE */
112234 
112235   if( (pLoop->wsFlags & WHERE_IPK)!=0
112236    && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
112237   ){
112238     /* Case 2:  We can directly reference a single row using an
112239     **          equality comparison against the ROWID field.  Or
112240     **          we reference multiple rows using a "rowid IN (...)"
112241     **          construct.
112242     */
112243     assert( pLoop->u.btree.nEq==1 );
112244     pTerm = pLoop->aLTerm[0];
112245     assert( pTerm!=0 );
112246     assert( pTerm->pExpr!=0 );
112247     assert( omitTable==0 );
112248     testcase( pTerm->wtFlags & TERM_VIRTUAL );
112249     iReleaseReg = ++pParse->nMem;
112250     iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
112251     if( iRowidReg!=iReleaseReg ) sqlite3ReleaseTempReg(pParse, iReleaseReg);
112252     addrNxt = pLevel->addrNxt;
112253     sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); VdbeCoverage(v);
112254     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
112255     VdbeCoverage(v);
112256     sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
112257     sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
112258     VdbeComment((v, "pk"));
112259     pLevel->op = OP_Noop;
112260   }else if( (pLoop->wsFlags & WHERE_IPK)!=0
112261          && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
112262   ){
112263     /* Case 3:  We have an inequality comparison against the ROWID field.
112264     */
112265     int testOp = OP_Noop;
112266     int start;
112267     int memEndValue = 0;
112268     WhereTerm *pStart, *pEnd;
112269 
112270     assert( omitTable==0 );
112271     j = 0;
112272     pStart = pEnd = 0;
112273     if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
112274     if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
112275     assert( pStart!=0 || pEnd!=0 );
112276     if( bRev ){
112277       pTerm = pStart;
112278       pStart = pEnd;
112279       pEnd = pTerm;
112280     }
112281     if( pStart ){
112282       Expr *pX;             /* The expression that defines the start bound */
112283       int r1, rTemp;        /* Registers for holding the start boundary */
112284 
112285       /* The following constant maps TK_xx codes into corresponding
112286       ** seek opcodes.  It depends on a particular ordering of TK_xx
112287       */
112288       const u8 aMoveOp[] = {
112289            /* TK_GT */  OP_SeekGT,
112290            /* TK_LE */  OP_SeekLE,
112291            /* TK_LT */  OP_SeekLT,
112292            /* TK_GE */  OP_SeekGE
112293       };
112294       assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
112295       assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
112296       assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
112297 
112298       assert( (pStart->wtFlags & TERM_VNULL)==0 );
112299       testcase( pStart->wtFlags & TERM_VIRTUAL );
112300       pX = pStart->pExpr;
112301       assert( pX!=0 );
112302       testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
112303       r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
112304       sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
112305       VdbeComment((v, "pk"));
112306       VdbeCoverageIf(v, pX->op==TK_GT);
112307       VdbeCoverageIf(v, pX->op==TK_LE);
112308       VdbeCoverageIf(v, pX->op==TK_LT);
112309       VdbeCoverageIf(v, pX->op==TK_GE);
112310       sqlite3ExprCacheAffinityChange(pParse, r1, 1);
112311       sqlite3ReleaseTempReg(pParse, rTemp);
112312       disableTerm(pLevel, pStart);
112313     }else{
112314       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
112315       VdbeCoverageIf(v, bRev==0);
112316       VdbeCoverageIf(v, bRev!=0);
112317     }
112318     if( pEnd ){
112319       Expr *pX;
112320       pX = pEnd->pExpr;
112321       assert( pX!=0 );
112322       assert( (pEnd->wtFlags & TERM_VNULL)==0 );
112323       testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
112324       testcase( pEnd->wtFlags & TERM_VIRTUAL );
112325       memEndValue = ++pParse->nMem;
112326       sqlite3ExprCode(pParse, pX->pRight, memEndValue);
112327       if( pX->op==TK_LT || pX->op==TK_GT ){
112328         testOp = bRev ? OP_Le : OP_Ge;
112329       }else{
112330         testOp = bRev ? OP_Lt : OP_Gt;
112331       }
112332       disableTerm(pLevel, pEnd);
112333     }
112334     start = sqlite3VdbeCurrentAddr(v);
112335     pLevel->op = bRev ? OP_Prev : OP_Next;
112336     pLevel->p1 = iCur;
112337     pLevel->p2 = start;
112338     assert( pLevel->p5==0 );
112339     if( testOp!=OP_Noop ){
112340       iRowidReg = ++pParse->nMem;
112341       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
112342       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
112343       sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
112344       VdbeCoverageIf(v, testOp==OP_Le);
112345       VdbeCoverageIf(v, testOp==OP_Lt);
112346       VdbeCoverageIf(v, testOp==OP_Ge);
112347       VdbeCoverageIf(v, testOp==OP_Gt);
112348       sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
112349     }
112350   }else if( pLoop->wsFlags & WHERE_INDEXED ){
112351     /* Case 4: A scan using an index.
112352     **
112353     **         The WHERE clause may contain zero or more equality
112354     **         terms ("==" or "IN" operators) that refer to the N
112355     **         left-most columns of the index. It may also contain
112356     **         inequality constraints (>, <, >= or <=) on the indexed
112357     **         column that immediately follows the N equalities. Only
112358     **         the right-most column can be an inequality - the rest must
112359     **         use the "==" and "IN" operators. For example, if the
112360     **         index is on (x,y,z), then the following clauses are all
112361     **         optimized:
112362     **
112363     **            x=5
112364     **            x=5 AND y=10
112365     **            x=5 AND y<10
112366     **            x=5 AND y>5 AND y<10
112367     **            x=5 AND y=5 AND z<=10
112368     **
112369     **         The z<10 term of the following cannot be used, only
112370     **         the x=5 term:
112371     **
112372     **            x=5 AND z<10
112373     **
112374     **         N may be zero if there are inequality constraints.
112375     **         If there are no inequality constraints, then N is at
112376     **         least one.
112377     **
112378     **         This case is also used when there are no WHERE clause
112379     **         constraints but an index is selected anyway, in order
112380     **         to force the output order to conform to an ORDER BY.
112381     */
112382     static const u8 aStartOp[] = {
112383       0,
112384       0,
112385       OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
112386       OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
112387       OP_SeekGT,           /* 4: (start_constraints  && !startEq && !bRev) */
112388       OP_SeekLT,           /* 5: (start_constraints  && !startEq &&  bRev) */
112389       OP_SeekGE,           /* 6: (start_constraints  &&  startEq && !bRev) */
112390       OP_SeekLE            /* 7: (start_constraints  &&  startEq &&  bRev) */
112391     };
112392     static const u8 aEndOp[] = {
112393       OP_IdxGE,            /* 0: (end_constraints && !bRev && !endEq) */
112394       OP_IdxGT,            /* 1: (end_constraints && !bRev &&  endEq) */
112395       OP_IdxLE,            /* 2: (end_constraints &&  bRev && !endEq) */
112396       OP_IdxLT,            /* 3: (end_constraints &&  bRev &&  endEq) */
112397     };
112398     u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
112399     int regBase;                 /* Base register holding constraint values */
112400     WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
112401     WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
112402     int startEq;                 /* True if range start uses ==, >= or <= */
112403     int endEq;                   /* True if range end uses ==, >= or <= */
112404     int start_constraints;       /* Start of range is constrained */
112405     int nConstraint;             /* Number of constraint terms */
112406     Index *pIdx;                 /* The index we will be using */
112407     int iIdxCur;                 /* The VDBE cursor for the index */
112408     int nExtraReg = 0;           /* Number of extra registers needed */
112409     int op;                      /* Instruction opcode */
112410     char *zStartAff;             /* Affinity for start of range constraint */
112411     char cEndAff = 0;            /* Affinity for end of range constraint */
112412     u8 bSeekPastNull = 0;        /* True to seek past initial nulls */
112413     u8 bStopAtNull = 0;          /* Add condition to terminate at NULLs */
112414 
112415     pIdx = pLoop->u.btree.pIndex;
112416     iIdxCur = pLevel->iIdxCur;
112417     assert( nEq>=pLoop->u.btree.nSkip );
112418 
112419     /* If this loop satisfies a sort order (pOrderBy) request that
112420     ** was passed to this function to implement a "SELECT min(x) ..."
112421     ** query, then the caller will only allow the loop to run for
112422     ** a single iteration. This means that the first row returned
112423     ** should not have a NULL value stored in 'x'. If column 'x' is
112424     ** the first one after the nEq equality constraints in the index,
112425     ** this requires some special handling.
112426     */
112427     if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
112428      && (pWInfo->bOBSat!=0)
112429      && (pIdx->nKeyCol>nEq)
112430     ){
112431       assert( pLoop->u.btree.nSkip==0 );
112432       bSeekPastNull = 1;
112433       nExtraReg = 1;
112434     }
112435 
112436     /* Find any inequality constraint terms for the start and end
112437     ** of the range.
112438     */
112439     j = nEq;
112440     if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
112441       pRangeStart = pLoop->aLTerm[j++];
112442       nExtraReg = 1;
112443     }
112444     if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
112445       pRangeEnd = pLoop->aLTerm[j++];
112446       nExtraReg = 1;
112447       if( pRangeStart==0
112448        && (j = pIdx->aiColumn[nEq])>=0
112449        && pIdx->pTable->aCol[j].notNull==0
112450       ){
112451         bSeekPastNull = 1;
112452       }
112453     }
112454     assert( pRangeEnd==0 || (pRangeEnd->wtFlags & TERM_VNULL)==0 );
112455 
112456     /* Generate code to evaluate all constraint terms using == or IN
112457     ** and store the values of those terms in an array of registers
112458     ** starting at regBase.
112459     */
112460     regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
112461     assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
112462     if( zStartAff ) cEndAff = zStartAff[nEq];
112463     addrNxt = pLevel->addrNxt;
112464 
112465     /* If we are doing a reverse order scan on an ascending index, or
112466     ** a forward order scan on a descending index, interchange the
112467     ** start and end terms (pRangeStart and pRangeEnd).
112468     */
112469     if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
112470      || (bRev && pIdx->nKeyCol==nEq)
112471     ){
112472       SWAP(WhereTerm *, pRangeEnd, pRangeStart);
112473       SWAP(u8, bSeekPastNull, bStopAtNull);
112474     }
112475 
112476     testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
112477     testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
112478     testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
112479     testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
112480     startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
112481     endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
112482     start_constraints = pRangeStart || nEq>0;
112483 
112484     /* Seek the index cursor to the start of the range. */
112485     nConstraint = nEq;
112486     if( pRangeStart ){
112487       Expr *pRight = pRangeStart->pExpr->pRight;
112488       sqlite3ExprCode(pParse, pRight, regBase+nEq);
112489       if( (pRangeStart->wtFlags & TERM_VNULL)==0
112490        && sqlite3ExprCanBeNull(pRight)
112491       ){
112492         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
112493         VdbeCoverage(v);
112494       }
112495       if( zStartAff ){
112496         if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
112497           /* Since the comparison is to be performed with no conversions
112498           ** applied to the operands, set the affinity to apply to pRight to
112499           ** SQLITE_AFF_NONE.  */
112500           zStartAff[nEq] = SQLITE_AFF_NONE;
112501         }
112502         if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
112503           zStartAff[nEq] = SQLITE_AFF_NONE;
112504         }
112505       }
112506       nConstraint++;
112507       testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
112508     }else if( bSeekPastNull ){
112509       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
112510       nConstraint++;
112511       startEq = 0;
112512       start_constraints = 1;
112513     }
112514     codeApplyAffinity(pParse, regBase, nConstraint - bSeekPastNull, zStartAff);
112515     op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
112516     assert( op!=0 );
112517     sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
112518     VdbeCoverage(v);
112519     VdbeCoverageIf(v, op==OP_Rewind);  testcase( op==OP_Rewind );
112520     VdbeCoverageIf(v, op==OP_Last);    testcase( op==OP_Last );
112521     VdbeCoverageIf(v, op==OP_SeekGT);  testcase( op==OP_SeekGT );
112522     VdbeCoverageIf(v, op==OP_SeekGE);  testcase( op==OP_SeekGE );
112523     VdbeCoverageIf(v, op==OP_SeekLE);  testcase( op==OP_SeekLE );
112524     VdbeCoverageIf(v, op==OP_SeekLT);  testcase( op==OP_SeekLT );
112525 
112526     /* Load the value for the inequality constraint at the end of the
112527     ** range (if any).
112528     */
112529     nConstraint = nEq;
112530     if( pRangeEnd ){
112531       Expr *pRight = pRangeEnd->pExpr->pRight;
112532       sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
112533       sqlite3ExprCode(pParse, pRight, regBase+nEq);
112534       if( (pRangeEnd->wtFlags & TERM_VNULL)==0
112535        && sqlite3ExprCanBeNull(pRight)
112536       ){
112537         sqlite3VdbeAddOp2(v, OP_IsNull, regBase+nEq, addrNxt);
112538         VdbeCoverage(v);
112539       }
112540       if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
112541        && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
112542       ){
112543         codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
112544       }
112545       nConstraint++;
112546       testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
112547     }else if( bStopAtNull ){
112548       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
112549       endEq = 0;
112550       nConstraint++;
112551     }
112552     sqlite3DbFree(db, zStartAff);
112553 
112554     /* Top of the loop body */
112555     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
112556 
112557     /* Check if the index cursor is past the end of the range. */
112558     if( nConstraint ){
112559       op = aEndOp[bRev*2 + endEq];
112560       sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
112561       testcase( op==OP_IdxGT );  VdbeCoverageIf(v, op==OP_IdxGT );
112562       testcase( op==OP_IdxGE );  VdbeCoverageIf(v, op==OP_IdxGE );
112563       testcase( op==OP_IdxLT );  VdbeCoverageIf(v, op==OP_IdxLT );
112564       testcase( op==OP_IdxLE );  VdbeCoverageIf(v, op==OP_IdxLE );
112565     }
112566 
112567     /* Seek the table cursor, if required */
112568     disableTerm(pLevel, pRangeStart);
112569     disableTerm(pLevel, pRangeEnd);
112570     if( omitTable ){
112571       /* pIdx is a covering index.  No need to access the main table. */
112572     }else if( HasRowid(pIdx->pTable) ){
112573       iRowidReg = ++pParse->nMem;
112574       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
112575       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
112576       sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
112577     }else{
112578       Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
112579       iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
112580       for(j=0; j<pPk->nKeyCol; j++){
112581         k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
112582         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
112583       }
112584       sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
112585                            iRowidReg, pPk->nKeyCol); VdbeCoverage(v);
112586     }
112587 
112588     /* Record the instruction used to terminate the loop. Disable
112589     ** WHERE clause terms made redundant by the index range scan.
112590     */
112591     if( pLoop->wsFlags & WHERE_ONEROW ){
112592       pLevel->op = OP_Noop;
112593     }else if( bRev ){
112594       pLevel->op = OP_Prev;
112595     }else{
112596       pLevel->op = OP_Next;
112597     }
112598     pLevel->p1 = iIdxCur;
112599     assert( (WHERE_UNQ_WANTED>>16)==1 );
112600     pLevel->p3 = (pLoop->wsFlags>>16)&1;
112601     if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
112602       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
112603     }else{
112604       assert( pLevel->p5==0 );
112605     }
112606   }else
112607 
112608 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
112609   if( pLoop->wsFlags & WHERE_MULTI_OR ){
112610     /* Case 5:  Two or more separately indexed terms connected by OR
112611     **
112612     ** Example:
112613     **
112614     **   CREATE TABLE t1(a,b,c,d);
112615     **   CREATE INDEX i1 ON t1(a);
112616     **   CREATE INDEX i2 ON t1(b);
112617     **   CREATE INDEX i3 ON t1(c);
112618     **
112619     **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
112620     **
112621     ** In the example, there are three indexed terms connected by OR.
112622     ** The top of the loop looks like this:
112623     **
112624     **          Null       1                # Zero the rowset in reg 1
112625     **
112626     ** Then, for each indexed term, the following. The arguments to
112627     ** RowSetTest are such that the rowid of the current row is inserted
112628     ** into the RowSet. If it is already present, control skips the
112629     ** Gosub opcode and jumps straight to the code generated by WhereEnd().
112630     **
112631     **        sqlite3WhereBegin(<term>)
112632     **          RowSetTest                  # Insert rowid into rowset
112633     **          Gosub      2 A
112634     **        sqlite3WhereEnd()
112635     **
112636     ** Following the above, code to terminate the loop. Label A, the target
112637     ** of the Gosub above, jumps to the instruction right after the Goto.
112638     **
112639     **          Null       1                # Zero the rowset in reg 1
112640     **          Goto       B                # The loop is finished.
112641     **
112642     **       A: <loop body>                 # Return data, whatever.
112643     **
112644     **          Return     2                # Jump back to the Gosub
112645     **
112646     **       B: <after the loop>
112647     **
112648     */
112649     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
112650     SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
112651     Index *pCov = 0;             /* Potential covering index (or NULL) */
112652     int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
112653 
112654     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
112655     int regRowset = 0;                        /* Register for RowSet object */
112656     int regRowid = 0;                         /* Register holding rowid */
112657     int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
112658     int iRetInit;                             /* Address of regReturn init */
112659     int untestedTerms = 0;             /* Some terms not completely tested */
112660     int ii;                            /* Loop counter */
112661     Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
112662 
112663     pTerm = pLoop->aLTerm[0];
112664     assert( pTerm!=0 );
112665     assert( pTerm->eOperator & WO_OR );
112666     assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
112667     pOrWc = &pTerm->u.pOrInfo->wc;
112668     pLevel->op = OP_Return;
112669     pLevel->p1 = regReturn;
112670 
112671     /* Set up a new SrcList in pOrTab containing the table being scanned
112672     ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
112673     ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
112674     */
112675     if( pWInfo->nLevel>1 ){
112676       int nNotReady;                 /* The number of notReady tables */
112677       struct SrcList_item *origSrc;     /* Original list of tables */
112678       nNotReady = pWInfo->nLevel - iLevel - 1;
112679       pOrTab = sqlite3StackAllocRaw(db,
112680                             sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
112681       if( pOrTab==0 ) return notReady;
112682       pOrTab->nAlloc = (u8)(nNotReady + 1);
112683       pOrTab->nSrc = pOrTab->nAlloc;
112684       memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
112685       origSrc = pWInfo->pTabList->a;
112686       for(k=1; k<=nNotReady; k++){
112687         memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
112688       }
112689     }else{
112690       pOrTab = pWInfo->pTabList;
112691     }
112692 
112693     /* Initialize the rowset register to contain NULL. An SQL NULL is
112694     ** equivalent to an empty rowset.
112695     **
112696     ** Also initialize regReturn to contain the address of the instruction
112697     ** immediately following the OP_Return at the bottom of the loop. This
112698     ** is required in a few obscure LEFT JOIN cases where control jumps
112699     ** over the top of the loop into the body of it. In this case the
112700     ** correct response for the end-of-loop code (the OP_Return) is to
112701     ** fall through to the next instruction, just as an OP_Next does if
112702     ** called on an uninitialized cursor.
112703     */
112704     if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
112705       regRowset = ++pParse->nMem;
112706       regRowid = ++pParse->nMem;
112707       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
112708     }
112709     iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
112710 
112711     /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
112712     ** Then for every term xN, evaluate as the subexpression: xN AND z
112713     ** That way, terms in y that are factored into the disjunction will
112714     ** be picked up by the recursive calls to sqlite3WhereBegin() below.
112715     **
112716     ** Actually, each subexpression is converted to "xN AND w" where w is
112717     ** the "interesting" terms of z - terms that did not originate in the
112718     ** ON or USING clause of a LEFT JOIN, and terms that are usable as
112719     ** indices.
112720     **
112721     ** This optimization also only applies if the (x1 OR x2 OR ...) term
112722     ** is not contained in the ON clause of a LEFT JOIN.
112723     ** See ticket http://www.sqlite.org/src/info/f2369304e4
112724     */
112725     if( pWC->nTerm>1 ){
112726       int iTerm;
112727       for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
112728         Expr *pExpr = pWC->a[iTerm].pExpr;
112729         if( &pWC->a[iTerm] == pTerm ) continue;
112730         if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
112731         testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
112732         testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
112733         if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue;
112734         if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
112735         pExpr = sqlite3ExprDup(db, pExpr, 0);
112736         pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
112737       }
112738       if( pAndExpr ){
112739         pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
112740       }
112741     }
112742 
112743     for(ii=0; ii<pOrWc->nTerm; ii++){
112744       WhereTerm *pOrTerm = &pOrWc->a[ii];
112745       if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
112746         WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
112747         Expr *pOrExpr = pOrTerm->pExpr;
112748         if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
112749           pAndExpr->pLeft = pOrExpr;
112750           pOrExpr = pAndExpr;
112751         }
112752         /* Loop through table entries that match term pOrTerm. */
112753         pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
112754                         WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
112755                         WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
112756         assert( pSubWInfo || pParse->nErr || db->mallocFailed );
112757         if( pSubWInfo ){
112758           WhereLoop *pSubLoop;
112759           explainOneScan(
112760               pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
112761           );
112762           if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
112763             int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
112764             int r;
112765             r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
112766                                          regRowid, 0);
112767             sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
112768                                  sqlite3VdbeCurrentAddr(v)+2, r, iSet);
112769             VdbeCoverage(v);
112770           }
112771           sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
112772 
112773           /* The pSubWInfo->untestedTerms flag means that this OR term
112774           ** contained one or more AND term from a notReady table.  The
112775           ** terms from the notReady table could not be tested and will
112776           ** need to be tested later.
112777           */
112778           if( pSubWInfo->untestedTerms ) untestedTerms = 1;
112779 
112780           /* If all of the OR-connected terms are optimized using the same
112781           ** index, and the index is opened using the same cursor number
112782           ** by each call to sqlite3WhereBegin() made by this loop, it may
112783           ** be possible to use that index as a covering index.
112784           **
112785           ** If the call to sqlite3WhereBegin() above resulted in a scan that
112786           ** uses an index, and this is either the first OR-connected term
112787           ** processed or the index is the same as that used by all previous
112788           ** terms, set pCov to the candidate covering index. Otherwise, set
112789           ** pCov to NULL to indicate that no candidate covering index will
112790           ** be available.
112791           */
112792           pSubLoop = pSubWInfo->a[0].pWLoop;
112793           assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
112794           if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
112795            && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
112796           ){
112797             assert( pSubWInfo->a[0].iIdxCur==iCovCur );
112798             pCov = pSubLoop->u.btree.pIndex;
112799           }else{
112800             pCov = 0;
112801           }
112802 
112803           /* Finish the loop through table entries that match term pOrTerm. */
112804           sqlite3WhereEnd(pSubWInfo);
112805         }
112806       }
112807     }
112808     pLevel->u.pCovidx = pCov;
112809     if( pCov ) pLevel->iIdxCur = iCovCur;
112810     if( pAndExpr ){
112811       pAndExpr->pLeft = 0;
112812       sqlite3ExprDelete(db, pAndExpr);
112813     }
112814     sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
112815     sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
112816     sqlite3VdbeResolveLabel(v, iLoopBody);
112817 
112818     if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
112819     if( !untestedTerms ) disableTerm(pLevel, pTerm);
112820   }else
112821 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
112822 
112823   {
112824     /* Case 6:  There is no usable index.  We must do a complete
112825     **          scan of the entire table.
112826     */
112827     static const u8 aStep[] = { OP_Next, OP_Prev };
112828     static const u8 aStart[] = { OP_Rewind, OP_Last };
112829     assert( bRev==0 || bRev==1 );
112830     if( pTabItem->isRecursive ){
112831       /* Tables marked isRecursive have only a single row that is stored in
112832       ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
112833       pLevel->op = OP_Noop;
112834     }else{
112835       pLevel->op = aStep[bRev];
112836       pLevel->p1 = iCur;
112837       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
112838       VdbeCoverageIf(v, bRev==0);
112839       VdbeCoverageIf(v, bRev!=0);
112840       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
112841     }
112842   }
112843 
112844   /* Insert code to test every subexpression that can be completely
112845   ** computed using the current set of tables.
112846   */
112847   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
112848     Expr *pE;
112849     testcase( pTerm->wtFlags & TERM_VIRTUAL );
112850     testcase( pTerm->wtFlags & TERM_CODED );
112851     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
112852     if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
112853       testcase( pWInfo->untestedTerms==0
112854                && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
112855       pWInfo->untestedTerms = 1;
112856       continue;
112857     }
112858     pE = pTerm->pExpr;
112859     assert( pE!=0 );
112860     if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
112861       continue;
112862     }
112863     sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
112864     pTerm->wtFlags |= TERM_CODED;
112865   }
112866 
112867   /* Insert code to test for implied constraints based on transitivity
112868   ** of the "==" operator.
112869   **
112870   ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
112871   ** and we are coding the t1 loop and the t2 loop has not yet coded,
112872   ** then we cannot use the "t1.a=t2.b" constraint, but we can code
112873   ** the implied "t1.a=123" constraint.
112874   */
112875   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
112876     Expr *pE, *pEAlt;
112877     WhereTerm *pAlt;
112878     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
112879     if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
112880     if( pTerm->leftCursor!=iCur ) continue;
112881     if( pLevel->iLeftJoin ) continue;
112882     pE = pTerm->pExpr;
112883     assert( !ExprHasProperty(pE, EP_FromJoin) );
112884     assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
112885     pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
112886     if( pAlt==0 ) continue;
112887     if( pAlt->wtFlags & (TERM_CODED) ) continue;
112888     testcase( pAlt->eOperator & WO_EQ );
112889     testcase( pAlt->eOperator & WO_IN );
112890     VdbeModuleComment((v, "begin transitive constraint"));
112891     pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
112892     if( pEAlt ){
112893       *pEAlt = *pAlt->pExpr;
112894       pEAlt->pLeft = pE->pLeft;
112895       sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
112896       sqlite3StackFree(db, pEAlt);
112897     }
112898   }
112899 
112900   /* For a LEFT OUTER JOIN, generate code that will record the fact that
112901   ** at least one row of the right table has matched the left table.
112902   */
112903   if( pLevel->iLeftJoin ){
112904     pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
112905     sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
112906     VdbeComment((v, "record LEFT JOIN hit"));
112907     sqlite3ExprCacheClear(pParse);
112908     for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
112909       testcase( pTerm->wtFlags & TERM_VIRTUAL );
112910       testcase( pTerm->wtFlags & TERM_CODED );
112911       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
112912       if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
112913         assert( pWInfo->untestedTerms );
112914         continue;
112915       }
112916       assert( pTerm->pExpr );
112917       sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
112918       pTerm->wtFlags |= TERM_CODED;
112919     }
112920   }
112921 
112922   return pLevel->notReady;
112923 }
112924 
112925 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
112926 /*
112927 ** Generate "Explanation" text for a WhereTerm.
112928 */
112929 static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){
112930   char zType[4];
112931   memcpy(zType, "...", 4);
112932   if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
112933   if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
112934   if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
112935   sqlite3ExplainPrintf(v, "%s ", zType);
112936   sqlite3ExplainExpr(v, pTerm->pExpr);
112937 }
112938 #endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */
112939 
112940 
112941 #ifdef WHERETRACE_ENABLED
112942 /*
112943 ** Print a WhereLoop object for debugging purposes
112944 */
112945 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
112946   WhereInfo *pWInfo = pWC->pWInfo;
112947   int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
112948   struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
112949   Table *pTab = pItem->pTab;
112950   sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
112951                      p->iTab, nb, p->maskSelf, nb, p->prereq);
112952   sqlite3DebugPrintf(" %12s",
112953                      pItem->zAlias ? pItem->zAlias : pTab->zName);
112954   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
112955      const char *zName;
112956      if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
112957       if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
112958         int i = sqlite3Strlen30(zName) - 1;
112959         while( zName[i]!='_' ) i--;
112960         zName += i;
112961       }
112962       sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
112963     }else{
112964       sqlite3DebugPrintf("%20s","");
112965     }
112966   }else{
112967     char *z;
112968     if( p->u.vtab.idxStr ){
112969       z = sqlite3_mprintf("(%d,\"%s\",%x)",
112970                 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
112971     }else{
112972       z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
112973     }
112974     sqlite3DebugPrintf(" %-19s", z);
112975     sqlite3_free(z);
112976   }
112977   sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm);
112978   sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
112979 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
112980   /* If the 0x100 bit of wheretracing is set, then show all of the constraint
112981   ** expressions in the WhereLoop.aLTerm[] array.
112982   */
112983   if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){  /* WHERETRACE 0x100 */
112984     int i;
112985     Vdbe *v = pWInfo->pParse->pVdbe;
112986     sqlite3ExplainBegin(v);
112987     for(i=0; i<p->nLTerm; i++){
112988       WhereTerm *pTerm = p->aLTerm[i];
112989       if( pTerm==0 ) continue;
112990       sqlite3ExplainPrintf(v, "  (%d) #%-2d ", i+1, (int)(pTerm-pWC->a));
112991       sqlite3ExplainPush(v);
112992       whereExplainTerm(v, pTerm);
112993       sqlite3ExplainPop(v);
112994       sqlite3ExplainNL(v);
112995     }
112996     sqlite3ExplainFinish(v);
112997     sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
112998   }
112999 #endif
113000 }
113001 #endif
113002 
113003 /*
113004 ** Convert bulk memory into a valid WhereLoop that can be passed
113005 ** to whereLoopClear harmlessly.
113006 */
113007 static void whereLoopInit(WhereLoop *p){
113008   p->aLTerm = p->aLTermSpace;
113009   p->nLTerm = 0;
113010   p->nLSlot = ArraySize(p->aLTermSpace);
113011   p->wsFlags = 0;
113012 }
113013 
113014 /*
113015 ** Clear the WhereLoop.u union.  Leave WhereLoop.pLTerm intact.
113016 */
113017 static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
113018   if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
113019     if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
113020       sqlite3_free(p->u.vtab.idxStr);
113021       p->u.vtab.needFree = 0;
113022       p->u.vtab.idxStr = 0;
113023     }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
113024       sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
113025       sqlite3KeyInfoUnref(p->u.btree.pIndex->pKeyInfo);
113026       sqlite3DbFree(db, p->u.btree.pIndex);
113027       p->u.btree.pIndex = 0;
113028     }
113029   }
113030 }
113031 
113032 /*
113033 ** Deallocate internal memory used by a WhereLoop object
113034 */
113035 static void whereLoopClear(sqlite3 *db, WhereLoop *p){
113036   if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
113037   whereLoopClearUnion(db, p);
113038   whereLoopInit(p);
113039 }
113040 
113041 /*
113042 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
113043 */
113044 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
113045   WhereTerm **paNew;
113046   if( p->nLSlot>=n ) return SQLITE_OK;
113047   n = (n+7)&~7;
113048   paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
113049   if( paNew==0 ) return SQLITE_NOMEM;
113050   memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
113051   if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
113052   p->aLTerm = paNew;
113053   p->nLSlot = n;
113054   return SQLITE_OK;
113055 }
113056 
113057 /*
113058 ** Transfer content from the second pLoop into the first.
113059 */
113060 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
113061   whereLoopClearUnion(db, pTo);
113062   if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
113063     memset(&pTo->u, 0, sizeof(pTo->u));
113064     return SQLITE_NOMEM;
113065   }
113066   memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
113067   memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
113068   if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
113069     pFrom->u.vtab.needFree = 0;
113070   }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
113071     pFrom->u.btree.pIndex = 0;
113072   }
113073   return SQLITE_OK;
113074 }
113075 
113076 /*
113077 ** Delete a WhereLoop object
113078 */
113079 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
113080   whereLoopClear(db, p);
113081   sqlite3DbFree(db, p);
113082 }
113083 
113084 /*
113085 ** Free a WhereInfo structure
113086 */
113087 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
113088   if( ALWAYS(pWInfo) ){
113089     whereClauseClear(&pWInfo->sWC);
113090     while( pWInfo->pLoops ){
113091       WhereLoop *p = pWInfo->pLoops;
113092       pWInfo->pLoops = p->pNextLoop;
113093       whereLoopDelete(db, p);
113094     }
113095     sqlite3DbFree(db, pWInfo);
113096   }
113097 }
113098 
113099 /*
113100 ** Insert or replace a WhereLoop entry using the template supplied.
113101 **
113102 ** An existing WhereLoop entry might be overwritten if the new template
113103 ** is better and has fewer dependencies.  Or the template will be ignored
113104 ** and no insert will occur if an existing WhereLoop is faster and has
113105 ** fewer dependencies than the template.  Otherwise a new WhereLoop is
113106 ** added based on the template.
113107 **
113108 ** If pBuilder->pOrSet is not NULL then we only care about only the
113109 ** prerequisites and rRun and nOut costs of the N best loops.  That
113110 ** information is gathered in the pBuilder->pOrSet object.  This special
113111 ** processing mode is used only for OR clause processing.
113112 **
113113 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
113114 ** still might overwrite similar loops with the new template if the
113115 ** template is better.  Loops may be overwritten if the following
113116 ** conditions are met:
113117 **
113118 **    (1)  They have the same iTab.
113119 **    (2)  They have the same iSortIdx.
113120 **    (3)  The template has same or fewer dependencies than the current loop
113121 **    (4)  The template has the same or lower cost than the current loop
113122 **    (5)  The template uses more terms of the same index but has no additional
113123 **         dependencies
113124 */
113125 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
113126   WhereLoop **ppPrev, *p, *pNext = 0;
113127   WhereInfo *pWInfo = pBuilder->pWInfo;
113128   sqlite3 *db = pWInfo->pParse->db;
113129 
113130   /* If pBuilder->pOrSet is defined, then only keep track of the costs
113131   ** and prereqs.
113132   */
113133   if( pBuilder->pOrSet!=0 ){
113134 #if WHERETRACE_ENABLED
113135     u16 n = pBuilder->pOrSet->n;
113136     int x =
113137 #endif
113138     whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
113139                                     pTemplate->nOut);
113140 #if WHERETRACE_ENABLED /* 0x8 */
113141     if( sqlite3WhereTrace & 0x8 ){
113142       sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
113143       whereLoopPrint(pTemplate, pBuilder->pWC);
113144     }
113145 #endif
113146     return SQLITE_OK;
113147   }
113148 
113149   /* Search for an existing WhereLoop to overwrite, or which takes
113150   ** priority over pTemplate.
113151   */
113152   for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
113153     if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
113154       /* If either the iTab or iSortIdx values for two WhereLoop are different
113155       ** then those WhereLoops need to be considered separately.  Neither is
113156       ** a candidate to replace the other. */
113157       continue;
113158     }
113159     /* In the current implementation, the rSetup value is either zero
113160     ** or the cost of building an automatic index (NlogN) and the NlogN
113161     ** is the same for compatible WhereLoops. */
113162     assert( p->rSetup==0 || pTemplate->rSetup==0
113163                  || p->rSetup==pTemplate->rSetup );
113164 
113165     /* whereLoopAddBtree() always generates and inserts the automatic index
113166     ** case first.  Hence compatible candidate WhereLoops never have a larger
113167     ** rSetup. Call this SETUP-INVARIANT */
113168     assert( p->rSetup>=pTemplate->rSetup );
113169 
113170     if( (p->prereq & pTemplate->prereq)==p->prereq
113171      && p->rSetup<=pTemplate->rSetup
113172      && p->rRun<=pTemplate->rRun
113173      && p->nOut<=pTemplate->nOut
113174     ){
113175       /* This branch taken when p is equal or better than pTemplate in
113176       ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
113177       ** (4) number of output rows. */
113178       assert( p->rSetup==pTemplate->rSetup );
113179       if( p->prereq==pTemplate->prereq
113180        && p->nLTerm<pTemplate->nLTerm
113181        && (p->wsFlags & pTemplate->wsFlags & WHERE_INDEXED)!=0
113182        && (p->u.btree.pIndex==pTemplate->u.btree.pIndex
113183           || pTemplate->rRun+p->nLTerm<=p->rRun+pTemplate->nLTerm)
113184       ){
113185         /* Overwrite an existing WhereLoop with an similar one that uses
113186         ** more terms of the index */
113187         pNext = p->pNextLoop;
113188         break;
113189       }else{
113190         /* pTemplate is not helpful.
113191         ** Return without changing or adding anything */
113192         goto whereLoopInsert_noop;
113193       }
113194     }
113195     if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
113196      && p->rRun>=pTemplate->rRun
113197      && p->nOut>=pTemplate->nOut
113198     ){
113199       /* Overwrite an existing WhereLoop with a better one: one that is
113200       ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
113201       ** or (4) number of output rows, and is no worse in any of those
113202       ** categories. */
113203       assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
113204       pNext = p->pNextLoop;
113205       break;
113206     }
113207   }
113208 
113209   /* If we reach this point it means that either p[] should be overwritten
113210   ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
113211   ** WhereLoop and insert it.
113212   */
113213 #if WHERETRACE_ENABLED /* 0x8 */
113214   if( sqlite3WhereTrace & 0x8 ){
113215     if( p!=0 ){
113216       sqlite3DebugPrintf("ins-del:  ");
113217       whereLoopPrint(p, pBuilder->pWC);
113218     }
113219     sqlite3DebugPrintf("ins-new:  ");
113220     whereLoopPrint(pTemplate, pBuilder->pWC);
113221   }
113222 #endif
113223   if( p==0 ){
113224     p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
113225     if( p==0 ) return SQLITE_NOMEM;
113226     whereLoopInit(p);
113227   }
113228   whereLoopXfer(db, p, pTemplate);
113229   p->pNextLoop = pNext;
113230   *ppPrev = p;
113231   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
113232     Index *pIndex = p->u.btree.pIndex;
113233     if( pIndex && pIndex->tnum==0 ){
113234       p->u.btree.pIndex = 0;
113235     }
113236   }
113237   return SQLITE_OK;
113238 
113239   /* Jump here if the insert is a no-op */
113240 whereLoopInsert_noop:
113241 #if WHERETRACE_ENABLED /* 0x8 */
113242   if( sqlite3WhereTrace & 0x8 ){
113243     sqlite3DebugPrintf("ins-noop: ");
113244     whereLoopPrint(pTemplate, pBuilder->pWC);
113245   }
113246 #endif
113247   return SQLITE_OK;
113248 }
113249 
113250 /*
113251 ** Adjust the WhereLoop.nOut value downward to account for terms of the
113252 ** WHERE clause that reference the loop but which are not used by an
113253 ** index.
113254 **
113255 ** In the current implementation, the first extra WHERE clause term reduces
113256 ** the number of output rows by a factor of 10 and each additional term
113257 ** reduces the number of output rows by sqrt(2).
113258 */
113259 static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop){
113260   WhereTerm *pTerm, *pX;
113261   Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
113262   int i, j;
113263 
113264   if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){
113265     return;
113266   }
113267   for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
113268     if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
113269     if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
113270     if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
113271     for(j=pLoop->nLTerm-1; j>=0; j--){
113272       pX = pLoop->aLTerm[j];
113273       if( pX==0 ) continue;
113274       if( pX==pTerm ) break;
113275       if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
113276     }
113277     if( j<0 ) pLoop->nOut += pTerm->truthProb;
113278   }
113279 }
113280 
113281 /*
113282 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
113283 ** Try to match one more.
113284 **
113285 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
113286 ** INTEGER PRIMARY KEY.
113287 */
113288 static int whereLoopAddBtreeIndex(
113289   WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
113290   struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
113291   Index *pProbe,                  /* An index on pSrc */
113292   LogEst nInMul                   /* log(Number of iterations due to IN) */
113293 ){
113294   WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
113295   Parse *pParse = pWInfo->pParse;        /* Parsing context */
113296   sqlite3 *db = pParse->db;       /* Database connection malloc context */
113297   WhereLoop *pNew;                /* Template WhereLoop under construction */
113298   WhereTerm *pTerm;               /* A WhereTerm under consideration */
113299   int opMask;                     /* Valid operators for constraints */
113300   WhereScan scan;                 /* Iterator for WHERE terms */
113301   Bitmask saved_prereq;           /* Original value of pNew->prereq */
113302   u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
113303   u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
113304   u16 saved_nSkip;                /* Original value of pNew->u.btree.nSkip */
113305   u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
113306   LogEst saved_nOut;              /* Original value of pNew->nOut */
113307   int iCol;                       /* Index of the column in the table */
113308   int rc = SQLITE_OK;             /* Return code */
113309   LogEst nRowEst;                 /* Estimated index selectivity */
113310   LogEst rLogSize;                /* Logarithm of table size */
113311   WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
113312 
113313   pNew = pBuilder->pNew;
113314   if( db->mallocFailed ) return SQLITE_NOMEM;
113315 
113316   assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
113317   assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
113318   if( pNew->wsFlags & WHERE_BTM_LIMIT ){
113319     opMask = WO_LT|WO_LE;
113320   }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
113321     opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
113322   }else{
113323     opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
113324   }
113325   if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
113326 
113327   assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
113328   if( pNew->u.btree.nEq < pProbe->nKeyCol ){
113329     iCol = pProbe->aiColumn[pNew->u.btree.nEq];
113330     nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
113331     if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
113332   }else{
113333     iCol = -1;
113334     nRowEst = 0;
113335   }
113336   pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
113337                         opMask, pProbe);
113338   saved_nEq = pNew->u.btree.nEq;
113339   saved_nSkip = pNew->u.btree.nSkip;
113340   saved_nLTerm = pNew->nLTerm;
113341   saved_wsFlags = pNew->wsFlags;
113342   saved_prereq = pNew->prereq;
113343   saved_nOut = pNew->nOut;
113344   pNew->rSetup = 0;
113345   rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
113346 
113347   /* Consider using a skip-scan if there are no WHERE clause constraints
113348   ** available for the left-most terms of the index, and if the average
113349   ** number of repeats in the left-most terms is at least 18.  The magic
113350   ** number 18 was found by experimentation to be the payoff point where
113351   ** skip-scan become faster than a full-scan.
113352   */
113353   if( pTerm==0
113354    && saved_nEq==saved_nSkip
113355    && saved_nEq+1<pProbe->nKeyCol
113356    && pProbe->aiRowEst[saved_nEq+1]>=18  /* TUNING: Minimum for skip-scan */
113357    && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
113358   ){
113359     LogEst nIter;
113360     pNew->u.btree.nEq++;
113361     pNew->u.btree.nSkip++;
113362     pNew->aLTerm[pNew->nLTerm++] = 0;
113363     pNew->wsFlags |= WHERE_SKIPSCAN;
113364     nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
113365     pNew->rRun = rLogSize + nIter;
113366     pNew->nOut += nIter;
113367     whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter);
113368     pNew->nOut = saved_nOut;
113369   }
113370   for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
113371     int nIn = 0;
113372 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113373     int nRecValid = pBuilder->nRecValid;
113374 #endif
113375     if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
113376      && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
113377     ){
113378       continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
113379     }
113380     if( pTerm->prereqRight & pNew->maskSelf ) continue;
113381 
113382     assert( pNew->nOut==saved_nOut );
113383 
113384     pNew->wsFlags = saved_wsFlags;
113385     pNew->u.btree.nEq = saved_nEq;
113386     pNew->nLTerm = saved_nLTerm;
113387     if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
113388     pNew->aLTerm[pNew->nLTerm++] = pTerm;
113389     pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
113390     pNew->rRun = rLogSize; /* Baseline cost is log2(N).  Adjustments below */
113391     if( pTerm->eOperator & WO_IN ){
113392       Expr *pExpr = pTerm->pExpr;
113393       pNew->wsFlags |= WHERE_COLUMN_IN;
113394       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
113395         /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
113396         nIn = 46;  assert( 46==sqlite3LogEst(25) );
113397       }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
113398         /* "x IN (value, value, ...)" */
113399         nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
113400       }
113401       pNew->rRun += nIn;
113402       pNew->u.btree.nEq++;
113403       pNew->nOut = nRowEst + nInMul + nIn;
113404     }else if( pTerm->eOperator & (WO_EQ) ){
113405       assert(
113406         (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
113407         || nInMul==0
113408       );
113409       pNew->wsFlags |= WHERE_COLUMN_EQ;
113410       if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1)){
113411         assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
113412         if( iCol>=0 && pProbe->onError==OE_None ){
113413           pNew->wsFlags |= WHERE_UNQ_WANTED;
113414         }else{
113415           pNew->wsFlags |= WHERE_ONEROW;
113416         }
113417       }
113418       pNew->u.btree.nEq++;
113419       pNew->nOut = nRowEst + nInMul;
113420     }else if( pTerm->eOperator & (WO_ISNULL) ){
113421       pNew->wsFlags |= WHERE_COLUMN_NULL;
113422       pNew->u.btree.nEq++;
113423       /* TUNING: IS NULL selects 2 rows */
113424       nIn = 10;  assert( 10==sqlite3LogEst(2) );
113425       pNew->nOut = nRowEst + nInMul + nIn;
113426     }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
113427       testcase( pTerm->eOperator & WO_GT );
113428       testcase( pTerm->eOperator & WO_GE );
113429       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
113430       pBtm = pTerm;
113431       pTop = 0;
113432     }else{
113433       assert( pTerm->eOperator & (WO_LT|WO_LE) );
113434       testcase( pTerm->eOperator & WO_LT );
113435       testcase( pTerm->eOperator & WO_LE );
113436       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
113437       pTop = pTerm;
113438       pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
113439                      pNew->aLTerm[pNew->nLTerm-2] : 0;
113440     }
113441     if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
113442       /* Adjust nOut and rRun for STAT3 range values */
113443       assert( pNew->nOut==saved_nOut );
113444       whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
113445     }
113446 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113447     if( nInMul==0
113448      && pProbe->nSample
113449      && pNew->u.btree.nEq<=pProbe->nSampleCol
113450      && OptimizationEnabled(db, SQLITE_Stat3)
113451     ){
113452       Expr *pExpr = pTerm->pExpr;
113453       tRowcnt nOut = 0;
113454       if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
113455         testcase( pTerm->eOperator & WO_EQ );
113456         testcase( pTerm->eOperator & WO_ISNULL );
113457         rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
113458       }else if( (pTerm->eOperator & WO_IN)
113459              &&  !ExprHasProperty(pExpr, EP_xIsSelect)  ){
113460         rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
113461       }
113462       assert( nOut==0 || rc==SQLITE_OK );
113463       if( nOut ){
113464         pNew->nOut = sqlite3LogEst(nOut);
113465         if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
113466       }
113467     }
113468 #endif
113469     if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
113470       /* Each row involves a step of the index, then a binary search of
113471       ** the main table */
113472       pNew->rRun =  sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
113473     }
113474     /* Step cost for each output row */
113475     pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
113476     whereLoopOutputAdjust(pBuilder->pWC, pNew);
113477     rc = whereLoopInsert(pBuilder, pNew);
113478     if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
113479      && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
113480     ){
113481       whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
113482     }
113483     pNew->nOut = saved_nOut;
113484 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113485     pBuilder->nRecValid = nRecValid;
113486 #endif
113487   }
113488   pNew->prereq = saved_prereq;
113489   pNew->u.btree.nEq = saved_nEq;
113490   pNew->u.btree.nSkip = saved_nSkip;
113491   pNew->wsFlags = saved_wsFlags;
113492   pNew->nOut = saved_nOut;
113493   pNew->nLTerm = saved_nLTerm;
113494   return rc;
113495 }
113496 
113497 /*
113498 ** Return True if it is possible that pIndex might be useful in
113499 ** implementing the ORDER BY clause in pBuilder.
113500 **
113501 ** Return False if pBuilder does not contain an ORDER BY clause or
113502 ** if there is no way for pIndex to be useful in implementing that
113503 ** ORDER BY clause.
113504 */
113505 static int indexMightHelpWithOrderBy(
113506   WhereLoopBuilder *pBuilder,
113507   Index *pIndex,
113508   int iCursor
113509 ){
113510   ExprList *pOB;
113511   int ii, jj;
113512 
113513   if( pIndex->bUnordered ) return 0;
113514   if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
113515   for(ii=0; ii<pOB->nExpr; ii++){
113516     Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
113517     if( pExpr->op!=TK_COLUMN ) return 0;
113518     if( pExpr->iTable==iCursor ){
113519       for(jj=0; jj<pIndex->nKeyCol; jj++){
113520         if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
113521       }
113522     }
113523   }
113524   return 0;
113525 }
113526 
113527 /*
113528 ** Return a bitmask where 1s indicate that the corresponding column of
113529 ** the table is used by an index.  Only the first 63 columns are considered.
113530 */
113531 static Bitmask columnsInIndex(Index *pIdx){
113532   Bitmask m = 0;
113533   int j;
113534   for(j=pIdx->nColumn-1; j>=0; j--){
113535     int x = pIdx->aiColumn[j];
113536     if( x>=0 ){
113537       testcase( x==BMS-1 );
113538       testcase( x==BMS-2 );
113539       if( x<BMS-1 ) m |= MASKBIT(x);
113540     }
113541   }
113542   return m;
113543 }
113544 
113545 /* Check to see if a partial index with pPartIndexWhere can be used
113546 ** in the current query.  Return true if it can be and false if not.
113547 */
113548 static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
113549   int i;
113550   WhereTerm *pTerm;
113551   for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
113552     if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1;
113553   }
113554   return 0;
113555 }
113556 
113557 /*
113558 ** Add all WhereLoop objects for a single table of the join where the table
113559 ** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
113560 ** a b-tree table, not a virtual table.
113561 */
113562 static int whereLoopAddBtree(
113563   WhereLoopBuilder *pBuilder, /* WHERE clause information */
113564   Bitmask mExtra              /* Extra prerequesites for using this table */
113565 ){
113566   WhereInfo *pWInfo;          /* WHERE analysis context */
113567   Index *pProbe;              /* An index we are evaluating */
113568   Index sPk;                  /* A fake index object for the primary key */
113569   tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
113570   i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
113571   SrcList *pTabList;          /* The FROM clause */
113572   struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
113573   WhereLoop *pNew;            /* Template WhereLoop object */
113574   int rc = SQLITE_OK;         /* Return code */
113575   int iSortIdx = 1;           /* Index number */
113576   int b;                      /* A boolean value */
113577   LogEst rSize;               /* number of rows in the table */
113578   LogEst rLogSize;            /* Logarithm of the number of rows in the table */
113579   WhereClause *pWC;           /* The parsed WHERE clause */
113580   Table *pTab;                /* Table being queried */
113581 
113582   pNew = pBuilder->pNew;
113583   pWInfo = pBuilder->pWInfo;
113584   pTabList = pWInfo->pTabList;
113585   pSrc = pTabList->a + pNew->iTab;
113586   pTab = pSrc->pTab;
113587   pWC = pBuilder->pWC;
113588   assert( !IsVirtual(pSrc->pTab) );
113589 
113590   if( pSrc->pIndex ){
113591     /* An INDEXED BY clause specifies a particular index to use */
113592     pProbe = pSrc->pIndex;
113593   }else if( !HasRowid(pTab) ){
113594     pProbe = pTab->pIndex;
113595   }else{
113596     /* There is no INDEXED BY clause.  Create a fake Index object in local
113597     ** variable sPk to represent the rowid primary key index.  Make this
113598     ** fake index the first in a chain of Index objects with all of the real
113599     ** indices to follow */
113600     Index *pFirst;                  /* First of real indices on the table */
113601     memset(&sPk, 0, sizeof(Index));
113602     sPk.nKeyCol = 1;
113603     sPk.aiColumn = &aiColumnPk;
113604     sPk.aiRowEst = aiRowEstPk;
113605     sPk.onError = OE_Replace;
113606     sPk.pTable = pTab;
113607     aiRowEstPk[0] = pTab->nRowEst;
113608     aiRowEstPk[1] = 1;
113609     pFirst = pSrc->pTab->pIndex;
113610     if( pSrc->notIndexed==0 ){
113611       /* The real indices of the table are only considered if the
113612       ** NOT INDEXED qualifier is omitted from the FROM clause */
113613       sPk.pNext = pFirst;
113614     }
113615     pProbe = &sPk;
113616   }
113617   rSize = sqlite3LogEst(pTab->nRowEst);
113618   rLogSize = estLog(rSize);
113619 
113620 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
113621   /* Automatic indexes */
113622   if( !pBuilder->pOrSet
113623    && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
113624    && pSrc->pIndex==0
113625    && !pSrc->viaCoroutine
113626    && !pSrc->notIndexed
113627    && HasRowid(pTab)
113628    && !pSrc->isCorrelated
113629    && !pSrc->isRecursive
113630   ){
113631     /* Generate auto-index WhereLoops */
113632     WhereTerm *pTerm;
113633     WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
113634     for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
113635       if( pTerm->prereqRight & pNew->maskSelf ) continue;
113636       if( termCanDriveIndex(pTerm, pSrc, 0) ){
113637         pNew->u.btree.nEq = 1;
113638         pNew->u.btree.nSkip = 0;
113639         pNew->u.btree.pIndex = 0;
113640         pNew->nLTerm = 1;
113641         pNew->aLTerm[0] = pTerm;
113642         /* TUNING: One-time cost for computing the automatic index is
113643         ** approximately 7*N*log2(N) where N is the number of rows in
113644         ** the table being indexed. */
113645         pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
113646         /* TUNING: Each index lookup yields 20 rows in the table.  This
113647         ** is more than the usual guess of 10 rows, since we have no way
113648         ** of knowning how selective the index will ultimately be.  It would
113649         ** not be unreasonable to make this value much larger. */
113650         pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
113651         pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
113652         pNew->wsFlags = WHERE_AUTO_INDEX;
113653         pNew->prereq = mExtra | pTerm->prereqRight;
113654         rc = whereLoopInsert(pBuilder, pNew);
113655       }
113656     }
113657   }
113658 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
113659 
113660   /* Loop over all indices
113661   */
113662   for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
113663     if( pProbe->pPartIdxWhere!=0
113664      && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
113665       continue;  /* Partial index inappropriate for this query */
113666     }
113667     pNew->u.btree.nEq = 0;
113668     pNew->u.btree.nSkip = 0;
113669     pNew->nLTerm = 0;
113670     pNew->iSortIdx = 0;
113671     pNew->rSetup = 0;
113672     pNew->prereq = mExtra;
113673     pNew->nOut = rSize;
113674     pNew->u.btree.pIndex = pProbe;
113675     b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
113676     /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
113677     assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
113678     if( pProbe->tnum<=0 ){
113679       /* Integer primary key index */
113680       pNew->wsFlags = WHERE_IPK;
113681 
113682       /* Full table scan */
113683       pNew->iSortIdx = b ? iSortIdx : 0;
113684       /* TUNING: Cost of full table scan is 3*(N + log2(N)).
113685       **  +  The extra 3 factor is to encourage the use of indexed lookups
113686       **     over full scans.  FIXME */
113687       pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
113688       whereLoopOutputAdjust(pWC, pNew);
113689       rc = whereLoopInsert(pBuilder, pNew);
113690       pNew->nOut = rSize;
113691       if( rc ) break;
113692     }else{
113693       Bitmask m;
113694       if( pProbe->isCovering ){
113695         pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
113696         m = 0;
113697       }else{
113698         m = pSrc->colUsed & ~columnsInIndex(pProbe);
113699         pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
113700       }
113701 
113702       /* Full scan via index */
113703       if( b
113704        || !HasRowid(pTab)
113705        || ( m==0
113706          && pProbe->bUnordered==0
113707          && (pProbe->szIdxRow<pTab->szTabRow)
113708          && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
113709          && sqlite3GlobalConfig.bUseCis
113710          && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
113711           )
113712       ){
113713         pNew->iSortIdx = b ? iSortIdx : 0;
113714         if( m==0 ){
113715           /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
113716           **  +  The extra factor K of between 1.1 and 3.0 that depends
113717           **     on the relative sizes of the table and the index.  K
113718           **     is smaller for smaller indices, thus favoring them.
113719           */
113720           pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
113721                         (15*pProbe->szIdxRow)/pTab->szTabRow;
113722         }else{
113723           /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
113724           ** which we will simplify to just N*log2(N) */
113725           pNew->rRun = rSize + rLogSize;
113726         }
113727         whereLoopOutputAdjust(pWC, pNew);
113728         rc = whereLoopInsert(pBuilder, pNew);
113729         pNew->nOut = rSize;
113730         if( rc ) break;
113731       }
113732     }
113733 
113734     rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
113735 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113736     sqlite3Stat4ProbeFree(pBuilder->pRec);
113737     pBuilder->nRecValid = 0;
113738     pBuilder->pRec = 0;
113739 #endif
113740 
113741     /* If there was an INDEXED BY clause, then only that one index is
113742     ** considered. */
113743     if( pSrc->pIndex ) break;
113744   }
113745   return rc;
113746 }
113747 
113748 #ifndef SQLITE_OMIT_VIRTUALTABLE
113749 /*
113750 ** Add all WhereLoop objects for a table of the join identified by
113751 ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
113752 */
113753 static int whereLoopAddVirtual(
113754   WhereLoopBuilder *pBuilder,  /* WHERE clause information */
113755   Bitmask mExtra
113756 ){
113757   WhereInfo *pWInfo;           /* WHERE analysis context */
113758   Parse *pParse;               /* The parsing context */
113759   WhereClause *pWC;            /* The WHERE clause */
113760   struct SrcList_item *pSrc;   /* The FROM clause term to search */
113761   Table *pTab;
113762   sqlite3 *db;
113763   sqlite3_index_info *pIdxInfo;
113764   struct sqlite3_index_constraint *pIdxCons;
113765   struct sqlite3_index_constraint_usage *pUsage;
113766   WhereTerm *pTerm;
113767   int i, j;
113768   int iTerm, mxTerm;
113769   int nConstraint;
113770   int seenIn = 0;              /* True if an IN operator is seen */
113771   int seenVar = 0;             /* True if a non-constant constraint is seen */
113772   int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
113773   WhereLoop *pNew;
113774   int rc = SQLITE_OK;
113775 
113776   pWInfo = pBuilder->pWInfo;
113777   pParse = pWInfo->pParse;
113778   db = pParse->db;
113779   pWC = pBuilder->pWC;
113780   pNew = pBuilder->pNew;
113781   pSrc = &pWInfo->pTabList->a[pNew->iTab];
113782   pTab = pSrc->pTab;
113783   assert( IsVirtual(pTab) );
113784   pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
113785   if( pIdxInfo==0 ) return SQLITE_NOMEM;
113786   pNew->prereq = 0;
113787   pNew->rSetup = 0;
113788   pNew->wsFlags = WHERE_VIRTUALTABLE;
113789   pNew->nLTerm = 0;
113790   pNew->u.vtab.needFree = 0;
113791   pUsage = pIdxInfo->aConstraintUsage;
113792   nConstraint = pIdxInfo->nConstraint;
113793   if( whereLoopResize(db, pNew, nConstraint) ){
113794     sqlite3DbFree(db, pIdxInfo);
113795     return SQLITE_NOMEM;
113796   }
113797 
113798   for(iPhase=0; iPhase<=3; iPhase++){
113799     if( !seenIn && (iPhase&1)!=0 ){
113800       iPhase++;
113801       if( iPhase>3 ) break;
113802     }
113803     if( !seenVar && iPhase>1 ) break;
113804     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
113805     for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
113806       j = pIdxCons->iTermOffset;
113807       pTerm = &pWC->a[j];
113808       switch( iPhase ){
113809         case 0:    /* Constants without IN operator */
113810           pIdxCons->usable = 0;
113811           if( (pTerm->eOperator & WO_IN)!=0 ){
113812             seenIn = 1;
113813           }
113814           if( pTerm->prereqRight!=0 ){
113815             seenVar = 1;
113816           }else if( (pTerm->eOperator & WO_IN)==0 ){
113817             pIdxCons->usable = 1;
113818           }
113819           break;
113820         case 1:    /* Constants with IN operators */
113821           assert( seenIn );
113822           pIdxCons->usable = (pTerm->prereqRight==0);
113823           break;
113824         case 2:    /* Variables without IN */
113825           assert( seenVar );
113826           pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
113827           break;
113828         default:   /* Variables with IN */
113829           assert( seenVar && seenIn );
113830           pIdxCons->usable = 1;
113831           break;
113832       }
113833     }
113834     memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
113835     if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
113836     pIdxInfo->idxStr = 0;
113837     pIdxInfo->idxNum = 0;
113838     pIdxInfo->needToFreeIdxStr = 0;
113839     pIdxInfo->orderByConsumed = 0;
113840     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
113841     pIdxInfo->estimatedRows = 25;
113842     rc = vtabBestIndex(pParse, pTab, pIdxInfo);
113843     if( rc ) goto whereLoopAddVtab_exit;
113844     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
113845     pNew->prereq = mExtra;
113846     mxTerm = -1;
113847     assert( pNew->nLSlot>=nConstraint );
113848     for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
113849     pNew->u.vtab.omitMask = 0;
113850     for(i=0; i<nConstraint; i++, pIdxCons++){
113851       if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
113852         j = pIdxCons->iTermOffset;
113853         if( iTerm>=nConstraint
113854          || j<0
113855          || j>=pWC->nTerm
113856          || pNew->aLTerm[iTerm]!=0
113857         ){
113858           rc = SQLITE_ERROR;
113859           sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
113860           goto whereLoopAddVtab_exit;
113861         }
113862         testcase( iTerm==nConstraint-1 );
113863         testcase( j==0 );
113864         testcase( j==pWC->nTerm-1 );
113865         pTerm = &pWC->a[j];
113866         pNew->prereq |= pTerm->prereqRight;
113867         assert( iTerm<pNew->nLSlot );
113868         pNew->aLTerm[iTerm] = pTerm;
113869         if( iTerm>mxTerm ) mxTerm = iTerm;
113870         testcase( iTerm==15 );
113871         testcase( iTerm==16 );
113872         if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
113873         if( (pTerm->eOperator & WO_IN)!=0 ){
113874           if( pUsage[i].omit==0 ){
113875             /* Do not attempt to use an IN constraint if the virtual table
113876             ** says that the equivalent EQ constraint cannot be safely omitted.
113877             ** If we do attempt to use such a constraint, some rows might be
113878             ** repeated in the output. */
113879             break;
113880           }
113881           /* A virtual table that is constrained by an IN clause may not
113882           ** consume the ORDER BY clause because (1) the order of IN terms
113883           ** is not necessarily related to the order of output terms and
113884           ** (2) Multiple outputs from a single IN value will not merge
113885           ** together.  */
113886           pIdxInfo->orderByConsumed = 0;
113887         }
113888       }
113889     }
113890     if( i>=nConstraint ){
113891       pNew->nLTerm = mxTerm+1;
113892       assert( pNew->nLTerm<=pNew->nLSlot );
113893       pNew->u.vtab.idxNum = pIdxInfo->idxNum;
113894       pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
113895       pIdxInfo->needToFreeIdxStr = 0;
113896       pNew->u.vtab.idxStr = pIdxInfo->idxStr;
113897       pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
113898                                      && pIdxInfo->orderByConsumed);
113899       pNew->rSetup = 0;
113900       pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
113901       pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
113902       whereLoopInsert(pBuilder, pNew);
113903       if( pNew->u.vtab.needFree ){
113904         sqlite3_free(pNew->u.vtab.idxStr);
113905         pNew->u.vtab.needFree = 0;
113906       }
113907     }
113908   }
113909 
113910 whereLoopAddVtab_exit:
113911   if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
113912   sqlite3DbFree(db, pIdxInfo);
113913   return rc;
113914 }
113915 #endif /* SQLITE_OMIT_VIRTUALTABLE */
113916 
113917 /*
113918 ** Add WhereLoop entries to handle OR terms.  This works for either
113919 ** btrees or virtual tables.
113920 */
113921 static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
113922   WhereInfo *pWInfo = pBuilder->pWInfo;
113923   WhereClause *pWC;
113924   WhereLoop *pNew;
113925   WhereTerm *pTerm, *pWCEnd;
113926   int rc = SQLITE_OK;
113927   int iCur;
113928   WhereClause tempWC;
113929   WhereLoopBuilder sSubBuild;
113930   WhereOrSet sSum, sCur, sPrev;
113931   struct SrcList_item *pItem;
113932 
113933   pWC = pBuilder->pWC;
113934   if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
113935   pWCEnd = pWC->a + pWC->nTerm;
113936   pNew = pBuilder->pNew;
113937   memset(&sSum, 0, sizeof(sSum));
113938   pItem = pWInfo->pTabList->a + pNew->iTab;
113939   if( !HasRowid(pItem->pTab) ) return SQLITE_OK;
113940   iCur = pItem->iCursor;
113941 
113942   for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
113943     if( (pTerm->eOperator & WO_OR)!=0
113944      && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
113945     ){
113946       WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
113947       WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
113948       WhereTerm *pOrTerm;
113949       int once = 1;
113950       int i, j;
113951 
113952       sSubBuild = *pBuilder;
113953       sSubBuild.pOrderBy = 0;
113954       sSubBuild.pOrSet = &sCur;
113955 
113956       for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
113957         if( (pOrTerm->eOperator & WO_AND)!=0 ){
113958           sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
113959         }else if( pOrTerm->leftCursor==iCur ){
113960           tempWC.pWInfo = pWC->pWInfo;
113961           tempWC.pOuter = pWC;
113962           tempWC.op = TK_AND;
113963           tempWC.nTerm = 1;
113964           tempWC.a = pOrTerm;
113965           sSubBuild.pWC = &tempWC;
113966         }else{
113967           continue;
113968         }
113969         sCur.n = 0;
113970 #ifndef SQLITE_OMIT_VIRTUALTABLE
113971         if( IsVirtual(pItem->pTab) ){
113972           rc = whereLoopAddVirtual(&sSubBuild, mExtra);
113973         }else
113974 #endif
113975         {
113976           rc = whereLoopAddBtree(&sSubBuild, mExtra);
113977         }
113978         assert( rc==SQLITE_OK || sCur.n==0 );
113979         if( sCur.n==0 ){
113980           sSum.n = 0;
113981           break;
113982         }else if( once ){
113983           whereOrMove(&sSum, &sCur);
113984           once = 0;
113985         }else{
113986           whereOrMove(&sPrev, &sSum);
113987           sSum.n = 0;
113988           for(i=0; i<sPrev.n; i++){
113989             for(j=0; j<sCur.n; j++){
113990               whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
113991                             sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
113992                             sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
113993             }
113994           }
113995         }
113996       }
113997       pNew->nLTerm = 1;
113998       pNew->aLTerm[0] = pTerm;
113999       pNew->wsFlags = WHERE_MULTI_OR;
114000       pNew->rSetup = 0;
114001       pNew->iSortIdx = 0;
114002       memset(&pNew->u, 0, sizeof(pNew->u));
114003       for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
114004         /* TUNING: Multiple by 3.5 for the secondary table lookup */
114005         pNew->rRun = sSum.a[i].rRun + 18;
114006         pNew->nOut = sSum.a[i].nOut;
114007         pNew->prereq = sSum.a[i].prereq;
114008         rc = whereLoopInsert(pBuilder, pNew);
114009       }
114010     }
114011   }
114012   return rc;
114013 }
114014 
114015 /*
114016 ** Add all WhereLoop objects for all tables
114017 */
114018 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
114019   WhereInfo *pWInfo = pBuilder->pWInfo;
114020   Bitmask mExtra = 0;
114021   Bitmask mPrior = 0;
114022   int iTab;
114023   SrcList *pTabList = pWInfo->pTabList;
114024   struct SrcList_item *pItem;
114025   sqlite3 *db = pWInfo->pParse->db;
114026   int nTabList = pWInfo->nLevel;
114027   int rc = SQLITE_OK;
114028   u8 priorJoinType = 0;
114029   WhereLoop *pNew;
114030 
114031   /* Loop over the tables in the join, from left to right */
114032   pNew = pBuilder->pNew;
114033   whereLoopInit(pNew);
114034   for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
114035     pNew->iTab = iTab;
114036     pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
114037     if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
114038       mExtra = mPrior;
114039     }
114040     priorJoinType = pItem->jointype;
114041     if( IsVirtual(pItem->pTab) ){
114042       rc = whereLoopAddVirtual(pBuilder, mExtra);
114043     }else{
114044       rc = whereLoopAddBtree(pBuilder, mExtra);
114045     }
114046     if( rc==SQLITE_OK ){
114047       rc = whereLoopAddOr(pBuilder, mExtra);
114048     }
114049     mPrior |= pNew->maskSelf;
114050     if( rc || db->mallocFailed ) break;
114051   }
114052   whereLoopClear(db, pNew);
114053   return rc;
114054 }
114055 
114056 /*
114057 ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
114058 ** parameters) to see if it outputs rows in the requested ORDER BY
114059 ** (or GROUP BY) without requiring a separate sort operation.  Return:
114060 **
114061 **    0:  ORDER BY is not satisfied.  Sorting required
114062 **    1:  ORDER BY is satisfied.      Omit sorting
114063 **   -1:  Unknown at this time
114064 **
114065 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
114066 ** strict.  With GROUP BY and DISTINCT the only requirement is that
114067 ** equivalent rows appear immediately adjacent to one another.  GROUP BY
114068 ** and DISTINT do not require rows to appear in any particular order as long
114069 ** as equivelent rows are grouped together.  Thus for GROUP BY and DISTINCT
114070 ** the pOrderBy terms can be matched in any order.  With ORDER BY, the
114071 ** pOrderBy terms must be matched in strict left-to-right order.
114072 */
114073 static int wherePathSatisfiesOrderBy(
114074   WhereInfo *pWInfo,    /* The WHERE clause */
114075   ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
114076   WherePath *pPath,     /* The WherePath to check */
114077   u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
114078   u16 nLoop,            /* Number of entries in pPath->aLoop[] */
114079   WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
114080   Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
114081 ){
114082   u8 revSet;            /* True if rev is known */
114083   u8 rev;               /* Composite sort order */
114084   u8 revIdx;            /* Index sort order */
114085   u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
114086   u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
114087   u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
114088   u16 nKeyCol;          /* Number of key columns in pIndex */
114089   u16 nColumn;          /* Total number of ordered columns in the index */
114090   u16 nOrderBy;         /* Number terms in the ORDER BY clause */
114091   int iLoop;            /* Index of WhereLoop in pPath being processed */
114092   int i, j;             /* Loop counters */
114093   int iCur;             /* Cursor number for current WhereLoop */
114094   int iColumn;          /* A column number within table iCur */
114095   WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
114096   WhereTerm *pTerm;     /* A single term of the WHERE clause */
114097   Expr *pOBExpr;        /* An expression from the ORDER BY clause */
114098   CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
114099   Index *pIndex;        /* The index associated with pLoop */
114100   sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
114101   Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
114102   Bitmask obDone;       /* Mask of all ORDER BY terms */
114103   Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
114104   Bitmask ready;              /* Mask of inner loops */
114105 
114106   /*
114107   ** We say the WhereLoop is "one-row" if it generates no more than one
114108   ** row of output.  A WhereLoop is one-row if all of the following are true:
114109   **  (a) All index columns match with WHERE_COLUMN_EQ.
114110   **  (b) The index is unique
114111   ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
114112   ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
114113   **
114114   ** We say the WhereLoop is "order-distinct" if the set of columns from
114115   ** that WhereLoop that are in the ORDER BY clause are different for every
114116   ** row of the WhereLoop.  Every one-row WhereLoop is automatically
114117   ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
114118   ** is not order-distinct. To be order-distinct is not quite the same as being
114119   ** UNIQUE since a UNIQUE column or index can have multiple rows that
114120   ** are NULL and NULL values are equivalent for the purpose of order-distinct.
114121   ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
114122   **
114123   ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
114124   ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
114125   ** automatically order-distinct.
114126   */
114127 
114128   assert( pOrderBy!=0 );
114129 
114130   /* Sortability of virtual tables is determined by the xBestIndex method
114131   ** of the virtual table itself */
114132   if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
114133     testcase( nLoop>0 );  /* True when outer loops are one-row and match
114134                           ** no ORDER BY terms */
114135     return pLast->u.vtab.isOrdered;
114136   }
114137   if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
114138 
114139   nOrderBy = pOrderBy->nExpr;
114140   testcase( nOrderBy==BMS-1 );
114141   if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
114142   isOrderDistinct = 1;
114143   obDone = MASKBIT(nOrderBy)-1;
114144   orderDistinctMask = 0;
114145   ready = 0;
114146   for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
114147     if( iLoop>0 ) ready |= pLoop->maskSelf;
114148     pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
114149     assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
114150     iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
114151 
114152     /* Mark off any ORDER BY term X that is a column in the table of
114153     ** the current loop for which there is term in the WHERE
114154     ** clause of the form X IS NULL or X=? that reference only outer
114155     ** loops.
114156     */
114157     for(i=0; i<nOrderBy; i++){
114158       if( MASKBIT(i) & obSat ) continue;
114159       pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
114160       if( pOBExpr->op!=TK_COLUMN ) continue;
114161       if( pOBExpr->iTable!=iCur ) continue;
114162       pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
114163                        ~ready, WO_EQ|WO_ISNULL, 0);
114164       if( pTerm==0 ) continue;
114165       if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
114166         const char *z1, *z2;
114167         pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
114168         if( !pColl ) pColl = db->pDfltColl;
114169         z1 = pColl->zName;
114170         pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
114171         if( !pColl ) pColl = db->pDfltColl;
114172         z2 = pColl->zName;
114173         if( sqlite3StrICmp(z1, z2)!=0 ) continue;
114174       }
114175       obSat |= MASKBIT(i);
114176     }
114177 
114178     if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
114179       if( pLoop->wsFlags & WHERE_IPK ){
114180         pIndex = 0;
114181         nKeyCol = 0;
114182         nColumn = 1;
114183       }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
114184         return 0;
114185       }else{
114186         nKeyCol = pIndex->nKeyCol;
114187         nColumn = pIndex->nColumn;
114188         assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
114189         assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
114190         isOrderDistinct = pIndex->onError!=OE_None;
114191       }
114192 
114193       /* Loop through all columns of the index and deal with the ones
114194       ** that are not constrained by == or IN.
114195       */
114196       rev = revSet = 0;
114197       distinctColumns = 0;
114198       for(j=0; j<nColumn; j++){
114199         u8 bOnce;   /* True to run the ORDER BY search loop */
114200 
114201         /* Skip over == and IS NULL terms */
114202         if( j<pLoop->u.btree.nEq
114203          && pLoop->u.btree.nSkip==0
114204          && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
114205         ){
114206           if( i & WO_ISNULL ){
114207             testcase( isOrderDistinct );
114208             isOrderDistinct = 0;
114209           }
114210           continue;
114211         }
114212 
114213         /* Get the column number in the table (iColumn) and sort order
114214         ** (revIdx) for the j-th column of the index.
114215         */
114216         if( pIndex ){
114217           iColumn = pIndex->aiColumn[j];
114218           revIdx = pIndex->aSortOrder[j];
114219           if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
114220         }else{
114221           iColumn = -1;
114222           revIdx = 0;
114223         }
114224 
114225         /* An unconstrained column that might be NULL means that this
114226         ** WhereLoop is not well-ordered
114227         */
114228         if( isOrderDistinct
114229          && iColumn>=0
114230          && j>=pLoop->u.btree.nEq
114231          && pIndex->pTable->aCol[iColumn].notNull==0
114232         ){
114233           isOrderDistinct = 0;
114234         }
114235 
114236         /* Find the ORDER BY term that corresponds to the j-th column
114237         ** of the index and and mark that ORDER BY term off
114238         */
114239         bOnce = 1;
114240         isMatch = 0;
114241         for(i=0; bOnce && i<nOrderBy; i++){
114242           if( MASKBIT(i) & obSat ) continue;
114243           pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
114244           testcase( wctrlFlags & WHERE_GROUPBY );
114245           testcase( wctrlFlags & WHERE_DISTINCTBY );
114246           if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
114247           if( pOBExpr->op!=TK_COLUMN ) continue;
114248           if( pOBExpr->iTable!=iCur ) continue;
114249           if( pOBExpr->iColumn!=iColumn ) continue;
114250           if( iColumn>=0 ){
114251             pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
114252             if( !pColl ) pColl = db->pDfltColl;
114253             if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
114254           }
114255           isMatch = 1;
114256           break;
114257         }
114258         if( isMatch ){
114259           if( iColumn<0 ){
114260             testcase( distinctColumns==0 );
114261             distinctColumns = 1;
114262           }
114263           obSat |= MASKBIT(i);
114264           if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
114265             /* Make sure the sort order is compatible in an ORDER BY clause.
114266             ** Sort order is irrelevant for a GROUP BY clause. */
114267             if( revSet ){
114268               if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0;
114269             }else{
114270               rev = revIdx ^ pOrderBy->a[i].sortOrder;
114271               if( rev ) *pRevMask |= MASKBIT(iLoop);
114272               revSet = 1;
114273             }
114274           }
114275         }else{
114276           /* No match found */
114277           if( j==0 || j<nKeyCol ){
114278             testcase( isOrderDistinct!=0 );
114279             isOrderDistinct = 0;
114280           }
114281           break;
114282         }
114283       } /* end Loop over all index columns */
114284       if( distinctColumns ){
114285         testcase( isOrderDistinct==0 );
114286         isOrderDistinct = 1;
114287       }
114288     } /* end-if not one-row */
114289 
114290     /* Mark off any other ORDER BY terms that reference pLoop */
114291     if( isOrderDistinct ){
114292       orderDistinctMask |= pLoop->maskSelf;
114293       for(i=0; i<nOrderBy; i++){
114294         Expr *p;
114295         Bitmask mTerm;
114296         if( MASKBIT(i) & obSat ) continue;
114297         p = pOrderBy->a[i].pExpr;
114298         mTerm = exprTableUsage(&pWInfo->sMaskSet,p);
114299         if( mTerm==0 && !sqlite3ExprIsConstant(p) ) continue;
114300         if( (mTerm&~orderDistinctMask)==0 ){
114301           obSat |= MASKBIT(i);
114302         }
114303       }
114304     }
114305   } /* End the loop over all WhereLoops from outer-most down to inner-most */
114306   if( obSat==obDone ) return 1;
114307   if( !isOrderDistinct ) return 0;
114308   return -1;
114309 }
114310 
114311 #ifdef WHERETRACE_ENABLED
114312 /* For debugging use only: */
114313 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
114314   static char zName[65];
114315   int i;
114316   for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
114317   if( pLast ) zName[i++] = pLast->cId;
114318   zName[i] = 0;
114319   return zName;
114320 }
114321 #endif
114322 
114323 
114324 /*
114325 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
114326 ** attempts to find the lowest cost path that visits each WhereLoop
114327 ** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
114328 **
114329 ** Assume that the total number of output rows that will need to be sorted
114330 ** will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
114331 ** costs if nRowEst==0.
114332 **
114333 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
114334 ** error occurs.
114335 */
114336 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
114337   int mxChoice;             /* Maximum number of simultaneous paths tracked */
114338   int nLoop;                /* Number of terms in the join */
114339   Parse *pParse;            /* Parsing context */
114340   sqlite3 *db;              /* The database connection */
114341   int iLoop;                /* Loop counter over the terms of the join */
114342   int ii, jj;               /* Loop counters */
114343   int mxI = 0;              /* Index of next entry to replace */
114344   LogEst rCost;             /* Cost of a path */
114345   LogEst nOut;              /* Number of outputs */
114346   LogEst mxCost = 0;        /* Maximum cost of a set of paths */
114347   LogEst mxOut = 0;         /* Maximum nOut value on the set of paths */
114348   LogEst rSortCost;         /* Cost to do a sort */
114349   int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
114350   WherePath *aFrom;         /* All nFrom paths at the previous level */
114351   WherePath *aTo;           /* The nTo best paths at the current level */
114352   WherePath *pFrom;         /* An element of aFrom[] that we are working on */
114353   WherePath *pTo;           /* An element of aTo[] that we are working on */
114354   WhereLoop *pWLoop;        /* One of the WhereLoop objects */
114355   WhereLoop **pX;           /* Used to divy up the pSpace memory */
114356   char *pSpace;             /* Temporary memory used by this routine */
114357 
114358   pParse = pWInfo->pParse;
114359   db = pParse->db;
114360   nLoop = pWInfo->nLevel;
114361   /* TUNING: For simple queries, only the best path is tracked.
114362   ** For 2-way joins, the 5 best paths are followed.
114363   ** For joins of 3 or more tables, track the 10 best paths */
114364   mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10);
114365   assert( nLoop<=pWInfo->pTabList->nSrc );
114366   WHERETRACE(0x002, ("---- begin solver\n"));
114367 
114368   /* Allocate and initialize space for aTo and aFrom */
114369   ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
114370   pSpace = sqlite3DbMallocRaw(db, ii);
114371   if( pSpace==0 ) return SQLITE_NOMEM;
114372   aTo = (WherePath*)pSpace;
114373   aFrom = aTo+mxChoice;
114374   memset(aFrom, 0, sizeof(aFrom[0]));
114375   pX = (WhereLoop**)(aFrom+mxChoice);
114376   for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
114377     pFrom->aLoop = pX;
114378   }
114379 
114380   /* Seed the search with a single WherePath containing zero WhereLoops.
114381   **
114382   ** TUNING: Do not let the number of iterations go above 25.  If the cost
114383   ** of computing an automatic index is not paid back within the first 25
114384   ** rows, then do not use the automatic index. */
114385   aFrom[0].nRow = MIN(pParse->nQueryLoop, 46);  assert( 46==sqlite3LogEst(25) );
114386   nFrom = 1;
114387 
114388   /* Precompute the cost of sorting the final result set, if the caller
114389   ** to sqlite3WhereBegin() was concerned about sorting */
114390   rSortCost = 0;
114391   if( pWInfo->pOrderBy==0 || nRowEst==0 ){
114392     aFrom[0].isOrderedValid = 1;
114393   }else{
114394     /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
114395     ** number of output rows. The 48 is the expected size of a row to sort.
114396     ** FIXME:  compute a better estimate of the 48 multiplier based on the
114397     ** result set expressions. */
114398     rSortCost = nRowEst + estLog(nRowEst);
114399     WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
114400   }
114401 
114402   /* Compute successively longer WherePaths using the previous generation
114403   ** of WherePaths as the basis for the next.  Keep track of the mxChoice
114404   ** best paths at each generation */
114405   for(iLoop=0; iLoop<nLoop; iLoop++){
114406     nTo = 0;
114407     for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
114408       for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
114409         Bitmask maskNew;
114410         Bitmask revMask = 0;
114411         u8 isOrderedValid = pFrom->isOrderedValid;
114412         u8 isOrdered = pFrom->isOrdered;
114413         if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
114414         if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
114415         /* At this point, pWLoop is a candidate to be the next loop.
114416         ** Compute its cost */
114417         rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
114418         rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
114419         nOut = pFrom->nRow + pWLoop->nOut;
114420         maskNew = pFrom->maskLoop | pWLoop->maskSelf;
114421         if( !isOrderedValid ){
114422           switch( wherePathSatisfiesOrderBy(pWInfo,
114423                        pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
114424                        iLoop, pWLoop, &revMask) ){
114425             case 1:  /* Yes.  pFrom+pWLoop does satisfy the ORDER BY clause */
114426               isOrdered = 1;
114427               isOrderedValid = 1;
114428               break;
114429             case 0:  /* No.  pFrom+pWLoop will require a separate sort */
114430               isOrdered = 0;
114431               isOrderedValid = 1;
114432               rCost = sqlite3LogEstAdd(rCost, rSortCost);
114433               break;
114434             default: /* Cannot tell yet.  Try again on the next iteration */
114435               break;
114436           }
114437         }else{
114438           revMask = pFrom->revLoop;
114439         }
114440         /* Check to see if pWLoop should be added to the mxChoice best so far */
114441         for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
114442           if( pTo->maskLoop==maskNew
114443            && pTo->isOrderedValid==isOrderedValid
114444            && ((pTo->rCost<=rCost && pTo->nRow<=nOut) ||
114445                 (pTo->rCost>=rCost && pTo->nRow>=nOut))
114446           ){
114447             testcase( jj==nTo-1 );
114448             break;
114449           }
114450         }
114451         if( jj>=nTo ){
114452           if( nTo>=mxChoice && rCost>=mxCost ){
114453 #ifdef WHERETRACE_ENABLED /* 0x4 */
114454             if( sqlite3WhereTrace&0x4 ){
114455               sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
114456                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114457                   isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
114458             }
114459 #endif
114460             continue;
114461           }
114462           /* Add a new Path to the aTo[] set */
114463           if( nTo<mxChoice ){
114464             /* Increase the size of the aTo set by one */
114465             jj = nTo++;
114466           }else{
114467             /* New path replaces the prior worst to keep count below mxChoice */
114468             jj = mxI;
114469           }
114470           pTo = &aTo[jj];
114471 #ifdef WHERETRACE_ENABLED /* 0x4 */
114472           if( sqlite3WhereTrace&0x4 ){
114473             sqlite3DebugPrintf("New    %s cost=%-3d,%3d order=%c\n",
114474                 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114475                 isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
114476           }
114477 #endif
114478         }else{
114479           if( pTo->rCost<=rCost && pTo->nRow<=nOut ){
114480 #ifdef WHERETRACE_ENABLED /* 0x4 */
114481             if( sqlite3WhereTrace&0x4 ){
114482               sqlite3DebugPrintf(
114483                   "Skip   %s cost=%-3d,%3d order=%c",
114484                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114485                   isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
114486               sqlite3DebugPrintf("   vs %s cost=%-3d,%d order=%c\n",
114487                   wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
114488                   pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
114489             }
114490 #endif
114491             testcase( pTo->rCost==rCost );
114492             continue;
114493           }
114494           testcase( pTo->rCost==rCost+1 );
114495           /* A new and better score for a previously created equivalent path */
114496 #ifdef WHERETRACE_ENABLED /* 0x4 */
114497           if( sqlite3WhereTrace&0x4 ){
114498             sqlite3DebugPrintf(
114499                 "Update %s cost=%-3d,%3d order=%c",
114500                 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
114501                 isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
114502             sqlite3DebugPrintf("  was %s cost=%-3d,%3d order=%c\n",
114503                 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
114504                 pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
114505           }
114506 #endif
114507         }
114508         /* pWLoop is a winner.  Add it to the set of best so far */
114509         pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
114510         pTo->revLoop = revMask;
114511         pTo->nRow = nOut;
114512         pTo->rCost = rCost;
114513         pTo->isOrderedValid = isOrderedValid;
114514         pTo->isOrdered = isOrdered;
114515         memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
114516         pTo->aLoop[iLoop] = pWLoop;
114517         if( nTo>=mxChoice ){
114518           mxI = 0;
114519           mxCost = aTo[0].rCost;
114520           mxOut = aTo[0].nRow;
114521           for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
114522             if( pTo->rCost>mxCost || (pTo->rCost==mxCost && pTo->nRow>mxOut) ){
114523               mxCost = pTo->rCost;
114524               mxOut = pTo->nRow;
114525               mxI = jj;
114526             }
114527           }
114528         }
114529       }
114530     }
114531 
114532 #ifdef WHERETRACE_ENABLED  /* >=2 */
114533     if( sqlite3WhereTrace>=2 ){
114534       sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
114535       for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
114536         sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
114537            wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
114538            pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
114539         if( pTo->isOrderedValid && pTo->isOrdered ){
114540           sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
114541         }else{
114542           sqlite3DebugPrintf("\n");
114543         }
114544       }
114545     }
114546 #endif
114547 
114548     /* Swap the roles of aFrom and aTo for the next generation */
114549     pFrom = aTo;
114550     aTo = aFrom;
114551     aFrom = pFrom;
114552     nFrom = nTo;
114553   }
114554 
114555   if( nFrom==0 ){
114556     sqlite3ErrorMsg(pParse, "no query solution");
114557     sqlite3DbFree(db, pSpace);
114558     return SQLITE_ERROR;
114559   }
114560 
114561   /* Find the lowest cost path.  pFrom will be left pointing to that path */
114562   pFrom = aFrom;
114563   for(ii=1; ii<nFrom; ii++){
114564     if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
114565   }
114566   assert( pWInfo->nLevel==nLoop );
114567   /* Load the lowest cost path into pWInfo */
114568   for(iLoop=0; iLoop<nLoop; iLoop++){
114569     WhereLevel *pLevel = pWInfo->a + iLoop;
114570     pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
114571     pLevel->iFrom = pWLoop->iTab;
114572     pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
114573   }
114574   if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
114575    && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
114576    && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
114577    && nRowEst
114578   ){
114579     Bitmask notUsed;
114580     int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
114581                  WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
114582     if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
114583   }
114584   if( pFrom->isOrdered ){
114585     if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
114586       pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
114587     }else{
114588       pWInfo->bOBSat = 1;
114589       pWInfo->revMask = pFrom->revLoop;
114590     }
114591   }
114592   pWInfo->nRowOut = pFrom->nRow;
114593 
114594   /* Free temporary memory and return success */
114595   sqlite3DbFree(db, pSpace);
114596   return SQLITE_OK;
114597 }
114598 
114599 /*
114600 ** Most queries use only a single table (they are not joins) and have
114601 ** simple == constraints against indexed fields.  This routine attempts
114602 ** to plan those simple cases using much less ceremony than the
114603 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
114604 ** times for the common case.
114605 **
114606 ** Return non-zero on success, if this query can be handled by this
114607 ** no-frills query planner.  Return zero if this query needs the
114608 ** general-purpose query planner.
114609 */
114610 static int whereShortCut(WhereLoopBuilder *pBuilder){
114611   WhereInfo *pWInfo;
114612   struct SrcList_item *pItem;
114613   WhereClause *pWC;
114614   WhereTerm *pTerm;
114615   WhereLoop *pLoop;
114616   int iCur;
114617   int j;
114618   Table *pTab;
114619   Index *pIdx;
114620 
114621   pWInfo = pBuilder->pWInfo;
114622   if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
114623   assert( pWInfo->pTabList->nSrc>=1 );
114624   pItem = pWInfo->pTabList->a;
114625   pTab = pItem->pTab;
114626   if( IsVirtual(pTab) ) return 0;
114627   if( pItem->zIndex ) return 0;
114628   iCur = pItem->iCursor;
114629   pWC = &pWInfo->sWC;
114630   pLoop = pBuilder->pNew;
114631   pLoop->wsFlags = 0;
114632   pLoop->u.btree.nSkip = 0;
114633   pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
114634   if( pTerm ){
114635     pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
114636     pLoop->aLTerm[0] = pTerm;
114637     pLoop->nLTerm = 1;
114638     pLoop->u.btree.nEq = 1;
114639     /* TUNING: Cost of a rowid lookup is 10 */
114640     pLoop->rRun = 33;  /* 33==sqlite3LogEst(10) */
114641   }else{
114642     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
114643       assert( pLoop->aLTermSpace==pLoop->aLTerm );
114644       assert( ArraySize(pLoop->aLTermSpace)==4 );
114645       if( pIdx->onError==OE_None
114646        || pIdx->pPartIdxWhere!=0
114647        || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
114648       ) continue;
114649       for(j=0; j<pIdx->nKeyCol; j++){
114650         pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
114651         if( pTerm==0 ) break;
114652         pLoop->aLTerm[j] = pTerm;
114653       }
114654       if( j!=pIdx->nKeyCol ) continue;
114655       pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
114656       if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
114657         pLoop->wsFlags |= WHERE_IDX_ONLY;
114658       }
114659       pLoop->nLTerm = j;
114660       pLoop->u.btree.nEq = j;
114661       pLoop->u.btree.pIndex = pIdx;
114662       /* TUNING: Cost of a unique index lookup is 15 */
114663       pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
114664       break;
114665     }
114666   }
114667   if( pLoop->wsFlags ){
114668     pLoop->nOut = (LogEst)1;
114669     pWInfo->a[0].pWLoop = pLoop;
114670     pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
114671     pWInfo->a[0].iTabCur = iCur;
114672     pWInfo->nRowOut = 1;
114673     if( pWInfo->pOrderBy ) pWInfo->bOBSat =  1;
114674     if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
114675       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114676     }
114677 #ifdef SQLITE_DEBUG
114678     pLoop->cId = '0';
114679 #endif
114680     return 1;
114681   }
114682   return 0;
114683 }
114684 
114685 /*
114686 ** Generate the beginning of the loop used for WHERE clause processing.
114687 ** The return value is a pointer to an opaque structure that contains
114688 ** information needed to terminate the loop.  Later, the calling routine
114689 ** should invoke sqlite3WhereEnd() with the return value of this function
114690 ** in order to complete the WHERE clause processing.
114691 **
114692 ** If an error occurs, this routine returns NULL.
114693 **
114694 ** The basic idea is to do a nested loop, one loop for each table in
114695 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
114696 ** same as a SELECT with only a single table in the FROM clause.)  For
114697 ** example, if the SQL is this:
114698 **
114699 **       SELECT * FROM t1, t2, t3 WHERE ...;
114700 **
114701 ** Then the code generated is conceptually like the following:
114702 **
114703 **      foreach row1 in t1 do       \    Code generated
114704 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
114705 **          foreach row3 in t3 do   /
114706 **            ...
114707 **          end                     \    Code generated
114708 **        end                        |-- by sqlite3WhereEnd()
114709 **      end                         /
114710 **
114711 ** Note that the loops might not be nested in the order in which they
114712 ** appear in the FROM clause if a different order is better able to make
114713 ** use of indices.  Note also that when the IN operator appears in
114714 ** the WHERE clause, it might result in additional nested loops for
114715 ** scanning through all values on the right-hand side of the IN.
114716 **
114717 ** There are Btree cursors associated with each table.  t1 uses cursor
114718 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
114719 ** And so forth.  This routine generates code to open those VDBE cursors
114720 ** and sqlite3WhereEnd() generates the code to close them.
114721 **
114722 ** The code that sqlite3WhereBegin() generates leaves the cursors named
114723 ** in pTabList pointing at their appropriate entries.  The [...] code
114724 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
114725 ** data from the various tables of the loop.
114726 **
114727 ** If the WHERE clause is empty, the foreach loops must each scan their
114728 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
114729 ** the tables have indices and there are terms in the WHERE clause that
114730 ** refer to those indices, a complete table scan can be avoided and the
114731 ** code will run much faster.  Most of the work of this routine is checking
114732 ** to see if there are indices that can be used to speed up the loop.
114733 **
114734 ** Terms of the WHERE clause are also used to limit which rows actually
114735 ** make it to the "..." in the middle of the loop.  After each "foreach",
114736 ** terms of the WHERE clause that use only terms in that loop and outer
114737 ** loops are evaluated and if false a jump is made around all subsequent
114738 ** inner loops (or around the "..." if the test occurs within the inner-
114739 ** most loop)
114740 **
114741 ** OUTER JOINS
114742 **
114743 ** An outer join of tables t1 and t2 is conceptally coded as follows:
114744 **
114745 **    foreach row1 in t1 do
114746 **      flag = 0
114747 **      foreach row2 in t2 do
114748 **        start:
114749 **          ...
114750 **          flag = 1
114751 **      end
114752 **      if flag==0 then
114753 **        move the row2 cursor to a null row
114754 **        goto start
114755 **      fi
114756 **    end
114757 **
114758 ** ORDER BY CLAUSE PROCESSING
114759 **
114760 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
114761 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
114762 ** if there is one.  If there is no ORDER BY clause or if this routine
114763 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
114764 **
114765 ** The iIdxCur parameter is the cursor number of an index.  If
114766 ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index
114767 ** to use for OR clause processing.  The WHERE clause should use this
114768 ** specific cursor.  If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
114769 ** the first cursor in an array of cursors for all indices.  iIdxCur should
114770 ** be used to compute the appropriate cursor depending on which index is
114771 ** used.
114772 */
114773 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
114774   Parse *pParse,        /* The parser context */
114775   SrcList *pTabList,    /* FROM clause: A list of all tables to be scanned */
114776   Expr *pWhere,         /* The WHERE clause */
114777   ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
114778   ExprList *pResultSet, /* Result set of the query */
114779   u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
114780   int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
114781 ){
114782   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
114783   int nTabList;              /* Number of elements in pTabList */
114784   WhereInfo *pWInfo;         /* Will become the return value of this function */
114785   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
114786   Bitmask notReady;          /* Cursors that are not yet positioned */
114787   WhereLoopBuilder sWLB;     /* The WhereLoop builder */
114788   WhereMaskSet *pMaskSet;    /* The expression mask set */
114789   WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
114790   WhereLoop *pLoop;          /* Pointer to a single WhereLoop object */
114791   int ii;                    /* Loop counter */
114792   sqlite3 *db;               /* Database connection */
114793   int rc;                    /* Return code */
114794 
114795 
114796   /* Variable initialization */
114797   db = pParse->db;
114798   memset(&sWLB, 0, sizeof(sWLB));
114799   sWLB.pOrderBy = pOrderBy;
114800 
114801   /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
114802   ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
114803   if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
114804     wctrlFlags &= ~WHERE_WANT_DISTINCT;
114805   }
114806 
114807   /* The number of tables in the FROM clause is limited by the number of
114808   ** bits in a Bitmask
114809   */
114810   testcase( pTabList->nSrc==BMS );
114811   if( pTabList->nSrc>BMS ){
114812     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
114813     return 0;
114814   }
114815 
114816   /* This function normally generates a nested loop for all tables in
114817   ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
114818   ** only generate code for the first table in pTabList and assume that
114819   ** any cursors associated with subsequent tables are uninitialized.
114820   */
114821   nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
114822 
114823   /* Allocate and initialize the WhereInfo structure that will become the
114824   ** return value. A single allocation is used to store the WhereInfo
114825   ** struct, the contents of WhereInfo.a[], the WhereClause structure
114826   ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
114827   ** field (type Bitmask) it must be aligned on an 8-byte boundary on
114828   ** some architectures. Hence the ROUND8() below.
114829   */
114830   nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
114831   pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
114832   if( db->mallocFailed ){
114833     sqlite3DbFree(db, pWInfo);
114834     pWInfo = 0;
114835     goto whereBeginError;
114836   }
114837   pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
114838   pWInfo->nLevel = nTabList;
114839   pWInfo->pParse = pParse;
114840   pWInfo->pTabList = pTabList;
114841   pWInfo->pOrderBy = pOrderBy;
114842   pWInfo->pResultSet = pResultSet;
114843   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
114844   pWInfo->wctrlFlags = wctrlFlags;
114845   pWInfo->savedNQueryLoop = pParse->nQueryLoop;
114846   pMaskSet = &pWInfo->sMaskSet;
114847   sWLB.pWInfo = pWInfo;
114848   sWLB.pWC = &pWInfo->sWC;
114849   sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
114850   assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
114851   whereLoopInit(sWLB.pNew);
114852 #ifdef SQLITE_DEBUG
114853   sWLB.pNew->cId = '*';
114854 #endif
114855 
114856   /* Split the WHERE clause into separate subexpressions where each
114857   ** subexpression is separated by an AND operator.
114858   */
114859   initMaskSet(pMaskSet);
114860   whereClauseInit(&pWInfo->sWC, pWInfo);
114861   whereSplit(&pWInfo->sWC, pWhere, TK_AND);
114862 
114863   /* Special case: a WHERE clause that is constant.  Evaluate the
114864   ** expression and either jump over all of the code or fall thru.
114865   */
114866   for(ii=0; ii<sWLB.pWC->nTerm; ii++){
114867     if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
114868       sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
114869                          SQLITE_JUMPIFNULL);
114870       sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
114871     }
114872   }
114873 
114874   /* Special case: No FROM clause
114875   */
114876   if( nTabList==0 ){
114877     if( pOrderBy ) pWInfo->bOBSat = 1;
114878     if( wctrlFlags & WHERE_WANT_DISTINCT ){
114879       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114880     }
114881   }
114882 
114883   /* Assign a bit from the bitmask to every term in the FROM clause.
114884   **
114885   ** When assigning bitmask values to FROM clause cursors, it must be
114886   ** the case that if X is the bitmask for the N-th FROM clause term then
114887   ** the bitmask for all FROM clause terms to the left of the N-th term
114888   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
114889   ** its Expr.iRightJoinTable value to find the bitmask of the right table
114890   ** of the join.  Subtracting one from the right table bitmask gives a
114891   ** bitmask for all tables to the left of the join.  Knowing the bitmask
114892   ** for all tables to the left of a left join is important.  Ticket #3015.
114893   **
114894   ** Note that bitmasks are created for all pTabList->nSrc tables in
114895   ** pTabList, not just the first nTabList tables.  nTabList is normally
114896   ** equal to pTabList->nSrc but might be shortened to 1 if the
114897   ** WHERE_ONETABLE_ONLY flag is set.
114898   */
114899   for(ii=0; ii<pTabList->nSrc; ii++){
114900     createMask(pMaskSet, pTabList->a[ii].iCursor);
114901   }
114902 #ifndef NDEBUG
114903   {
114904     Bitmask toTheLeft = 0;
114905     for(ii=0; ii<pTabList->nSrc; ii++){
114906       Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
114907       assert( (m-1)==toTheLeft );
114908       toTheLeft |= m;
114909     }
114910   }
114911 #endif
114912 
114913   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
114914   ** add new virtual terms onto the end of the WHERE clause.  We do not
114915   ** want to analyze these virtual terms, so start analyzing at the end
114916   ** and work forward so that the added virtual terms are never processed.
114917   */
114918   exprAnalyzeAll(pTabList, &pWInfo->sWC);
114919   if( db->mallocFailed ){
114920     goto whereBeginError;
114921   }
114922 
114923   if( wctrlFlags & WHERE_WANT_DISTINCT ){
114924     if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
114925       /* The DISTINCT marking is pointless.  Ignore it. */
114926       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114927     }else if( pOrderBy==0 ){
114928       /* Try to ORDER BY the result set to make distinct processing easier */
114929       pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
114930       pWInfo->pOrderBy = pResultSet;
114931     }
114932   }
114933 
114934   /* Construct the WhereLoop objects */
114935   WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
114936   /* Display all terms of the WHERE clause */
114937 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
114938   if( sqlite3WhereTrace & 0x100 ){
114939     int i;
114940     Vdbe *v = pParse->pVdbe;
114941     sqlite3ExplainBegin(v);
114942     for(i=0; i<sWLB.pWC->nTerm; i++){
114943       sqlite3ExplainPrintf(v, "#%-2d ", i);
114944       sqlite3ExplainPush(v);
114945       whereExplainTerm(v, &sWLB.pWC->a[i]);
114946       sqlite3ExplainPop(v);
114947       sqlite3ExplainNL(v);
114948     }
114949     sqlite3ExplainFinish(v);
114950     sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
114951   }
114952 #endif
114953   if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
114954     rc = whereLoopAddAll(&sWLB);
114955     if( rc ) goto whereBeginError;
114956 
114957     /* Display all of the WhereLoop objects if wheretrace is enabled */
114958 #ifdef WHERETRACE_ENABLED /* !=0 */
114959     if( sqlite3WhereTrace ){
114960       WhereLoop *p;
114961       int i;
114962       static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
114963                                        "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
114964       for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
114965         p->cId = zLabel[i%sizeof(zLabel)];
114966         whereLoopPrint(p, sWLB.pWC);
114967       }
114968     }
114969 #endif
114970 
114971     wherePathSolver(pWInfo, 0);
114972     if( db->mallocFailed ) goto whereBeginError;
114973     if( pWInfo->pOrderBy ){
114974        wherePathSolver(pWInfo, pWInfo->nRowOut+1);
114975        if( db->mallocFailed ) goto whereBeginError;
114976     }
114977   }
114978   if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
114979      pWInfo->revMask = (Bitmask)(-1);
114980   }
114981   if( pParse->nErr || NEVER(db->mallocFailed) ){
114982     goto whereBeginError;
114983   }
114984 #ifdef WHERETRACE_ENABLED /* !=0 */
114985   if( sqlite3WhereTrace ){
114986     int ii;
114987     sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
114988     if( pWInfo->bOBSat ){
114989       sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask);
114990     }
114991     switch( pWInfo->eDistinct ){
114992       case WHERE_DISTINCT_UNIQUE: {
114993         sqlite3DebugPrintf("  DISTINCT=unique");
114994         break;
114995       }
114996       case WHERE_DISTINCT_ORDERED: {
114997         sqlite3DebugPrintf("  DISTINCT=ordered");
114998         break;
114999       }
115000       case WHERE_DISTINCT_UNORDERED: {
115001         sqlite3DebugPrintf("  DISTINCT=unordered");
115002         break;
115003       }
115004     }
115005     sqlite3DebugPrintf("\n");
115006     for(ii=0; ii<pWInfo->nLevel; ii++){
115007       whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
115008     }
115009   }
115010 #endif
115011   /* Attempt to omit tables from the join that do not effect the result */
115012   if( pWInfo->nLevel>=2
115013    && pResultSet!=0
115014    && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
115015   ){
115016     Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
115017     if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
115018     while( pWInfo->nLevel>=2 ){
115019       WhereTerm *pTerm, *pEnd;
115020       pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
115021       if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
115022       if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
115023        && (pLoop->wsFlags & WHERE_ONEROW)==0
115024       ){
115025         break;
115026       }
115027       if( (tabUsed & pLoop->maskSelf)!=0 ) break;
115028       pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
115029       for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
115030         if( (pTerm->prereqAll & pLoop->maskSelf)!=0
115031          && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
115032         ){
115033           break;
115034         }
115035       }
115036       if( pTerm<pEnd ) break;
115037       WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
115038       pWInfo->nLevel--;
115039       nTabList--;
115040     }
115041   }
115042   WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
115043   pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
115044 
115045   /* If the caller is an UPDATE or DELETE statement that is requesting
115046   ** to use a one-pass algorithm, determine if this is appropriate.
115047   ** The one-pass algorithm only works if the WHERE clause constrains
115048   ** the statement to update a single row.
115049   */
115050   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
115051   if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
115052    && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
115053     pWInfo->okOnePass = 1;
115054     if( HasRowid(pTabList->a[0].pTab) ){
115055       pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
115056     }
115057   }
115058 
115059   /* Open all tables in the pTabList and any indices selected for
115060   ** searching those tables.
115061   */
115062   notReady = ~(Bitmask)0;
115063   for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
115064     Table *pTab;     /* Table to open */
115065     int iDb;         /* Index of database containing table/index */
115066     struct SrcList_item *pTabItem;
115067 
115068     pTabItem = &pTabList->a[pLevel->iFrom];
115069     pTab = pTabItem->pTab;
115070     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
115071     pLoop = pLevel->pWLoop;
115072     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
115073       /* Do nothing */
115074     }else
115075 #ifndef SQLITE_OMIT_VIRTUALTABLE
115076     if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
115077       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
115078       int iCur = pTabItem->iCursor;
115079       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
115080     }else if( IsVirtual(pTab) ){
115081       /* noop */
115082     }else
115083 #endif
115084     if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
115085          && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
115086       int op = OP_OpenRead;
115087       if( pWInfo->okOnePass ){
115088         op = OP_OpenWrite;
115089         pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
115090       };
115091       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
115092       assert( pTabItem->iCursor==pLevel->iTabCur );
115093       testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
115094       testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
115095       if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){
115096         Bitmask b = pTabItem->colUsed;
115097         int n = 0;
115098         for(; b; b=b>>1, n++){}
115099         sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
115100                             SQLITE_INT_TO_PTR(n), P4_INT32);
115101         assert( n<=pTab->nCol );
115102       }
115103     }else{
115104       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
115105     }
115106     if( pLoop->wsFlags & WHERE_INDEXED ){
115107       Index *pIx = pLoop->u.btree.pIndex;
115108       int iIndexCur;
115109       int op = OP_OpenRead;
115110       /* iIdxCur is always set if to a positive value if ONEPASS is possible */
115111       assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
115112       if( pWInfo->okOnePass ){
115113         Index *pJ = pTabItem->pTab->pIndex;
115114         iIndexCur = iIdxCur;
115115         assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
115116         while( ALWAYS(pJ) && pJ!=pIx ){
115117           iIndexCur++;
115118           pJ = pJ->pNext;
115119         }
115120         op = OP_OpenWrite;
115121         pWInfo->aiCurOnePass[1] = iIndexCur;
115122       }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
115123         iIndexCur = iIdxCur;
115124       }else{
115125         iIndexCur = pParse->nTab++;
115126       }
115127       pLevel->iIdxCur = iIndexCur;
115128       assert( pIx->pSchema==pTab->pSchema );
115129       assert( iIndexCur>=0 );
115130       sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
115131       sqlite3VdbeSetP4KeyInfo(pParse, pIx);
115132       VdbeComment((v, "%s", pIx->zName));
115133     }
115134     if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
115135     notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
115136   }
115137   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
115138   if( db->mallocFailed ) goto whereBeginError;
115139 
115140   /* Generate the code to do the search.  Each iteration of the for
115141   ** loop below generates code for a single nested loop of the VM
115142   ** program.
115143   */
115144   notReady = ~(Bitmask)0;
115145   for(ii=0; ii<nTabList; ii++){
115146     pLevel = &pWInfo->a[ii];
115147 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
115148     if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
115149       constructAutomaticIndex(pParse, &pWInfo->sWC,
115150                 &pTabList->a[pLevel->iFrom], notReady, pLevel);
115151       if( db->mallocFailed ) goto whereBeginError;
115152     }
115153 #endif
115154     explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
115155     pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
115156     notReady = codeOneLoopStart(pWInfo, ii, notReady);
115157     pWInfo->iContinue = pLevel->addrCont;
115158   }
115159 
115160   /* Done. */
115161   VdbeModuleComment((v, "Begin WHERE-core"));
115162   return pWInfo;
115163 
115164   /* Jump here if malloc fails */
115165 whereBeginError:
115166   if( pWInfo ){
115167     pParse->nQueryLoop = pWInfo->savedNQueryLoop;
115168     whereInfoFree(db, pWInfo);
115169   }
115170   return 0;
115171 }
115172 
115173 /*
115174 ** Generate the end of the WHERE loop.  See comments on
115175 ** sqlite3WhereBegin() for additional information.
115176 */
115177 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
115178   Parse *pParse = pWInfo->pParse;
115179   Vdbe *v = pParse->pVdbe;
115180   int i;
115181   WhereLevel *pLevel;
115182   WhereLoop *pLoop;
115183   SrcList *pTabList = pWInfo->pTabList;
115184   sqlite3 *db = pParse->db;
115185 
115186   /* Generate loop termination code.
115187   */
115188   VdbeModuleComment((v, "End WHERE-core"));
115189   sqlite3ExprCacheClear(pParse);
115190   for(i=pWInfo->nLevel-1; i>=0; i--){
115191     int addr;
115192     pLevel = &pWInfo->a[i];
115193     pLoop = pLevel->pWLoop;
115194     sqlite3VdbeResolveLabel(v, pLevel->addrCont);
115195     if( pLevel->op!=OP_Noop ){
115196       sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);
115197       sqlite3VdbeChangeP5(v, pLevel->p5);
115198       VdbeCoverage(v);
115199       VdbeCoverageIf(v, pLevel->op==OP_Next);
115200       VdbeCoverageIf(v, pLevel->op==OP_Prev);
115201       VdbeCoverageIf(v, pLevel->op==OP_VNext);
115202     }
115203     if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
115204       struct InLoop *pIn;
115205       int j;
115206       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
115207       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
115208         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
115209         sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
115210         VdbeCoverage(v);
115211         VdbeCoverageIf(v, pIn->eEndLoopOp==OP_PrevIfOpen);
115212         VdbeCoverageIf(v, pIn->eEndLoopOp==OP_NextIfOpen);
115213         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
115214       }
115215       sqlite3DbFree(db, pLevel->u.in.aInLoop);
115216     }
115217     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
115218     if( pLevel->addrSkip ){
115219       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
115220       VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
115221       sqlite3VdbeJumpHere(v, pLevel->addrSkip);
115222       sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
115223     }
115224     if( pLevel->iLeftJoin ){
115225       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); VdbeCoverage(v);
115226       assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
115227            || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
115228       if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
115229         sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
115230       }
115231       if( pLoop->wsFlags & WHERE_INDEXED ){
115232         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
115233       }
115234       if( pLevel->op==OP_Return ){
115235         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
115236       }else{
115237         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
115238       }
115239       sqlite3VdbeJumpHere(v, addr);
115240     }
115241     VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
115242                      pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
115243   }
115244 
115245   /* The "break" point is here, just past the end of the outer loop.
115246   ** Set it.
115247   */
115248   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
115249 
115250   assert( pWInfo->nLevel<=pTabList->nSrc );
115251   for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
115252     int k, last;
115253     VdbeOp *pOp;
115254     Index *pIdx = 0;
115255     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
115256     Table *pTab = pTabItem->pTab;
115257     assert( pTab!=0 );
115258     pLoop = pLevel->pWLoop;
115259 
115260     /* For a co-routine, change all OP_Column references to the table of
115261     ** the co-routine into OP_SCopy of result contained in a register.
115262     ** OP_Rowid becomes OP_Null.
115263     */
115264     if( pTabItem->viaCoroutine && !db->mallocFailed ){
115265       last = sqlite3VdbeCurrentAddr(v);
115266       k = pLevel->addrBody;
115267       pOp = sqlite3VdbeGetOp(v, k);
115268       for(; k<last; k++, pOp++){
115269         if( pOp->p1!=pLevel->iTabCur ) continue;
115270         if( pOp->opcode==OP_Column ){
115271           pOp->opcode = OP_SCopy;
115272           pOp->p1 = pOp->p2 + pTabItem->regResult;
115273           pOp->p2 = pOp->p3;
115274           pOp->p3 = 0;
115275         }else if( pOp->opcode==OP_Rowid ){
115276           pOp->opcode = OP_Null;
115277           pOp->p1 = 0;
115278           pOp->p3 = 0;
115279         }
115280       }
115281       continue;
115282     }
115283 
115284     /* Close all of the cursors that were opened by sqlite3WhereBegin.
115285     ** Except, do not close cursors that will be reused by the OR optimization
115286     ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
115287     ** created for the ONEPASS optimization.
115288     */
115289     if( (pTab->tabFlags & TF_Ephemeral)==0
115290      && pTab->pSelect==0
115291      && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
115292     ){
115293       int ws = pLoop->wsFlags;
115294       if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
115295         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
115296       }
115297       if( (ws & WHERE_INDEXED)!=0
115298        && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
115299        && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
115300       ){
115301         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
115302       }
115303     }
115304 
115305     /* If this scan uses an index, make VDBE code substitutions to read data
115306     ** from the index instead of from the table where possible.  In some cases
115307     ** this optimization prevents the table from ever being read, which can
115308     ** yield a significant performance boost.
115309     **
115310     ** Calls to the code generator in between sqlite3WhereBegin and
115311     ** sqlite3WhereEnd will have created code that references the table
115312     ** directly.  This loop scans all that code looking for opcodes
115313     ** that reference the table and converts them into opcodes that
115314     ** reference the index.
115315     */
115316     if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
115317       pIdx = pLoop->u.btree.pIndex;
115318     }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
115319       pIdx = pLevel->u.pCovidx;
115320     }
115321     if( pIdx && !db->mallocFailed ){
115322       last = sqlite3VdbeCurrentAddr(v);
115323       k = pLevel->addrBody;
115324       pOp = sqlite3VdbeGetOp(v, k);
115325       for(; k<last; k++, pOp++){
115326         if( pOp->p1!=pLevel->iTabCur ) continue;
115327         if( pOp->opcode==OP_Column ){
115328           int x = pOp->p2;
115329           assert( pIdx->pTable==pTab );
115330           if( !HasRowid(pTab) ){
115331             Index *pPk = sqlite3PrimaryKeyIndex(pTab);
115332             x = pPk->aiColumn[x];
115333           }
115334           x = sqlite3ColumnOfIndex(pIdx, x);
115335           if( x>=0 ){
115336             pOp->p2 = x;
115337             pOp->p1 = pLevel->iIdxCur;
115338           }
115339           assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
115340         }else if( pOp->opcode==OP_Rowid ){
115341           pOp->p1 = pLevel->iIdxCur;
115342           pOp->opcode = OP_IdxRowid;
115343         }
115344       }
115345     }
115346   }
115347 
115348   /* Final cleanup
115349   */
115350   pParse->nQueryLoop = pWInfo->savedNQueryLoop;
115351   whereInfoFree(db, pWInfo);
115352   return;
115353 }
115354 
115355 /************** End of where.c ***********************************************/
115356 /************** Begin file parse.c *******************************************/
115357 /* Driver template for the LEMON parser generator.
115358 ** The author disclaims copyright to this source code.
115359 **
115360 ** This version of "lempar.c" is modified, slightly, for use by SQLite.
115361 ** The only modifications are the addition of a couple of NEVER()
115362 ** macros to disable tests that are needed in the case of a general
115363 ** LALR(1) grammar but which are always false in the
115364 ** specific grammar used by SQLite.
115365 */
115366 /* First off, code is included that follows the "include" declaration
115367 ** in the input grammar file. */
115368 /* #include <stdio.h> */
115369 
115370 
115371 /*
115372 ** Disable all error recovery processing in the parser push-down
115373 ** automaton.
115374 */
115375 #define YYNOERRORRECOVERY 1
115376 
115377 /*
115378 ** Make yytestcase() the same as testcase()
115379 */
115380 #define yytestcase(X) testcase(X)
115381 
115382 /*
115383 ** An instance of this structure holds information about the
115384 ** LIMIT clause of a SELECT statement.
115385 */
115386 struct LimitVal {
115387   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
115388   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
115389 };
115390 
115391 /*
115392 ** An instance of this structure is used to store the LIKE,
115393 ** GLOB, NOT LIKE, and NOT GLOB operators.
115394 */
115395 struct LikeOp {
115396   Token eOperator;  /* "like" or "glob" or "regexp" */
115397   int bNot;         /* True if the NOT keyword is present */
115398 };
115399 
115400 /*
115401 ** An instance of the following structure describes the event of a
115402 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
115403 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
115404 **
115405 **      UPDATE ON (a,b,c)
115406 **
115407 ** Then the "b" IdList records the list "a,b,c".
115408 */
115409 struct TrigEvent { int a; IdList * b; };
115410 
115411 /*
115412 ** An instance of this structure holds the ATTACH key and the key type.
115413 */
115414 struct AttachKey { int type;  Token key; };
115415 
115416 
115417   /* This is a utility routine used to set the ExprSpan.zStart and
115418   ** ExprSpan.zEnd values of pOut so that the span covers the complete
115419   ** range of text beginning with pStart and going to the end of pEnd.
115420   */
115421   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
115422     pOut->zStart = pStart->z;
115423     pOut->zEnd = &pEnd->z[pEnd->n];
115424   }
115425 
115426   /* Construct a new Expr object from a single identifier.  Use the
115427   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
115428   ** that created the expression.
115429   */
115430   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
115431     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
115432     pOut->zStart = pValue->z;
115433     pOut->zEnd = &pValue->z[pValue->n];
115434   }
115435 
115436   /* This routine constructs a binary expression node out of two ExprSpan
115437   ** objects and uses the result to populate a new ExprSpan object.
115438   */
115439   static void spanBinaryExpr(
115440     ExprSpan *pOut,     /* Write the result here */
115441     Parse *pParse,      /* The parsing context.  Errors accumulate here */
115442     int op,             /* The binary operation */
115443     ExprSpan *pLeft,    /* The left operand */
115444     ExprSpan *pRight    /* The right operand */
115445   ){
115446     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
115447     pOut->zStart = pLeft->zStart;
115448     pOut->zEnd = pRight->zEnd;
115449   }
115450 
115451   /* Construct an expression node for a unary postfix operator
115452   */
115453   static void spanUnaryPostfix(
115454     ExprSpan *pOut,        /* Write the new expression node here */
115455     Parse *pParse,         /* Parsing context to record errors */
115456     int op,                /* The operator */
115457     ExprSpan *pOperand,    /* The operand */
115458     Token *pPostOp         /* The operand token for setting the span */
115459   ){
115460     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
115461     pOut->zStart = pOperand->zStart;
115462     pOut->zEnd = &pPostOp->z[pPostOp->n];
115463   }
115464 
115465   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
115466   ** unary TK_ISNULL or TK_NOTNULL expression. */
115467   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
115468     sqlite3 *db = pParse->db;
115469     if( db->mallocFailed==0 && pY->op==TK_NULL ){
115470       pA->op = (u8)op;
115471       sqlite3ExprDelete(db, pA->pRight);
115472       pA->pRight = 0;
115473     }
115474   }
115475 
115476   /* Construct an expression node for a unary prefix operator
115477   */
115478   static void spanUnaryPrefix(
115479     ExprSpan *pOut,        /* Write the new expression node here */
115480     Parse *pParse,         /* Parsing context to record errors */
115481     int op,                /* The operator */
115482     ExprSpan *pOperand,    /* The operand */
115483     Token *pPreOp         /* The operand token for setting the span */
115484   ){
115485     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
115486     pOut->zStart = pPreOp->z;
115487     pOut->zEnd = pOperand->zEnd;
115488   }
115489 /* Next is all token values, in a form suitable for use by makeheaders.
115490 ** This section will be null unless lemon is run with the -m switch.
115491 */
115492 /*
115493 ** These constants (all generated automatically by the parser generator)
115494 ** specify the various kinds of tokens (terminals) that the parser
115495 ** understands.
115496 **
115497 ** Each symbol here is a terminal symbol in the grammar.
115498 */
115499 /* Make sure the INTERFACE macro is defined.
115500 */
115501 #ifndef INTERFACE
115502 # define INTERFACE 1
115503 #endif
115504 /* The next thing included is series of defines which control
115505 ** various aspects of the generated parser.
115506 **    YYCODETYPE         is the data type used for storing terminal
115507 **                       and nonterminal numbers.  "unsigned char" is
115508 **                       used if there are fewer than 250 terminals
115509 **                       and nonterminals.  "int" is used otherwise.
115510 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
115511 **                       to no legal terminal or nonterminal number.  This
115512 **                       number is used to fill in empty slots of the hash
115513 **                       table.
115514 **    YYFALLBACK         If defined, this indicates that one or more tokens
115515 **                       have fall-back values which should be used if the
115516 **                       original value of the token will not parse.
115517 **    YYACTIONTYPE       is the data type used for storing terminal
115518 **                       and nonterminal numbers.  "unsigned char" is
115519 **                       used if there are fewer than 250 rules and
115520 **                       states combined.  "int" is used otherwise.
115521 **    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given
115522 **                       directly to the parser from the tokenizer.
115523 **    YYMINORTYPE        is the data type used for all minor tokens.
115524 **                       This is typically a union of many types, one of
115525 **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
115526 **                       for base tokens is called "yy0".
115527 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
115528 **                       zero the stack is dynamically sized using realloc()
115529 **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
115530 **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
115531 **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
115532 **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
115533 **    YYNSTATE           the combined number of states.
115534 **    YYNRULE            the number of rules in the grammar
115535 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
115536 **                       defined, then do no error processing.
115537 */
115538 #define YYCODETYPE unsigned char
115539 #define YYNOCODE 254
115540 #define YYACTIONTYPE unsigned short int
115541 #define YYWILDCARD 70
115542 #define sqlite3ParserTOKENTYPE Token
115543 typedef union {
115544   int yyinit;
115545   sqlite3ParserTOKENTYPE yy0;
115546   Select* yy3;
115547   ExprList* yy14;
115548   With* yy59;
115549   SrcList* yy65;
115550   struct LikeOp yy96;
115551   Expr* yy132;
115552   u8 yy186;
115553   int yy328;
115554   ExprSpan yy346;
115555   struct TrigEvent yy378;
115556   u16 yy381;
115557   IdList* yy408;
115558   struct {int value; int mask;} yy429;
115559   TriggerStep* yy473;
115560   struct LimitVal yy476;
115561 } YYMINORTYPE;
115562 #ifndef YYSTACKDEPTH
115563 #define YYSTACKDEPTH 100
115564 #endif
115565 #define sqlite3ParserARG_SDECL Parse *pParse;
115566 #define sqlite3ParserARG_PDECL ,Parse *pParse
115567 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
115568 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
115569 #define YYNSTATE 642
115570 #define YYNRULE 327
115571 #define YYFALLBACK 1
115572 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
115573 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
115574 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
115575 
115576 /* The yyzerominor constant is used to initialize instances of
115577 ** YYMINORTYPE objects to zero. */
115578 static const YYMINORTYPE yyzerominor = { 0 };
115579 
115580 /* Define the yytestcase() macro to be a no-op if is not already defined
115581 ** otherwise.
115582 **
115583 ** Applications can choose to define yytestcase() in the %include section
115584 ** to a macro that can assist in verifying code coverage.  For production
115585 ** code the yytestcase() macro should be turned off.  But it is useful
115586 ** for testing.
115587 */
115588 #ifndef yytestcase
115589 # define yytestcase(X)
115590 #endif
115591 
115592 
115593 /* Next are the tables used to determine what action to take based on the
115594 ** current state and lookahead token.  These tables are used to implement
115595 ** functions that take a state number and lookahead value and return an
115596 ** action integer.
115597 **
115598 ** Suppose the action integer is N.  Then the action is determined as
115599 ** follows
115600 **
115601 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
115602 **                                      token onto the stack and goto state N.
115603 **
115604 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
115605 **
115606 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
115607 **
115608 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
115609 **
115610 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
115611 **                                      slots in the yy_action[] table.
115612 **
115613 ** The action table is constructed as a single large table named yy_action[].
115614 ** Given state S and lookahead X, the action is computed as
115615 **
115616 **      yy_action[ yy_shift_ofst[S] + X ]
115617 **
115618 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
115619 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
115620 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
115621 ** and that yy_default[S] should be used instead.
115622 **
115623 ** The formula above is for computing the action when the lookahead is
115624 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
115625 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
115626 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
115627 ** YY_SHIFT_USE_DFLT.
115628 **
115629 ** The following are the tables generated in this section:
115630 **
115631 **  yy_action[]        A single table containing all actions.
115632 **  yy_lookahead[]     A table containing the lookahead for each entry in
115633 **                     yy_action.  Used to detect hash collisions.
115634 **  yy_shift_ofst[]    For each state, the offset into yy_action for
115635 **                     shifting terminals.
115636 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
115637 **                     shifting non-terminals after a reduce.
115638 **  yy_default[]       Default action for each state.
115639 */
115640 #define YY_ACTTAB_COUNT (1497)
115641 static const YYACTIONTYPE yy_action[] = {
115642  /*     0 */   306,  212,  432,  955,  639,  191,  955,  295,  559,   88,
115643  /*    10 */    88,   88,   88,   81,   86,   86,   86,   86,   85,   85,
115644  /*    20 */    84,   84,   84,   83,  330,  185,  184,  183,  635,  635,
115645  /*    30 */   292,  606,  606,   88,   88,   88,   88,  683,   86,   86,
115646  /*    40 */    86,   86,   85,   85,   84,   84,   84,   83,  330,   16,
115647  /*    50 */   436,  597,   89,   90,   80,  600,  599,  601,  601,   87,
115648  /*    60 */    87,   88,   88,   88,   88,  684,   86,   86,   86,   86,
115649  /*    70 */    85,   85,   84,   84,   84,   83,  330,  306,  559,   84,
115650  /*    80 */    84,   84,   83,  330,   65,   86,   86,   86,   86,   85,
115651  /*    90 */    85,   84,   84,   84,   83,  330,  635,  635,  634,  633,
115652  /*   100 */   182,  682,  550,  379,  376,  375,   17,  322,  606,  606,
115653  /*   110 */   371,  198,  479,   91,  374,   82,   79,  165,   85,   85,
115654  /*   120 */    84,   84,   84,   83,  330,  598,  635,  635,  107,   89,
115655  /*   130 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
115656  /*   140 */    88,   88,  186,   86,   86,   86,   86,   85,   85,   84,
115657  /*   150 */    84,   84,   83,  330,  306,  594,  594,  142,  328,  327,
115658  /*   160 */   484,  249,  344,  238,  635,  635,  634,  633,  585,  448,
115659  /*   170 */   526,  525,  229,  388,    1,  394,  450,  584,  449,  635,
115660  /*   180 */   635,  635,  635,  319,  395,  606,  606,  199,  157,  273,
115661  /*   190 */   382,  268,  381,  187,  635,  635,  634,  633,  311,  555,
115662  /*   200 */   266,  593,  593,  266,  347,  588,   89,   90,   80,  600,
115663  /*   210 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  478,
115664  /*   220 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
115665  /*   230 */   330,  306,  272,  536,  634,  633,  146,  610,  197,  310,
115666  /*   240 */   575,  182,  482,  271,  379,  376,  375,  506,   21,  634,
115667  /*   250 */   633,  634,  633,  635,  635,  374,  611,  574,  548,  440,
115668  /*   260 */   111,  563,  606,  606,  634,  633,  324,  479,  608,  608,
115669  /*   270 */   608,  300,  435,  573,  119,  407,  210,  162,  562,  883,
115670  /*   280 */   592,  592,  306,   89,   90,   80,  600,  599,  601,  601,
115671  /*   290 */    87,   87,   88,   88,   88,   88,  506,   86,   86,   86,
115672  /*   300 */    86,   85,   85,   84,   84,   84,   83,  330,  620,  111,
115673  /*   310 */   635,  635,  361,  606,  606,  358,  249,  349,  248,  433,
115674  /*   320 */   243,  479,  586,  634,  633,  195,  611,   93,  119,  221,
115675  /*   330 */   575,  497,  534,  534,   89,   90,   80,  600,  599,  601,
115676  /*   340 */   601,   87,   87,   88,   88,   88,   88,  574,   86,   86,
115677  /*   350 */    86,   86,   85,   85,   84,   84,   84,   83,  330,  306,
115678  /*   360 */    77,  429,  638,  573,  589,  530,  240,  230,  242,  105,
115679  /*   370 */   249,  349,  248,  515,  588,  208,  460,  529,  564,  173,
115680  /*   380 */   634,  633,  970,  144,  430,    2,  424,  228,  380,  557,
115681  /*   390 */   606,  606,  190,  153,  159,  158,  514,   51,  632,  631,
115682  /*   400 */   630,   71,  536,  432,  954,  196,  610,  954,  614,   45,
115683  /*   410 */    18,   89,   90,   80,  600,  599,  601,  601,   87,   87,
115684  /*   420 */    88,   88,   88,   88,  261,   86,   86,   86,   86,   85,
115685  /*   430 */    85,   84,   84,   84,   83,  330,  306,  608,  608,  608,
115686  /*   440 */   542,  424,  402,  385,  241,  506,  451,  320,  211,  543,
115687  /*   450 */   164,  436,  386,  293,  451,  587,  108,  496,  111,  334,
115688  /*   460 */   391,  591,  424,  614,   27,  452,  453,  606,  606,   72,
115689  /*   470 */   257,   70,  259,  452,  339,  342,  564,  582,   68,  415,
115690  /*   480 */   469,  328,  327,   62,  614,   45,  110,  393,   89,   90,
115691  /*   490 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
115692  /*   500 */    88,  152,   86,   86,   86,   86,   85,   85,   84,   84,
115693  /*   510 */    84,   83,  330,  306,  110,  499,  520,  538,  402,  389,
115694  /*   520 */   424,  110,  566,  500,  593,  593,  454,   82,   79,  165,
115695  /*   530 */   424,  591,  384,  564,  340,  615,  188,  162,  424,  350,
115696  /*   540 */   616,  424,  614,   44,  606,  606,  445,  582,  300,  434,
115697  /*   550 */   151,   19,  614,    9,  568,  580,  348,  615,  469,  567,
115698  /*   560 */   614,   26,  616,  614,   45,   89,   90,   80,  600,  599,
115699  /*   570 */   601,  601,   87,   87,   88,   88,   88,   88,  411,   86,
115700  /*   580 */    86,   86,   86,   85,   85,   84,   84,   84,   83,  330,
115701  /*   590 */   306,  579,  110,  578,  521,  282,  433,  398,  400,  255,
115702  /*   600 */   486,   82,   79,  165,  487,  164,   82,   79,  165,  488,
115703  /*   610 */   488,  364,  387,  424,  544,  544,  509,  350,  362,  155,
115704  /*   620 */   191,  606,  606,  559,  642,  640,  333,   82,   79,  165,
115705  /*   630 */   305,  564,  507,  312,  357,  614,   45,  329,  596,  595,
115706  /*   640 */   194,  337,   89,   90,   80,  600,  599,  601,  601,   87,
115707  /*   650 */    87,   88,   88,   88,   88,  424,   86,   86,   86,   86,
115708  /*   660 */    85,   85,   84,   84,   84,   83,  330,  306,   20,  323,
115709  /*   670 */   150,  263,  211,  543,  421,  596,  595,  614,   22,  424,
115710  /*   680 */   193,  424,  284,  424,  391,  424,  509,  424,  577,  424,
115711  /*   690 */   186,  335,  424,  559,  424,  313,  120,  546,  606,  606,
115712  /*   700 */    67,  614,   47,  614,   50,  614,   48,  614,  100,  614,
115713  /*   710 */    99,  614,  101,  576,  614,  102,  614,  109,  326,   89,
115714  /*   720 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
115715  /*   730 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
115716  /*   740 */    84,   84,   83,  330,  306,  424,  311,  424,  585,   54,
115717  /*   750 */   424,  516,  517,  590,  614,  112,  424,  584,  424,  572,
115718  /*   760 */   424,  195,  424,  571,  424,   67,  424,  614,   94,  614,
115719  /*   770 */    98,  424,  614,   97,  264,  606,  606,  195,  614,   46,
115720  /*   780 */   614,   96,  614,   30,  614,   49,  614,  115,  614,  114,
115721  /*   790 */   418,  229,  388,  614,  113,  306,   89,   90,   80,  600,
115722  /*   800 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  424,
115723  /*   810 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
115724  /*   820 */   330,  119,  424,  590,  110,  372,  606,  606,  195,   53,
115725  /*   830 */   250,  614,   29,  195,  472,  438,  729,  190,  302,  498,
115726  /*   840 */    14,  523,  641,    2,  614,   43,  306,   89,   90,   80,
115727  /*   850 */   600,  599,  601,  601,   87,   87,   88,   88,   88,   88,
115728  /*   860 */   424,   86,   86,   86,   86,   85,   85,   84,   84,   84,
115729  /*   870 */    83,  330,  424,  613,  964,  964,  354,  606,  606,  420,
115730  /*   880 */   312,   64,  614,   42,  391,  355,  283,  437,  301,  255,
115731  /*   890 */   414,  410,  495,  492,  614,   28,  471,  306,   89,   90,
115732  /*   900 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
115733  /*   910 */    88,  424,   86,   86,   86,   86,   85,   85,   84,   84,
115734  /*   920 */    84,   83,  330,  424,  110,  110,  110,  110,  606,  606,
115735  /*   930 */   110,  254,   13,  614,   41,  532,  531,  283,  481,  531,
115736  /*   940 */   457,  284,  119,  561,  356,  614,   40,  284,  306,   89,
115737  /*   950 */    78,   80,  600,  599,  601,  601,   87,   87,   88,   88,
115738  /*   960 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
115739  /*   970 */    84,   84,   83,  330,  110,  424,  341,  220,  555,  606,
115740  /*   980 */   606,  351,  555,  318,  614,   95,  413,  255,   83,  330,
115741  /*   990 */   284,  284,  255,  640,  333,  356,  255,  614,   39,  306,
115742  /*  1000 */   356,   90,   80,  600,  599,  601,  601,   87,   87,   88,
115743  /*  1010 */    88,   88,   88,  424,   86,   86,   86,   86,   85,   85,
115744  /*  1020 */    84,   84,   84,   83,  330,  424,  317,  316,  141,  465,
115745  /*  1030 */   606,  606,  219,  619,  463,  614,   10,  417,  462,  255,
115746  /*  1040 */   189,  510,  553,  351,  207,  363,  161,  614,   38,  315,
115747  /*  1050 */   218,  255,  255,   80,  600,  599,  601,  601,   87,   87,
115748  /*  1060 */    88,   88,   88,   88,  424,   86,   86,   86,   86,   85,
115749  /*  1070 */    85,   84,   84,   84,   83,  330,   76,  419,  255,    3,
115750  /*  1080 */   878,  461,  424,  247,  331,  331,  614,   37,  217,   76,
115751  /*  1090 */   419,  390,    3,  216,  215,  422,    4,  331,  331,  424,
115752  /*  1100 */   547,   12,  424,  545,  614,   36,  424,  541,  422,  424,
115753  /*  1110 */   540,  424,  214,  424,  408,  424,  539,  403,  605,  605,
115754  /*  1120 */   237,  614,   25,  119,  614,   24,  588,  408,  614,   45,
115755  /*  1130 */   118,  614,   35,  614,   34,  614,   33,  614,   23,  588,
115756  /*  1140 */    60,  223,  603,  602,  513,  378,   73,   74,  140,  139,
115757  /*  1150 */   424,  110,  265,   75,  426,  425,   59,  424,  610,   73,
115758  /*  1160 */    74,  549,  402,  404,  424,  373,   75,  426,  425,  604,
115759  /*  1170 */   138,  610,  614,   11,  392,   76,  419,  181,    3,  614,
115760  /*  1180 */    32,  271,  369,  331,  331,  493,  614,   31,  149,  608,
115761  /*  1190 */   608,  608,  607,   15,  422,  365,  614,    8,  137,  489,
115762  /*  1200 */   136,  190,  608,  608,  608,  607,   15,  485,  176,  135,
115763  /*  1210 */     7,  252,  477,  408,  174,  133,  175,  474,   57,   56,
115764  /*  1220 */   132,  130,  119,   76,  419,  588,    3,  468,  245,  464,
115765  /*  1230 */   171,  331,  331,  125,  123,  456,  447,  122,  446,  104,
115766  /*  1240 */   336,  231,  422,  166,  154,   73,   74,  332,  116,  431,
115767  /*  1250 */   121,  309,   75,  426,  425,  222,  106,  610,  308,  637,
115768  /*  1260 */   204,  408,  629,  627,  628,    6,  200,  428,  427,  290,
115769  /*  1270 */   203,  622,  201,  588,   62,   63,  289,   66,  419,  399,
115770  /*  1280 */     3,  401,  288,   92,  143,  331,  331,  287,  608,  608,
115771  /*  1290 */   608,  607,   15,   73,   74,  227,  422,  325,   69,  416,
115772  /*  1300 */    75,  426,  425,  612,  412,  610,  192,   61,  569,  209,
115773  /*  1310 */   396,  226,  278,  225,  383,  408,  527,  558,  276,  533,
115774  /*  1320 */   552,  528,  321,  523,  370,  508,  180,  588,  494,  179,
115775  /*  1330 */   366,  117,  253,  269,  522,  503,  608,  608,  608,  607,
115776  /*  1340 */    15,  551,  502,   58,  274,  524,  178,   73,   74,  304,
115777  /*  1350 */   501,  368,  303,  206,   75,  426,  425,  491,  360,  610,
115778  /*  1360 */   213,  177,  483,  131,  345,  298,  297,  296,  202,  294,
115779  /*  1370 */   480,  490,  466,  134,  172,  129,  444,  346,  470,  128,
115780  /*  1380 */   314,  459,  103,  127,  126,  148,  124,  167,  443,  235,
115781  /*  1390 */   608,  608,  608,  607,   15,  442,  439,  623,  234,  299,
115782  /*  1400 */   145,  583,  291,  377,  581,  160,  119,  156,  270,  636,
115783  /*  1410 */   971,  169,  279,  626,  520,  625,  473,  624,  170,  621,
115784  /*  1420 */   618,  119,  168,   55,  409,  423,  537,  609,  286,  285,
115785  /*  1430 */   405,  570,  560,  556,    5,   52,  458,  554,  147,  267,
115786  /*  1440 */   519,  504,  518,  406,  262,  239,  260,  512,  343,  511,
115787  /*  1450 */   258,  353,  565,  256,  224,  251,  359,  277,  275,  476,
115788  /*  1460 */   475,  246,  352,  244,  467,  455,  236,  233,  232,  307,
115789  /*  1470 */   441,  281,  205,  163,  397,  280,  535,  505,  330,  617,
115790  /*  1480 */   971,  971,  971,  971,  367,  971,  971,  971,  971,  971,
115791  /*  1490 */   971,  971,  971,  971,  971,  971,  338,
115792 };
115793 static const YYCODETYPE yy_lookahead[] = {
115794  /*     0 */    19,   22,   22,   23,    1,   24,   26,   15,   27,   80,
115795  /*    10 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
115796  /*    20 */    91,   92,   93,   94,   95,  108,  109,  110,   27,   28,
115797  /*    30 */    23,   50,   51,   80,   81,   82,   83,  122,   85,   86,
115798  /*    40 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   22,
115799  /*    50 */    70,   23,   71,   72,   73,   74,   75,   76,   77,   78,
115800  /*    60 */    79,   80,   81,   82,   83,  122,   85,   86,   87,   88,
115801  /*    70 */    89,   90,   91,   92,   93,   94,   95,   19,   97,   91,
115802  /*    80 */    92,   93,   94,   95,   26,   85,   86,   87,   88,   89,
115803  /*    90 */    90,   91,   92,   93,   94,   95,   27,   28,   97,   98,
115804  /*   100 */    99,  122,  211,  102,  103,  104,   79,   19,   50,   51,
115805  /*   110 */    19,  122,   59,   55,  113,  224,  225,  226,   89,   90,
115806  /*   120 */    91,   92,   93,   94,   95,   23,   27,   28,   26,   71,
115807  /*   130 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
115808  /*   140 */    82,   83,   51,   85,   86,   87,   88,   89,   90,   91,
115809  /*   150 */    92,   93,   94,   95,   19,  132,  133,   58,   89,   90,
115810  /*   160 */    21,  108,  109,  110,   27,   28,   97,   98,   33,  100,
115811  /*   170 */     7,    8,  119,  120,   22,   19,  107,   42,  109,   27,
115812  /*   180 */    28,   27,   28,   95,   28,   50,   51,   99,  100,  101,
115813  /*   190 */   102,  103,  104,  105,   27,   28,   97,   98,  107,  152,
115814  /*   200 */   112,  132,  133,  112,   65,   69,   71,   72,   73,   74,
115815  /*   210 */    75,   76,   77,   78,   79,   80,   81,   82,   83,   11,
115816  /*   220 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
115817  /*   230 */    95,   19,  101,   97,   97,   98,   24,  101,  122,  157,
115818  /*   240 */    12,   99,  103,  112,  102,  103,  104,  152,   22,   97,
115819  /*   250 */    98,   97,   98,   27,   28,  113,   27,   29,   91,  164,
115820  /*   260 */   165,  124,   50,   51,   97,   98,  219,   59,  132,  133,
115821  /*   270 */   134,   22,   23,   45,   66,   47,  212,  213,  124,  140,
115822  /*   280 */   132,  133,   19,   71,   72,   73,   74,   75,   76,   77,
115823  /*   290 */    78,   79,   80,   81,   82,   83,  152,   85,   86,   87,
115824  /*   300 */    88,   89,   90,   91,   92,   93,   94,   95,  164,  165,
115825  /*   310 */    27,   28,  230,   50,   51,  233,  108,  109,  110,   70,
115826  /*   320 */    16,   59,   23,   97,   98,   26,   97,   22,   66,  185,
115827  /*   330 */    12,  187,   27,   28,   71,   72,   73,   74,   75,   76,
115828  /*   340 */    77,   78,   79,   80,   81,   82,   83,   29,   85,   86,
115829  /*   350 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   19,
115830  /*   360 */    22,  148,  149,   45,   23,   47,   62,  154,   64,  156,
115831  /*   370 */   108,  109,  110,   37,   69,   23,  163,   59,   26,   26,
115832  /*   380 */    97,   98,  144,  145,  146,  147,  152,  200,   52,   23,
115833  /*   390 */    50,   51,   26,   22,   89,   90,   60,  210,    7,    8,
115834  /*   400 */     9,  138,   97,   22,   23,   26,  101,   26,  174,  175,
115835  /*   410 */   197,   71,   72,   73,   74,   75,   76,   77,   78,   79,
115836  /*   420 */    80,   81,   82,   83,   16,   85,   86,   87,   88,   89,
115837  /*   430 */    90,   91,   92,   93,   94,   95,   19,  132,  133,  134,
115838  /*   440 */    23,  152,  208,  209,  140,  152,  152,  111,  195,  196,
115839  /*   450 */    98,   70,  163,  160,  152,   23,   22,  164,  165,  246,
115840  /*   460 */   207,   27,  152,  174,  175,  171,  172,   50,   51,  137,
115841  /*   470 */    62,  139,   64,  171,  172,  222,  124,   27,  138,   24,
115842  /*   480 */   163,   89,   90,  130,  174,  175,  197,  163,   71,   72,
115843  /*   490 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
115844  /*   500 */    83,   22,   85,   86,   87,   88,   89,   90,   91,   92,
115845  /*   510 */    93,   94,   95,   19,  197,  181,  182,   23,  208,  209,
115846  /*   520 */   152,  197,   26,  189,  132,  133,  232,  224,  225,  226,
115847  /*   530 */   152,   97,   91,   26,  232,  116,  212,  213,  152,  222,
115848  /*   540 */   121,  152,  174,  175,   50,   51,  243,   97,   22,   23,
115849  /*   550 */    22,  234,  174,  175,  177,   23,  239,  116,  163,  177,
115850  /*   560 */   174,  175,  121,  174,  175,   71,   72,   73,   74,   75,
115851  /*   570 */    76,   77,   78,   79,   80,   81,   82,   83,   24,   85,
115852  /*   580 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
115853  /*   590 */    19,   23,  197,   11,   23,  227,   70,  208,  220,  152,
115854  /*   600 */    31,  224,  225,  226,   35,   98,  224,  225,  226,  108,
115855  /*   610 */   109,  110,  115,  152,  117,  118,   27,  222,   49,  123,
115856  /*   620 */    24,   50,   51,   27,    0,    1,    2,  224,  225,  226,
115857  /*   630 */   166,  124,  168,  169,  239,  174,  175,  170,  171,  172,
115858  /*   640 */    22,  194,   71,   72,   73,   74,   75,   76,   77,   78,
115859  /*   650 */    79,   80,   81,   82,   83,  152,   85,   86,   87,   88,
115860  /*   660 */    89,   90,   91,   92,   93,   94,   95,   19,   22,  208,
115861  /*   670 */    24,   23,  195,  196,  170,  171,  172,  174,  175,  152,
115862  /*   680 */    26,  152,  152,  152,  207,  152,   97,  152,   23,  152,
115863  /*   690 */    51,  244,  152,   97,  152,  247,  248,   23,   50,   51,
115864  /*   700 */    26,  174,  175,  174,  175,  174,  175,  174,  175,  174,
115865  /*   710 */   175,  174,  175,   23,  174,  175,  174,  175,  188,   71,
115866  /*   720 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
115867  /*   730 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
115868  /*   740 */    92,   93,   94,   95,   19,  152,  107,  152,   33,   24,
115869  /*   750 */   152,  100,  101,   27,  174,  175,  152,   42,  152,   23,
115870  /*   760 */   152,   26,  152,   23,  152,   26,  152,  174,  175,  174,
115871  /*   770 */   175,  152,  174,  175,   23,   50,   51,   26,  174,  175,
115872  /*   780 */   174,  175,  174,  175,  174,  175,  174,  175,  174,  175,
115873  /*   790 */   163,  119,  120,  174,  175,   19,   71,   72,   73,   74,
115874  /*   800 */    75,   76,   77,   78,   79,   80,   81,   82,   83,  152,
115875  /*   810 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
115876  /*   820 */    95,   66,  152,   97,  197,   23,   50,   51,   26,   53,
115877  /*   830 */    23,  174,  175,   26,   23,   23,   23,   26,   26,   26,
115878  /*   840 */    36,  106,  146,  147,  174,  175,   19,   71,   72,   73,
115879  /*   850 */    74,   75,   76,   77,   78,   79,   80,   81,   82,   83,
115880  /*   860 */   152,   85,   86,   87,   88,   89,   90,   91,   92,   93,
115881  /*   870 */    94,   95,  152,  196,  119,  120,   19,   50,   51,  168,
115882  /*   880 */   169,   26,  174,  175,  207,   28,  152,  249,  250,  152,
115883  /*   890 */   163,  163,  163,  163,  174,  175,  163,   19,   71,   72,
115884  /*   900 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
115885  /*   910 */    83,  152,   85,   86,   87,   88,   89,   90,   91,   92,
115886  /*   920 */    93,   94,   95,  152,  197,  197,  197,  197,   50,   51,
115887  /*   930 */   197,  194,   36,  174,  175,  191,  192,  152,  191,  192,
115888  /*   940 */   163,  152,   66,  124,  152,  174,  175,  152,   19,   71,
115889  /*   950 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
115890  /*   960 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
115891  /*   970 */    92,   93,   94,   95,  197,  152,  100,  188,  152,   50,
115892  /*   980 */    51,  152,  152,  188,  174,  175,  252,  152,   94,   95,
115893  /*   990 */   152,  152,  152,    1,    2,  152,  152,  174,  175,   19,
115894  /*  1000 */   152,   72,   73,   74,   75,   76,   77,   78,   79,   80,
115895  /*  1010 */    81,   82,   83,  152,   85,   86,   87,   88,   89,   90,
115896  /*  1020 */    91,   92,   93,   94,   95,  152,  188,  188,   22,  194,
115897  /*  1030 */    50,   51,  240,  173,  194,  174,  175,  252,  194,  152,
115898  /*  1040 */    36,  181,   28,  152,   23,  219,  122,  174,  175,  219,
115899  /*  1050 */   221,  152,  152,   73,   74,   75,   76,   77,   78,   79,
115900  /*  1060 */    80,   81,   82,   83,  152,   85,   86,   87,   88,   89,
115901  /*  1070 */    90,   91,   92,   93,   94,   95,   19,   20,  152,   22,
115902  /*  1080 */    23,  194,  152,  240,   27,   28,  174,  175,  240,   19,
115903  /*  1090 */    20,   26,   22,  194,  194,   38,   22,   27,   28,  152,
115904  /*  1100 */    23,   22,  152,  116,  174,  175,  152,   23,   38,  152,
115905  /*  1110 */    23,  152,  221,  152,   57,  152,   23,  163,   50,   51,
115906  /*  1120 */   194,  174,  175,   66,  174,  175,   69,   57,  174,  175,
115907  /*  1130 */    40,  174,  175,  174,  175,  174,  175,  174,  175,   69,
115908  /*  1140 */    22,   53,   74,   75,   30,   53,   89,   90,   22,   22,
115909  /*  1150 */   152,  197,   23,   96,   97,   98,   22,  152,  101,   89,
115910  /*  1160 */    90,   91,  208,  209,  152,   53,   96,   97,   98,  101,
115911  /*  1170 */    22,  101,  174,  175,  152,   19,   20,  105,   22,  174,
115912  /*  1180 */   175,  112,   19,   27,   28,   20,  174,  175,   24,  132,
115913  /*  1190 */   133,  134,  135,  136,   38,   44,  174,  175,  107,   61,
115914  /*  1200 */    54,   26,  132,  133,  134,  135,  136,   54,  107,   22,
115915  /*  1210 */     5,  140,    1,   57,   36,  111,  122,   28,   79,   79,
115916  /*  1220 */   131,  123,   66,   19,   20,   69,   22,    1,   16,   20,
115917  /*  1230 */   125,   27,   28,  123,  111,  120,   23,  131,   23,   16,
115918  /*  1240 */    68,  142,   38,   15,   22,   89,   90,    3,  167,    4,
115919  /*  1250 */   248,  251,   96,   97,   98,  180,  180,  101,  251,  151,
115920  /*  1260 */     6,   57,  151,   13,  151,   26,   25,  151,  161,  202,
115921  /*  1270 */   153,  162,  153,   69,  130,  128,  203,   19,   20,  127,
115922  /*  1280 */    22,  126,  204,  129,   22,   27,   28,  205,  132,  133,
115923  /*  1290 */   134,  135,  136,   89,   90,  231,   38,   95,  137,  179,
115924  /*  1300 */    96,   97,   98,  206,  179,  101,  122,  107,  159,  159,
115925  /*  1310 */   125,  231,  216,  228,  107,   57,  184,  217,  216,  176,
115926  /*  1320 */   217,  176,   48,  106,   18,  184,  158,   69,  159,  158,
115927  /*  1330 */    46,   71,  237,  176,  176,  176,  132,  133,  134,  135,
115928  /*  1340 */   136,  217,  176,  137,  216,  178,  158,   89,   90,  179,
115929  /*  1350 */   176,  159,  179,  159,   96,   97,   98,  159,  159,  101,
115930  /*  1360 */     5,  158,  202,   22,   18,   10,   11,   12,   13,   14,
115931  /*  1370 */   190,  238,   17,  190,  158,  193,   41,  159,  202,  193,
115932  /*  1380 */   159,  202,  245,  193,  193,  223,  190,   32,  159,   34,
115933  /*  1390 */   132,  133,  134,  135,  136,  159,   39,  155,   43,  150,
115934  /*  1400 */   223,  177,  201,  178,  177,  186,   66,  199,  177,  152,
115935  /*  1410 */   253,   56,  215,  152,  182,  152,  202,  152,   63,  152,
115936  /*  1420 */   152,   66,   67,  242,  229,  152,  174,  152,  152,  152,
115937  /*  1430 */   152,  152,  152,  152,  199,  242,  202,  152,  198,  152,
115938  /*  1440 */   152,  152,  183,  192,  152,  215,  152,  183,  215,  183,
115939  /*  1450 */   152,  241,  214,  152,  211,  152,  152,  211,  211,  152,
115940  /*  1460 */   152,  241,  152,  152,  152,  152,  152,  152,  152,  114,
115941  /*  1470 */   152,  152,  235,  152,  152,  152,  174,  187,   95,  174,
115942  /*  1480 */   253,  253,  253,  253,  236,  253,  253,  253,  253,  253,
115943  /*  1490 */   253,  253,  253,  253,  253,  253,  141,
115944 };
115945 #define YY_SHIFT_USE_DFLT (-86)
115946 #define YY_SHIFT_COUNT (429)
115947 #define YY_SHIFT_MIN   (-85)
115948 #define YY_SHIFT_MAX   (1383)
115949 static const short yy_shift_ofst[] = {
115950  /*     0 */   992, 1057, 1355, 1156, 1204, 1204,    1,  262,  -19,  135,
115951  /*    10 */   135,  776, 1204, 1204, 1204, 1204,   69,   69,   53,  208,
115952  /*    20 */   283,  755,   58,  725,  648,  571,  494,  417,  340,  263,
115953  /*    30 */   212,  827,  827,  827,  827,  827,  827,  827,  827,  827,
115954  /*    40 */   827,  827,  827,  827,  827,  827,  878,  827,  929,  980,
115955  /*    50 */   980, 1070, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115956  /*    60 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115957  /*    70 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115958  /*    80 */  1258, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115959  /*    90 */  1204, 1204, 1204, 1204,  -71,  -47,  -47,  -47,  -47,  -47,
115960  /*   100 */     0,   29,  -12,  283,  283,  139,   91,  392,  392,  894,
115961  /*   110 */   672,  726, 1383,  -86,  -86,  -86,   88,  318,  318,   99,
115962  /*   120 */   381,  -20,  283,  283,  283,  283,  283,  283,  283,  283,
115963  /*   130 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
115964  /*   140 */   283,  283,  283,  283,  624,  876,  726,  672, 1340, 1340,
115965  /*   150 */  1340, 1340, 1340, 1340,  -86,  -86,  -86,  305,  136,  136,
115966  /*   160 */   142,  167,  226,  154,  137,  152,  283,  283,  283,  283,
115967  /*   170 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
115968  /*   180 */   283,  283,  283,  336,  336,  336,  283,  283,  352,  283,
115969  /*   190 */   283,  283,  283,  283,  228,  283,  283,  283,  283,  283,
115970  /*   200 */   283,  283,  283,  283,  283,  501,  569,  596,  596,  596,
115971  /*   210 */   507,  497,  441,  391,  353,  156,  156,  857,  353,  857,
115972  /*   220 */   735,  813,  639,  715,  156,  332,  715,  715,  496,  419,
115973  /*   230 */   646, 1357, 1184, 1184, 1335, 1335, 1184, 1341, 1260, 1144,
115974  /*   240 */  1346, 1346, 1346, 1346, 1184, 1306, 1144, 1341, 1260, 1260,
115975  /*   250 */  1144, 1184, 1306, 1206, 1284, 1184, 1184, 1306, 1184, 1306,
115976  /*   260 */  1184, 1306, 1262, 1207, 1207, 1207, 1274, 1262, 1207, 1217,
115977  /*   270 */  1207, 1274, 1207, 1207, 1185, 1200, 1185, 1200, 1185, 1200,
115978  /*   280 */  1184, 1184, 1161, 1262, 1202, 1202, 1262, 1154, 1155, 1147,
115979  /*   290 */  1152, 1144, 1241, 1239, 1250, 1250, 1254, 1254, 1254, 1254,
115980  /*   300 */   -86,  -86,  -86,  -86,  -86,  -86, 1068,  304,  526,  249,
115981  /*   310 */   408,  -83,  434,  812,   27,  811,  807,  802,  751,  589,
115982  /*   320 */   651,  163,  131,  674,  366,  450,  299,  148,   23,  102,
115983  /*   330 */   229,  -21, 1245, 1244, 1222, 1099, 1228, 1172, 1223, 1215,
115984  /*   340 */  1213, 1115, 1106, 1123, 1110, 1209, 1105, 1212, 1226, 1098,
115985  /*   350 */  1089, 1140, 1139, 1104, 1189, 1178, 1094, 1211, 1205, 1187,
115986  /*   360 */  1101, 1071, 1153, 1175, 1146, 1138, 1151, 1091, 1164, 1165,
115987  /*   370 */  1163, 1069, 1072, 1148, 1112, 1134, 1127, 1129, 1126, 1092,
115988  /*   380 */  1114, 1118, 1088, 1090, 1093, 1087, 1084,  987, 1079, 1077,
115989  /*   390 */  1074, 1065,  924, 1021, 1014, 1004, 1006,  819,  739,  896,
115990  /*   400 */   855,  804,  739,  740,  736,  690,  654,  665,  618,  582,
115991  /*   410 */   568,  528,  554,  379,  532,  479,  455,  379,  432,  371,
115992  /*   420 */   341,   28,  338,  116,  -11,  -57,  -85,    7,   -8,    3,
115993 };
115994 #define YY_REDUCE_USE_DFLT (-110)
115995 #define YY_REDUCE_COUNT (305)
115996 #define YY_REDUCE_MIN   (-109)
115997 #define YY_REDUCE_MAX   (1323)
115998 static const short yy_reduce_ofst[] = {
115999  /*     0 */   238,  954,  213,  289,  310,  234,  144,  317, -109,  382,
116000  /*    10 */   377,  303,  461,  389,  378,  368,  302,  294,  253,  395,
116001  /*    20 */   293,  324,  403,  403,  403,  403,  403,  403,  403,  403,
116002  /*    30 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
116003  /*    40 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
116004  /*    50 */   403, 1022, 1012, 1005,  998,  963,  961,  959,  957,  950,
116005  /*    60 */   947,  930,  912,  873,  861,  823,  810,  771,  759,  720,
116006  /*    70 */   708,  670,  657,  619,  614,  612,  610,  608,  606,  604,
116007  /*    80 */   598,  595,  593,  580,  542,  540,  537,  535,  533,  531,
116008  /*    90 */   529,  527,  503,  386,  403,  403,  403,  403,  403,  403,
116009  /*   100 */   403,  403,  403,   95,  447,   82,  334,  504,  467,  403,
116010  /*   110 */   477,  464,  403,  403,  403,  403,  860,  747,  744,  785,
116011  /*   120 */   638,  638,  926,  891,  900,  899,  887,  844,  840,  835,
116012  /*   130 */   848,  830,  843,  829,  792,  839,  826,  737,  838,  795,
116013  /*   140 */   789,   47,  734,  530,  696,  777,  711,  677,  733,  730,
116014  /*   150 */   729,  728,  727,  627,  448,   64,  187, 1305, 1302, 1252,
116015  /*   160 */  1290, 1273, 1323, 1322, 1321, 1319, 1318, 1316, 1315, 1314,
116016  /*   170 */  1313, 1312, 1311, 1310, 1308, 1307, 1304, 1303, 1301, 1298,
116017  /*   180 */  1294, 1292, 1289, 1266, 1264, 1259, 1288, 1287, 1238, 1285,
116018  /*   190 */  1281, 1280, 1279, 1278, 1251, 1277, 1276, 1275, 1273, 1268,
116019  /*   200 */  1267, 1265, 1263, 1261, 1257, 1248, 1237, 1247, 1246, 1243,
116020  /*   210 */  1238, 1240, 1235, 1249, 1234, 1233, 1230, 1220, 1214, 1210,
116021  /*   220 */  1225, 1219, 1232, 1231, 1197, 1195, 1227, 1224, 1201, 1208,
116022  /*   230 */  1242, 1137, 1236, 1229, 1193, 1181, 1221, 1177, 1196, 1179,
116023  /*   240 */  1191, 1190, 1186, 1182, 1218, 1216, 1176, 1162, 1183, 1180,
116024  /*   250 */  1160, 1199, 1203, 1133, 1095, 1198, 1194, 1188, 1192, 1171,
116025  /*   260 */  1169, 1168, 1173, 1174, 1166, 1159, 1141, 1170, 1158, 1167,
116026  /*   270 */  1157, 1132, 1145, 1143, 1124, 1128, 1103, 1102, 1100, 1096,
116027  /*   280 */  1150, 1149, 1085, 1125, 1080, 1064, 1120, 1097, 1082, 1078,
116028  /*   290 */  1073, 1067, 1109, 1107, 1119, 1117, 1116, 1113, 1111, 1108,
116029  /*   300 */  1007, 1000, 1002, 1076, 1075, 1081,
116030 };
116031 static const YYACTIONTYPE yy_default[] = {
116032  /*     0 */   647,  964,  964,  964,  878,  878,  969,  964,  774,  802,
116033  /*    10 */   802,  938,  969,  969,  969,  876,  969,  969,  969,  964,
116034  /*    20 */   969,  778,  808,  969,  969,  969,  969,  969,  969,  969,
116035  /*    30 */   969,  937,  939,  816,  815,  918,  789,  813,  806,  810,
116036  /*    40 */   879,  872,  873,  871,  875,  880,  969,  809,  841,  856,
116037  /*    50 */   840,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116038  /*    60 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116039  /*    70 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116040  /*    80 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116041  /*    90 */   969,  969,  969,  969,  850,  855,  862,  854,  851,  843,
116042  /*   100 */   842,  844,  845,  969,  969,  673,  739,  969,  969,  846,
116043  /*   110 */   969,  685,  847,  859,  858,  857,  680,  969,  969,  969,
116044  /*   120 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116045  /*   130 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116046  /*   140 */   969,  969,  969,  969,  647,  964,  969,  969,  964,  964,
116047  /*   150 */   964,  964,  964,  964,  956,  778,  768,  969,  969,  969,
116048  /*   160 */   969,  969,  969,  969,  969,  969,  969,  944,  942,  969,
116049  /*   170 */   891,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116050  /*   180 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116051  /*   190 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116052  /*   200 */   969,  969,  969,  969,  653,  969,  911,  774,  774,  774,
116053  /*   210 */   776,  754,  766,  655,  812,  791,  791,  923,  812,  923,
116054  /*   220 */   710,  733,  707,  802,  791,  874,  802,  802,  775,  766,
116055  /*   230 */   969,  949,  782,  782,  941,  941,  782,  821,  743,  812,
116056  /*   240 */   750,  750,  750,  750,  782,  670,  812,  821,  743,  743,
116057  /*   250 */   812,  782,  670,  917,  915,  782,  782,  670,  782,  670,
116058  /*   260 */   782,  670,  884,  741,  741,  741,  725,  884,  741,  710,
116059  /*   270 */   741,  725,  741,  741,  795,  790,  795,  790,  795,  790,
116060  /*   280 */   782,  782,  969,  884,  888,  888,  884,  807,  796,  805,
116061  /*   290 */   803,  812,  676,  728,  663,  663,  652,  652,  652,  652,
116062  /*   300 */   961,  961,  956,  712,  712,  695,  969,  969,  969,  969,
116063  /*   310 */   969,  969,  687,  969,  893,  969,  969,  969,  969,  969,
116064  /*   320 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116065  /*   330 */   969,  828,  969,  648,  951,  969,  969,  948,  969,  969,
116066  /*   340 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116067  /*   350 */   969,  969,  969,  969,  969,  969,  921,  969,  969,  969,
116068  /*   360 */   969,  969,  969,  914,  913,  969,  969,  969,  969,  969,
116069  /*   370 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
116070  /*   380 */   969,  969,  969,  969,  969,  969,  969,  757,  969,  969,
116071  /*   390 */   969,  761,  969,  969,  969,  969,  969,  969,  804,  969,
116072  /*   400 */   797,  969,  877,  969,  969,  969,  969,  969,  969,  969,
116073  /*   410 */   969,  969,  969,  966,  969,  969,  969,  965,  969,  969,
116074  /*   420 */   969,  969,  969,  830,  969,  829,  833,  969,  661,  969,
116075  /*   430 */   644,  649,  960,  963,  962,  959,  958,  957,  952,  950,
116076  /*   440 */   947,  946,  945,  943,  940,  936,  897,  895,  902,  901,
116077  /*   450 */   900,  899,  898,  896,  894,  892,  818,  817,  814,  811,
116078  /*   460 */   753,  935,  890,  752,  749,  748,  669,  953,  920,  929,
116079  /*   470 */   928,  927,  822,  926,  925,  924,  922,  919,  906,  820,
116080  /*   480 */   819,  744,  882,  881,  672,  910,  909,  908,  912,  916,
116081  /*   490 */   907,  784,  751,  671,  668,  675,  679,  731,  732,  740,
116082  /*   500 */   738,  737,  736,  735,  734,  730,  681,  686,  724,  709,
116083  /*   510 */   708,  717,  716,  722,  721,  720,  719,  718,  715,  714,
116084  /*   520 */   713,  706,  705,  711,  704,  727,  726,  723,  703,  747,
116085  /*   530 */   746,  745,  742,  702,  701,  700,  833,  699,  698,  838,
116086  /*   540 */   837,  866,  826,  755,  759,  758,  762,  763,  771,  770,
116087  /*   550 */   769,  780,  781,  793,  792,  824,  823,  794,  779,  773,
116088  /*   560 */   772,  788,  787,  786,  785,  777,  767,  799,  798,  868,
116089  /*   570 */   783,  867,  865,  934,  933,  932,  931,  930,  870,  967,
116090  /*   580 */   968,  887,  889,  886,  801,  800,  885,  869,  839,  836,
116091  /*   590 */   690,  691,  905,  904,  903,  693,  692,  689,  688,  863,
116092  /*   600 */   860,  852,  864,  861,  853,  849,  848,  834,  832,  831,
116093  /*   610 */   827,  835,  760,  756,  825,  765,  764,  697,  696,  694,
116094  /*   620 */   678,  677,  674,  667,  665,  664,  666,  662,  660,  659,
116095  /*   630 */   658,  657,  656,  684,  683,  682,  654,  651,  650,  646,
116096  /*   640 */   645,  643,
116097 };
116098 
116099 /* The next table maps tokens into fallback tokens.  If a construct
116100 ** like the following:
116101 **
116102 **      %fallback ID X Y Z.
116103 **
116104 ** appears in the grammar, then ID becomes a fallback token for X, Y,
116105 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
116106 ** but it does not parse, the type of the token is changed to ID and
116107 ** the parse is retried before an error is thrown.
116108 */
116109 #ifdef YYFALLBACK
116110 static const YYCODETYPE yyFallback[] = {
116111     0,  /*          $ => nothing */
116112     0,  /*       SEMI => nothing */
116113    27,  /*    EXPLAIN => ID */
116114    27,  /*      QUERY => ID */
116115    27,  /*       PLAN => ID */
116116    27,  /*      BEGIN => ID */
116117     0,  /* TRANSACTION => nothing */
116118    27,  /*   DEFERRED => ID */
116119    27,  /*  IMMEDIATE => ID */
116120    27,  /*  EXCLUSIVE => ID */
116121     0,  /*     COMMIT => nothing */
116122    27,  /*        END => ID */
116123    27,  /*   ROLLBACK => ID */
116124    27,  /*  SAVEPOINT => ID */
116125    27,  /*    RELEASE => ID */
116126     0,  /*         TO => nothing */
116127     0,  /*      TABLE => nothing */
116128     0,  /*     CREATE => nothing */
116129    27,  /*         IF => ID */
116130     0,  /*        NOT => nothing */
116131     0,  /*     EXISTS => nothing */
116132    27,  /*       TEMP => ID */
116133     0,  /*         LP => nothing */
116134     0,  /*         RP => nothing */
116135     0,  /*         AS => nothing */
116136    27,  /*    WITHOUT => ID */
116137     0,  /*      COMMA => nothing */
116138     0,  /*         ID => nothing */
116139     0,  /*    INDEXED => nothing */
116140    27,  /*      ABORT => ID */
116141    27,  /*     ACTION => ID */
116142    27,  /*      AFTER => ID */
116143    27,  /*    ANALYZE => ID */
116144    27,  /*        ASC => ID */
116145    27,  /*     ATTACH => ID */
116146    27,  /*     BEFORE => ID */
116147    27,  /*         BY => ID */
116148    27,  /*    CASCADE => ID */
116149    27,  /*       CAST => ID */
116150    27,  /*   COLUMNKW => ID */
116151    27,  /*   CONFLICT => ID */
116152    27,  /*   DATABASE => ID */
116153    27,  /*       DESC => ID */
116154    27,  /*     DETACH => ID */
116155    27,  /*       EACH => ID */
116156    27,  /*       FAIL => ID */
116157    27,  /*        FOR => ID */
116158    27,  /*     IGNORE => ID */
116159    27,  /*  INITIALLY => ID */
116160    27,  /*    INSTEAD => ID */
116161    27,  /*    LIKE_KW => ID */
116162    27,  /*      MATCH => ID */
116163    27,  /*         NO => ID */
116164    27,  /*        KEY => ID */
116165    27,  /*         OF => ID */
116166    27,  /*     OFFSET => ID */
116167    27,  /*     PRAGMA => ID */
116168    27,  /*      RAISE => ID */
116169    27,  /*  RECURSIVE => ID */
116170    27,  /*    REPLACE => ID */
116171    27,  /*   RESTRICT => ID */
116172    27,  /*        ROW => ID */
116173    27,  /*    TRIGGER => ID */
116174    27,  /*     VACUUM => ID */
116175    27,  /*       VIEW => ID */
116176    27,  /*    VIRTUAL => ID */
116177    27,  /*       WITH => ID */
116178    27,  /*    REINDEX => ID */
116179    27,  /*     RENAME => ID */
116180    27,  /*   CTIME_KW => ID */
116181 };
116182 #endif /* YYFALLBACK */
116183 
116184 /* The following structure represents a single element of the
116185 ** parser's stack.  Information stored includes:
116186 **
116187 **   +  The state number for the parser at this level of the stack.
116188 **
116189 **   +  The value of the token stored at this level of the stack.
116190 **      (In other words, the "major" token.)
116191 **
116192 **   +  The semantic value stored at this level of the stack.  This is
116193 **      the information used by the action routines in the grammar.
116194 **      It is sometimes called the "minor" token.
116195 */
116196 struct yyStackEntry {
116197   YYACTIONTYPE stateno;  /* The state-number */
116198   YYCODETYPE major;      /* The major token value.  This is the code
116199                          ** number for the token at this stack level */
116200   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
116201                          ** is the value of the token  */
116202 };
116203 typedef struct yyStackEntry yyStackEntry;
116204 
116205 /* The state of the parser is completely contained in an instance of
116206 ** the following structure */
116207 struct yyParser {
116208   int yyidx;                    /* Index of top element in stack */
116209 #ifdef YYTRACKMAXSTACKDEPTH
116210   int yyidxMax;                 /* Maximum value of yyidx */
116211 #endif
116212   int yyerrcnt;                 /* Shifts left before out of the error */
116213   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
116214 #if YYSTACKDEPTH<=0
116215   int yystksz;                  /* Current side of the stack */
116216   yyStackEntry *yystack;        /* The parser's stack */
116217 #else
116218   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
116219 #endif
116220 };
116221 typedef struct yyParser yyParser;
116222 
116223 #ifndef NDEBUG
116224 /* #include <stdio.h> */
116225 static FILE *yyTraceFILE = 0;
116226 static char *yyTracePrompt = 0;
116227 #endif /* NDEBUG */
116228 
116229 #ifndef NDEBUG
116230 /*
116231 ** Turn parser tracing on by giving a stream to which to write the trace
116232 ** and a prompt to preface each trace message.  Tracing is turned off
116233 ** by making either argument NULL
116234 **
116235 ** Inputs:
116236 ** <ul>
116237 ** <li> A FILE* to which trace output should be written.
116238 **      If NULL, then tracing is turned off.
116239 ** <li> A prefix string written at the beginning of every
116240 **      line of trace output.  If NULL, then tracing is
116241 **      turned off.
116242 ** </ul>
116243 **
116244 ** Outputs:
116245 ** None.
116246 */
116247 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
116248   yyTraceFILE = TraceFILE;
116249   yyTracePrompt = zTracePrompt;
116250   if( yyTraceFILE==0 ) yyTracePrompt = 0;
116251   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
116252 }
116253 #endif /* NDEBUG */
116254 
116255 #ifndef NDEBUG
116256 /* For tracing shifts, the names of all terminals and nonterminals
116257 ** are required.  The following table supplies these names */
116258 static const char *const yyTokenName[] = {
116259   "$",             "SEMI",          "EXPLAIN",       "QUERY",
116260   "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",
116261   "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",
116262   "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",
116263   "TABLE",         "CREATE",        "IF",            "NOT",
116264   "EXISTS",        "TEMP",          "LP",            "RP",
116265   "AS",            "WITHOUT",       "COMMA",         "ID",
116266   "INDEXED",       "ABORT",         "ACTION",        "AFTER",
116267   "ANALYZE",       "ASC",           "ATTACH",        "BEFORE",
116268   "BY",            "CASCADE",       "CAST",          "COLUMNKW",
116269   "CONFLICT",      "DATABASE",      "DESC",          "DETACH",
116270   "EACH",          "FAIL",          "FOR",           "IGNORE",
116271   "INITIALLY",     "INSTEAD",       "LIKE_KW",       "MATCH",
116272   "NO",            "KEY",           "OF",            "OFFSET",
116273   "PRAGMA",        "RAISE",         "RECURSIVE",     "REPLACE",
116274   "RESTRICT",      "ROW",           "TRIGGER",       "VACUUM",
116275   "VIEW",          "VIRTUAL",       "WITH",          "REINDEX",
116276   "RENAME",        "CTIME_KW",      "ANY",           "OR",
116277   "AND",           "IS",            "BETWEEN",       "IN",
116278   "ISNULL",        "NOTNULL",       "NE",            "EQ",
116279   "GT",            "LE",            "LT",            "GE",
116280   "ESCAPE",        "BITAND",        "BITOR",         "LSHIFT",
116281   "RSHIFT",        "PLUS",          "MINUS",         "STAR",
116282   "SLASH",         "REM",           "CONCAT",        "COLLATE",
116283   "BITNOT",        "STRING",        "JOIN_KW",       "CONSTRAINT",
116284   "DEFAULT",       "NULL",          "PRIMARY",       "UNIQUE",
116285   "CHECK",         "REFERENCES",    "AUTOINCR",      "ON",
116286   "INSERT",        "DELETE",        "UPDATE",        "SET",
116287   "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",
116288   "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",
116289   "VALUES",        "DISTINCT",      "DOT",           "FROM",
116290   "JOIN",          "USING",         "ORDER",         "GROUP",
116291   "HAVING",        "LIMIT",         "WHERE",         "INTO",
116292   "INTEGER",       "FLOAT",         "BLOB",          "VARIABLE",
116293   "CASE",          "WHEN",          "THEN",          "ELSE",
116294   "INDEX",         "ALTER",         "ADD",           "error",
116295   "input",         "cmdlist",       "ecmd",          "explain",
116296   "cmdx",          "cmd",           "transtype",     "trans_opt",
116297   "nm",            "savepoint_opt",  "create_table",  "create_table_args",
116298   "createkw",      "temp",          "ifnotexists",   "dbnm",
116299   "columnlist",    "conslist_opt",  "table_options",  "select",
116300   "column",        "columnid",      "type",          "carglist",
116301   "typetoken",     "typename",      "signed",        "plus_num",
116302   "minus_num",     "ccons",         "term",          "expr",
116303   "onconf",        "sortorder",     "autoinc",       "idxlist_opt",
116304   "refargs",       "defer_subclause",  "refarg",        "refact",
116305   "init_deferred_pred_opt",  "conslist",      "tconscomma",    "tcons",
116306   "idxlist",       "defer_subclause_opt",  "orconf",        "resolvetype",
116307   "raisetype",     "ifexists",      "fullname",      "selectnowith",
116308   "oneselect",     "with",          "multiselect_op",  "distinct",
116309   "selcollist",    "from",          "where_opt",     "groupby_opt",
116310   "having_opt",    "orderby_opt",   "limit_opt",     "values",
116311   "nexprlist",     "exprlist",      "sclp",          "as",
116312   "seltablist",    "stl_prefix",    "joinop",        "indexed_opt",
116313   "on_opt",        "using_opt",     "joinop2",       "idlist",
116314   "sortlist",      "setlist",       "insert_cmd",    "inscollist_opt",
116315   "likeop",        "between_op",    "in_op",         "case_operand",
116316   "case_exprlist",  "case_else",     "uniqueflag",    "collate",
116317   "nmnum",         "trigger_decl",  "trigger_cmd_list",  "trigger_time",
116318   "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd",
116319   "trnm",          "tridxby",       "database_kw_opt",  "key_opt",
116320   "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist",
116321   "vtabarg",       "vtabargtoken",  "lp",            "anylist",
116322   "wqlist",
116323 };
116324 #endif /* NDEBUG */
116325 
116326 #ifndef NDEBUG
116327 /* For tracing reduce actions, the names of all rules are required.
116328 */
116329 static const char *const yyRuleName[] = {
116330  /*   0 */ "input ::= cmdlist",
116331  /*   1 */ "cmdlist ::= cmdlist ecmd",
116332  /*   2 */ "cmdlist ::= ecmd",
116333  /*   3 */ "ecmd ::= SEMI",
116334  /*   4 */ "ecmd ::= explain cmdx SEMI",
116335  /*   5 */ "explain ::=",
116336  /*   6 */ "explain ::= EXPLAIN",
116337  /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
116338  /*   8 */ "cmdx ::= cmd",
116339  /*   9 */ "cmd ::= BEGIN transtype trans_opt",
116340  /*  10 */ "trans_opt ::=",
116341  /*  11 */ "trans_opt ::= TRANSACTION",
116342  /*  12 */ "trans_opt ::= TRANSACTION nm",
116343  /*  13 */ "transtype ::=",
116344  /*  14 */ "transtype ::= DEFERRED",
116345  /*  15 */ "transtype ::= IMMEDIATE",
116346  /*  16 */ "transtype ::= EXCLUSIVE",
116347  /*  17 */ "cmd ::= COMMIT trans_opt",
116348  /*  18 */ "cmd ::= END trans_opt",
116349  /*  19 */ "cmd ::= ROLLBACK trans_opt",
116350  /*  20 */ "savepoint_opt ::= SAVEPOINT",
116351  /*  21 */ "savepoint_opt ::=",
116352  /*  22 */ "cmd ::= SAVEPOINT nm",
116353  /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
116354  /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
116355  /*  25 */ "cmd ::= create_table create_table_args",
116356  /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
116357  /*  27 */ "createkw ::= CREATE",
116358  /*  28 */ "ifnotexists ::=",
116359  /*  29 */ "ifnotexists ::= IF NOT EXISTS",
116360  /*  30 */ "temp ::= TEMP",
116361  /*  31 */ "temp ::=",
116362  /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
116363  /*  33 */ "create_table_args ::= AS select",
116364  /*  34 */ "table_options ::=",
116365  /*  35 */ "table_options ::= WITHOUT nm",
116366  /*  36 */ "columnlist ::= columnlist COMMA column",
116367  /*  37 */ "columnlist ::= column",
116368  /*  38 */ "column ::= columnid type carglist",
116369  /*  39 */ "columnid ::= nm",
116370  /*  40 */ "nm ::= ID|INDEXED",
116371  /*  41 */ "nm ::= STRING",
116372  /*  42 */ "nm ::= JOIN_KW",
116373  /*  43 */ "type ::=",
116374  /*  44 */ "type ::= typetoken",
116375  /*  45 */ "typetoken ::= typename",
116376  /*  46 */ "typetoken ::= typename LP signed RP",
116377  /*  47 */ "typetoken ::= typename LP signed COMMA signed RP",
116378  /*  48 */ "typename ::= ID|STRING",
116379  /*  49 */ "typename ::= typename ID|STRING",
116380  /*  50 */ "signed ::= plus_num",
116381  /*  51 */ "signed ::= minus_num",
116382  /*  52 */ "carglist ::= carglist ccons",
116383  /*  53 */ "carglist ::=",
116384  /*  54 */ "ccons ::= CONSTRAINT nm",
116385  /*  55 */ "ccons ::= DEFAULT term",
116386  /*  56 */ "ccons ::= DEFAULT LP expr RP",
116387  /*  57 */ "ccons ::= DEFAULT PLUS term",
116388  /*  58 */ "ccons ::= DEFAULT MINUS term",
116389  /*  59 */ "ccons ::= DEFAULT ID|INDEXED",
116390  /*  60 */ "ccons ::= NULL onconf",
116391  /*  61 */ "ccons ::= NOT NULL onconf",
116392  /*  62 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
116393  /*  63 */ "ccons ::= UNIQUE onconf",
116394  /*  64 */ "ccons ::= CHECK LP expr RP",
116395  /*  65 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
116396  /*  66 */ "ccons ::= defer_subclause",
116397  /*  67 */ "ccons ::= COLLATE ID|STRING",
116398  /*  68 */ "autoinc ::=",
116399  /*  69 */ "autoinc ::= AUTOINCR",
116400  /*  70 */ "refargs ::=",
116401  /*  71 */ "refargs ::= refargs refarg",
116402  /*  72 */ "refarg ::= MATCH nm",
116403  /*  73 */ "refarg ::= ON INSERT refact",
116404  /*  74 */ "refarg ::= ON DELETE refact",
116405  /*  75 */ "refarg ::= ON UPDATE refact",
116406  /*  76 */ "refact ::= SET NULL",
116407  /*  77 */ "refact ::= SET DEFAULT",
116408  /*  78 */ "refact ::= CASCADE",
116409  /*  79 */ "refact ::= RESTRICT",
116410  /*  80 */ "refact ::= NO ACTION",
116411  /*  81 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
116412  /*  82 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
116413  /*  83 */ "init_deferred_pred_opt ::=",
116414  /*  84 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
116415  /*  85 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
116416  /*  86 */ "conslist_opt ::=",
116417  /*  87 */ "conslist_opt ::= COMMA conslist",
116418  /*  88 */ "conslist ::= conslist tconscomma tcons",
116419  /*  89 */ "conslist ::= tcons",
116420  /*  90 */ "tconscomma ::= COMMA",
116421  /*  91 */ "tconscomma ::=",
116422  /*  92 */ "tcons ::= CONSTRAINT nm",
116423  /*  93 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
116424  /*  94 */ "tcons ::= UNIQUE LP idxlist RP onconf",
116425  /*  95 */ "tcons ::= CHECK LP expr RP onconf",
116426  /*  96 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
116427  /*  97 */ "defer_subclause_opt ::=",
116428  /*  98 */ "defer_subclause_opt ::= defer_subclause",
116429  /*  99 */ "onconf ::=",
116430  /* 100 */ "onconf ::= ON CONFLICT resolvetype",
116431  /* 101 */ "orconf ::=",
116432  /* 102 */ "orconf ::= OR resolvetype",
116433  /* 103 */ "resolvetype ::= raisetype",
116434  /* 104 */ "resolvetype ::= IGNORE",
116435  /* 105 */ "resolvetype ::= REPLACE",
116436  /* 106 */ "cmd ::= DROP TABLE ifexists fullname",
116437  /* 107 */ "ifexists ::= IF EXISTS",
116438  /* 108 */ "ifexists ::=",
116439  /* 109 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
116440  /* 110 */ "cmd ::= DROP VIEW ifexists fullname",
116441  /* 111 */ "cmd ::= select",
116442  /* 112 */ "select ::= with selectnowith",
116443  /* 113 */ "selectnowith ::= oneselect",
116444  /* 114 */ "selectnowith ::= selectnowith multiselect_op oneselect",
116445  /* 115 */ "multiselect_op ::= UNION",
116446  /* 116 */ "multiselect_op ::= UNION ALL",
116447  /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
116448  /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
116449  /* 119 */ "oneselect ::= values",
116450  /* 120 */ "values ::= VALUES LP nexprlist RP",
116451  /* 121 */ "values ::= values COMMA LP exprlist RP",
116452  /* 122 */ "distinct ::= DISTINCT",
116453  /* 123 */ "distinct ::= ALL",
116454  /* 124 */ "distinct ::=",
116455  /* 125 */ "sclp ::= selcollist COMMA",
116456  /* 126 */ "sclp ::=",
116457  /* 127 */ "selcollist ::= sclp expr as",
116458  /* 128 */ "selcollist ::= sclp STAR",
116459  /* 129 */ "selcollist ::= sclp nm DOT STAR",
116460  /* 130 */ "as ::= AS nm",
116461  /* 131 */ "as ::= ID|STRING",
116462  /* 132 */ "as ::=",
116463  /* 133 */ "from ::=",
116464  /* 134 */ "from ::= FROM seltablist",
116465  /* 135 */ "stl_prefix ::= seltablist joinop",
116466  /* 136 */ "stl_prefix ::=",
116467  /* 137 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
116468  /* 138 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
116469  /* 139 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
116470  /* 140 */ "dbnm ::=",
116471  /* 141 */ "dbnm ::= DOT nm",
116472  /* 142 */ "fullname ::= nm dbnm",
116473  /* 143 */ "joinop ::= COMMA|JOIN",
116474  /* 144 */ "joinop ::= JOIN_KW JOIN",
116475  /* 145 */ "joinop ::= JOIN_KW nm JOIN",
116476  /* 146 */ "joinop ::= JOIN_KW nm nm JOIN",
116477  /* 147 */ "on_opt ::= ON expr",
116478  /* 148 */ "on_opt ::=",
116479  /* 149 */ "indexed_opt ::=",
116480  /* 150 */ "indexed_opt ::= INDEXED BY nm",
116481  /* 151 */ "indexed_opt ::= NOT INDEXED",
116482  /* 152 */ "using_opt ::= USING LP idlist RP",
116483  /* 153 */ "using_opt ::=",
116484  /* 154 */ "orderby_opt ::=",
116485  /* 155 */ "orderby_opt ::= ORDER BY sortlist",
116486  /* 156 */ "sortlist ::= sortlist COMMA expr sortorder",
116487  /* 157 */ "sortlist ::= expr sortorder",
116488  /* 158 */ "sortorder ::= ASC",
116489  /* 159 */ "sortorder ::= DESC",
116490  /* 160 */ "sortorder ::=",
116491  /* 161 */ "groupby_opt ::=",
116492  /* 162 */ "groupby_opt ::= GROUP BY nexprlist",
116493  /* 163 */ "having_opt ::=",
116494  /* 164 */ "having_opt ::= HAVING expr",
116495  /* 165 */ "limit_opt ::=",
116496  /* 166 */ "limit_opt ::= LIMIT expr",
116497  /* 167 */ "limit_opt ::= LIMIT expr OFFSET expr",
116498  /* 168 */ "limit_opt ::= LIMIT expr COMMA expr",
116499  /* 169 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
116500  /* 170 */ "where_opt ::=",
116501  /* 171 */ "where_opt ::= WHERE expr",
116502  /* 172 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
116503  /* 173 */ "setlist ::= setlist COMMA nm EQ expr",
116504  /* 174 */ "setlist ::= nm EQ expr",
116505  /* 175 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt select",
116506  /* 176 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
116507  /* 177 */ "insert_cmd ::= INSERT orconf",
116508  /* 178 */ "insert_cmd ::= REPLACE",
116509  /* 179 */ "inscollist_opt ::=",
116510  /* 180 */ "inscollist_opt ::= LP idlist RP",
116511  /* 181 */ "idlist ::= idlist COMMA nm",
116512  /* 182 */ "idlist ::= nm",
116513  /* 183 */ "expr ::= term",
116514  /* 184 */ "expr ::= LP expr RP",
116515  /* 185 */ "term ::= NULL",
116516  /* 186 */ "expr ::= ID|INDEXED",
116517  /* 187 */ "expr ::= JOIN_KW",
116518  /* 188 */ "expr ::= nm DOT nm",
116519  /* 189 */ "expr ::= nm DOT nm DOT nm",
116520  /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
116521  /* 191 */ "term ::= STRING",
116522  /* 192 */ "expr ::= VARIABLE",
116523  /* 193 */ "expr ::= expr COLLATE ID|STRING",
116524  /* 194 */ "expr ::= CAST LP expr AS typetoken RP",
116525  /* 195 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
116526  /* 196 */ "expr ::= ID|INDEXED LP STAR RP",
116527  /* 197 */ "term ::= CTIME_KW",
116528  /* 198 */ "expr ::= expr AND expr",
116529  /* 199 */ "expr ::= expr OR expr",
116530  /* 200 */ "expr ::= expr LT|GT|GE|LE expr",
116531  /* 201 */ "expr ::= expr EQ|NE expr",
116532  /* 202 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
116533  /* 203 */ "expr ::= expr PLUS|MINUS expr",
116534  /* 204 */ "expr ::= expr STAR|SLASH|REM expr",
116535  /* 205 */ "expr ::= expr CONCAT expr",
116536  /* 206 */ "likeop ::= LIKE_KW|MATCH",
116537  /* 207 */ "likeop ::= NOT LIKE_KW|MATCH",
116538  /* 208 */ "expr ::= expr likeop expr",
116539  /* 209 */ "expr ::= expr likeop expr ESCAPE expr",
116540  /* 210 */ "expr ::= expr ISNULL|NOTNULL",
116541  /* 211 */ "expr ::= expr NOT NULL",
116542  /* 212 */ "expr ::= expr IS expr",
116543  /* 213 */ "expr ::= expr IS NOT expr",
116544  /* 214 */ "expr ::= NOT expr",
116545  /* 215 */ "expr ::= BITNOT expr",
116546  /* 216 */ "expr ::= MINUS expr",
116547  /* 217 */ "expr ::= PLUS expr",
116548  /* 218 */ "between_op ::= BETWEEN",
116549  /* 219 */ "between_op ::= NOT BETWEEN",
116550  /* 220 */ "expr ::= expr between_op expr AND expr",
116551  /* 221 */ "in_op ::= IN",
116552  /* 222 */ "in_op ::= NOT IN",
116553  /* 223 */ "expr ::= expr in_op LP exprlist RP",
116554  /* 224 */ "expr ::= LP select RP",
116555  /* 225 */ "expr ::= expr in_op LP select RP",
116556  /* 226 */ "expr ::= expr in_op nm dbnm",
116557  /* 227 */ "expr ::= EXISTS LP select RP",
116558  /* 228 */ "expr ::= CASE case_operand case_exprlist case_else END",
116559  /* 229 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
116560  /* 230 */ "case_exprlist ::= WHEN expr THEN expr",
116561  /* 231 */ "case_else ::= ELSE expr",
116562  /* 232 */ "case_else ::=",
116563  /* 233 */ "case_operand ::= expr",
116564  /* 234 */ "case_operand ::=",
116565  /* 235 */ "exprlist ::= nexprlist",
116566  /* 236 */ "exprlist ::=",
116567  /* 237 */ "nexprlist ::= nexprlist COMMA expr",
116568  /* 238 */ "nexprlist ::= expr",
116569  /* 239 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt",
116570  /* 240 */ "uniqueflag ::= UNIQUE",
116571  /* 241 */ "uniqueflag ::=",
116572  /* 242 */ "idxlist_opt ::=",
116573  /* 243 */ "idxlist_opt ::= LP idxlist RP",
116574  /* 244 */ "idxlist ::= idxlist COMMA nm collate sortorder",
116575  /* 245 */ "idxlist ::= nm collate sortorder",
116576  /* 246 */ "collate ::=",
116577  /* 247 */ "collate ::= COLLATE ID|STRING",
116578  /* 248 */ "cmd ::= DROP INDEX ifexists fullname",
116579  /* 249 */ "cmd ::= VACUUM",
116580  /* 250 */ "cmd ::= VACUUM nm",
116581  /* 251 */ "cmd ::= PRAGMA nm dbnm",
116582  /* 252 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
116583  /* 253 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
116584  /* 254 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
116585  /* 255 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
116586  /* 256 */ "nmnum ::= plus_num",
116587  /* 257 */ "nmnum ::= nm",
116588  /* 258 */ "nmnum ::= ON",
116589  /* 259 */ "nmnum ::= DELETE",
116590  /* 260 */ "nmnum ::= DEFAULT",
116591  /* 261 */ "plus_num ::= PLUS INTEGER|FLOAT",
116592  /* 262 */ "plus_num ::= INTEGER|FLOAT",
116593  /* 263 */ "minus_num ::= MINUS INTEGER|FLOAT",
116594  /* 264 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
116595  /* 265 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
116596  /* 266 */ "trigger_time ::= BEFORE",
116597  /* 267 */ "trigger_time ::= AFTER",
116598  /* 268 */ "trigger_time ::= INSTEAD OF",
116599  /* 269 */ "trigger_time ::=",
116600  /* 270 */ "trigger_event ::= DELETE|INSERT",
116601  /* 271 */ "trigger_event ::= UPDATE",
116602  /* 272 */ "trigger_event ::= UPDATE OF idlist",
116603  /* 273 */ "foreach_clause ::=",
116604  /* 274 */ "foreach_clause ::= FOR EACH ROW",
116605  /* 275 */ "when_clause ::=",
116606  /* 276 */ "when_clause ::= WHEN expr",
116607  /* 277 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
116608  /* 278 */ "trigger_cmd_list ::= trigger_cmd SEMI",
116609  /* 279 */ "trnm ::= nm",
116610  /* 280 */ "trnm ::= nm DOT nm",
116611  /* 281 */ "tridxby ::=",
116612  /* 282 */ "tridxby ::= INDEXED BY nm",
116613  /* 283 */ "tridxby ::= NOT INDEXED",
116614  /* 284 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
116615  /* 285 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
116616  /* 286 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
116617  /* 287 */ "trigger_cmd ::= select",
116618  /* 288 */ "expr ::= RAISE LP IGNORE RP",
116619  /* 289 */ "expr ::= RAISE LP raisetype COMMA nm RP",
116620  /* 290 */ "raisetype ::= ROLLBACK",
116621  /* 291 */ "raisetype ::= ABORT",
116622  /* 292 */ "raisetype ::= FAIL",
116623  /* 293 */ "cmd ::= DROP TRIGGER ifexists fullname",
116624  /* 294 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
116625  /* 295 */ "cmd ::= DETACH database_kw_opt expr",
116626  /* 296 */ "key_opt ::=",
116627  /* 297 */ "key_opt ::= KEY expr",
116628  /* 298 */ "database_kw_opt ::= DATABASE",
116629  /* 299 */ "database_kw_opt ::=",
116630  /* 300 */ "cmd ::= REINDEX",
116631  /* 301 */ "cmd ::= REINDEX nm dbnm",
116632  /* 302 */ "cmd ::= ANALYZE",
116633  /* 303 */ "cmd ::= ANALYZE nm dbnm",
116634  /* 304 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
116635  /* 305 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
116636  /* 306 */ "add_column_fullname ::= fullname",
116637  /* 307 */ "kwcolumn_opt ::=",
116638  /* 308 */ "kwcolumn_opt ::= COLUMNKW",
116639  /* 309 */ "cmd ::= create_vtab",
116640  /* 310 */ "cmd ::= create_vtab LP vtabarglist RP",
116641  /* 311 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
116642  /* 312 */ "vtabarglist ::= vtabarg",
116643  /* 313 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
116644  /* 314 */ "vtabarg ::=",
116645  /* 315 */ "vtabarg ::= vtabarg vtabargtoken",
116646  /* 316 */ "vtabargtoken ::= ANY",
116647  /* 317 */ "vtabargtoken ::= lp anylist RP",
116648  /* 318 */ "lp ::= LP",
116649  /* 319 */ "anylist ::=",
116650  /* 320 */ "anylist ::= anylist LP anylist RP",
116651  /* 321 */ "anylist ::= anylist ANY",
116652  /* 322 */ "with ::=",
116653  /* 323 */ "with ::= WITH wqlist",
116654  /* 324 */ "with ::= WITH RECURSIVE wqlist",
116655  /* 325 */ "wqlist ::= nm idxlist_opt AS LP select RP",
116656  /* 326 */ "wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP",
116657 };
116658 #endif /* NDEBUG */
116659 
116660 
116661 #if YYSTACKDEPTH<=0
116662 /*
116663 ** Try to increase the size of the parser stack.
116664 */
116665 static void yyGrowStack(yyParser *p){
116666   int newSize;
116667   yyStackEntry *pNew;
116668 
116669   newSize = p->yystksz*2 + 100;
116670   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
116671   if( pNew ){
116672     p->yystack = pNew;
116673     p->yystksz = newSize;
116674 #ifndef NDEBUG
116675     if( yyTraceFILE ){
116676       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
116677               yyTracePrompt, p->yystksz);
116678     }
116679 #endif
116680   }
116681 }
116682 #endif
116683 
116684 /*
116685 ** This function allocates a new parser.
116686 ** The only argument is a pointer to a function which works like
116687 ** malloc.
116688 **
116689 ** Inputs:
116690 ** A pointer to the function used to allocate memory.
116691 **
116692 ** Outputs:
116693 ** A pointer to a parser.  This pointer is used in subsequent calls
116694 ** to sqlite3Parser and sqlite3ParserFree.
116695 */
116696 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
116697   yyParser *pParser;
116698   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
116699   if( pParser ){
116700     pParser->yyidx = -1;
116701 #ifdef YYTRACKMAXSTACKDEPTH
116702     pParser->yyidxMax = 0;
116703 #endif
116704 #if YYSTACKDEPTH<=0
116705     pParser->yystack = NULL;
116706     pParser->yystksz = 0;
116707     yyGrowStack(pParser);
116708 #endif
116709   }
116710   return pParser;
116711 }
116712 
116713 /* The following function deletes the value associated with a
116714 ** symbol.  The symbol can be either a terminal or nonterminal.
116715 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
116716 ** the value.
116717 */
116718 static void yy_destructor(
116719   yyParser *yypParser,    /* The parser */
116720   YYCODETYPE yymajor,     /* Type code for object to destroy */
116721   YYMINORTYPE *yypminor   /* The object to be destroyed */
116722 ){
116723   sqlite3ParserARG_FETCH;
116724   switch( yymajor ){
116725     /* Here is inserted the actions which take place when a
116726     ** terminal or non-terminal is destroyed.  This can happen
116727     ** when the symbol is popped from the stack during a
116728     ** reduce or during error processing or when a parser is
116729     ** being destroyed before it is finished parsing.
116730     **
116731     ** Note: during a reduce, the only symbols destroyed are those
116732     ** which appear on the RHS of the rule, but which are not used
116733     ** inside the C code.
116734     */
116735     case 163: /* select */
116736     case 195: /* selectnowith */
116737     case 196: /* oneselect */
116738     case 207: /* values */
116739 {
116740 sqlite3SelectDelete(pParse->db, (yypminor->yy3));
116741 }
116742       break;
116743     case 174: /* term */
116744     case 175: /* expr */
116745 {
116746 sqlite3ExprDelete(pParse->db, (yypminor->yy346).pExpr);
116747 }
116748       break;
116749     case 179: /* idxlist_opt */
116750     case 188: /* idxlist */
116751     case 200: /* selcollist */
116752     case 203: /* groupby_opt */
116753     case 205: /* orderby_opt */
116754     case 208: /* nexprlist */
116755     case 209: /* exprlist */
116756     case 210: /* sclp */
116757     case 220: /* sortlist */
116758     case 221: /* setlist */
116759     case 228: /* case_exprlist */
116760 {
116761 sqlite3ExprListDelete(pParse->db, (yypminor->yy14));
116762 }
116763       break;
116764     case 194: /* fullname */
116765     case 201: /* from */
116766     case 212: /* seltablist */
116767     case 213: /* stl_prefix */
116768 {
116769 sqlite3SrcListDelete(pParse->db, (yypminor->yy65));
116770 }
116771       break;
116772     case 197: /* with */
116773     case 252: /* wqlist */
116774 {
116775 sqlite3WithDelete(pParse->db, (yypminor->yy59));
116776 }
116777       break;
116778     case 202: /* where_opt */
116779     case 204: /* having_opt */
116780     case 216: /* on_opt */
116781     case 227: /* case_operand */
116782     case 229: /* case_else */
116783     case 238: /* when_clause */
116784     case 243: /* key_opt */
116785 {
116786 sqlite3ExprDelete(pParse->db, (yypminor->yy132));
116787 }
116788       break;
116789     case 217: /* using_opt */
116790     case 219: /* idlist */
116791     case 223: /* inscollist_opt */
116792 {
116793 sqlite3IdListDelete(pParse->db, (yypminor->yy408));
116794 }
116795       break;
116796     case 234: /* trigger_cmd_list */
116797     case 239: /* trigger_cmd */
116798 {
116799 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy473));
116800 }
116801       break;
116802     case 236: /* trigger_event */
116803 {
116804 sqlite3IdListDelete(pParse->db, (yypminor->yy378).b);
116805 }
116806       break;
116807     default:  break;   /* If no destructor action specified: do nothing */
116808   }
116809 }
116810 
116811 /*
116812 ** Pop the parser's stack once.
116813 **
116814 ** If there is a destructor routine associated with the token which
116815 ** is popped from the stack, then call it.
116816 **
116817 ** Return the major token number for the symbol popped.
116818 */
116819 static int yy_pop_parser_stack(yyParser *pParser){
116820   YYCODETYPE yymajor;
116821   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
116822 
116823   /* There is no mechanism by which the parser stack can be popped below
116824   ** empty in SQLite.  */
116825   if( NEVER(pParser->yyidx<0) ) return 0;
116826 #ifndef NDEBUG
116827   if( yyTraceFILE && pParser->yyidx>=0 ){
116828     fprintf(yyTraceFILE,"%sPopping %s\n",
116829       yyTracePrompt,
116830       yyTokenName[yytos->major]);
116831   }
116832 #endif
116833   yymajor = yytos->major;
116834   yy_destructor(pParser, yymajor, &yytos->minor);
116835   pParser->yyidx--;
116836   return yymajor;
116837 }
116838 
116839 /*
116840 ** Deallocate and destroy a parser.  Destructors are all called for
116841 ** all stack elements before shutting the parser down.
116842 **
116843 ** Inputs:
116844 ** <ul>
116845 ** <li>  A pointer to the parser.  This should be a pointer
116846 **       obtained from sqlite3ParserAlloc.
116847 ** <li>  A pointer to a function used to reclaim memory obtained
116848 **       from malloc.
116849 ** </ul>
116850 */
116851 SQLITE_PRIVATE void sqlite3ParserFree(
116852   void *p,                    /* The parser to be deleted */
116853   void (*freeProc)(void*)     /* Function used to reclaim memory */
116854 ){
116855   yyParser *pParser = (yyParser*)p;
116856   /* In SQLite, we never try to destroy a parser that was not successfully
116857   ** created in the first place. */
116858   if( NEVER(pParser==0) ) return;
116859   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
116860 #if YYSTACKDEPTH<=0
116861   free(pParser->yystack);
116862 #endif
116863   (*freeProc)((void*)pParser);
116864 }
116865 
116866 /*
116867 ** Return the peak depth of the stack for a parser.
116868 */
116869 #ifdef YYTRACKMAXSTACKDEPTH
116870 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
116871   yyParser *pParser = (yyParser*)p;
116872   return pParser->yyidxMax;
116873 }
116874 #endif
116875 
116876 /*
116877 ** Find the appropriate action for a parser given the terminal
116878 ** look-ahead token iLookAhead.
116879 **
116880 ** If the look-ahead token is YYNOCODE, then check to see if the action is
116881 ** independent of the look-ahead.  If it is, return the action, otherwise
116882 ** return YY_NO_ACTION.
116883 */
116884 static int yy_find_shift_action(
116885   yyParser *pParser,        /* The parser */
116886   YYCODETYPE iLookAhead     /* The look-ahead token */
116887 ){
116888   int i;
116889   int stateno = pParser->yystack[pParser->yyidx].stateno;
116890 
116891   if( stateno>YY_SHIFT_COUNT
116892    || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
116893     return yy_default[stateno];
116894   }
116895   assert( iLookAhead!=YYNOCODE );
116896   i += iLookAhead;
116897   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
116898     if( iLookAhead>0 ){
116899 #ifdef YYFALLBACK
116900       YYCODETYPE iFallback;            /* Fallback token */
116901       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
116902              && (iFallback = yyFallback[iLookAhead])!=0 ){
116903 #ifndef NDEBUG
116904         if( yyTraceFILE ){
116905           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
116906              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
116907         }
116908 #endif
116909         return yy_find_shift_action(pParser, iFallback);
116910       }
116911 #endif
116912 #ifdef YYWILDCARD
116913       {
116914         int j = i - iLookAhead + YYWILDCARD;
116915         if(
116916 #if YY_SHIFT_MIN+YYWILDCARD<0
116917           j>=0 &&
116918 #endif
116919 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
116920           j<YY_ACTTAB_COUNT &&
116921 #endif
116922           yy_lookahead[j]==YYWILDCARD
116923         ){
116924 #ifndef NDEBUG
116925           if( yyTraceFILE ){
116926             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
116927                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
116928           }
116929 #endif /* NDEBUG */
116930           return yy_action[j];
116931         }
116932       }
116933 #endif /* YYWILDCARD */
116934     }
116935     return yy_default[stateno];
116936   }else{
116937     return yy_action[i];
116938   }
116939 }
116940 
116941 /*
116942 ** Find the appropriate action for a parser given the non-terminal
116943 ** look-ahead token iLookAhead.
116944 **
116945 ** If the look-ahead token is YYNOCODE, then check to see if the action is
116946 ** independent of the look-ahead.  If it is, return the action, otherwise
116947 ** return YY_NO_ACTION.
116948 */
116949 static int yy_find_reduce_action(
116950   int stateno,              /* Current state number */
116951   YYCODETYPE iLookAhead     /* The look-ahead token */
116952 ){
116953   int i;
116954 #ifdef YYERRORSYMBOL
116955   if( stateno>YY_REDUCE_COUNT ){
116956     return yy_default[stateno];
116957   }
116958 #else
116959   assert( stateno<=YY_REDUCE_COUNT );
116960 #endif
116961   i = yy_reduce_ofst[stateno];
116962   assert( i!=YY_REDUCE_USE_DFLT );
116963   assert( iLookAhead!=YYNOCODE );
116964   i += iLookAhead;
116965 #ifdef YYERRORSYMBOL
116966   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
116967     return yy_default[stateno];
116968   }
116969 #else
116970   assert( i>=0 && i<YY_ACTTAB_COUNT );
116971   assert( yy_lookahead[i]==iLookAhead );
116972 #endif
116973   return yy_action[i];
116974 }
116975 
116976 /*
116977 ** The following routine is called if the stack overflows.
116978 */
116979 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
116980    sqlite3ParserARG_FETCH;
116981    yypParser->yyidx--;
116982 #ifndef NDEBUG
116983    if( yyTraceFILE ){
116984      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
116985    }
116986 #endif
116987    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
116988    /* Here code is inserted which will execute if the parser
116989    ** stack every overflows */
116990 
116991   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
116992   sqlite3ErrorMsg(pParse, "parser stack overflow");
116993    sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
116994 }
116995 
116996 /*
116997 ** Perform a shift action.
116998 */
116999 static void yy_shift(
117000   yyParser *yypParser,          /* The parser to be shifted */
117001   int yyNewState,               /* The new state to shift in */
117002   int yyMajor,                  /* The major token to shift in */
117003   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
117004 ){
117005   yyStackEntry *yytos;
117006   yypParser->yyidx++;
117007 #ifdef YYTRACKMAXSTACKDEPTH
117008   if( yypParser->yyidx>yypParser->yyidxMax ){
117009     yypParser->yyidxMax = yypParser->yyidx;
117010   }
117011 #endif
117012 #if YYSTACKDEPTH>0
117013   if( yypParser->yyidx>=YYSTACKDEPTH ){
117014     yyStackOverflow(yypParser, yypMinor);
117015     return;
117016   }
117017 #else
117018   if( yypParser->yyidx>=yypParser->yystksz ){
117019     yyGrowStack(yypParser);
117020     if( yypParser->yyidx>=yypParser->yystksz ){
117021       yyStackOverflow(yypParser, yypMinor);
117022       return;
117023     }
117024   }
117025 #endif
117026   yytos = &yypParser->yystack[yypParser->yyidx];
117027   yytos->stateno = (YYACTIONTYPE)yyNewState;
117028   yytos->major = (YYCODETYPE)yyMajor;
117029   yytos->minor = *yypMinor;
117030 #ifndef NDEBUG
117031   if( yyTraceFILE && yypParser->yyidx>0 ){
117032     int i;
117033     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
117034     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
117035     for(i=1; i<=yypParser->yyidx; i++)
117036       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
117037     fprintf(yyTraceFILE,"\n");
117038   }
117039 #endif
117040 }
117041 
117042 /* The following table contains information about every rule that
117043 ** is used during the reduce.
117044 */
117045 static const struct {
117046   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
117047   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
117048 } yyRuleInfo[] = {
117049   { 144, 1 },
117050   { 145, 2 },
117051   { 145, 1 },
117052   { 146, 1 },
117053   { 146, 3 },
117054   { 147, 0 },
117055   { 147, 1 },
117056   { 147, 3 },
117057   { 148, 1 },
117058   { 149, 3 },
117059   { 151, 0 },
117060   { 151, 1 },
117061   { 151, 2 },
117062   { 150, 0 },
117063   { 150, 1 },
117064   { 150, 1 },
117065   { 150, 1 },
117066   { 149, 2 },
117067   { 149, 2 },
117068   { 149, 2 },
117069   { 153, 1 },
117070   { 153, 0 },
117071   { 149, 2 },
117072   { 149, 3 },
117073   { 149, 5 },
117074   { 149, 2 },
117075   { 154, 6 },
117076   { 156, 1 },
117077   { 158, 0 },
117078   { 158, 3 },
117079   { 157, 1 },
117080   { 157, 0 },
117081   { 155, 5 },
117082   { 155, 2 },
117083   { 162, 0 },
117084   { 162, 2 },
117085   { 160, 3 },
117086   { 160, 1 },
117087   { 164, 3 },
117088   { 165, 1 },
117089   { 152, 1 },
117090   { 152, 1 },
117091   { 152, 1 },
117092   { 166, 0 },
117093   { 166, 1 },
117094   { 168, 1 },
117095   { 168, 4 },
117096   { 168, 6 },
117097   { 169, 1 },
117098   { 169, 2 },
117099   { 170, 1 },
117100   { 170, 1 },
117101   { 167, 2 },
117102   { 167, 0 },
117103   { 173, 2 },
117104   { 173, 2 },
117105   { 173, 4 },
117106   { 173, 3 },
117107   { 173, 3 },
117108   { 173, 2 },
117109   { 173, 2 },
117110   { 173, 3 },
117111   { 173, 5 },
117112   { 173, 2 },
117113   { 173, 4 },
117114   { 173, 4 },
117115   { 173, 1 },
117116   { 173, 2 },
117117   { 178, 0 },
117118   { 178, 1 },
117119   { 180, 0 },
117120   { 180, 2 },
117121   { 182, 2 },
117122   { 182, 3 },
117123   { 182, 3 },
117124   { 182, 3 },
117125   { 183, 2 },
117126   { 183, 2 },
117127   { 183, 1 },
117128   { 183, 1 },
117129   { 183, 2 },
117130   { 181, 3 },
117131   { 181, 2 },
117132   { 184, 0 },
117133   { 184, 2 },
117134   { 184, 2 },
117135   { 161, 0 },
117136   { 161, 2 },
117137   { 185, 3 },
117138   { 185, 1 },
117139   { 186, 1 },
117140   { 186, 0 },
117141   { 187, 2 },
117142   { 187, 7 },
117143   { 187, 5 },
117144   { 187, 5 },
117145   { 187, 10 },
117146   { 189, 0 },
117147   { 189, 1 },
117148   { 176, 0 },
117149   { 176, 3 },
117150   { 190, 0 },
117151   { 190, 2 },
117152   { 191, 1 },
117153   { 191, 1 },
117154   { 191, 1 },
117155   { 149, 4 },
117156   { 193, 2 },
117157   { 193, 0 },
117158   { 149, 8 },
117159   { 149, 4 },
117160   { 149, 1 },
117161   { 163, 2 },
117162   { 195, 1 },
117163   { 195, 3 },
117164   { 198, 1 },
117165   { 198, 2 },
117166   { 198, 1 },
117167   { 196, 9 },
117168   { 196, 1 },
117169   { 207, 4 },
117170   { 207, 5 },
117171   { 199, 1 },
117172   { 199, 1 },
117173   { 199, 0 },
117174   { 210, 2 },
117175   { 210, 0 },
117176   { 200, 3 },
117177   { 200, 2 },
117178   { 200, 4 },
117179   { 211, 2 },
117180   { 211, 1 },
117181   { 211, 0 },
117182   { 201, 0 },
117183   { 201, 2 },
117184   { 213, 2 },
117185   { 213, 0 },
117186   { 212, 7 },
117187   { 212, 7 },
117188   { 212, 7 },
117189   { 159, 0 },
117190   { 159, 2 },
117191   { 194, 2 },
117192   { 214, 1 },
117193   { 214, 2 },
117194   { 214, 3 },
117195   { 214, 4 },
117196   { 216, 2 },
117197   { 216, 0 },
117198   { 215, 0 },
117199   { 215, 3 },
117200   { 215, 2 },
117201   { 217, 4 },
117202   { 217, 0 },
117203   { 205, 0 },
117204   { 205, 3 },
117205   { 220, 4 },
117206   { 220, 2 },
117207   { 177, 1 },
117208   { 177, 1 },
117209   { 177, 0 },
117210   { 203, 0 },
117211   { 203, 3 },
117212   { 204, 0 },
117213   { 204, 2 },
117214   { 206, 0 },
117215   { 206, 2 },
117216   { 206, 4 },
117217   { 206, 4 },
117218   { 149, 6 },
117219   { 202, 0 },
117220   { 202, 2 },
117221   { 149, 8 },
117222   { 221, 5 },
117223   { 221, 3 },
117224   { 149, 6 },
117225   { 149, 7 },
117226   { 222, 2 },
117227   { 222, 1 },
117228   { 223, 0 },
117229   { 223, 3 },
117230   { 219, 3 },
117231   { 219, 1 },
117232   { 175, 1 },
117233   { 175, 3 },
117234   { 174, 1 },
117235   { 175, 1 },
117236   { 175, 1 },
117237   { 175, 3 },
117238   { 175, 5 },
117239   { 174, 1 },
117240   { 174, 1 },
117241   { 175, 1 },
117242   { 175, 3 },
117243   { 175, 6 },
117244   { 175, 5 },
117245   { 175, 4 },
117246   { 174, 1 },
117247   { 175, 3 },
117248   { 175, 3 },
117249   { 175, 3 },
117250   { 175, 3 },
117251   { 175, 3 },
117252   { 175, 3 },
117253   { 175, 3 },
117254   { 175, 3 },
117255   { 224, 1 },
117256   { 224, 2 },
117257   { 175, 3 },
117258   { 175, 5 },
117259   { 175, 2 },
117260   { 175, 3 },
117261   { 175, 3 },
117262   { 175, 4 },
117263   { 175, 2 },
117264   { 175, 2 },
117265   { 175, 2 },
117266   { 175, 2 },
117267   { 225, 1 },
117268   { 225, 2 },
117269   { 175, 5 },
117270   { 226, 1 },
117271   { 226, 2 },
117272   { 175, 5 },
117273   { 175, 3 },
117274   { 175, 5 },
117275   { 175, 4 },
117276   { 175, 4 },
117277   { 175, 5 },
117278   { 228, 5 },
117279   { 228, 4 },
117280   { 229, 2 },
117281   { 229, 0 },
117282   { 227, 1 },
117283   { 227, 0 },
117284   { 209, 1 },
117285   { 209, 0 },
117286   { 208, 3 },
117287   { 208, 1 },
117288   { 149, 12 },
117289   { 230, 1 },
117290   { 230, 0 },
117291   { 179, 0 },
117292   { 179, 3 },
117293   { 188, 5 },
117294   { 188, 3 },
117295   { 231, 0 },
117296   { 231, 2 },
117297   { 149, 4 },
117298   { 149, 1 },
117299   { 149, 2 },
117300   { 149, 3 },
117301   { 149, 5 },
117302   { 149, 6 },
117303   { 149, 5 },
117304   { 149, 6 },
117305   { 232, 1 },
117306   { 232, 1 },
117307   { 232, 1 },
117308   { 232, 1 },
117309   { 232, 1 },
117310   { 171, 2 },
117311   { 171, 1 },
117312   { 172, 2 },
117313   { 149, 5 },
117314   { 233, 11 },
117315   { 235, 1 },
117316   { 235, 1 },
117317   { 235, 2 },
117318   { 235, 0 },
117319   { 236, 1 },
117320   { 236, 1 },
117321   { 236, 3 },
117322   { 237, 0 },
117323   { 237, 3 },
117324   { 238, 0 },
117325   { 238, 2 },
117326   { 234, 3 },
117327   { 234, 2 },
117328   { 240, 1 },
117329   { 240, 3 },
117330   { 241, 0 },
117331   { 241, 3 },
117332   { 241, 2 },
117333   { 239, 7 },
117334   { 239, 5 },
117335   { 239, 5 },
117336   { 239, 1 },
117337   { 175, 4 },
117338   { 175, 6 },
117339   { 192, 1 },
117340   { 192, 1 },
117341   { 192, 1 },
117342   { 149, 4 },
117343   { 149, 6 },
117344   { 149, 3 },
117345   { 243, 0 },
117346   { 243, 2 },
117347   { 242, 1 },
117348   { 242, 0 },
117349   { 149, 1 },
117350   { 149, 3 },
117351   { 149, 1 },
117352   { 149, 3 },
117353   { 149, 6 },
117354   { 149, 6 },
117355   { 244, 1 },
117356   { 245, 0 },
117357   { 245, 1 },
117358   { 149, 1 },
117359   { 149, 4 },
117360   { 246, 8 },
117361   { 247, 1 },
117362   { 247, 3 },
117363   { 248, 0 },
117364   { 248, 2 },
117365   { 249, 1 },
117366   { 249, 3 },
117367   { 250, 1 },
117368   { 251, 0 },
117369   { 251, 4 },
117370   { 251, 2 },
117371   { 197, 0 },
117372   { 197, 2 },
117373   { 197, 3 },
117374   { 252, 6 },
117375   { 252, 8 },
117376 };
117377 
117378 static void yy_accept(yyParser*);  /* Forward Declaration */
117379 
117380 /*
117381 ** Perform a reduce action and the shift that must immediately
117382 ** follow the reduce.
117383 */
117384 static void yy_reduce(
117385   yyParser *yypParser,         /* The parser */
117386   int yyruleno                 /* Number of the rule by which to reduce */
117387 ){
117388   int yygoto;                     /* The next state */
117389   int yyact;                      /* The next action */
117390   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
117391   yyStackEntry *yymsp;            /* The top of the parser's stack */
117392   int yysize;                     /* Amount to pop the stack */
117393   sqlite3ParserARG_FETCH;
117394   yymsp = &yypParser->yystack[yypParser->yyidx];
117395 #ifndef NDEBUG
117396   if( yyTraceFILE && yyruleno>=0
117397         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
117398     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
117399       yyRuleName[yyruleno]);
117400   }
117401 #endif /* NDEBUG */
117402 
117403   /* Silence complaints from purify about yygotominor being uninitialized
117404   ** in some cases when it is copied into the stack after the following
117405   ** switch.  yygotominor is uninitialized when a rule reduces that does
117406   ** not set the value of its left-hand side nonterminal.  Leaving the
117407   ** value of the nonterminal uninitialized is utterly harmless as long
117408   ** as the value is never used.  So really the only thing this code
117409   ** accomplishes is to quieten purify.
117410   **
117411   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
117412   ** without this code, their parser segfaults.  I'm not sure what there
117413   ** parser is doing to make this happen.  This is the second bug report
117414   ** from wireshark this week.  Clearly they are stressing Lemon in ways
117415   ** that it has not been previously stressed...  (SQLite ticket #2172)
117416   */
117417   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
117418   yygotominor = yyzerominor;
117419 
117420 
117421   switch( yyruleno ){
117422   /* Beginning here are the reduction cases.  A typical example
117423   ** follows:
117424   **   case 0:
117425   **  #line <lineno> <grammarfile>
117426   **     { ... }           // User supplied code
117427   **  #line <lineno> <thisfile>
117428   **     break;
117429   */
117430       case 5: /* explain ::= */
117431 { sqlite3BeginParse(pParse, 0); }
117432         break;
117433       case 6: /* explain ::= EXPLAIN */
117434 { sqlite3BeginParse(pParse, 1); }
117435         break;
117436       case 7: /* explain ::= EXPLAIN QUERY PLAN */
117437 { sqlite3BeginParse(pParse, 2); }
117438         break;
117439       case 8: /* cmdx ::= cmd */
117440 { sqlite3FinishCoding(pParse); }
117441         break;
117442       case 9: /* cmd ::= BEGIN transtype trans_opt */
117443 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy328);}
117444         break;
117445       case 13: /* transtype ::= */
117446 {yygotominor.yy328 = TK_DEFERRED;}
117447         break;
117448       case 14: /* transtype ::= DEFERRED */
117449       case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
117450       case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
117451       case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
117452       case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
117453 {yygotominor.yy328 = yymsp[0].major;}
117454         break;
117455       case 17: /* cmd ::= COMMIT trans_opt */
117456       case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
117457 {sqlite3CommitTransaction(pParse);}
117458         break;
117459       case 19: /* cmd ::= ROLLBACK trans_opt */
117460 {sqlite3RollbackTransaction(pParse);}
117461         break;
117462       case 22: /* cmd ::= SAVEPOINT nm */
117463 {
117464   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
117465 }
117466         break;
117467       case 23: /* cmd ::= RELEASE savepoint_opt nm */
117468 {
117469   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
117470 }
117471         break;
117472       case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
117473 {
117474   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
117475 }
117476         break;
117477       case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
117478 {
117479    sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy328,0,0,yymsp[-2].minor.yy328);
117480 }
117481         break;
117482       case 27: /* createkw ::= CREATE */
117483 {
117484   pParse->db->lookaside.bEnabled = 0;
117485   yygotominor.yy0 = yymsp[0].minor.yy0;
117486 }
117487         break;
117488       case 28: /* ifnotexists ::= */
117489       case 31: /* temp ::= */ yytestcase(yyruleno==31);
117490       case 68: /* autoinc ::= */ yytestcase(yyruleno==68);
117491       case 81: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==81);
117492       case 83: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==83);
117493       case 85: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==85);
117494       case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
117495       case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
117496       case 218: /* between_op ::= BETWEEN */ yytestcase(yyruleno==218);
117497       case 221: /* in_op ::= IN */ yytestcase(yyruleno==221);
117498 {yygotominor.yy328 = 0;}
117499         break;
117500       case 29: /* ifnotexists ::= IF NOT EXISTS */
117501       case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
117502       case 69: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==69);
117503       case 84: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==84);
117504       case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
117505       case 219: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==219);
117506       case 222: /* in_op ::= NOT IN */ yytestcase(yyruleno==222);
117507 {yygotominor.yy328 = 1;}
117508         break;
117509       case 32: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
117510 {
117511   sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy186,0);
117512 }
117513         break;
117514       case 33: /* create_table_args ::= AS select */
117515 {
117516   sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy3);
117517   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
117518 }
117519         break;
117520       case 34: /* table_options ::= */
117521 {yygotominor.yy186 = 0;}
117522         break;
117523       case 35: /* table_options ::= WITHOUT nm */
117524 {
117525   if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
117526     yygotominor.yy186 = TF_WithoutRowid;
117527   }else{
117528     yygotominor.yy186 = 0;
117529     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
117530   }
117531 }
117532         break;
117533       case 38: /* column ::= columnid type carglist */
117534 {
117535   yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
117536   yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
117537 }
117538         break;
117539       case 39: /* columnid ::= nm */
117540 {
117541   sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
117542   yygotominor.yy0 = yymsp[0].minor.yy0;
117543   pParse->constraintName.n = 0;
117544 }
117545         break;
117546       case 40: /* nm ::= ID|INDEXED */
117547       case 41: /* nm ::= STRING */ yytestcase(yyruleno==41);
117548       case 42: /* nm ::= JOIN_KW */ yytestcase(yyruleno==42);
117549       case 45: /* typetoken ::= typename */ yytestcase(yyruleno==45);
117550       case 48: /* typename ::= ID|STRING */ yytestcase(yyruleno==48);
117551       case 130: /* as ::= AS nm */ yytestcase(yyruleno==130);
117552       case 131: /* as ::= ID|STRING */ yytestcase(yyruleno==131);
117553       case 141: /* dbnm ::= DOT nm */ yytestcase(yyruleno==141);
117554       case 150: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==150);
117555       case 247: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==247);
117556       case 256: /* nmnum ::= plus_num */ yytestcase(yyruleno==256);
117557       case 257: /* nmnum ::= nm */ yytestcase(yyruleno==257);
117558       case 258: /* nmnum ::= ON */ yytestcase(yyruleno==258);
117559       case 259: /* nmnum ::= DELETE */ yytestcase(yyruleno==259);
117560       case 260: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==260);
117561       case 261: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==261);
117562       case 262: /* plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==262);
117563       case 263: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==263);
117564       case 279: /* trnm ::= nm */ yytestcase(yyruleno==279);
117565 {yygotominor.yy0 = yymsp[0].minor.yy0;}
117566         break;
117567       case 44: /* type ::= typetoken */
117568 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
117569         break;
117570       case 46: /* typetoken ::= typename LP signed RP */
117571 {
117572   yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
117573   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
117574 }
117575         break;
117576       case 47: /* typetoken ::= typename LP signed COMMA signed RP */
117577 {
117578   yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
117579   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
117580 }
117581         break;
117582       case 49: /* typename ::= typename ID|STRING */
117583 {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);}
117584         break;
117585       case 54: /* ccons ::= CONSTRAINT nm */
117586       case 92: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
117587 {pParse->constraintName = yymsp[0].minor.yy0;}
117588         break;
117589       case 55: /* ccons ::= DEFAULT term */
117590       case 57: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==57);
117591 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy346);}
117592         break;
117593       case 56: /* ccons ::= DEFAULT LP expr RP */
117594 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy346);}
117595         break;
117596       case 58: /* ccons ::= DEFAULT MINUS term */
117597 {
117598   ExprSpan v;
117599   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy346.pExpr, 0, 0);
117600   v.zStart = yymsp[-1].minor.yy0.z;
117601   v.zEnd = yymsp[0].minor.yy346.zEnd;
117602   sqlite3AddDefaultValue(pParse,&v);
117603 }
117604         break;
117605       case 59: /* ccons ::= DEFAULT ID|INDEXED */
117606 {
117607   ExprSpan v;
117608   spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
117609   sqlite3AddDefaultValue(pParse,&v);
117610 }
117611         break;
117612       case 61: /* ccons ::= NOT NULL onconf */
117613 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy328);}
117614         break;
117615       case 62: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
117616 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy328,yymsp[0].minor.yy328,yymsp[-2].minor.yy328);}
117617         break;
117618       case 63: /* ccons ::= UNIQUE onconf */
117619 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy328,0,0,0,0);}
117620         break;
117621       case 64: /* ccons ::= CHECK LP expr RP */
117622 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy346.pExpr);}
117623         break;
117624       case 65: /* ccons ::= REFERENCES nm idxlist_opt refargs */
117625 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy14,yymsp[0].minor.yy328);}
117626         break;
117627       case 66: /* ccons ::= defer_subclause */
117628 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy328);}
117629         break;
117630       case 67: /* ccons ::= COLLATE ID|STRING */
117631 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
117632         break;
117633       case 70: /* refargs ::= */
117634 { yygotominor.yy328 = OE_None*0x0101; /* EV: R-19803-45884 */}
117635         break;
117636       case 71: /* refargs ::= refargs refarg */
117637 { yygotominor.yy328 = (yymsp[-1].minor.yy328 & ~yymsp[0].minor.yy429.mask) | yymsp[0].minor.yy429.value; }
117638         break;
117639       case 72: /* refarg ::= MATCH nm */
117640       case 73: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==73);
117641 { yygotominor.yy429.value = 0;     yygotominor.yy429.mask = 0x000000; }
117642         break;
117643       case 74: /* refarg ::= ON DELETE refact */
117644 { yygotominor.yy429.value = yymsp[0].minor.yy328;     yygotominor.yy429.mask = 0x0000ff; }
117645         break;
117646       case 75: /* refarg ::= ON UPDATE refact */
117647 { yygotominor.yy429.value = yymsp[0].minor.yy328<<8;  yygotominor.yy429.mask = 0x00ff00; }
117648         break;
117649       case 76: /* refact ::= SET NULL */
117650 { yygotominor.yy328 = OE_SetNull;  /* EV: R-33326-45252 */}
117651         break;
117652       case 77: /* refact ::= SET DEFAULT */
117653 { yygotominor.yy328 = OE_SetDflt;  /* EV: R-33326-45252 */}
117654         break;
117655       case 78: /* refact ::= CASCADE */
117656 { yygotominor.yy328 = OE_Cascade;  /* EV: R-33326-45252 */}
117657         break;
117658       case 79: /* refact ::= RESTRICT */
117659 { yygotominor.yy328 = OE_Restrict; /* EV: R-33326-45252 */}
117660         break;
117661       case 80: /* refact ::= NO ACTION */
117662 { yygotominor.yy328 = OE_None;     /* EV: R-33326-45252 */}
117663         break;
117664       case 82: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
117665       case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
117666       case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
117667       case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
117668 {yygotominor.yy328 = yymsp[0].minor.yy328;}
117669         break;
117670       case 86: /* conslist_opt ::= */
117671 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
117672         break;
117673       case 87: /* conslist_opt ::= COMMA conslist */
117674 {yygotominor.yy0 = yymsp[-1].minor.yy0;}
117675         break;
117676       case 90: /* tconscomma ::= COMMA */
117677 {pParse->constraintName.n = 0;}
117678         break;
117679       case 93: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
117680 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy14,yymsp[0].minor.yy328,yymsp[-2].minor.yy328,0);}
117681         break;
117682       case 94: /* tcons ::= UNIQUE LP idxlist RP onconf */
117683 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy14,yymsp[0].minor.yy328,0,0,0,0);}
117684         break;
117685       case 95: /* tcons ::= CHECK LP expr RP onconf */
117686 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy346.pExpr);}
117687         break;
117688       case 96: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
117689 {
117690     sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy14, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[-1].minor.yy328);
117691     sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy328);
117692 }
117693         break;
117694       case 99: /* onconf ::= */
117695 {yygotominor.yy328 = OE_Default;}
117696         break;
117697       case 101: /* orconf ::= */
117698 {yygotominor.yy186 = OE_Default;}
117699         break;
117700       case 102: /* orconf ::= OR resolvetype */
117701 {yygotominor.yy186 = (u8)yymsp[0].minor.yy328;}
117702         break;
117703       case 104: /* resolvetype ::= IGNORE */
117704 {yygotominor.yy328 = OE_Ignore;}
117705         break;
117706       case 105: /* resolvetype ::= REPLACE */
117707 {yygotominor.yy328 = OE_Replace;}
117708         break;
117709       case 106: /* cmd ::= DROP TABLE ifexists fullname */
117710 {
117711   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 0, yymsp[-1].minor.yy328);
117712 }
117713         break;
117714       case 109: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
117715 {
117716   sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy3, yymsp[-6].minor.yy328, yymsp[-4].minor.yy328);
117717 }
117718         break;
117719       case 110: /* cmd ::= DROP VIEW ifexists fullname */
117720 {
117721   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 1, yymsp[-1].minor.yy328);
117722 }
117723         break;
117724       case 111: /* cmd ::= select */
117725 {
117726   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
117727   sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
117728   sqlite3ExplainBegin(pParse->pVdbe);
117729   sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy3);
117730   sqlite3ExplainFinish(pParse->pVdbe);
117731   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
117732 }
117733         break;
117734       case 112: /* select ::= with selectnowith */
117735 {
117736   Select *p = yymsp[0].minor.yy3, *pNext, *pLoop;
117737   if( p ){
117738     int cnt = 0, mxSelect;
117739     p->pWith = yymsp[-1].minor.yy59;
117740     if( p->pPrior ){
117741       pNext = 0;
117742       for(pLoop=p; pLoop; pNext=pLoop, pLoop=pLoop->pPrior, cnt++){
117743         pLoop->pNext = pNext;
117744         pLoop->selFlags |= SF_Compound;
117745       }
117746       mxSelect = pParse->db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
117747       if( mxSelect && cnt>mxSelect ){
117748         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
117749       }
117750     }
117751   }else{
117752     sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59);
117753   }
117754   yygotominor.yy3 = p;
117755 }
117756         break;
117757       case 113: /* selectnowith ::= oneselect */
117758       case 119: /* oneselect ::= values */ yytestcase(yyruleno==119);
117759 {yygotominor.yy3 = yymsp[0].minor.yy3;}
117760         break;
117761       case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
117762 {
117763   Select *pRhs = yymsp[0].minor.yy3;
117764   if( pRhs && pRhs->pPrior ){
117765     SrcList *pFrom;
117766     Token x;
117767     x.n = 0;
117768     pFrom = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&x,pRhs,0,0);
117769     pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0,0);
117770   }
117771   if( pRhs ){
117772     pRhs->op = (u8)yymsp[-1].minor.yy328;
117773     pRhs->pPrior = yymsp[-2].minor.yy3;
117774     if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
117775   }else{
117776     sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
117777   }
117778   yygotominor.yy3 = pRhs;
117779 }
117780         break;
117781       case 116: /* multiselect_op ::= UNION ALL */
117782 {yygotominor.yy328 = TK_ALL;}
117783         break;
117784       case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
117785 {
117786   yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy14,yymsp[-5].minor.yy65,yymsp[-4].minor.yy132,yymsp[-3].minor.yy14,yymsp[-2].minor.yy132,yymsp[-1].minor.yy14,yymsp[-7].minor.yy381,yymsp[0].minor.yy476.pLimit,yymsp[0].minor.yy476.pOffset);
117787 }
117788         break;
117789       case 120: /* values ::= VALUES LP nexprlist RP */
117790 {
117791   yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
117792 }
117793         break;
117794       case 121: /* values ::= values COMMA LP exprlist RP */
117795 {
117796   Select *pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
117797   if( pRight ){
117798     pRight->op = TK_ALL;
117799     pRight->pPrior = yymsp[-4].minor.yy3;
117800     yygotominor.yy3 = pRight;
117801   }else{
117802     yygotominor.yy3 = yymsp[-4].minor.yy3;
117803   }
117804 }
117805         break;
117806       case 122: /* distinct ::= DISTINCT */
117807 {yygotominor.yy381 = SF_Distinct;}
117808         break;
117809       case 123: /* distinct ::= ALL */
117810       case 124: /* distinct ::= */ yytestcase(yyruleno==124);
117811 {yygotominor.yy381 = 0;}
117812         break;
117813       case 125: /* sclp ::= selcollist COMMA */
117814       case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
117815 {yygotominor.yy14 = yymsp[-1].minor.yy14;}
117816         break;
117817       case 126: /* sclp ::= */
117818       case 154: /* orderby_opt ::= */ yytestcase(yyruleno==154);
117819       case 161: /* groupby_opt ::= */ yytestcase(yyruleno==161);
117820       case 236: /* exprlist ::= */ yytestcase(yyruleno==236);
117821       case 242: /* idxlist_opt ::= */ yytestcase(yyruleno==242);
117822 {yygotominor.yy14 = 0;}
117823         break;
117824       case 127: /* selcollist ::= sclp expr as */
117825 {
117826    yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy14, yymsp[-1].minor.yy346.pExpr);
117827    if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[0].minor.yy0, 1);
117828    sqlite3ExprListSetSpan(pParse,yygotominor.yy14,&yymsp[-1].minor.yy346);
117829 }
117830         break;
117831       case 128: /* selcollist ::= sclp STAR */
117832 {
117833   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
117834   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy14, p);
117835 }
117836         break;
117837       case 129: /* selcollist ::= sclp nm DOT STAR */
117838 {
117839   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
117840   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
117841   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
117842   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14, pDot);
117843 }
117844         break;
117845       case 132: /* as ::= */
117846 {yygotominor.yy0.n = 0;}
117847         break;
117848       case 133: /* from ::= */
117849 {yygotominor.yy65 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy65));}
117850         break;
117851       case 134: /* from ::= FROM seltablist */
117852 {
117853   yygotominor.yy65 = yymsp[0].minor.yy65;
117854   sqlite3SrcListShiftJoinType(yygotominor.yy65);
117855 }
117856         break;
117857       case 135: /* stl_prefix ::= seltablist joinop */
117858 {
117859    yygotominor.yy65 = yymsp[-1].minor.yy65;
117860    if( ALWAYS(yygotominor.yy65 && yygotominor.yy65->nSrc>0) ) yygotominor.yy65->a[yygotominor.yy65->nSrc-1].jointype = (u8)yymsp[0].minor.yy328;
117861 }
117862         break;
117863       case 136: /* stl_prefix ::= */
117864 {yygotominor.yy65 = 0;}
117865         break;
117866       case 137: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
117867 {
117868   yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
117869   sqlite3SrcListIndexedBy(pParse, yygotominor.yy65, &yymsp[-2].minor.yy0);
117870 }
117871         break;
117872       case 138: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
117873 {
117874     yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy3,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
117875   }
117876         break;
117877       case 139: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
117878 {
117879     if( yymsp[-6].minor.yy65==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy132==0 && yymsp[0].minor.yy408==0 ){
117880       yygotominor.yy65 = yymsp[-4].minor.yy65;
117881     }else if( yymsp[-4].minor.yy65->nSrc==1 ){
117882       yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
117883       if( yygotominor.yy65 ){
117884         struct SrcList_item *pNew = &yygotominor.yy65->a[yygotominor.yy65->nSrc-1];
117885         struct SrcList_item *pOld = yymsp[-4].minor.yy65->a;
117886         pNew->zName = pOld->zName;
117887         pNew->zDatabase = pOld->zDatabase;
117888         pNew->pSelect = pOld->pSelect;
117889         pOld->zName = pOld->zDatabase = 0;
117890         pOld->pSelect = 0;
117891       }
117892       sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy65);
117893     }else{
117894       Select *pSubquery;
117895       sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy65);
117896       pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy65,0,0,0,0,SF_NestedFrom,0,0);
117897       yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
117898     }
117899   }
117900         break;
117901       case 140: /* dbnm ::= */
117902       case 149: /* indexed_opt ::= */ yytestcase(yyruleno==149);
117903 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
117904         break;
117905       case 142: /* fullname ::= nm dbnm */
117906 {yygotominor.yy65 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
117907         break;
117908       case 143: /* joinop ::= COMMA|JOIN */
117909 { yygotominor.yy328 = JT_INNER; }
117910         break;
117911       case 144: /* joinop ::= JOIN_KW JOIN */
117912 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
117913         break;
117914       case 145: /* joinop ::= JOIN_KW nm JOIN */
117915 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
117916         break;
117917       case 146: /* joinop ::= JOIN_KW nm nm JOIN */
117918 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
117919         break;
117920       case 147: /* on_opt ::= ON expr */
117921       case 164: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==164);
117922       case 171: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==171);
117923       case 231: /* case_else ::= ELSE expr */ yytestcase(yyruleno==231);
117924       case 233: /* case_operand ::= expr */ yytestcase(yyruleno==233);
117925 {yygotominor.yy132 = yymsp[0].minor.yy346.pExpr;}
117926         break;
117927       case 148: /* on_opt ::= */
117928       case 163: /* having_opt ::= */ yytestcase(yyruleno==163);
117929       case 170: /* where_opt ::= */ yytestcase(yyruleno==170);
117930       case 232: /* case_else ::= */ yytestcase(yyruleno==232);
117931       case 234: /* case_operand ::= */ yytestcase(yyruleno==234);
117932 {yygotominor.yy132 = 0;}
117933         break;
117934       case 151: /* indexed_opt ::= NOT INDEXED */
117935 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
117936         break;
117937       case 152: /* using_opt ::= USING LP idlist RP */
117938       case 180: /* inscollist_opt ::= LP idlist RP */ yytestcase(yyruleno==180);
117939 {yygotominor.yy408 = yymsp[-1].minor.yy408;}
117940         break;
117941       case 153: /* using_opt ::= */
117942       case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
117943 {yygotominor.yy408 = 0;}
117944         break;
117945       case 155: /* orderby_opt ::= ORDER BY sortlist */
117946       case 162: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==162);
117947       case 235: /* exprlist ::= nexprlist */ yytestcase(yyruleno==235);
117948 {yygotominor.yy14 = yymsp[0].minor.yy14;}
117949         break;
117950       case 156: /* sortlist ::= sortlist COMMA expr sortorder */
117951 {
117952   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14,yymsp[-1].minor.yy346.pExpr);
117953   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
117954 }
117955         break;
117956       case 157: /* sortlist ::= expr sortorder */
117957 {
117958   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy346.pExpr);
117959   if( yygotominor.yy14 && ALWAYS(yygotominor.yy14->a) ) yygotominor.yy14->a[0].sortOrder = (u8)yymsp[0].minor.yy328;
117960 }
117961         break;
117962       case 158: /* sortorder ::= ASC */
117963       case 160: /* sortorder ::= */ yytestcase(yyruleno==160);
117964 {yygotominor.yy328 = SQLITE_SO_ASC;}
117965         break;
117966       case 159: /* sortorder ::= DESC */
117967 {yygotominor.yy328 = SQLITE_SO_DESC;}
117968         break;
117969       case 165: /* limit_opt ::= */
117970 {yygotominor.yy476.pLimit = 0; yygotominor.yy476.pOffset = 0;}
117971         break;
117972       case 166: /* limit_opt ::= LIMIT expr */
117973 {yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr; yygotominor.yy476.pOffset = 0;}
117974         break;
117975       case 167: /* limit_opt ::= LIMIT expr OFFSET expr */
117976 {yygotominor.yy476.pLimit = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pOffset = yymsp[0].minor.yy346.pExpr;}
117977         break;
117978       case 168: /* limit_opt ::= LIMIT expr COMMA expr */
117979 {yygotominor.yy476.pOffset = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr;}
117980         break;
117981       case 169: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */
117982 {
117983   sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
117984   sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy65, &yymsp[-1].minor.yy0);
117985   sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy65,yymsp[0].minor.yy132);
117986 }
117987         break;
117988       case 172: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */
117989 {
117990   sqlite3WithPush(pParse, yymsp[-7].minor.yy59, 1);
117991   sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy65, &yymsp[-3].minor.yy0);
117992   sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy14,"set list");
117993   sqlite3Update(pParse,yymsp[-4].minor.yy65,yymsp[-1].minor.yy14,yymsp[0].minor.yy132,yymsp[-5].minor.yy186);
117994 }
117995         break;
117996       case 173: /* setlist ::= setlist COMMA nm EQ expr */
117997 {
117998   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[0].minor.yy346.pExpr);
117999   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
118000 }
118001         break;
118002       case 174: /* setlist ::= nm EQ expr */
118003 {
118004   yygotominor.yy14 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy346.pExpr);
118005   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
118006 }
118007         break;
118008       case 175: /* cmd ::= with insert_cmd INTO fullname inscollist_opt select */
118009 {
118010   sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
118011   sqlite3Insert(pParse, yymsp[-2].minor.yy65, yymsp[0].minor.yy3, yymsp[-1].minor.yy408, yymsp[-4].minor.yy186);
118012 }
118013         break;
118014       case 176: /* cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
118015 {
118016   sqlite3WithPush(pParse, yymsp[-6].minor.yy59, 1);
118017   sqlite3Insert(pParse, yymsp[-3].minor.yy65, 0, yymsp[-2].minor.yy408, yymsp[-5].minor.yy186);
118018 }
118019         break;
118020       case 177: /* insert_cmd ::= INSERT orconf */
118021 {yygotominor.yy186 = yymsp[0].minor.yy186;}
118022         break;
118023       case 178: /* insert_cmd ::= REPLACE */
118024 {yygotominor.yy186 = OE_Replace;}
118025         break;
118026       case 181: /* idlist ::= idlist COMMA nm */
118027 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy408,&yymsp[0].minor.yy0);}
118028         break;
118029       case 182: /* idlist ::= nm */
118030 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
118031         break;
118032       case 183: /* expr ::= term */
118033 {yygotominor.yy346 = yymsp[0].minor.yy346;}
118034         break;
118035       case 184: /* expr ::= LP expr RP */
118036 {yygotominor.yy346.pExpr = yymsp[-1].minor.yy346.pExpr; spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
118037         break;
118038       case 185: /* term ::= NULL */
118039       case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
118040       case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
118041 {spanExpr(&yygotominor.yy346, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
118042         break;
118043       case 186: /* expr ::= ID|INDEXED */
118044       case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
118045 {spanExpr(&yygotominor.yy346, pParse, TK_ID, &yymsp[0].minor.yy0);}
118046         break;
118047       case 188: /* expr ::= nm DOT nm */
118048 {
118049   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
118050   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
118051   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
118052   spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
118053 }
118054         break;
118055       case 189: /* expr ::= nm DOT nm DOT nm */
118056 {
118057   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
118058   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
118059   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
118060   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
118061   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
118062   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
118063 }
118064         break;
118065       case 192: /* expr ::= VARIABLE */
118066 {
118067   if( yymsp[0].minor.yy0.n>=2 && yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1]) ){
118068     /* When doing a nested parse, one can include terms in an expression
118069     ** that look like this:   #1 #2 ...  These terms refer to registers
118070     ** in the virtual machine.  #N is the N-th register. */
118071     if( pParse->nested==0 ){
118072       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
118073       yygotominor.yy346.pExpr = 0;
118074     }else{
118075       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
118076       if( yygotominor.yy346.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy346.pExpr->iTable);
118077     }
118078   }else{
118079     spanExpr(&yygotominor.yy346, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
118080     sqlite3ExprAssignVarNumber(pParse, yygotominor.yy346.pExpr);
118081   }
118082   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
118083 }
118084         break;
118085       case 193: /* expr ::= expr COLLATE ID|STRING */
118086 {
118087   yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0);
118088   yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
118089   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118090 }
118091         break;
118092       case 194: /* expr ::= CAST LP expr AS typetoken RP */
118093 {
118094   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy346.pExpr, 0, &yymsp[-1].minor.yy0);
118095   spanSet(&yygotominor.yy346,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
118096 }
118097         break;
118098       case 195: /* expr ::= ID|INDEXED LP distinct exprlist RP */
118099 {
118100   if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
118101     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
118102   }
118103   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
118104   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
118105   if( yymsp[-2].minor.yy381 && yygotominor.yy346.pExpr ){
118106     yygotominor.yy346.pExpr->flags |= EP_Distinct;
118107   }
118108 }
118109         break;
118110       case 196: /* expr ::= ID|INDEXED LP STAR RP */
118111 {
118112   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
118113   spanSet(&yygotominor.yy346,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
118114 }
118115         break;
118116       case 197: /* term ::= CTIME_KW */
118117 {
118118   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0);
118119   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
118120 }
118121         break;
118122       case 198: /* expr ::= expr AND expr */
118123       case 199: /* expr ::= expr OR expr */ yytestcase(yyruleno==199);
118124       case 200: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==200);
118125       case 201: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==201);
118126       case 202: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==202);
118127       case 203: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==203);
118128       case 204: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==204);
118129       case 205: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==205);
118130 {spanBinaryExpr(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);}
118131         break;
118132       case 206: /* likeop ::= LIKE_KW|MATCH */
118133 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 0;}
118134         break;
118135       case 207: /* likeop ::= NOT LIKE_KW|MATCH */
118136 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 1;}
118137         break;
118138       case 208: /* expr ::= expr likeop expr */
118139 {
118140   ExprList *pList;
118141   pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy346.pExpr);
118142   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy346.pExpr);
118143   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy96.eOperator);
118144   if( yymsp[-1].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
118145   yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
118146   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
118147   if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
118148 }
118149         break;
118150       case 209: /* expr ::= expr likeop expr ESCAPE expr */
118151 {
118152   ExprList *pList;
118153   pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
118154   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy346.pExpr);
118155   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
118156   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy96.eOperator);
118157   if( yymsp[-3].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
118158   yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
118159   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
118160   if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
118161 }
118162         break;
118163       case 210: /* expr ::= expr ISNULL|NOTNULL */
118164 {spanUnaryPostfix(&yygotominor.yy346,pParse,yymsp[0].major,&yymsp[-1].minor.yy346,&yymsp[0].minor.yy0);}
118165         break;
118166       case 211: /* expr ::= expr NOT NULL */
118167 {spanUnaryPostfix(&yygotominor.yy346,pParse,TK_NOTNULL,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy0);}
118168         break;
118169       case 212: /* expr ::= expr IS expr */
118170 {
118171   spanBinaryExpr(&yygotominor.yy346,pParse,TK_IS,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);
118172   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_ISNULL);
118173 }
118174         break;
118175       case 213: /* expr ::= expr IS NOT expr */
118176 {
118177   spanBinaryExpr(&yygotominor.yy346,pParse,TK_ISNOT,&yymsp[-3].minor.yy346,&yymsp[0].minor.yy346);
118178   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_NOTNULL);
118179 }
118180         break;
118181       case 214: /* expr ::= NOT expr */
118182       case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
118183 {spanUnaryPrefix(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
118184         break;
118185       case 216: /* expr ::= MINUS expr */
118186 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UMINUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
118187         break;
118188       case 217: /* expr ::= PLUS expr */
118189 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UPLUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
118190         break;
118191       case 220: /* expr ::= expr between_op expr AND expr */
118192 {
118193   ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
118194   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
118195   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy346.pExpr, 0, 0);
118196   if( yygotominor.yy346.pExpr ){
118197     yygotominor.yy346.pExpr->x.pList = pList;
118198   }else{
118199     sqlite3ExprListDelete(pParse->db, pList);
118200   }
118201   if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
118202   yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
118203   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
118204 }
118205         break;
118206       case 223: /* expr ::= expr in_op LP exprlist RP */
118207 {
118208     if( yymsp[-1].minor.yy14==0 ){
118209       /* Expressions of the form
118210       **
118211       **      expr1 IN ()
118212       **      expr1 NOT IN ()
118213       **
118214       ** simplify to constants 0 (false) and 1 (true), respectively,
118215       ** regardless of the value of expr1.
118216       */
118217       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy328]);
118218       sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy346.pExpr);
118219     }else{
118220       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
118221       if( yygotominor.yy346.pExpr ){
118222         yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy14;
118223         sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
118224       }else{
118225         sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
118226       }
118227       if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
118228     }
118229     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
118230     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118231   }
118232         break;
118233       case 224: /* expr ::= LP select RP */
118234 {
118235     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
118236     if( yygotominor.yy346.pExpr ){
118237       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
118238       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
118239       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
118240     }else{
118241       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
118242     }
118243     yygotominor.yy346.zStart = yymsp[-2].minor.yy0.z;
118244     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118245   }
118246         break;
118247       case 225: /* expr ::= expr in_op LP select RP */
118248 {
118249     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
118250     if( yygotominor.yy346.pExpr ){
118251       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
118252       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
118253       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
118254     }else{
118255       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
118256     }
118257     if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
118258     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
118259     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118260   }
118261         break;
118262       case 226: /* expr ::= expr in_op nm dbnm */
118263 {
118264     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
118265     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy346.pExpr, 0, 0);
118266     if( yygotominor.yy346.pExpr ){
118267       yygotominor.yy346.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
118268       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
118269       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
118270     }else{
118271       sqlite3SrcListDelete(pParse->db, pSrc);
118272     }
118273     if( yymsp[-2].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
118274     yygotominor.yy346.zStart = yymsp[-3].minor.yy346.zStart;
118275     yygotominor.yy346.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n];
118276   }
118277         break;
118278       case 227: /* expr ::= EXISTS LP select RP */
118279 {
118280     Expr *p = yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
118281     if( p ){
118282       p->x.pSelect = yymsp[-1].minor.yy3;
118283       ExprSetProperty(p, EP_xIsSelect);
118284       sqlite3ExprSetHeight(pParse, p);
118285     }else{
118286       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
118287     }
118288     yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
118289     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118290   }
118291         break;
118292       case 228: /* expr ::= CASE case_operand case_exprlist case_else END */
118293 {
118294   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy132, 0, 0);
118295   if( yygotominor.yy346.pExpr ){
118296     yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy132 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[-1].minor.yy132) : yymsp[-2].minor.yy14;
118297     sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
118298   }else{
118299     sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy14);
118300     sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy132);
118301   }
118302   yygotominor.yy346.zStart = yymsp[-4].minor.yy0.z;
118303   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118304 }
118305         break;
118306       case 229: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
118307 {
118308   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[-2].minor.yy346.pExpr);
118309   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
118310 }
118311         break;
118312       case 230: /* case_exprlist ::= WHEN expr THEN expr */
118313 {
118314   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
118315   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
118316 }
118317         break;
118318       case 237: /* nexprlist ::= nexprlist COMMA expr */
118319 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[0].minor.yy346.pExpr);}
118320         break;
118321       case 238: /* nexprlist ::= expr */
118322 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy346.pExpr);}
118323         break;
118324       case 239: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt */
118325 {
118326   sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
118327                      sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy14, yymsp[-10].minor.yy328,
118328                       &yymsp[-11].minor.yy0, yymsp[0].minor.yy132, SQLITE_SO_ASC, yymsp[-8].minor.yy328);
118329 }
118330         break;
118331       case 240: /* uniqueflag ::= UNIQUE */
118332       case 291: /* raisetype ::= ABORT */ yytestcase(yyruleno==291);
118333 {yygotominor.yy328 = OE_Abort;}
118334         break;
118335       case 241: /* uniqueflag ::= */
118336 {yygotominor.yy328 = OE_None;}
118337         break;
118338       case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
118339 {
118340   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
118341   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
118342   sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
118343   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
118344   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
118345 }
118346         break;
118347       case 245: /* idxlist ::= nm collate sortorder */
118348 {
118349   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
118350   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
118351   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
118352   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
118353   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
118354 }
118355         break;
118356       case 246: /* collate ::= */
118357 {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
118358         break;
118359       case 248: /* cmd ::= DROP INDEX ifexists fullname */
118360 {sqlite3DropIndex(pParse, yymsp[0].minor.yy65, yymsp[-1].minor.yy328);}
118361         break;
118362       case 249: /* cmd ::= VACUUM */
118363       case 250: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==250);
118364 {sqlite3Vacuum(pParse);}
118365         break;
118366       case 251: /* cmd ::= PRAGMA nm dbnm */
118367 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
118368         break;
118369       case 252: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
118370 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
118371         break;
118372       case 253: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
118373 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
118374         break;
118375       case 254: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
118376 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
118377         break;
118378       case 255: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
118379 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
118380         break;
118381       case 264: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
118382 {
118383   Token all;
118384   all.z = yymsp[-3].minor.yy0.z;
118385   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
118386   sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy473, &all);
118387 }
118388         break;
118389       case 265: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
118390 {
118391   sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy328, yymsp[-4].minor.yy378.a, yymsp[-4].minor.yy378.b, yymsp[-2].minor.yy65, yymsp[0].minor.yy132, yymsp[-10].minor.yy328, yymsp[-8].minor.yy328);
118392   yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
118393 }
118394         break;
118395       case 266: /* trigger_time ::= BEFORE */
118396       case 269: /* trigger_time ::= */ yytestcase(yyruleno==269);
118397 { yygotominor.yy328 = TK_BEFORE; }
118398         break;
118399       case 267: /* trigger_time ::= AFTER */
118400 { yygotominor.yy328 = TK_AFTER;  }
118401         break;
118402       case 268: /* trigger_time ::= INSTEAD OF */
118403 { yygotominor.yy328 = TK_INSTEAD;}
118404         break;
118405       case 270: /* trigger_event ::= DELETE|INSERT */
118406       case 271: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==271);
118407 {yygotominor.yy378.a = yymsp[0].major; yygotominor.yy378.b = 0;}
118408         break;
118409       case 272: /* trigger_event ::= UPDATE OF idlist */
118410 {yygotominor.yy378.a = TK_UPDATE; yygotominor.yy378.b = yymsp[0].minor.yy408;}
118411         break;
118412       case 275: /* when_clause ::= */
118413       case 296: /* key_opt ::= */ yytestcase(yyruleno==296);
118414 { yygotominor.yy132 = 0; }
118415         break;
118416       case 276: /* when_clause ::= WHEN expr */
118417       case 297: /* key_opt ::= KEY expr */ yytestcase(yyruleno==297);
118418 { yygotominor.yy132 = yymsp[0].minor.yy346.pExpr; }
118419         break;
118420       case 277: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
118421 {
118422   assert( yymsp[-2].minor.yy473!=0 );
118423   yymsp[-2].minor.yy473->pLast->pNext = yymsp[-1].minor.yy473;
118424   yymsp[-2].minor.yy473->pLast = yymsp[-1].minor.yy473;
118425   yygotominor.yy473 = yymsp[-2].minor.yy473;
118426 }
118427         break;
118428       case 278: /* trigger_cmd_list ::= trigger_cmd SEMI */
118429 {
118430   assert( yymsp[-1].minor.yy473!=0 );
118431   yymsp[-1].minor.yy473->pLast = yymsp[-1].minor.yy473;
118432   yygotominor.yy473 = yymsp[-1].minor.yy473;
118433 }
118434         break;
118435       case 280: /* trnm ::= nm DOT nm */
118436 {
118437   yygotominor.yy0 = yymsp[0].minor.yy0;
118438   sqlite3ErrorMsg(pParse,
118439         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
118440         "statements within triggers");
118441 }
118442         break;
118443       case 282: /* tridxby ::= INDEXED BY nm */
118444 {
118445   sqlite3ErrorMsg(pParse,
118446         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
118447         "within triggers");
118448 }
118449         break;
118450       case 283: /* tridxby ::= NOT INDEXED */
118451 {
118452   sqlite3ErrorMsg(pParse,
118453         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
118454         "within triggers");
118455 }
118456         break;
118457       case 284: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
118458 { yygotominor.yy473 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy14, yymsp[0].minor.yy132, yymsp[-5].minor.yy186); }
118459         break;
118460       case 285: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
118461 {yygotominor.yy473 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy408, yymsp[0].minor.yy3, yymsp[-4].minor.yy186);}
118462         break;
118463       case 286: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
118464 {yygotominor.yy473 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy132);}
118465         break;
118466       case 287: /* trigger_cmd ::= select */
118467 {yygotominor.yy473 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy3); }
118468         break;
118469       case 288: /* expr ::= RAISE LP IGNORE RP */
118470 {
118471   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
118472   if( yygotominor.yy346.pExpr ){
118473     yygotominor.yy346.pExpr->affinity = OE_Ignore;
118474   }
118475   yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
118476   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118477 }
118478         break;
118479       case 289: /* expr ::= RAISE LP raisetype COMMA nm RP */
118480 {
118481   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0);
118482   if( yygotominor.yy346.pExpr ) {
118483     yygotominor.yy346.pExpr->affinity = (char)yymsp[-3].minor.yy328;
118484   }
118485   yygotominor.yy346.zStart = yymsp[-5].minor.yy0.z;
118486   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
118487 }
118488         break;
118489       case 290: /* raisetype ::= ROLLBACK */
118490 {yygotominor.yy328 = OE_Rollback;}
118491         break;
118492       case 292: /* raisetype ::= FAIL */
118493 {yygotominor.yy328 = OE_Fail;}
118494         break;
118495       case 293: /* cmd ::= DROP TRIGGER ifexists fullname */
118496 {
118497   sqlite3DropTrigger(pParse,yymsp[0].minor.yy65,yymsp[-1].minor.yy328);
118498 }
118499         break;
118500       case 294: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
118501 {
118502   sqlite3Attach(pParse, yymsp[-3].minor.yy346.pExpr, yymsp[-1].minor.yy346.pExpr, yymsp[0].minor.yy132);
118503 }
118504         break;
118505       case 295: /* cmd ::= DETACH database_kw_opt expr */
118506 {
118507   sqlite3Detach(pParse, yymsp[0].minor.yy346.pExpr);
118508 }
118509         break;
118510       case 300: /* cmd ::= REINDEX */
118511 {sqlite3Reindex(pParse, 0, 0);}
118512         break;
118513       case 301: /* cmd ::= REINDEX nm dbnm */
118514 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
118515         break;
118516       case 302: /* cmd ::= ANALYZE */
118517 {sqlite3Analyze(pParse, 0, 0);}
118518         break;
118519       case 303: /* cmd ::= ANALYZE nm dbnm */
118520 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
118521         break;
118522       case 304: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
118523 {
118524   sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy65,&yymsp[0].minor.yy0);
118525 }
118526         break;
118527       case 305: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
118528 {
118529   sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
118530 }
118531         break;
118532       case 306: /* add_column_fullname ::= fullname */
118533 {
118534   pParse->db->lookaside.bEnabled = 0;
118535   sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy65);
118536 }
118537         break;
118538       case 309: /* cmd ::= create_vtab */
118539 {sqlite3VtabFinishParse(pParse,0);}
118540         break;
118541       case 310: /* cmd ::= create_vtab LP vtabarglist RP */
118542 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
118543         break;
118544       case 311: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
118545 {
118546     sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy328);
118547 }
118548         break;
118549       case 314: /* vtabarg ::= */
118550 {sqlite3VtabArgInit(pParse);}
118551         break;
118552       case 316: /* vtabargtoken ::= ANY */
118553       case 317: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==317);
118554       case 318: /* lp ::= LP */ yytestcase(yyruleno==318);
118555 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
118556         break;
118557       case 322: /* with ::= */
118558 {yygotominor.yy59 = 0;}
118559         break;
118560       case 323: /* with ::= WITH wqlist */
118561       case 324: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==324);
118562 { yygotominor.yy59 = yymsp[0].minor.yy59; }
118563         break;
118564       case 325: /* wqlist ::= nm idxlist_opt AS LP select RP */
118565 {
118566   yygotominor.yy59 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
118567 }
118568         break;
118569       case 326: /* wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP */
118570 {
118571   yygotominor.yy59 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy59, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
118572 }
118573         break;
118574       default:
118575       /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
118576       /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
118577       /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
118578       /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
118579       /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
118580       /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
118581       /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
118582       /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
118583       /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
118584       /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
118585       /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
118586       /* (36) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==36);
118587       /* (37) columnlist ::= column */ yytestcase(yyruleno==37);
118588       /* (43) type ::= */ yytestcase(yyruleno==43);
118589       /* (50) signed ::= plus_num */ yytestcase(yyruleno==50);
118590       /* (51) signed ::= minus_num */ yytestcase(yyruleno==51);
118591       /* (52) carglist ::= carglist ccons */ yytestcase(yyruleno==52);
118592       /* (53) carglist ::= */ yytestcase(yyruleno==53);
118593       /* (60) ccons ::= NULL onconf */ yytestcase(yyruleno==60);
118594       /* (88) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==88);
118595       /* (89) conslist ::= tcons */ yytestcase(yyruleno==89);
118596       /* (91) tconscomma ::= */ yytestcase(yyruleno==91);
118597       /* (273) foreach_clause ::= */ yytestcase(yyruleno==273);
118598       /* (274) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==274);
118599       /* (281) tridxby ::= */ yytestcase(yyruleno==281);
118600       /* (298) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==298);
118601       /* (299) database_kw_opt ::= */ yytestcase(yyruleno==299);
118602       /* (307) kwcolumn_opt ::= */ yytestcase(yyruleno==307);
118603       /* (308) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==308);
118604       /* (312) vtabarglist ::= vtabarg */ yytestcase(yyruleno==312);
118605       /* (313) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==313);
118606       /* (315) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==315);
118607       /* (319) anylist ::= */ yytestcase(yyruleno==319);
118608       /* (320) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==320);
118609       /* (321) anylist ::= anylist ANY */ yytestcase(yyruleno==321);
118610         break;
118611   };
118612   assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
118613   yygoto = yyRuleInfo[yyruleno].lhs;
118614   yysize = yyRuleInfo[yyruleno].nrhs;
118615   yypParser->yyidx -= yysize;
118616   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
118617   if( yyact < YYNSTATE ){
118618 #ifdef NDEBUG
118619     /* If we are not debugging and the reduce action popped at least
118620     ** one element off the stack, then we can push the new element back
118621     ** onto the stack here, and skip the stack overflow test in yy_shift().
118622     ** That gives a significant speed improvement. */
118623     if( yysize ){
118624       yypParser->yyidx++;
118625       yymsp -= yysize-1;
118626       yymsp->stateno = (YYACTIONTYPE)yyact;
118627       yymsp->major = (YYCODETYPE)yygoto;
118628       yymsp->minor = yygotominor;
118629     }else
118630 #endif
118631     {
118632       yy_shift(yypParser,yyact,yygoto,&yygotominor);
118633     }
118634   }else{
118635     assert( yyact == YYNSTATE + YYNRULE + 1 );
118636     yy_accept(yypParser);
118637   }
118638 }
118639 
118640 /*
118641 ** The following code executes when the parse fails
118642 */
118643 #ifndef YYNOERRORRECOVERY
118644 static void yy_parse_failed(
118645   yyParser *yypParser           /* The parser */
118646 ){
118647   sqlite3ParserARG_FETCH;
118648 #ifndef NDEBUG
118649   if( yyTraceFILE ){
118650     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
118651   }
118652 #endif
118653   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
118654   /* Here code is inserted which will be executed whenever the
118655   ** parser fails */
118656   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
118657 }
118658 #endif /* YYNOERRORRECOVERY */
118659 
118660 /*
118661 ** The following code executes when a syntax error first occurs.
118662 */
118663 static void yy_syntax_error(
118664   yyParser *yypParser,           /* The parser */
118665   int yymajor,                   /* The major type of the error token */
118666   YYMINORTYPE yyminor            /* The minor type of the error token */
118667 ){
118668   sqlite3ParserARG_FETCH;
118669 #define TOKEN (yyminor.yy0)
118670 
118671   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
118672   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
118673   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
118674   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
118675 }
118676 
118677 /*
118678 ** The following is executed when the parser accepts
118679 */
118680 static void yy_accept(
118681   yyParser *yypParser           /* The parser */
118682 ){
118683   sqlite3ParserARG_FETCH;
118684 #ifndef NDEBUG
118685   if( yyTraceFILE ){
118686     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
118687   }
118688 #endif
118689   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
118690   /* Here code is inserted which will be executed whenever the
118691   ** parser accepts */
118692   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
118693 }
118694 
118695 /* The main parser program.
118696 ** The first argument is a pointer to a structure obtained from
118697 ** "sqlite3ParserAlloc" which describes the current state of the parser.
118698 ** The second argument is the major token number.  The third is
118699 ** the minor token.  The fourth optional argument is whatever the
118700 ** user wants (and specified in the grammar) and is available for
118701 ** use by the action routines.
118702 **
118703 ** Inputs:
118704 ** <ul>
118705 ** <li> A pointer to the parser (an opaque structure.)
118706 ** <li> The major token number.
118707 ** <li> The minor token number.
118708 ** <li> An option argument of a grammar-specified type.
118709 ** </ul>
118710 **
118711 ** Outputs:
118712 ** None.
118713 */
118714 SQLITE_PRIVATE void sqlite3Parser(
118715   void *yyp,                   /* The parser */
118716   int yymajor,                 /* The major token code number */
118717   sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
118718   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
118719 ){
118720   YYMINORTYPE yyminorunion;
118721   int yyact;            /* The parser action. */
118722 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
118723   int yyendofinput;     /* True if we are at the end of input */
118724 #endif
118725 #ifdef YYERRORSYMBOL
118726   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
118727 #endif
118728   yyParser *yypParser;  /* The parser */
118729 
118730   /* (re)initialize the parser, if necessary */
118731   yypParser = (yyParser*)yyp;
118732   if( yypParser->yyidx<0 ){
118733 #if YYSTACKDEPTH<=0
118734     if( yypParser->yystksz <=0 ){
118735       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
118736       yyminorunion = yyzerominor;
118737       yyStackOverflow(yypParser, &yyminorunion);
118738       return;
118739     }
118740 #endif
118741     yypParser->yyidx = 0;
118742     yypParser->yyerrcnt = -1;
118743     yypParser->yystack[0].stateno = 0;
118744     yypParser->yystack[0].major = 0;
118745   }
118746   yyminorunion.yy0 = yyminor;
118747 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
118748   yyendofinput = (yymajor==0);
118749 #endif
118750   sqlite3ParserARG_STORE;
118751 
118752 #ifndef NDEBUG
118753   if( yyTraceFILE ){
118754     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
118755   }
118756 #endif
118757 
118758   do{
118759     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
118760     if( yyact<YYNSTATE ){
118761       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
118762       yypParser->yyerrcnt--;
118763       yymajor = YYNOCODE;
118764     }else if( yyact < YYNSTATE + YYNRULE ){
118765       yy_reduce(yypParser,yyact-YYNSTATE);
118766     }else{
118767       assert( yyact == YY_ERROR_ACTION );
118768 #ifdef YYERRORSYMBOL
118769       int yymx;
118770 #endif
118771 #ifndef NDEBUG
118772       if( yyTraceFILE ){
118773         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
118774       }
118775 #endif
118776 #ifdef YYERRORSYMBOL
118777       /* A syntax error has occurred.
118778       ** The response to an error depends upon whether or not the
118779       ** grammar defines an error token "ERROR".
118780       **
118781       ** This is what we do if the grammar does define ERROR:
118782       **
118783       **  * Call the %syntax_error function.
118784       **
118785       **  * Begin popping the stack until we enter a state where
118786       **    it is legal to shift the error symbol, then shift
118787       **    the error symbol.
118788       **
118789       **  * Set the error count to three.
118790       **
118791       **  * Begin accepting and shifting new tokens.  No new error
118792       **    processing will occur until three tokens have been
118793       **    shifted successfully.
118794       **
118795       */
118796       if( yypParser->yyerrcnt<0 ){
118797         yy_syntax_error(yypParser,yymajor,yyminorunion);
118798       }
118799       yymx = yypParser->yystack[yypParser->yyidx].major;
118800       if( yymx==YYERRORSYMBOL || yyerrorhit ){
118801 #ifndef NDEBUG
118802         if( yyTraceFILE ){
118803           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
118804              yyTracePrompt,yyTokenName[yymajor]);
118805         }
118806 #endif
118807         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
118808         yymajor = YYNOCODE;
118809       }else{
118810          while(
118811           yypParser->yyidx >= 0 &&
118812           yymx != YYERRORSYMBOL &&
118813           (yyact = yy_find_reduce_action(
118814                         yypParser->yystack[yypParser->yyidx].stateno,
118815                         YYERRORSYMBOL)) >= YYNSTATE
118816         ){
118817           yy_pop_parser_stack(yypParser);
118818         }
118819         if( yypParser->yyidx < 0 || yymajor==0 ){
118820           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
118821           yy_parse_failed(yypParser);
118822           yymajor = YYNOCODE;
118823         }else if( yymx!=YYERRORSYMBOL ){
118824           YYMINORTYPE u2;
118825           u2.YYERRSYMDT = 0;
118826           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
118827         }
118828       }
118829       yypParser->yyerrcnt = 3;
118830       yyerrorhit = 1;
118831 #elif defined(YYNOERRORRECOVERY)
118832       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
118833       ** do any kind of error recovery.  Instead, simply invoke the syntax
118834       ** error routine and continue going as if nothing had happened.
118835       **
118836       ** Applications can set this macro (for example inside %include) if
118837       ** they intend to abandon the parse upon the first syntax error seen.
118838       */
118839       yy_syntax_error(yypParser,yymajor,yyminorunion);
118840       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
118841       yymajor = YYNOCODE;
118842 
118843 #else  /* YYERRORSYMBOL is not defined */
118844       /* This is what we do if the grammar does not define ERROR:
118845       **
118846       **  * Report an error message, and throw away the input token.
118847       **
118848       **  * If the input token is $, then fail the parse.
118849       **
118850       ** As before, subsequent error messages are suppressed until
118851       ** three input tokens have been successfully shifted.
118852       */
118853       if( yypParser->yyerrcnt<=0 ){
118854         yy_syntax_error(yypParser,yymajor,yyminorunion);
118855       }
118856       yypParser->yyerrcnt = 3;
118857       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
118858       if( yyendofinput ){
118859         yy_parse_failed(yypParser);
118860       }
118861       yymajor = YYNOCODE;
118862 #endif
118863     }
118864   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
118865   return;
118866 }
118867 
118868 /************** End of parse.c ***********************************************/
118869 /************** Begin file tokenize.c ****************************************/
118870 /*
118871 ** 2001 September 15
118872 **
118873 ** The author disclaims copyright to this source code.  In place of
118874 ** a legal notice, here is a blessing:
118875 **
118876 **    May you do good and not evil.
118877 **    May you find forgiveness for yourself and forgive others.
118878 **    May you share freely, never taking more than you give.
118879 **
118880 *************************************************************************
118881 ** An tokenizer for SQL
118882 **
118883 ** This file contains C code that splits an SQL input string up into
118884 ** individual tokens and sends those tokens one-by-one over to the
118885 ** parser for analysis.
118886 */
118887 /* #include <stdlib.h> */
118888 
118889 /*
118890 ** The charMap() macro maps alphabetic characters into their
118891 ** lower-case ASCII equivalent.  On ASCII machines, this is just
118892 ** an upper-to-lower case map.  On EBCDIC machines we also need
118893 ** to adjust the encoding.  Only alphabetic characters and underscores
118894 ** need to be translated.
118895 */
118896 #ifdef SQLITE_ASCII
118897 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
118898 #endif
118899 #ifdef SQLITE_EBCDIC
118900 # define charMap(X) ebcdicToAscii[(unsigned char)X]
118901 const unsigned char ebcdicToAscii[] = {
118902 /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
118903    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
118904    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
118905    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
118906    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
118907    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
118908    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
118909    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
118910    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
118911    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
118912    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
118913    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
118914    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
118915    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
118916    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
118917    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
118918    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
118919 };
118920 #endif
118921 
118922 /*
118923 ** The sqlite3KeywordCode function looks up an identifier to determine if
118924 ** it is a keyword.  If it is a keyword, the token code of that keyword is
118925 ** returned.  If the input is not a keyword, TK_ID is returned.
118926 **
118927 ** The implementation of this routine was generated by a program,
118928 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
118929 ** The output of the mkkeywordhash.c program is written into a file
118930 ** named keywordhash.h and then included into this source file by
118931 ** the #include below.
118932 */
118933 /************** Include keywordhash.h in the middle of tokenize.c ************/
118934 /************** Begin file keywordhash.h *************************************/
118935 /***** This file contains automatically generated code ******
118936 **
118937 ** The code in this file has been automatically generated by
118938 **
118939 **   sqlite/tool/mkkeywordhash.c
118940 **
118941 ** The code in this file implements a function that determines whether
118942 ** or not a given identifier is really an SQL keyword.  The same thing
118943 ** might be implemented more directly using a hand-written hash table.
118944 ** But by using this automatically generated code, the size of the code
118945 ** is substantially reduced.  This is important for embedded applications
118946 ** on platforms with limited memory.
118947 */
118948 /* Hash score: 182 */
118949 static int keywordCode(const char *z, int n){
118950   /* zText[] encodes 834 bytes of keywords in 554 bytes */
118951   /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
118952   /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
118953   /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
118954   /*   UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE         */
118955   /*   BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH     */
118956   /*   IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN     */
118957   /*   WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT         */
118958   /*   CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL        */
118959   /*   FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING        */
118960   /*   VACUUMVIEWINITIALLY                                                */
118961   static const char zText[553] = {
118962     'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
118963     'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
118964     'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
118965     'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
118966     'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
118967     'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
118968     'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
118969     'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
118970     'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
118971     'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
118972     'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
118973     'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
118974     'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E',
118975     'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A',
118976     'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A',
118977     'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A',
118978     'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J',
118979     'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L',
118980     'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E',
118981     'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H',
118982     'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E',
118983     'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E',
118984     'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M',
118985     'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R',
118986     'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A',
118987     'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
118988     'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O',
118989     'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T',
118990     'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R',
118991     'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M',
118992     'V','I','E','W','I','N','I','T','I','A','L','L','Y',
118993   };
118994   static const unsigned char aHash[127] = {
118995       76, 105, 117,  74,   0,  45,   0,   0,  82,   0,  77,   0,   0,
118996       42,  12,  78,  15,   0, 116,  85,  54, 112,   0,  19,   0,   0,
118997      121,   0, 119, 115,   0,  22,  93,   0,   9,   0,   0,  70,  71,
118998        0,  69,   6,   0,  48,  90, 102,   0, 118, 101,   0,   0,  44,
118999        0, 103,  24,   0,  17,   0, 122,  53,  23,   0,   5, 110,  25,
119000       96,   0,   0, 124, 106,  60, 123,  57,  28,  55,   0,  91,   0,
119001      100,  26,   0,  99,   0,   0,   0,  95,  92,  97,  88, 109,  14,
119002       39, 108,   0,  81,   0,  18,  89, 111,  32,   0, 120,  80, 113,
119003       62,  46,  84,   0,   0,  94,  40,  59, 114,   0,  36,   0,   0,
119004       29,   0,  86,  63,  64,   0,  20,  61,   0,  56,
119005   };
119006   static const unsigned char aNext[124] = {
119007        0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
119008        0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
119009        0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
119010        0,   0,   0,   0,  33,   0,  21,   0,   0,   0,   0,   0,  50,
119011        0,  43,   3,  47,   0,   0,   0,   0,  30,   0,  58,   0,  38,
119012        0,   0,   0,   1,  66,   0,   0,  67,   0,  41,   0,   0,   0,
119013        0,   0,   0,  49,  65,   0,   0,   0,   0,  31,  52,  16,  34,
119014       10,   0,   0,   0,   0,   0,   0,   0,  11,  72,  79,   0,   8,
119015        0, 104,  98,   0, 107,   0,  87,   0,  75,  51,   0,  27,  37,
119016       73,  83,   0,  35,  68,   0,   0,
119017   };
119018   static const unsigned char aLen[124] = {
119019        7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
119020        7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
119021       11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
119022        4,   6,   2,   3,   9,   4,   2,   6,   5,   7,   4,   5,   7,
119023        6,   6,   5,   6,   5,   5,   9,   7,   7,   3,   2,   4,   4,
119024        7,   3,   6,   4,   7,   6,  12,   6,   9,   4,   6,   5,   4,
119025        7,   6,   5,   6,   7,   5,   4,   5,   6,   5,   7,   3,   7,
119026       13,   2,   2,   4,   6,   6,   8,   5,  17,  12,   7,   8,   8,
119027        2,   4,   4,   4,   4,   4,   2,   2,   6,   5,   8,   5,   8,
119028        3,   5,   5,   6,   4,   9,   3,
119029   };
119030   static const unsigned short int aOffset[124] = {
119031        0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
119032       36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
119033       86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
119034      159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
119035      199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246,
119036      250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318,
119037      320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380,
119038      387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459,
119039      460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513,
119040      521, 524, 529, 534, 540, 544, 549,
119041   };
119042   static const unsigned char aCode[124] = {
119043     TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,
119044     TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,
119045     TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,
119046     TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,
119047     TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,
119048     TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,
119049     TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,
119050     TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
119051     TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,
119052     TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_WITHOUT,    TK_WITH,
119053     TK_JOIN_KW,    TK_RELEASE,    TK_ATTACH,     TK_HAVING,     TK_GROUP,
119054     TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RECURSIVE,  TK_BETWEEN,
119055     TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       TK_LIKE_KW,
119056     TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       TK_COLLATE,
119057     TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  TK_JOIN,
119058     TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    TK_PRAGMA,
119059     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      TK_WHEN,
119060     TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    TK_AND,
119061     TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         TK_CAST,
119062     TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    TK_CTIME_KW,
119063     TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   TK_IS,
119064     TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    TK_LIKE_KW,
119065     TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      TK_RESTRICT,
119066     TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        TK_UNION,      TK_USING,
119067     TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  TK_ALL,
119068   };
119069   int h, i;
119070   if( n<2 ) return TK_ID;
119071   h = ((charMap(z[0])*4) ^
119072       (charMap(z[n-1])*3) ^
119073       n) % 127;
119074   for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
119075     if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
119076       testcase( i==0 ); /* REINDEX */
119077       testcase( i==1 ); /* INDEXED */
119078       testcase( i==2 ); /* INDEX */
119079       testcase( i==3 ); /* DESC */
119080       testcase( i==4 ); /* ESCAPE */
119081       testcase( i==5 ); /* EACH */
119082       testcase( i==6 ); /* CHECK */
119083       testcase( i==7 ); /* KEY */
119084       testcase( i==8 ); /* BEFORE */
119085       testcase( i==9 ); /* FOREIGN */
119086       testcase( i==10 ); /* FOR */
119087       testcase( i==11 ); /* IGNORE */
119088       testcase( i==12 ); /* REGEXP */
119089       testcase( i==13 ); /* EXPLAIN */
119090       testcase( i==14 ); /* INSTEAD */
119091       testcase( i==15 ); /* ADD */
119092       testcase( i==16 ); /* DATABASE */
119093       testcase( i==17 ); /* AS */
119094       testcase( i==18 ); /* SELECT */
119095       testcase( i==19 ); /* TABLE */
119096       testcase( i==20 ); /* LEFT */
119097       testcase( i==21 ); /* THEN */
119098       testcase( i==22 ); /* END */
119099       testcase( i==23 ); /* DEFERRABLE */
119100       testcase( i==24 ); /* ELSE */
119101       testcase( i==25 ); /* EXCEPT */
119102       testcase( i==26 ); /* TRANSACTION */
119103       testcase( i==27 ); /* ACTION */
119104       testcase( i==28 ); /* ON */
119105       testcase( i==29 ); /* NATURAL */
119106       testcase( i==30 ); /* ALTER */
119107       testcase( i==31 ); /* RAISE */
119108       testcase( i==32 ); /* EXCLUSIVE */
119109       testcase( i==33 ); /* EXISTS */
119110       testcase( i==34 ); /* SAVEPOINT */
119111       testcase( i==35 ); /* INTERSECT */
119112       testcase( i==36 ); /* TRIGGER */
119113       testcase( i==37 ); /* REFERENCES */
119114       testcase( i==38 ); /* CONSTRAINT */
119115       testcase( i==39 ); /* INTO */
119116       testcase( i==40 ); /* OFFSET */
119117       testcase( i==41 ); /* OF */
119118       testcase( i==42 ); /* SET */
119119       testcase( i==43 ); /* TEMPORARY */
119120       testcase( i==44 ); /* TEMP */
119121       testcase( i==45 ); /* OR */
119122       testcase( i==46 ); /* UNIQUE */
119123       testcase( i==47 ); /* QUERY */
119124       testcase( i==48 ); /* WITHOUT */
119125       testcase( i==49 ); /* WITH */
119126       testcase( i==50 ); /* OUTER */
119127       testcase( i==51 ); /* RELEASE */
119128       testcase( i==52 ); /* ATTACH */
119129       testcase( i==53 ); /* HAVING */
119130       testcase( i==54 ); /* GROUP */
119131       testcase( i==55 ); /* UPDATE */
119132       testcase( i==56 ); /* BEGIN */
119133       testcase( i==57 ); /* INNER */
119134       testcase( i==58 ); /* RECURSIVE */
119135       testcase( i==59 ); /* BETWEEN */
119136       testcase( i==60 ); /* NOTNULL */
119137       testcase( i==61 ); /* NOT */
119138       testcase( i==62 ); /* NO */
119139       testcase( i==63 ); /* NULL */
119140       testcase( i==64 ); /* LIKE */
119141       testcase( i==65 ); /* CASCADE */
119142       testcase( i==66 ); /* ASC */
119143       testcase( i==67 ); /* DELETE */
119144       testcase( i==68 ); /* CASE */
119145       testcase( i==69 ); /* COLLATE */
119146       testcase( i==70 ); /* CREATE */
119147       testcase( i==71 ); /* CURRENT_DATE */
119148       testcase( i==72 ); /* DETACH */
119149       testcase( i==73 ); /* IMMEDIATE */
119150       testcase( i==74 ); /* JOIN */
119151       testcase( i==75 ); /* INSERT */
119152       testcase( i==76 ); /* MATCH */
119153       testcase( i==77 ); /* PLAN */
119154       testcase( i==78 ); /* ANALYZE */
119155       testcase( i==79 ); /* PRAGMA */
119156       testcase( i==80 ); /* ABORT */
119157       testcase( i==81 ); /* VALUES */
119158       testcase( i==82 ); /* VIRTUAL */
119159       testcase( i==83 ); /* LIMIT */
119160       testcase( i==84 ); /* WHEN */
119161       testcase( i==85 ); /* WHERE */
119162       testcase( i==86 ); /* RENAME */
119163       testcase( i==87 ); /* AFTER */
119164       testcase( i==88 ); /* REPLACE */
119165       testcase( i==89 ); /* AND */
119166       testcase( i==90 ); /* DEFAULT */
119167       testcase( i==91 ); /* AUTOINCREMENT */
119168       testcase( i==92 ); /* TO */
119169       testcase( i==93 ); /* IN */
119170       testcase( i==94 ); /* CAST */
119171       testcase( i==95 ); /* COLUMN */
119172       testcase( i==96 ); /* COMMIT */
119173       testcase( i==97 ); /* CONFLICT */
119174       testcase( i==98 ); /* CROSS */
119175       testcase( i==99 ); /* CURRENT_TIMESTAMP */
119176       testcase( i==100 ); /* CURRENT_TIME */
119177       testcase( i==101 ); /* PRIMARY */
119178       testcase( i==102 ); /* DEFERRED */
119179       testcase( i==103 ); /* DISTINCT */
119180       testcase( i==104 ); /* IS */
119181       testcase( i==105 ); /* DROP */
119182       testcase( i==106 ); /* FAIL */
119183       testcase( i==107 ); /* FROM */
119184       testcase( i==108 ); /* FULL */
119185       testcase( i==109 ); /* GLOB */
119186       testcase( i==110 ); /* BY */
119187       testcase( i==111 ); /* IF */
119188       testcase( i==112 ); /* ISNULL */
119189       testcase( i==113 ); /* ORDER */
119190       testcase( i==114 ); /* RESTRICT */
119191       testcase( i==115 ); /* RIGHT */
119192       testcase( i==116 ); /* ROLLBACK */
119193       testcase( i==117 ); /* ROW */
119194       testcase( i==118 ); /* UNION */
119195       testcase( i==119 ); /* USING */
119196       testcase( i==120 ); /* VACUUM */
119197       testcase( i==121 ); /* VIEW */
119198       testcase( i==122 ); /* INITIALLY */
119199       testcase( i==123 ); /* ALL */
119200       return aCode[i];
119201     }
119202   }
119203   return TK_ID;
119204 }
119205 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
119206   return keywordCode((char*)z, n);
119207 }
119208 #define SQLITE_N_KEYWORD 124
119209 
119210 /************** End of keywordhash.h *****************************************/
119211 /************** Continuing where we left off in tokenize.c *******************/
119212 
119213 
119214 /*
119215 ** If X is a character that can be used in an identifier then
119216 ** IdChar(X) will be true.  Otherwise it is false.
119217 **
119218 ** For ASCII, any character with the high-order bit set is
119219 ** allowed in an identifier.  For 7-bit characters,
119220 ** sqlite3IsIdChar[X] must be 1.
119221 **
119222 ** For EBCDIC, the rules are more complex but have the same
119223 ** end result.
119224 **
119225 ** Ticket #1066.  the SQL standard does not allow '$' in the
119226 ** middle of identfiers.  But many SQL implementations do.
119227 ** SQLite will allow '$' in identifiers for compatibility.
119228 ** But the feature is undocumented.
119229 */
119230 #ifdef SQLITE_ASCII
119231 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
119232 #endif
119233 #ifdef SQLITE_EBCDIC
119234 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
119235 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
119236     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
119237     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
119238     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
119239     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
119240     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
119241     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
119242     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
119243     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
119244     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
119245     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
119246     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
119247     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
119248 };
119249 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
119250 #endif
119251 
119252 
119253 /*
119254 ** Return the length of the token that begins at z[0].
119255 ** Store the token type in *tokenType before returning.
119256 */
119257 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
119258   int i, c;
119259   switch( *z ){
119260     case ' ': case '\t': case '\n': case '\f': case '\r': {
119261       testcase( z[0]==' ' );
119262       testcase( z[0]=='\t' );
119263       testcase( z[0]=='\n' );
119264       testcase( z[0]=='\f' );
119265       testcase( z[0]=='\r' );
119266       for(i=1; sqlite3Isspace(z[i]); i++){}
119267       *tokenType = TK_SPACE;
119268       return i;
119269     }
119270     case '-': {
119271       if( z[1]=='-' ){
119272         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
119273         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
119274         return i;
119275       }
119276       *tokenType = TK_MINUS;
119277       return 1;
119278     }
119279     case '(': {
119280       *tokenType = TK_LP;
119281       return 1;
119282     }
119283     case ')': {
119284       *tokenType = TK_RP;
119285       return 1;
119286     }
119287     case ';': {
119288       *tokenType = TK_SEMI;
119289       return 1;
119290     }
119291     case '+': {
119292       *tokenType = TK_PLUS;
119293       return 1;
119294     }
119295     case '*': {
119296       *tokenType = TK_STAR;
119297       return 1;
119298     }
119299     case '/': {
119300       if( z[1]!='*' || z[2]==0 ){
119301         *tokenType = TK_SLASH;
119302         return 1;
119303       }
119304       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
119305       if( c ) i++;
119306       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
119307       return i;
119308     }
119309     case '%': {
119310       *tokenType = TK_REM;
119311       return 1;
119312     }
119313     case '=': {
119314       *tokenType = TK_EQ;
119315       return 1 + (z[1]=='=');
119316     }
119317     case '<': {
119318       if( (c=z[1])=='=' ){
119319         *tokenType = TK_LE;
119320         return 2;
119321       }else if( c=='>' ){
119322         *tokenType = TK_NE;
119323         return 2;
119324       }else if( c=='<' ){
119325         *tokenType = TK_LSHIFT;
119326         return 2;
119327       }else{
119328         *tokenType = TK_LT;
119329         return 1;
119330       }
119331     }
119332     case '>': {
119333       if( (c=z[1])=='=' ){
119334         *tokenType = TK_GE;
119335         return 2;
119336       }else if( c=='>' ){
119337         *tokenType = TK_RSHIFT;
119338         return 2;
119339       }else{
119340         *tokenType = TK_GT;
119341         return 1;
119342       }
119343     }
119344     case '!': {
119345       if( z[1]!='=' ){
119346         *tokenType = TK_ILLEGAL;
119347         return 2;
119348       }else{
119349         *tokenType = TK_NE;
119350         return 2;
119351       }
119352     }
119353     case '|': {
119354       if( z[1]!='|' ){
119355         *tokenType = TK_BITOR;
119356         return 1;
119357       }else{
119358         *tokenType = TK_CONCAT;
119359         return 2;
119360       }
119361     }
119362     case ',': {
119363       *tokenType = TK_COMMA;
119364       return 1;
119365     }
119366     case '&': {
119367       *tokenType = TK_BITAND;
119368       return 1;
119369     }
119370     case '~': {
119371       *tokenType = TK_BITNOT;
119372       return 1;
119373     }
119374     case '`':
119375     case '\'':
119376     case '"': {
119377       int delim = z[0];
119378       testcase( delim=='`' );
119379       testcase( delim=='\'' );
119380       testcase( delim=='"' );
119381       for(i=1; (c=z[i])!=0; i++){
119382         if( c==delim ){
119383           if( z[i+1]==delim ){
119384             i++;
119385           }else{
119386             break;
119387           }
119388         }
119389       }
119390       if( c=='\'' ){
119391         *tokenType = TK_STRING;
119392         return i+1;
119393       }else if( c!=0 ){
119394         *tokenType = TK_ID;
119395         return i+1;
119396       }else{
119397         *tokenType = TK_ILLEGAL;
119398         return i;
119399       }
119400     }
119401     case '.': {
119402 #ifndef SQLITE_OMIT_FLOATING_POINT
119403       if( !sqlite3Isdigit(z[1]) )
119404 #endif
119405       {
119406         *tokenType = TK_DOT;
119407         return 1;
119408       }
119409       /* If the next character is a digit, this is a floating point
119410       ** number that begins with ".".  Fall thru into the next case */
119411     }
119412     case '0': case '1': case '2': case '3': case '4':
119413     case '5': case '6': case '7': case '8': case '9': {
119414       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
119415       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
119416       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
119417       testcase( z[0]=='9' );
119418       *tokenType = TK_INTEGER;
119419       for(i=0; sqlite3Isdigit(z[i]); i++){}
119420 #ifndef SQLITE_OMIT_FLOATING_POINT
119421       if( z[i]=='.' ){
119422         i++;
119423         while( sqlite3Isdigit(z[i]) ){ i++; }
119424         *tokenType = TK_FLOAT;
119425       }
119426       if( (z[i]=='e' || z[i]=='E') &&
119427            ( sqlite3Isdigit(z[i+1])
119428             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
119429            )
119430       ){
119431         i += 2;
119432         while( sqlite3Isdigit(z[i]) ){ i++; }
119433         *tokenType = TK_FLOAT;
119434       }
119435 #endif
119436       while( IdChar(z[i]) ){
119437         *tokenType = TK_ILLEGAL;
119438         i++;
119439       }
119440       return i;
119441     }
119442     case '[': {
119443       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
119444       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
119445       return i;
119446     }
119447     case '?': {
119448       *tokenType = TK_VARIABLE;
119449       for(i=1; sqlite3Isdigit(z[i]); i++){}
119450       return i;
119451     }
119452 #ifndef SQLITE_OMIT_TCL_VARIABLE
119453     case '$':
119454 #endif
119455     case '@':  /* For compatibility with MS SQL Server */
119456     case '#':
119457     case ':': {
119458       int n = 0;
119459       testcase( z[0]=='$' );  testcase( z[0]=='@' );
119460       testcase( z[0]==':' );  testcase( z[0]=='#' );
119461       *tokenType = TK_VARIABLE;
119462       for(i=1; (c=z[i])!=0; i++){
119463         if( IdChar(c) ){
119464           n++;
119465 #ifndef SQLITE_OMIT_TCL_VARIABLE
119466         }else if( c=='(' && n>0 ){
119467           do{
119468             i++;
119469           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
119470           if( c==')' ){
119471             i++;
119472           }else{
119473             *tokenType = TK_ILLEGAL;
119474           }
119475           break;
119476         }else if( c==':' && z[i+1]==':' ){
119477           i++;
119478 #endif
119479         }else{
119480           break;
119481         }
119482       }
119483       if( n==0 ) *tokenType = TK_ILLEGAL;
119484       return i;
119485     }
119486 #ifndef SQLITE_OMIT_BLOB_LITERAL
119487     case 'x': case 'X': {
119488       testcase( z[0]=='x' ); testcase( z[0]=='X' );
119489       if( z[1]=='\'' ){
119490         *tokenType = TK_BLOB;
119491         for(i=2; sqlite3Isxdigit(z[i]); i++){}
119492         if( z[i]!='\'' || i%2 ){
119493           *tokenType = TK_ILLEGAL;
119494           while( z[i] && z[i]!='\'' ){ i++; }
119495         }
119496         if( z[i] ) i++;
119497         return i;
119498       }
119499       /* Otherwise fall through to the next case */
119500     }
119501 #endif
119502     default: {
119503       if( !IdChar(*z) ){
119504         break;
119505       }
119506       for(i=1; IdChar(z[i]); i++){}
119507       *tokenType = keywordCode((char*)z, i);
119508       return i;
119509     }
119510   }
119511   *tokenType = TK_ILLEGAL;
119512   return 1;
119513 }
119514 
119515 /*
119516 ** Run the parser on the given SQL string.  The parser structure is
119517 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
119518 ** then an and attempt is made to write an error message into
119519 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
119520 ** error message.
119521 */
119522 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
119523   int nErr = 0;                   /* Number of errors encountered */
119524   int i;                          /* Loop counter */
119525   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
119526   int tokenType;                  /* type of the next token */
119527   int lastTokenParsed = -1;       /* type of the previous token */
119528   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
119529   sqlite3 *db = pParse->db;       /* The database connection */
119530   int mxSqlLen;                   /* Max length of an SQL string */
119531 
119532 
119533   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
119534   if( db->nVdbeActive==0 ){
119535     db->u1.isInterrupted = 0;
119536   }
119537   pParse->rc = SQLITE_OK;
119538   pParse->zTail = zSql;
119539   i = 0;
119540   assert( pzErrMsg!=0 );
119541   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
119542   if( pEngine==0 ){
119543     db->mallocFailed = 1;
119544     return SQLITE_NOMEM;
119545   }
119546   assert( pParse->pNewTable==0 );
119547   assert( pParse->pNewTrigger==0 );
119548   assert( pParse->nVar==0 );
119549   assert( pParse->nzVar==0 );
119550   assert( pParse->azVar==0 );
119551   enableLookaside = db->lookaside.bEnabled;
119552   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
119553   while( !db->mallocFailed && zSql[i]!=0 ){
119554     assert( i>=0 );
119555     pParse->sLastToken.z = &zSql[i];
119556     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
119557     i += pParse->sLastToken.n;
119558     if( i>mxSqlLen ){
119559       pParse->rc = SQLITE_TOOBIG;
119560       break;
119561     }
119562     switch( tokenType ){
119563       case TK_SPACE: {
119564         if( db->u1.isInterrupted ){
119565           sqlite3ErrorMsg(pParse, "interrupt");
119566           pParse->rc = SQLITE_INTERRUPT;
119567           goto abort_parse;
119568         }
119569         break;
119570       }
119571       case TK_ILLEGAL: {
119572         sqlite3DbFree(db, *pzErrMsg);
119573         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
119574                         &pParse->sLastToken);
119575         nErr++;
119576         goto abort_parse;
119577       }
119578       case TK_SEMI: {
119579         pParse->zTail = &zSql[i];
119580         /* Fall thru into the default case */
119581       }
119582       default: {
119583         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
119584         lastTokenParsed = tokenType;
119585         if( pParse->rc!=SQLITE_OK ){
119586           goto abort_parse;
119587         }
119588         break;
119589       }
119590     }
119591   }
119592 abort_parse:
119593   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
119594     if( lastTokenParsed!=TK_SEMI ){
119595       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
119596       pParse->zTail = &zSql[i];
119597     }
119598     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
119599   }
119600 #ifdef YYTRACKMAXSTACKDEPTH
119601   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
119602       sqlite3ParserStackPeak(pEngine)
119603   );
119604 #endif /* YYDEBUG */
119605   sqlite3ParserFree(pEngine, sqlite3_free);
119606   db->lookaside.bEnabled = enableLookaside;
119607   if( db->mallocFailed ){
119608     pParse->rc = SQLITE_NOMEM;
119609   }
119610   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
119611     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
119612   }
119613   assert( pzErrMsg!=0 );
119614   if( pParse->zErrMsg ){
119615     *pzErrMsg = pParse->zErrMsg;
119616     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
119617     pParse->zErrMsg = 0;
119618     nErr++;
119619   }
119620   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
119621     sqlite3VdbeDelete(pParse->pVdbe);
119622     pParse->pVdbe = 0;
119623   }
119624 #ifndef SQLITE_OMIT_SHARED_CACHE
119625   if( pParse->nested==0 ){
119626     sqlite3DbFree(db, pParse->aTableLock);
119627     pParse->aTableLock = 0;
119628     pParse->nTableLock = 0;
119629   }
119630 #endif
119631 #ifndef SQLITE_OMIT_VIRTUALTABLE
119632   sqlite3_free(pParse->apVtabLock);
119633 #endif
119634 
119635   if( !IN_DECLARE_VTAB ){
119636     /* If the pParse->declareVtab flag is set, do not delete any table
119637     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
119638     ** will take responsibility for freeing the Table structure.
119639     */
119640     sqlite3DeleteTable(db, pParse->pNewTable);
119641   }
119642 
119643   if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
119644   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
119645   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
119646   sqlite3DbFree(db, pParse->azVar);
119647   while( pParse->pAinc ){
119648     AutoincInfo *p = pParse->pAinc;
119649     pParse->pAinc = p->pNext;
119650     sqlite3DbFree(db, p);
119651   }
119652   while( pParse->pZombieTab ){
119653     Table *p = pParse->pZombieTab;
119654     pParse->pZombieTab = p->pNextZombie;
119655     sqlite3DeleteTable(db, p);
119656   }
119657   if( nErr>0 && pParse->rc==SQLITE_OK ){
119658     pParse->rc = SQLITE_ERROR;
119659   }
119660   return nErr;
119661 }
119662 
119663 /************** End of tokenize.c ********************************************/
119664 /************** Begin file complete.c ****************************************/
119665 /*
119666 ** 2001 September 15
119667 **
119668 ** The author disclaims copyright to this source code.  In place of
119669 ** a legal notice, here is a blessing:
119670 **
119671 **    May you do good and not evil.
119672 **    May you find forgiveness for yourself and forgive others.
119673 **    May you share freely, never taking more than you give.
119674 **
119675 *************************************************************************
119676 ** An tokenizer for SQL
119677 **
119678 ** This file contains C code that implements the sqlite3_complete() API.
119679 ** This code used to be part of the tokenizer.c source file.  But by
119680 ** separating it out, the code will be automatically omitted from
119681 ** static links that do not use it.
119682 */
119683 #ifndef SQLITE_OMIT_COMPLETE
119684 
119685 /*
119686 ** This is defined in tokenize.c.  We just have to import the definition.
119687 */
119688 #ifndef SQLITE_AMALGAMATION
119689 #ifdef SQLITE_ASCII
119690 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
119691 #endif
119692 #ifdef SQLITE_EBCDIC
119693 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
119694 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
119695 #endif
119696 #endif /* SQLITE_AMALGAMATION */
119697 
119698 
119699 /*
119700 ** Token types used by the sqlite3_complete() routine.  See the header
119701 ** comments on that procedure for additional information.
119702 */
119703 #define tkSEMI    0
119704 #define tkWS      1
119705 #define tkOTHER   2
119706 #ifndef SQLITE_OMIT_TRIGGER
119707 #define tkEXPLAIN 3
119708 #define tkCREATE  4
119709 #define tkTEMP    5
119710 #define tkTRIGGER 6
119711 #define tkEND     7
119712 #endif
119713 
119714 /*
119715 ** Return TRUE if the given SQL string ends in a semicolon.
119716 **
119717 ** Special handling is require for CREATE TRIGGER statements.
119718 ** Whenever the CREATE TRIGGER keywords are seen, the statement
119719 ** must end with ";END;".
119720 **
119721 ** This implementation uses a state machine with 8 states:
119722 **
119723 **   (0) INVALID   We have not yet seen a non-whitespace character.
119724 **
119725 **   (1) START     At the beginning or end of an SQL statement.  This routine
119726 **                 returns 1 if it ends in the START state and 0 if it ends
119727 **                 in any other state.
119728 **
119729 **   (2) NORMAL    We are in the middle of statement which ends with a single
119730 **                 semicolon.
119731 **
119732 **   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
119733 **                 a statement.
119734 **
119735 **   (4) CREATE    The keyword CREATE has been seen at the beginning of a
119736 **                 statement, possibly preceeded by EXPLAIN and/or followed by
119737 **                 TEMP or TEMPORARY
119738 **
119739 **   (5) TRIGGER   We are in the middle of a trigger definition that must be
119740 **                 ended by a semicolon, the keyword END, and another semicolon.
119741 **
119742 **   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
119743 **                 the end of a trigger definition.
119744 **
119745 **   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
119746 **                 of a trigger difinition.
119747 **
119748 ** Transitions between states above are determined by tokens extracted
119749 ** from the input.  The following tokens are significant:
119750 **
119751 **   (0) tkSEMI      A semicolon.
119752 **   (1) tkWS        Whitespace.
119753 **   (2) tkOTHER     Any other SQL token.
119754 **   (3) tkEXPLAIN   The "explain" keyword.
119755 **   (4) tkCREATE    The "create" keyword.
119756 **   (5) tkTEMP      The "temp" or "temporary" keyword.
119757 **   (6) tkTRIGGER   The "trigger" keyword.
119758 **   (7) tkEND       The "end" keyword.
119759 **
119760 ** Whitespace never causes a state transition and is always ignored.
119761 ** This means that a SQL string of all whitespace is invalid.
119762 **
119763 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
119764 ** to recognize the end of a trigger can be omitted.  All we have to do
119765 ** is look for a semicolon that is not part of an string or comment.
119766 */
119767 SQLITE_API int sqlite3_complete(const char *zSql){
119768   u8 state = 0;   /* Current state, using numbers defined in header comment */
119769   u8 token;       /* Value of the next token */
119770 
119771 #ifndef SQLITE_OMIT_TRIGGER
119772   /* A complex statement machine used to detect the end of a CREATE TRIGGER
119773   ** statement.  This is the normal case.
119774   */
119775   static const u8 trans[8][8] = {
119776                      /* Token:                                                */
119777      /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
119778      /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
119779      /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
119780      /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
119781      /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
119782      /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
119783      /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
119784      /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
119785      /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
119786   };
119787 #else
119788   /* If triggers are not supported by this compile then the statement machine
119789   ** used to detect the end of a statement is much simplier
119790   */
119791   static const u8 trans[3][3] = {
119792                      /* Token:           */
119793      /* State:       **  SEMI  WS  OTHER */
119794      /* 0 INVALID: */ {    1,  0,     2, },
119795      /* 1   START: */ {    1,  1,     2, },
119796      /* 2  NORMAL: */ {    1,  2,     2, },
119797   };
119798 #endif /* SQLITE_OMIT_TRIGGER */
119799 
119800   while( *zSql ){
119801     switch( *zSql ){
119802       case ';': {  /* A semicolon */
119803         token = tkSEMI;
119804         break;
119805       }
119806       case ' ':
119807       case '\r':
119808       case '\t':
119809       case '\n':
119810       case '\f': {  /* White space is ignored */
119811         token = tkWS;
119812         break;
119813       }
119814       case '/': {   /* C-style comments */
119815         if( zSql[1]!='*' ){
119816           token = tkOTHER;
119817           break;
119818         }
119819         zSql += 2;
119820         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
119821         if( zSql[0]==0 ) return 0;
119822         zSql++;
119823         token = tkWS;
119824         break;
119825       }
119826       case '-': {   /* SQL-style comments from "--" to end of line */
119827         if( zSql[1]!='-' ){
119828           token = tkOTHER;
119829           break;
119830         }
119831         while( *zSql && *zSql!='\n' ){ zSql++; }
119832         if( *zSql==0 ) return state==1;
119833         token = tkWS;
119834         break;
119835       }
119836       case '[': {   /* Microsoft-style identifiers in [...] */
119837         zSql++;
119838         while( *zSql && *zSql!=']' ){ zSql++; }
119839         if( *zSql==0 ) return 0;
119840         token = tkOTHER;
119841         break;
119842       }
119843       case '`':     /* Grave-accent quoted symbols used by MySQL */
119844       case '"':     /* single- and double-quoted strings */
119845       case '\'': {
119846         int c = *zSql;
119847         zSql++;
119848         while( *zSql && *zSql!=c ){ zSql++; }
119849         if( *zSql==0 ) return 0;
119850         token = tkOTHER;
119851         break;
119852       }
119853       default: {
119854 #ifdef SQLITE_EBCDIC
119855         unsigned char c;
119856 #endif
119857         if( IdChar((u8)*zSql) ){
119858           /* Keywords and unquoted identifiers */
119859           int nId;
119860           for(nId=1; IdChar(zSql[nId]); nId++){}
119861 #ifdef SQLITE_OMIT_TRIGGER
119862           token = tkOTHER;
119863 #else
119864           switch( *zSql ){
119865             case 'c': case 'C': {
119866               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
119867                 token = tkCREATE;
119868               }else{
119869                 token = tkOTHER;
119870               }
119871               break;
119872             }
119873             case 't': case 'T': {
119874               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
119875                 token = tkTRIGGER;
119876               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
119877                 token = tkTEMP;
119878               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
119879                 token = tkTEMP;
119880               }else{
119881                 token = tkOTHER;
119882               }
119883               break;
119884             }
119885             case 'e':  case 'E': {
119886               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
119887                 token = tkEND;
119888               }else
119889 #ifndef SQLITE_OMIT_EXPLAIN
119890               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
119891                 token = tkEXPLAIN;
119892               }else
119893 #endif
119894               {
119895                 token = tkOTHER;
119896               }
119897               break;
119898             }
119899             default: {
119900               token = tkOTHER;
119901               break;
119902             }
119903           }
119904 #endif /* SQLITE_OMIT_TRIGGER */
119905           zSql += nId-1;
119906         }else{
119907           /* Operators and special symbols */
119908           token = tkOTHER;
119909         }
119910         break;
119911       }
119912     }
119913     state = trans[state][token];
119914     zSql++;
119915   }
119916   return state==1;
119917 }
119918 
119919 #ifndef SQLITE_OMIT_UTF16
119920 /*
119921 ** This routine is the same as the sqlite3_complete() routine described
119922 ** above, except that the parameter is required to be UTF-16 encoded, not
119923 ** UTF-8.
119924 */
119925 SQLITE_API int sqlite3_complete16(const void *zSql){
119926   sqlite3_value *pVal;
119927   char const *zSql8;
119928   int rc = SQLITE_NOMEM;
119929 
119930 #ifndef SQLITE_OMIT_AUTOINIT
119931   rc = sqlite3_initialize();
119932   if( rc ) return rc;
119933 #endif
119934   pVal = sqlite3ValueNew(0);
119935   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
119936   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
119937   if( zSql8 ){
119938     rc = sqlite3_complete(zSql8);
119939   }else{
119940     rc = SQLITE_NOMEM;
119941   }
119942   sqlite3ValueFree(pVal);
119943   return sqlite3ApiExit(0, rc);
119944 }
119945 #endif /* SQLITE_OMIT_UTF16 */
119946 #endif /* SQLITE_OMIT_COMPLETE */
119947 
119948 /************** End of complete.c ********************************************/
119949 /************** Begin file main.c ********************************************/
119950 /*
119951 ** 2001 September 15
119952 **
119953 ** The author disclaims copyright to this source code.  In place of
119954 ** a legal notice, here is a blessing:
119955 **
119956 **    May you do good and not evil.
119957 **    May you find forgiveness for yourself and forgive others.
119958 **    May you share freely, never taking more than you give.
119959 **
119960 *************************************************************************
119961 ** Main file for the SQLite library.  The routines in this file
119962 ** implement the programmer interface to the library.  Routines in
119963 ** other files are for internal use by SQLite and should not be
119964 ** accessed by users of the library.
119965 */
119966 
119967 #ifdef SQLITE_ENABLE_FTS3
119968 /************** Include fts3.h in the middle of main.c ***********************/
119969 /************** Begin file fts3.h ********************************************/
119970 /*
119971 ** 2006 Oct 10
119972 **
119973 ** The author disclaims copyright to this source code.  In place of
119974 ** a legal notice, here is a blessing:
119975 **
119976 **    May you do good and not evil.
119977 **    May you find forgiveness for yourself and forgive others.
119978 **    May you share freely, never taking more than you give.
119979 **
119980 ******************************************************************************
119981 **
119982 ** This header file is used by programs that want to link against the
119983 ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
119984 */
119985 
119986 #if 0
119987 extern "C" {
119988 #endif  /* __cplusplus */
119989 
119990 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
119991 
119992 #if 0
119993 }  /* extern "C" */
119994 #endif  /* __cplusplus */
119995 
119996 /************** End of fts3.h ************************************************/
119997 /************** Continuing where we left off in main.c ***********************/
119998 #endif
119999 #ifdef SQLITE_ENABLE_RTREE
120000 /************** Include rtree.h in the middle of main.c **********************/
120001 /************** Begin file rtree.h *******************************************/
120002 /*
120003 ** 2008 May 26
120004 **
120005 ** The author disclaims copyright to this source code.  In place of
120006 ** a legal notice, here is a blessing:
120007 **
120008 **    May you do good and not evil.
120009 **    May you find forgiveness for yourself and forgive others.
120010 **    May you share freely, never taking more than you give.
120011 **
120012 ******************************************************************************
120013 **
120014 ** This header file is used by programs that want to link against the
120015 ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
120016 */
120017 
120018 #if 0
120019 extern "C" {
120020 #endif  /* __cplusplus */
120021 
120022 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
120023 
120024 #if 0
120025 }  /* extern "C" */
120026 #endif  /* __cplusplus */
120027 
120028 /************** End of rtree.h ***********************************************/
120029 /************** Continuing where we left off in main.c ***********************/
120030 #endif
120031 #ifdef SQLITE_ENABLE_ICU
120032 /************** Include sqliteicu.h in the middle of main.c ******************/
120033 /************** Begin file sqliteicu.h ***************************************/
120034 /*
120035 ** 2008 May 26
120036 **
120037 ** The author disclaims copyright to this source code.  In place of
120038 ** a legal notice, here is a blessing:
120039 **
120040 **    May you do good and not evil.
120041 **    May you find forgiveness for yourself and forgive others.
120042 **    May you share freely, never taking more than you give.
120043 **
120044 ******************************************************************************
120045 **
120046 ** This header file is used by programs that want to link against the
120047 ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
120048 */
120049 
120050 #if 0
120051 extern "C" {
120052 #endif  /* __cplusplus */
120053 
120054 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
120055 
120056 #if 0
120057 }  /* extern "C" */
120058 #endif  /* __cplusplus */
120059 
120060 
120061 /************** End of sqliteicu.h *******************************************/
120062 /************** Continuing where we left off in main.c ***********************/
120063 #endif
120064 
120065 #ifndef SQLITE_AMALGAMATION
120066 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
120067 ** contains the text of SQLITE_VERSION macro.
120068 */
120069 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
120070 #endif
120071 
120072 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
120073 ** a pointer to the to the sqlite3_version[] string constant.
120074 */
120075 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
120076 
120077 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
120078 ** pointer to a string constant whose value is the same as the
120079 ** SQLITE_SOURCE_ID C preprocessor macro.
120080 */
120081 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
120082 
120083 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
120084 ** returns an integer equal to SQLITE_VERSION_NUMBER.
120085 */
120086 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
120087 
120088 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
120089 ** zero if and only if SQLite was compiled with mutexing code omitted due to
120090 ** the SQLITE_THREADSAFE compile-time option being set to 0.
120091 */
120092 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
120093 
120094 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
120095 /*
120096 ** If the following function pointer is not NULL and if
120097 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
120098 ** I/O active are written using this function.  These messages
120099 ** are intended for debugging activity only.
120100 */
120101 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
120102 #endif
120103 
120104 /*
120105 ** If the following global variable points to a string which is the
120106 ** name of a directory, then that directory will be used to store
120107 ** temporary files.
120108 **
120109 ** See also the "PRAGMA temp_store_directory" SQL command.
120110 */
120111 SQLITE_API char *sqlite3_temp_directory = 0;
120112 
120113 /*
120114 ** If the following global variable points to a string which is the
120115 ** name of a directory, then that directory will be used to store
120116 ** all database files specified with a relative pathname.
120117 **
120118 ** See also the "PRAGMA data_store_directory" SQL command.
120119 */
120120 SQLITE_API char *sqlite3_data_directory = 0;
120121 
120122 /*
120123 ** Initialize SQLite.
120124 **
120125 ** This routine must be called to initialize the memory allocation,
120126 ** VFS, and mutex subsystems prior to doing any serious work with
120127 ** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
120128 ** this routine will be called automatically by key routines such as
120129 ** sqlite3_open().
120130 **
120131 ** This routine is a no-op except on its very first call for the process,
120132 ** or for the first call after a call to sqlite3_shutdown.
120133 **
120134 ** The first thread to call this routine runs the initialization to
120135 ** completion.  If subsequent threads call this routine before the first
120136 ** thread has finished the initialization process, then the subsequent
120137 ** threads must block until the first thread finishes with the initialization.
120138 **
120139 ** The first thread might call this routine recursively.  Recursive
120140 ** calls to this routine should not block, of course.  Otherwise the
120141 ** initialization process would never complete.
120142 **
120143 ** Let X be the first thread to enter this routine.  Let Y be some other
120144 ** thread.  Then while the initial invocation of this routine by X is
120145 ** incomplete, it is required that:
120146 **
120147 **    *  Calls to this routine from Y must block until the outer-most
120148 **       call by X completes.
120149 **
120150 **    *  Recursive calls to this routine from thread X return immediately
120151 **       without blocking.
120152 */
120153 SQLITE_API int sqlite3_initialize(void){
120154   MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
120155   int rc;                                      /* Result code */
120156 #ifdef SQLITE_EXTRA_INIT
120157   int bRunExtraInit = 0;                       /* Extra initialization needed */
120158 #endif
120159 
120160 #ifdef SQLITE_OMIT_WSD
120161   rc = sqlite3_wsd_init(4096, 24);
120162   if( rc!=SQLITE_OK ){
120163     return rc;
120164   }
120165 #endif
120166 
120167   /* If SQLite is already completely initialized, then this call
120168   ** to sqlite3_initialize() should be a no-op.  But the initialization
120169   ** must be complete.  So isInit must not be set until the very end
120170   ** of this routine.
120171   */
120172   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
120173 
120174   /* Make sure the mutex subsystem is initialized.  If unable to
120175   ** initialize the mutex subsystem, return early with the error.
120176   ** If the system is so sick that we are unable to allocate a mutex,
120177   ** there is not much SQLite is going to be able to do.
120178   **
120179   ** The mutex subsystem must take care of serializing its own
120180   ** initialization.
120181   */
120182   rc = sqlite3MutexInit();
120183   if( rc ) return rc;
120184 
120185   /* Initialize the malloc() system and the recursive pInitMutex mutex.
120186   ** This operation is protected by the STATIC_MASTER mutex.  Note that
120187   ** MutexAlloc() is called for a static mutex prior to initializing the
120188   ** malloc subsystem - this implies that the allocation of a static
120189   ** mutex must not require support from the malloc subsystem.
120190   */
120191   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
120192   sqlite3_mutex_enter(pMaster);
120193   sqlite3GlobalConfig.isMutexInit = 1;
120194   if( !sqlite3GlobalConfig.isMallocInit ){
120195     rc = sqlite3MallocInit();
120196   }
120197   if( rc==SQLITE_OK ){
120198     sqlite3GlobalConfig.isMallocInit = 1;
120199     if( !sqlite3GlobalConfig.pInitMutex ){
120200       sqlite3GlobalConfig.pInitMutex =
120201            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
120202       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
120203         rc = SQLITE_NOMEM;
120204       }
120205     }
120206   }
120207   if( rc==SQLITE_OK ){
120208     sqlite3GlobalConfig.nRefInitMutex++;
120209   }
120210   sqlite3_mutex_leave(pMaster);
120211 
120212   /* If rc is not SQLITE_OK at this point, then either the malloc
120213   ** subsystem could not be initialized or the system failed to allocate
120214   ** the pInitMutex mutex. Return an error in either case.  */
120215   if( rc!=SQLITE_OK ){
120216     return rc;
120217   }
120218 
120219   /* Do the rest of the initialization under the recursive mutex so
120220   ** that we will be able to handle recursive calls into
120221   ** sqlite3_initialize().  The recursive calls normally come through
120222   ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
120223   ** recursive calls might also be possible.
120224   **
120225   ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
120226   ** to the xInit method, so the xInit method need not be threadsafe.
120227   **
120228   ** The following mutex is what serializes access to the appdef pcache xInit
120229   ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
120230   ** call to sqlite3PcacheInitialize().
120231   */
120232   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
120233   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
120234     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
120235     sqlite3GlobalConfig.inProgress = 1;
120236     memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
120237     sqlite3RegisterGlobalFunctions();
120238     if( sqlite3GlobalConfig.isPCacheInit==0 ){
120239       rc = sqlite3PcacheInitialize();
120240     }
120241     if( rc==SQLITE_OK ){
120242       sqlite3GlobalConfig.isPCacheInit = 1;
120243       rc = sqlite3OsInit();
120244     }
120245     if( rc==SQLITE_OK ){
120246       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
120247           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
120248       sqlite3GlobalConfig.isInit = 1;
120249 #ifdef SQLITE_EXTRA_INIT
120250       bRunExtraInit = 1;
120251 #endif
120252     }
120253     sqlite3GlobalConfig.inProgress = 0;
120254   }
120255   sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
120256 
120257   /* Go back under the static mutex and clean up the recursive
120258   ** mutex to prevent a resource leak.
120259   */
120260   sqlite3_mutex_enter(pMaster);
120261   sqlite3GlobalConfig.nRefInitMutex--;
120262   if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
120263     assert( sqlite3GlobalConfig.nRefInitMutex==0 );
120264     sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
120265     sqlite3GlobalConfig.pInitMutex = 0;
120266   }
120267   sqlite3_mutex_leave(pMaster);
120268 
120269   /* The following is just a sanity check to make sure SQLite has
120270   ** been compiled correctly.  It is important to run this code, but
120271   ** we don't want to run it too often and soak up CPU cycles for no
120272   ** reason.  So we run it once during initialization.
120273   */
120274 #ifndef NDEBUG
120275 #ifndef SQLITE_OMIT_FLOATING_POINT
120276   /* This section of code's only "output" is via assert() statements. */
120277   if ( rc==SQLITE_OK ){
120278     u64 x = (((u64)1)<<63)-1;
120279     double y;
120280     assert(sizeof(x)==8);
120281     assert(sizeof(x)==sizeof(y));
120282     memcpy(&y, &x, 8);
120283     assert( sqlite3IsNaN(y) );
120284   }
120285 #endif
120286 #endif
120287 
120288   /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
120289   ** compile-time option.
120290   */
120291 #ifdef SQLITE_EXTRA_INIT
120292   if( bRunExtraInit ){
120293     int SQLITE_EXTRA_INIT(const char*);
120294     rc = SQLITE_EXTRA_INIT(0);
120295   }
120296 #endif
120297 
120298   return rc;
120299 }
120300 
120301 /*
120302 ** Undo the effects of sqlite3_initialize().  Must not be called while
120303 ** there are outstanding database connections or memory allocations or
120304 ** while any part of SQLite is otherwise in use in any thread.  This
120305 ** routine is not threadsafe.  But it is safe to invoke this routine
120306 ** on when SQLite is already shut down.  If SQLite is already shut down
120307 ** when this routine is invoked, then this routine is a harmless no-op.
120308 */
120309 SQLITE_API int sqlite3_shutdown(void){
120310   if( sqlite3GlobalConfig.isInit ){
120311 #ifdef SQLITE_EXTRA_SHUTDOWN
120312     void SQLITE_EXTRA_SHUTDOWN(void);
120313     SQLITE_EXTRA_SHUTDOWN();
120314 #endif
120315     sqlite3_os_end();
120316     sqlite3_reset_auto_extension();
120317     sqlite3GlobalConfig.isInit = 0;
120318   }
120319   if( sqlite3GlobalConfig.isPCacheInit ){
120320     sqlite3PcacheShutdown();
120321     sqlite3GlobalConfig.isPCacheInit = 0;
120322   }
120323   if( sqlite3GlobalConfig.isMallocInit ){
120324     sqlite3MallocEnd();
120325     sqlite3GlobalConfig.isMallocInit = 0;
120326 
120327 #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
120328     /* The heap subsystem has now been shutdown and these values are supposed
120329     ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
120330     ** which would rely on that heap subsystem; therefore, make sure these
120331     ** values cannot refer to heap memory that was just invalidated when the
120332     ** heap subsystem was shutdown.  This is only done if the current call to
120333     ** this function resulted in the heap subsystem actually being shutdown.
120334     */
120335     sqlite3_data_directory = 0;
120336     sqlite3_temp_directory = 0;
120337 #endif
120338   }
120339   if( sqlite3GlobalConfig.isMutexInit ){
120340     sqlite3MutexEnd();
120341     sqlite3GlobalConfig.isMutexInit = 0;
120342   }
120343 
120344   return SQLITE_OK;
120345 }
120346 
120347 /*
120348 ** This API allows applications to modify the global configuration of
120349 ** the SQLite library at run-time.
120350 **
120351 ** This routine should only be called when there are no outstanding
120352 ** database connections or memory allocations.  This routine is not
120353 ** threadsafe.  Failure to heed these warnings can lead to unpredictable
120354 ** behavior.
120355 */
120356 SQLITE_API int sqlite3_config(int op, ...){
120357   va_list ap;
120358   int rc = SQLITE_OK;
120359 
120360   /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
120361   ** the SQLite library is in use. */
120362   if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
120363 
120364   va_start(ap, op);
120365   switch( op ){
120366 
120367     /* Mutex configuration options are only available in a threadsafe
120368     ** compile.
120369     */
120370 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
120371     case SQLITE_CONFIG_SINGLETHREAD: {
120372       /* Disable all mutexing */
120373       sqlite3GlobalConfig.bCoreMutex = 0;
120374       sqlite3GlobalConfig.bFullMutex = 0;
120375       break;
120376     }
120377     case SQLITE_CONFIG_MULTITHREAD: {
120378       /* Disable mutexing of database connections */
120379       /* Enable mutexing of core data structures */
120380       sqlite3GlobalConfig.bCoreMutex = 1;
120381       sqlite3GlobalConfig.bFullMutex = 0;
120382       break;
120383     }
120384     case SQLITE_CONFIG_SERIALIZED: {
120385       /* Enable all mutexing */
120386       sqlite3GlobalConfig.bCoreMutex = 1;
120387       sqlite3GlobalConfig.bFullMutex = 1;
120388       break;
120389     }
120390     case SQLITE_CONFIG_MUTEX: {
120391       /* Specify an alternative mutex implementation */
120392       sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
120393       break;
120394     }
120395     case SQLITE_CONFIG_GETMUTEX: {
120396       /* Retrieve the current mutex implementation */
120397       *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
120398       break;
120399     }
120400 #endif
120401 
120402 
120403     case SQLITE_CONFIG_MALLOC: {
120404       /* Specify an alternative malloc implementation */
120405       sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
120406       break;
120407     }
120408     case SQLITE_CONFIG_GETMALLOC: {
120409       /* Retrieve the current malloc() implementation */
120410       if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
120411       *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
120412       break;
120413     }
120414     case SQLITE_CONFIG_MEMSTATUS: {
120415       /* Enable or disable the malloc status collection */
120416       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
120417       break;
120418     }
120419     case SQLITE_CONFIG_SCRATCH: {
120420       /* Designate a buffer for scratch memory space */
120421       sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
120422       sqlite3GlobalConfig.szScratch = va_arg(ap, int);
120423       sqlite3GlobalConfig.nScratch = va_arg(ap, int);
120424       break;
120425     }
120426     case SQLITE_CONFIG_PAGECACHE: {
120427       /* Designate a buffer for page cache memory space */
120428       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
120429       sqlite3GlobalConfig.szPage = va_arg(ap, int);
120430       sqlite3GlobalConfig.nPage = va_arg(ap, int);
120431       break;
120432     }
120433 
120434     case SQLITE_CONFIG_PCACHE: {
120435       /* no-op */
120436       break;
120437     }
120438     case SQLITE_CONFIG_GETPCACHE: {
120439       /* now an error */
120440       rc = SQLITE_ERROR;
120441       break;
120442     }
120443 
120444     case SQLITE_CONFIG_PCACHE2: {
120445       /* Specify an alternative page cache implementation */
120446       sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
120447       break;
120448     }
120449     case SQLITE_CONFIG_GETPCACHE2: {
120450       if( sqlite3GlobalConfig.pcache2.xInit==0 ){
120451         sqlite3PCacheSetDefault();
120452       }
120453       *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
120454       break;
120455     }
120456 
120457 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
120458     case SQLITE_CONFIG_HEAP: {
120459       /* Designate a buffer for heap memory space */
120460       sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
120461       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
120462       sqlite3GlobalConfig.mnReq = va_arg(ap, int);
120463 
120464       if( sqlite3GlobalConfig.mnReq<1 ){
120465         sqlite3GlobalConfig.mnReq = 1;
120466       }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
120467         /* cap min request size at 2^12 */
120468         sqlite3GlobalConfig.mnReq = (1<<12);
120469       }
120470 
120471       if( sqlite3GlobalConfig.pHeap==0 ){
120472         /* If the heap pointer is NULL, then restore the malloc implementation
120473         ** back to NULL pointers too.  This will cause the malloc to go
120474         ** back to its default implementation when sqlite3_initialize() is
120475         ** run.
120476         */
120477         memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
120478       }else{
120479         /* The heap pointer is not NULL, then install one of the
120480         ** mem5.c/mem3.c methods.  The enclosing #if guarantees at
120481         ** least one of these methods is currently enabled.
120482         */
120483 #ifdef SQLITE_ENABLE_MEMSYS3
120484         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
120485 #endif
120486 #ifdef SQLITE_ENABLE_MEMSYS5
120487         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
120488 #endif
120489       }
120490       break;
120491     }
120492 #endif
120493 
120494     case SQLITE_CONFIG_LOOKASIDE: {
120495       sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
120496       sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
120497       break;
120498     }
120499 
120500     /* Record a pointer to the logger function and its first argument.
120501     ** The default is NULL.  Logging is disabled if the function pointer is
120502     ** NULL.
120503     */
120504     case SQLITE_CONFIG_LOG: {
120505       /* MSVC is picky about pulling func ptrs from va lists.
120506       ** http://support.microsoft.com/kb/47961
120507       ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
120508       */
120509       typedef void(*LOGFUNC_t)(void*,int,const char*);
120510       sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
120511       sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
120512       break;
120513     }
120514 
120515     case SQLITE_CONFIG_URI: {
120516       sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
120517       break;
120518     }
120519 
120520     case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
120521       sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
120522       break;
120523     }
120524 
120525 #ifdef SQLITE_ENABLE_SQLLOG
120526     case SQLITE_CONFIG_SQLLOG: {
120527       typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
120528       sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
120529       sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
120530       break;
120531     }
120532 #endif
120533 
120534     case SQLITE_CONFIG_MMAP_SIZE: {
120535       sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
120536       sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
120537       if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
120538         mxMmap = SQLITE_MAX_MMAP_SIZE;
120539       }
120540       sqlite3GlobalConfig.mxMmap = mxMmap;
120541       if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
120542       if( szMmap>mxMmap) szMmap = mxMmap;
120543       sqlite3GlobalConfig.szMmap = szMmap;
120544       break;
120545     }
120546 
120547 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC)
120548     case SQLITE_CONFIG_WIN32_HEAPSIZE: {
120549       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
120550       break;
120551     }
120552 #endif
120553 
120554     default: {
120555       rc = SQLITE_ERROR;
120556       break;
120557     }
120558   }
120559   va_end(ap);
120560   return rc;
120561 }
120562 
120563 /*
120564 ** Set up the lookaside buffers for a database connection.
120565 ** Return SQLITE_OK on success.
120566 ** If lookaside is already active, return SQLITE_BUSY.
120567 **
120568 ** The sz parameter is the number of bytes in each lookaside slot.
120569 ** The cnt parameter is the number of slots.  If pStart is NULL the
120570 ** space for the lookaside memory is obtained from sqlite3_malloc().
120571 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
120572 ** the lookaside memory.
120573 */
120574 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
120575   void *pStart;
120576   if( db->lookaside.nOut ){
120577     return SQLITE_BUSY;
120578   }
120579   /* Free any existing lookaside buffer for this handle before
120580   ** allocating a new one so we don't have to have space for
120581   ** both at the same time.
120582   */
120583   if( db->lookaside.bMalloced ){
120584     sqlite3_free(db->lookaside.pStart);
120585   }
120586   /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
120587   ** than a pointer to be useful.
120588   */
120589   sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
120590   if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
120591   if( cnt<0 ) cnt = 0;
120592   if( sz==0 || cnt==0 ){
120593     sz = 0;
120594     pStart = 0;
120595   }else if( pBuf==0 ){
120596     sqlite3BeginBenignMalloc();
120597     pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
120598     sqlite3EndBenignMalloc();
120599     if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
120600   }else{
120601     pStart = pBuf;
120602   }
120603   db->lookaside.pStart = pStart;
120604   db->lookaside.pFree = 0;
120605   db->lookaside.sz = (u16)sz;
120606   if( pStart ){
120607     int i;
120608     LookasideSlot *p;
120609     assert( sz > (int)sizeof(LookasideSlot*) );
120610     p = (LookasideSlot*)pStart;
120611     for(i=cnt-1; i>=0; i--){
120612       p->pNext = db->lookaside.pFree;
120613       db->lookaside.pFree = p;
120614       p = (LookasideSlot*)&((u8*)p)[sz];
120615     }
120616     db->lookaside.pEnd = p;
120617     db->lookaside.bEnabled = 1;
120618     db->lookaside.bMalloced = pBuf==0 ?1:0;
120619   }else{
120620     db->lookaside.pStart = db;
120621     db->lookaside.pEnd = db;
120622     db->lookaside.bEnabled = 0;
120623     db->lookaside.bMalloced = 0;
120624   }
120625   return SQLITE_OK;
120626 }
120627 
120628 /*
120629 ** Return the mutex associated with a database connection.
120630 */
120631 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
120632   return db->mutex;
120633 }
120634 
120635 /*
120636 ** Free up as much memory as we can from the given database
120637 ** connection.
120638 */
120639 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
120640   int i;
120641   sqlite3_mutex_enter(db->mutex);
120642   sqlite3BtreeEnterAll(db);
120643   for(i=0; i<db->nDb; i++){
120644     Btree *pBt = db->aDb[i].pBt;
120645     if( pBt ){
120646       Pager *pPager = sqlite3BtreePager(pBt);
120647       sqlite3PagerShrink(pPager);
120648     }
120649   }
120650   sqlite3BtreeLeaveAll(db);
120651   sqlite3_mutex_leave(db->mutex);
120652   return SQLITE_OK;
120653 }
120654 
120655 /*
120656 ** Configuration settings for an individual database connection
120657 */
120658 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
120659   va_list ap;
120660   int rc;
120661   va_start(ap, op);
120662   switch( op ){
120663     case SQLITE_DBCONFIG_LOOKASIDE: {
120664       void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
120665       int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
120666       int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
120667       rc = setupLookaside(db, pBuf, sz, cnt);
120668       break;
120669     }
120670     default: {
120671       static const struct {
120672         int op;      /* The opcode */
120673         u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
120674       } aFlagOp[] = {
120675         { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
120676         { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
120677       };
120678       unsigned int i;
120679       rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
120680       for(i=0; i<ArraySize(aFlagOp); i++){
120681         if( aFlagOp[i].op==op ){
120682           int onoff = va_arg(ap, int);
120683           int *pRes = va_arg(ap, int*);
120684           int oldFlags = db->flags;
120685           if( onoff>0 ){
120686             db->flags |= aFlagOp[i].mask;
120687           }else if( onoff==0 ){
120688             db->flags &= ~aFlagOp[i].mask;
120689           }
120690           if( oldFlags!=db->flags ){
120691             sqlite3ExpirePreparedStatements(db);
120692           }
120693           if( pRes ){
120694             *pRes = (db->flags & aFlagOp[i].mask)!=0;
120695           }
120696           rc = SQLITE_OK;
120697           break;
120698         }
120699       }
120700       break;
120701     }
120702   }
120703   va_end(ap);
120704   return rc;
120705 }
120706 
120707 
120708 /*
120709 ** Return true if the buffer z[0..n-1] contains all spaces.
120710 */
120711 static int allSpaces(const char *z, int n){
120712   while( n>0 && z[n-1]==' ' ){ n--; }
120713   return n==0;
120714 }
120715 
120716 /*
120717 ** This is the default collating function named "BINARY" which is always
120718 ** available.
120719 **
120720 ** If the padFlag argument is not NULL then space padding at the end
120721 ** of strings is ignored.  This implements the RTRIM collation.
120722 */
120723 static int binCollFunc(
120724   void *padFlag,
120725   int nKey1, const void *pKey1,
120726   int nKey2, const void *pKey2
120727 ){
120728   int rc, n;
120729   n = nKey1<nKey2 ? nKey1 : nKey2;
120730   rc = memcmp(pKey1, pKey2, n);
120731   if( rc==0 ){
120732     if( padFlag
120733      && allSpaces(((char*)pKey1)+n, nKey1-n)
120734      && allSpaces(((char*)pKey2)+n, nKey2-n)
120735     ){
120736       /* Leave rc unchanged at 0 */
120737     }else{
120738       rc = nKey1 - nKey2;
120739     }
120740   }
120741   return rc;
120742 }
120743 
120744 /*
120745 ** Another built-in collating sequence: NOCASE.
120746 **
120747 ** This collating sequence is intended to be used for "case independent
120748 ** comparison". SQLite's knowledge of upper and lower case equivalents
120749 ** extends only to the 26 characters used in the English language.
120750 **
120751 ** At the moment there is only a UTF-8 implementation.
120752 */
120753 static int nocaseCollatingFunc(
120754   void *NotUsed,
120755   int nKey1, const void *pKey1,
120756   int nKey2, const void *pKey2
120757 ){
120758   int r = sqlite3StrNICmp(
120759       (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
120760   UNUSED_PARAMETER(NotUsed);
120761   if( 0==r ){
120762     r = nKey1-nKey2;
120763   }
120764   return r;
120765 }
120766 
120767 /*
120768 ** Return the ROWID of the most recent insert
120769 */
120770 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
120771   return db->lastRowid;
120772 }
120773 
120774 /*
120775 ** Return the number of changes in the most recent call to sqlite3_exec().
120776 */
120777 SQLITE_API int sqlite3_changes(sqlite3 *db){
120778   return db->nChange;
120779 }
120780 
120781 /*
120782 ** Return the number of changes since the database handle was opened.
120783 */
120784 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
120785   return db->nTotalChange;
120786 }
120787 
120788 /*
120789 ** Close all open savepoints. This function only manipulates fields of the
120790 ** database handle object, it does not close any savepoints that may be open
120791 ** at the b-tree/pager level.
120792 */
120793 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
120794   while( db->pSavepoint ){
120795     Savepoint *pTmp = db->pSavepoint;
120796     db->pSavepoint = pTmp->pNext;
120797     sqlite3DbFree(db, pTmp);
120798   }
120799   db->nSavepoint = 0;
120800   db->nStatement = 0;
120801   db->isTransactionSavepoint = 0;
120802 }
120803 
120804 /*
120805 ** Invoke the destructor function associated with FuncDef p, if any. Except,
120806 ** if this is not the last copy of the function, do not invoke it. Multiple
120807 ** copies of a single function are created when create_function() is called
120808 ** with SQLITE_ANY as the encoding.
120809 */
120810 static void functionDestroy(sqlite3 *db, FuncDef *p){
120811   FuncDestructor *pDestructor = p->pDestructor;
120812   if( pDestructor ){
120813     pDestructor->nRef--;
120814     if( pDestructor->nRef==0 ){
120815       pDestructor->xDestroy(pDestructor->pUserData);
120816       sqlite3DbFree(db, pDestructor);
120817     }
120818   }
120819 }
120820 
120821 /*
120822 ** Disconnect all sqlite3_vtab objects that belong to database connection
120823 ** db. This is called when db is being closed.
120824 */
120825 static void disconnectAllVtab(sqlite3 *db){
120826 #ifndef SQLITE_OMIT_VIRTUALTABLE
120827   int i;
120828   sqlite3BtreeEnterAll(db);
120829   for(i=0; i<db->nDb; i++){
120830     Schema *pSchema = db->aDb[i].pSchema;
120831     if( db->aDb[i].pSchema ){
120832       HashElem *p;
120833       for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
120834         Table *pTab = (Table *)sqliteHashData(p);
120835         if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
120836       }
120837     }
120838   }
120839   sqlite3BtreeLeaveAll(db);
120840 #else
120841   UNUSED_PARAMETER(db);
120842 #endif
120843 }
120844 
120845 /*
120846 ** Return TRUE if database connection db has unfinalized prepared
120847 ** statements or unfinished sqlite3_backup objects.
120848 */
120849 static int connectionIsBusy(sqlite3 *db){
120850   int j;
120851   assert( sqlite3_mutex_held(db->mutex) );
120852   if( db->pVdbe ) return 1;
120853   for(j=0; j<db->nDb; j++){
120854     Btree *pBt = db->aDb[j].pBt;
120855     if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
120856   }
120857   return 0;
120858 }
120859 
120860 /*
120861 ** Close an existing SQLite database
120862 */
120863 static int sqlite3Close(sqlite3 *db, int forceZombie){
120864   if( !db ){
120865     return SQLITE_OK;
120866   }
120867   if( !sqlite3SafetyCheckSickOrOk(db) ){
120868     return SQLITE_MISUSE_BKPT;
120869   }
120870   sqlite3_mutex_enter(db->mutex);
120871 
120872   /* Force xDisconnect calls on all virtual tables */
120873   disconnectAllVtab(db);
120874 
120875   /* If a transaction is open, the disconnectAllVtab() call above
120876   ** will not have called the xDisconnect() method on any virtual
120877   ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
120878   ** call will do so. We need to do this before the check for active
120879   ** SQL statements below, as the v-table implementation may be storing
120880   ** some prepared statements internally.
120881   */
120882   sqlite3VtabRollback(db);
120883 
120884   /* Legacy behavior (sqlite3_close() behavior) is to return
120885   ** SQLITE_BUSY if the connection can not be closed immediately.
120886   */
120887   if( !forceZombie && connectionIsBusy(db) ){
120888     sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
120889        "statements or unfinished backups");
120890     sqlite3_mutex_leave(db->mutex);
120891     return SQLITE_BUSY;
120892   }
120893 
120894 #ifdef SQLITE_ENABLE_SQLLOG
120895   if( sqlite3GlobalConfig.xSqllog ){
120896     /* Closing the handle. Fourth parameter is passed the value 2. */
120897     sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
120898   }
120899 #endif
120900 
120901   /* Convert the connection into a zombie and then close it.
120902   */
120903   db->magic = SQLITE_MAGIC_ZOMBIE;
120904   sqlite3LeaveMutexAndCloseZombie(db);
120905   return SQLITE_OK;
120906 }
120907 
120908 /*
120909 ** Two variations on the public interface for closing a database
120910 ** connection. The sqlite3_close() version returns SQLITE_BUSY and
120911 ** leaves the connection option if there are unfinalized prepared
120912 ** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
120913 ** version forces the connection to become a zombie if there are
120914 ** unclosed resources, and arranges for deallocation when the last
120915 ** prepare statement or sqlite3_backup closes.
120916 */
120917 SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
120918 SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
120919 
120920 
120921 /*
120922 ** Close the mutex on database connection db.
120923 **
120924 ** Furthermore, if database connection db is a zombie (meaning that there
120925 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
120926 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has
120927 ** finished, then free all resources.
120928 */
120929 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
120930   HashElem *i;                    /* Hash table iterator */
120931   int j;
120932 
120933   /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
120934   ** or if the connection has not yet been closed by sqlite3_close_v2(),
120935   ** then just leave the mutex and return.
120936   */
120937   if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
120938     sqlite3_mutex_leave(db->mutex);
120939     return;
120940   }
120941 
120942   /* If we reach this point, it means that the database connection has
120943   ** closed all sqlite3_stmt and sqlite3_backup objects and has been
120944   ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
120945   ** go ahead and free all resources.
120946   */
120947 
120948   /* If a transaction is open, roll it back. This also ensures that if
120949   ** any database schemas have been modified by an uncommitted transaction
120950   ** they are reset. And that the required b-tree mutex is held to make
120951   ** the pager rollback and schema reset an atomic operation. */
120952   sqlite3RollbackAll(db, SQLITE_OK);
120953 
120954   /* Free any outstanding Savepoint structures. */
120955   sqlite3CloseSavepoints(db);
120956 
120957   /* Close all database connections */
120958   for(j=0; j<db->nDb; j++){
120959     struct Db *pDb = &db->aDb[j];
120960     if( pDb->pBt ){
120961       sqlite3BtreeClose(pDb->pBt);
120962       pDb->pBt = 0;
120963       if( j!=1 ){
120964         pDb->pSchema = 0;
120965       }
120966     }
120967   }
120968   /* Clear the TEMP schema separately and last */
120969   if( db->aDb[1].pSchema ){
120970     sqlite3SchemaClear(db->aDb[1].pSchema);
120971   }
120972   sqlite3VtabUnlockList(db);
120973 
120974   /* Free up the array of auxiliary databases */
120975   sqlite3CollapseDatabaseArray(db);
120976   assert( db->nDb<=2 );
120977   assert( db->aDb==db->aDbStatic );
120978 
120979   /* Tell the code in notify.c that the connection no longer holds any
120980   ** locks and does not require any further unlock-notify callbacks.
120981   */
120982   sqlite3ConnectionClosed(db);
120983 
120984   for(j=0; j<ArraySize(db->aFunc.a); j++){
120985     FuncDef *pNext, *pHash, *p;
120986     for(p=db->aFunc.a[j]; p; p=pHash){
120987       pHash = p->pHash;
120988       while( p ){
120989         functionDestroy(db, p);
120990         pNext = p->pNext;
120991         sqlite3DbFree(db, p);
120992         p = pNext;
120993       }
120994     }
120995   }
120996   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
120997     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
120998     /* Invoke any destructors registered for collation sequence user data. */
120999     for(j=0; j<3; j++){
121000       if( pColl[j].xDel ){
121001         pColl[j].xDel(pColl[j].pUser);
121002       }
121003     }
121004     sqlite3DbFree(db, pColl);
121005   }
121006   sqlite3HashClear(&db->aCollSeq);
121007 #ifndef SQLITE_OMIT_VIRTUALTABLE
121008   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
121009     Module *pMod = (Module *)sqliteHashData(i);
121010     if( pMod->xDestroy ){
121011       pMod->xDestroy(pMod->pAux);
121012     }
121013     sqlite3DbFree(db, pMod);
121014   }
121015   sqlite3HashClear(&db->aModule);
121016 #endif
121017 
121018   sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
121019   sqlite3ValueFree(db->pErr);
121020   sqlite3CloseExtensions(db);
121021 
121022   db->magic = SQLITE_MAGIC_ERROR;
121023 
121024   /* The temp-database schema is allocated differently from the other schema
121025   ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
121026   ** So it needs to be freed here. Todo: Why not roll the temp schema into
121027   ** the same sqliteMalloc() as the one that allocates the database
121028   ** structure?
121029   */
121030   sqlite3DbFree(db, db->aDb[1].pSchema);
121031   sqlite3_mutex_leave(db->mutex);
121032   db->magic = SQLITE_MAGIC_CLOSED;
121033   sqlite3_mutex_free(db->mutex);
121034   assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
121035   if( db->lookaside.bMalloced ){
121036     sqlite3_free(db->lookaside.pStart);
121037   }
121038   sqlite3_free(db);
121039 }
121040 
121041 /*
121042 ** Rollback all database files.  If tripCode is not SQLITE_OK, then
121043 ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
121044 ** breaker") and made to return tripCode if there are any further
121045 ** attempts to use that cursor.
121046 */
121047 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
121048   int i;
121049   int inTrans = 0;
121050   assert( sqlite3_mutex_held(db->mutex) );
121051   sqlite3BeginBenignMalloc();
121052 
121053   /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
121054   ** This is important in case the transaction being rolled back has
121055   ** modified the database schema. If the b-tree mutexes are not taken
121056   ** here, then another shared-cache connection might sneak in between
121057   ** the database rollback and schema reset, which can cause false
121058   ** corruption reports in some cases.  */
121059   sqlite3BtreeEnterAll(db);
121060 
121061   for(i=0; i<db->nDb; i++){
121062     Btree *p = db->aDb[i].pBt;
121063     if( p ){
121064       if( sqlite3BtreeIsInTrans(p) ){
121065         inTrans = 1;
121066       }
121067       sqlite3BtreeRollback(p, tripCode);
121068     }
121069   }
121070   sqlite3VtabRollback(db);
121071   sqlite3EndBenignMalloc();
121072 
121073   if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
121074     sqlite3ExpirePreparedStatements(db);
121075     sqlite3ResetAllSchemasOfConnection(db);
121076   }
121077   sqlite3BtreeLeaveAll(db);
121078 
121079   /* Any deferred constraint violations have now been resolved. */
121080   db->nDeferredCons = 0;
121081   db->nDeferredImmCons = 0;
121082   db->flags &= ~SQLITE_DeferFKs;
121083 
121084   /* If one has been configured, invoke the rollback-hook callback */
121085   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
121086     db->xRollbackCallback(db->pRollbackArg);
121087   }
121088 }
121089 
121090 /*
121091 ** Return a static string containing the name corresponding to the error code
121092 ** specified in the argument.
121093 */
121094 #if defined(SQLITE_TEST)
121095 SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
121096   const char *zName = 0;
121097   int i, origRc = rc;
121098   for(i=0; i<2 && zName==0; i++, rc &= 0xff){
121099     switch( rc ){
121100       case SQLITE_OK:                 zName = "SQLITE_OK";                break;
121101       case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
121102       case SQLITE_INTERNAL:           zName = "SQLITE_INTERNAL";          break;
121103       case SQLITE_PERM:               zName = "SQLITE_PERM";              break;
121104       case SQLITE_ABORT:              zName = "SQLITE_ABORT";             break;
121105       case SQLITE_ABORT_ROLLBACK:     zName = "SQLITE_ABORT_ROLLBACK";    break;
121106       case SQLITE_BUSY:               zName = "SQLITE_BUSY";              break;
121107       case SQLITE_BUSY_RECOVERY:      zName = "SQLITE_BUSY_RECOVERY";     break;
121108       case SQLITE_BUSY_SNAPSHOT:      zName = "SQLITE_BUSY_SNAPSHOT";     break;
121109       case SQLITE_LOCKED:             zName = "SQLITE_LOCKED";            break;
121110       case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
121111       case SQLITE_NOMEM:              zName = "SQLITE_NOMEM";             break;
121112       case SQLITE_READONLY:           zName = "SQLITE_READONLY";          break;
121113       case SQLITE_READONLY_RECOVERY:  zName = "SQLITE_READONLY_RECOVERY"; break;
121114       case SQLITE_READONLY_CANTLOCK:  zName = "SQLITE_READONLY_CANTLOCK"; break;
121115       case SQLITE_READONLY_ROLLBACK:  zName = "SQLITE_READONLY_ROLLBACK"; break;
121116       case SQLITE_READONLY_DBMOVED:   zName = "SQLITE_READONLY_DBMOVED";  break;
121117       case SQLITE_INTERRUPT:          zName = "SQLITE_INTERRUPT";         break;
121118       case SQLITE_IOERR:              zName = "SQLITE_IOERR";             break;
121119       case SQLITE_IOERR_READ:         zName = "SQLITE_IOERR_READ";        break;
121120       case SQLITE_IOERR_SHORT_READ:   zName = "SQLITE_IOERR_SHORT_READ";  break;
121121       case SQLITE_IOERR_WRITE:        zName = "SQLITE_IOERR_WRITE";       break;
121122       case SQLITE_IOERR_FSYNC:        zName = "SQLITE_IOERR_FSYNC";       break;
121123       case SQLITE_IOERR_DIR_FSYNC:    zName = "SQLITE_IOERR_DIR_FSYNC";   break;
121124       case SQLITE_IOERR_TRUNCATE:     zName = "SQLITE_IOERR_TRUNCATE";    break;
121125       case SQLITE_IOERR_FSTAT:        zName = "SQLITE_IOERR_FSTAT";       break;
121126       case SQLITE_IOERR_UNLOCK:       zName = "SQLITE_IOERR_UNLOCK";      break;
121127       case SQLITE_IOERR_RDLOCK:       zName = "SQLITE_IOERR_RDLOCK";      break;
121128       case SQLITE_IOERR_DELETE:       zName = "SQLITE_IOERR_DELETE";      break;
121129       case SQLITE_IOERR_BLOCKED:      zName = "SQLITE_IOERR_BLOCKED";     break;
121130       case SQLITE_IOERR_NOMEM:        zName = "SQLITE_IOERR_NOMEM";       break;
121131       case SQLITE_IOERR_ACCESS:       zName = "SQLITE_IOERR_ACCESS";      break;
121132       case SQLITE_IOERR_CHECKRESERVEDLOCK:
121133                                 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
121134       case SQLITE_IOERR_LOCK:         zName = "SQLITE_IOERR_LOCK";        break;
121135       case SQLITE_IOERR_CLOSE:        zName = "SQLITE_IOERR_CLOSE";       break;
121136       case SQLITE_IOERR_DIR_CLOSE:    zName = "SQLITE_IOERR_DIR_CLOSE";   break;
121137       case SQLITE_IOERR_SHMOPEN:      zName = "SQLITE_IOERR_SHMOPEN";     break;
121138       case SQLITE_IOERR_SHMSIZE:      zName = "SQLITE_IOERR_SHMSIZE";     break;
121139       case SQLITE_IOERR_SHMLOCK:      zName = "SQLITE_IOERR_SHMLOCK";     break;
121140       case SQLITE_IOERR_SHMMAP:       zName = "SQLITE_IOERR_SHMMAP";      break;
121141       case SQLITE_IOERR_SEEK:         zName = "SQLITE_IOERR_SEEK";        break;
121142       case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
121143       case SQLITE_IOERR_MMAP:         zName = "SQLITE_IOERR_MMAP";        break;
121144       case SQLITE_IOERR_GETTEMPPATH:  zName = "SQLITE_IOERR_GETTEMPPATH"; break;
121145       case SQLITE_IOERR_CONVPATH:     zName = "SQLITE_IOERR_CONVPATH";    break;
121146       case SQLITE_CORRUPT:            zName = "SQLITE_CORRUPT";           break;
121147       case SQLITE_CORRUPT_VTAB:       zName = "SQLITE_CORRUPT_VTAB";      break;
121148       case SQLITE_NOTFOUND:           zName = "SQLITE_NOTFOUND";          break;
121149       case SQLITE_FULL:               zName = "SQLITE_FULL";              break;
121150       case SQLITE_CANTOPEN:           zName = "SQLITE_CANTOPEN";          break;
121151       case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
121152       case SQLITE_CANTOPEN_ISDIR:     zName = "SQLITE_CANTOPEN_ISDIR";    break;
121153       case SQLITE_CANTOPEN_FULLPATH:  zName = "SQLITE_CANTOPEN_FULLPATH"; break;
121154       case SQLITE_CANTOPEN_CONVPATH:  zName = "SQLITE_CANTOPEN_CONVPATH"; break;
121155       case SQLITE_PROTOCOL:           zName = "SQLITE_PROTOCOL";          break;
121156       case SQLITE_EMPTY:              zName = "SQLITE_EMPTY";             break;
121157       case SQLITE_SCHEMA:             zName = "SQLITE_SCHEMA";            break;
121158       case SQLITE_TOOBIG:             zName = "SQLITE_TOOBIG";            break;
121159       case SQLITE_CONSTRAINT:         zName = "SQLITE_CONSTRAINT";        break;
121160       case SQLITE_CONSTRAINT_UNIQUE:  zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
121161       case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
121162       case SQLITE_CONSTRAINT_FOREIGNKEY:
121163                                 zName = "SQLITE_CONSTRAINT_FOREIGNKEY";   break;
121164       case SQLITE_CONSTRAINT_CHECK:   zName = "SQLITE_CONSTRAINT_CHECK";  break;
121165       case SQLITE_CONSTRAINT_PRIMARYKEY:
121166                                 zName = "SQLITE_CONSTRAINT_PRIMARYKEY";   break;
121167       case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
121168       case SQLITE_CONSTRAINT_COMMITHOOK:
121169                                 zName = "SQLITE_CONSTRAINT_COMMITHOOK";   break;
121170       case SQLITE_CONSTRAINT_VTAB:    zName = "SQLITE_CONSTRAINT_VTAB";   break;
121171       case SQLITE_CONSTRAINT_FUNCTION:
121172                                 zName = "SQLITE_CONSTRAINT_FUNCTION";     break;
121173       case SQLITE_CONSTRAINT_ROWID:   zName = "SQLITE_CONSTRAINT_ROWID";  break;
121174       case SQLITE_MISMATCH:           zName = "SQLITE_MISMATCH";          break;
121175       case SQLITE_MISUSE:             zName = "SQLITE_MISUSE";            break;
121176       case SQLITE_NOLFS:              zName = "SQLITE_NOLFS";             break;
121177       case SQLITE_AUTH:               zName = "SQLITE_AUTH";              break;
121178       case SQLITE_FORMAT:             zName = "SQLITE_FORMAT";            break;
121179       case SQLITE_RANGE:              zName = "SQLITE_RANGE";             break;
121180       case SQLITE_NOTADB:             zName = "SQLITE_NOTADB";            break;
121181       case SQLITE_ROW:                zName = "SQLITE_ROW";               break;
121182       case SQLITE_NOTICE:             zName = "SQLITE_NOTICE";            break;
121183       case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
121184       case SQLITE_NOTICE_RECOVER_ROLLBACK:
121185                                 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
121186       case SQLITE_WARNING:            zName = "SQLITE_WARNING";           break;
121187       case SQLITE_WARNING_AUTOINDEX:  zName = "SQLITE_WARNING_AUTOINDEX"; break;
121188       case SQLITE_DONE:               zName = "SQLITE_DONE";              break;
121189     }
121190   }
121191   if( zName==0 ){
121192     static char zBuf[50];
121193     sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
121194     zName = zBuf;
121195   }
121196   return zName;
121197 }
121198 #endif
121199 
121200 /*
121201 ** Return a static string that describes the kind of error specified in the
121202 ** argument.
121203 */
121204 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
121205   static const char* const aMsg[] = {
121206     /* SQLITE_OK          */ "not an error",
121207     /* SQLITE_ERROR       */ "SQL logic error or missing database",
121208     /* SQLITE_INTERNAL    */ 0,
121209     /* SQLITE_PERM        */ "access permission denied",
121210     /* SQLITE_ABORT       */ "callback requested query abort",
121211     /* SQLITE_BUSY        */ "database is locked",
121212     /* SQLITE_LOCKED      */ "database table is locked",
121213     /* SQLITE_NOMEM       */ "out of memory",
121214     /* SQLITE_READONLY    */ "attempt to write a readonly database",
121215     /* SQLITE_INTERRUPT   */ "interrupted",
121216     /* SQLITE_IOERR       */ "disk I/O error",
121217     /* SQLITE_CORRUPT     */ "database disk image is malformed",
121218     /* SQLITE_NOTFOUND    */ "unknown operation",
121219     /* SQLITE_FULL        */ "database or disk is full",
121220     /* SQLITE_CANTOPEN    */ "unable to open database file",
121221     /* SQLITE_PROTOCOL    */ "locking protocol",
121222     /* SQLITE_EMPTY       */ "table contains no data",
121223     /* SQLITE_SCHEMA      */ "database schema has changed",
121224     /* SQLITE_TOOBIG      */ "string or blob too big",
121225     /* SQLITE_CONSTRAINT  */ "constraint failed",
121226     /* SQLITE_MISMATCH    */ "datatype mismatch",
121227     /* SQLITE_MISUSE      */ "library routine called out of sequence",
121228     /* SQLITE_NOLFS       */ "large file support is disabled",
121229     /* SQLITE_AUTH        */ "authorization denied",
121230     /* SQLITE_FORMAT      */ "auxiliary database format error",
121231     /* SQLITE_RANGE       */ "bind or column index out of range",
121232     /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
121233   };
121234   const char *zErr = "unknown error";
121235   switch( rc ){
121236     case SQLITE_ABORT_ROLLBACK: {
121237       zErr = "abort due to ROLLBACK";
121238       break;
121239     }
121240     default: {
121241       rc &= 0xff;
121242       if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
121243         zErr = aMsg[rc];
121244       }
121245       break;
121246     }
121247   }
121248   return zErr;
121249 }
121250 
121251 /*
121252 ** This routine implements a busy callback that sleeps and tries
121253 ** again until a timeout value is reached.  The timeout value is
121254 ** an integer number of milliseconds passed in as the first
121255 ** argument.
121256 */
121257 static int sqliteDefaultBusyCallback(
121258  void *ptr,               /* Database connection */
121259  int count                /* Number of times table has been busy */
121260 ){
121261 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
121262   static const u8 delays[] =
121263      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
121264   static const u8 totals[] =
121265      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
121266 # define NDELAY ArraySize(delays)
121267   sqlite3 *db = (sqlite3 *)ptr;
121268   int timeout = db->busyTimeout;
121269   int delay, prior;
121270 
121271   assert( count>=0 );
121272   if( count < NDELAY ){
121273     delay = delays[count];
121274     prior = totals[count];
121275   }else{
121276     delay = delays[NDELAY-1];
121277     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
121278   }
121279   if( prior + delay > timeout ){
121280     delay = timeout - prior;
121281     if( delay<=0 ) return 0;
121282   }
121283   sqlite3OsSleep(db->pVfs, delay*1000);
121284   return 1;
121285 #else
121286   sqlite3 *db = (sqlite3 *)ptr;
121287   int timeout = ((sqlite3 *)ptr)->busyTimeout;
121288   if( (count+1)*1000 > timeout ){
121289     return 0;
121290   }
121291   sqlite3OsSleep(db->pVfs, 1000000);
121292   return 1;
121293 #endif
121294 }
121295 
121296 /*
121297 ** Invoke the given busy handler.
121298 **
121299 ** This routine is called when an operation failed with a lock.
121300 ** If this routine returns non-zero, the lock is retried.  If it
121301 ** returns 0, the operation aborts with an SQLITE_BUSY error.
121302 */
121303 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
121304   int rc;
121305   if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
121306   rc = p->xFunc(p->pArg, p->nBusy);
121307   if( rc==0 ){
121308     p->nBusy = -1;
121309   }else{
121310     p->nBusy++;
121311   }
121312   return rc;
121313 }
121314 
121315 /*
121316 ** This routine sets the busy callback for an Sqlite database to the
121317 ** given callback function with the given argument.
121318 */
121319 SQLITE_API int sqlite3_busy_handler(
121320   sqlite3 *db,
121321   int (*xBusy)(void*,int),
121322   void *pArg
121323 ){
121324   sqlite3_mutex_enter(db->mutex);
121325   db->busyHandler.xFunc = xBusy;
121326   db->busyHandler.pArg = pArg;
121327   db->busyHandler.nBusy = 0;
121328   db->busyTimeout = 0;
121329   sqlite3_mutex_leave(db->mutex);
121330   return SQLITE_OK;
121331 }
121332 
121333 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
121334 /*
121335 ** This routine sets the progress callback for an Sqlite database to the
121336 ** given callback function with the given argument. The progress callback will
121337 ** be invoked every nOps opcodes.
121338 */
121339 SQLITE_API void sqlite3_progress_handler(
121340   sqlite3 *db,
121341   int nOps,
121342   int (*xProgress)(void*),
121343   void *pArg
121344 ){
121345   sqlite3_mutex_enter(db->mutex);
121346   if( nOps>0 ){
121347     db->xProgress = xProgress;
121348     db->nProgressOps = (unsigned)nOps;
121349     db->pProgressArg = pArg;
121350   }else{
121351     db->xProgress = 0;
121352     db->nProgressOps = 0;
121353     db->pProgressArg = 0;
121354   }
121355   sqlite3_mutex_leave(db->mutex);
121356 }
121357 #endif
121358 
121359 
121360 /*
121361 ** This routine installs a default busy handler that waits for the
121362 ** specified number of milliseconds before returning 0.
121363 */
121364 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
121365   if( ms>0 ){
121366     sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
121367     db->busyTimeout = ms;
121368   }else{
121369     sqlite3_busy_handler(db, 0, 0);
121370   }
121371   return SQLITE_OK;
121372 }
121373 
121374 /*
121375 ** Cause any pending operation to stop at its earliest opportunity.
121376 */
121377 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
121378   db->u1.isInterrupted = 1;
121379 }
121380 
121381 
121382 /*
121383 ** This function is exactly the same as sqlite3_create_function(), except
121384 ** that it is designed to be called by internal code. The difference is
121385 ** that if a malloc() fails in sqlite3_create_function(), an error code
121386 ** is returned and the mallocFailed flag cleared.
121387 */
121388 SQLITE_PRIVATE int sqlite3CreateFunc(
121389   sqlite3 *db,
121390   const char *zFunctionName,
121391   int nArg,
121392   int enc,
121393   void *pUserData,
121394   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
121395   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
121396   void (*xFinal)(sqlite3_context*),
121397   FuncDestructor *pDestructor
121398 ){
121399   FuncDef *p;
121400   int nName;
121401   int extraFlags;
121402 
121403   assert( sqlite3_mutex_held(db->mutex) );
121404   if( zFunctionName==0 ||
121405       (xFunc && (xFinal || xStep)) ||
121406       (!xFunc && (xFinal && !xStep)) ||
121407       (!xFunc && (!xFinal && xStep)) ||
121408       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
121409       (255<(nName = sqlite3Strlen30( zFunctionName))) ){
121410     return SQLITE_MISUSE_BKPT;
121411   }
121412 
121413   assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
121414   extraFlags = enc &  SQLITE_DETERMINISTIC;
121415   enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
121416 
121417 #ifndef SQLITE_OMIT_UTF16
121418   /* If SQLITE_UTF16 is specified as the encoding type, transform this
121419   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
121420   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
121421   **
121422   ** If SQLITE_ANY is specified, add three versions of the function
121423   ** to the hash table.
121424   */
121425   if( enc==SQLITE_UTF16 ){
121426     enc = SQLITE_UTF16NATIVE;
121427   }else if( enc==SQLITE_ANY ){
121428     int rc;
121429     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
121430          pUserData, xFunc, xStep, xFinal, pDestructor);
121431     if( rc==SQLITE_OK ){
121432       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
121433           pUserData, xFunc, xStep, xFinal, pDestructor);
121434     }
121435     if( rc!=SQLITE_OK ){
121436       return rc;
121437     }
121438     enc = SQLITE_UTF16BE;
121439   }
121440 #else
121441   enc = SQLITE_UTF8;
121442 #endif
121443 
121444   /* Check if an existing function is being overridden or deleted. If so,
121445   ** and there are active VMs, then return SQLITE_BUSY. If a function
121446   ** is being overridden/deleted but there are no active VMs, allow the
121447   ** operation to continue but invalidate all precompiled statements.
121448   */
121449   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
121450   if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
121451     if( db->nVdbeActive ){
121452       sqlite3Error(db, SQLITE_BUSY,
121453         "unable to delete/modify user-function due to active statements");
121454       assert( !db->mallocFailed );
121455       return SQLITE_BUSY;
121456     }else{
121457       sqlite3ExpirePreparedStatements(db);
121458     }
121459   }
121460 
121461   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
121462   assert(p || db->mallocFailed);
121463   if( !p ){
121464     return SQLITE_NOMEM;
121465   }
121466 
121467   /* If an older version of the function with a configured destructor is
121468   ** being replaced invoke the destructor function here. */
121469   functionDestroy(db, p);
121470 
121471   if( pDestructor ){
121472     pDestructor->nRef++;
121473   }
121474   p->pDestructor = pDestructor;
121475   p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
121476   testcase( p->funcFlags & SQLITE_DETERMINISTIC );
121477   p->xFunc = xFunc;
121478   p->xStep = xStep;
121479   p->xFinalize = xFinal;
121480   p->pUserData = pUserData;
121481   p->nArg = (u16)nArg;
121482   return SQLITE_OK;
121483 }
121484 
121485 /*
121486 ** Create new user functions.
121487 */
121488 SQLITE_API int sqlite3_create_function(
121489   sqlite3 *db,
121490   const char *zFunc,
121491   int nArg,
121492   int enc,
121493   void *p,
121494   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
121495   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
121496   void (*xFinal)(sqlite3_context*)
121497 ){
121498   return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
121499                                     xFinal, 0);
121500 }
121501 
121502 SQLITE_API int sqlite3_create_function_v2(
121503   sqlite3 *db,
121504   const char *zFunc,
121505   int nArg,
121506   int enc,
121507   void *p,
121508   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
121509   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
121510   void (*xFinal)(sqlite3_context*),
121511   void (*xDestroy)(void *)
121512 ){
121513   int rc = SQLITE_ERROR;
121514   FuncDestructor *pArg = 0;
121515   sqlite3_mutex_enter(db->mutex);
121516   if( xDestroy ){
121517     pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
121518     if( !pArg ){
121519       xDestroy(p);
121520       goto out;
121521     }
121522     pArg->xDestroy = xDestroy;
121523     pArg->pUserData = p;
121524   }
121525   rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
121526   if( pArg && pArg->nRef==0 ){
121527     assert( rc!=SQLITE_OK );
121528     xDestroy(p);
121529     sqlite3DbFree(db, pArg);
121530   }
121531 
121532  out:
121533   rc = sqlite3ApiExit(db, rc);
121534   sqlite3_mutex_leave(db->mutex);
121535   return rc;
121536 }
121537 
121538 #ifndef SQLITE_OMIT_UTF16
121539 SQLITE_API int sqlite3_create_function16(
121540   sqlite3 *db,
121541   const void *zFunctionName,
121542   int nArg,
121543   int eTextRep,
121544   void *p,
121545   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
121546   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
121547   void (*xFinal)(sqlite3_context*)
121548 ){
121549   int rc;
121550   char *zFunc8;
121551   sqlite3_mutex_enter(db->mutex);
121552   assert( !db->mallocFailed );
121553   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
121554   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
121555   sqlite3DbFree(db, zFunc8);
121556   rc = sqlite3ApiExit(db, rc);
121557   sqlite3_mutex_leave(db->mutex);
121558   return rc;
121559 }
121560 #endif
121561 
121562 
121563 /*
121564 ** Declare that a function has been overloaded by a virtual table.
121565 **
121566 ** If the function already exists as a regular global function, then
121567 ** this routine is a no-op.  If the function does not exist, then create
121568 ** a new one that always throws a run-time error.
121569 **
121570 ** When virtual tables intend to provide an overloaded function, they
121571 ** should call this routine to make sure the global function exists.
121572 ** A global function must exist in order for name resolution to work
121573 ** properly.
121574 */
121575 SQLITE_API int sqlite3_overload_function(
121576   sqlite3 *db,
121577   const char *zName,
121578   int nArg
121579 ){
121580   int nName = sqlite3Strlen30(zName);
121581   int rc = SQLITE_OK;
121582   sqlite3_mutex_enter(db->mutex);
121583   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
121584     rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
121585                            0, sqlite3InvalidFunction, 0, 0, 0);
121586   }
121587   rc = sqlite3ApiExit(db, rc);
121588   sqlite3_mutex_leave(db->mutex);
121589   return rc;
121590 }
121591 
121592 #ifndef SQLITE_OMIT_TRACE
121593 /*
121594 ** Register a trace function.  The pArg from the previously registered trace
121595 ** is returned.
121596 **
121597 ** A NULL trace function means that no tracing is executes.  A non-NULL
121598 ** trace is a pointer to a function that is invoked at the start of each
121599 ** SQL statement.
121600 */
121601 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
121602   void *pOld;
121603   sqlite3_mutex_enter(db->mutex);
121604   pOld = db->pTraceArg;
121605   db->xTrace = xTrace;
121606   db->pTraceArg = pArg;
121607   sqlite3_mutex_leave(db->mutex);
121608   return pOld;
121609 }
121610 /*
121611 ** Register a profile function.  The pArg from the previously registered
121612 ** profile function is returned.
121613 **
121614 ** A NULL profile function means that no profiling is executes.  A non-NULL
121615 ** profile is a pointer to a function that is invoked at the conclusion of
121616 ** each SQL statement that is run.
121617 */
121618 SQLITE_API void *sqlite3_profile(
121619   sqlite3 *db,
121620   void (*xProfile)(void*,const char*,sqlite_uint64),
121621   void *pArg
121622 ){
121623   void *pOld;
121624   sqlite3_mutex_enter(db->mutex);
121625   pOld = db->pProfileArg;
121626   db->xProfile = xProfile;
121627   db->pProfileArg = pArg;
121628   sqlite3_mutex_leave(db->mutex);
121629   return pOld;
121630 }
121631 #endif /* SQLITE_OMIT_TRACE */
121632 
121633 /*
121634 ** Register a function to be invoked when a transaction commits.
121635 ** If the invoked function returns non-zero, then the commit becomes a
121636 ** rollback.
121637 */
121638 SQLITE_API void *sqlite3_commit_hook(
121639   sqlite3 *db,              /* Attach the hook to this database */
121640   int (*xCallback)(void*),  /* Function to invoke on each commit */
121641   void *pArg                /* Argument to the function */
121642 ){
121643   void *pOld;
121644   sqlite3_mutex_enter(db->mutex);
121645   pOld = db->pCommitArg;
121646   db->xCommitCallback = xCallback;
121647   db->pCommitArg = pArg;
121648   sqlite3_mutex_leave(db->mutex);
121649   return pOld;
121650 }
121651 
121652 /*
121653 ** Register a callback to be invoked each time a row is updated,
121654 ** inserted or deleted using this database connection.
121655 */
121656 SQLITE_API void *sqlite3_update_hook(
121657   sqlite3 *db,              /* Attach the hook to this database */
121658   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
121659   void *pArg                /* Argument to the function */
121660 ){
121661   void *pRet;
121662   sqlite3_mutex_enter(db->mutex);
121663   pRet = db->pUpdateArg;
121664   db->xUpdateCallback = xCallback;
121665   db->pUpdateArg = pArg;
121666   sqlite3_mutex_leave(db->mutex);
121667   return pRet;
121668 }
121669 
121670 /*
121671 ** Register a callback to be invoked each time a transaction is rolled
121672 ** back by this database connection.
121673 */
121674 SQLITE_API void *sqlite3_rollback_hook(
121675   sqlite3 *db,              /* Attach the hook to this database */
121676   void (*xCallback)(void*), /* Callback function */
121677   void *pArg                /* Argument to the function */
121678 ){
121679   void *pRet;
121680   sqlite3_mutex_enter(db->mutex);
121681   pRet = db->pRollbackArg;
121682   db->xRollbackCallback = xCallback;
121683   db->pRollbackArg = pArg;
121684   sqlite3_mutex_leave(db->mutex);
121685   return pRet;
121686 }
121687 
121688 #ifndef SQLITE_OMIT_WAL
121689 /*
121690 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
121691 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
121692 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
121693 ** wal_autocheckpoint()).
121694 */
121695 SQLITE_PRIVATE int sqlite3WalDefaultHook(
121696   void *pClientData,     /* Argument */
121697   sqlite3 *db,           /* Connection */
121698   const char *zDb,       /* Database */
121699   int nFrame             /* Size of WAL */
121700 ){
121701   if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
121702     sqlite3BeginBenignMalloc();
121703     sqlite3_wal_checkpoint(db, zDb);
121704     sqlite3EndBenignMalloc();
121705   }
121706   return SQLITE_OK;
121707 }
121708 #endif /* SQLITE_OMIT_WAL */
121709 
121710 /*
121711 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
121712 ** a database after committing a transaction if there are nFrame or
121713 ** more frames in the log file. Passing zero or a negative value as the
121714 ** nFrame parameter disables automatic checkpoints entirely.
121715 **
121716 ** The callback registered by this function replaces any existing callback
121717 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
121718 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
121719 ** configured by this function.
121720 */
121721 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
121722 #ifdef SQLITE_OMIT_WAL
121723   UNUSED_PARAMETER(db);
121724   UNUSED_PARAMETER(nFrame);
121725 #else
121726   if( nFrame>0 ){
121727     sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
121728   }else{
121729     sqlite3_wal_hook(db, 0, 0);
121730   }
121731 #endif
121732   return SQLITE_OK;
121733 }
121734 
121735 /*
121736 ** Register a callback to be invoked each time a transaction is written
121737 ** into the write-ahead-log by this database connection.
121738 */
121739 SQLITE_API void *sqlite3_wal_hook(
121740   sqlite3 *db,                    /* Attach the hook to this db handle */
121741   int(*xCallback)(void *, sqlite3*, const char*, int),
121742   void *pArg                      /* First argument passed to xCallback() */
121743 ){
121744 #ifndef SQLITE_OMIT_WAL
121745   void *pRet;
121746   sqlite3_mutex_enter(db->mutex);
121747   pRet = db->pWalArg;
121748   db->xWalCallback = xCallback;
121749   db->pWalArg = pArg;
121750   sqlite3_mutex_leave(db->mutex);
121751   return pRet;
121752 #else
121753   return 0;
121754 #endif
121755 }
121756 
121757 /*
121758 ** Checkpoint database zDb.
121759 */
121760 SQLITE_API int sqlite3_wal_checkpoint_v2(
121761   sqlite3 *db,                    /* Database handle */
121762   const char *zDb,                /* Name of attached database (or NULL) */
121763   int eMode,                      /* SQLITE_CHECKPOINT_* value */
121764   int *pnLog,                     /* OUT: Size of WAL log in frames */
121765   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
121766 ){
121767 #ifdef SQLITE_OMIT_WAL
121768   return SQLITE_OK;
121769 #else
121770   int rc;                         /* Return code */
121771   int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
121772 
121773   /* Initialize the output variables to -1 in case an error occurs. */
121774   if( pnLog ) *pnLog = -1;
121775   if( pnCkpt ) *pnCkpt = -1;
121776 
121777   assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
121778   assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
121779   assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
121780   if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
121781     return SQLITE_MISUSE;
121782   }
121783 
121784   sqlite3_mutex_enter(db->mutex);
121785   if( zDb && zDb[0] ){
121786     iDb = sqlite3FindDbName(db, zDb);
121787   }
121788   if( iDb<0 ){
121789     rc = SQLITE_ERROR;
121790     sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
121791   }else{
121792     rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
121793     sqlite3Error(db, rc, 0);
121794   }
121795   rc = sqlite3ApiExit(db, rc);
121796   sqlite3_mutex_leave(db->mutex);
121797   return rc;
121798 #endif
121799 }
121800 
121801 
121802 /*
121803 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
121804 ** to contains a zero-length string, all attached databases are
121805 ** checkpointed.
121806 */
121807 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
121808   return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
121809 }
121810 
121811 #ifndef SQLITE_OMIT_WAL
121812 /*
121813 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
121814 ** not currently open in WAL mode.
121815 **
121816 ** If a transaction is open on the database being checkpointed, this
121817 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
121818 ** an error occurs while running the checkpoint, an SQLite error code is
121819 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
121820 **
121821 ** The mutex on database handle db should be held by the caller. The mutex
121822 ** associated with the specific b-tree being checkpointed is taken by
121823 ** this function while the checkpoint is running.
121824 **
121825 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
121826 ** checkpointed. If an error is encountered it is returned immediately -
121827 ** no attempt is made to checkpoint any remaining databases.
121828 **
121829 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
121830 */
121831 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
121832   int rc = SQLITE_OK;             /* Return code */
121833   int i;                          /* Used to iterate through attached dbs */
121834   int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
121835 
121836   assert( sqlite3_mutex_held(db->mutex) );
121837   assert( !pnLog || *pnLog==-1 );
121838   assert( !pnCkpt || *pnCkpt==-1 );
121839 
121840   for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
121841     if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
121842       rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
121843       pnLog = 0;
121844       pnCkpt = 0;
121845       if( rc==SQLITE_BUSY ){
121846         bBusy = 1;
121847         rc = SQLITE_OK;
121848       }
121849     }
121850   }
121851 
121852   return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
121853 }
121854 #endif /* SQLITE_OMIT_WAL */
121855 
121856 /*
121857 ** This function returns true if main-memory should be used instead of
121858 ** a temporary file for transient pager files and statement journals.
121859 ** The value returned depends on the value of db->temp_store (runtime
121860 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
121861 ** following table describes the relationship between these two values
121862 ** and this functions return value.
121863 **
121864 **   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
121865 **   -----------------     --------------     ------------------------------
121866 **   0                     any                file      (return 0)
121867 **   1                     1                  file      (return 0)
121868 **   1                     2                  memory    (return 1)
121869 **   1                     0                  file      (return 0)
121870 **   2                     1                  file      (return 0)
121871 **   2                     2                  memory    (return 1)
121872 **   2                     0                  memory    (return 1)
121873 **   3                     any                memory    (return 1)
121874 */
121875 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
121876 #if SQLITE_TEMP_STORE==1
121877   return ( db->temp_store==2 );
121878 #endif
121879 #if SQLITE_TEMP_STORE==2
121880   return ( db->temp_store!=1 );
121881 #endif
121882 #if SQLITE_TEMP_STORE==3
121883   return 1;
121884 #endif
121885 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
121886   return 0;
121887 #endif
121888 }
121889 
121890 /*
121891 ** Return UTF-8 encoded English language explanation of the most recent
121892 ** error.
121893 */
121894 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
121895   const char *z;
121896   if( !db ){
121897     return sqlite3ErrStr(SQLITE_NOMEM);
121898   }
121899   if( !sqlite3SafetyCheckSickOrOk(db) ){
121900     return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
121901   }
121902   sqlite3_mutex_enter(db->mutex);
121903   if( db->mallocFailed ){
121904     z = sqlite3ErrStr(SQLITE_NOMEM);
121905   }else{
121906     testcase( db->pErr==0 );
121907     z = (char*)sqlite3_value_text(db->pErr);
121908     assert( !db->mallocFailed );
121909     if( z==0 ){
121910       z = sqlite3ErrStr(db->errCode);
121911     }
121912   }
121913   sqlite3_mutex_leave(db->mutex);
121914   return z;
121915 }
121916 
121917 #ifndef SQLITE_OMIT_UTF16
121918 /*
121919 ** Return UTF-16 encoded English language explanation of the most recent
121920 ** error.
121921 */
121922 SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
121923   static const u16 outOfMem[] = {
121924     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
121925   };
121926   static const u16 misuse[] = {
121927     'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
121928     'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
121929     'c', 'a', 'l', 'l', 'e', 'd', ' ',
121930     'o', 'u', 't', ' ',
121931     'o', 'f', ' ',
121932     's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
121933   };
121934 
121935   const void *z;
121936   if( !db ){
121937     return (void *)outOfMem;
121938   }
121939   if( !sqlite3SafetyCheckSickOrOk(db) ){
121940     return (void *)misuse;
121941   }
121942   sqlite3_mutex_enter(db->mutex);
121943   if( db->mallocFailed ){
121944     z = (void *)outOfMem;
121945   }else{
121946     z = sqlite3_value_text16(db->pErr);
121947     if( z==0 ){
121948       sqlite3Error(db, db->errCode, sqlite3ErrStr(db->errCode));
121949       z = sqlite3_value_text16(db->pErr);
121950     }
121951     /* A malloc() may have failed within the call to sqlite3_value_text16()
121952     ** above. If this is the case, then the db->mallocFailed flag needs to
121953     ** be cleared before returning. Do this directly, instead of via
121954     ** sqlite3ApiExit(), to avoid setting the database handle error message.
121955     */
121956     db->mallocFailed = 0;
121957   }
121958   sqlite3_mutex_leave(db->mutex);
121959   return z;
121960 }
121961 #endif /* SQLITE_OMIT_UTF16 */
121962 
121963 /*
121964 ** Return the most recent error code generated by an SQLite routine. If NULL is
121965 ** passed to this function, we assume a malloc() failed during sqlite3_open().
121966 */
121967 SQLITE_API int sqlite3_errcode(sqlite3 *db){
121968   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
121969     return SQLITE_MISUSE_BKPT;
121970   }
121971   if( !db || db->mallocFailed ){
121972     return SQLITE_NOMEM;
121973   }
121974   return db->errCode & db->errMask;
121975 }
121976 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
121977   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
121978     return SQLITE_MISUSE_BKPT;
121979   }
121980   if( !db || db->mallocFailed ){
121981     return SQLITE_NOMEM;
121982   }
121983   return db->errCode;
121984 }
121985 
121986 /*
121987 ** Return a string that describes the kind of error specified in the
121988 ** argument.  For now, this simply calls the internal sqlite3ErrStr()
121989 ** function.
121990 */
121991 SQLITE_API const char *sqlite3_errstr(int rc){
121992   return sqlite3ErrStr(rc);
121993 }
121994 
121995 /*
121996 ** Invalidate all cached KeyInfo objects for database connection "db"
121997 */
121998 static void invalidateCachedKeyInfo(sqlite3 *db){
121999   Db *pDb;                    /* A single database */
122000   int iDb;                    /* The database index number */
122001   HashElem *k;                /* For looping over tables in pDb */
122002   Table *pTab;                /* A table in the database */
122003   Index *pIdx;                /* Each index */
122004 
122005   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
122006     if( pDb->pBt==0 ) continue;
122007     sqlite3BtreeEnter(pDb->pBt);
122008     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
122009       pTab = (Table*)sqliteHashData(k);
122010       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
122011         if( pIdx->pKeyInfo && pIdx->pKeyInfo->db==db ){
122012           sqlite3KeyInfoUnref(pIdx->pKeyInfo);
122013           pIdx->pKeyInfo = 0;
122014         }
122015       }
122016     }
122017     sqlite3BtreeLeave(pDb->pBt);
122018   }
122019 }
122020 
122021 /*
122022 ** Create a new collating function for database "db".  The name is zName
122023 ** and the encoding is enc.
122024 */
122025 static int createCollation(
122026   sqlite3* db,
122027   const char *zName,
122028   u8 enc,
122029   void* pCtx,
122030   int(*xCompare)(void*,int,const void*,int,const void*),
122031   void(*xDel)(void*)
122032 ){
122033   CollSeq *pColl;
122034   int enc2;
122035   int nName = sqlite3Strlen30(zName);
122036 
122037   assert( sqlite3_mutex_held(db->mutex) );
122038 
122039   /* If SQLITE_UTF16 is specified as the encoding type, transform this
122040   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
122041   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
122042   */
122043   enc2 = enc;
122044   testcase( enc2==SQLITE_UTF16 );
122045   testcase( enc2==SQLITE_UTF16_ALIGNED );
122046   if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
122047     enc2 = SQLITE_UTF16NATIVE;
122048   }
122049   if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
122050     return SQLITE_MISUSE_BKPT;
122051   }
122052 
122053   /* Check if this call is removing or replacing an existing collation
122054   ** sequence. If so, and there are active VMs, return busy. If there
122055   ** are no active VMs, invalidate any pre-compiled statements.
122056   */
122057   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
122058   if( pColl && pColl->xCmp ){
122059     if( db->nVdbeActive ){
122060       sqlite3Error(db, SQLITE_BUSY,
122061         "unable to delete/modify collation sequence due to active statements");
122062       return SQLITE_BUSY;
122063     }
122064     sqlite3ExpirePreparedStatements(db);
122065     invalidateCachedKeyInfo(db);
122066 
122067     /* If collation sequence pColl was created directly by a call to
122068     ** sqlite3_create_collation, and not generated by synthCollSeq(),
122069     ** then any copies made by synthCollSeq() need to be invalidated.
122070     ** Also, collation destructor - CollSeq.xDel() - function may need
122071     ** to be called.
122072     */
122073     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
122074       CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
122075       int j;
122076       for(j=0; j<3; j++){
122077         CollSeq *p = &aColl[j];
122078         if( p->enc==pColl->enc ){
122079           if( p->xDel ){
122080             p->xDel(p->pUser);
122081           }
122082           p->xCmp = 0;
122083         }
122084       }
122085     }
122086   }
122087 
122088   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
122089   if( pColl==0 ) return SQLITE_NOMEM;
122090   pColl->xCmp = xCompare;
122091   pColl->pUser = pCtx;
122092   pColl->xDel = xDel;
122093   pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
122094   sqlite3Error(db, SQLITE_OK, 0);
122095   return SQLITE_OK;
122096 }
122097 
122098 
122099 /*
122100 ** This array defines hard upper bounds on limit values.  The
122101 ** initializer must be kept in sync with the SQLITE_LIMIT_*
122102 ** #defines in sqlite3.h.
122103 */
122104 static const int aHardLimit[] = {
122105   SQLITE_MAX_LENGTH,
122106   SQLITE_MAX_SQL_LENGTH,
122107   SQLITE_MAX_COLUMN,
122108   SQLITE_MAX_EXPR_DEPTH,
122109   SQLITE_MAX_COMPOUND_SELECT,
122110   SQLITE_MAX_VDBE_OP,
122111   SQLITE_MAX_FUNCTION_ARG,
122112   SQLITE_MAX_ATTACHED,
122113   SQLITE_MAX_LIKE_PATTERN_LENGTH,
122114   SQLITE_MAX_VARIABLE_NUMBER,
122115   SQLITE_MAX_TRIGGER_DEPTH,
122116 };
122117 
122118 /*
122119 ** Make sure the hard limits are set to reasonable values
122120 */
122121 #if SQLITE_MAX_LENGTH<100
122122 # error SQLITE_MAX_LENGTH must be at least 100
122123 #endif
122124 #if SQLITE_MAX_SQL_LENGTH<100
122125 # error SQLITE_MAX_SQL_LENGTH must be at least 100
122126 #endif
122127 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
122128 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
122129 #endif
122130 #if SQLITE_MAX_COMPOUND_SELECT<2
122131 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
122132 #endif
122133 #if SQLITE_MAX_VDBE_OP<40
122134 # error SQLITE_MAX_VDBE_OP must be at least 40
122135 #endif
122136 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
122137 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
122138 #endif
122139 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
122140 # error SQLITE_MAX_ATTACHED must be between 0 and 62
122141 #endif
122142 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
122143 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
122144 #endif
122145 #if SQLITE_MAX_COLUMN>32767
122146 # error SQLITE_MAX_COLUMN must not exceed 32767
122147 #endif
122148 #if SQLITE_MAX_TRIGGER_DEPTH<1
122149 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
122150 #endif
122151 
122152 
122153 /*
122154 ** Change the value of a limit.  Report the old value.
122155 ** If an invalid limit index is supplied, report -1.
122156 ** Make no changes but still report the old value if the
122157 ** new limit is negative.
122158 **
122159 ** A new lower limit does not shrink existing constructs.
122160 ** It merely prevents new constructs that exceed the limit
122161 ** from forming.
122162 */
122163 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
122164   int oldLimit;
122165 
122166 
122167   /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
122168   ** there is a hard upper bound set at compile-time by a C preprocessor
122169   ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
122170   ** "_MAX_".)
122171   */
122172   assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
122173   assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
122174   assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
122175   assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
122176   assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
122177   assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
122178   assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
122179   assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
122180   assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
122181                                                SQLITE_MAX_LIKE_PATTERN_LENGTH );
122182   assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
122183   assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
122184   assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
122185 
122186 
122187   if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
122188     return -1;
122189   }
122190   oldLimit = db->aLimit[limitId];
122191   if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
122192     if( newLimit>aHardLimit[limitId] ){
122193       newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
122194     }
122195     db->aLimit[limitId] = newLimit;
122196   }
122197   return oldLimit;                     /* IMP: R-53341-35419 */
122198 }
122199 
122200 /*
122201 ** This function is used to parse both URIs and non-URI filenames passed by the
122202 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
122203 ** URIs specified as part of ATTACH statements.
122204 **
122205 ** The first argument to this function is the name of the VFS to use (or
122206 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
122207 ** query parameter. The second argument contains the URI (or non-URI filename)
122208 ** itself. When this function is called the *pFlags variable should contain
122209 ** the default flags to open the database handle with. The value stored in
122210 ** *pFlags may be updated before returning if the URI filename contains
122211 ** "cache=xxx" or "mode=xxx" query parameters.
122212 **
122213 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
122214 ** the VFS that should be used to open the database file. *pzFile is set to
122215 ** point to a buffer containing the name of the file to open. It is the
122216 ** responsibility of the caller to eventually call sqlite3_free() to release
122217 ** this buffer.
122218 **
122219 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
122220 ** may be set to point to a buffer containing an English language error
122221 ** message. It is the responsibility of the caller to eventually release
122222 ** this buffer by calling sqlite3_free().
122223 */
122224 SQLITE_PRIVATE int sqlite3ParseUri(
122225   const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
122226   const char *zUri,               /* Nul-terminated URI to parse */
122227   unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
122228   sqlite3_vfs **ppVfs,            /* OUT: VFS to use */
122229   char **pzFile,                  /* OUT: Filename component of URI */
122230   char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
122231 ){
122232   int rc = SQLITE_OK;
122233   unsigned int flags = *pFlags;
122234   const char *zVfs = zDefaultVfs;
122235   char *zFile;
122236   char c;
122237   int nUri = sqlite3Strlen30(zUri);
122238 
122239   assert( *pzErrMsg==0 );
122240 
122241   if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
122242    && nUri>=5 && memcmp(zUri, "file:", 5)==0
122243   ){
122244     char *zOpt;
122245     int eState;                   /* Parser state when parsing URI */
122246     int iIn;                      /* Input character index */
122247     int iOut = 0;                 /* Output character index */
122248     int nByte = nUri+2;           /* Bytes of space to allocate */
122249 
122250     /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
122251     ** method that there may be extra parameters following the file-name.  */
122252     flags |= SQLITE_OPEN_URI;
122253 
122254     for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
122255     zFile = sqlite3_malloc(nByte);
122256     if( !zFile ) return SQLITE_NOMEM;
122257 
122258     iIn = 5;
122259 #ifndef SQLITE_ALLOW_URI_AUTHORITY
122260     /* Discard the scheme and authority segments of the URI. */
122261     if( zUri[5]=='/' && zUri[6]=='/' ){
122262       iIn = 7;
122263       while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
122264       if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
122265         *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
122266             iIn-7, &zUri[7]);
122267         rc = SQLITE_ERROR;
122268         goto parse_uri_out;
122269       }
122270     }
122271 #endif
122272 
122273     /* Copy the filename and any query parameters into the zFile buffer.
122274     ** Decode %HH escape codes along the way.
122275     **
122276     ** Within this loop, variable eState may be set to 0, 1 or 2, depending
122277     ** on the parsing context. As follows:
122278     **
122279     **   0: Parsing file-name.
122280     **   1: Parsing name section of a name=value query parameter.
122281     **   2: Parsing value section of a name=value query parameter.
122282     */
122283     eState = 0;
122284     while( (c = zUri[iIn])!=0 && c!='#' ){
122285       iIn++;
122286       if( c=='%'
122287        && sqlite3Isxdigit(zUri[iIn])
122288        && sqlite3Isxdigit(zUri[iIn+1])
122289       ){
122290         int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
122291         octet += sqlite3HexToInt(zUri[iIn++]);
122292 
122293         assert( octet>=0 && octet<256 );
122294         if( octet==0 ){
122295           /* This branch is taken when "%00" appears within the URI. In this
122296           ** case we ignore all text in the remainder of the path, name or
122297           ** value currently being parsed. So ignore the current character
122298           ** and skip to the next "?", "=" or "&", as appropriate. */
122299           while( (c = zUri[iIn])!=0 && c!='#'
122300               && (eState!=0 || c!='?')
122301               && (eState!=1 || (c!='=' && c!='&'))
122302               && (eState!=2 || c!='&')
122303           ){
122304             iIn++;
122305           }
122306           continue;
122307         }
122308         c = octet;
122309       }else if( eState==1 && (c=='&' || c=='=') ){
122310         if( zFile[iOut-1]==0 ){
122311           /* An empty option name. Ignore this option altogether. */
122312           while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
122313           continue;
122314         }
122315         if( c=='&' ){
122316           zFile[iOut++] = '\0';
122317         }else{
122318           eState = 2;
122319         }
122320         c = 0;
122321       }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
122322         c = 0;
122323         eState = 1;
122324       }
122325       zFile[iOut++] = c;
122326     }
122327     if( eState==1 ) zFile[iOut++] = '\0';
122328     zFile[iOut++] = '\0';
122329     zFile[iOut++] = '\0';
122330 
122331     /* Check if there were any options specified that should be interpreted
122332     ** here. Options that are interpreted here include "vfs" and those that
122333     ** correspond to flags that may be passed to the sqlite3_open_v2()
122334     ** method. */
122335     zOpt = &zFile[sqlite3Strlen30(zFile)+1];
122336     while( zOpt[0] ){
122337       int nOpt = sqlite3Strlen30(zOpt);
122338       char *zVal = &zOpt[nOpt+1];
122339       int nVal = sqlite3Strlen30(zVal);
122340 
122341       if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
122342         zVfs = zVal;
122343       }else{
122344         struct OpenMode {
122345           const char *z;
122346           int mode;
122347         } *aMode = 0;
122348         char *zModeType = 0;
122349         int mask = 0;
122350         int limit = 0;
122351 
122352         if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
122353           static struct OpenMode aCacheMode[] = {
122354             { "shared",  SQLITE_OPEN_SHAREDCACHE },
122355             { "private", SQLITE_OPEN_PRIVATECACHE },
122356             { 0, 0 }
122357           };
122358 
122359           mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
122360           aMode = aCacheMode;
122361           limit = mask;
122362           zModeType = "cache";
122363         }
122364         if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
122365           static struct OpenMode aOpenMode[] = {
122366             { "ro",  SQLITE_OPEN_READONLY },
122367             { "rw",  SQLITE_OPEN_READWRITE },
122368             { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
122369             { "memory", SQLITE_OPEN_MEMORY },
122370             { 0, 0 }
122371           };
122372 
122373           mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
122374                    | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
122375           aMode = aOpenMode;
122376           limit = mask & flags;
122377           zModeType = "access";
122378         }
122379 
122380         if( aMode ){
122381           int i;
122382           int mode = 0;
122383           for(i=0; aMode[i].z; i++){
122384             const char *z = aMode[i].z;
122385             if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
122386               mode = aMode[i].mode;
122387               break;
122388             }
122389           }
122390           if( mode==0 ){
122391             *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
122392             rc = SQLITE_ERROR;
122393             goto parse_uri_out;
122394           }
122395           if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
122396             *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
122397                                         zModeType, zVal);
122398             rc = SQLITE_PERM;
122399             goto parse_uri_out;
122400           }
122401           flags = (flags & ~mask) | mode;
122402         }
122403       }
122404 
122405       zOpt = &zVal[nVal+1];
122406     }
122407 
122408   }else{
122409     zFile = sqlite3_malloc(nUri+2);
122410     if( !zFile ) return SQLITE_NOMEM;
122411     memcpy(zFile, zUri, nUri);
122412     zFile[nUri] = '\0';
122413     zFile[nUri+1] = '\0';
122414     flags &= ~SQLITE_OPEN_URI;
122415   }
122416 
122417   *ppVfs = sqlite3_vfs_find(zVfs);
122418   if( *ppVfs==0 ){
122419     *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
122420     rc = SQLITE_ERROR;
122421   }
122422  parse_uri_out:
122423   if( rc!=SQLITE_OK ){
122424     sqlite3_free(zFile);
122425     zFile = 0;
122426   }
122427   *pFlags = flags;
122428   *pzFile = zFile;
122429   return rc;
122430 }
122431 
122432 
122433 /*
122434 ** This routine does the work of opening a database on behalf of
122435 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
122436 ** is UTF-8 encoded.
122437 */
122438 static int openDatabase(
122439   const char *zFilename, /* Database filename UTF-8 encoded */
122440   sqlite3 **ppDb,        /* OUT: Returned database handle */
122441   unsigned int flags,    /* Operational flags */
122442   const char *zVfs       /* Name of the VFS to use */
122443 ){
122444   sqlite3 *db;                    /* Store allocated handle here */
122445   int rc;                         /* Return code */
122446   int isThreadsafe;               /* True for threadsafe connections */
122447   char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
122448   char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
122449 
122450   *ppDb = 0;
122451 #ifndef SQLITE_OMIT_AUTOINIT
122452   rc = sqlite3_initialize();
122453   if( rc ) return rc;
122454 #endif
122455 
122456   /* Only allow sensible combinations of bits in the flags argument.
122457   ** Throw an error if any non-sense combination is used.  If we
122458   ** do not block illegal combinations here, it could trigger
122459   ** assert() statements in deeper layers.  Sensible combinations
122460   ** are:
122461   **
122462   **  1:  SQLITE_OPEN_READONLY
122463   **  2:  SQLITE_OPEN_READWRITE
122464   **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
122465   */
122466   assert( SQLITE_OPEN_READONLY  == 0x01 );
122467   assert( SQLITE_OPEN_READWRITE == 0x02 );
122468   assert( SQLITE_OPEN_CREATE    == 0x04 );
122469   testcase( (1<<(flags&7))==0x02 ); /* READONLY */
122470   testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
122471   testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
122472   if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
122473 
122474   if( sqlite3GlobalConfig.bCoreMutex==0 ){
122475     isThreadsafe = 0;
122476   }else if( flags & SQLITE_OPEN_NOMUTEX ){
122477     isThreadsafe = 0;
122478   }else if( flags & SQLITE_OPEN_FULLMUTEX ){
122479     isThreadsafe = 1;
122480   }else{
122481     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
122482   }
122483   if( flags & SQLITE_OPEN_PRIVATECACHE ){
122484     flags &= ~SQLITE_OPEN_SHAREDCACHE;
122485   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
122486     flags |= SQLITE_OPEN_SHAREDCACHE;
122487   }
122488 
122489   /* Remove harmful bits from the flags parameter
122490   **
122491   ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
122492   ** dealt with in the previous code block.  Besides these, the only
122493   ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
122494   ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
122495   ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
122496   ** off all other flags.
122497   */
122498   flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
122499                SQLITE_OPEN_EXCLUSIVE |
122500                SQLITE_OPEN_MAIN_DB |
122501                SQLITE_OPEN_TEMP_DB |
122502                SQLITE_OPEN_TRANSIENT_DB |
122503                SQLITE_OPEN_MAIN_JOURNAL |
122504                SQLITE_OPEN_TEMP_JOURNAL |
122505                SQLITE_OPEN_SUBJOURNAL |
122506                SQLITE_OPEN_MASTER_JOURNAL |
122507                SQLITE_OPEN_NOMUTEX |
122508                SQLITE_OPEN_FULLMUTEX |
122509                SQLITE_OPEN_WAL
122510              );
122511 
122512   /* Allocate the sqlite data structure */
122513   db = sqlite3MallocZero( sizeof(sqlite3) );
122514   if( db==0 ) goto opendb_out;
122515   if( isThreadsafe ){
122516     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
122517     if( db->mutex==0 ){
122518       sqlite3_free(db);
122519       db = 0;
122520       goto opendb_out;
122521     }
122522   }
122523   sqlite3_mutex_enter(db->mutex);
122524   db->errMask = 0xff;
122525   db->nDb = 2;
122526   db->magic = SQLITE_MAGIC_BUSY;
122527   db->aDb = db->aDbStatic;
122528 
122529   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
122530   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
122531   db->autoCommit = 1;
122532   db->nextAutovac = -1;
122533   db->szMmap = sqlite3GlobalConfig.szMmap;
122534   db->nextPagesize = 0;
122535   db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
122536 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
122537                  | SQLITE_AutoIndex
122538 #endif
122539 #if SQLITE_DEFAULT_FILE_FORMAT<4
122540                  | SQLITE_LegacyFileFmt
122541 #endif
122542 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
122543                  | SQLITE_LoadExtension
122544 #endif
122545 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
122546                  | SQLITE_RecTriggers
122547 #endif
122548 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
122549                  | SQLITE_ForeignKeys
122550 #endif
122551       ;
122552   sqlite3HashInit(&db->aCollSeq);
122553 #ifndef SQLITE_OMIT_VIRTUALTABLE
122554   sqlite3HashInit(&db->aModule);
122555 #endif
122556 
122557   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
122558   ** and UTF-16, so add a version for each to avoid any unnecessary
122559   ** conversions. The only error that can occur here is a malloc() failure.
122560   */
122561   createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
122562   createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
122563   createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
122564   createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
122565   if( db->mallocFailed ){
122566     goto opendb_out;
122567   }
122568   db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
122569   assert( db->pDfltColl!=0 );
122570 
122571   /* Also add a UTF-8 case-insensitive collation sequence. */
122572   createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
122573 
122574   /* Parse the filename/URI argument. */
122575   db->openFlags = flags;
122576   rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
122577   if( rc!=SQLITE_OK ){
122578     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
122579     sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
122580     sqlite3_free(zErrMsg);
122581     goto opendb_out;
122582   }
122583 
122584   /* Open the backend database driver */
122585   rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
122586                         flags | SQLITE_OPEN_MAIN_DB);
122587   if( rc!=SQLITE_OK ){
122588     if( rc==SQLITE_IOERR_NOMEM ){
122589       rc = SQLITE_NOMEM;
122590     }
122591     sqlite3Error(db, rc, 0);
122592     goto opendb_out;
122593   }
122594   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
122595   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
122596 
122597 
122598   /* The default safety_level for the main database is 'full'; for the temp
122599   ** database it is 'NONE'. This matches the pager layer defaults.
122600   */
122601   db->aDb[0].zName = "main";
122602   db->aDb[0].safety_level = 3;
122603   db->aDb[1].zName = "temp";
122604   db->aDb[1].safety_level = 1;
122605 
122606   db->magic = SQLITE_MAGIC_OPEN;
122607   if( db->mallocFailed ){
122608     goto opendb_out;
122609   }
122610 
122611   /* Register all built-in functions, but do not attempt to read the
122612   ** database schema yet. This is delayed until the first time the database
122613   ** is accessed.
122614   */
122615   sqlite3Error(db, SQLITE_OK, 0);
122616   sqlite3RegisterBuiltinFunctions(db);
122617 
122618   /* Load automatic extensions - extensions that have been registered
122619   ** using the sqlite3_automatic_extension() API.
122620   */
122621   rc = sqlite3_errcode(db);
122622   if( rc==SQLITE_OK ){
122623     sqlite3AutoLoadExtensions(db);
122624     rc = sqlite3_errcode(db);
122625     if( rc!=SQLITE_OK ){
122626       goto opendb_out;
122627     }
122628   }
122629 
122630 #ifdef SQLITE_ENABLE_FTS1
122631   if( !db->mallocFailed ){
122632     extern int sqlite3Fts1Init(sqlite3*);
122633     rc = sqlite3Fts1Init(db);
122634   }
122635 #endif
122636 
122637 #ifdef SQLITE_ENABLE_FTS2
122638   if( !db->mallocFailed && rc==SQLITE_OK ){
122639     extern int sqlite3Fts2Init(sqlite3*);
122640     rc = sqlite3Fts2Init(db);
122641   }
122642 #endif
122643 
122644 #ifdef SQLITE_ENABLE_FTS3
122645   if( !db->mallocFailed && rc==SQLITE_OK ){
122646     rc = sqlite3Fts3Init(db);
122647   }
122648 #endif
122649 
122650 #ifdef SQLITE_ENABLE_ICU
122651   if( !db->mallocFailed && rc==SQLITE_OK ){
122652     rc = sqlite3IcuInit(db);
122653   }
122654 #endif
122655 
122656 #ifdef SQLITE_ENABLE_RTREE
122657   if( !db->mallocFailed && rc==SQLITE_OK){
122658     rc = sqlite3RtreeInit(db);
122659   }
122660 #endif
122661 
122662   /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
122663   ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
122664   ** mode.  Doing nothing at all also makes NORMAL the default.
122665   */
122666 #ifdef SQLITE_DEFAULT_LOCKING_MODE
122667   db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
122668   sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
122669                           SQLITE_DEFAULT_LOCKING_MODE);
122670 #endif
122671 
122672   if( rc ) sqlite3Error(db, rc, 0);
122673 
122674   /* Enable the lookaside-malloc subsystem */
122675   setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
122676                         sqlite3GlobalConfig.nLookaside);
122677 
122678   sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
122679 
122680 opendb_out:
122681   sqlite3_free(zOpen);
122682   if( db ){
122683     assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
122684     sqlite3_mutex_leave(db->mutex);
122685   }
122686   rc = sqlite3_errcode(db);
122687   assert( db!=0 || rc==SQLITE_NOMEM );
122688   if( rc==SQLITE_NOMEM ){
122689     sqlite3_close(db);
122690     db = 0;
122691   }else if( rc!=SQLITE_OK ){
122692     db->magic = SQLITE_MAGIC_SICK;
122693   }
122694   *ppDb = db;
122695 #ifdef SQLITE_ENABLE_SQLLOG
122696   if( sqlite3GlobalConfig.xSqllog ){
122697     /* Opening a db handle. Fourth parameter is passed 0. */
122698     void *pArg = sqlite3GlobalConfig.pSqllogArg;
122699     sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
122700   }
122701 #endif
122702   return sqlite3ApiExit(0, rc);
122703 }
122704 
122705 /*
122706 ** Open a new database handle.
122707 */
122708 SQLITE_API int sqlite3_open(
122709   const char *zFilename,
122710   sqlite3 **ppDb
122711 ){
122712   return openDatabase(zFilename, ppDb,
122713                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
122714 }
122715 SQLITE_API int sqlite3_open_v2(
122716   const char *filename,   /* Database filename (UTF-8) */
122717   sqlite3 **ppDb,         /* OUT: SQLite db handle */
122718   int flags,              /* Flags */
122719   const char *zVfs        /* Name of VFS module to use */
122720 ){
122721   return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
122722 }
122723 
122724 #ifndef SQLITE_OMIT_UTF16
122725 /*
122726 ** Open a new database handle.
122727 */
122728 SQLITE_API int sqlite3_open16(
122729   const void *zFilename,
122730   sqlite3 **ppDb
122731 ){
122732   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
122733   sqlite3_value *pVal;
122734   int rc;
122735 
122736   assert( zFilename );
122737   assert( ppDb );
122738   *ppDb = 0;
122739 #ifndef SQLITE_OMIT_AUTOINIT
122740   rc = sqlite3_initialize();
122741   if( rc ) return rc;
122742 #endif
122743   pVal = sqlite3ValueNew(0);
122744   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
122745   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
122746   if( zFilename8 ){
122747     rc = openDatabase(zFilename8, ppDb,
122748                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
122749     assert( *ppDb || rc==SQLITE_NOMEM );
122750     if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
122751       ENC(*ppDb) = SQLITE_UTF16NATIVE;
122752     }
122753   }else{
122754     rc = SQLITE_NOMEM;
122755   }
122756   sqlite3ValueFree(pVal);
122757 
122758   return sqlite3ApiExit(0, rc);
122759 }
122760 #endif /* SQLITE_OMIT_UTF16 */
122761 
122762 /*
122763 ** Register a new collation sequence with the database handle db.
122764 */
122765 SQLITE_API int sqlite3_create_collation(
122766   sqlite3* db,
122767   const char *zName,
122768   int enc,
122769   void* pCtx,
122770   int(*xCompare)(void*,int,const void*,int,const void*)
122771 ){
122772   int rc;
122773   sqlite3_mutex_enter(db->mutex);
122774   assert( !db->mallocFailed );
122775   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
122776   rc = sqlite3ApiExit(db, rc);
122777   sqlite3_mutex_leave(db->mutex);
122778   return rc;
122779 }
122780 
122781 /*
122782 ** Register a new collation sequence with the database handle db.
122783 */
122784 SQLITE_API int sqlite3_create_collation_v2(
122785   sqlite3* db,
122786   const char *zName,
122787   int enc,
122788   void* pCtx,
122789   int(*xCompare)(void*,int,const void*,int,const void*),
122790   void(*xDel)(void*)
122791 ){
122792   int rc;
122793   sqlite3_mutex_enter(db->mutex);
122794   assert( !db->mallocFailed );
122795   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
122796   rc = sqlite3ApiExit(db, rc);
122797   sqlite3_mutex_leave(db->mutex);
122798   return rc;
122799 }
122800 
122801 #ifndef SQLITE_OMIT_UTF16
122802 /*
122803 ** Register a new collation sequence with the database handle db.
122804 */
122805 SQLITE_API int sqlite3_create_collation16(
122806   sqlite3* db,
122807   const void *zName,
122808   int enc,
122809   void* pCtx,
122810   int(*xCompare)(void*,int,const void*,int,const void*)
122811 ){
122812   int rc = SQLITE_OK;
122813   char *zName8;
122814   sqlite3_mutex_enter(db->mutex);
122815   assert( !db->mallocFailed );
122816   zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
122817   if( zName8 ){
122818     rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
122819     sqlite3DbFree(db, zName8);
122820   }
122821   rc = sqlite3ApiExit(db, rc);
122822   sqlite3_mutex_leave(db->mutex);
122823   return rc;
122824 }
122825 #endif /* SQLITE_OMIT_UTF16 */
122826 
122827 /*
122828 ** Register a collation sequence factory callback with the database handle
122829 ** db. Replace any previously installed collation sequence factory.
122830 */
122831 SQLITE_API int sqlite3_collation_needed(
122832   sqlite3 *db,
122833   void *pCollNeededArg,
122834   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
122835 ){
122836   sqlite3_mutex_enter(db->mutex);
122837   db->xCollNeeded = xCollNeeded;
122838   db->xCollNeeded16 = 0;
122839   db->pCollNeededArg = pCollNeededArg;
122840   sqlite3_mutex_leave(db->mutex);
122841   return SQLITE_OK;
122842 }
122843 
122844 #ifndef SQLITE_OMIT_UTF16
122845 /*
122846 ** Register a collation sequence factory callback with the database handle
122847 ** db. Replace any previously installed collation sequence factory.
122848 */
122849 SQLITE_API int sqlite3_collation_needed16(
122850   sqlite3 *db,
122851   void *pCollNeededArg,
122852   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
122853 ){
122854   sqlite3_mutex_enter(db->mutex);
122855   db->xCollNeeded = 0;
122856   db->xCollNeeded16 = xCollNeeded16;
122857   db->pCollNeededArg = pCollNeededArg;
122858   sqlite3_mutex_leave(db->mutex);
122859   return SQLITE_OK;
122860 }
122861 #endif /* SQLITE_OMIT_UTF16 */
122862 
122863 #ifndef SQLITE_OMIT_DEPRECATED
122864 /*
122865 ** This function is now an anachronism. It used to be used to recover from a
122866 ** malloc() failure, but SQLite now does this automatically.
122867 */
122868 SQLITE_API int sqlite3_global_recover(void){
122869   return SQLITE_OK;
122870 }
122871 #endif
122872 
122873 /*
122874 ** Test to see whether or not the database connection is in autocommit
122875 ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
122876 ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
122877 ** by the next COMMIT or ROLLBACK.
122878 */
122879 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
122880   return db->autoCommit;
122881 }
122882 
122883 /*
122884 ** The following routines are subtitutes for constants SQLITE_CORRUPT,
122885 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
122886 ** constants.  They server two purposes:
122887 **
122888 **   1.  Serve as a convenient place to set a breakpoint in a debugger
122889 **       to detect when version error conditions occurs.
122890 **
122891 **   2.  Invoke sqlite3_log() to provide the source code location where
122892 **       a low-level error is first detected.
122893 */
122894 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
122895   testcase( sqlite3GlobalConfig.xLog!=0 );
122896   sqlite3_log(SQLITE_CORRUPT,
122897               "database corruption at line %d of [%.10s]",
122898               lineno, 20+sqlite3_sourceid());
122899   return SQLITE_CORRUPT;
122900 }
122901 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
122902   testcase( sqlite3GlobalConfig.xLog!=0 );
122903   sqlite3_log(SQLITE_MISUSE,
122904               "misuse at line %d of [%.10s]",
122905               lineno, 20+sqlite3_sourceid());
122906   return SQLITE_MISUSE;
122907 }
122908 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
122909   testcase( sqlite3GlobalConfig.xLog!=0 );
122910   sqlite3_log(SQLITE_CANTOPEN,
122911               "cannot open file at line %d of [%.10s]",
122912               lineno, 20+sqlite3_sourceid());
122913   return SQLITE_CANTOPEN;
122914 }
122915 
122916 
122917 #ifndef SQLITE_OMIT_DEPRECATED
122918 /*
122919 ** This is a convenience routine that makes sure that all thread-specific
122920 ** data for this thread has been deallocated.
122921 **
122922 ** SQLite no longer uses thread-specific data so this routine is now a
122923 ** no-op.  It is retained for historical compatibility.
122924 */
122925 SQLITE_API void sqlite3_thread_cleanup(void){
122926 }
122927 #endif
122928 
122929 /*
122930 ** Return meta information about a specific column of a database table.
122931 ** See comment in sqlite3.h (sqlite.h.in) for details.
122932 */
122933 #ifdef SQLITE_ENABLE_COLUMN_METADATA
122934 SQLITE_API int sqlite3_table_column_metadata(
122935   sqlite3 *db,                /* Connection handle */
122936   const char *zDbName,        /* Database name or NULL */
122937   const char *zTableName,     /* Table name */
122938   const char *zColumnName,    /* Column name */
122939   char const **pzDataType,    /* OUTPUT: Declared data type */
122940   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
122941   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
122942   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
122943   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
122944 ){
122945   int rc;
122946   char *zErrMsg = 0;
122947   Table *pTab = 0;
122948   Column *pCol = 0;
122949   int iCol;
122950 
122951   char const *zDataType = 0;
122952   char const *zCollSeq = 0;
122953   int notnull = 0;
122954   int primarykey = 0;
122955   int autoinc = 0;
122956 
122957   /* Ensure the database schema has been loaded */
122958   sqlite3_mutex_enter(db->mutex);
122959   sqlite3BtreeEnterAll(db);
122960   rc = sqlite3Init(db, &zErrMsg);
122961   if( SQLITE_OK!=rc ){
122962     goto error_out;
122963   }
122964 
122965   /* Locate the table in question */
122966   pTab = sqlite3FindTable(db, zTableName, zDbName);
122967   if( !pTab || pTab->pSelect ){
122968     pTab = 0;
122969     goto error_out;
122970   }
122971 
122972   /* Find the column for which info is requested */
122973   if( sqlite3IsRowid(zColumnName) ){
122974     iCol = pTab->iPKey;
122975     if( iCol>=0 ){
122976       pCol = &pTab->aCol[iCol];
122977     }
122978   }else{
122979     for(iCol=0; iCol<pTab->nCol; iCol++){
122980       pCol = &pTab->aCol[iCol];
122981       if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
122982         break;
122983       }
122984     }
122985     if( iCol==pTab->nCol ){
122986       pTab = 0;
122987       goto error_out;
122988     }
122989   }
122990 
122991   /* The following block stores the meta information that will be returned
122992   ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
122993   ** and autoinc. At this point there are two possibilities:
122994   **
122995   **     1. The specified column name was rowid", "oid" or "_rowid_"
122996   **        and there is no explicitly declared IPK column.
122997   **
122998   **     2. The table is not a view and the column name identified an
122999   **        explicitly declared column. Copy meta information from *pCol.
123000   */
123001   if( pCol ){
123002     zDataType = pCol->zType;
123003     zCollSeq = pCol->zColl;
123004     notnull = pCol->notNull!=0;
123005     primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
123006     autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
123007   }else{
123008     zDataType = "INTEGER";
123009     primarykey = 1;
123010   }
123011   if( !zCollSeq ){
123012     zCollSeq = "BINARY";
123013   }
123014 
123015 error_out:
123016   sqlite3BtreeLeaveAll(db);
123017 
123018   /* Whether the function call succeeded or failed, set the output parameters
123019   ** to whatever their local counterparts contain. If an error did occur,
123020   ** this has the effect of zeroing all output parameters.
123021   */
123022   if( pzDataType ) *pzDataType = zDataType;
123023   if( pzCollSeq ) *pzCollSeq = zCollSeq;
123024   if( pNotNull ) *pNotNull = notnull;
123025   if( pPrimaryKey ) *pPrimaryKey = primarykey;
123026   if( pAutoinc ) *pAutoinc = autoinc;
123027 
123028   if( SQLITE_OK==rc && !pTab ){
123029     sqlite3DbFree(db, zErrMsg);
123030     zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
123031         zColumnName);
123032     rc = SQLITE_ERROR;
123033   }
123034   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
123035   sqlite3DbFree(db, zErrMsg);
123036   rc = sqlite3ApiExit(db, rc);
123037   sqlite3_mutex_leave(db->mutex);
123038   return rc;
123039 }
123040 #endif
123041 
123042 /*
123043 ** Sleep for a little while.  Return the amount of time slept.
123044 */
123045 SQLITE_API int sqlite3_sleep(int ms){
123046   sqlite3_vfs *pVfs;
123047   int rc;
123048   pVfs = sqlite3_vfs_find(0);
123049   if( pVfs==0 ) return 0;
123050 
123051   /* This function works in milliseconds, but the underlying OsSleep()
123052   ** API uses microseconds. Hence the 1000's.
123053   */
123054   rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
123055   return rc;
123056 }
123057 
123058 /*
123059 ** Enable or disable the extended result codes.
123060 */
123061 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
123062   sqlite3_mutex_enter(db->mutex);
123063   db->errMask = onoff ? 0xffffffff : 0xff;
123064   sqlite3_mutex_leave(db->mutex);
123065   return SQLITE_OK;
123066 }
123067 
123068 /*
123069 ** Invoke the xFileControl method on a particular database.
123070 */
123071 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
123072   int rc = SQLITE_ERROR;
123073   Btree *pBtree;
123074 
123075   sqlite3_mutex_enter(db->mutex);
123076   pBtree = sqlite3DbNameToBtree(db, zDbName);
123077   if( pBtree ){
123078     Pager *pPager;
123079     sqlite3_file *fd;
123080     sqlite3BtreeEnter(pBtree);
123081     pPager = sqlite3BtreePager(pBtree);
123082     assert( pPager!=0 );
123083     fd = sqlite3PagerFile(pPager);
123084     assert( fd!=0 );
123085     if( op==SQLITE_FCNTL_FILE_POINTER ){
123086       *(sqlite3_file**)pArg = fd;
123087       rc = SQLITE_OK;
123088     }else if( fd->pMethods ){
123089       rc = sqlite3OsFileControl(fd, op, pArg);
123090     }else{
123091       rc = SQLITE_NOTFOUND;
123092     }
123093     sqlite3BtreeLeave(pBtree);
123094   }
123095   sqlite3_mutex_leave(db->mutex);
123096   return rc;
123097 }
123098 
123099 /*
123100 ** Interface to the testing logic.
123101 */
123102 SQLITE_API int sqlite3_test_control(int op, ...){
123103   int rc = 0;
123104 #ifndef SQLITE_OMIT_BUILTIN_TEST
123105   va_list ap;
123106   va_start(ap, op);
123107   switch( op ){
123108 
123109     /*
123110     ** Save the current state of the PRNG.
123111     */
123112     case SQLITE_TESTCTRL_PRNG_SAVE: {
123113       sqlite3PrngSaveState();
123114       break;
123115     }
123116 
123117     /*
123118     ** Restore the state of the PRNG to the last state saved using
123119     ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
123120     ** this verb acts like PRNG_RESET.
123121     */
123122     case SQLITE_TESTCTRL_PRNG_RESTORE: {
123123       sqlite3PrngRestoreState();
123124       break;
123125     }
123126 
123127     /*
123128     ** Reset the PRNG back to its uninitialized state.  The next call
123129     ** to sqlite3_randomness() will reseed the PRNG using a single call
123130     ** to the xRandomness method of the default VFS.
123131     */
123132     case SQLITE_TESTCTRL_PRNG_RESET: {
123133       sqlite3_randomness(0,0);
123134       break;
123135     }
123136 
123137     /*
123138     **  sqlite3_test_control(BITVEC_TEST, size, program)
123139     **
123140     ** Run a test against a Bitvec object of size.  The program argument
123141     ** is an array of integers that defines the test.  Return -1 on a
123142     ** memory allocation error, 0 on success, or non-zero for an error.
123143     ** See the sqlite3BitvecBuiltinTest() for additional information.
123144     */
123145     case SQLITE_TESTCTRL_BITVEC_TEST: {
123146       int sz = va_arg(ap, int);
123147       int *aProg = va_arg(ap, int*);
123148       rc = sqlite3BitvecBuiltinTest(sz, aProg);
123149       break;
123150     }
123151 
123152     /*
123153     **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
123154     **
123155     ** Register hooks to call to indicate which malloc() failures
123156     ** are benign.
123157     */
123158     case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
123159       typedef void (*void_function)(void);
123160       void_function xBenignBegin;
123161       void_function xBenignEnd;
123162       xBenignBegin = va_arg(ap, void_function);
123163       xBenignEnd = va_arg(ap, void_function);
123164       sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
123165       break;
123166     }
123167 
123168     /*
123169     **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
123170     **
123171     ** Set the PENDING byte to the value in the argument, if X>0.
123172     ** Make no changes if X==0.  Return the value of the pending byte
123173     ** as it existing before this routine was called.
123174     **
123175     ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
123176     ** an incompatible database file format.  Changing the PENDING byte
123177     ** while any database connection is open results in undefined and
123178     ** dileterious behavior.
123179     */
123180     case SQLITE_TESTCTRL_PENDING_BYTE: {
123181       rc = PENDING_BYTE;
123182 #ifndef SQLITE_OMIT_WSD
123183       {
123184         unsigned int newVal = va_arg(ap, unsigned int);
123185         if( newVal ) sqlite3PendingByte = newVal;
123186       }
123187 #endif
123188       break;
123189     }
123190 
123191     /*
123192     **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
123193     **
123194     ** This action provides a run-time test to see whether or not
123195     ** assert() was enabled at compile-time.  If X is true and assert()
123196     ** is enabled, then the return value is true.  If X is true and
123197     ** assert() is disabled, then the return value is zero.  If X is
123198     ** false and assert() is enabled, then the assertion fires and the
123199     ** process aborts.  If X is false and assert() is disabled, then the
123200     ** return value is zero.
123201     */
123202     case SQLITE_TESTCTRL_ASSERT: {
123203       volatile int x = 0;
123204       assert( (x = va_arg(ap,int))!=0 );
123205       rc = x;
123206       break;
123207     }
123208 
123209 
123210     /*
123211     **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
123212     **
123213     ** This action provides a run-time test to see how the ALWAYS and
123214     ** NEVER macros were defined at compile-time.
123215     **
123216     ** The return value is ALWAYS(X).
123217     **
123218     ** The recommended test is X==2.  If the return value is 2, that means
123219     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
123220     ** default setting.  If the return value is 1, then ALWAYS() is either
123221     ** hard-coded to true or else it asserts if its argument is false.
123222     ** The first behavior (hard-coded to true) is the case if
123223     ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
123224     ** behavior (assert if the argument to ALWAYS() is false) is the case if
123225     ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
123226     **
123227     ** The run-time test procedure might look something like this:
123228     **
123229     **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
123230     **      // ALWAYS() and NEVER() are no-op pass-through macros
123231     **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
123232     **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
123233     **    }else{
123234     **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
123235     **    }
123236     */
123237     case SQLITE_TESTCTRL_ALWAYS: {
123238       int x = va_arg(ap,int);
123239       rc = ALWAYS(x);
123240       break;
123241     }
123242 
123243     /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
123244     **
123245     ** Set the nReserve size to N for the main database on the database
123246     ** connection db.
123247     */
123248     case SQLITE_TESTCTRL_RESERVE: {
123249       sqlite3 *db = va_arg(ap, sqlite3*);
123250       int x = va_arg(ap,int);
123251       sqlite3_mutex_enter(db->mutex);
123252       sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
123253       sqlite3_mutex_leave(db->mutex);
123254       break;
123255     }
123256 
123257     /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
123258     **
123259     ** Enable or disable various optimizations for testing purposes.  The
123260     ** argument N is a bitmask of optimizations to be disabled.  For normal
123261     ** operation N should be 0.  The idea is that a test program (like the
123262     ** SQL Logic Test or SLT test module) can run the same SQL multiple times
123263     ** with various optimizations disabled to verify that the same answer
123264     ** is obtained in every case.
123265     */
123266     case SQLITE_TESTCTRL_OPTIMIZATIONS: {
123267       sqlite3 *db = va_arg(ap, sqlite3*);
123268       db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
123269       break;
123270     }
123271 
123272 #ifdef SQLITE_N_KEYWORD
123273     /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
123274     **
123275     ** If zWord is a keyword recognized by the parser, then return the
123276     ** number of keywords.  Or if zWord is not a keyword, return 0.
123277     **
123278     ** This test feature is only available in the amalgamation since
123279     ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
123280     ** is built using separate source files.
123281     */
123282     case SQLITE_TESTCTRL_ISKEYWORD: {
123283       const char *zWord = va_arg(ap, const char*);
123284       int n = sqlite3Strlen30(zWord);
123285       rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
123286       break;
123287     }
123288 #endif
123289 
123290     /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
123291     **
123292     ** Pass pFree into sqlite3ScratchFree().
123293     ** If sz>0 then allocate a scratch buffer into pNew.
123294     */
123295     case SQLITE_TESTCTRL_SCRATCHMALLOC: {
123296       void *pFree, **ppNew;
123297       int sz;
123298       sz = va_arg(ap, int);
123299       ppNew = va_arg(ap, void**);
123300       pFree = va_arg(ap, void*);
123301       if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
123302       sqlite3ScratchFree(pFree);
123303       break;
123304     }
123305 
123306     /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
123307     **
123308     ** If parameter onoff is non-zero, configure the wrappers so that all
123309     ** subsequent calls to localtime() and variants fail. If onoff is zero,
123310     ** undo this setting.
123311     */
123312     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
123313       sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
123314       break;
123315     }
123316 
123317 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
123318     /*   sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
123319     **                        sqlite3_stmt*,const char**);
123320     **
123321     ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
123322     ** a string that describes the optimized parse tree.  This test-control
123323     ** returns a pointer to that string.
123324     */
123325     case SQLITE_TESTCTRL_EXPLAIN_STMT: {
123326       sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
123327       const char **pzRet = va_arg(ap, const char**);
123328       *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
123329       break;
123330     }
123331 #endif
123332 
123333     /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
123334     **
123335     ** Set or clear a flag that indicates that the database file is always well-
123336     ** formed and never corrupt.  This flag is clear by default, indicating that
123337     ** database files might have arbitrary corruption.  Setting the flag during
123338     ** testing causes certain assert() statements in the code to be activated
123339     ** that demonstrat invariants on well-formed database files.
123340     */
123341     case SQLITE_TESTCTRL_NEVER_CORRUPT: {
123342       sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
123343       break;
123344     }
123345 
123346 
123347     /*   sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE, xCallback, ptr);
123348     **
123349     ** Set the VDBE coverage callback function to xCallback with context
123350     ** pointer ptr.
123351     */
123352     case SQLITE_TESTCTRL_VDBE_COVERAGE: {
123353 #ifdef SQLITE_VDBE_COVERAGE
123354       typedef void (*branch_callback)(void*,int,u8,u8);
123355       sqlite3GlobalConfig.xVdbeBranch = va_arg(ap,branch_callback);
123356       sqlite3GlobalConfig.pVdbeBranchArg = va_arg(ap,void*);
123357 #endif
123358       break;
123359     }
123360 
123361   }
123362   va_end(ap);
123363 #endif /* SQLITE_OMIT_BUILTIN_TEST */
123364   return rc;
123365 }
123366 
123367 /*
123368 ** This is a utility routine, useful to VFS implementations, that checks
123369 ** to see if a database file was a URI that contained a specific query
123370 ** parameter, and if so obtains the value of the query parameter.
123371 **
123372 ** The zFilename argument is the filename pointer passed into the xOpen()
123373 ** method of a VFS implementation.  The zParam argument is the name of the
123374 ** query parameter we seek.  This routine returns the value of the zParam
123375 ** parameter if it exists.  If the parameter does not exist, this routine
123376 ** returns a NULL pointer.
123377 */
123378 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
123379   if( zFilename==0 ) return 0;
123380   zFilename += sqlite3Strlen30(zFilename) + 1;
123381   while( zFilename[0] ){
123382     int x = strcmp(zFilename, zParam);
123383     zFilename += sqlite3Strlen30(zFilename) + 1;
123384     if( x==0 ) return zFilename;
123385     zFilename += sqlite3Strlen30(zFilename) + 1;
123386   }
123387   return 0;
123388 }
123389 
123390 /*
123391 ** Return a boolean value for a query parameter.
123392 */
123393 SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
123394   const char *z = sqlite3_uri_parameter(zFilename, zParam);
123395   bDflt = bDflt!=0;
123396   return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
123397 }
123398 
123399 /*
123400 ** Return a 64-bit integer value for a query parameter.
123401 */
123402 SQLITE_API sqlite3_int64 sqlite3_uri_int64(
123403   const char *zFilename,    /* Filename as passed to xOpen */
123404   const char *zParam,       /* URI parameter sought */
123405   sqlite3_int64 bDflt       /* return if parameter is missing */
123406 ){
123407   const char *z = sqlite3_uri_parameter(zFilename, zParam);
123408   sqlite3_int64 v;
123409   if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
123410     bDflt = v;
123411   }
123412   return bDflt;
123413 }
123414 
123415 /*
123416 ** Return the Btree pointer identified by zDbName.  Return NULL if not found.
123417 */
123418 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
123419   int i;
123420   for(i=0; i<db->nDb; i++){
123421     if( db->aDb[i].pBt
123422      && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
123423     ){
123424       return db->aDb[i].pBt;
123425     }
123426   }
123427   return 0;
123428 }
123429 
123430 /*
123431 ** Return the filename of the database associated with a database
123432 ** connection.
123433 */
123434 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
123435   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
123436   return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
123437 }
123438 
123439 /*
123440 ** Return 1 if database is read-only or 0 if read/write.  Return -1 if
123441 ** no such database exists.
123442 */
123443 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
123444   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
123445   return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
123446 }
123447 
123448 /************** End of main.c ************************************************/
123449 /************** Begin file notify.c ******************************************/
123450 /*
123451 ** 2009 March 3
123452 **
123453 ** The author disclaims copyright to this source code.  In place of
123454 ** a legal notice, here is a blessing:
123455 **
123456 **    May you do good and not evil.
123457 **    May you find forgiveness for yourself and forgive others.
123458 **    May you share freely, never taking more than you give.
123459 **
123460 *************************************************************************
123461 **
123462 ** This file contains the implementation of the sqlite3_unlock_notify()
123463 ** API method and its associated functionality.
123464 */
123465 
123466 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
123467 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
123468 
123469 /*
123470 ** Public interfaces:
123471 **
123472 **   sqlite3ConnectionBlocked()
123473 **   sqlite3ConnectionUnlocked()
123474 **   sqlite3ConnectionClosed()
123475 **   sqlite3_unlock_notify()
123476 */
123477 
123478 #define assertMutexHeld() \
123479   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
123480 
123481 /*
123482 ** Head of a linked list of all sqlite3 objects created by this process
123483 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
123484 ** is not NULL. This variable may only accessed while the STATIC_MASTER
123485 ** mutex is held.
123486 */
123487 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
123488 
123489 #ifndef NDEBUG
123490 /*
123491 ** This function is a complex assert() that verifies the following
123492 ** properties of the blocked connections list:
123493 **
123494 **   1) Each entry in the list has a non-NULL value for either
123495 **      pUnlockConnection or pBlockingConnection, or both.
123496 **
123497 **   2) All entries in the list that share a common value for
123498 **      xUnlockNotify are grouped together.
123499 **
123500 **   3) If the argument db is not NULL, then none of the entries in the
123501 **      blocked connections list have pUnlockConnection or pBlockingConnection
123502 **      set to db. This is used when closing connection db.
123503 */
123504 static void checkListProperties(sqlite3 *db){
123505   sqlite3 *p;
123506   for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
123507     int seen = 0;
123508     sqlite3 *p2;
123509 
123510     /* Verify property (1) */
123511     assert( p->pUnlockConnection || p->pBlockingConnection );
123512 
123513     /* Verify property (2) */
123514     for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
123515       if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
123516       assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
123517       assert( db==0 || p->pUnlockConnection!=db );
123518       assert( db==0 || p->pBlockingConnection!=db );
123519     }
123520   }
123521 }
123522 #else
123523 # define checkListProperties(x)
123524 #endif
123525 
123526 /*
123527 ** Remove connection db from the blocked connections list. If connection
123528 ** db is not currently a part of the list, this function is a no-op.
123529 */
123530 static void removeFromBlockedList(sqlite3 *db){
123531   sqlite3 **pp;
123532   assertMutexHeld();
123533   for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
123534     if( *pp==db ){
123535       *pp = (*pp)->pNextBlocked;
123536       break;
123537     }
123538   }
123539 }
123540 
123541 /*
123542 ** Add connection db to the blocked connections list. It is assumed
123543 ** that it is not already a part of the list.
123544 */
123545 static void addToBlockedList(sqlite3 *db){
123546   sqlite3 **pp;
123547   assertMutexHeld();
123548   for(
123549     pp=&sqlite3BlockedList;
123550     *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
123551     pp=&(*pp)->pNextBlocked
123552   );
123553   db->pNextBlocked = *pp;
123554   *pp = db;
123555 }
123556 
123557 /*
123558 ** Obtain the STATIC_MASTER mutex.
123559 */
123560 static void enterMutex(void){
123561   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
123562   checkListProperties(0);
123563 }
123564 
123565 /*
123566 ** Release the STATIC_MASTER mutex.
123567 */
123568 static void leaveMutex(void){
123569   assertMutexHeld();
123570   checkListProperties(0);
123571   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
123572 }
123573 
123574 /*
123575 ** Register an unlock-notify callback.
123576 **
123577 ** This is called after connection "db" has attempted some operation
123578 ** but has received an SQLITE_LOCKED error because another connection
123579 ** (call it pOther) in the same process was busy using the same shared
123580 ** cache.  pOther is found by looking at db->pBlockingConnection.
123581 **
123582 ** If there is no blocking connection, the callback is invoked immediately,
123583 ** before this routine returns.
123584 **
123585 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
123586 ** a deadlock.
123587 **
123588 ** Otherwise, make arrangements to invoke xNotify when pOther drops
123589 ** its locks.
123590 **
123591 ** Each call to this routine overrides any prior callbacks registered
123592 ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
123593 ** cancelled.
123594 */
123595 SQLITE_API int sqlite3_unlock_notify(
123596   sqlite3 *db,
123597   void (*xNotify)(void **, int),
123598   void *pArg
123599 ){
123600   int rc = SQLITE_OK;
123601 
123602   sqlite3_mutex_enter(db->mutex);
123603   enterMutex();
123604 
123605   if( xNotify==0 ){
123606     removeFromBlockedList(db);
123607     db->pBlockingConnection = 0;
123608     db->pUnlockConnection = 0;
123609     db->xUnlockNotify = 0;
123610     db->pUnlockArg = 0;
123611   }else if( 0==db->pBlockingConnection ){
123612     /* The blocking transaction has been concluded. Or there never was a
123613     ** blocking transaction. In either case, invoke the notify callback
123614     ** immediately.
123615     */
123616     xNotify(&pArg, 1);
123617   }else{
123618     sqlite3 *p;
123619 
123620     for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
123621     if( p ){
123622       rc = SQLITE_LOCKED;              /* Deadlock detected. */
123623     }else{
123624       db->pUnlockConnection = db->pBlockingConnection;
123625       db->xUnlockNotify = xNotify;
123626       db->pUnlockArg = pArg;
123627       removeFromBlockedList(db);
123628       addToBlockedList(db);
123629     }
123630   }
123631 
123632   leaveMutex();
123633   assert( !db->mallocFailed );
123634   sqlite3Error(db, rc, (rc?"database is deadlocked":0));
123635   sqlite3_mutex_leave(db->mutex);
123636   return rc;
123637 }
123638 
123639 /*
123640 ** This function is called while stepping or preparing a statement
123641 ** associated with connection db. The operation will return SQLITE_LOCKED
123642 ** to the user because it requires a lock that will not be available
123643 ** until connection pBlocker concludes its current transaction.
123644 */
123645 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
123646   enterMutex();
123647   if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
123648     addToBlockedList(db);
123649   }
123650   db->pBlockingConnection = pBlocker;
123651   leaveMutex();
123652 }
123653 
123654 /*
123655 ** This function is called when
123656 ** the transaction opened by database db has just finished. Locks held
123657 ** by database connection db have been released.
123658 **
123659 ** This function loops through each entry in the blocked connections
123660 ** list and does the following:
123661 **
123662 **   1) If the sqlite3.pBlockingConnection member of a list entry is
123663 **      set to db, then set pBlockingConnection=0.
123664 **
123665 **   2) If the sqlite3.pUnlockConnection member of a list entry is
123666 **      set to db, then invoke the configured unlock-notify callback and
123667 **      set pUnlockConnection=0.
123668 **
123669 **   3) If the two steps above mean that pBlockingConnection==0 and
123670 **      pUnlockConnection==0, remove the entry from the blocked connections
123671 **      list.
123672 */
123673 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
123674   void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
123675   int nArg = 0;                            /* Number of entries in aArg[] */
123676   sqlite3 **pp;                            /* Iterator variable */
123677   void **aArg;               /* Arguments to the unlock callback */
123678   void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
123679   void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
123680 
123681   aArg = aStatic;
123682   enterMutex();         /* Enter STATIC_MASTER mutex */
123683 
123684   /* This loop runs once for each entry in the blocked-connections list. */
123685   for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
123686     sqlite3 *p = *pp;
123687 
123688     /* Step 1. */
123689     if( p->pBlockingConnection==db ){
123690       p->pBlockingConnection = 0;
123691     }
123692 
123693     /* Step 2. */
123694     if( p->pUnlockConnection==db ){
123695       assert( p->xUnlockNotify );
123696       if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
123697         xUnlockNotify(aArg, nArg);
123698         nArg = 0;
123699       }
123700 
123701       sqlite3BeginBenignMalloc();
123702       assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
123703       assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
123704       if( (!aDyn && nArg==(int)ArraySize(aStatic))
123705        || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
123706       ){
123707         /* The aArg[] array needs to grow. */
123708         void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
123709         if( pNew ){
123710           memcpy(pNew, aArg, nArg*sizeof(void *));
123711           sqlite3_free(aDyn);
123712           aDyn = aArg = pNew;
123713         }else{
123714           /* This occurs when the array of context pointers that need to
123715           ** be passed to the unlock-notify callback is larger than the
123716           ** aStatic[] array allocated on the stack and the attempt to
123717           ** allocate a larger array from the heap has failed.
123718           **
123719           ** This is a difficult situation to handle. Returning an error
123720           ** code to the caller is insufficient, as even if an error code
123721           ** is returned the transaction on connection db will still be
123722           ** closed and the unlock-notify callbacks on blocked connections
123723           ** will go unissued. This might cause the application to wait
123724           ** indefinitely for an unlock-notify callback that will never
123725           ** arrive.
123726           **
123727           ** Instead, invoke the unlock-notify callback with the context
123728           ** array already accumulated. We can then clear the array and
123729           ** begin accumulating any further context pointers without
123730           ** requiring any dynamic allocation. This is sub-optimal because
123731           ** it means that instead of one callback with a large array of
123732           ** context pointers the application will receive two or more
123733           ** callbacks with smaller arrays of context pointers, which will
123734           ** reduce the applications ability to prioritize multiple
123735           ** connections. But it is the best that can be done under the
123736           ** circumstances.
123737           */
123738           xUnlockNotify(aArg, nArg);
123739           nArg = 0;
123740         }
123741       }
123742       sqlite3EndBenignMalloc();
123743 
123744       aArg[nArg++] = p->pUnlockArg;
123745       xUnlockNotify = p->xUnlockNotify;
123746       p->pUnlockConnection = 0;
123747       p->xUnlockNotify = 0;
123748       p->pUnlockArg = 0;
123749     }
123750 
123751     /* Step 3. */
123752     if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
123753       /* Remove connection p from the blocked connections list. */
123754       *pp = p->pNextBlocked;
123755       p->pNextBlocked = 0;
123756     }else{
123757       pp = &p->pNextBlocked;
123758     }
123759   }
123760 
123761   if( nArg!=0 ){
123762     xUnlockNotify(aArg, nArg);
123763   }
123764   sqlite3_free(aDyn);
123765   leaveMutex();         /* Leave STATIC_MASTER mutex */
123766 }
123767 
123768 /*
123769 ** This is called when the database connection passed as an argument is
123770 ** being closed. The connection is removed from the blocked list.
123771 */
123772 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
123773   sqlite3ConnectionUnlocked(db);
123774   enterMutex();
123775   removeFromBlockedList(db);
123776   checkListProperties(db);
123777   leaveMutex();
123778 }
123779 #endif
123780 
123781 /************** End of notify.c **********************************************/
123782 /************** Begin file fts3.c ********************************************/
123783 /*
123784 ** 2006 Oct 10
123785 **
123786 ** The author disclaims copyright to this source code.  In place of
123787 ** a legal notice, here is a blessing:
123788 **
123789 **    May you do good and not evil.
123790 **    May you find forgiveness for yourself and forgive others.
123791 **    May you share freely, never taking more than you give.
123792 **
123793 ******************************************************************************
123794 **
123795 ** This is an SQLite module implementing full-text search.
123796 */
123797 
123798 /*
123799 ** The code in this file is only compiled if:
123800 **
123801 **     * The FTS3 module is being built as an extension
123802 **       (in which case SQLITE_CORE is not defined), or
123803 **
123804 **     * The FTS3 module is being built into the core of
123805 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
123806 */
123807 
123808 /* The full-text index is stored in a series of b+tree (-like)
123809 ** structures called segments which map terms to doclists.  The
123810 ** structures are like b+trees in layout, but are constructed from the
123811 ** bottom up in optimal fashion and are not updatable.  Since trees
123812 ** are built from the bottom up, things will be described from the
123813 ** bottom up.
123814 **
123815 **
123816 **** Varints ****
123817 ** The basic unit of encoding is a variable-length integer called a
123818 ** varint.  We encode variable-length integers in little-endian order
123819 ** using seven bits * per byte as follows:
123820 **
123821 ** KEY:
123822 **         A = 0xxxxxxx    7 bits of data and one flag bit
123823 **         B = 1xxxxxxx    7 bits of data and one flag bit
123824 **
123825 **  7 bits - A
123826 ** 14 bits - BA
123827 ** 21 bits - BBA
123828 ** and so on.
123829 **
123830 ** This is similar in concept to how sqlite encodes "varints" but
123831 ** the encoding is not the same.  SQLite varints are big-endian
123832 ** are are limited to 9 bytes in length whereas FTS3 varints are
123833 ** little-endian and can be up to 10 bytes in length (in theory).
123834 **
123835 ** Example encodings:
123836 **
123837 **     1:    0x01
123838 **   127:    0x7f
123839 **   128:    0x81 0x00
123840 **
123841 **
123842 **** Document lists ****
123843 ** A doclist (document list) holds a docid-sorted list of hits for a
123844 ** given term.  Doclists hold docids and associated token positions.
123845 ** A docid is the unique integer identifier for a single document.
123846 ** A position is the index of a word within the document.  The first
123847 ** word of the document has a position of 0.
123848 **
123849 ** FTS3 used to optionally store character offsets using a compile-time
123850 ** option.  But that functionality is no longer supported.
123851 **
123852 ** A doclist is stored like this:
123853 **
123854 ** array {
123855 **   varint docid;          (delta from previous doclist)
123856 **   array {                (position list for column 0)
123857 **     varint position;     (2 more than the delta from previous position)
123858 **   }
123859 **   array {
123860 **     varint POS_COLUMN;   (marks start of position list for new column)
123861 **     varint column;       (index of new column)
123862 **     array {
123863 **       varint position;   (2 more than the delta from previous position)
123864 **     }
123865 **   }
123866 **   varint POS_END;        (marks end of positions for this document.
123867 ** }
123868 **
123869 ** Here, array { X } means zero or more occurrences of X, adjacent in
123870 ** memory.  A "position" is an index of a token in the token stream
123871 ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
123872 ** in the same logical place as the position element, and act as sentinals
123873 ** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
123874 ** The positions numbers are not stored literally but rather as two more
123875 ** than the difference from the prior position, or the just the position plus
123876 ** 2 for the first position.  Example:
123877 **
123878 **   label:       A B C D E  F  G H   I  J K
123879 **   value:     123 5 9 1 1 14 35 0 234 72 0
123880 **
123881 ** The 123 value is the first docid.  For column zero in this document
123882 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
123883 ** at D signals the start of a new column; the 1 at E indicates that the
123884 ** new column is column number 1.  There are two positions at 12 and 45
123885 ** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
123886 ** 234 at I is the delta to next docid (357).  It has one position 70
123887 ** (72-2) and then terminates with the 0 at K.
123888 **
123889 ** A "position-list" is the list of positions for multiple columns for
123890 ** a single docid.  A "column-list" is the set of positions for a single
123891 ** column.  Hence, a position-list consists of one or more column-lists,
123892 ** a document record consists of a docid followed by a position-list and
123893 ** a doclist consists of one or more document records.
123894 **
123895 ** A bare doclist omits the position information, becoming an
123896 ** array of varint-encoded docids.
123897 **
123898 **** Segment leaf nodes ****
123899 ** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
123900 ** nodes are written using LeafWriter, and read using LeafReader (to
123901 ** iterate through a single leaf node's data) and LeavesReader (to
123902 ** iterate through a segment's entire leaf layer).  Leaf nodes have
123903 ** the format:
123904 **
123905 ** varint iHeight;             (height from leaf level, always 0)
123906 ** varint nTerm;               (length of first term)
123907 ** char pTerm[nTerm];          (content of first term)
123908 ** varint nDoclist;            (length of term's associated doclist)
123909 ** char pDoclist[nDoclist];    (content of doclist)
123910 ** array {
123911 **                             (further terms are delta-encoded)
123912 **   varint nPrefix;           (length of prefix shared with previous term)
123913 **   varint nSuffix;           (length of unshared suffix)
123914 **   char pTermSuffix[nSuffix];(unshared suffix of next term)
123915 **   varint nDoclist;          (length of term's associated doclist)
123916 **   char pDoclist[nDoclist];  (content of doclist)
123917 ** }
123918 **
123919 ** Here, array { X } means zero or more occurrences of X, adjacent in
123920 ** memory.
123921 **
123922 ** Leaf nodes are broken into blocks which are stored contiguously in
123923 ** the %_segments table in sorted order.  This means that when the end
123924 ** of a node is reached, the next term is in the node with the next
123925 ** greater node id.
123926 **
123927 ** New data is spilled to a new leaf node when the current node
123928 ** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
123929 ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
123930 ** node (a leaf node with a single term and doclist).  The goal of
123931 ** these settings is to pack together groups of small doclists while
123932 ** making it efficient to directly access large doclists.  The
123933 ** assumption is that large doclists represent terms which are more
123934 ** likely to be query targets.
123935 **
123936 ** TODO(shess) It may be useful for blocking decisions to be more
123937 ** dynamic.  For instance, it may make more sense to have a 2.5k leaf
123938 ** node rather than splitting into 2k and .5k nodes.  My intuition is
123939 ** that this might extend through 2x or 4x the pagesize.
123940 **
123941 **
123942 **** Segment interior nodes ****
123943 ** Segment interior nodes store blockids for subtree nodes and terms
123944 ** to describe what data is stored by the each subtree.  Interior
123945 ** nodes are written using InteriorWriter, and read using
123946 ** InteriorReader.  InteriorWriters are created as needed when
123947 ** SegmentWriter creates new leaf nodes, or when an interior node
123948 ** itself grows too big and must be split.  The format of interior
123949 ** nodes:
123950 **
123951 ** varint iHeight;           (height from leaf level, always >0)
123952 ** varint iBlockid;          (block id of node's leftmost subtree)
123953 ** optional {
123954 **   varint nTerm;           (length of first term)
123955 **   char pTerm[nTerm];      (content of first term)
123956 **   array {
123957 **                                (further terms are delta-encoded)
123958 **     varint nPrefix;            (length of shared prefix with previous term)
123959 **     varint nSuffix;            (length of unshared suffix)
123960 **     char pTermSuffix[nSuffix]; (unshared suffix of next term)
123961 **   }
123962 ** }
123963 **
123964 ** Here, optional { X } means an optional element, while array { X }
123965 ** means zero or more occurrences of X, adjacent in memory.
123966 **
123967 ** An interior node encodes n terms separating n+1 subtrees.  The
123968 ** subtree blocks are contiguous, so only the first subtree's blockid
123969 ** is encoded.  The subtree at iBlockid will contain all terms less
123970 ** than the first term encoded (or all terms if no term is encoded).
123971 ** Otherwise, for terms greater than or equal to pTerm[i] but less
123972 ** than pTerm[i+1], the subtree for that term will be rooted at
123973 ** iBlockid+i.  Interior nodes only store enough term data to
123974 ** distinguish adjacent children (if the rightmost term of the left
123975 ** child is "something", and the leftmost term of the right child is
123976 ** "wicked", only "w" is stored).
123977 **
123978 ** New data is spilled to a new interior node at the same height when
123979 ** the current node exceeds INTERIOR_MAX bytes (default 2048).
123980 ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
123981 ** interior nodes and making the tree too skinny.  The interior nodes
123982 ** at a given height are naturally tracked by interior nodes at
123983 ** height+1, and so on.
123984 **
123985 **
123986 **** Segment directory ****
123987 ** The segment directory in table %_segdir stores meta-information for
123988 ** merging and deleting segments, and also the root node of the
123989 ** segment's tree.
123990 **
123991 ** The root node is the top node of the segment's tree after encoding
123992 ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
123993 ** This could be either a leaf node or an interior node.  If the top
123994 ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
123995 ** and a new root interior node is generated (which should always fit
123996 ** within ROOT_MAX because it only needs space for 2 varints, the
123997 ** height and the blockid of the previous root).
123998 **
123999 ** The meta-information in the segment directory is:
124000 **   level               - segment level (see below)
124001 **   idx                 - index within level
124002 **                       - (level,idx uniquely identify a segment)
124003 **   start_block         - first leaf node
124004 **   leaves_end_block    - last leaf node
124005 **   end_block           - last block (including interior nodes)
124006 **   root                - contents of root node
124007 **
124008 ** If the root node is a leaf node, then start_block,
124009 ** leaves_end_block, and end_block are all 0.
124010 **
124011 **
124012 **** Segment merging ****
124013 ** To amortize update costs, segments are grouped into levels and
124014 ** merged in batches.  Each increase in level represents exponentially
124015 ** more documents.
124016 **
124017 ** New documents (actually, document updates) are tokenized and
124018 ** written individually (using LeafWriter) to a level 0 segment, with
124019 ** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
124020 ** level 0 segments are merged into a single level 1 segment.  Level 1
124021 ** is populated like level 0, and eventually MERGE_COUNT level 1
124022 ** segments are merged to a single level 2 segment (representing
124023 ** MERGE_COUNT^2 updates), and so on.
124024 **
124025 ** A segment merge traverses all segments at a given level in
124026 ** parallel, performing a straightforward sorted merge.  Since segment
124027 ** leaf nodes are written in to the %_segments table in order, this
124028 ** merge traverses the underlying sqlite disk structures efficiently.
124029 ** After the merge, all segment blocks from the merged level are
124030 ** deleted.
124031 **
124032 ** MERGE_COUNT controls how often we merge segments.  16 seems to be
124033 ** somewhat of a sweet spot for insertion performance.  32 and 64 show
124034 ** very similar performance numbers to 16 on insertion, though they're
124035 ** a tiny bit slower (perhaps due to more overhead in merge-time
124036 ** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
124037 ** 16, 2 about 66% slower than 16.
124038 **
124039 ** At query time, high MERGE_COUNT increases the number of segments
124040 ** which need to be scanned and merged.  For instance, with 100k docs
124041 ** inserted:
124042 **
124043 **    MERGE_COUNT   segments
124044 **       16           25
124045 **        8           12
124046 **        4           10
124047 **        2            6
124048 **
124049 ** This appears to have only a moderate impact on queries for very
124050 ** frequent terms (which are somewhat dominated by segment merge
124051 ** costs), and infrequent and non-existent terms still seem to be fast
124052 ** even with many segments.
124053 **
124054 ** TODO(shess) That said, it would be nice to have a better query-side
124055 ** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
124056 ** optimizations to things like doclist merging will swing the sweet
124057 ** spot around.
124058 **
124059 **
124060 **
124061 **** Handling of deletions and updates ****
124062 ** Since we're using a segmented structure, with no docid-oriented
124063 ** index into the term index, we clearly cannot simply update the term
124064 ** index when a document is deleted or updated.  For deletions, we
124065 ** write an empty doclist (varint(docid) varint(POS_END)), for updates
124066 ** we simply write the new doclist.  Segment merges overwrite older
124067 ** data for a particular docid with newer data, so deletes or updates
124068 ** will eventually overtake the earlier data and knock it out.  The
124069 ** query logic likewise merges doclists so that newer data knocks out
124070 ** older data.
124071 */
124072 
124073 /************** Include fts3Int.h in the middle of fts3.c ********************/
124074 /************** Begin file fts3Int.h *****************************************/
124075 /*
124076 ** 2009 Nov 12
124077 **
124078 ** The author disclaims copyright to this source code.  In place of
124079 ** a legal notice, here is a blessing:
124080 **
124081 **    May you do good and not evil.
124082 **    May you find forgiveness for yourself and forgive others.
124083 **    May you share freely, never taking more than you give.
124084 **
124085 ******************************************************************************
124086 **
124087 */
124088 #ifndef _FTSINT_H
124089 #define _FTSINT_H
124090 
124091 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
124092 # define NDEBUG 1
124093 #endif
124094 
124095 /*
124096 ** FTS4 is really an extension for FTS3.  It is enabled using the
124097 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
124098 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
124099 */
124100 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
124101 # define SQLITE_ENABLE_FTS3
124102 #endif
124103 
124104 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
124105 
124106 /* If not building as part of the core, include sqlite3ext.h. */
124107 #ifndef SQLITE_CORE
124108 SQLITE_EXTENSION_INIT3
124109 #endif
124110 
124111 /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
124112 /************** Begin file fts3_tokenizer.h **********************************/
124113 /*
124114 ** 2006 July 10
124115 **
124116 ** The author disclaims copyright to this source code.
124117 **
124118 *************************************************************************
124119 ** Defines the interface to tokenizers used by fulltext-search.  There
124120 ** are three basic components:
124121 **
124122 ** sqlite3_tokenizer_module is a singleton defining the tokenizer
124123 ** interface functions.  This is essentially the class structure for
124124 ** tokenizers.
124125 **
124126 ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
124127 ** including customization information defined at creation time.
124128 **
124129 ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
124130 ** tokens from a particular input.
124131 */
124132 #ifndef _FTS3_TOKENIZER_H_
124133 #define _FTS3_TOKENIZER_H_
124134 
124135 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
124136 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
124137 ** we will need a way to register the API consistently.
124138 */
124139 
124140 /*
124141 ** Structures used by the tokenizer interface. When a new tokenizer
124142 ** implementation is registered, the caller provides a pointer to
124143 ** an sqlite3_tokenizer_module containing pointers to the callback
124144 ** functions that make up an implementation.
124145 **
124146 ** When an fts3 table is created, it passes any arguments passed to
124147 ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
124148 ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
124149 ** implementation. The xCreate() function in turn returns an
124150 ** sqlite3_tokenizer structure representing the specific tokenizer to
124151 ** be used for the fts3 table (customized by the tokenizer clause arguments).
124152 **
124153 ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
124154 ** method is called. It returns an sqlite3_tokenizer_cursor object
124155 ** that may be used to tokenize a specific input buffer based on
124156 ** the tokenization rules supplied by a specific sqlite3_tokenizer
124157 ** object.
124158 */
124159 typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
124160 typedef struct sqlite3_tokenizer sqlite3_tokenizer;
124161 typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
124162 
124163 struct sqlite3_tokenizer_module {
124164 
124165   /*
124166   ** Structure version. Should always be set to 0 or 1.
124167   */
124168   int iVersion;
124169 
124170   /*
124171   ** Create a new tokenizer. The values in the argv[] array are the
124172   ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
124173   ** TABLE statement that created the fts3 table. For example, if
124174   ** the following SQL is executed:
124175   **
124176   **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
124177   **
124178   ** then argc is set to 2, and the argv[] array contains pointers
124179   ** to the strings "arg1" and "arg2".
124180   **
124181   ** This method should return either SQLITE_OK (0), or an SQLite error
124182   ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
124183   ** to point at the newly created tokenizer structure. The generic
124184   ** sqlite3_tokenizer.pModule variable should not be initialized by
124185   ** this callback. The caller will do so.
124186   */
124187   int (*xCreate)(
124188     int argc,                           /* Size of argv array */
124189     const char *const*argv,             /* Tokenizer argument strings */
124190     sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
124191   );
124192 
124193   /*
124194   ** Destroy an existing tokenizer. The fts3 module calls this method
124195   ** exactly once for each successful call to xCreate().
124196   */
124197   int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
124198 
124199   /*
124200   ** Create a tokenizer cursor to tokenize an input buffer. The caller
124201   ** is responsible for ensuring that the input buffer remains valid
124202   ** until the cursor is closed (using the xClose() method).
124203   */
124204   int (*xOpen)(
124205     sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
124206     const char *pInput, int nBytes,      /* Input buffer */
124207     sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
124208   );
124209 
124210   /*
124211   ** Destroy an existing tokenizer cursor. The fts3 module calls this
124212   ** method exactly once for each successful call to xOpen().
124213   */
124214   int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
124215 
124216   /*
124217   ** Retrieve the next token from the tokenizer cursor pCursor. This
124218   ** method should either return SQLITE_OK and set the values of the
124219   ** "OUT" variables identified below, or SQLITE_DONE to indicate that
124220   ** the end of the buffer has been reached, or an SQLite error code.
124221   **
124222   ** *ppToken should be set to point at a buffer containing the
124223   ** normalized version of the token (i.e. after any case-folding and/or
124224   ** stemming has been performed). *pnBytes should be set to the length
124225   ** of this buffer in bytes. The input text that generated the token is
124226   ** identified by the byte offsets returned in *piStartOffset and
124227   ** *piEndOffset. *piStartOffset should be set to the index of the first
124228   ** byte of the token in the input buffer. *piEndOffset should be set
124229   ** to the index of the first byte just past the end of the token in
124230   ** the input buffer.
124231   **
124232   ** The buffer *ppToken is set to point at is managed by the tokenizer
124233   ** implementation. It is only required to be valid until the next call
124234   ** to xNext() or xClose().
124235   */
124236   /* TODO(shess) current implementation requires pInput to be
124237   ** nul-terminated.  This should either be fixed, or pInput/nBytes
124238   ** should be converted to zInput.
124239   */
124240   int (*xNext)(
124241     sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
124242     const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
124243     int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
124244     int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
124245     int *piPosition      /* OUT: Number of tokens returned before this one */
124246   );
124247 
124248   /***********************************************************************
124249   ** Methods below this point are only available if iVersion>=1.
124250   */
124251 
124252   /*
124253   ** Configure the language id of a tokenizer cursor.
124254   */
124255   int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
124256 };
124257 
124258 struct sqlite3_tokenizer {
124259   const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
124260   /* Tokenizer implementations will typically add additional fields */
124261 };
124262 
124263 struct sqlite3_tokenizer_cursor {
124264   sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
124265   /* Tokenizer implementations will typically add additional fields */
124266 };
124267 
124268 int fts3_global_term_cnt(int iTerm, int iCol);
124269 int fts3_term_cnt(int iTerm, int iCol);
124270 
124271 
124272 #endif /* _FTS3_TOKENIZER_H_ */
124273 
124274 /************** End of fts3_tokenizer.h **************************************/
124275 /************** Continuing where we left off in fts3Int.h ********************/
124276 /************** Include fts3_hash.h in the middle of fts3Int.h ***************/
124277 /************** Begin file fts3_hash.h ***************************************/
124278 /*
124279 ** 2001 September 22
124280 **
124281 ** The author disclaims copyright to this source code.  In place of
124282 ** a legal notice, here is a blessing:
124283 **
124284 **    May you do good and not evil.
124285 **    May you find forgiveness for yourself and forgive others.
124286 **    May you share freely, never taking more than you give.
124287 **
124288 *************************************************************************
124289 ** This is the header file for the generic hash-table implementation
124290 ** used in SQLite.  We've modified it slightly to serve as a standalone
124291 ** hash table implementation for the full-text indexing module.
124292 **
124293 */
124294 #ifndef _FTS3_HASH_H_
124295 #define _FTS3_HASH_H_
124296 
124297 /* Forward declarations of structures. */
124298 typedef struct Fts3Hash Fts3Hash;
124299 typedef struct Fts3HashElem Fts3HashElem;
124300 
124301 /* A complete hash table is an instance of the following structure.
124302 ** The internals of this structure are intended to be opaque -- client
124303 ** code should not attempt to access or modify the fields of this structure
124304 ** directly.  Change this structure only by using the routines below.
124305 ** However, many of the "procedures" and "functions" for modifying and
124306 ** accessing this structure are really macros, so we can't really make
124307 ** this structure opaque.
124308 */
124309 struct Fts3Hash {
124310   char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
124311   char copyKey;           /* True if copy of key made on insert */
124312   int count;              /* Number of entries in this table */
124313   Fts3HashElem *first;    /* The first element of the array */
124314   int htsize;             /* Number of buckets in the hash table */
124315   struct _fts3ht {        /* the hash table */
124316     int count;               /* Number of entries with this hash */
124317     Fts3HashElem *chain;     /* Pointer to first entry with this hash */
124318   } *ht;
124319 };
124320 
124321 /* Each element in the hash table is an instance of the following
124322 ** structure.  All elements are stored on a single doubly-linked list.
124323 **
124324 ** Again, this structure is intended to be opaque, but it can't really
124325 ** be opaque because it is used by macros.
124326 */
124327 struct Fts3HashElem {
124328   Fts3HashElem *next, *prev; /* Next and previous elements in the table */
124329   void *data;                /* Data associated with this element */
124330   void *pKey; int nKey;      /* Key associated with this element */
124331 };
124332 
124333 /*
124334 ** There are 2 different modes of operation for a hash table:
124335 **
124336 **   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
124337 **                           (including the null-terminator, if any).  Case
124338 **                           is respected in comparisons.
124339 **
124340 **   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long.
124341 **                           memcmp() is used to compare keys.
124342 **
124343 ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
124344 */
124345 #define FTS3_HASH_STRING    1
124346 #define FTS3_HASH_BINARY    2
124347 
124348 /*
124349 ** Access routines.  To delete, insert a NULL pointer.
124350 */
124351 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
124352 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
124353 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
124354 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
124355 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
124356 
124357 /*
124358 ** Shorthand for the functions above
124359 */
124360 #define fts3HashInit     sqlite3Fts3HashInit
124361 #define fts3HashInsert   sqlite3Fts3HashInsert
124362 #define fts3HashFind     sqlite3Fts3HashFind
124363 #define fts3HashClear    sqlite3Fts3HashClear
124364 #define fts3HashFindElem sqlite3Fts3HashFindElem
124365 
124366 /*
124367 ** Macros for looping over all elements of a hash table.  The idiom is
124368 ** like this:
124369 **
124370 **   Fts3Hash h;
124371 **   Fts3HashElem *p;
124372 **   ...
124373 **   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
124374 **     SomeStructure *pData = fts3HashData(p);
124375 **     // do something with pData
124376 **   }
124377 */
124378 #define fts3HashFirst(H)  ((H)->first)
124379 #define fts3HashNext(E)   ((E)->next)
124380 #define fts3HashData(E)   ((E)->data)
124381 #define fts3HashKey(E)    ((E)->pKey)
124382 #define fts3HashKeysize(E) ((E)->nKey)
124383 
124384 /*
124385 ** Number of entries in a hash table
124386 */
124387 #define fts3HashCount(H)  ((H)->count)
124388 
124389 #endif /* _FTS3_HASH_H_ */
124390 
124391 /************** End of fts3_hash.h *******************************************/
124392 /************** Continuing where we left off in fts3Int.h ********************/
124393 
124394 /*
124395 ** This constant determines the maximum depth of an FTS expression tree
124396 ** that the library will create and use. FTS uses recursion to perform
124397 ** various operations on the query tree, so the disadvantage of a large
124398 ** limit is that it may allow very large queries to use large amounts
124399 ** of stack space (perhaps causing a stack overflow).
124400 */
124401 #ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
124402 # define SQLITE_FTS3_MAX_EXPR_DEPTH 12
124403 #endif
124404 
124405 
124406 /*
124407 ** This constant controls how often segments are merged. Once there are
124408 ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
124409 ** segment of level N+1.
124410 */
124411 #define FTS3_MERGE_COUNT 16
124412 
124413 /*
124414 ** This is the maximum amount of data (in bytes) to store in the
124415 ** Fts3Table.pendingTerms hash table. Normally, the hash table is
124416 ** populated as documents are inserted/updated/deleted in a transaction
124417 ** and used to create a new segment when the transaction is committed.
124418 ** However if this limit is reached midway through a transaction, a new
124419 ** segment is created and the hash table cleared immediately.
124420 */
124421 #define FTS3_MAX_PENDING_DATA (1*1024*1024)
124422 
124423 /*
124424 ** Macro to return the number of elements in an array. SQLite has a
124425 ** similar macro called ArraySize(). Use a different name to avoid
124426 ** a collision when building an amalgamation with built-in FTS3.
124427 */
124428 #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
124429 
124430 
124431 #ifndef MIN
124432 # define MIN(x,y) ((x)<(y)?(x):(y))
124433 #endif
124434 #ifndef MAX
124435 # define MAX(x,y) ((x)>(y)?(x):(y))
124436 #endif
124437 
124438 /*
124439 ** Maximum length of a varint encoded integer. The varint format is different
124440 ** from that used by SQLite, so the maximum length is 10, not 9.
124441 */
124442 #define FTS3_VARINT_MAX 10
124443 
124444 /*
124445 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
124446 ** in the document set and zero or more prefix indexes. All indexes are stored
124447 ** as one or more b+-trees in the %_segments and %_segdir tables.
124448 **
124449 ** It is possible to determine which index a b+-tree belongs to based on the
124450 ** value stored in the "%_segdir.level" column. Given this value L, the index
124451 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
124452 ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
124453 ** between 1024 and 2047 to index 1, and so on.
124454 **
124455 ** It is considered impossible for an index to use more than 1024 levels. In
124456 ** theory though this may happen, but only after at least
124457 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
124458 */
124459 #define FTS3_SEGDIR_MAXLEVEL      1024
124460 #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
124461 
124462 /*
124463 ** The testcase() macro is only used by the amalgamation.  If undefined,
124464 ** make it a no-op.
124465 */
124466 #ifndef testcase
124467 # define testcase(X)
124468 #endif
124469 
124470 /*
124471 ** Terminator values for position-lists and column-lists.
124472 */
124473 #define POS_COLUMN  (1)     /* Column-list terminator */
124474 #define POS_END     (0)     /* Position-list terminator */
124475 
124476 /*
124477 ** This section provides definitions to allow the
124478 ** FTS3 extension to be compiled outside of the
124479 ** amalgamation.
124480 */
124481 #ifndef SQLITE_AMALGAMATION
124482 /*
124483 ** Macros indicating that conditional expressions are always true or
124484 ** false.
124485 */
124486 #ifdef SQLITE_COVERAGE_TEST
124487 # define ALWAYS(x) (1)
124488 # define NEVER(X)  (0)
124489 #else
124490 # define ALWAYS(x) (x)
124491 # define NEVER(x)  (x)
124492 #endif
124493 
124494 /*
124495 ** Internal types used by SQLite.
124496 */
124497 typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
124498 typedef short int i16;            /* 2-byte (or larger) signed integer */
124499 typedef unsigned int u32;         /* 4-byte unsigned integer */
124500 typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
124501 typedef sqlite3_int64 i64;        /* 8-byte signed integer */
124502 
124503 /*
124504 ** Macro used to suppress compiler warnings for unused parameters.
124505 */
124506 #define UNUSED_PARAMETER(x) (void)(x)
124507 
124508 /*
124509 ** Activate assert() only if SQLITE_TEST is enabled.
124510 */
124511 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
124512 # define NDEBUG 1
124513 #endif
124514 
124515 /*
124516 ** The TESTONLY macro is used to enclose variable declarations or
124517 ** other bits of code that are needed to support the arguments
124518 ** within testcase() and assert() macros.
124519 */
124520 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
124521 # define TESTONLY(X)  X
124522 #else
124523 # define TESTONLY(X)
124524 #endif
124525 
124526 #endif /* SQLITE_AMALGAMATION */
124527 
124528 #ifdef SQLITE_DEBUG
124529 SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
124530 # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
124531 #else
124532 # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
124533 #endif
124534 
124535 typedef struct Fts3Table Fts3Table;
124536 typedef struct Fts3Cursor Fts3Cursor;
124537 typedef struct Fts3Expr Fts3Expr;
124538 typedef struct Fts3Phrase Fts3Phrase;
124539 typedef struct Fts3PhraseToken Fts3PhraseToken;
124540 
124541 typedef struct Fts3Doclist Fts3Doclist;
124542 typedef struct Fts3SegFilter Fts3SegFilter;
124543 typedef struct Fts3DeferredToken Fts3DeferredToken;
124544 typedef struct Fts3SegReader Fts3SegReader;
124545 typedef struct Fts3MultiSegReader Fts3MultiSegReader;
124546 
124547 /*
124548 ** A connection to a fulltext index is an instance of the following
124549 ** structure. The xCreate and xConnect methods create an instance
124550 ** of this structure and xDestroy and xDisconnect free that instance.
124551 ** All other methods receive a pointer to the structure as one of their
124552 ** arguments.
124553 */
124554 struct Fts3Table {
124555   sqlite3_vtab base;              /* Base class used by SQLite core */
124556   sqlite3 *db;                    /* The database connection */
124557   const char *zDb;                /* logical database name */
124558   const char *zName;              /* virtual table name */
124559   int nColumn;                    /* number of named columns in virtual table */
124560   char **azColumn;                /* column names.  malloced */
124561   u8 *abNotindexed;               /* True for 'notindexed' columns */
124562   sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
124563   char *zContentTbl;              /* content=xxx option, or NULL */
124564   char *zLanguageid;              /* languageid=xxx option, or NULL */
124565   u8 bAutoincrmerge;              /* True if automerge=1 */
124566   u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
124567 
124568   /* Precompiled statements used by the implementation. Each of these
124569   ** statements is run and reset within a single virtual table API call.
124570   */
124571   sqlite3_stmt *aStmt[37];
124572 
124573   char *zReadExprlist;
124574   char *zWriteExprlist;
124575 
124576   int nNodeSize;                  /* Soft limit for node size */
124577   u8 bFts4;                       /* True for FTS4, false for FTS3 */
124578   u8 bHasStat;                    /* True if %_stat table exists */
124579   u8 bHasDocsize;                 /* True if %_docsize table exists */
124580   u8 bDescIdx;                    /* True if doclists are in reverse order */
124581   u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
124582   int nPgsz;                      /* Page size for host database */
124583   char *zSegmentsTbl;             /* Name of %_segments table */
124584   sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
124585 
124586   /*
124587   ** The following array of hash tables is used to buffer pending index
124588   ** updates during transactions. All pending updates buffered at any one
124589   ** time must share a common language-id (see the FTS4 langid= feature).
124590   ** The current language id is stored in variable iPrevLangid.
124591   **
124592   ** A single FTS4 table may have multiple full-text indexes. For each index
124593   ** there is an entry in the aIndex[] array. Index 0 is an index of all the
124594   ** terms that appear in the document set. Each subsequent index in aIndex[]
124595   ** is an index of prefixes of a specific length.
124596   **
124597   ** Variable nPendingData contains an estimate the memory consumed by the
124598   ** pending data structures, including hash table overhead, but not including
124599   ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
124600   ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
124601   ** recently inserted record.
124602   */
124603   int nIndex;                     /* Size of aIndex[] */
124604   struct Fts3Index {
124605     int nPrefix;                  /* Prefix length (0 for main terms index) */
124606     Fts3Hash hPending;            /* Pending terms table for this index */
124607   } *aIndex;
124608   int nMaxPendingData;            /* Max pending data before flush to disk */
124609   int nPendingData;               /* Current bytes of pending data */
124610   sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
124611   int iPrevLangid;                /* Langid of recently inserted document */
124612 
124613 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
124614   /* State variables used for validating that the transaction control
124615   ** methods of the virtual table are called at appropriate times.  These
124616   ** values do not contribute to FTS functionality; they are used for
124617   ** verifying the operation of the SQLite core.
124618   */
124619   int inTransaction;     /* True after xBegin but before xCommit/xRollback */
124620   int mxSavepoint;       /* Largest valid xSavepoint integer */
124621 #endif
124622 
124623 #ifdef SQLITE_TEST
124624   /* True to disable the incremental doclist optimization. This is controled
124625   ** by special insert command 'test-no-incr-doclist'.  */
124626   int bNoIncrDoclist;
124627 #endif
124628 };
124629 
124630 /*
124631 ** When the core wants to read from the virtual table, it creates a
124632 ** virtual table cursor (an instance of the following structure) using
124633 ** the xOpen method. Cursors are destroyed using the xClose method.
124634 */
124635 struct Fts3Cursor {
124636   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
124637   i16 eSearch;                    /* Search strategy (see below) */
124638   u8 isEof;                       /* True if at End Of Results */
124639   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
124640   sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
124641   Fts3Expr *pExpr;                /* Parsed MATCH query string */
124642   int iLangid;                    /* Language being queried for */
124643   int nPhrase;                    /* Number of matchable phrases in query */
124644   Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
124645   sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
124646   char *pNextId;                  /* Pointer into the body of aDoclist */
124647   char *aDoclist;                 /* List of docids for full-text queries */
124648   int nDoclist;                   /* Size of buffer at aDoclist */
124649   u8 bDesc;                       /* True to sort in descending order */
124650   int eEvalmode;                  /* An FTS3_EVAL_XX constant */
124651   int nRowAvg;                    /* Average size of database rows, in pages */
124652   sqlite3_int64 nDoc;             /* Documents in table */
124653   i64 iMinDocid;                  /* Minimum docid to return */
124654   i64 iMaxDocid;                  /* Maximum docid to return */
124655   int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
124656   u32 *aMatchinfo;                /* Information about most recent match */
124657   int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
124658   char *zMatchinfo;               /* Matchinfo specification */
124659 };
124660 
124661 #define FTS3_EVAL_FILTER    0
124662 #define FTS3_EVAL_NEXT      1
124663 #define FTS3_EVAL_MATCHINFO 2
124664 
124665 /*
124666 ** The Fts3Cursor.eSearch member is always set to one of the following.
124667 ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
124668 ** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
124669 ** of the column to be searched.  For example, in
124670 **
124671 **     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
124672 **     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
124673 **
124674 ** Because the LHS of the MATCH operator is 2nd column "b",
124675 ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
124676 ** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1"
124677 ** indicating that all columns should be searched,
124678 ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
124679 */
124680 #define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
124681 #define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
124682 #define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
124683 
124684 /*
124685 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
124686 ** the xBestIndex() method contains the Fts3Cursor.eSearch value described
124687 ** above. The upper 16-bits contain a combination of the following
124688 ** bits, used to describe extra constraints on full-text searches.
124689 */
124690 #define FTS3_HAVE_LANGID    0x00010000      /* languageid=? */
124691 #define FTS3_HAVE_DOCID_GE  0x00020000      /* docid>=? */
124692 #define FTS3_HAVE_DOCID_LE  0x00040000      /* docid<=? */
124693 
124694 struct Fts3Doclist {
124695   char *aAll;                    /* Array containing doclist (or NULL) */
124696   int nAll;                      /* Size of a[] in bytes */
124697   char *pNextDocid;              /* Pointer to next docid */
124698 
124699   sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
124700   int bFreeList;                 /* True if pList should be sqlite3_free()d */
124701   char *pList;                   /* Pointer to position list following iDocid */
124702   int nList;                     /* Length of position list */
124703 };
124704 
124705 /*
124706 ** A "phrase" is a sequence of one or more tokens that must match in
124707 ** sequence.  A single token is the base case and the most common case.
124708 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
124709 ** nToken will be the number of tokens in the string.
124710 */
124711 struct Fts3PhraseToken {
124712   char *z;                        /* Text of the token */
124713   int n;                          /* Number of bytes in buffer z */
124714   int isPrefix;                   /* True if token ends with a "*" character */
124715   int bFirst;                     /* True if token must appear at position 0 */
124716 
124717   /* Variables above this point are populated when the expression is
124718   ** parsed (by code in fts3_expr.c). Below this point the variables are
124719   ** used when evaluating the expression. */
124720   Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
124721   Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
124722 };
124723 
124724 struct Fts3Phrase {
124725   /* Cache of doclist for this phrase. */
124726   Fts3Doclist doclist;
124727   int bIncr;                 /* True if doclist is loaded incrementally */
124728   int iDoclistToken;
124729 
124730   /* Variables below this point are populated by fts3_expr.c when parsing
124731   ** a MATCH expression. Everything above is part of the evaluation phase.
124732   */
124733   int nToken;                /* Number of tokens in the phrase */
124734   int iColumn;               /* Index of column this phrase must match */
124735   Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
124736 };
124737 
124738 /*
124739 ** A tree of these objects forms the RHS of a MATCH operator.
124740 **
124741 ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
124742 ** points to a malloced buffer, size nDoclist bytes, containing the results
124743 ** of this phrase query in FTS3 doclist format. As usual, the initial
124744 ** "Length" field found in doclists stored on disk is omitted from this
124745 ** buffer.
124746 **
124747 ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
124748 ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
124749 ** where nCol is the number of columns in the queried FTS table. The array
124750 ** is populated as follows:
124751 **
124752 **   aMI[iCol*3 + 0] = Undefined
124753 **   aMI[iCol*3 + 1] = Number of occurrences
124754 **   aMI[iCol*3 + 2] = Number of rows containing at least one instance
124755 **
124756 ** The aMI array is allocated using sqlite3_malloc(). It should be freed
124757 ** when the expression node is.
124758 */
124759 struct Fts3Expr {
124760   int eType;                 /* One of the FTSQUERY_XXX values defined below */
124761   int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
124762   Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
124763   Fts3Expr *pLeft;           /* Left operand */
124764   Fts3Expr *pRight;          /* Right operand */
124765   Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
124766 
124767   /* The following are used by the fts3_eval.c module. */
124768   sqlite3_int64 iDocid;      /* Current docid */
124769   u8 bEof;                   /* True this expression is at EOF already */
124770   u8 bStart;                 /* True if iDocid is valid */
124771   u8 bDeferred;              /* True if this expression is entirely deferred */
124772 
124773   u32 *aMI;
124774 };
124775 
124776 /*
124777 ** Candidate values for Fts3Query.eType. Note that the order of the first
124778 ** four values is in order of precedence when parsing expressions. For
124779 ** example, the following:
124780 **
124781 **   "a OR b AND c NOT d NEAR e"
124782 **
124783 ** is equivalent to:
124784 **
124785 **   "a OR (b AND (c NOT (d NEAR e)))"
124786 */
124787 #define FTSQUERY_NEAR   1
124788 #define FTSQUERY_NOT    2
124789 #define FTSQUERY_AND    3
124790 #define FTSQUERY_OR     4
124791 #define FTSQUERY_PHRASE 5
124792 
124793 
124794 /* fts3_write.c */
124795 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
124796 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
124797 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
124798 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
124799 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
124800   sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
124801 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
124802   Fts3Table*,int,const char*,int,int,Fts3SegReader**);
124803 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
124804 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
124805 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
124806 
124807 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
124808 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
124809 
124810 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
124811 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
124812 SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
124813 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
124814 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
124815 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
124816 #else
124817 # define sqlite3Fts3FreeDeferredTokens(x)
124818 # define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
124819 # define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
124820 # define sqlite3Fts3FreeDeferredDoclists(x)
124821 # define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
124822 #endif
124823 
124824 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
124825 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
124826 
124827 /* Special values interpreted by sqlite3SegReaderCursor() */
124828 #define FTS3_SEGCURSOR_PENDING        -1
124829 #define FTS3_SEGCURSOR_ALL            -2
124830 
124831 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
124832 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
124833 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
124834 
124835 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
124836     int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
124837 
124838 /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
124839 #define FTS3_SEGMENT_REQUIRE_POS   0x00000001
124840 #define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
124841 #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
124842 #define FTS3_SEGMENT_PREFIX        0x00000008
124843 #define FTS3_SEGMENT_SCAN          0x00000010
124844 #define FTS3_SEGMENT_FIRST         0x00000020
124845 
124846 /* Type passed as 4th argument to SegmentReaderIterate() */
124847 struct Fts3SegFilter {
124848   const char *zTerm;
124849   int nTerm;
124850   int iCol;
124851   int flags;
124852 };
124853 
124854 struct Fts3MultiSegReader {
124855   /* Used internally by sqlite3Fts3SegReaderXXX() calls */
124856   Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
124857   int nSegment;                   /* Size of apSegment array */
124858   int nAdvance;                   /* How many seg-readers to advance */
124859   Fts3SegFilter *pFilter;         /* Pointer to filter object */
124860   char *aBuffer;                  /* Buffer to merge doclists in */
124861   int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
124862 
124863   int iColFilter;                 /* If >=0, filter for this column */
124864   int bRestart;
124865 
124866   /* Used by fts3.c only. */
124867   int nCost;                      /* Cost of running iterator */
124868   int bLookup;                    /* True if a lookup of a single entry. */
124869 
124870   /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
124871   char *zTerm;                    /* Pointer to term buffer */
124872   int nTerm;                      /* Size of zTerm in bytes */
124873   char *aDoclist;                 /* Pointer to doclist buffer */
124874   int nDoclist;                   /* Size of aDoclist[] in bytes */
124875 };
124876 
124877 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
124878 
124879 #define fts3GetVarint32(p, piVal) (                                           \
124880   (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
124881 )
124882 
124883 /* fts3.c */
124884 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
124885 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
124886 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
124887 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
124888 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
124889 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
124890 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
124891 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
124892 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
124893 
124894 /* fts3_tokenizer.c */
124895 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
124896 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
124897 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
124898     sqlite3_tokenizer **, char **
124899 );
124900 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
124901 
124902 /* fts3_snippet.c */
124903 SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
124904 SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
124905   const char *, const char *, int, int
124906 );
124907 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
124908 
124909 /* fts3_expr.c */
124910 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
124911   char **, int, int, int, const char *, int, Fts3Expr **, char **
124912 );
124913 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
124914 #ifdef SQLITE_TEST
124915 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
124916 SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
124917 #endif
124918 
124919 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
124920   sqlite3_tokenizer_cursor **
124921 );
124922 
124923 /* fts3_aux.c */
124924 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
124925 
124926 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
124927 
124928 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
124929     Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
124930 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
124931     Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
124932 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
124933 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
124934 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
124935 
124936 /* fts3_tokenize_vtab.c */
124937 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
124938 
124939 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
124940 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
124941 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
124942 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
124943 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
124944 #endif
124945 
124946 #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
124947 #endif /* _FTSINT_H */
124948 
124949 /************** End of fts3Int.h *********************************************/
124950 /************** Continuing where we left off in fts3.c ***********************/
124951 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
124952 
124953 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
124954 # define SQLITE_CORE 1
124955 #endif
124956 
124957 /* #include <assert.h> */
124958 /* #include <stdlib.h> */
124959 /* #include <stddef.h> */
124960 /* #include <stdio.h> */
124961 /* #include <string.h> */
124962 /* #include <stdarg.h> */
124963 
124964 #ifndef SQLITE_CORE
124965   SQLITE_EXTENSION_INIT1
124966 #endif
124967 
124968 static int fts3EvalNext(Fts3Cursor *pCsr);
124969 static int fts3EvalStart(Fts3Cursor *pCsr);
124970 static int fts3TermSegReaderCursor(
124971     Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
124972 
124973 /*
124974 ** Write a 64-bit variable-length integer to memory starting at p[0].
124975 ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
124976 ** The number of bytes written is returned.
124977 */
124978 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
124979   unsigned char *q = (unsigned char *) p;
124980   sqlite_uint64 vu = v;
124981   do{
124982     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
124983     vu >>= 7;
124984   }while( vu!=0 );
124985   q[-1] &= 0x7f;  /* turn off high bit in final byte */
124986   assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
124987   return (int) (q - (unsigned char *)p);
124988 }
124989 
124990 #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
124991   v = (v & mask1) | ( (*ptr++) << shift );                    \
124992   if( (v & mask2)==0 ){ var = v; return ret; }
124993 #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
124994   v = (*ptr++);                                               \
124995   if( (v & mask2)==0 ){ var = v; return ret; }
124996 
124997 /*
124998 ** Read a 64-bit variable-length integer from memory starting at p[0].
124999 ** Return the number of bytes read, or 0 on error.
125000 ** The value is stored in *v.
125001 */
125002 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
125003   const char *pStart = p;
125004   u32 a;
125005   u64 b;
125006   int shift;
125007 
125008   GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
125009   GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
125010   GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
125011   GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
125012   b = (a & 0x0FFFFFFF );
125013 
125014   for(shift=28; shift<=63; shift+=7){
125015     u64 c = *p++;
125016     b += (c&0x7F) << shift;
125017     if( (c & 0x80)==0 ) break;
125018   }
125019   *v = b;
125020   return (int)(p - pStart);
125021 }
125022 
125023 /*
125024 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
125025 ** 32-bit integer before it is returned.
125026 */
125027 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
125028   u32 a;
125029 
125030 #ifndef fts3GetVarint32
125031   GETVARINT_INIT(a, p, 0,  0x00,     0x80, *pi, 1);
125032 #else
125033   a = (*p++);
125034   assert( a & 0x80 );
125035 #endif
125036 
125037   GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *pi, 2);
125038   GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *pi, 3);
125039   GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4);
125040   a = (a & 0x0FFFFFFF );
125041   *pi = (int)(a | ((u32)(*p & 0x0F) << 28));
125042   return 5;
125043 }
125044 
125045 /*
125046 ** Return the number of bytes required to encode v as a varint
125047 */
125048 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
125049   int i = 0;
125050   do{
125051     i++;
125052     v >>= 7;
125053   }while( v!=0 );
125054   return i;
125055 }
125056 
125057 /*
125058 ** Convert an SQL-style quoted string into a normal string by removing
125059 ** the quote characters.  The conversion is done in-place.  If the
125060 ** input does not begin with a quote character, then this routine
125061 ** is a no-op.
125062 **
125063 ** Examples:
125064 **
125065 **     "abc"   becomes   abc
125066 **     'xyz'   becomes   xyz
125067 **     [pqr]   becomes   pqr
125068 **     `mno`   becomes   mno
125069 **
125070 */
125071 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
125072   char quote;                     /* Quote character (if any ) */
125073 
125074   quote = z[0];
125075   if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
125076     int iIn = 1;                  /* Index of next byte to read from input */
125077     int iOut = 0;                 /* Index of next byte to write to output */
125078 
125079     /* If the first byte was a '[', then the close-quote character is a ']' */
125080     if( quote=='[' ) quote = ']';
125081 
125082     while( ALWAYS(z[iIn]) ){
125083       if( z[iIn]==quote ){
125084         if( z[iIn+1]!=quote ) break;
125085         z[iOut++] = quote;
125086         iIn += 2;
125087       }else{
125088         z[iOut++] = z[iIn++];
125089       }
125090     }
125091     z[iOut] = '\0';
125092   }
125093 }
125094 
125095 /*
125096 ** Read a single varint from the doclist at *pp and advance *pp to point
125097 ** to the first byte past the end of the varint.  Add the value of the varint
125098 ** to *pVal.
125099 */
125100 static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
125101   sqlite3_int64 iVal;
125102   *pp += sqlite3Fts3GetVarint(*pp, &iVal);
125103   *pVal += iVal;
125104 }
125105 
125106 /*
125107 ** When this function is called, *pp points to the first byte following a
125108 ** varint that is part of a doclist (or position-list, or any other list
125109 ** of varints). This function moves *pp to point to the start of that varint,
125110 ** and sets *pVal by the varint value.
125111 **
125112 ** Argument pStart points to the first byte of the doclist that the
125113 ** varint is part of.
125114 */
125115 static void fts3GetReverseVarint(
125116   char **pp,
125117   char *pStart,
125118   sqlite3_int64 *pVal
125119 ){
125120   sqlite3_int64 iVal;
125121   char *p;
125122 
125123   /* Pointer p now points at the first byte past the varint we are
125124   ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
125125   ** clear on character p[-1]. */
125126   for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
125127   p++;
125128   *pp = p;
125129 
125130   sqlite3Fts3GetVarint(p, &iVal);
125131   *pVal = iVal;
125132 }
125133 
125134 /*
125135 ** The xDisconnect() virtual table method.
125136 */
125137 static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
125138   Fts3Table *p = (Fts3Table *)pVtab;
125139   int i;
125140 
125141   assert( p->nPendingData==0 );
125142   assert( p->pSegments==0 );
125143 
125144   /* Free any prepared statements held */
125145   for(i=0; i<SizeofArray(p->aStmt); i++){
125146     sqlite3_finalize(p->aStmt[i]);
125147   }
125148   sqlite3_free(p->zSegmentsTbl);
125149   sqlite3_free(p->zReadExprlist);
125150   sqlite3_free(p->zWriteExprlist);
125151   sqlite3_free(p->zContentTbl);
125152   sqlite3_free(p->zLanguageid);
125153 
125154   /* Invoke the tokenizer destructor to free the tokenizer. */
125155   p->pTokenizer->pModule->xDestroy(p->pTokenizer);
125156 
125157   sqlite3_free(p);
125158   return SQLITE_OK;
125159 }
125160 
125161 /*
125162 ** Construct one or more SQL statements from the format string given
125163 ** and then evaluate those statements. The success code is written
125164 ** into *pRc.
125165 **
125166 ** If *pRc is initially non-zero then this routine is a no-op.
125167 */
125168 static void fts3DbExec(
125169   int *pRc,              /* Success code */
125170   sqlite3 *db,           /* Database in which to run SQL */
125171   const char *zFormat,   /* Format string for SQL */
125172   ...                    /* Arguments to the format string */
125173 ){
125174   va_list ap;
125175   char *zSql;
125176   if( *pRc ) return;
125177   va_start(ap, zFormat);
125178   zSql = sqlite3_vmprintf(zFormat, ap);
125179   va_end(ap);
125180   if( zSql==0 ){
125181     *pRc = SQLITE_NOMEM;
125182   }else{
125183     *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
125184     sqlite3_free(zSql);
125185   }
125186 }
125187 
125188 /*
125189 ** The xDestroy() virtual table method.
125190 */
125191 static int fts3DestroyMethod(sqlite3_vtab *pVtab){
125192   Fts3Table *p = (Fts3Table *)pVtab;
125193   int rc = SQLITE_OK;              /* Return code */
125194   const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
125195   sqlite3 *db = p->db;             /* Database handle */
125196 
125197   /* Drop the shadow tables */
125198   if( p->zContentTbl==0 ){
125199     fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
125200   }
125201   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
125202   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
125203   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
125204   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
125205 
125206   /* If everything has worked, invoke fts3DisconnectMethod() to free the
125207   ** memory associated with the Fts3Table structure and return SQLITE_OK.
125208   ** Otherwise, return an SQLite error code.
125209   */
125210   return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
125211 }
125212 
125213 
125214 /*
125215 ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
125216 ** passed as the first argument. This is done as part of the xConnect()
125217 ** and xCreate() methods.
125218 **
125219 ** If *pRc is non-zero when this function is called, it is a no-op.
125220 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
125221 ** before returning.
125222 */
125223 static void fts3DeclareVtab(int *pRc, Fts3Table *p){
125224   if( *pRc==SQLITE_OK ){
125225     int i;                        /* Iterator variable */
125226     int rc;                       /* Return code */
125227     char *zSql;                   /* SQL statement passed to declare_vtab() */
125228     char *zCols;                  /* List of user defined columns */
125229     const char *zLanguageid;
125230 
125231     zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
125232     sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
125233 
125234     /* Create a list of user columns for the virtual table */
125235     zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
125236     for(i=1; zCols && i<p->nColumn; i++){
125237       zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
125238     }
125239 
125240     /* Create the whole "CREATE TABLE" statement to pass to SQLite */
125241     zSql = sqlite3_mprintf(
125242         "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
125243         zCols, p->zName, zLanguageid
125244     );
125245     if( !zCols || !zSql ){
125246       rc = SQLITE_NOMEM;
125247     }else{
125248       rc = sqlite3_declare_vtab(p->db, zSql);
125249     }
125250 
125251     sqlite3_free(zSql);
125252     sqlite3_free(zCols);
125253     *pRc = rc;
125254   }
125255 }
125256 
125257 /*
125258 ** Create the %_stat table if it does not already exist.
125259 */
125260 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
125261   fts3DbExec(pRc, p->db,
125262       "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
125263           "(id INTEGER PRIMARY KEY, value BLOB);",
125264       p->zDb, p->zName
125265   );
125266   if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
125267 }
125268 
125269 /*
125270 ** Create the backing store tables (%_content, %_segments and %_segdir)
125271 ** required by the FTS3 table passed as the only argument. This is done
125272 ** as part of the vtab xCreate() method.
125273 **
125274 ** If the p->bHasDocsize boolean is true (indicating that this is an
125275 ** FTS4 table, not an FTS3 table) then also create the %_docsize and
125276 ** %_stat tables required by FTS4.
125277 */
125278 static int fts3CreateTables(Fts3Table *p){
125279   int rc = SQLITE_OK;             /* Return code */
125280   int i;                          /* Iterator variable */
125281   sqlite3 *db = p->db;            /* The database connection */
125282 
125283   if( p->zContentTbl==0 ){
125284     const char *zLanguageid = p->zLanguageid;
125285     char *zContentCols;           /* Columns of %_content table */
125286 
125287     /* Create a list of user columns for the content table */
125288     zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
125289     for(i=0; zContentCols && i<p->nColumn; i++){
125290       char *z = p->azColumn[i];
125291       zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
125292     }
125293     if( zLanguageid && zContentCols ){
125294       zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
125295     }
125296     if( zContentCols==0 ) rc = SQLITE_NOMEM;
125297 
125298     /* Create the content table */
125299     fts3DbExec(&rc, db,
125300        "CREATE TABLE %Q.'%q_content'(%s)",
125301        p->zDb, p->zName, zContentCols
125302     );
125303     sqlite3_free(zContentCols);
125304   }
125305 
125306   /* Create other tables */
125307   fts3DbExec(&rc, db,
125308       "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
125309       p->zDb, p->zName
125310   );
125311   fts3DbExec(&rc, db,
125312       "CREATE TABLE %Q.'%q_segdir'("
125313         "level INTEGER,"
125314         "idx INTEGER,"
125315         "start_block INTEGER,"
125316         "leaves_end_block INTEGER,"
125317         "end_block INTEGER,"
125318         "root BLOB,"
125319         "PRIMARY KEY(level, idx)"
125320       ");",
125321       p->zDb, p->zName
125322   );
125323   if( p->bHasDocsize ){
125324     fts3DbExec(&rc, db,
125325         "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
125326         p->zDb, p->zName
125327     );
125328   }
125329   assert( p->bHasStat==p->bFts4 );
125330   if( p->bHasStat ){
125331     sqlite3Fts3CreateStatTable(&rc, p);
125332   }
125333   return rc;
125334 }
125335 
125336 /*
125337 ** Store the current database page-size in bytes in p->nPgsz.
125338 **
125339 ** If *pRc is non-zero when this function is called, it is a no-op.
125340 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
125341 ** before returning.
125342 */
125343 static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
125344   if( *pRc==SQLITE_OK ){
125345     int rc;                       /* Return code */
125346     char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
125347     sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
125348 
125349     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
125350     if( !zSql ){
125351       rc = SQLITE_NOMEM;
125352     }else{
125353       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
125354       if( rc==SQLITE_OK ){
125355         sqlite3_step(pStmt);
125356         p->nPgsz = sqlite3_column_int(pStmt, 0);
125357         rc = sqlite3_finalize(pStmt);
125358       }else if( rc==SQLITE_AUTH ){
125359         p->nPgsz = 1024;
125360         rc = SQLITE_OK;
125361       }
125362     }
125363     assert( p->nPgsz>0 || rc!=SQLITE_OK );
125364     sqlite3_free(zSql);
125365     *pRc = rc;
125366   }
125367 }
125368 
125369 /*
125370 ** "Special" FTS4 arguments are column specifications of the following form:
125371 **
125372 **   <key> = <value>
125373 **
125374 ** There may not be whitespace surrounding the "=" character. The <value>
125375 ** term may be quoted, but the <key> may not.
125376 */
125377 static int fts3IsSpecialColumn(
125378   const char *z,
125379   int *pnKey,
125380   char **pzValue
125381 ){
125382   char *zValue;
125383   const char *zCsr = z;
125384 
125385   while( *zCsr!='=' ){
125386     if( *zCsr=='\0' ) return 0;
125387     zCsr++;
125388   }
125389 
125390   *pnKey = (int)(zCsr-z);
125391   zValue = sqlite3_mprintf("%s", &zCsr[1]);
125392   if( zValue ){
125393     sqlite3Fts3Dequote(zValue);
125394   }
125395   *pzValue = zValue;
125396   return 1;
125397 }
125398 
125399 /*
125400 ** Append the output of a printf() style formatting to an existing string.
125401 */
125402 static void fts3Appendf(
125403   int *pRc,                       /* IN/OUT: Error code */
125404   char **pz,                      /* IN/OUT: Pointer to string buffer */
125405   const char *zFormat,            /* Printf format string to append */
125406   ...                             /* Arguments for printf format string */
125407 ){
125408   if( *pRc==SQLITE_OK ){
125409     va_list ap;
125410     char *z;
125411     va_start(ap, zFormat);
125412     z = sqlite3_vmprintf(zFormat, ap);
125413     va_end(ap);
125414     if( z && *pz ){
125415       char *z2 = sqlite3_mprintf("%s%s", *pz, z);
125416       sqlite3_free(z);
125417       z = z2;
125418     }
125419     if( z==0 ) *pRc = SQLITE_NOMEM;
125420     sqlite3_free(*pz);
125421     *pz = z;
125422   }
125423 }
125424 
125425 /*
125426 ** Return a copy of input string zInput enclosed in double-quotes (") and
125427 ** with all double quote characters escaped. For example:
125428 **
125429 **     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
125430 **
125431 ** The pointer returned points to memory obtained from sqlite3_malloc(). It
125432 ** is the callers responsibility to call sqlite3_free() to release this
125433 ** memory.
125434 */
125435 static char *fts3QuoteId(char const *zInput){
125436   int nRet;
125437   char *zRet;
125438   nRet = 2 + (int)strlen(zInput)*2 + 1;
125439   zRet = sqlite3_malloc(nRet);
125440   if( zRet ){
125441     int i;
125442     char *z = zRet;
125443     *(z++) = '"';
125444     for(i=0; zInput[i]; i++){
125445       if( zInput[i]=='"' ) *(z++) = '"';
125446       *(z++) = zInput[i];
125447     }
125448     *(z++) = '"';
125449     *(z++) = '\0';
125450   }
125451   return zRet;
125452 }
125453 
125454 /*
125455 ** Return a list of comma separated SQL expressions and a FROM clause that
125456 ** could be used in a SELECT statement such as the following:
125457 **
125458 **     SELECT <list of expressions> FROM %_content AS x ...
125459 **
125460 ** to return the docid, followed by each column of text data in order
125461 ** from left to write. If parameter zFunc is not NULL, then instead of
125462 ** being returned directly each column of text data is passed to an SQL
125463 ** function named zFunc first. For example, if zFunc is "unzip" and the
125464 ** table has the three user-defined columns "a", "b", and "c", the following
125465 ** string is returned:
125466 **
125467 **     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
125468 **
125469 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
125470 ** is the responsibility of the caller to eventually free it.
125471 **
125472 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
125473 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
125474 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
125475 ** no error occurs, *pRc is left unmodified.
125476 */
125477 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
125478   char *zRet = 0;
125479   char *zFree = 0;
125480   char *zFunction;
125481   int i;
125482 
125483   if( p->zContentTbl==0 ){
125484     if( !zFunc ){
125485       zFunction = "";
125486     }else{
125487       zFree = zFunction = fts3QuoteId(zFunc);
125488     }
125489     fts3Appendf(pRc, &zRet, "docid");
125490     for(i=0; i<p->nColumn; i++){
125491       fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
125492     }
125493     if( p->zLanguageid ){
125494       fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
125495     }
125496     sqlite3_free(zFree);
125497   }else{
125498     fts3Appendf(pRc, &zRet, "rowid");
125499     for(i=0; i<p->nColumn; i++){
125500       fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
125501     }
125502     if( p->zLanguageid ){
125503       fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
125504     }
125505   }
125506   fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
125507       p->zDb,
125508       (p->zContentTbl ? p->zContentTbl : p->zName),
125509       (p->zContentTbl ? "" : "_content")
125510   );
125511   return zRet;
125512 }
125513 
125514 /*
125515 ** Return a list of N comma separated question marks, where N is the number
125516 ** of columns in the %_content table (one for the docid plus one for each
125517 ** user-defined text column).
125518 **
125519 ** If argument zFunc is not NULL, then all but the first question mark
125520 ** is preceded by zFunc and an open bracket, and followed by a closed
125521 ** bracket. For example, if zFunc is "zip" and the FTS3 table has three
125522 ** user-defined text columns, the following string is returned:
125523 **
125524 **     "?, zip(?), zip(?), zip(?)"
125525 **
125526 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
125527 ** is the responsibility of the caller to eventually free it.
125528 **
125529 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
125530 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
125531 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
125532 ** no error occurs, *pRc is left unmodified.
125533 */
125534 static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
125535   char *zRet = 0;
125536   char *zFree = 0;
125537   char *zFunction;
125538   int i;
125539 
125540   if( !zFunc ){
125541     zFunction = "";
125542   }else{
125543     zFree = zFunction = fts3QuoteId(zFunc);
125544   }
125545   fts3Appendf(pRc, &zRet, "?");
125546   for(i=0; i<p->nColumn; i++){
125547     fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
125548   }
125549   if( p->zLanguageid ){
125550     fts3Appendf(pRc, &zRet, ", ?");
125551   }
125552   sqlite3_free(zFree);
125553   return zRet;
125554 }
125555 
125556 /*
125557 ** This function interprets the string at (*pp) as a non-negative integer
125558 ** value. It reads the integer and sets *pnOut to the value read, then
125559 ** sets *pp to point to the byte immediately following the last byte of
125560 ** the integer value.
125561 **
125562 ** Only decimal digits ('0'..'9') may be part of an integer value.
125563 **
125564 ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
125565 ** the output value undefined. Otherwise SQLITE_OK is returned.
125566 **
125567 ** This function is used when parsing the "prefix=" FTS4 parameter.
125568 */
125569 static int fts3GobbleInt(const char **pp, int *pnOut){
125570   const char *p;                  /* Iterator pointer */
125571   int nInt = 0;                   /* Output value */
125572 
125573   for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
125574     nInt = nInt * 10 + (p[0] - '0');
125575   }
125576   if( p==*pp ) return SQLITE_ERROR;
125577   *pnOut = nInt;
125578   *pp = p;
125579   return SQLITE_OK;
125580 }
125581 
125582 /*
125583 ** This function is called to allocate an array of Fts3Index structures
125584 ** representing the indexes maintained by the current FTS table. FTS tables
125585 ** always maintain the main "terms" index, but may also maintain one or
125586 ** more "prefix" indexes, depending on the value of the "prefix=" parameter
125587 ** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
125588 **
125589 ** Argument zParam is passed the value of the "prefix=" option if one was
125590 ** specified, or NULL otherwise.
125591 **
125592 ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
125593 ** the allocated array. *pnIndex is set to the number of elements in the
125594 ** array. If an error does occur, an SQLite error code is returned.
125595 **
125596 ** Regardless of whether or not an error is returned, it is the responsibility
125597 ** of the caller to call sqlite3_free() on the output array to free it.
125598 */
125599 static int fts3PrefixParameter(
125600   const char *zParam,             /* ABC in prefix=ABC parameter to parse */
125601   int *pnIndex,                   /* OUT: size of *apIndex[] array */
125602   struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
125603 ){
125604   struct Fts3Index *aIndex;       /* Allocated array */
125605   int nIndex = 1;                 /* Number of entries in array */
125606 
125607   if( zParam && zParam[0] ){
125608     const char *p;
125609     nIndex++;
125610     for(p=zParam; *p; p++){
125611       if( *p==',' ) nIndex++;
125612     }
125613   }
125614 
125615   aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
125616   *apIndex = aIndex;
125617   *pnIndex = nIndex;
125618   if( !aIndex ){
125619     return SQLITE_NOMEM;
125620   }
125621 
125622   memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
125623   if( zParam ){
125624     const char *p = zParam;
125625     int i;
125626     for(i=1; i<nIndex; i++){
125627       int nPrefix;
125628       if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
125629       aIndex[i].nPrefix = nPrefix;
125630       p++;
125631     }
125632   }
125633 
125634   return SQLITE_OK;
125635 }
125636 
125637 /*
125638 ** This function is called when initializing an FTS4 table that uses the
125639 ** content=xxx option. It determines the number of and names of the columns
125640 ** of the new FTS4 table.
125641 **
125642 ** The third argument passed to this function is the value passed to the
125643 ** config=xxx option (i.e. "xxx"). This function queries the database for
125644 ** a table of that name. If found, the output variables are populated
125645 ** as follows:
125646 **
125647 **   *pnCol:   Set to the number of columns table xxx has,
125648 **
125649 **   *pnStr:   Set to the total amount of space required to store a copy
125650 **             of each columns name, including the nul-terminator.
125651 **
125652 **   *pazCol:  Set to point to an array of *pnCol strings. Each string is
125653 **             the name of the corresponding column in table xxx. The array
125654 **             and its contents are allocated using a single allocation. It
125655 **             is the responsibility of the caller to free this allocation
125656 **             by eventually passing the *pazCol value to sqlite3_free().
125657 **
125658 ** If the table cannot be found, an error code is returned and the output
125659 ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
125660 ** returned (and the output variables are undefined).
125661 */
125662 static int fts3ContentColumns(
125663   sqlite3 *db,                    /* Database handle */
125664   const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
125665   const char *zTbl,               /* Name of content table */
125666   const char ***pazCol,           /* OUT: Malloc'd array of column names */
125667   int *pnCol,                     /* OUT: Size of array *pazCol */
125668   int *pnStr                      /* OUT: Bytes of string content */
125669 ){
125670   int rc = SQLITE_OK;             /* Return code */
125671   char *zSql;                     /* "SELECT *" statement on zTbl */
125672   sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
125673 
125674   zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
125675   if( !zSql ){
125676     rc = SQLITE_NOMEM;
125677   }else{
125678     rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
125679   }
125680   sqlite3_free(zSql);
125681 
125682   if( rc==SQLITE_OK ){
125683     const char **azCol;           /* Output array */
125684     int nStr = 0;                 /* Size of all column names (incl. 0x00) */
125685     int nCol;                     /* Number of table columns */
125686     int i;                        /* Used to iterate through columns */
125687 
125688     /* Loop through the returned columns. Set nStr to the number of bytes of
125689     ** space required to store a copy of each column name, including the
125690     ** nul-terminator byte.  */
125691     nCol = sqlite3_column_count(pStmt);
125692     for(i=0; i<nCol; i++){
125693       const char *zCol = sqlite3_column_name(pStmt, i);
125694       nStr += (int)strlen(zCol) + 1;
125695     }
125696 
125697     /* Allocate and populate the array to return. */
125698     azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
125699     if( azCol==0 ){
125700       rc = SQLITE_NOMEM;
125701     }else{
125702       char *p = (char *)&azCol[nCol];
125703       for(i=0; i<nCol; i++){
125704         const char *zCol = sqlite3_column_name(pStmt, i);
125705         int n = (int)strlen(zCol)+1;
125706         memcpy(p, zCol, n);
125707         azCol[i] = p;
125708         p += n;
125709       }
125710     }
125711     sqlite3_finalize(pStmt);
125712 
125713     /* Set the output variables. */
125714     *pnCol = nCol;
125715     *pnStr = nStr;
125716     *pazCol = azCol;
125717   }
125718 
125719   return rc;
125720 }
125721 
125722 /*
125723 ** This function is the implementation of both the xConnect and xCreate
125724 ** methods of the FTS3 virtual table.
125725 **
125726 ** The argv[] array contains the following:
125727 **
125728 **   argv[0]   -> module name  ("fts3" or "fts4")
125729 **   argv[1]   -> database name
125730 **   argv[2]   -> table name
125731 **   argv[...] -> "column name" and other module argument fields.
125732 */
125733 static int fts3InitVtab(
125734   int isCreate,                   /* True for xCreate, false for xConnect */
125735   sqlite3 *db,                    /* The SQLite database connection */
125736   void *pAux,                     /* Hash table containing tokenizers */
125737   int argc,                       /* Number of elements in argv array */
125738   const char * const *argv,       /* xCreate/xConnect argument array */
125739   sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
125740   char **pzErr                    /* Write any error message here */
125741 ){
125742   Fts3Hash *pHash = (Fts3Hash *)pAux;
125743   Fts3Table *p = 0;               /* Pointer to allocated vtab */
125744   int rc = SQLITE_OK;             /* Return code */
125745   int i;                          /* Iterator variable */
125746   int nByte;                      /* Size of allocation used for *p */
125747   int iCol;                       /* Column index */
125748   int nString = 0;                /* Bytes required to hold all column names */
125749   int nCol = 0;                   /* Number of columns in the FTS table */
125750   char *zCsr;                     /* Space for holding column names */
125751   int nDb;                        /* Bytes required to hold database name */
125752   int nName;                      /* Bytes required to hold table name */
125753   int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
125754   const char **aCol;              /* Array of column names */
125755   sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
125756 
125757   int nIndex;                     /* Size of aIndex[] array */
125758   struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
125759 
125760   /* The results of parsing supported FTS4 key=value options: */
125761   int bNoDocsize = 0;             /* True to omit %_docsize table */
125762   int bDescIdx = 0;               /* True to store descending indexes */
125763   char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
125764   char *zCompress = 0;            /* compress=? parameter (or NULL) */
125765   char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
125766   char *zContent = 0;             /* content=? parameter (or NULL) */
125767   char *zLanguageid = 0;          /* languageid=? parameter (or NULL) */
125768   char **azNotindexed = 0;        /* The set of notindexed= columns */
125769   int nNotindexed = 0;            /* Size of azNotindexed[] array */
125770 
125771   assert( strlen(argv[0])==4 );
125772   assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
125773        || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
125774   );
125775 
125776   nDb = (int)strlen(argv[1]) + 1;
125777   nName = (int)strlen(argv[2]) + 1;
125778 
125779   nByte = sizeof(const char *) * (argc-2);
125780   aCol = (const char **)sqlite3_malloc(nByte);
125781   if( aCol ){
125782     memset((void*)aCol, 0, nByte);
125783     azNotindexed = (char **)sqlite3_malloc(nByte);
125784   }
125785   if( azNotindexed ){
125786     memset(azNotindexed, 0, nByte);
125787   }
125788   if( !aCol || !azNotindexed ){
125789     rc = SQLITE_NOMEM;
125790     goto fts3_init_out;
125791   }
125792 
125793   /* Loop through all of the arguments passed by the user to the FTS3/4
125794   ** module (i.e. all the column names and special arguments). This loop
125795   ** does the following:
125796   **
125797   **   + Figures out the number of columns the FTSX table will have, and
125798   **     the number of bytes of space that must be allocated to store copies
125799   **     of the column names.
125800   **
125801   **   + If there is a tokenizer specification included in the arguments,
125802   **     initializes the tokenizer pTokenizer.
125803   */
125804   for(i=3; rc==SQLITE_OK && i<argc; i++){
125805     char const *z = argv[i];
125806     int nKey;
125807     char *zVal;
125808 
125809     /* Check if this is a tokenizer specification */
125810     if( !pTokenizer
125811      && strlen(z)>8
125812      && 0==sqlite3_strnicmp(z, "tokenize", 8)
125813      && 0==sqlite3Fts3IsIdChar(z[8])
125814     ){
125815       rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
125816     }
125817 
125818     /* Check if it is an FTS4 special argument. */
125819     else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
125820       struct Fts4Option {
125821         const char *zOpt;
125822         int nOpt;
125823       } aFts4Opt[] = {
125824         { "matchinfo",   9 },     /* 0 -> MATCHINFO */
125825         { "prefix",      6 },     /* 1 -> PREFIX */
125826         { "compress",    8 },     /* 2 -> COMPRESS */
125827         { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
125828         { "order",       5 },     /* 4 -> ORDER */
125829         { "content",     7 },     /* 5 -> CONTENT */
125830         { "languageid", 10 },     /* 6 -> LANGUAGEID */
125831         { "notindexed", 10 }      /* 7 -> NOTINDEXED */
125832       };
125833 
125834       int iOpt;
125835       if( !zVal ){
125836         rc = SQLITE_NOMEM;
125837       }else{
125838         for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
125839           struct Fts4Option *pOp = &aFts4Opt[iOpt];
125840           if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
125841             break;
125842           }
125843         }
125844         if( iOpt==SizeofArray(aFts4Opt) ){
125845           *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
125846           rc = SQLITE_ERROR;
125847         }else{
125848           switch( iOpt ){
125849             case 0:               /* MATCHINFO */
125850               if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
125851                 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
125852                 rc = SQLITE_ERROR;
125853               }
125854               bNoDocsize = 1;
125855               break;
125856 
125857             case 1:               /* PREFIX */
125858               sqlite3_free(zPrefix);
125859               zPrefix = zVal;
125860               zVal = 0;
125861               break;
125862 
125863             case 2:               /* COMPRESS */
125864               sqlite3_free(zCompress);
125865               zCompress = zVal;
125866               zVal = 0;
125867               break;
125868 
125869             case 3:               /* UNCOMPRESS */
125870               sqlite3_free(zUncompress);
125871               zUncompress = zVal;
125872               zVal = 0;
125873               break;
125874 
125875             case 4:               /* ORDER */
125876               if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
125877                && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
125878               ){
125879                 *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
125880                 rc = SQLITE_ERROR;
125881               }
125882               bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
125883               break;
125884 
125885             case 5:              /* CONTENT */
125886               sqlite3_free(zContent);
125887               zContent = zVal;
125888               zVal = 0;
125889               break;
125890 
125891             case 6:              /* LANGUAGEID */
125892               assert( iOpt==6 );
125893               sqlite3_free(zLanguageid);
125894               zLanguageid = zVal;
125895               zVal = 0;
125896               break;
125897 
125898             case 7:              /* NOTINDEXED */
125899               azNotindexed[nNotindexed++] = zVal;
125900               zVal = 0;
125901               break;
125902           }
125903         }
125904         sqlite3_free(zVal);
125905       }
125906     }
125907 
125908     /* Otherwise, the argument is a column name. */
125909     else {
125910       nString += (int)(strlen(z) + 1);
125911       aCol[nCol++] = z;
125912     }
125913   }
125914 
125915   /* If a content=xxx option was specified, the following:
125916   **
125917   **   1. Ignore any compress= and uncompress= options.
125918   **
125919   **   2. If no column names were specified as part of the CREATE VIRTUAL
125920   **      TABLE statement, use all columns from the content table.
125921   */
125922   if( rc==SQLITE_OK && zContent ){
125923     sqlite3_free(zCompress);
125924     sqlite3_free(zUncompress);
125925     zCompress = 0;
125926     zUncompress = 0;
125927     if( nCol==0 ){
125928       sqlite3_free((void*)aCol);
125929       aCol = 0;
125930       rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
125931 
125932       /* If a languageid= option was specified, remove the language id
125933       ** column from the aCol[] array. */
125934       if( rc==SQLITE_OK && zLanguageid ){
125935         int j;
125936         for(j=0; j<nCol; j++){
125937           if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
125938             int k;
125939             for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
125940             nCol--;
125941             break;
125942           }
125943         }
125944       }
125945     }
125946   }
125947   if( rc!=SQLITE_OK ) goto fts3_init_out;
125948 
125949   if( nCol==0 ){
125950     assert( nString==0 );
125951     aCol[0] = "content";
125952     nString = 8;
125953     nCol = 1;
125954   }
125955 
125956   if( pTokenizer==0 ){
125957     rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
125958     if( rc!=SQLITE_OK ) goto fts3_init_out;
125959   }
125960   assert( pTokenizer );
125961 
125962   rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
125963   if( rc==SQLITE_ERROR ){
125964     assert( zPrefix );
125965     *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
125966   }
125967   if( rc!=SQLITE_OK ) goto fts3_init_out;
125968 
125969   /* Allocate and populate the Fts3Table structure. */
125970   nByte = sizeof(Fts3Table) +                  /* Fts3Table */
125971           nCol * sizeof(char *) +              /* azColumn */
125972           nIndex * sizeof(struct Fts3Index) +  /* aIndex */
125973           nCol * sizeof(u8) +                  /* abNotindexed */
125974           nName +                              /* zName */
125975           nDb +                                /* zDb */
125976           nString;                             /* Space for azColumn strings */
125977   p = (Fts3Table*)sqlite3_malloc(nByte);
125978   if( p==0 ){
125979     rc = SQLITE_NOMEM;
125980     goto fts3_init_out;
125981   }
125982   memset(p, 0, nByte);
125983   p->db = db;
125984   p->nColumn = nCol;
125985   p->nPendingData = 0;
125986   p->azColumn = (char **)&p[1];
125987   p->pTokenizer = pTokenizer;
125988   p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
125989   p->bHasDocsize = (isFts4 && bNoDocsize==0);
125990   p->bHasStat = isFts4;
125991   p->bFts4 = isFts4;
125992   p->bDescIdx = bDescIdx;
125993   p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
125994   p->zContentTbl = zContent;
125995   p->zLanguageid = zLanguageid;
125996   zContent = 0;
125997   zLanguageid = 0;
125998   TESTONLY( p->inTransaction = -1 );
125999   TESTONLY( p->mxSavepoint = -1 );
126000 
126001   p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
126002   memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
126003   p->nIndex = nIndex;
126004   for(i=0; i<nIndex; i++){
126005     fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
126006   }
126007   p->abNotindexed = (u8 *)&p->aIndex[nIndex];
126008 
126009   /* Fill in the zName and zDb fields of the vtab structure. */
126010   zCsr = (char *)&p->abNotindexed[nCol];
126011   p->zName = zCsr;
126012   memcpy(zCsr, argv[2], nName);
126013   zCsr += nName;
126014   p->zDb = zCsr;
126015   memcpy(zCsr, argv[1], nDb);
126016   zCsr += nDb;
126017 
126018   /* Fill in the azColumn array */
126019   for(iCol=0; iCol<nCol; iCol++){
126020     char *z;
126021     int n = 0;
126022     z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
126023     memcpy(zCsr, z, n);
126024     zCsr[n] = '\0';
126025     sqlite3Fts3Dequote(zCsr);
126026     p->azColumn[iCol] = zCsr;
126027     zCsr += n+1;
126028     assert( zCsr <= &((char *)p)[nByte] );
126029   }
126030 
126031   /* Fill in the abNotindexed array */
126032   for(iCol=0; iCol<nCol; iCol++){
126033     int n = (int)strlen(p->azColumn[iCol]);
126034     for(i=0; i<nNotindexed; i++){
126035       char *zNot = azNotindexed[i];
126036       if( zNot && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) ){
126037         p->abNotindexed[iCol] = 1;
126038         sqlite3_free(zNot);
126039         azNotindexed[i] = 0;
126040       }
126041     }
126042   }
126043   for(i=0; i<nNotindexed; i++){
126044     if( azNotindexed[i] ){
126045       *pzErr = sqlite3_mprintf("no such column: %s", azNotindexed[i]);
126046       rc = SQLITE_ERROR;
126047     }
126048   }
126049 
126050   if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
126051     char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
126052     rc = SQLITE_ERROR;
126053     *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
126054   }
126055   p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
126056   p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
126057   if( rc!=SQLITE_OK ) goto fts3_init_out;
126058 
126059   /* If this is an xCreate call, create the underlying tables in the
126060   ** database. TODO: For xConnect(), it could verify that said tables exist.
126061   */
126062   if( isCreate ){
126063     rc = fts3CreateTables(p);
126064   }
126065 
126066   /* Check to see if a legacy fts3 table has been "upgraded" by the
126067   ** addition of a %_stat table so that it can use incremental merge.
126068   */
126069   if( !isFts4 && !isCreate ){
126070     int rc2 = SQLITE_OK;
126071     fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
126072                p->zDb, p->zName);
126073     if( rc2==SQLITE_OK ) p->bHasStat = 1;
126074   }
126075 
126076   /* Figure out the page-size for the database. This is required in order to
126077   ** estimate the cost of loading large doclists from the database.  */
126078   fts3DatabasePageSize(&rc, p);
126079   p->nNodeSize = p->nPgsz-35;
126080 
126081   /* Declare the table schema to SQLite. */
126082   fts3DeclareVtab(&rc, p);
126083 
126084 fts3_init_out:
126085   sqlite3_free(zPrefix);
126086   sqlite3_free(aIndex);
126087   sqlite3_free(zCompress);
126088   sqlite3_free(zUncompress);
126089   sqlite3_free(zContent);
126090   sqlite3_free(zLanguageid);
126091   for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
126092   sqlite3_free((void *)aCol);
126093   sqlite3_free((void *)azNotindexed);
126094   if( rc!=SQLITE_OK ){
126095     if( p ){
126096       fts3DisconnectMethod((sqlite3_vtab *)p);
126097     }else if( pTokenizer ){
126098       pTokenizer->pModule->xDestroy(pTokenizer);
126099     }
126100   }else{
126101     assert( p->pSegments==0 );
126102     *ppVTab = &p->base;
126103   }
126104   return rc;
126105 }
126106 
126107 /*
126108 ** The xConnect() and xCreate() methods for the virtual table. All the
126109 ** work is done in function fts3InitVtab().
126110 */
126111 static int fts3ConnectMethod(
126112   sqlite3 *db,                    /* Database connection */
126113   void *pAux,                     /* Pointer to tokenizer hash table */
126114   int argc,                       /* Number of elements in argv array */
126115   const char * const *argv,       /* xCreate/xConnect argument array */
126116   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
126117   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
126118 ){
126119   return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
126120 }
126121 static int fts3CreateMethod(
126122   sqlite3 *db,                    /* Database connection */
126123   void *pAux,                     /* Pointer to tokenizer hash table */
126124   int argc,                       /* Number of elements in argv array */
126125   const char * const *argv,       /* xCreate/xConnect argument array */
126126   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
126127   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
126128 ){
126129   return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
126130 }
126131 
126132 /*
126133 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
126134 ** extension is currently being used by a version of SQLite too old to
126135 ** support estimatedRows. In that case this function is a no-op.
126136 */
126137 static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
126138 #if SQLITE_VERSION_NUMBER>=3008002
126139   if( sqlite3_libversion_number()>=3008002 ){
126140     pIdxInfo->estimatedRows = nRow;
126141   }
126142 #endif
126143 }
126144 
126145 /*
126146 ** Implementation of the xBestIndex method for FTS3 tables. There
126147 ** are three possible strategies, in order of preference:
126148 **
126149 **   1. Direct lookup by rowid or docid.
126150 **   2. Full-text search using a MATCH operator on a non-docid column.
126151 **   3. Linear scan of %_content table.
126152 */
126153 static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
126154   Fts3Table *p = (Fts3Table *)pVTab;
126155   int i;                          /* Iterator variable */
126156   int iCons = -1;                 /* Index of constraint to use */
126157 
126158   int iLangidCons = -1;           /* Index of langid=x constraint, if present */
126159   int iDocidGe = -1;              /* Index of docid>=x constraint, if present */
126160   int iDocidLe = -1;              /* Index of docid<=x constraint, if present */
126161   int iIdx;
126162 
126163   /* By default use a full table scan. This is an expensive option,
126164   ** so search through the constraints to see if a more efficient
126165   ** strategy is possible.
126166   */
126167   pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
126168   pInfo->estimatedCost = 5000000;
126169   for(i=0; i<pInfo->nConstraint; i++){
126170     int bDocid;                 /* True if this constraint is on docid */
126171     struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
126172     if( pCons->usable==0 ){
126173       if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
126174         /* There exists an unusable MATCH constraint. This means that if
126175         ** the planner does elect to use the results of this call as part
126176         ** of the overall query plan the user will see an "unable to use
126177         ** function MATCH in the requested context" error. To discourage
126178         ** this, return a very high cost here.  */
126179         pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
126180         pInfo->estimatedCost = 1e50;
126181         fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
126182         return SQLITE_OK;
126183       }
126184       continue;
126185     }
126186 
126187     bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
126188 
126189     /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
126190     if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
126191       pInfo->idxNum = FTS3_DOCID_SEARCH;
126192       pInfo->estimatedCost = 1.0;
126193       iCons = i;
126194     }
126195 
126196     /* A MATCH constraint. Use a full-text search.
126197     **
126198     ** If there is more than one MATCH constraint available, use the first
126199     ** one encountered. If there is both a MATCH constraint and a direct
126200     ** rowid/docid lookup, prefer the MATCH strategy. This is done even
126201     ** though the rowid/docid lookup is faster than a MATCH query, selecting
126202     ** it would lead to an "unable to use function MATCH in the requested
126203     ** context" error.
126204     */
126205     if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
126206      && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
126207     ){
126208       pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
126209       pInfo->estimatedCost = 2.0;
126210       iCons = i;
126211     }
126212 
126213     /* Equality constraint on the langid column */
126214     if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
126215      && pCons->iColumn==p->nColumn + 2
126216     ){
126217       iLangidCons = i;
126218     }
126219 
126220     if( bDocid ){
126221       switch( pCons->op ){
126222         case SQLITE_INDEX_CONSTRAINT_GE:
126223         case SQLITE_INDEX_CONSTRAINT_GT:
126224           iDocidGe = i;
126225           break;
126226 
126227         case SQLITE_INDEX_CONSTRAINT_LE:
126228         case SQLITE_INDEX_CONSTRAINT_LT:
126229           iDocidLe = i;
126230           break;
126231       }
126232     }
126233   }
126234 
126235   iIdx = 1;
126236   if( iCons>=0 ){
126237     pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
126238     pInfo->aConstraintUsage[iCons].omit = 1;
126239   }
126240   if( iLangidCons>=0 ){
126241     pInfo->idxNum |= FTS3_HAVE_LANGID;
126242     pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
126243   }
126244   if( iDocidGe>=0 ){
126245     pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
126246     pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
126247   }
126248   if( iDocidLe>=0 ){
126249     pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
126250     pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
126251   }
126252 
126253   /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
126254   ** docid) order. Both ascending and descending are possible.
126255   */
126256   if( pInfo->nOrderBy==1 ){
126257     struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
126258     if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
126259       if( pOrder->desc ){
126260         pInfo->idxStr = "DESC";
126261       }else{
126262         pInfo->idxStr = "ASC";
126263       }
126264       pInfo->orderByConsumed = 1;
126265     }
126266   }
126267 
126268   assert( p->pSegments==0 );
126269   return SQLITE_OK;
126270 }
126271 
126272 /*
126273 ** Implementation of xOpen method.
126274 */
126275 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
126276   sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
126277 
126278   UNUSED_PARAMETER(pVTab);
126279 
126280   /* Allocate a buffer large enough for an Fts3Cursor structure. If the
126281   ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
126282   ** if the allocation fails, return SQLITE_NOMEM.
126283   */
126284   *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
126285   if( !pCsr ){
126286     return SQLITE_NOMEM;
126287   }
126288   memset(pCsr, 0, sizeof(Fts3Cursor));
126289   return SQLITE_OK;
126290 }
126291 
126292 /*
126293 ** Close the cursor.  For additional information see the documentation
126294 ** on the xClose method of the virtual table interface.
126295 */
126296 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
126297   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
126298   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
126299   sqlite3_finalize(pCsr->pStmt);
126300   sqlite3Fts3ExprFree(pCsr->pExpr);
126301   sqlite3Fts3FreeDeferredTokens(pCsr);
126302   sqlite3_free(pCsr->aDoclist);
126303   sqlite3_free(pCsr->aMatchinfo);
126304   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
126305   sqlite3_free(pCsr);
126306   return SQLITE_OK;
126307 }
126308 
126309 /*
126310 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
126311 ** compose and prepare an SQL statement of the form:
126312 **
126313 **    "SELECT <columns> FROM %_content WHERE rowid = ?"
126314 **
126315 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
126316 ** it. If an error occurs, return an SQLite error code.
126317 **
126318 ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
126319 */
126320 static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
126321   int rc = SQLITE_OK;
126322   if( pCsr->pStmt==0 ){
126323     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
126324     char *zSql;
126325     zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
126326     if( !zSql ) return SQLITE_NOMEM;
126327     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
126328     sqlite3_free(zSql);
126329   }
126330   *ppStmt = pCsr->pStmt;
126331   return rc;
126332 }
126333 
126334 /*
126335 ** Position the pCsr->pStmt statement so that it is on the row
126336 ** of the %_content table that contains the last match.  Return
126337 ** SQLITE_OK on success.
126338 */
126339 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
126340   int rc = SQLITE_OK;
126341   if( pCsr->isRequireSeek ){
126342     sqlite3_stmt *pStmt = 0;
126343 
126344     rc = fts3CursorSeekStmt(pCsr, &pStmt);
126345     if( rc==SQLITE_OK ){
126346       sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
126347       pCsr->isRequireSeek = 0;
126348       if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
126349         return SQLITE_OK;
126350       }else{
126351         rc = sqlite3_reset(pCsr->pStmt);
126352         if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
126353           /* If no row was found and no error has occurred, then the %_content
126354           ** table is missing a row that is present in the full-text index.
126355           ** The data structures are corrupt.  */
126356           rc = FTS_CORRUPT_VTAB;
126357           pCsr->isEof = 1;
126358         }
126359       }
126360     }
126361   }
126362 
126363   if( rc!=SQLITE_OK && pContext ){
126364     sqlite3_result_error_code(pContext, rc);
126365   }
126366   return rc;
126367 }
126368 
126369 /*
126370 ** This function is used to process a single interior node when searching
126371 ** a b-tree for a term or term prefix. The node data is passed to this
126372 ** function via the zNode/nNode parameters. The term to search for is
126373 ** passed in zTerm/nTerm.
126374 **
126375 ** If piFirst is not NULL, then this function sets *piFirst to the blockid
126376 ** of the child node that heads the sub-tree that may contain the term.
126377 **
126378 ** If piLast is not NULL, then *piLast is set to the right-most child node
126379 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
126380 ** a prefix.
126381 **
126382 ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
126383 */
126384 static int fts3ScanInteriorNode(
126385   const char *zTerm,              /* Term to select leaves for */
126386   int nTerm,                      /* Size of term zTerm in bytes */
126387   const char *zNode,              /* Buffer containing segment interior node */
126388   int nNode,                      /* Size of buffer at zNode */
126389   sqlite3_int64 *piFirst,         /* OUT: Selected child node */
126390   sqlite3_int64 *piLast           /* OUT: Selected child node */
126391 ){
126392   int rc = SQLITE_OK;             /* Return code */
126393   const char *zCsr = zNode;       /* Cursor to iterate through node */
126394   const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
126395   char *zBuffer = 0;              /* Buffer to load terms into */
126396   int nAlloc = 0;                 /* Size of allocated buffer */
126397   int isFirstTerm = 1;            /* True when processing first term on page */
126398   sqlite3_int64 iChild;           /* Block id of child node to descend to */
126399 
126400   /* Skip over the 'height' varint that occurs at the start of every
126401   ** interior node. Then load the blockid of the left-child of the b-tree
126402   ** node into variable iChild.
126403   **
126404   ** Even if the data structure on disk is corrupted, this (reading two
126405   ** varints from the buffer) does not risk an overread. If zNode is a
126406   ** root node, then the buffer comes from a SELECT statement. SQLite does
126407   ** not make this guarantee explicitly, but in practice there are always
126408   ** either more than 20 bytes of allocated space following the nNode bytes of
126409   ** contents, or two zero bytes. Or, if the node is read from the %_segments
126410   ** table, then there are always 20 bytes of zeroed padding following the
126411   ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
126412   */
126413   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
126414   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
126415   if( zCsr>zEnd ){
126416     return FTS_CORRUPT_VTAB;
126417   }
126418 
126419   while( zCsr<zEnd && (piFirst || piLast) ){
126420     int cmp;                      /* memcmp() result */
126421     int nSuffix;                  /* Size of term suffix */
126422     int nPrefix = 0;              /* Size of term prefix */
126423     int nBuffer;                  /* Total term size */
126424 
126425     /* Load the next term on the node into zBuffer. Use realloc() to expand
126426     ** the size of zBuffer if required.  */
126427     if( !isFirstTerm ){
126428       zCsr += fts3GetVarint32(zCsr, &nPrefix);
126429     }
126430     isFirstTerm = 0;
126431     zCsr += fts3GetVarint32(zCsr, &nSuffix);
126432 
126433     if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
126434       rc = FTS_CORRUPT_VTAB;
126435       goto finish_scan;
126436     }
126437     if( nPrefix+nSuffix>nAlloc ){
126438       char *zNew;
126439       nAlloc = (nPrefix+nSuffix) * 2;
126440       zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
126441       if( !zNew ){
126442         rc = SQLITE_NOMEM;
126443         goto finish_scan;
126444       }
126445       zBuffer = zNew;
126446     }
126447     assert( zBuffer );
126448     memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
126449     nBuffer = nPrefix + nSuffix;
126450     zCsr += nSuffix;
126451 
126452     /* Compare the term we are searching for with the term just loaded from
126453     ** the interior node. If the specified term is greater than or equal
126454     ** to the term from the interior node, then all terms on the sub-tree
126455     ** headed by node iChild are smaller than zTerm. No need to search
126456     ** iChild.
126457     **
126458     ** If the interior node term is larger than the specified term, then
126459     ** the tree headed by iChild may contain the specified term.
126460     */
126461     cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
126462     if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
126463       *piFirst = iChild;
126464       piFirst = 0;
126465     }
126466 
126467     if( piLast && cmp<0 ){
126468       *piLast = iChild;
126469       piLast = 0;
126470     }
126471 
126472     iChild++;
126473   };
126474 
126475   if( piFirst ) *piFirst = iChild;
126476   if( piLast ) *piLast = iChild;
126477 
126478  finish_scan:
126479   sqlite3_free(zBuffer);
126480   return rc;
126481 }
126482 
126483 
126484 /*
126485 ** The buffer pointed to by argument zNode (size nNode bytes) contains an
126486 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
126487 ** contains a term. This function searches the sub-tree headed by the zNode
126488 ** node for the range of leaf nodes that may contain the specified term
126489 ** or terms for which the specified term is a prefix.
126490 **
126491 ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
126492 ** left-most leaf node in the tree that may contain the specified term.
126493 ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
126494 ** right-most leaf node that may contain a term for which the specified
126495 ** term is a prefix.
126496 **
126497 ** It is possible that the range of returned leaf nodes does not contain
126498 ** the specified term or any terms for which it is a prefix. However, if the
126499 ** segment does contain any such terms, they are stored within the identified
126500 ** range. Because this function only inspects interior segment nodes (and
126501 ** never loads leaf nodes into memory), it is not possible to be sure.
126502 **
126503 ** If an error occurs, an error code other than SQLITE_OK is returned.
126504 */
126505 static int fts3SelectLeaf(
126506   Fts3Table *p,                   /* Virtual table handle */
126507   const char *zTerm,              /* Term to select leaves for */
126508   int nTerm,                      /* Size of term zTerm in bytes */
126509   const char *zNode,              /* Buffer containing segment interior node */
126510   int nNode,                      /* Size of buffer at zNode */
126511   sqlite3_int64 *piLeaf,          /* Selected leaf node */
126512   sqlite3_int64 *piLeaf2          /* Selected leaf node */
126513 ){
126514   int rc;                         /* Return code */
126515   int iHeight;                    /* Height of this node in tree */
126516 
126517   assert( piLeaf || piLeaf2 );
126518 
126519   fts3GetVarint32(zNode, &iHeight);
126520   rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
126521   assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
126522 
126523   if( rc==SQLITE_OK && iHeight>1 ){
126524     char *zBlob = 0;              /* Blob read from %_segments table */
126525     int nBlob;                    /* Size of zBlob in bytes */
126526 
126527     if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
126528       rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
126529       if( rc==SQLITE_OK ){
126530         rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
126531       }
126532       sqlite3_free(zBlob);
126533       piLeaf = 0;
126534       zBlob = 0;
126535     }
126536 
126537     if( rc==SQLITE_OK ){
126538       rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
126539     }
126540     if( rc==SQLITE_OK ){
126541       rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
126542     }
126543     sqlite3_free(zBlob);
126544   }
126545 
126546   return rc;
126547 }
126548 
126549 /*
126550 ** This function is used to create delta-encoded serialized lists of FTS3
126551 ** varints. Each call to this function appends a single varint to a list.
126552 */
126553 static void fts3PutDeltaVarint(
126554   char **pp,                      /* IN/OUT: Output pointer */
126555   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
126556   sqlite3_int64 iVal              /* Write this value to the list */
126557 ){
126558   assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
126559   *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
126560   *piPrev = iVal;
126561 }
126562 
126563 /*
126564 ** When this function is called, *ppPoslist is assumed to point to the
126565 ** start of a position-list. After it returns, *ppPoslist points to the
126566 ** first byte after the position-list.
126567 **
126568 ** A position list is list of positions (delta encoded) and columns for
126569 ** a single document record of a doclist.  So, in other words, this
126570 ** routine advances *ppPoslist so that it points to the next docid in
126571 ** the doclist, or to the first byte past the end of the doclist.
126572 **
126573 ** If pp is not NULL, then the contents of the position list are copied
126574 ** to *pp. *pp is set to point to the first byte past the last byte copied
126575 ** before this function returns.
126576 */
126577 static void fts3PoslistCopy(char **pp, char **ppPoslist){
126578   char *pEnd = *ppPoslist;
126579   char c = 0;
126580 
126581   /* The end of a position list is marked by a zero encoded as an FTS3
126582   ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
126583   ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
126584   ** of some other, multi-byte, value.
126585   **
126586   ** The following while-loop moves pEnd to point to the first byte that is not
126587   ** immediately preceded by a byte with the 0x80 bit set. Then increments
126588   ** pEnd once more so that it points to the byte immediately following the
126589   ** last byte in the position-list.
126590   */
126591   while( *pEnd | c ){
126592     c = *pEnd++ & 0x80;
126593     testcase( c!=0 && (*pEnd)==0 );
126594   }
126595   pEnd++;  /* Advance past the POS_END terminator byte */
126596 
126597   if( pp ){
126598     int n = (int)(pEnd - *ppPoslist);
126599     char *p = *pp;
126600     memcpy(p, *ppPoslist, n);
126601     p += n;
126602     *pp = p;
126603   }
126604   *ppPoslist = pEnd;
126605 }
126606 
126607 /*
126608 ** When this function is called, *ppPoslist is assumed to point to the
126609 ** start of a column-list. After it returns, *ppPoslist points to the
126610 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
126611 **
126612 ** A column-list is list of delta-encoded positions for a single column
126613 ** within a single document within a doclist.
126614 **
126615 ** The column-list is terminated either by a POS_COLUMN varint (1) or
126616 ** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
126617 ** the POS_COLUMN or POS_END that terminates the column-list.
126618 **
126619 ** If pp is not NULL, then the contents of the column-list are copied
126620 ** to *pp. *pp is set to point to the first byte past the last byte copied
126621 ** before this function returns.  The POS_COLUMN or POS_END terminator
126622 ** is not copied into *pp.
126623 */
126624 static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
126625   char *pEnd = *ppPoslist;
126626   char c = 0;
126627 
126628   /* A column-list is terminated by either a 0x01 or 0x00 byte that is
126629   ** not part of a multi-byte varint.
126630   */
126631   while( 0xFE & (*pEnd | c) ){
126632     c = *pEnd++ & 0x80;
126633     testcase( c!=0 && ((*pEnd)&0xfe)==0 );
126634   }
126635   if( pp ){
126636     int n = (int)(pEnd - *ppPoslist);
126637     char *p = *pp;
126638     memcpy(p, *ppPoslist, n);
126639     p += n;
126640     *pp = p;
126641   }
126642   *ppPoslist = pEnd;
126643 }
126644 
126645 /*
126646 ** Value used to signify the end of an position-list. This is safe because
126647 ** it is not possible to have a document with 2^31 terms.
126648 */
126649 #define POSITION_LIST_END 0x7fffffff
126650 
126651 /*
126652 ** This function is used to help parse position-lists. When this function is
126653 ** called, *pp may point to the start of the next varint in the position-list
126654 ** being parsed, or it may point to 1 byte past the end of the position-list
126655 ** (in which case **pp will be a terminator bytes POS_END (0) or
126656 ** (1)).
126657 **
126658 ** If *pp points past the end of the current position-list, set *pi to
126659 ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
126660 ** increment the current value of *pi by the value read, and set *pp to
126661 ** point to the next value before returning.
126662 **
126663 ** Before calling this routine *pi must be initialized to the value of
126664 ** the previous position, or zero if we are reading the first position
126665 ** in the position-list.  Because positions are delta-encoded, the value
126666 ** of the previous position is needed in order to compute the value of
126667 ** the next position.
126668 */
126669 static void fts3ReadNextPos(
126670   char **pp,                    /* IN/OUT: Pointer into position-list buffer */
126671   sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
126672 ){
126673   if( (**pp)&0xFE ){
126674     fts3GetDeltaVarint(pp, pi);
126675     *pi -= 2;
126676   }else{
126677     *pi = POSITION_LIST_END;
126678   }
126679 }
126680 
126681 /*
126682 ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
126683 ** the value of iCol encoded as a varint to *pp.   This will start a new
126684 ** column list.
126685 **
126686 ** Set *pp to point to the byte just after the last byte written before
126687 ** returning (do not modify it if iCol==0). Return the total number of bytes
126688 ** written (0 if iCol==0).
126689 */
126690 static int fts3PutColNumber(char **pp, int iCol){
126691   int n = 0;                      /* Number of bytes written */
126692   if( iCol ){
126693     char *p = *pp;                /* Output pointer */
126694     n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
126695     *p = 0x01;
126696     *pp = &p[n];
126697   }
126698   return n;
126699 }
126700 
126701 /*
126702 ** Compute the union of two position lists.  The output written
126703 ** into *pp contains all positions of both *pp1 and *pp2 in sorted
126704 ** order and with any duplicates removed.  All pointers are
126705 ** updated appropriately.   The caller is responsible for insuring
126706 ** that there is enough space in *pp to hold the complete output.
126707 */
126708 static void fts3PoslistMerge(
126709   char **pp,                      /* Output buffer */
126710   char **pp1,                     /* Left input list */
126711   char **pp2                      /* Right input list */
126712 ){
126713   char *p = *pp;
126714   char *p1 = *pp1;
126715   char *p2 = *pp2;
126716 
126717   while( *p1 || *p2 ){
126718     int iCol1;         /* The current column index in pp1 */
126719     int iCol2;         /* The current column index in pp2 */
126720 
126721     if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1);
126722     else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
126723     else iCol1 = 0;
126724 
126725     if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2);
126726     else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
126727     else iCol2 = 0;
126728 
126729     if( iCol1==iCol2 ){
126730       sqlite3_int64 i1 = 0;       /* Last position from pp1 */
126731       sqlite3_int64 i2 = 0;       /* Last position from pp2 */
126732       sqlite3_int64 iPrev = 0;
126733       int n = fts3PutColNumber(&p, iCol1);
126734       p1 += n;
126735       p2 += n;
126736 
126737       /* At this point, both p1 and p2 point to the start of column-lists
126738       ** for the same column (the column with index iCol1 and iCol2).
126739       ** A column-list is a list of non-negative delta-encoded varints, each
126740       ** incremented by 2 before being stored. Each list is terminated by a
126741       ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
126742       ** and writes the results to buffer p. p is left pointing to the byte
126743       ** after the list written. No terminator (POS_END or POS_COLUMN) is
126744       ** written to the output.
126745       */
126746       fts3GetDeltaVarint(&p1, &i1);
126747       fts3GetDeltaVarint(&p2, &i2);
126748       do {
126749         fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
126750         iPrev -= 2;
126751         if( i1==i2 ){
126752           fts3ReadNextPos(&p1, &i1);
126753           fts3ReadNextPos(&p2, &i2);
126754         }else if( i1<i2 ){
126755           fts3ReadNextPos(&p1, &i1);
126756         }else{
126757           fts3ReadNextPos(&p2, &i2);
126758         }
126759       }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
126760     }else if( iCol1<iCol2 ){
126761       p1 += fts3PutColNumber(&p, iCol1);
126762       fts3ColumnlistCopy(&p, &p1);
126763     }else{
126764       p2 += fts3PutColNumber(&p, iCol2);
126765       fts3ColumnlistCopy(&p, &p2);
126766     }
126767   }
126768 
126769   *p++ = POS_END;
126770   *pp = p;
126771   *pp1 = p1 + 1;
126772   *pp2 = p2 + 1;
126773 }
126774 
126775 /*
126776 ** This function is used to merge two position lists into one. When it is
126777 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
126778 ** the part of a doclist that follows each document id. For example, if a row
126779 ** contains:
126780 **
126781 **     'a b c'|'x y z'|'a b b a'
126782 **
126783 ** Then the position list for this row for token 'b' would consist of:
126784 **
126785 **     0x02 0x01 0x02 0x03 0x03 0x00
126786 **
126787 ** When this function returns, both *pp1 and *pp2 are left pointing to the
126788 ** byte following the 0x00 terminator of their respective position lists.
126789 **
126790 ** If isSaveLeft is 0, an entry is added to the output position list for
126791 ** each position in *pp2 for which there exists one or more positions in
126792 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
126793 ** when the *pp1 token appears before the *pp2 token, but not more than nToken
126794 ** slots before it.
126795 **
126796 ** e.g. nToken==1 searches for adjacent positions.
126797 */
126798 static int fts3PoslistPhraseMerge(
126799   char **pp,                      /* IN/OUT: Preallocated output buffer */
126800   int nToken,                     /* Maximum difference in token positions */
126801   int isSaveLeft,                 /* Save the left position */
126802   int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
126803   char **pp1,                     /* IN/OUT: Left input list */
126804   char **pp2                      /* IN/OUT: Right input list */
126805 ){
126806   char *p = *pp;
126807   char *p1 = *pp1;
126808   char *p2 = *pp2;
126809   int iCol1 = 0;
126810   int iCol2 = 0;
126811 
126812   /* Never set both isSaveLeft and isExact for the same invocation. */
126813   assert( isSaveLeft==0 || isExact==0 );
126814 
126815   assert( p!=0 && *p1!=0 && *p2!=0 );
126816   if( *p1==POS_COLUMN ){
126817     p1++;
126818     p1 += fts3GetVarint32(p1, &iCol1);
126819   }
126820   if( *p2==POS_COLUMN ){
126821     p2++;
126822     p2 += fts3GetVarint32(p2, &iCol2);
126823   }
126824 
126825   while( 1 ){
126826     if( iCol1==iCol2 ){
126827       char *pSave = p;
126828       sqlite3_int64 iPrev = 0;
126829       sqlite3_int64 iPos1 = 0;
126830       sqlite3_int64 iPos2 = 0;
126831 
126832       if( iCol1 ){
126833         *p++ = POS_COLUMN;
126834         p += sqlite3Fts3PutVarint(p, iCol1);
126835       }
126836 
126837       assert( *p1!=POS_END && *p1!=POS_COLUMN );
126838       assert( *p2!=POS_END && *p2!=POS_COLUMN );
126839       fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
126840       fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
126841 
126842       while( 1 ){
126843         if( iPos2==iPos1+nToken
126844          || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
126845         ){
126846           sqlite3_int64 iSave;
126847           iSave = isSaveLeft ? iPos1 : iPos2;
126848           fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
126849           pSave = 0;
126850           assert( p );
126851         }
126852         if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
126853           if( (*p2&0xFE)==0 ) break;
126854           fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
126855         }else{
126856           if( (*p1&0xFE)==0 ) break;
126857           fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
126858         }
126859       }
126860 
126861       if( pSave ){
126862         assert( pp && p );
126863         p = pSave;
126864       }
126865 
126866       fts3ColumnlistCopy(0, &p1);
126867       fts3ColumnlistCopy(0, &p2);
126868       assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
126869       if( 0==*p1 || 0==*p2 ) break;
126870 
126871       p1++;
126872       p1 += fts3GetVarint32(p1, &iCol1);
126873       p2++;
126874       p2 += fts3GetVarint32(p2, &iCol2);
126875     }
126876 
126877     /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
126878     ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
126879     ** end of the position list, or the 0x01 that precedes the next
126880     ** column-number in the position list.
126881     */
126882     else if( iCol1<iCol2 ){
126883       fts3ColumnlistCopy(0, &p1);
126884       if( 0==*p1 ) break;
126885       p1++;
126886       p1 += fts3GetVarint32(p1, &iCol1);
126887     }else{
126888       fts3ColumnlistCopy(0, &p2);
126889       if( 0==*p2 ) break;
126890       p2++;
126891       p2 += fts3GetVarint32(p2, &iCol2);
126892     }
126893   }
126894 
126895   fts3PoslistCopy(0, &p2);
126896   fts3PoslistCopy(0, &p1);
126897   *pp1 = p1;
126898   *pp2 = p2;
126899   if( *pp==p ){
126900     return 0;
126901   }
126902   *p++ = 0x00;
126903   *pp = p;
126904   return 1;
126905 }
126906 
126907 /*
126908 ** Merge two position-lists as required by the NEAR operator. The argument
126909 ** position lists correspond to the left and right phrases of an expression
126910 ** like:
126911 **
126912 **     "phrase 1" NEAR "phrase number 2"
126913 **
126914 ** Position list *pp1 corresponds to the left-hand side of the NEAR
126915 ** expression and *pp2 to the right. As usual, the indexes in the position
126916 ** lists are the offsets of the last token in each phrase (tokens "1" and "2"
126917 ** in the example above).
126918 **
126919 ** The output position list - written to *pp - is a copy of *pp2 with those
126920 ** entries that are not sufficiently NEAR entries in *pp1 removed.
126921 */
126922 static int fts3PoslistNearMerge(
126923   char **pp,                      /* Output buffer */
126924   char *aTmp,                     /* Temporary buffer space */
126925   int nRight,                     /* Maximum difference in token positions */
126926   int nLeft,                      /* Maximum difference in token positions */
126927   char **pp1,                     /* IN/OUT: Left input list */
126928   char **pp2                      /* IN/OUT: Right input list */
126929 ){
126930   char *p1 = *pp1;
126931   char *p2 = *pp2;
126932 
126933   char *pTmp1 = aTmp;
126934   char *pTmp2;
126935   char *aTmp2;
126936   int res = 1;
126937 
126938   fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
126939   aTmp2 = pTmp2 = pTmp1;
126940   *pp1 = p1;
126941   *pp2 = p2;
126942   fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
126943   if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
126944     fts3PoslistMerge(pp, &aTmp, &aTmp2);
126945   }else if( pTmp1!=aTmp ){
126946     fts3PoslistCopy(pp, &aTmp);
126947   }else if( pTmp2!=aTmp2 ){
126948     fts3PoslistCopy(pp, &aTmp2);
126949   }else{
126950     res = 0;
126951   }
126952 
126953   return res;
126954 }
126955 
126956 /*
126957 ** An instance of this function is used to merge together the (potentially
126958 ** large number of) doclists for each term that matches a prefix query.
126959 ** See function fts3TermSelectMerge() for details.
126960 */
126961 typedef struct TermSelect TermSelect;
126962 struct TermSelect {
126963   char *aaOutput[16];             /* Malloc'd output buffers */
126964   int anOutput[16];               /* Size each output buffer in bytes */
126965 };
126966 
126967 /*
126968 ** This function is used to read a single varint from a buffer. Parameter
126969 ** pEnd points 1 byte past the end of the buffer. When this function is
126970 ** called, if *pp points to pEnd or greater, then the end of the buffer
126971 ** has been reached. In this case *pp is set to 0 and the function returns.
126972 **
126973 ** If *pp does not point to or past pEnd, then a single varint is read
126974 ** from *pp. *pp is then set to point 1 byte past the end of the read varint.
126975 **
126976 ** If bDescIdx is false, the value read is added to *pVal before returning.
126977 ** If it is true, the value read is subtracted from *pVal before this
126978 ** function returns.
126979 */
126980 static void fts3GetDeltaVarint3(
126981   char **pp,                      /* IN/OUT: Point to read varint from */
126982   char *pEnd,                     /* End of buffer */
126983   int bDescIdx,                   /* True if docids are descending */
126984   sqlite3_int64 *pVal             /* IN/OUT: Integer value */
126985 ){
126986   if( *pp>=pEnd ){
126987     *pp = 0;
126988   }else{
126989     sqlite3_int64 iVal;
126990     *pp += sqlite3Fts3GetVarint(*pp, &iVal);
126991     if( bDescIdx ){
126992       *pVal -= iVal;
126993     }else{
126994       *pVal += iVal;
126995     }
126996   }
126997 }
126998 
126999 /*
127000 ** This function is used to write a single varint to a buffer. The varint
127001 ** is written to *pp. Before returning, *pp is set to point 1 byte past the
127002 ** end of the value written.
127003 **
127004 ** If *pbFirst is zero when this function is called, the value written to
127005 ** the buffer is that of parameter iVal.
127006 **
127007 ** If *pbFirst is non-zero when this function is called, then the value
127008 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
127009 ** (if bDescIdx is non-zero).
127010 **
127011 ** Before returning, this function always sets *pbFirst to 1 and *piPrev
127012 ** to the value of parameter iVal.
127013 */
127014 static void fts3PutDeltaVarint3(
127015   char **pp,                      /* IN/OUT: Output pointer */
127016   int bDescIdx,                   /* True for descending docids */
127017   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
127018   int *pbFirst,                   /* IN/OUT: True after first int written */
127019   sqlite3_int64 iVal              /* Write this value to the list */
127020 ){
127021   sqlite3_int64 iWrite;
127022   if( bDescIdx==0 || *pbFirst==0 ){
127023     iWrite = iVal - *piPrev;
127024   }else{
127025     iWrite = *piPrev - iVal;
127026   }
127027   assert( *pbFirst || *piPrev==0 );
127028   assert( *pbFirst==0 || iWrite>0 );
127029   *pp += sqlite3Fts3PutVarint(*pp, iWrite);
127030   *piPrev = iVal;
127031   *pbFirst = 1;
127032 }
127033 
127034 
127035 /*
127036 ** This macro is used by various functions that merge doclists. The two
127037 ** arguments are 64-bit docid values. If the value of the stack variable
127038 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
127039 ** Otherwise, (i2-i1).
127040 **
127041 ** Using this makes it easier to write code that can merge doclists that are
127042 ** sorted in either ascending or descending order.
127043 */
127044 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
127045 
127046 /*
127047 ** This function does an "OR" merge of two doclists (output contains all
127048 ** positions contained in either argument doclist). If the docids in the
127049 ** input doclists are sorted in ascending order, parameter bDescDoclist
127050 ** should be false. If they are sorted in ascending order, it should be
127051 ** passed a non-zero value.
127052 **
127053 ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
127054 ** containing the output doclist and SQLITE_OK is returned. In this case
127055 ** *pnOut is set to the number of bytes in the output doclist.
127056 **
127057 ** If an error occurs, an SQLite error code is returned. The output values
127058 ** are undefined in this case.
127059 */
127060 static int fts3DoclistOrMerge(
127061   int bDescDoclist,               /* True if arguments are desc */
127062   char *a1, int n1,               /* First doclist */
127063   char *a2, int n2,               /* Second doclist */
127064   char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
127065 ){
127066   sqlite3_int64 i1 = 0;
127067   sqlite3_int64 i2 = 0;
127068   sqlite3_int64 iPrev = 0;
127069   char *pEnd1 = &a1[n1];
127070   char *pEnd2 = &a2[n2];
127071   char *p1 = a1;
127072   char *p2 = a2;
127073   char *p;
127074   char *aOut;
127075   int bFirstOut = 0;
127076 
127077   *paOut = 0;
127078   *pnOut = 0;
127079 
127080   /* Allocate space for the output. Both the input and output doclists
127081   ** are delta encoded. If they are in ascending order (bDescDoclist==0),
127082   ** then the first docid in each list is simply encoded as a varint. For
127083   ** each subsequent docid, the varint stored is the difference between the
127084   ** current and previous docid (a positive number - since the list is in
127085   ** ascending order).
127086   **
127087   ** The first docid written to the output is therefore encoded using the
127088   ** same number of bytes as it is in whichever of the input lists it is
127089   ** read from. And each subsequent docid read from the same input list
127090   ** consumes either the same or less bytes as it did in the input (since
127091   ** the difference between it and the previous value in the output must
127092   ** be a positive value less than or equal to the delta value read from
127093   ** the input list). The same argument applies to all but the first docid
127094   ** read from the 'other' list. And to the contents of all position lists
127095   ** that will be copied and merged from the input to the output.
127096   **
127097   ** However, if the first docid copied to the output is a negative number,
127098   ** then the encoding of the first docid from the 'other' input list may
127099   ** be larger in the output than it was in the input (since the delta value
127100   ** may be a larger positive integer than the actual docid).
127101   **
127102   ** The space required to store the output is therefore the sum of the
127103   ** sizes of the two inputs, plus enough space for exactly one of the input
127104   ** docids to grow.
127105   **
127106   ** A symetric argument may be made if the doclists are in descending
127107   ** order.
127108   */
127109   aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
127110   if( !aOut ) return SQLITE_NOMEM;
127111 
127112   p = aOut;
127113   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
127114   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
127115   while( p1 || p2 ){
127116     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
127117 
127118     if( p2 && p1 && iDiff==0 ){
127119       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
127120       fts3PoslistMerge(&p, &p1, &p2);
127121       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
127122       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
127123     }else if( !p2 || (p1 && iDiff<0) ){
127124       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
127125       fts3PoslistCopy(&p, &p1);
127126       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
127127     }else{
127128       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
127129       fts3PoslistCopy(&p, &p2);
127130       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
127131     }
127132   }
127133 
127134   *paOut = aOut;
127135   *pnOut = (int)(p-aOut);
127136   assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
127137   return SQLITE_OK;
127138 }
127139 
127140 /*
127141 ** This function does a "phrase" merge of two doclists. In a phrase merge,
127142 ** the output contains a copy of each position from the right-hand input
127143 ** doclist for which there is a position in the left-hand input doclist
127144 ** exactly nDist tokens before it.
127145 **
127146 ** If the docids in the input doclists are sorted in ascending order,
127147 ** parameter bDescDoclist should be false. If they are sorted in ascending
127148 ** order, it should be passed a non-zero value.
127149 **
127150 ** The right-hand input doclist is overwritten by this function.
127151 */
127152 static void fts3DoclistPhraseMerge(
127153   int bDescDoclist,               /* True if arguments are desc */
127154   int nDist,                      /* Distance from left to right (1=adjacent) */
127155   char *aLeft, int nLeft,         /* Left doclist */
127156   char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
127157 ){
127158   sqlite3_int64 i1 = 0;
127159   sqlite3_int64 i2 = 0;
127160   sqlite3_int64 iPrev = 0;
127161   char *pEnd1 = &aLeft[nLeft];
127162   char *pEnd2 = &aRight[*pnRight];
127163   char *p1 = aLeft;
127164   char *p2 = aRight;
127165   char *p;
127166   int bFirstOut = 0;
127167   char *aOut = aRight;
127168 
127169   assert( nDist>0 );
127170 
127171   p = aOut;
127172   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
127173   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
127174 
127175   while( p1 && p2 ){
127176     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
127177     if( iDiff==0 ){
127178       char *pSave = p;
127179       sqlite3_int64 iPrevSave = iPrev;
127180       int bFirstOutSave = bFirstOut;
127181 
127182       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
127183       if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
127184         p = pSave;
127185         iPrev = iPrevSave;
127186         bFirstOut = bFirstOutSave;
127187       }
127188       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
127189       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
127190     }else if( iDiff<0 ){
127191       fts3PoslistCopy(0, &p1);
127192       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
127193     }else{
127194       fts3PoslistCopy(0, &p2);
127195       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
127196     }
127197   }
127198 
127199   *pnRight = (int)(p - aOut);
127200 }
127201 
127202 /*
127203 ** Argument pList points to a position list nList bytes in size. This
127204 ** function checks to see if the position list contains any entries for
127205 ** a token in position 0 (of any column). If so, it writes argument iDelta
127206 ** to the output buffer pOut, followed by a position list consisting only
127207 ** of the entries from pList at position 0, and terminated by an 0x00 byte.
127208 ** The value returned is the number of bytes written to pOut (if any).
127209 */
127210 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
127211   sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
127212   char *pList,                    /* Position list (no 0x00 term) */
127213   int nList,                      /* Size of pList in bytes */
127214   char *pOut                      /* Write output here */
127215 ){
127216   int nOut = 0;
127217   int bWritten = 0;               /* True once iDelta has been written */
127218   char *p = pList;
127219   char *pEnd = &pList[nList];
127220 
127221   if( *p!=0x01 ){
127222     if( *p==0x02 ){
127223       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
127224       pOut[nOut++] = 0x02;
127225       bWritten = 1;
127226     }
127227     fts3ColumnlistCopy(0, &p);
127228   }
127229 
127230   while( p<pEnd && *p==0x01 ){
127231     sqlite3_int64 iCol;
127232     p++;
127233     p += sqlite3Fts3GetVarint(p, &iCol);
127234     if( *p==0x02 ){
127235       if( bWritten==0 ){
127236         nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
127237         bWritten = 1;
127238       }
127239       pOut[nOut++] = 0x01;
127240       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
127241       pOut[nOut++] = 0x02;
127242     }
127243     fts3ColumnlistCopy(0, &p);
127244   }
127245   if( bWritten ){
127246     pOut[nOut++] = 0x00;
127247   }
127248 
127249   return nOut;
127250 }
127251 
127252 
127253 /*
127254 ** Merge all doclists in the TermSelect.aaOutput[] array into a single
127255 ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
127256 ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
127257 **
127258 ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
127259 ** the responsibility of the caller to free any doclists left in the
127260 ** TermSelect.aaOutput[] array.
127261 */
127262 static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
127263   char *aOut = 0;
127264   int nOut = 0;
127265   int i;
127266 
127267   /* Loop through the doclists in the aaOutput[] array. Merge them all
127268   ** into a single doclist.
127269   */
127270   for(i=0; i<SizeofArray(pTS->aaOutput); i++){
127271     if( pTS->aaOutput[i] ){
127272       if( !aOut ){
127273         aOut = pTS->aaOutput[i];
127274         nOut = pTS->anOutput[i];
127275         pTS->aaOutput[i] = 0;
127276       }else{
127277         int nNew;
127278         char *aNew;
127279 
127280         int rc = fts3DoclistOrMerge(p->bDescIdx,
127281             pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
127282         );
127283         if( rc!=SQLITE_OK ){
127284           sqlite3_free(aOut);
127285           return rc;
127286         }
127287 
127288         sqlite3_free(pTS->aaOutput[i]);
127289         sqlite3_free(aOut);
127290         pTS->aaOutput[i] = 0;
127291         aOut = aNew;
127292         nOut = nNew;
127293       }
127294     }
127295   }
127296 
127297   pTS->aaOutput[0] = aOut;
127298   pTS->anOutput[0] = nOut;
127299   return SQLITE_OK;
127300 }
127301 
127302 /*
127303 ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
127304 ** as the first argument. The merge is an "OR" merge (see function
127305 ** fts3DoclistOrMerge() for details).
127306 **
127307 ** This function is called with the doclist for each term that matches
127308 ** a queried prefix. It merges all these doclists into one, the doclist
127309 ** for the specified prefix. Since there can be a very large number of
127310 ** doclists to merge, the merging is done pair-wise using the TermSelect
127311 ** object.
127312 **
127313 ** This function returns SQLITE_OK if the merge is successful, or an
127314 ** SQLite error code (SQLITE_NOMEM) if an error occurs.
127315 */
127316 static int fts3TermSelectMerge(
127317   Fts3Table *p,                   /* FTS table handle */
127318   TermSelect *pTS,                /* TermSelect object to merge into */
127319   char *aDoclist,                 /* Pointer to doclist */
127320   int nDoclist                    /* Size of aDoclist in bytes */
127321 ){
127322   if( pTS->aaOutput[0]==0 ){
127323     /* If this is the first term selected, copy the doclist to the output
127324     ** buffer using memcpy(). */
127325     pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
127326     pTS->anOutput[0] = nDoclist;
127327     if( pTS->aaOutput[0] ){
127328       memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
127329     }else{
127330       return SQLITE_NOMEM;
127331     }
127332   }else{
127333     char *aMerge = aDoclist;
127334     int nMerge = nDoclist;
127335     int iOut;
127336 
127337     for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
127338       if( pTS->aaOutput[iOut]==0 ){
127339         assert( iOut>0 );
127340         pTS->aaOutput[iOut] = aMerge;
127341         pTS->anOutput[iOut] = nMerge;
127342         break;
127343       }else{
127344         char *aNew;
127345         int nNew;
127346 
127347         int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
127348             pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
127349         );
127350         if( rc!=SQLITE_OK ){
127351           if( aMerge!=aDoclist ) sqlite3_free(aMerge);
127352           return rc;
127353         }
127354 
127355         if( aMerge!=aDoclist ) sqlite3_free(aMerge);
127356         sqlite3_free(pTS->aaOutput[iOut]);
127357         pTS->aaOutput[iOut] = 0;
127358 
127359         aMerge = aNew;
127360         nMerge = nNew;
127361         if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
127362           pTS->aaOutput[iOut] = aMerge;
127363           pTS->anOutput[iOut] = nMerge;
127364         }
127365       }
127366     }
127367   }
127368   return SQLITE_OK;
127369 }
127370 
127371 /*
127372 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
127373 */
127374 static int fts3SegReaderCursorAppend(
127375   Fts3MultiSegReader *pCsr,
127376   Fts3SegReader *pNew
127377 ){
127378   if( (pCsr->nSegment%16)==0 ){
127379     Fts3SegReader **apNew;
127380     int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
127381     apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
127382     if( !apNew ){
127383       sqlite3Fts3SegReaderFree(pNew);
127384       return SQLITE_NOMEM;
127385     }
127386     pCsr->apSegment = apNew;
127387   }
127388   pCsr->apSegment[pCsr->nSegment++] = pNew;
127389   return SQLITE_OK;
127390 }
127391 
127392 /*
127393 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
127394 ** 8th argument.
127395 **
127396 ** This function returns SQLITE_OK if successful, or an SQLite error code
127397 ** otherwise.
127398 */
127399 static int fts3SegReaderCursor(
127400   Fts3Table *p,                   /* FTS3 table handle */
127401   int iLangid,                    /* Language id */
127402   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
127403   int iLevel,                     /* Level of segments to scan */
127404   const char *zTerm,              /* Term to query for */
127405   int nTerm,                      /* Size of zTerm in bytes */
127406   int isPrefix,                   /* True for a prefix search */
127407   int isScan,                     /* True to scan from zTerm to EOF */
127408   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
127409 ){
127410   int rc = SQLITE_OK;             /* Error code */
127411   sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
127412   int rc2;                        /* Result of sqlite3_reset() */
127413 
127414   /* If iLevel is less than 0 and this is not a scan, include a seg-reader
127415   ** for the pending-terms. If this is a scan, then this call must be being
127416   ** made by an fts4aux module, not an FTS table. In this case calling
127417   ** Fts3SegReaderPending might segfault, as the data structures used by
127418   ** fts4aux are not completely populated. So it's easiest to filter these
127419   ** calls out here.  */
127420   if( iLevel<0 && p->aIndex ){
127421     Fts3SegReader *pSeg = 0;
127422     rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
127423     if( rc==SQLITE_OK && pSeg ){
127424       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
127425     }
127426   }
127427 
127428   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
127429     if( rc==SQLITE_OK ){
127430       rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
127431     }
127432 
127433     while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
127434       Fts3SegReader *pSeg = 0;
127435 
127436       /* Read the values returned by the SELECT into local variables. */
127437       sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
127438       sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
127439       sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
127440       int nRoot = sqlite3_column_bytes(pStmt, 4);
127441       char const *zRoot = sqlite3_column_blob(pStmt, 4);
127442 
127443       /* If zTerm is not NULL, and this segment is not stored entirely on its
127444       ** root node, the range of leaves scanned can be reduced. Do this. */
127445       if( iStartBlock && zTerm ){
127446         sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
127447         rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
127448         if( rc!=SQLITE_OK ) goto finished;
127449         if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
127450       }
127451 
127452       rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
127453           (isPrefix==0 && isScan==0),
127454           iStartBlock, iLeavesEndBlock,
127455           iEndBlock, zRoot, nRoot, &pSeg
127456       );
127457       if( rc!=SQLITE_OK ) goto finished;
127458       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
127459     }
127460   }
127461 
127462  finished:
127463   rc2 = sqlite3_reset(pStmt);
127464   if( rc==SQLITE_DONE ) rc = rc2;
127465 
127466   return rc;
127467 }
127468 
127469 /*
127470 ** Set up a cursor object for iterating through a full-text index or a
127471 ** single level therein.
127472 */
127473 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
127474   Fts3Table *p,                   /* FTS3 table handle */
127475   int iLangid,                    /* Language-id to search */
127476   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
127477   int iLevel,                     /* Level of segments to scan */
127478   const char *zTerm,              /* Term to query for */
127479   int nTerm,                      /* Size of zTerm in bytes */
127480   int isPrefix,                   /* True for a prefix search */
127481   int isScan,                     /* True to scan from zTerm to EOF */
127482   Fts3MultiSegReader *pCsr       /* Cursor object to populate */
127483 ){
127484   assert( iIndex>=0 && iIndex<p->nIndex );
127485   assert( iLevel==FTS3_SEGCURSOR_ALL
127486       ||  iLevel==FTS3_SEGCURSOR_PENDING
127487       ||  iLevel>=0
127488   );
127489   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
127490   assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
127491   assert( isPrefix==0 || isScan==0 );
127492 
127493   memset(pCsr, 0, sizeof(Fts3MultiSegReader));
127494   return fts3SegReaderCursor(
127495       p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
127496   );
127497 }
127498 
127499 /*
127500 ** In addition to its current configuration, have the Fts3MultiSegReader
127501 ** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
127502 **
127503 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
127504 */
127505 static int fts3SegReaderCursorAddZero(
127506   Fts3Table *p,                   /* FTS virtual table handle */
127507   int iLangid,
127508   const char *zTerm,              /* Term to scan doclist of */
127509   int nTerm,                      /* Number of bytes in zTerm */
127510   Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
127511 ){
127512   return fts3SegReaderCursor(p,
127513       iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
127514   );
127515 }
127516 
127517 /*
127518 ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
127519 ** if isPrefix is true, to scan the doclist for all terms for which
127520 ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
127521 ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
127522 ** an SQLite error code.
127523 **
127524 ** It is the responsibility of the caller to free this object by eventually
127525 ** passing it to fts3SegReaderCursorFree()
127526 **
127527 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
127528 ** Output parameter *ppSegcsr is set to 0 if an error occurs.
127529 */
127530 static int fts3TermSegReaderCursor(
127531   Fts3Cursor *pCsr,               /* Virtual table cursor handle */
127532   const char *zTerm,              /* Term to query for */
127533   int nTerm,                      /* Size of zTerm in bytes */
127534   int isPrefix,                   /* True for a prefix search */
127535   Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
127536 ){
127537   Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
127538   int rc = SQLITE_NOMEM;          /* Return code */
127539 
127540   pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
127541   if( pSegcsr ){
127542     int i;
127543     int bFound = 0;               /* True once an index has been found */
127544     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
127545 
127546     if( isPrefix ){
127547       for(i=1; bFound==0 && i<p->nIndex; i++){
127548         if( p->aIndex[i].nPrefix==nTerm ){
127549           bFound = 1;
127550           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
127551               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
127552           );
127553           pSegcsr->bLookup = 1;
127554         }
127555       }
127556 
127557       for(i=1; bFound==0 && i<p->nIndex; i++){
127558         if( p->aIndex[i].nPrefix==nTerm+1 ){
127559           bFound = 1;
127560           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
127561               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
127562           );
127563           if( rc==SQLITE_OK ){
127564             rc = fts3SegReaderCursorAddZero(
127565                 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
127566             );
127567           }
127568         }
127569       }
127570     }
127571 
127572     if( bFound==0 ){
127573       rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
127574           0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
127575       );
127576       pSegcsr->bLookup = !isPrefix;
127577     }
127578   }
127579 
127580   *ppSegcsr = pSegcsr;
127581   return rc;
127582 }
127583 
127584 /*
127585 ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
127586 */
127587 static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
127588   sqlite3Fts3SegReaderFinish(pSegcsr);
127589   sqlite3_free(pSegcsr);
127590 }
127591 
127592 /*
127593 ** This function retrieves the doclist for the specified term (or term
127594 ** prefix) from the database.
127595 */
127596 static int fts3TermSelect(
127597   Fts3Table *p,                   /* Virtual table handle */
127598   Fts3PhraseToken *pTok,          /* Token to query for */
127599   int iColumn,                    /* Column to query (or -ve for all columns) */
127600   int *pnOut,                     /* OUT: Size of buffer at *ppOut */
127601   char **ppOut                    /* OUT: Malloced result buffer */
127602 ){
127603   int rc;                         /* Return code */
127604   Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
127605   TermSelect tsc;                 /* Object for pair-wise doclist merging */
127606   Fts3SegFilter filter;           /* Segment term filter configuration */
127607 
127608   pSegcsr = pTok->pSegcsr;
127609   memset(&tsc, 0, sizeof(TermSelect));
127610 
127611   filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
127612         | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
127613         | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
127614         | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
127615   filter.iCol = iColumn;
127616   filter.zTerm = pTok->z;
127617   filter.nTerm = pTok->n;
127618 
127619   rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
127620   while( SQLITE_OK==rc
127621       && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
127622   ){
127623     rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
127624   }
127625 
127626   if( rc==SQLITE_OK ){
127627     rc = fts3TermSelectFinishMerge(p, &tsc);
127628   }
127629   if( rc==SQLITE_OK ){
127630     *ppOut = tsc.aaOutput[0];
127631     *pnOut = tsc.anOutput[0];
127632   }else{
127633     int i;
127634     for(i=0; i<SizeofArray(tsc.aaOutput); i++){
127635       sqlite3_free(tsc.aaOutput[i]);
127636     }
127637   }
127638 
127639   fts3SegReaderCursorFree(pSegcsr);
127640   pTok->pSegcsr = 0;
127641   return rc;
127642 }
127643 
127644 /*
127645 ** This function counts the total number of docids in the doclist stored
127646 ** in buffer aList[], size nList bytes.
127647 **
127648 ** If the isPoslist argument is true, then it is assumed that the doclist
127649 ** contains a position-list following each docid. Otherwise, it is assumed
127650 ** that the doclist is simply a list of docids stored as delta encoded
127651 ** varints.
127652 */
127653 static int fts3DoclistCountDocids(char *aList, int nList){
127654   int nDoc = 0;                   /* Return value */
127655   if( aList ){
127656     char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
127657     char *p = aList;              /* Cursor */
127658     while( p<aEnd ){
127659       nDoc++;
127660       while( (*p++)&0x80 );     /* Skip docid varint */
127661       fts3PoslistCopy(0, &p);   /* Skip over position list */
127662     }
127663   }
127664 
127665   return nDoc;
127666 }
127667 
127668 /*
127669 ** Advance the cursor to the next row in the %_content table that
127670 ** matches the search criteria.  For a MATCH search, this will be
127671 ** the next row that matches. For a full-table scan, this will be
127672 ** simply the next row in the %_content table.  For a docid lookup,
127673 ** this routine simply sets the EOF flag.
127674 **
127675 ** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
127676 ** even if we reach end-of-file.  The fts3EofMethod() will be called
127677 ** subsequently to determine whether or not an EOF was hit.
127678 */
127679 static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
127680   int rc;
127681   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
127682   if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
127683     if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
127684       pCsr->isEof = 1;
127685       rc = sqlite3_reset(pCsr->pStmt);
127686     }else{
127687       pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
127688       rc = SQLITE_OK;
127689     }
127690   }else{
127691     rc = fts3EvalNext((Fts3Cursor *)pCursor);
127692   }
127693   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
127694   return rc;
127695 }
127696 
127697 /*
127698 ** The following are copied from sqliteInt.h.
127699 **
127700 ** Constants for the largest and smallest possible 64-bit signed integers.
127701 ** These macros are designed to work correctly on both 32-bit and 64-bit
127702 ** compilers.
127703 */
127704 #ifndef SQLITE_AMALGAMATION
127705 # define LARGEST_INT64  (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
127706 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
127707 #endif
127708 
127709 /*
127710 ** If the numeric type of argument pVal is "integer", then return it
127711 ** converted to a 64-bit signed integer. Otherwise, return a copy of
127712 ** the second parameter, iDefault.
127713 */
127714 static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
127715   if( pVal ){
127716     int eType = sqlite3_value_numeric_type(pVal);
127717     if( eType==SQLITE_INTEGER ){
127718       return sqlite3_value_int64(pVal);
127719     }
127720   }
127721   return iDefault;
127722 }
127723 
127724 /*
127725 ** This is the xFilter interface for the virtual table.  See
127726 ** the virtual table xFilter method documentation for additional
127727 ** information.
127728 **
127729 ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
127730 ** the %_content table.
127731 **
127732 ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
127733 ** in the %_content table.
127734 **
127735 ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
127736 ** column on the left-hand side of the MATCH operator is column
127737 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
127738 ** side of the MATCH operator.
127739 */
127740 static int fts3FilterMethod(
127741   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
127742   int idxNum,                     /* Strategy index */
127743   const char *idxStr,             /* Unused */
127744   int nVal,                       /* Number of elements in apVal */
127745   sqlite3_value **apVal           /* Arguments for the indexing scheme */
127746 ){
127747   int rc;
127748   char *zSql;                     /* SQL statement used to access %_content */
127749   int eSearch;
127750   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
127751   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
127752 
127753   sqlite3_value *pCons = 0;       /* The MATCH or rowid constraint, if any */
127754   sqlite3_value *pLangid = 0;     /* The "langid = ?" constraint, if any */
127755   sqlite3_value *pDocidGe = 0;    /* The "docid >= ?" constraint, if any */
127756   sqlite3_value *pDocidLe = 0;    /* The "docid <= ?" constraint, if any */
127757   int iIdx;
127758 
127759   UNUSED_PARAMETER(idxStr);
127760   UNUSED_PARAMETER(nVal);
127761 
127762   eSearch = (idxNum & 0x0000FFFF);
127763   assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
127764   assert( p->pSegments==0 );
127765 
127766   /* Collect arguments into local variables */
127767   iIdx = 0;
127768   if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
127769   if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
127770   if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
127771   if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
127772   assert( iIdx==nVal );
127773 
127774   /* In case the cursor has been used before, clear it now. */
127775   sqlite3_finalize(pCsr->pStmt);
127776   sqlite3_free(pCsr->aDoclist);
127777   sqlite3Fts3ExprFree(pCsr->pExpr);
127778   memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
127779 
127780   /* Set the lower and upper bounds on docids to return */
127781   pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
127782   pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
127783 
127784   if( idxStr ){
127785     pCsr->bDesc = (idxStr[0]=='D');
127786   }else{
127787     pCsr->bDesc = p->bDescIdx;
127788   }
127789   pCsr->eSearch = (i16)eSearch;
127790 
127791   if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
127792     int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
127793     const char *zQuery = (const char *)sqlite3_value_text(pCons);
127794 
127795     if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
127796       return SQLITE_NOMEM;
127797     }
127798 
127799     pCsr->iLangid = 0;
127800     if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
127801 
127802     assert( p->base.zErrMsg==0 );
127803     rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
127804         p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
127805         &p->base.zErrMsg
127806     );
127807     if( rc!=SQLITE_OK ){
127808       return rc;
127809     }
127810 
127811     rc = fts3EvalStart(pCsr);
127812     sqlite3Fts3SegmentsClose(p);
127813     if( rc!=SQLITE_OK ) return rc;
127814     pCsr->pNextId = pCsr->aDoclist;
127815     pCsr->iPrevId = 0;
127816   }
127817 
127818   /* Compile a SELECT statement for this cursor. For a full-table-scan, the
127819   ** statement loops through all rows of the %_content table. For a
127820   ** full-text query or docid lookup, the statement retrieves a single
127821   ** row by docid.
127822   */
127823   if( eSearch==FTS3_FULLSCAN_SEARCH ){
127824     zSql = sqlite3_mprintf(
127825         "SELECT %s ORDER BY rowid %s",
127826         p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
127827     );
127828     if( zSql ){
127829       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
127830       sqlite3_free(zSql);
127831     }else{
127832       rc = SQLITE_NOMEM;
127833     }
127834   }else if( eSearch==FTS3_DOCID_SEARCH ){
127835     rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
127836     if( rc==SQLITE_OK ){
127837       rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
127838     }
127839   }
127840   if( rc!=SQLITE_OK ) return rc;
127841 
127842   return fts3NextMethod(pCursor);
127843 }
127844 
127845 /*
127846 ** This is the xEof method of the virtual table. SQLite calls this
127847 ** routine to find out if it has reached the end of a result set.
127848 */
127849 static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
127850   return ((Fts3Cursor *)pCursor)->isEof;
127851 }
127852 
127853 /*
127854 ** This is the xRowid method. The SQLite core calls this routine to
127855 ** retrieve the rowid for the current row of the result set. fts3
127856 ** exposes %_content.docid as the rowid for the virtual table. The
127857 ** rowid should be written to *pRowid.
127858 */
127859 static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
127860   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
127861   *pRowid = pCsr->iPrevId;
127862   return SQLITE_OK;
127863 }
127864 
127865 /*
127866 ** This is the xColumn method, called by SQLite to request a value from
127867 ** the row that the supplied cursor currently points to.
127868 **
127869 ** If:
127870 **
127871 **   (iCol <  p->nColumn)   -> The value of the iCol'th user column.
127872 **   (iCol == p->nColumn)   -> Magic column with the same name as the table.
127873 **   (iCol == p->nColumn+1) -> Docid column
127874 **   (iCol == p->nColumn+2) -> Langid column
127875 */
127876 static int fts3ColumnMethod(
127877   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
127878   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
127879   int iCol                        /* Index of column to read value from */
127880 ){
127881   int rc = SQLITE_OK;             /* Return Code */
127882   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
127883   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
127884 
127885   /* The column value supplied by SQLite must be in range. */
127886   assert( iCol>=0 && iCol<=p->nColumn+2 );
127887 
127888   if( iCol==p->nColumn+1 ){
127889     /* This call is a request for the "docid" column. Since "docid" is an
127890     ** alias for "rowid", use the xRowid() method to obtain the value.
127891     */
127892     sqlite3_result_int64(pCtx, pCsr->iPrevId);
127893   }else if( iCol==p->nColumn ){
127894     /* The extra column whose name is the same as the table.
127895     ** Return a blob which is a pointer to the cursor.  */
127896     sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
127897   }else if( iCol==p->nColumn+2 && pCsr->pExpr ){
127898     sqlite3_result_int64(pCtx, pCsr->iLangid);
127899   }else{
127900     /* The requested column is either a user column (one that contains
127901     ** indexed data), or the language-id column.  */
127902     rc = fts3CursorSeek(0, pCsr);
127903 
127904     if( rc==SQLITE_OK ){
127905       if( iCol==p->nColumn+2 ){
127906         int iLangid = 0;
127907         if( p->zLanguageid ){
127908           iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1);
127909         }
127910         sqlite3_result_int(pCtx, iLangid);
127911       }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
127912         sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
127913       }
127914     }
127915   }
127916 
127917   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
127918   return rc;
127919 }
127920 
127921 /*
127922 ** This function is the implementation of the xUpdate callback used by
127923 ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
127924 ** inserted, updated or deleted.
127925 */
127926 static int fts3UpdateMethod(
127927   sqlite3_vtab *pVtab,            /* Virtual table handle */
127928   int nArg,                       /* Size of argument array */
127929   sqlite3_value **apVal,          /* Array of arguments */
127930   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
127931 ){
127932   return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
127933 }
127934 
127935 /*
127936 ** Implementation of xSync() method. Flush the contents of the pending-terms
127937 ** hash-table to the database.
127938 */
127939 static int fts3SyncMethod(sqlite3_vtab *pVtab){
127940 
127941   /* Following an incremental-merge operation, assuming that the input
127942   ** segments are not completely consumed (the usual case), they are updated
127943   ** in place to remove the entries that have already been merged. This
127944   ** involves updating the leaf block that contains the smallest unmerged
127945   ** entry and each block (if any) between the leaf and the root node. So
127946   ** if the height of the input segment b-trees is N, and input segments
127947   ** are merged eight at a time, updating the input segments at the end
127948   ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
127949   ** small - often between 0 and 2. So the overhead of the incremental
127950   ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
127951   ** dwarfing the actual productive work accomplished, the incremental merge
127952   ** is only attempted if it will write at least 64 leaf blocks. Hence
127953   ** nMinMerge.
127954   **
127955   ** Of course, updating the input segments also involves deleting a bunch
127956   ** of blocks from the segments table. But this is not considered overhead
127957   ** as it would also be required by a crisis-merge that used the same input
127958   ** segments.
127959   */
127960   const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
127961 
127962   Fts3Table *p = (Fts3Table*)pVtab;
127963   int rc = sqlite3Fts3PendingTermsFlush(p);
127964 
127965   if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
127966     int mxLevel = 0;              /* Maximum relative level value in db */
127967     int A;                        /* Incr-merge parameter A */
127968 
127969     rc = sqlite3Fts3MaxLevel(p, &mxLevel);
127970     assert( rc==SQLITE_OK || mxLevel==0 );
127971     A = p->nLeafAdd * mxLevel;
127972     A += (A/2);
127973     if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
127974   }
127975   sqlite3Fts3SegmentsClose(p);
127976   return rc;
127977 }
127978 
127979 /*
127980 ** Implementation of xBegin() method. This is a no-op.
127981 */
127982 static int fts3BeginMethod(sqlite3_vtab *pVtab){
127983   Fts3Table *p = (Fts3Table*)pVtab;
127984   UNUSED_PARAMETER(pVtab);
127985   assert( p->pSegments==0 );
127986   assert( p->nPendingData==0 );
127987   assert( p->inTransaction!=1 );
127988   TESTONLY( p->inTransaction = 1 );
127989   TESTONLY( p->mxSavepoint = -1; );
127990   p->nLeafAdd = 0;
127991   return SQLITE_OK;
127992 }
127993 
127994 /*
127995 ** Implementation of xCommit() method. This is a no-op. The contents of
127996 ** the pending-terms hash-table have already been flushed into the database
127997 ** by fts3SyncMethod().
127998 */
127999 static int fts3CommitMethod(sqlite3_vtab *pVtab){
128000   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
128001   UNUSED_PARAMETER(pVtab);
128002   assert( p->nPendingData==0 );
128003   assert( p->inTransaction!=0 );
128004   assert( p->pSegments==0 );
128005   TESTONLY( p->inTransaction = 0 );
128006   TESTONLY( p->mxSavepoint = -1; );
128007   return SQLITE_OK;
128008 }
128009 
128010 /*
128011 ** Implementation of xRollback(). Discard the contents of the pending-terms
128012 ** hash-table. Any changes made to the database are reverted by SQLite.
128013 */
128014 static int fts3RollbackMethod(sqlite3_vtab *pVtab){
128015   Fts3Table *p = (Fts3Table*)pVtab;
128016   sqlite3Fts3PendingTermsClear(p);
128017   assert( p->inTransaction!=0 );
128018   TESTONLY( p->inTransaction = 0 );
128019   TESTONLY( p->mxSavepoint = -1; );
128020   return SQLITE_OK;
128021 }
128022 
128023 /*
128024 ** When called, *ppPoslist must point to the byte immediately following the
128025 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
128026 ** moves *ppPoslist so that it instead points to the first byte of the
128027 ** same position list.
128028 */
128029 static void fts3ReversePoslist(char *pStart, char **ppPoslist){
128030   char *p = &(*ppPoslist)[-2];
128031   char c = 0;
128032 
128033   while( p>pStart && (c=*p--)==0 );
128034   while( p>pStart && (*p & 0x80) | c ){
128035     c = *p--;
128036   }
128037   if( p>pStart ){ p = &p[2]; }
128038   while( *p++&0x80 );
128039   *ppPoslist = p;
128040 }
128041 
128042 /*
128043 ** Helper function used by the implementation of the overloaded snippet(),
128044 ** offsets() and optimize() SQL functions.
128045 **
128046 ** If the value passed as the third argument is a blob of size
128047 ** sizeof(Fts3Cursor*), then the blob contents are copied to the
128048 ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
128049 ** message is written to context pContext and SQLITE_ERROR returned. The
128050 ** string passed via zFunc is used as part of the error message.
128051 */
128052 static int fts3FunctionArg(
128053   sqlite3_context *pContext,      /* SQL function call context */
128054   const char *zFunc,              /* Function name */
128055   sqlite3_value *pVal,            /* argv[0] passed to function */
128056   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
128057 ){
128058   Fts3Cursor *pRet;
128059   if( sqlite3_value_type(pVal)!=SQLITE_BLOB
128060    || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
128061   ){
128062     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
128063     sqlite3_result_error(pContext, zErr, -1);
128064     sqlite3_free(zErr);
128065     return SQLITE_ERROR;
128066   }
128067   memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
128068   *ppCsr = pRet;
128069   return SQLITE_OK;
128070 }
128071 
128072 /*
128073 ** Implementation of the snippet() function for FTS3
128074 */
128075 static void fts3SnippetFunc(
128076   sqlite3_context *pContext,      /* SQLite function call context */
128077   int nVal,                       /* Size of apVal[] array */
128078   sqlite3_value **apVal           /* Array of arguments */
128079 ){
128080   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
128081   const char *zStart = "<b>";
128082   const char *zEnd = "</b>";
128083   const char *zEllipsis = "<b>...</b>";
128084   int iCol = -1;
128085   int nToken = 15;                /* Default number of tokens in snippet */
128086 
128087   /* There must be at least one argument passed to this function (otherwise
128088   ** the non-overloaded version would have been called instead of this one).
128089   */
128090   assert( nVal>=1 );
128091 
128092   if( nVal>6 ){
128093     sqlite3_result_error(pContext,
128094         "wrong number of arguments to function snippet()", -1);
128095     return;
128096   }
128097   if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
128098 
128099   switch( nVal ){
128100     case 6: nToken = sqlite3_value_int(apVal[5]);
128101     case 5: iCol = sqlite3_value_int(apVal[4]);
128102     case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
128103     case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
128104     case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
128105   }
128106   if( !zEllipsis || !zEnd || !zStart ){
128107     sqlite3_result_error_nomem(pContext);
128108   }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
128109     sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
128110   }
128111 }
128112 
128113 /*
128114 ** Implementation of the offsets() function for FTS3
128115 */
128116 static void fts3OffsetsFunc(
128117   sqlite3_context *pContext,      /* SQLite function call context */
128118   int nVal,                       /* Size of argument array */
128119   sqlite3_value **apVal           /* Array of arguments */
128120 ){
128121   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
128122 
128123   UNUSED_PARAMETER(nVal);
128124 
128125   assert( nVal==1 );
128126   if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
128127   assert( pCsr );
128128   if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
128129     sqlite3Fts3Offsets(pContext, pCsr);
128130   }
128131 }
128132 
128133 /*
128134 ** Implementation of the special optimize() function for FTS3. This
128135 ** function merges all segments in the database to a single segment.
128136 ** Example usage is:
128137 **
128138 **   SELECT optimize(t) FROM t LIMIT 1;
128139 **
128140 ** where 't' is the name of an FTS3 table.
128141 */
128142 static void fts3OptimizeFunc(
128143   sqlite3_context *pContext,      /* SQLite function call context */
128144   int nVal,                       /* Size of argument array */
128145   sqlite3_value **apVal           /* Array of arguments */
128146 ){
128147   int rc;                         /* Return code */
128148   Fts3Table *p;                   /* Virtual table handle */
128149   Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
128150 
128151   UNUSED_PARAMETER(nVal);
128152 
128153   assert( nVal==1 );
128154   if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
128155   p = (Fts3Table *)pCursor->base.pVtab;
128156   assert( p );
128157 
128158   rc = sqlite3Fts3Optimize(p);
128159 
128160   switch( rc ){
128161     case SQLITE_OK:
128162       sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
128163       break;
128164     case SQLITE_DONE:
128165       sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
128166       break;
128167     default:
128168       sqlite3_result_error_code(pContext, rc);
128169       break;
128170   }
128171 }
128172 
128173 /*
128174 ** Implementation of the matchinfo() function for FTS3
128175 */
128176 static void fts3MatchinfoFunc(
128177   sqlite3_context *pContext,      /* SQLite function call context */
128178   int nVal,                       /* Size of argument array */
128179   sqlite3_value **apVal           /* Array of arguments */
128180 ){
128181   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
128182   assert( nVal==1 || nVal==2 );
128183   if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
128184     const char *zArg = 0;
128185     if( nVal>1 ){
128186       zArg = (const char *)sqlite3_value_text(apVal[1]);
128187     }
128188     sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
128189   }
128190 }
128191 
128192 /*
128193 ** This routine implements the xFindFunction method for the FTS3
128194 ** virtual table.
128195 */
128196 static int fts3FindFunctionMethod(
128197   sqlite3_vtab *pVtab,            /* Virtual table handle */
128198   int nArg,                       /* Number of SQL function arguments */
128199   const char *zName,              /* Name of SQL function */
128200   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
128201   void **ppArg                    /* Unused */
128202 ){
128203   struct Overloaded {
128204     const char *zName;
128205     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
128206   } aOverload[] = {
128207     { "snippet", fts3SnippetFunc },
128208     { "offsets", fts3OffsetsFunc },
128209     { "optimize", fts3OptimizeFunc },
128210     { "matchinfo", fts3MatchinfoFunc },
128211   };
128212   int i;                          /* Iterator variable */
128213 
128214   UNUSED_PARAMETER(pVtab);
128215   UNUSED_PARAMETER(nArg);
128216   UNUSED_PARAMETER(ppArg);
128217 
128218   for(i=0; i<SizeofArray(aOverload); i++){
128219     if( strcmp(zName, aOverload[i].zName)==0 ){
128220       *pxFunc = aOverload[i].xFunc;
128221       return 1;
128222     }
128223   }
128224 
128225   /* No function of the specified name was found. Return 0. */
128226   return 0;
128227 }
128228 
128229 /*
128230 ** Implementation of FTS3 xRename method. Rename an fts3 table.
128231 */
128232 static int fts3RenameMethod(
128233   sqlite3_vtab *pVtab,            /* Virtual table handle */
128234   const char *zName               /* New name of table */
128235 ){
128236   Fts3Table *p = (Fts3Table *)pVtab;
128237   sqlite3 *db = p->db;            /* Database connection */
128238   int rc;                         /* Return Code */
128239 
128240   /* As it happens, the pending terms table is always empty here. This is
128241   ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
128242   ** always opens a savepoint transaction. And the xSavepoint() method
128243   ** flushes the pending terms table. But leave the (no-op) call to
128244   ** PendingTermsFlush() in in case that changes.
128245   */
128246   assert( p->nPendingData==0 );
128247   rc = sqlite3Fts3PendingTermsFlush(p);
128248 
128249   if( p->zContentTbl==0 ){
128250     fts3DbExec(&rc, db,
128251       "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
128252       p->zDb, p->zName, zName
128253     );
128254   }
128255 
128256   if( p->bHasDocsize ){
128257     fts3DbExec(&rc, db,
128258       "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
128259       p->zDb, p->zName, zName
128260     );
128261   }
128262   if( p->bHasStat ){
128263     fts3DbExec(&rc, db,
128264       "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
128265       p->zDb, p->zName, zName
128266     );
128267   }
128268   fts3DbExec(&rc, db,
128269     "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
128270     p->zDb, p->zName, zName
128271   );
128272   fts3DbExec(&rc, db,
128273     "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
128274     p->zDb, p->zName, zName
128275   );
128276   return rc;
128277 }
128278 
128279 /*
128280 ** The xSavepoint() method.
128281 **
128282 ** Flush the contents of the pending-terms table to disk.
128283 */
128284 static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
128285   int rc = SQLITE_OK;
128286   UNUSED_PARAMETER(iSavepoint);
128287   assert( ((Fts3Table *)pVtab)->inTransaction );
128288   assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
128289   TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
128290   if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
128291     rc = fts3SyncMethod(pVtab);
128292   }
128293   return rc;
128294 }
128295 
128296 /*
128297 ** The xRelease() method.
128298 **
128299 ** This is a no-op.
128300 */
128301 static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
128302   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
128303   UNUSED_PARAMETER(iSavepoint);
128304   UNUSED_PARAMETER(pVtab);
128305   assert( p->inTransaction );
128306   assert( p->mxSavepoint >= iSavepoint );
128307   TESTONLY( p->mxSavepoint = iSavepoint-1 );
128308   return SQLITE_OK;
128309 }
128310 
128311 /*
128312 ** The xRollbackTo() method.
128313 **
128314 ** Discard the contents of the pending terms table.
128315 */
128316 static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
128317   Fts3Table *p = (Fts3Table*)pVtab;
128318   UNUSED_PARAMETER(iSavepoint);
128319   assert( p->inTransaction );
128320   assert( p->mxSavepoint >= iSavepoint );
128321   TESTONLY( p->mxSavepoint = iSavepoint );
128322   sqlite3Fts3PendingTermsClear(p);
128323   return SQLITE_OK;
128324 }
128325 
128326 static const sqlite3_module fts3Module = {
128327   /* iVersion      */ 2,
128328   /* xCreate       */ fts3CreateMethod,
128329   /* xConnect      */ fts3ConnectMethod,
128330   /* xBestIndex    */ fts3BestIndexMethod,
128331   /* xDisconnect   */ fts3DisconnectMethod,
128332   /* xDestroy      */ fts3DestroyMethod,
128333   /* xOpen         */ fts3OpenMethod,
128334   /* xClose        */ fts3CloseMethod,
128335   /* xFilter       */ fts3FilterMethod,
128336   /* xNext         */ fts3NextMethod,
128337   /* xEof          */ fts3EofMethod,
128338   /* xColumn       */ fts3ColumnMethod,
128339   /* xRowid        */ fts3RowidMethod,
128340   /* xUpdate       */ fts3UpdateMethod,
128341   /* xBegin        */ fts3BeginMethod,
128342   /* xSync         */ fts3SyncMethod,
128343   /* xCommit       */ fts3CommitMethod,
128344   /* xRollback     */ fts3RollbackMethod,
128345   /* xFindFunction */ fts3FindFunctionMethod,
128346   /* xRename */       fts3RenameMethod,
128347   /* xSavepoint    */ fts3SavepointMethod,
128348   /* xRelease      */ fts3ReleaseMethod,
128349   /* xRollbackTo   */ fts3RollbackToMethod,
128350 };
128351 
128352 /*
128353 ** This function is registered as the module destructor (called when an
128354 ** FTS3 enabled database connection is closed). It frees the memory
128355 ** allocated for the tokenizer hash table.
128356 */
128357 static void hashDestroy(void *p){
128358   Fts3Hash *pHash = (Fts3Hash *)p;
128359   sqlite3Fts3HashClear(pHash);
128360   sqlite3_free(pHash);
128361 }
128362 
128363 /*
128364 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
128365 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
128366 ** respectively. The following three forward declarations are for functions
128367 ** declared in these files used to retrieve the respective implementations.
128368 **
128369 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
128370 ** to by the argument to point to the "simple" tokenizer implementation.
128371 ** And so on.
128372 */
128373 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
128374 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
128375 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
128376 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
128377 #endif
128378 #ifdef SQLITE_ENABLE_ICU
128379 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
128380 #endif
128381 
128382 /*
128383 ** Initialize the fts3 extension. If this extension is built as part
128384 ** of the sqlite library, then this function is called directly by
128385 ** SQLite. If fts3 is built as a dynamically loadable extension, this
128386 ** function is called by the sqlite3_extension_init() entry point.
128387 */
128388 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
128389   int rc = SQLITE_OK;
128390   Fts3Hash *pHash = 0;
128391   const sqlite3_tokenizer_module *pSimple = 0;
128392   const sqlite3_tokenizer_module *pPorter = 0;
128393 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
128394   const sqlite3_tokenizer_module *pUnicode = 0;
128395 #endif
128396 
128397 #ifdef SQLITE_ENABLE_ICU
128398   const sqlite3_tokenizer_module *pIcu = 0;
128399   sqlite3Fts3IcuTokenizerModule(&pIcu);
128400 #endif
128401 
128402 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
128403   sqlite3Fts3UnicodeTokenizer(&pUnicode);
128404 #endif
128405 
128406 #ifdef SQLITE_TEST
128407   rc = sqlite3Fts3InitTerm(db);
128408   if( rc!=SQLITE_OK ) return rc;
128409 #endif
128410 
128411   rc = sqlite3Fts3InitAux(db);
128412   if( rc!=SQLITE_OK ) return rc;
128413 
128414   sqlite3Fts3SimpleTokenizerModule(&pSimple);
128415   sqlite3Fts3PorterTokenizerModule(&pPorter);
128416 
128417   /* Allocate and initialize the hash-table used to store tokenizers. */
128418   pHash = sqlite3_malloc(sizeof(Fts3Hash));
128419   if( !pHash ){
128420     rc = SQLITE_NOMEM;
128421   }else{
128422     sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
128423   }
128424 
128425   /* Load the built-in tokenizers into the hash table */
128426   if( rc==SQLITE_OK ){
128427     if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
128428      || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
128429 
128430 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
128431      || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
128432 #endif
128433 #ifdef SQLITE_ENABLE_ICU
128434      || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
128435 #endif
128436     ){
128437       rc = SQLITE_NOMEM;
128438     }
128439   }
128440 
128441 #ifdef SQLITE_TEST
128442   if( rc==SQLITE_OK ){
128443     rc = sqlite3Fts3ExprInitTestInterface(db);
128444   }
128445 #endif
128446 
128447   /* Create the virtual table wrapper around the hash-table and overload
128448   ** the two scalar functions. If this is successful, register the
128449   ** module with sqlite.
128450   */
128451   if( SQLITE_OK==rc
128452    && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
128453    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
128454    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
128455    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
128456    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
128457    && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
128458   ){
128459     rc = sqlite3_create_module_v2(
128460         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
128461     );
128462     if( rc==SQLITE_OK ){
128463       rc = sqlite3_create_module_v2(
128464           db, "fts4", &fts3Module, (void *)pHash, 0
128465       );
128466     }
128467     if( rc==SQLITE_OK ){
128468       rc = sqlite3Fts3InitTok(db, (void *)pHash);
128469     }
128470     return rc;
128471   }
128472 
128473 
128474   /* An error has occurred. Delete the hash table and return the error code. */
128475   assert( rc!=SQLITE_OK );
128476   if( pHash ){
128477     sqlite3Fts3HashClear(pHash);
128478     sqlite3_free(pHash);
128479   }
128480   return rc;
128481 }
128482 
128483 /*
128484 ** Allocate an Fts3MultiSegReader for each token in the expression headed
128485 ** by pExpr.
128486 **
128487 ** An Fts3SegReader object is a cursor that can seek or scan a range of
128488 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
128489 ** Fts3SegReader objects internally to provide an interface to seek or scan
128490 ** within the union of all segments of a b-tree. Hence the name.
128491 **
128492 ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
128493 ** segment b-tree (if the term is not a prefix or it is a prefix for which
128494 ** there exists prefix b-tree of the right length) then it may be traversed
128495 ** and merged incrementally. Otherwise, it has to be merged into an in-memory
128496 ** doclist and then traversed.
128497 */
128498 static void fts3EvalAllocateReaders(
128499   Fts3Cursor *pCsr,               /* FTS cursor handle */
128500   Fts3Expr *pExpr,                /* Allocate readers for this expression */
128501   int *pnToken,                   /* OUT: Total number of tokens in phrase. */
128502   int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
128503   int *pRc                        /* IN/OUT: Error code */
128504 ){
128505   if( pExpr && SQLITE_OK==*pRc ){
128506     if( pExpr->eType==FTSQUERY_PHRASE ){
128507       int i;
128508       int nToken = pExpr->pPhrase->nToken;
128509       *pnToken += nToken;
128510       for(i=0; i<nToken; i++){
128511         Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
128512         int rc = fts3TermSegReaderCursor(pCsr,
128513             pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
128514         );
128515         if( rc!=SQLITE_OK ){
128516           *pRc = rc;
128517           return;
128518         }
128519       }
128520       assert( pExpr->pPhrase->iDoclistToken==0 );
128521       pExpr->pPhrase->iDoclistToken = -1;
128522     }else{
128523       *pnOr += (pExpr->eType==FTSQUERY_OR);
128524       fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
128525       fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
128526     }
128527   }
128528 }
128529 
128530 /*
128531 ** Arguments pList/nList contain the doclist for token iToken of phrase p.
128532 ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
128533 **
128534 ** This function assumes that pList points to a buffer allocated using
128535 ** sqlite3_malloc(). This function takes responsibility for eventually
128536 ** freeing the buffer.
128537 */
128538 static void fts3EvalPhraseMergeToken(
128539   Fts3Table *pTab,                /* FTS Table pointer */
128540   Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
128541   int iToken,                     /* Token pList/nList corresponds to */
128542   char *pList,                    /* Pointer to doclist */
128543   int nList                       /* Number of bytes in pList */
128544 ){
128545   assert( iToken!=p->iDoclistToken );
128546 
128547   if( pList==0 ){
128548     sqlite3_free(p->doclist.aAll);
128549     p->doclist.aAll = 0;
128550     p->doclist.nAll = 0;
128551   }
128552 
128553   else if( p->iDoclistToken<0 ){
128554     p->doclist.aAll = pList;
128555     p->doclist.nAll = nList;
128556   }
128557 
128558   else if( p->doclist.aAll==0 ){
128559     sqlite3_free(pList);
128560   }
128561 
128562   else {
128563     char *pLeft;
128564     char *pRight;
128565     int nLeft;
128566     int nRight;
128567     int nDiff;
128568 
128569     if( p->iDoclistToken<iToken ){
128570       pLeft = p->doclist.aAll;
128571       nLeft = p->doclist.nAll;
128572       pRight = pList;
128573       nRight = nList;
128574       nDiff = iToken - p->iDoclistToken;
128575     }else{
128576       pRight = p->doclist.aAll;
128577       nRight = p->doclist.nAll;
128578       pLeft = pList;
128579       nLeft = nList;
128580       nDiff = p->iDoclistToken - iToken;
128581     }
128582 
128583     fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
128584     sqlite3_free(pLeft);
128585     p->doclist.aAll = pRight;
128586     p->doclist.nAll = nRight;
128587   }
128588 
128589   if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
128590 }
128591 
128592 /*
128593 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
128594 ** does not take deferred tokens into account.
128595 **
128596 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
128597 */
128598 static int fts3EvalPhraseLoad(
128599   Fts3Cursor *pCsr,               /* FTS Cursor handle */
128600   Fts3Phrase *p                   /* Phrase object */
128601 ){
128602   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
128603   int iToken;
128604   int rc = SQLITE_OK;
128605 
128606   for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
128607     Fts3PhraseToken *pToken = &p->aToken[iToken];
128608     assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
128609 
128610     if( pToken->pSegcsr ){
128611       int nThis = 0;
128612       char *pThis = 0;
128613       rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
128614       if( rc==SQLITE_OK ){
128615         fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
128616       }
128617     }
128618     assert( pToken->pSegcsr==0 );
128619   }
128620 
128621   return rc;
128622 }
128623 
128624 /*
128625 ** This function is called on each phrase after the position lists for
128626 ** any deferred tokens have been loaded into memory. It updates the phrases
128627 ** current position list to include only those positions that are really
128628 ** instances of the phrase (after considering deferred tokens). If this
128629 ** means that the phrase does not appear in the current row, doclist.pList
128630 ** and doclist.nList are both zeroed.
128631 **
128632 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
128633 */
128634 static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
128635   int iToken;                     /* Used to iterate through phrase tokens */
128636   char *aPoslist = 0;             /* Position list for deferred tokens */
128637   int nPoslist = 0;               /* Number of bytes in aPoslist */
128638   int iPrev = -1;                 /* Token number of previous deferred token */
128639 
128640   assert( pPhrase->doclist.bFreeList==0 );
128641 
128642   for(iToken=0; iToken<pPhrase->nToken; iToken++){
128643     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
128644     Fts3DeferredToken *pDeferred = pToken->pDeferred;
128645 
128646     if( pDeferred ){
128647       char *pList;
128648       int nList;
128649       int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
128650       if( rc!=SQLITE_OK ) return rc;
128651 
128652       if( pList==0 ){
128653         sqlite3_free(aPoslist);
128654         pPhrase->doclist.pList = 0;
128655         pPhrase->doclist.nList = 0;
128656         return SQLITE_OK;
128657 
128658       }else if( aPoslist==0 ){
128659         aPoslist = pList;
128660         nPoslist = nList;
128661 
128662       }else{
128663         char *aOut = pList;
128664         char *p1 = aPoslist;
128665         char *p2 = aOut;
128666 
128667         assert( iPrev>=0 );
128668         fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
128669         sqlite3_free(aPoslist);
128670         aPoslist = pList;
128671         nPoslist = (int)(aOut - aPoslist);
128672         if( nPoslist==0 ){
128673           sqlite3_free(aPoslist);
128674           pPhrase->doclist.pList = 0;
128675           pPhrase->doclist.nList = 0;
128676           return SQLITE_OK;
128677         }
128678       }
128679       iPrev = iToken;
128680     }
128681   }
128682 
128683   if( iPrev>=0 ){
128684     int nMaxUndeferred = pPhrase->iDoclistToken;
128685     if( nMaxUndeferred<0 ){
128686       pPhrase->doclist.pList = aPoslist;
128687       pPhrase->doclist.nList = nPoslist;
128688       pPhrase->doclist.iDocid = pCsr->iPrevId;
128689       pPhrase->doclist.bFreeList = 1;
128690     }else{
128691       int nDistance;
128692       char *p1;
128693       char *p2;
128694       char *aOut;
128695 
128696       if( nMaxUndeferred>iPrev ){
128697         p1 = aPoslist;
128698         p2 = pPhrase->doclist.pList;
128699         nDistance = nMaxUndeferred - iPrev;
128700       }else{
128701         p1 = pPhrase->doclist.pList;
128702         p2 = aPoslist;
128703         nDistance = iPrev - nMaxUndeferred;
128704       }
128705 
128706       aOut = (char *)sqlite3_malloc(nPoslist+8);
128707       if( !aOut ){
128708         sqlite3_free(aPoslist);
128709         return SQLITE_NOMEM;
128710       }
128711 
128712       pPhrase->doclist.pList = aOut;
128713       if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
128714         pPhrase->doclist.bFreeList = 1;
128715         pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
128716       }else{
128717         sqlite3_free(aOut);
128718         pPhrase->doclist.pList = 0;
128719         pPhrase->doclist.nList = 0;
128720       }
128721       sqlite3_free(aPoslist);
128722     }
128723   }
128724 
128725   return SQLITE_OK;
128726 }
128727 
128728 /*
128729 ** Maximum number of tokens a phrase may have to be considered for the
128730 ** incremental doclists strategy.
128731 */
128732 #define MAX_INCR_PHRASE_TOKENS 4
128733 
128734 /*
128735 ** This function is called for each Fts3Phrase in a full-text query
128736 ** expression to initialize the mechanism for returning rows. Once this
128737 ** function has been called successfully on an Fts3Phrase, it may be
128738 ** used with fts3EvalPhraseNext() to iterate through the matching docids.
128739 **
128740 ** If parameter bOptOk is true, then the phrase may (or may not) use the
128741 ** incremental loading strategy. Otherwise, the entire doclist is loaded into
128742 ** memory within this call.
128743 **
128744 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
128745 */
128746 static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
128747   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
128748   int rc = SQLITE_OK;             /* Error code */
128749   int i;
128750 
128751   /* Determine if doclists may be loaded from disk incrementally. This is
128752   ** possible if the bOptOk argument is true, the FTS doclists will be
128753   ** scanned in forward order, and the phrase consists of
128754   ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
128755   ** tokens or prefix tokens that cannot use a prefix-index.  */
128756   int bHaveIncr = 0;
128757   int bIncrOk = (bOptOk
128758    && pCsr->bDesc==pTab->bDescIdx
128759    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
128760    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
128761 #ifdef SQLITE_TEST
128762    && pTab->bNoIncrDoclist==0
128763 #endif
128764   );
128765   for(i=0; bIncrOk==1 && i<p->nToken; i++){
128766     Fts3PhraseToken *pToken = &p->aToken[i];
128767     if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
128768       bIncrOk = 0;
128769     }
128770     if( pToken->pSegcsr ) bHaveIncr = 1;
128771   }
128772 
128773   if( bIncrOk && bHaveIncr ){
128774     /* Use the incremental approach. */
128775     int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
128776     for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
128777       Fts3PhraseToken *pToken = &p->aToken[i];
128778       Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
128779       if( pSegcsr ){
128780         rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
128781       }
128782     }
128783     p->bIncr = 1;
128784   }else{
128785     /* Load the full doclist for the phrase into memory. */
128786     rc = fts3EvalPhraseLoad(pCsr, p);
128787     p->bIncr = 0;
128788   }
128789 
128790   assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
128791   return rc;
128792 }
128793 
128794 /*
128795 ** This function is used to iterate backwards (from the end to start)
128796 ** through doclists. It is used by this module to iterate through phrase
128797 ** doclists in reverse and by the fts3_write.c module to iterate through
128798 ** pending-terms lists when writing to databases with "order=desc".
128799 **
128800 ** The doclist may be sorted in ascending (parameter bDescIdx==0) or
128801 ** descending (parameter bDescIdx==1) order of docid. Regardless, this
128802 ** function iterates from the end of the doclist to the beginning.
128803 */
128804 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
128805   int bDescIdx,                   /* True if the doclist is desc */
128806   char *aDoclist,                 /* Pointer to entire doclist */
128807   int nDoclist,                   /* Length of aDoclist in bytes */
128808   char **ppIter,                  /* IN/OUT: Iterator pointer */
128809   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
128810   int *pnList,                    /* OUT: List length pointer */
128811   u8 *pbEof                       /* OUT: End-of-file flag */
128812 ){
128813   char *p = *ppIter;
128814 
128815   assert( nDoclist>0 );
128816   assert( *pbEof==0 );
128817   assert( p || *piDocid==0 );
128818   assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
128819 
128820   if( p==0 ){
128821     sqlite3_int64 iDocid = 0;
128822     char *pNext = 0;
128823     char *pDocid = aDoclist;
128824     char *pEnd = &aDoclist[nDoclist];
128825     int iMul = 1;
128826 
128827     while( pDocid<pEnd ){
128828       sqlite3_int64 iDelta;
128829       pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
128830       iDocid += (iMul * iDelta);
128831       pNext = pDocid;
128832       fts3PoslistCopy(0, &pDocid);
128833       while( pDocid<pEnd && *pDocid==0 ) pDocid++;
128834       iMul = (bDescIdx ? -1 : 1);
128835     }
128836 
128837     *pnList = (int)(pEnd - pNext);
128838     *ppIter = pNext;
128839     *piDocid = iDocid;
128840   }else{
128841     int iMul = (bDescIdx ? -1 : 1);
128842     sqlite3_int64 iDelta;
128843     fts3GetReverseVarint(&p, aDoclist, &iDelta);
128844     *piDocid -= (iMul * iDelta);
128845 
128846     if( p==aDoclist ){
128847       *pbEof = 1;
128848     }else{
128849       char *pSave = p;
128850       fts3ReversePoslist(aDoclist, &p);
128851       *pnList = (int)(pSave - p);
128852     }
128853     *ppIter = p;
128854   }
128855 }
128856 
128857 /*
128858 ** Iterate forwards through a doclist.
128859 */
128860 SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
128861   int bDescIdx,                   /* True if the doclist is desc */
128862   char *aDoclist,                 /* Pointer to entire doclist */
128863   int nDoclist,                   /* Length of aDoclist in bytes */
128864   char **ppIter,                  /* IN/OUT: Iterator pointer */
128865   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
128866   u8 *pbEof                       /* OUT: End-of-file flag */
128867 ){
128868   char *p = *ppIter;
128869 
128870   assert( nDoclist>0 );
128871   assert( *pbEof==0 );
128872   assert( p || *piDocid==0 );
128873   assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
128874 
128875   if( p==0 ){
128876     p = aDoclist;
128877     p += sqlite3Fts3GetVarint(p, piDocid);
128878   }else{
128879     fts3PoslistCopy(0, &p);
128880     if( p>=&aDoclist[nDoclist] ){
128881       *pbEof = 1;
128882     }else{
128883       sqlite3_int64 iVar;
128884       p += sqlite3Fts3GetVarint(p, &iVar);
128885       *piDocid += ((bDescIdx ? -1 : 1) * iVar);
128886     }
128887   }
128888 
128889   *ppIter = p;
128890 }
128891 
128892 /*
128893 ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
128894 ** to true if EOF is reached.
128895 */
128896 static void fts3EvalDlPhraseNext(
128897   Fts3Table *pTab,
128898   Fts3Doclist *pDL,
128899   u8 *pbEof
128900 ){
128901   char *pIter;                            /* Used to iterate through aAll */
128902   char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
128903 
128904   if( pDL->pNextDocid ){
128905     pIter = pDL->pNextDocid;
128906   }else{
128907     pIter = pDL->aAll;
128908   }
128909 
128910   if( pIter>=pEnd ){
128911     /* We have already reached the end of this doclist. EOF. */
128912     *pbEof = 1;
128913   }else{
128914     sqlite3_int64 iDelta;
128915     pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
128916     if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
128917       pDL->iDocid += iDelta;
128918     }else{
128919       pDL->iDocid -= iDelta;
128920     }
128921     pDL->pList = pIter;
128922     fts3PoslistCopy(0, &pIter);
128923     pDL->nList = (int)(pIter - pDL->pList);
128924 
128925     /* pIter now points just past the 0x00 that terminates the position-
128926     ** list for document pDL->iDocid. However, if this position-list was
128927     ** edited in place by fts3EvalNearTrim(), then pIter may not actually
128928     ** point to the start of the next docid value. The following line deals
128929     ** with this case by advancing pIter past the zero-padding added by
128930     ** fts3EvalNearTrim().  */
128931     while( pIter<pEnd && *pIter==0 ) pIter++;
128932 
128933     pDL->pNextDocid = pIter;
128934     assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
128935     *pbEof = 0;
128936   }
128937 }
128938 
128939 /*
128940 ** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
128941 */
128942 typedef struct TokenDoclist TokenDoclist;
128943 struct TokenDoclist {
128944   int bIgnore;
128945   sqlite3_int64 iDocid;
128946   char *pList;
128947   int nList;
128948 };
128949 
128950 /*
128951 ** Token pToken is an incrementally loaded token that is part of a
128952 ** multi-token phrase. Advance it to the next matching document in the
128953 ** database and populate output variable *p with the details of the new
128954 ** entry. Or, if the iterator has reached EOF, set *pbEof to true.
128955 **
128956 ** If an error occurs, return an SQLite error code. Otherwise, return
128957 ** SQLITE_OK.
128958 */
128959 static int incrPhraseTokenNext(
128960   Fts3Table *pTab,                /* Virtual table handle */
128961   Fts3Phrase *pPhrase,            /* Phrase to advance token of */
128962   int iToken,                     /* Specific token to advance */
128963   TokenDoclist *p,                /* OUT: Docid and doclist for new entry */
128964   u8 *pbEof                       /* OUT: True if iterator is at EOF */
128965 ){
128966   int rc = SQLITE_OK;
128967 
128968   if( pPhrase->iDoclistToken==iToken ){
128969     assert( p->bIgnore==0 );
128970     assert( pPhrase->aToken[iToken].pSegcsr==0 );
128971     fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
128972     p->pList = pPhrase->doclist.pList;
128973     p->nList = pPhrase->doclist.nList;
128974     p->iDocid = pPhrase->doclist.iDocid;
128975   }else{
128976     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
128977     assert( pToken->pDeferred==0 );
128978     assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
128979     if( pToken->pSegcsr ){
128980       assert( p->bIgnore==0 );
128981       rc = sqlite3Fts3MsrIncrNext(
128982           pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
128983       );
128984       if( p->pList==0 ) *pbEof = 1;
128985     }else{
128986       p->bIgnore = 1;
128987     }
128988   }
128989 
128990   return rc;
128991 }
128992 
128993 
128994 /*
128995 ** The phrase iterator passed as the second argument:
128996 **
128997 **   * features at least one token that uses an incremental doclist, and
128998 **
128999 **   * does not contain any deferred tokens.
129000 **
129001 ** Advance it to the next matching documnent in the database and populate
129002 ** the Fts3Doclist.pList and nList fields.
129003 **
129004 ** If there is no "next" entry and no error occurs, then *pbEof is set to
129005 ** 1 before returning. Otherwise, if no error occurs and the iterator is
129006 ** successfully advanced, *pbEof is set to 0.
129007 **
129008 ** If an error occurs, return an SQLite error code. Otherwise, return
129009 ** SQLITE_OK.
129010 */
129011 static int fts3EvalIncrPhraseNext(
129012   Fts3Cursor *pCsr,               /* FTS Cursor handle */
129013   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
129014   u8 *pbEof                       /* OUT: Set to 1 if EOF */
129015 ){
129016   int rc = SQLITE_OK;
129017   Fts3Doclist *pDL = &p->doclist;
129018   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
129019   u8 bEof = 0;
129020 
129021   /* This is only called if it is guaranteed that the phrase has at least
129022   ** one incremental token. In which case the bIncr flag is set. */
129023   assert( p->bIncr==1 );
129024 
129025   if( p->nToken==1 && p->bIncr ){
129026     rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
129027         &pDL->iDocid, &pDL->pList, &pDL->nList
129028     );
129029     if( pDL->pList==0 ) bEof = 1;
129030   }else{
129031     int bDescDoclist = pCsr->bDesc;
129032     struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
129033 
129034     memset(a, 0, sizeof(a));
129035     assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
129036     assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
129037 
129038     while( bEof==0 ){
129039       int bMaxSet = 0;
129040       sqlite3_int64 iMax = 0;     /* Largest docid for all iterators */
129041       int i;                      /* Used to iterate through tokens */
129042 
129043       /* Advance the iterator for each token in the phrase once. */
129044       for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
129045         rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
129046         if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
129047           iMax = a[i].iDocid;
129048           bMaxSet = 1;
129049         }
129050       }
129051       assert( rc!=SQLITE_OK || a[p->nToken-1].bIgnore==0 );
129052       assert( rc!=SQLITE_OK || bMaxSet );
129053 
129054       /* Keep advancing iterators until they all point to the same document */
129055       for(i=0; i<p->nToken; i++){
129056         while( rc==SQLITE_OK && bEof==0
129057             && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
129058         ){
129059           rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
129060           if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
129061             iMax = a[i].iDocid;
129062             i = 0;
129063           }
129064         }
129065       }
129066 
129067       /* Check if the current entries really are a phrase match */
129068       if( bEof==0 ){
129069         int nList = 0;
129070         int nByte = a[p->nToken-1].nList;
129071         char *aDoclist = sqlite3_malloc(nByte+1);
129072         if( !aDoclist ) return SQLITE_NOMEM;
129073         memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
129074 
129075         for(i=0; i<(p->nToken-1); i++){
129076           if( a[i].bIgnore==0 ){
129077             char *pL = a[i].pList;
129078             char *pR = aDoclist;
129079             char *pOut = aDoclist;
129080             int nDist = p->nToken-1-i;
129081             int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
129082             if( res==0 ) break;
129083             nList = (int)(pOut - aDoclist);
129084           }
129085         }
129086         if( i==(p->nToken-1) ){
129087           pDL->iDocid = iMax;
129088           pDL->pList = aDoclist;
129089           pDL->nList = nList;
129090           pDL->bFreeList = 1;
129091           break;
129092         }
129093         sqlite3_free(aDoclist);
129094       }
129095     }
129096   }
129097 
129098   *pbEof = bEof;
129099   return rc;
129100 }
129101 
129102 /*
129103 ** Attempt to move the phrase iterator to point to the next matching docid.
129104 ** If an error occurs, return an SQLite error code. Otherwise, return
129105 ** SQLITE_OK.
129106 **
129107 ** If there is no "next" entry and no error occurs, then *pbEof is set to
129108 ** 1 before returning. Otherwise, if no error occurs and the iterator is
129109 ** successfully advanced, *pbEof is set to 0.
129110 */
129111 static int fts3EvalPhraseNext(
129112   Fts3Cursor *pCsr,               /* FTS Cursor handle */
129113   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
129114   u8 *pbEof                       /* OUT: Set to 1 if EOF */
129115 ){
129116   int rc = SQLITE_OK;
129117   Fts3Doclist *pDL = &p->doclist;
129118   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
129119 
129120   if( p->bIncr ){
129121     rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
129122   }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
129123     sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
129124         &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
129125     );
129126     pDL->pList = pDL->pNextDocid;
129127   }else{
129128     fts3EvalDlPhraseNext(pTab, pDL, pbEof);
129129   }
129130 
129131   return rc;
129132 }
129133 
129134 /*
129135 **
129136 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
129137 ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
129138 ** expression. Also the Fts3Expr.bDeferred variable is set to true for any
129139 ** expressions for which all descendent tokens are deferred.
129140 **
129141 ** If parameter bOptOk is zero, then it is guaranteed that the
129142 ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
129143 ** each phrase in the expression (subject to deferred token processing).
129144 ** Or, if bOptOk is non-zero, then one or more tokens within the expression
129145 ** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
129146 **
129147 ** If an error occurs within this function, *pRc is set to an SQLite error
129148 ** code before returning.
129149 */
129150 static void fts3EvalStartReaders(
129151   Fts3Cursor *pCsr,               /* FTS Cursor handle */
129152   Fts3Expr *pExpr,                /* Expression to initialize phrases in */
129153   int *pRc                        /* IN/OUT: Error code */
129154 ){
129155   if( pExpr && SQLITE_OK==*pRc ){
129156     if( pExpr->eType==FTSQUERY_PHRASE ){
129157       int i;
129158       int nToken = pExpr->pPhrase->nToken;
129159       for(i=0; i<nToken; i++){
129160         if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
129161       }
129162       pExpr->bDeferred = (i==nToken);
129163       *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
129164     }else{
129165       fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
129166       fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
129167       pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
129168     }
129169   }
129170 }
129171 
129172 /*
129173 ** An array of the following structures is assembled as part of the process
129174 ** of selecting tokens to defer before the query starts executing (as part
129175 ** of the xFilter() method). There is one element in the array for each
129176 ** token in the FTS expression.
129177 **
129178 ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
129179 ** to phrases that are connected only by AND and NEAR operators (not OR or
129180 ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
129181 ** separately. The root of a tokens AND/NEAR cluster is stored in
129182 ** Fts3TokenAndCost.pRoot.
129183 */
129184 typedef struct Fts3TokenAndCost Fts3TokenAndCost;
129185 struct Fts3TokenAndCost {
129186   Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
129187   int iToken;                     /* Position of token in phrase */
129188   Fts3PhraseToken *pToken;        /* The token itself */
129189   Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
129190   int nOvfl;                      /* Number of overflow pages to load doclist */
129191   int iCol;                       /* The column the token must match */
129192 };
129193 
129194 /*
129195 ** This function is used to populate an allocated Fts3TokenAndCost array.
129196 **
129197 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
129198 ** Otherwise, if an error occurs during execution, *pRc is set to an
129199 ** SQLite error code.
129200 */
129201 static void fts3EvalTokenCosts(
129202   Fts3Cursor *pCsr,               /* FTS Cursor handle */
129203   Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
129204   Fts3Expr *pExpr,                /* Expression to consider */
129205   Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
129206   Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
129207   int *pRc                        /* IN/OUT: Error code */
129208 ){
129209   if( *pRc==SQLITE_OK ){
129210     if( pExpr->eType==FTSQUERY_PHRASE ){
129211       Fts3Phrase *pPhrase = pExpr->pPhrase;
129212       int i;
129213       for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
129214         Fts3TokenAndCost *pTC = (*ppTC)++;
129215         pTC->pPhrase = pPhrase;
129216         pTC->iToken = i;
129217         pTC->pRoot = pRoot;
129218         pTC->pToken = &pPhrase->aToken[i];
129219         pTC->iCol = pPhrase->iColumn;
129220         *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
129221       }
129222     }else if( pExpr->eType!=FTSQUERY_NOT ){
129223       assert( pExpr->eType==FTSQUERY_OR
129224            || pExpr->eType==FTSQUERY_AND
129225            || pExpr->eType==FTSQUERY_NEAR
129226       );
129227       assert( pExpr->pLeft && pExpr->pRight );
129228       if( pExpr->eType==FTSQUERY_OR ){
129229         pRoot = pExpr->pLeft;
129230         **ppOr = pRoot;
129231         (*ppOr)++;
129232       }
129233       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
129234       if( pExpr->eType==FTSQUERY_OR ){
129235         pRoot = pExpr->pRight;
129236         **ppOr = pRoot;
129237         (*ppOr)++;
129238       }
129239       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
129240     }
129241   }
129242 }
129243 
129244 /*
129245 ** Determine the average document (row) size in pages. If successful,
129246 ** write this value to *pnPage and return SQLITE_OK. Otherwise, return
129247 ** an SQLite error code.
129248 **
129249 ** The average document size in pages is calculated by first calculating
129250 ** determining the average size in bytes, B. If B is less than the amount
129251 ** of data that will fit on a single leaf page of an intkey table in
129252 ** this database, then the average docsize is 1. Otherwise, it is 1 plus
129253 ** the number of overflow pages consumed by a record B bytes in size.
129254 */
129255 static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
129256   if( pCsr->nRowAvg==0 ){
129257     /* The average document size, which is required to calculate the cost
129258     ** of each doclist, has not yet been determined. Read the required
129259     ** data from the %_stat table to calculate it.
129260     **
129261     ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
129262     ** varints, where nCol is the number of columns in the FTS3 table.
129263     ** The first varint is the number of documents currently stored in
129264     ** the table. The following nCol varints contain the total amount of
129265     ** data stored in all rows of each column of the table, from left
129266     ** to right.
129267     */
129268     int rc;
129269     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
129270     sqlite3_stmt *pStmt;
129271     sqlite3_int64 nDoc = 0;
129272     sqlite3_int64 nByte = 0;
129273     const char *pEnd;
129274     const char *a;
129275 
129276     rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
129277     if( rc!=SQLITE_OK ) return rc;
129278     a = sqlite3_column_blob(pStmt, 0);
129279     assert( a );
129280 
129281     pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
129282     a += sqlite3Fts3GetVarint(a, &nDoc);
129283     while( a<pEnd ){
129284       a += sqlite3Fts3GetVarint(a, &nByte);
129285     }
129286     if( nDoc==0 || nByte==0 ){
129287       sqlite3_reset(pStmt);
129288       return FTS_CORRUPT_VTAB;
129289     }
129290 
129291     pCsr->nDoc = nDoc;
129292     pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
129293     assert( pCsr->nRowAvg>0 );
129294     rc = sqlite3_reset(pStmt);
129295     if( rc!=SQLITE_OK ) return rc;
129296   }
129297 
129298   *pnPage = pCsr->nRowAvg;
129299   return SQLITE_OK;
129300 }
129301 
129302 /*
129303 ** This function is called to select the tokens (if any) that will be
129304 ** deferred. The array aTC[] has already been populated when this is
129305 ** called.
129306 **
129307 ** This function is called once for each AND/NEAR cluster in the
129308 ** expression. Each invocation determines which tokens to defer within
129309 ** the cluster with root node pRoot. See comments above the definition
129310 ** of struct Fts3TokenAndCost for more details.
129311 **
129312 ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
129313 ** called on each token to defer. Otherwise, an SQLite error code is
129314 ** returned.
129315 */
129316 static int fts3EvalSelectDeferred(
129317   Fts3Cursor *pCsr,               /* FTS Cursor handle */
129318   Fts3Expr *pRoot,                /* Consider tokens with this root node */
129319   Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
129320   int nTC                         /* Number of entries in aTC[] */
129321 ){
129322   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
129323   int nDocSize = 0;               /* Number of pages per doc loaded */
129324   int rc = SQLITE_OK;             /* Return code */
129325   int ii;                         /* Iterator variable for various purposes */
129326   int nOvfl = 0;                  /* Total overflow pages used by doclists */
129327   int nToken = 0;                 /* Total number of tokens in cluster */
129328 
129329   int nMinEst = 0;                /* The minimum count for any phrase so far. */
129330   int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
129331 
129332   /* Tokens are never deferred for FTS tables created using the content=xxx
129333   ** option. The reason being that it is not guaranteed that the content
129334   ** table actually contains the same data as the index. To prevent this from
129335   ** causing any problems, the deferred token optimization is completely
129336   ** disabled for content=xxx tables. */
129337   if( pTab->zContentTbl ){
129338     return SQLITE_OK;
129339   }
129340 
129341   /* Count the tokens in this AND/NEAR cluster. If none of the doclists
129342   ** associated with the tokens spill onto overflow pages, or if there is
129343   ** only 1 token, exit early. No tokens to defer in this case. */
129344   for(ii=0; ii<nTC; ii++){
129345     if( aTC[ii].pRoot==pRoot ){
129346       nOvfl += aTC[ii].nOvfl;
129347       nToken++;
129348     }
129349   }
129350   if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
129351 
129352   /* Obtain the average docsize (in pages). */
129353   rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
129354   assert( rc!=SQLITE_OK || nDocSize>0 );
129355 
129356 
129357   /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
129358   ** of the number of overflow pages that will be loaded by the pager layer
129359   ** to retrieve the entire doclist for the token from the full-text index.
129360   ** Load the doclists for tokens that are either:
129361   **
129362   **   a. The cheapest token in the entire query (i.e. the one visited by the
129363   **      first iteration of this loop), or
129364   **
129365   **   b. Part of a multi-token phrase.
129366   **
129367   ** After each token doclist is loaded, merge it with the others from the
129368   ** same phrase and count the number of documents that the merged doclist
129369   ** contains. Set variable "nMinEst" to the smallest number of documents in
129370   ** any phrase doclist for which 1 or more token doclists have been loaded.
129371   ** Let nOther be the number of other phrases for which it is certain that
129372   ** one or more tokens will not be deferred.
129373   **
129374   ** Then, for each token, defer it if loading the doclist would result in
129375   ** loading N or more overflow pages into memory, where N is computed as:
129376   **
129377   **    (nMinEst + 4^nOther - 1) / (4^nOther)
129378   */
129379   for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
129380     int iTC;                      /* Used to iterate through aTC[] array. */
129381     Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
129382 
129383     /* Set pTC to point to the cheapest remaining token. */
129384     for(iTC=0; iTC<nTC; iTC++){
129385       if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
129386        && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
129387       ){
129388         pTC = &aTC[iTC];
129389       }
129390     }
129391     assert( pTC );
129392 
129393     if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
129394       /* The number of overflow pages to load for this (and therefore all
129395       ** subsequent) tokens is greater than the estimated number of pages
129396       ** that will be loaded if all subsequent tokens are deferred.
129397       */
129398       Fts3PhraseToken *pToken = pTC->pToken;
129399       rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
129400       fts3SegReaderCursorFree(pToken->pSegcsr);
129401       pToken->pSegcsr = 0;
129402     }else{
129403       /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
129404       ** for-loop. Except, limit the value to 2^24 to prevent it from
129405       ** overflowing the 32-bit integer it is stored in. */
129406       if( ii<12 ) nLoad4 = nLoad4*4;
129407 
129408       if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
129409         /* Either this is the cheapest token in the entire query, or it is
129410         ** part of a multi-token phrase. Either way, the entire doclist will
129411         ** (eventually) be loaded into memory. It may as well be now. */
129412         Fts3PhraseToken *pToken = pTC->pToken;
129413         int nList = 0;
129414         char *pList = 0;
129415         rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
129416         assert( rc==SQLITE_OK || pList==0 );
129417         if( rc==SQLITE_OK ){
129418           int nCount;
129419           fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
129420           nCount = fts3DoclistCountDocids(
129421               pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
129422           );
129423           if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
129424         }
129425       }
129426     }
129427     pTC->pToken = 0;
129428   }
129429 
129430   return rc;
129431 }
129432 
129433 /*
129434 ** This function is called from within the xFilter method. It initializes
129435 ** the full-text query currently stored in pCsr->pExpr. To iterate through
129436 ** the results of a query, the caller does:
129437 **
129438 **    fts3EvalStart(pCsr);
129439 **    while( 1 ){
129440 **      fts3EvalNext(pCsr);
129441 **      if( pCsr->bEof ) break;
129442 **      ... return row pCsr->iPrevId to the caller ...
129443 **    }
129444 */
129445 static int fts3EvalStart(Fts3Cursor *pCsr){
129446   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
129447   int rc = SQLITE_OK;
129448   int nToken = 0;
129449   int nOr = 0;
129450 
129451   /* Allocate a MultiSegReader for each token in the expression. */
129452   fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
129453 
129454   /* Determine which, if any, tokens in the expression should be deferred. */
129455 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
129456   if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
129457     Fts3TokenAndCost *aTC;
129458     Fts3Expr **apOr;
129459     aTC = (Fts3TokenAndCost *)sqlite3_malloc(
129460         sizeof(Fts3TokenAndCost) * nToken
129461       + sizeof(Fts3Expr *) * nOr * 2
129462     );
129463     apOr = (Fts3Expr **)&aTC[nToken];
129464 
129465     if( !aTC ){
129466       rc = SQLITE_NOMEM;
129467     }else{
129468       int ii;
129469       Fts3TokenAndCost *pTC = aTC;
129470       Fts3Expr **ppOr = apOr;
129471 
129472       fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
129473       nToken = (int)(pTC-aTC);
129474       nOr = (int)(ppOr-apOr);
129475 
129476       if( rc==SQLITE_OK ){
129477         rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
129478         for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
129479           rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
129480         }
129481       }
129482 
129483       sqlite3_free(aTC);
129484     }
129485   }
129486 #endif
129487 
129488   fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
129489   return rc;
129490 }
129491 
129492 /*
129493 ** Invalidate the current position list for phrase pPhrase.
129494 */
129495 static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
129496   if( pPhrase->doclist.bFreeList ){
129497     sqlite3_free(pPhrase->doclist.pList);
129498   }
129499   pPhrase->doclist.pList = 0;
129500   pPhrase->doclist.nList = 0;
129501   pPhrase->doclist.bFreeList = 0;
129502 }
129503 
129504 /*
129505 ** This function is called to edit the position list associated with
129506 ** the phrase object passed as the fifth argument according to a NEAR
129507 ** condition. For example:
129508 **
129509 **     abc NEAR/5 "def ghi"
129510 **
129511 ** Parameter nNear is passed the NEAR distance of the expression (5 in
129512 ** the example above). When this function is called, *paPoslist points to
129513 ** the position list, and *pnToken is the number of phrase tokens in, the
129514 ** phrase on the other side of the NEAR operator to pPhrase. For example,
129515 ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
129516 ** the position list associated with phrase "abc".
129517 **
129518 ** All positions in the pPhrase position list that are not sufficiently
129519 ** close to a position in the *paPoslist position list are removed. If this
129520 ** leaves 0 positions, zero is returned. Otherwise, non-zero.
129521 **
129522 ** Before returning, *paPoslist is set to point to the position lsit
129523 ** associated with pPhrase. And *pnToken is set to the number of tokens in
129524 ** pPhrase.
129525 */
129526 static int fts3EvalNearTrim(
129527   int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
129528   char *aTmp,                     /* Temporary space to use */
129529   char **paPoslist,               /* IN/OUT: Position list */
129530   int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
129531   Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
129532 ){
129533   int nParam1 = nNear + pPhrase->nToken;
129534   int nParam2 = nNear + *pnToken;
129535   int nNew;
129536   char *p2;
129537   char *pOut;
129538   int res;
129539 
129540   assert( pPhrase->doclist.pList );
129541 
129542   p2 = pOut = pPhrase->doclist.pList;
129543   res = fts3PoslistNearMerge(
129544     &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
129545   );
129546   if( res ){
129547     nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
129548     assert( pPhrase->doclist.pList[nNew]=='\0' );
129549     assert( nNew<=pPhrase->doclist.nList && nNew>0 );
129550     memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
129551     pPhrase->doclist.nList = nNew;
129552     *paPoslist = pPhrase->doclist.pList;
129553     *pnToken = pPhrase->nToken;
129554   }
129555 
129556   return res;
129557 }
129558 
129559 /*
129560 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
129561 ** Otherwise, it advances the expression passed as the second argument to
129562 ** point to the next matching row in the database. Expressions iterate through
129563 ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
129564 ** or descending if it is non-zero.
129565 **
129566 ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
129567 ** successful, the following variables in pExpr are set:
129568 **
129569 **   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
129570 **   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
129571 **
129572 ** If the expression is of type FTSQUERY_PHRASE, and the expression is not
129573 ** at EOF, then the following variables are populated with the position list
129574 ** for the phrase for the visited row:
129575 **
129576 **   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
129577 **   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
129578 **
129579 ** It says above that this function advances the expression to the next
129580 ** matching row. This is usually true, but there are the following exceptions:
129581 **
129582 **   1. Deferred tokens are not taken into account. If a phrase consists
129583 **      entirely of deferred tokens, it is assumed to match every row in
129584 **      the db. In this case the position-list is not populated at all.
129585 **
129586 **      Or, if a phrase contains one or more deferred tokens and one or
129587 **      more non-deferred tokens, then the expression is advanced to the
129588 **      next possible match, considering only non-deferred tokens. In other
129589 **      words, if the phrase is "A B C", and "B" is deferred, the expression
129590 **      is advanced to the next row that contains an instance of "A * C",
129591 **      where "*" may match any single token. The position list in this case
129592 **      is populated as for "A * C" before returning.
129593 **
129594 **   2. NEAR is treated as AND. If the expression is "x NEAR y", it is
129595 **      advanced to point to the next row that matches "x AND y".
129596 **
129597 ** See fts3EvalTestDeferredAndNear() for details on testing if a row is
129598 ** really a match, taking into account deferred tokens and NEAR operators.
129599 */
129600 static void fts3EvalNextRow(
129601   Fts3Cursor *pCsr,               /* FTS Cursor handle */
129602   Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
129603   int *pRc                        /* IN/OUT: Error code */
129604 ){
129605   if( *pRc==SQLITE_OK ){
129606     int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
129607     assert( pExpr->bEof==0 );
129608     pExpr->bStart = 1;
129609 
129610     switch( pExpr->eType ){
129611       case FTSQUERY_NEAR:
129612       case FTSQUERY_AND: {
129613         Fts3Expr *pLeft = pExpr->pLeft;
129614         Fts3Expr *pRight = pExpr->pRight;
129615         assert( !pLeft->bDeferred || !pRight->bDeferred );
129616 
129617         if( pLeft->bDeferred ){
129618           /* LHS is entirely deferred. So we assume it matches every row.
129619           ** Advance the RHS iterator to find the next row visited. */
129620           fts3EvalNextRow(pCsr, pRight, pRc);
129621           pExpr->iDocid = pRight->iDocid;
129622           pExpr->bEof = pRight->bEof;
129623         }else if( pRight->bDeferred ){
129624           /* RHS is entirely deferred. So we assume it matches every row.
129625           ** Advance the LHS iterator to find the next row visited. */
129626           fts3EvalNextRow(pCsr, pLeft, pRc);
129627           pExpr->iDocid = pLeft->iDocid;
129628           pExpr->bEof = pLeft->bEof;
129629         }else{
129630           /* Neither the RHS or LHS are deferred. */
129631           fts3EvalNextRow(pCsr, pLeft, pRc);
129632           fts3EvalNextRow(pCsr, pRight, pRc);
129633           while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
129634             sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
129635             if( iDiff==0 ) break;
129636             if( iDiff<0 ){
129637               fts3EvalNextRow(pCsr, pLeft, pRc);
129638             }else{
129639               fts3EvalNextRow(pCsr, pRight, pRc);
129640             }
129641           }
129642           pExpr->iDocid = pLeft->iDocid;
129643           pExpr->bEof = (pLeft->bEof || pRight->bEof);
129644         }
129645         break;
129646       }
129647 
129648       case FTSQUERY_OR: {
129649         Fts3Expr *pLeft = pExpr->pLeft;
129650         Fts3Expr *pRight = pExpr->pRight;
129651         sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
129652 
129653         assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
129654         assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
129655 
129656         if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
129657           fts3EvalNextRow(pCsr, pLeft, pRc);
129658         }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
129659           fts3EvalNextRow(pCsr, pRight, pRc);
129660         }else{
129661           fts3EvalNextRow(pCsr, pLeft, pRc);
129662           fts3EvalNextRow(pCsr, pRight, pRc);
129663         }
129664 
129665         pExpr->bEof = (pLeft->bEof && pRight->bEof);
129666         iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
129667         if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
129668           pExpr->iDocid = pLeft->iDocid;
129669         }else{
129670           pExpr->iDocid = pRight->iDocid;
129671         }
129672 
129673         break;
129674       }
129675 
129676       case FTSQUERY_NOT: {
129677         Fts3Expr *pLeft = pExpr->pLeft;
129678         Fts3Expr *pRight = pExpr->pRight;
129679 
129680         if( pRight->bStart==0 ){
129681           fts3EvalNextRow(pCsr, pRight, pRc);
129682           assert( *pRc!=SQLITE_OK || pRight->bStart );
129683         }
129684 
129685         fts3EvalNextRow(pCsr, pLeft, pRc);
129686         if( pLeft->bEof==0 ){
129687           while( !*pRc
129688               && !pRight->bEof
129689               && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
129690           ){
129691             fts3EvalNextRow(pCsr, pRight, pRc);
129692           }
129693         }
129694         pExpr->iDocid = pLeft->iDocid;
129695         pExpr->bEof = pLeft->bEof;
129696         break;
129697       }
129698 
129699       default: {
129700         Fts3Phrase *pPhrase = pExpr->pPhrase;
129701         fts3EvalInvalidatePoslist(pPhrase);
129702         *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
129703         pExpr->iDocid = pPhrase->doclist.iDocid;
129704         break;
129705       }
129706     }
129707   }
129708 }
129709 
129710 /*
129711 ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
129712 ** cluster, then this function returns 1 immediately.
129713 **
129714 ** Otherwise, it checks if the current row really does match the NEAR
129715 ** expression, using the data currently stored in the position lists
129716 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
129717 **
129718 ** If the current row is a match, the position list associated with each
129719 ** phrase in the NEAR expression is edited in place to contain only those
129720 ** phrase instances sufficiently close to their peers to satisfy all NEAR
129721 ** constraints. In this case it returns 1. If the NEAR expression does not
129722 ** match the current row, 0 is returned. The position lists may or may not
129723 ** be edited if 0 is returned.
129724 */
129725 static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
129726   int res = 1;
129727 
129728   /* The following block runs if pExpr is the root of a NEAR query.
129729   ** For example, the query:
129730   **
129731   **         "w" NEAR "x" NEAR "y" NEAR "z"
129732   **
129733   ** which is represented in tree form as:
129734   **
129735   **                               |
129736   **                          +--NEAR--+      <-- root of NEAR query
129737   **                          |        |
129738   **                     +--NEAR--+   "z"
129739   **                     |        |
129740   **                +--NEAR--+   "y"
129741   **                |        |
129742   **               "w"      "x"
129743   **
129744   ** The right-hand child of a NEAR node is always a phrase. The
129745   ** left-hand child may be either a phrase or a NEAR node. There are
129746   ** no exceptions to this - it's the way the parser in fts3_expr.c works.
129747   */
129748   if( *pRc==SQLITE_OK
129749    && pExpr->eType==FTSQUERY_NEAR
129750    && pExpr->bEof==0
129751    && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
129752   ){
129753     Fts3Expr *p;
129754     int nTmp = 0;                 /* Bytes of temp space */
129755     char *aTmp;                   /* Temp space for PoslistNearMerge() */
129756 
129757     /* Allocate temporary working space. */
129758     for(p=pExpr; p->pLeft; p=p->pLeft){
129759       nTmp += p->pRight->pPhrase->doclist.nList;
129760     }
129761     nTmp += p->pPhrase->doclist.nList;
129762     if( nTmp==0 ){
129763       res = 0;
129764     }else{
129765       aTmp = sqlite3_malloc(nTmp*2);
129766       if( !aTmp ){
129767         *pRc = SQLITE_NOMEM;
129768         res = 0;
129769       }else{
129770         char *aPoslist = p->pPhrase->doclist.pList;
129771         int nToken = p->pPhrase->nToken;
129772 
129773         for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
129774           Fts3Phrase *pPhrase = p->pRight->pPhrase;
129775           int nNear = p->nNear;
129776           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
129777         }
129778 
129779         aPoslist = pExpr->pRight->pPhrase->doclist.pList;
129780         nToken = pExpr->pRight->pPhrase->nToken;
129781         for(p=pExpr->pLeft; p && res; p=p->pLeft){
129782           int nNear;
129783           Fts3Phrase *pPhrase;
129784           assert( p->pParent && p->pParent->pLeft==p );
129785           nNear = p->pParent->nNear;
129786           pPhrase = (
129787               p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
129788               );
129789           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
129790         }
129791       }
129792 
129793       sqlite3_free(aTmp);
129794     }
129795   }
129796 
129797   return res;
129798 }
129799 
129800 /*
129801 ** This function is a helper function for fts3EvalTestDeferredAndNear().
129802 ** Assuming no error occurs or has occurred, It returns non-zero if the
129803 ** expression passed as the second argument matches the row that pCsr
129804 ** currently points to, or zero if it does not.
129805 **
129806 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
129807 ** If an error occurs during execution of this function, *pRc is set to
129808 ** the appropriate SQLite error code. In this case the returned value is
129809 ** undefined.
129810 */
129811 static int fts3EvalTestExpr(
129812   Fts3Cursor *pCsr,               /* FTS cursor handle */
129813   Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
129814   int *pRc                        /* IN/OUT: Error code */
129815 ){
129816   int bHit = 1;                   /* Return value */
129817   if( *pRc==SQLITE_OK ){
129818     switch( pExpr->eType ){
129819       case FTSQUERY_NEAR:
129820       case FTSQUERY_AND:
129821         bHit = (
129822             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
129823          && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
129824          && fts3EvalNearTest(pExpr, pRc)
129825         );
129826 
129827         /* If the NEAR expression does not match any rows, zero the doclist for
129828         ** all phrases involved in the NEAR. This is because the snippet(),
129829         ** offsets() and matchinfo() functions are not supposed to recognize
129830         ** any instances of phrases that are part of unmatched NEAR queries.
129831         ** For example if this expression:
129832         **
129833         **    ... MATCH 'a OR (b NEAR c)'
129834         **
129835         ** is matched against a row containing:
129836         **
129837         **        'a b d e'
129838         **
129839         ** then any snippet() should ony highlight the "a" term, not the "b"
129840         ** (as "b" is part of a non-matching NEAR clause).
129841         */
129842         if( bHit==0
129843          && pExpr->eType==FTSQUERY_NEAR
129844          && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
129845         ){
129846           Fts3Expr *p;
129847           for(p=pExpr; p->pPhrase==0; p=p->pLeft){
129848             if( p->pRight->iDocid==pCsr->iPrevId ){
129849               fts3EvalInvalidatePoslist(p->pRight->pPhrase);
129850             }
129851           }
129852           if( p->iDocid==pCsr->iPrevId ){
129853             fts3EvalInvalidatePoslist(p->pPhrase);
129854           }
129855         }
129856 
129857         break;
129858 
129859       case FTSQUERY_OR: {
129860         int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
129861         int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
129862         bHit = bHit1 || bHit2;
129863         break;
129864       }
129865 
129866       case FTSQUERY_NOT:
129867         bHit = (
129868             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
129869          && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
129870         );
129871         break;
129872 
129873       default: {
129874 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
129875         if( pCsr->pDeferred
129876          && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
129877         ){
129878           Fts3Phrase *pPhrase = pExpr->pPhrase;
129879           assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
129880           if( pExpr->bDeferred ){
129881             fts3EvalInvalidatePoslist(pPhrase);
129882           }
129883           *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
129884           bHit = (pPhrase->doclist.pList!=0);
129885           pExpr->iDocid = pCsr->iPrevId;
129886         }else
129887 #endif
129888         {
129889           bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
129890         }
129891         break;
129892       }
129893     }
129894   }
129895   return bHit;
129896 }
129897 
129898 /*
129899 ** This function is called as the second part of each xNext operation when
129900 ** iterating through the results of a full-text query. At this point the
129901 ** cursor points to a row that matches the query expression, with the
129902 ** following caveats:
129903 **
129904 **   * Up until this point, "NEAR" operators in the expression have been
129905 **     treated as "AND".
129906 **
129907 **   * Deferred tokens have not yet been considered.
129908 **
129909 ** If *pRc is not SQLITE_OK when this function is called, it immediately
129910 ** returns 0. Otherwise, it tests whether or not after considering NEAR
129911 ** operators and deferred tokens the current row is still a match for the
129912 ** expression. It returns 1 if both of the following are true:
129913 **
129914 **   1. *pRc is SQLITE_OK when this function returns, and
129915 **
129916 **   2. After scanning the current FTS table row for the deferred tokens,
129917 **      it is determined that the row does *not* match the query.
129918 **
129919 ** Or, if no error occurs and it seems the current row does match the FTS
129920 ** query, return 0.
129921 */
129922 static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
129923   int rc = *pRc;
129924   int bMiss = 0;
129925   if( rc==SQLITE_OK ){
129926 
129927     /* If there are one or more deferred tokens, load the current row into
129928     ** memory and scan it to determine the position list for each deferred
129929     ** token. Then, see if this row is really a match, considering deferred
129930     ** tokens and NEAR operators (neither of which were taken into account
129931     ** earlier, by fts3EvalNextRow()).
129932     */
129933     if( pCsr->pDeferred ){
129934       rc = fts3CursorSeek(0, pCsr);
129935       if( rc==SQLITE_OK ){
129936         rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
129937       }
129938     }
129939     bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
129940 
129941     /* Free the position-lists accumulated for each deferred token above. */
129942     sqlite3Fts3FreeDeferredDoclists(pCsr);
129943     *pRc = rc;
129944   }
129945   return (rc==SQLITE_OK && bMiss);
129946 }
129947 
129948 /*
129949 ** Advance to the next document that matches the FTS expression in
129950 ** Fts3Cursor.pExpr.
129951 */
129952 static int fts3EvalNext(Fts3Cursor *pCsr){
129953   int rc = SQLITE_OK;             /* Return Code */
129954   Fts3Expr *pExpr = pCsr->pExpr;
129955   assert( pCsr->isEof==0 );
129956   if( pExpr==0 ){
129957     pCsr->isEof = 1;
129958   }else{
129959     do {
129960       if( pCsr->isRequireSeek==0 ){
129961         sqlite3_reset(pCsr->pStmt);
129962       }
129963       assert( sqlite3_data_count(pCsr->pStmt)==0 );
129964       fts3EvalNextRow(pCsr, pExpr, &rc);
129965       pCsr->isEof = pExpr->bEof;
129966       pCsr->isRequireSeek = 1;
129967       pCsr->isMatchinfoNeeded = 1;
129968       pCsr->iPrevId = pExpr->iDocid;
129969     }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
129970   }
129971 
129972   /* Check if the cursor is past the end of the docid range specified
129973   ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag.  */
129974   if( rc==SQLITE_OK && (
129975         (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
129976      || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
129977   )){
129978     pCsr->isEof = 1;
129979   }
129980 
129981   return rc;
129982 }
129983 
129984 /*
129985 ** Restart interation for expression pExpr so that the next call to
129986 ** fts3EvalNext() visits the first row. Do not allow incremental
129987 ** loading or merging of phrase doclists for this iteration.
129988 **
129989 ** If *pRc is other than SQLITE_OK when this function is called, it is
129990 ** a no-op. If an error occurs within this function, *pRc is set to an
129991 ** SQLite error code before returning.
129992 */
129993 static void fts3EvalRestart(
129994   Fts3Cursor *pCsr,
129995   Fts3Expr *pExpr,
129996   int *pRc
129997 ){
129998   if( pExpr && *pRc==SQLITE_OK ){
129999     Fts3Phrase *pPhrase = pExpr->pPhrase;
130000 
130001     if( pPhrase ){
130002       fts3EvalInvalidatePoslist(pPhrase);
130003       if( pPhrase->bIncr ){
130004         int i;
130005         for(i=0; i<pPhrase->nToken; i++){
130006           Fts3PhraseToken *pToken = &pPhrase->aToken[i];
130007           assert( pToken->pDeferred==0 );
130008           if( pToken->pSegcsr ){
130009             sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
130010           }
130011         }
130012         *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
130013       }
130014       pPhrase->doclist.pNextDocid = 0;
130015       pPhrase->doclist.iDocid = 0;
130016     }
130017 
130018     pExpr->iDocid = 0;
130019     pExpr->bEof = 0;
130020     pExpr->bStart = 0;
130021 
130022     fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
130023     fts3EvalRestart(pCsr, pExpr->pRight, pRc);
130024   }
130025 }
130026 
130027 /*
130028 ** After allocating the Fts3Expr.aMI[] array for each phrase in the
130029 ** expression rooted at pExpr, the cursor iterates through all rows matched
130030 ** by pExpr, calling this function for each row. This function increments
130031 ** the values in Fts3Expr.aMI[] according to the position-list currently
130032 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
130033 ** expression nodes.
130034 */
130035 static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
130036   if( pExpr ){
130037     Fts3Phrase *pPhrase = pExpr->pPhrase;
130038     if( pPhrase && pPhrase->doclist.pList ){
130039       int iCol = 0;
130040       char *p = pPhrase->doclist.pList;
130041 
130042       assert( *p );
130043       while( 1 ){
130044         u8 c = 0;
130045         int iCnt = 0;
130046         while( 0xFE & (*p | c) ){
130047           if( (c&0x80)==0 ) iCnt++;
130048           c = *p++ & 0x80;
130049         }
130050 
130051         /* aMI[iCol*3 + 1] = Number of occurrences
130052         ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
130053         */
130054         pExpr->aMI[iCol*3 + 1] += iCnt;
130055         pExpr->aMI[iCol*3 + 2] += (iCnt>0);
130056         if( *p==0x00 ) break;
130057         p++;
130058         p += fts3GetVarint32(p, &iCol);
130059       }
130060     }
130061 
130062     fts3EvalUpdateCounts(pExpr->pLeft);
130063     fts3EvalUpdateCounts(pExpr->pRight);
130064   }
130065 }
130066 
130067 /*
130068 ** Expression pExpr must be of type FTSQUERY_PHRASE.
130069 **
130070 ** If it is not already allocated and populated, this function allocates and
130071 ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
130072 ** of a NEAR expression, then it also allocates and populates the same array
130073 ** for all other phrases that are part of the NEAR expression.
130074 **
130075 ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
130076 ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
130077 */
130078 static int fts3EvalGatherStats(
130079   Fts3Cursor *pCsr,               /* Cursor object */
130080   Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
130081 ){
130082   int rc = SQLITE_OK;             /* Return code */
130083 
130084   assert( pExpr->eType==FTSQUERY_PHRASE );
130085   if( pExpr->aMI==0 ){
130086     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
130087     Fts3Expr *pRoot;                /* Root of NEAR expression */
130088     Fts3Expr *p;                    /* Iterator used for several purposes */
130089 
130090     sqlite3_int64 iPrevId = pCsr->iPrevId;
130091     sqlite3_int64 iDocid;
130092     u8 bEof;
130093 
130094     /* Find the root of the NEAR expression */
130095     pRoot = pExpr;
130096     while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
130097       pRoot = pRoot->pParent;
130098     }
130099     iDocid = pRoot->iDocid;
130100     bEof = pRoot->bEof;
130101     assert( pRoot->bStart );
130102 
130103     /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
130104     for(p=pRoot; p; p=p->pLeft){
130105       Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
130106       assert( pE->aMI==0 );
130107       pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
130108       if( !pE->aMI ) return SQLITE_NOMEM;
130109       memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
130110     }
130111 
130112     fts3EvalRestart(pCsr, pRoot, &rc);
130113 
130114     while( pCsr->isEof==0 && rc==SQLITE_OK ){
130115 
130116       do {
130117         /* Ensure the %_content statement is reset. */
130118         if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
130119         assert( sqlite3_data_count(pCsr->pStmt)==0 );
130120 
130121         /* Advance to the next document */
130122         fts3EvalNextRow(pCsr, pRoot, &rc);
130123         pCsr->isEof = pRoot->bEof;
130124         pCsr->isRequireSeek = 1;
130125         pCsr->isMatchinfoNeeded = 1;
130126         pCsr->iPrevId = pRoot->iDocid;
130127       }while( pCsr->isEof==0
130128            && pRoot->eType==FTSQUERY_NEAR
130129            && fts3EvalTestDeferredAndNear(pCsr, &rc)
130130       );
130131 
130132       if( rc==SQLITE_OK && pCsr->isEof==0 ){
130133         fts3EvalUpdateCounts(pRoot);
130134       }
130135     }
130136 
130137     pCsr->isEof = 0;
130138     pCsr->iPrevId = iPrevId;
130139 
130140     if( bEof ){
130141       pRoot->bEof = bEof;
130142     }else{
130143       /* Caution: pRoot may iterate through docids in ascending or descending
130144       ** order. For this reason, even though it seems more defensive, the
130145       ** do loop can not be written:
130146       **
130147       **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
130148       */
130149       fts3EvalRestart(pCsr, pRoot, &rc);
130150       do {
130151         fts3EvalNextRow(pCsr, pRoot, &rc);
130152         assert( pRoot->bEof==0 );
130153       }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
130154       fts3EvalTestDeferredAndNear(pCsr, &rc);
130155     }
130156   }
130157   return rc;
130158 }
130159 
130160 /*
130161 ** This function is used by the matchinfo() module to query a phrase
130162 ** expression node for the following information:
130163 **
130164 **   1. The total number of occurrences of the phrase in each column of
130165 **      the FTS table (considering all rows), and
130166 **
130167 **   2. For each column, the number of rows in the table for which the
130168 **      column contains at least one instance of the phrase.
130169 **
130170 ** If no error occurs, SQLITE_OK is returned and the values for each column
130171 ** written into the array aiOut as follows:
130172 **
130173 **   aiOut[iCol*3 + 1] = Number of occurrences
130174 **   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
130175 **
130176 ** Caveats:
130177 **
130178 **   * If a phrase consists entirely of deferred tokens, then all output
130179 **     values are set to the number of documents in the table. In other
130180 **     words we assume that very common tokens occur exactly once in each
130181 **     column of each row of the table.
130182 **
130183 **   * If a phrase contains some deferred tokens (and some non-deferred
130184 **     tokens), count the potential occurrence identified by considering
130185 **     the non-deferred tokens instead of actual phrase occurrences.
130186 **
130187 **   * If the phrase is part of a NEAR expression, then only phrase instances
130188 **     that meet the NEAR constraint are included in the counts.
130189 */
130190 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
130191   Fts3Cursor *pCsr,               /* FTS cursor handle */
130192   Fts3Expr *pExpr,                /* Phrase expression */
130193   u32 *aiOut                      /* Array to write results into (see above) */
130194 ){
130195   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
130196   int rc = SQLITE_OK;
130197   int iCol;
130198 
130199   if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
130200     assert( pCsr->nDoc>0 );
130201     for(iCol=0; iCol<pTab->nColumn; iCol++){
130202       aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
130203       aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
130204     }
130205   }else{
130206     rc = fts3EvalGatherStats(pCsr, pExpr);
130207     if( rc==SQLITE_OK ){
130208       assert( pExpr->aMI );
130209       for(iCol=0; iCol<pTab->nColumn; iCol++){
130210         aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
130211         aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
130212       }
130213     }
130214   }
130215 
130216   return rc;
130217 }
130218 
130219 /*
130220 ** The expression pExpr passed as the second argument to this function
130221 ** must be of type FTSQUERY_PHRASE.
130222 **
130223 ** The returned value is either NULL or a pointer to a buffer containing
130224 ** a position-list indicating the occurrences of the phrase in column iCol
130225 ** of the current row.
130226 **
130227 ** More specifically, the returned buffer contains 1 varint for each
130228 ** occurrence of the phrase in the column, stored using the normal (delta+2)
130229 ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
130230 ** if the requested column contains "a b X c d X X" and the position-list
130231 ** for 'X' is requested, the buffer returned may contain:
130232 **
130233 **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
130234 **
130235 ** This function works regardless of whether or not the phrase is deferred,
130236 ** incremental, or neither.
130237 */
130238 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
130239   Fts3Cursor *pCsr,               /* FTS3 cursor object */
130240   Fts3Expr *pExpr,                /* Phrase to return doclist for */
130241   int iCol,                       /* Column to return position list for */
130242   char **ppOut                    /* OUT: Pointer to position list */
130243 ){
130244   Fts3Phrase *pPhrase = pExpr->pPhrase;
130245   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
130246   char *pIter;
130247   int iThis;
130248   sqlite3_int64 iDocid;
130249 
130250   /* If this phrase is applies specifically to some column other than
130251   ** column iCol, return a NULL pointer.  */
130252   *ppOut = 0;
130253   assert( iCol>=0 && iCol<pTab->nColumn );
130254   if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
130255     return SQLITE_OK;
130256   }
130257 
130258   iDocid = pExpr->iDocid;
130259   pIter = pPhrase->doclist.pList;
130260   if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
130261     int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
130262     int iMul;                     /* +1 if csr dir matches index dir, else -1 */
130263     int bOr = 0;
130264     u8 bEof = 0;
130265     u8 bTreeEof = 0;
130266     Fts3Expr *p;                  /* Used to iterate from pExpr to root */
130267     Fts3Expr *pNear;              /* Most senior NEAR ancestor (or pExpr) */
130268 
130269     /* Check if this phrase descends from an OR expression node. If not,
130270     ** return NULL. Otherwise, the entry that corresponds to docid
130271     ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
130272     ** tree that the node is part of has been marked as EOF, but the node
130273     ** itself is not EOF, then it may point to an earlier entry. */
130274     pNear = pExpr;
130275     for(p=pExpr->pParent; p; p=p->pParent){
130276       if( p->eType==FTSQUERY_OR ) bOr = 1;
130277       if( p->eType==FTSQUERY_NEAR ) pNear = p;
130278       if( p->bEof ) bTreeEof = 1;
130279     }
130280     if( bOr==0 ) return SQLITE_OK;
130281 
130282     /* This is the descendent of an OR node. In this case we cannot use
130283     ** an incremental phrase. Load the entire doclist for the phrase
130284     ** into memory in this case.  */
130285     if( pPhrase->bIncr ){
130286       int rc = SQLITE_OK;
130287       int bEofSave = pExpr->bEof;
130288       fts3EvalRestart(pCsr, pExpr, &rc);
130289       while( rc==SQLITE_OK && !pExpr->bEof ){
130290         fts3EvalNextRow(pCsr, pExpr, &rc);
130291         if( bEofSave==0 && pExpr->iDocid==iDocid ) break;
130292       }
130293       pIter = pPhrase->doclist.pList;
130294       assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
130295       if( rc!=SQLITE_OK ) return rc;
130296     }
130297 
130298     iMul = ((pCsr->bDesc==bDescDoclist) ? 1 : -1);
130299     while( bTreeEof==1
130300         && pNear->bEof==0
130301         && (DOCID_CMP(pNear->iDocid, pCsr->iPrevId) * iMul)<0
130302     ){
130303       int rc = SQLITE_OK;
130304       fts3EvalNextRow(pCsr, pExpr, &rc);
130305       if( rc!=SQLITE_OK ) return rc;
130306       iDocid = pExpr->iDocid;
130307       pIter = pPhrase->doclist.pList;
130308     }
130309 
130310     bEof = (pPhrase->doclist.nAll==0);
130311     assert( bDescDoclist==0 || bDescDoclist==1 );
130312     assert( pCsr->bDesc==0 || pCsr->bDesc==1 );
130313 
130314     if( bEof==0 ){
130315       if( pCsr->bDesc==bDescDoclist ){
130316         int dummy;
130317         if( pNear->bEof ){
130318           /* This expression is already at EOF. So position it to point to the
130319           ** last entry in the doclist at pPhrase->doclist.aAll[]. Variable
130320           ** iDocid is already set for this entry, so all that is required is
130321           ** to set pIter to point to the first byte of the last position-list
130322           ** in the doclist.
130323           **
130324           ** It would also be correct to set pIter and iDocid to zero. In
130325           ** this case, the first call to sqltie3Fts4DoclistPrev() below
130326           ** would also move the iterator to point to the last entry in the
130327           ** doclist. However, this is expensive, as to do so it has to
130328           ** iterate through the entire doclist from start to finish (since
130329           ** it does not know the docid for the last entry).  */
130330           pIter = &pPhrase->doclist.aAll[pPhrase->doclist.nAll-1];
130331           fts3ReversePoslist(pPhrase->doclist.aAll, &pIter);
130332         }
130333         while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
130334           sqlite3Fts3DoclistPrev(
130335               bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
130336               &pIter, &iDocid, &dummy, &bEof
130337           );
130338         }
130339       }else{
130340         if( pNear->bEof ){
130341           pIter = 0;
130342           iDocid = 0;
130343         }
130344         while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
130345           sqlite3Fts3DoclistNext(
130346               bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
130347               &pIter, &iDocid, &bEof
130348           );
130349         }
130350       }
130351     }
130352 
130353     if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
130354   }
130355   if( pIter==0 ) return SQLITE_OK;
130356 
130357   if( *pIter==0x01 ){
130358     pIter++;
130359     pIter += fts3GetVarint32(pIter, &iThis);
130360   }else{
130361     iThis = 0;
130362   }
130363   while( iThis<iCol ){
130364     fts3ColumnlistCopy(0, &pIter);
130365     if( *pIter==0x00 ) return 0;
130366     pIter++;
130367     pIter += fts3GetVarint32(pIter, &iThis);
130368   }
130369 
130370   *ppOut = ((iCol==iThis)?pIter:0);
130371   return SQLITE_OK;
130372 }
130373 
130374 /*
130375 ** Free all components of the Fts3Phrase structure that were allocated by
130376 ** the eval module. Specifically, this means to free:
130377 **
130378 **   * the contents of pPhrase->doclist, and
130379 **   * any Fts3MultiSegReader objects held by phrase tokens.
130380 */
130381 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
130382   if( pPhrase ){
130383     int i;
130384     sqlite3_free(pPhrase->doclist.aAll);
130385     fts3EvalInvalidatePoslist(pPhrase);
130386     memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
130387     for(i=0; i<pPhrase->nToken; i++){
130388       fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
130389       pPhrase->aToken[i].pSegcsr = 0;
130390     }
130391   }
130392 }
130393 
130394 
130395 /*
130396 ** Return SQLITE_CORRUPT_VTAB.
130397 */
130398 #ifdef SQLITE_DEBUG
130399 SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
130400   return SQLITE_CORRUPT_VTAB;
130401 }
130402 #endif
130403 
130404 #if !SQLITE_CORE
130405 /*
130406 ** Initialize API pointer table, if required.
130407 */
130408 #ifdef _WIN32
130409 __declspec(dllexport)
130410 #endif
130411 SQLITE_API int sqlite3_fts3_init(
130412   sqlite3 *db,
130413   char **pzErrMsg,
130414   const sqlite3_api_routines *pApi
130415 ){
130416   SQLITE_EXTENSION_INIT2(pApi)
130417   return sqlite3Fts3Init(db);
130418 }
130419 #endif
130420 
130421 #endif
130422 
130423 /************** End of fts3.c ************************************************/
130424 /************** Begin file fts3_aux.c ****************************************/
130425 /*
130426 ** 2011 Jan 27
130427 **
130428 ** The author disclaims copyright to this source code.  In place of
130429 ** a legal notice, here is a blessing:
130430 **
130431 **    May you do good and not evil.
130432 **    May you find forgiveness for yourself and forgive others.
130433 **    May you share freely, never taking more than you give.
130434 **
130435 ******************************************************************************
130436 **
130437 */
130438 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
130439 
130440 /* #include <string.h> */
130441 /* #include <assert.h> */
130442 
130443 typedef struct Fts3auxTable Fts3auxTable;
130444 typedef struct Fts3auxCursor Fts3auxCursor;
130445 
130446 struct Fts3auxTable {
130447   sqlite3_vtab base;              /* Base class used by SQLite core */
130448   Fts3Table *pFts3Tab;
130449 };
130450 
130451 struct Fts3auxCursor {
130452   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
130453   Fts3MultiSegReader csr;        /* Must be right after "base" */
130454   Fts3SegFilter filter;
130455   char *zStop;
130456   int nStop;                      /* Byte-length of string zStop */
130457   int iLangid;                    /* Language id to query */
130458   int isEof;                      /* True if cursor is at EOF */
130459   sqlite3_int64 iRowid;           /* Current rowid */
130460 
130461   int iCol;                       /* Current value of 'col' column */
130462   int nStat;                      /* Size of aStat[] array */
130463   struct Fts3auxColstats {
130464     sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
130465     sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
130466   } *aStat;
130467 };
130468 
130469 /*
130470 ** Schema of the terms table.
130471 */
130472 #define FTS3_AUX_SCHEMA \
130473   "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
130474 
130475 /*
130476 ** This function does all the work for both the xConnect and xCreate methods.
130477 ** These tables have no persistent representation of their own, so xConnect
130478 ** and xCreate are identical operations.
130479 */
130480 static int fts3auxConnectMethod(
130481   sqlite3 *db,                    /* Database connection */
130482   void *pUnused,                  /* Unused */
130483   int argc,                       /* Number of elements in argv array */
130484   const char * const *argv,       /* xCreate/xConnect argument array */
130485   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
130486   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
130487 ){
130488   char const *zDb;                /* Name of database (e.g. "main") */
130489   char const *zFts3;              /* Name of fts3 table */
130490   int nDb;                        /* Result of strlen(zDb) */
130491   int nFts3;                      /* Result of strlen(zFts3) */
130492   int nByte;                      /* Bytes of space to allocate here */
130493   int rc;                         /* value returned by declare_vtab() */
130494   Fts3auxTable *p;                /* Virtual table object to return */
130495 
130496   UNUSED_PARAMETER(pUnused);
130497 
130498   /* The user should invoke this in one of two forms:
130499   **
130500   **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
130501   **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
130502   */
130503   if( argc!=4 && argc!=5 ) goto bad_args;
130504 
130505   zDb = argv[1];
130506   nDb = (int)strlen(zDb);
130507   if( argc==5 ){
130508     if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
130509       zDb = argv[3];
130510       nDb = (int)strlen(zDb);
130511       zFts3 = argv[4];
130512     }else{
130513       goto bad_args;
130514     }
130515   }else{
130516     zFts3 = argv[3];
130517   }
130518   nFts3 = (int)strlen(zFts3);
130519 
130520   rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
130521   if( rc!=SQLITE_OK ) return rc;
130522 
130523   nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
130524   p = (Fts3auxTable *)sqlite3_malloc(nByte);
130525   if( !p ) return SQLITE_NOMEM;
130526   memset(p, 0, nByte);
130527 
130528   p->pFts3Tab = (Fts3Table *)&p[1];
130529   p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
130530   p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
130531   p->pFts3Tab->db = db;
130532   p->pFts3Tab->nIndex = 1;
130533 
130534   memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
130535   memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
130536   sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
130537 
130538   *ppVtab = (sqlite3_vtab *)p;
130539   return SQLITE_OK;
130540 
130541  bad_args:
130542   *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
130543   return SQLITE_ERROR;
130544 }
130545 
130546 /*
130547 ** This function does the work for both the xDisconnect and xDestroy methods.
130548 ** These tables have no persistent representation of their own, so xDisconnect
130549 ** and xDestroy are identical operations.
130550 */
130551 static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
130552   Fts3auxTable *p = (Fts3auxTable *)pVtab;
130553   Fts3Table *pFts3 = p->pFts3Tab;
130554   int i;
130555 
130556   /* Free any prepared statements held */
130557   for(i=0; i<SizeofArray(pFts3->aStmt); i++){
130558     sqlite3_finalize(pFts3->aStmt[i]);
130559   }
130560   sqlite3_free(pFts3->zSegmentsTbl);
130561   sqlite3_free(p);
130562   return SQLITE_OK;
130563 }
130564 
130565 #define FTS4AUX_EQ_CONSTRAINT 1
130566 #define FTS4AUX_GE_CONSTRAINT 2
130567 #define FTS4AUX_LE_CONSTRAINT 4
130568 
130569 /*
130570 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
130571 */
130572 static int fts3auxBestIndexMethod(
130573   sqlite3_vtab *pVTab,
130574   sqlite3_index_info *pInfo
130575 ){
130576   int i;
130577   int iEq = -1;
130578   int iGe = -1;
130579   int iLe = -1;
130580   int iLangid = -1;
130581   int iNext = 1;                  /* Next free argvIndex value */
130582 
130583   UNUSED_PARAMETER(pVTab);
130584 
130585   /* This vtab delivers always results in "ORDER BY term ASC" order. */
130586   if( pInfo->nOrderBy==1
130587    && pInfo->aOrderBy[0].iColumn==0
130588    && pInfo->aOrderBy[0].desc==0
130589   ){
130590     pInfo->orderByConsumed = 1;
130591   }
130592 
130593   /* Search for equality and range constraints on the "term" column.
130594   ** And equality constraints on the hidden "languageid" column. */
130595   for(i=0; i<pInfo->nConstraint; i++){
130596     if( pInfo->aConstraint[i].usable ){
130597       int op = pInfo->aConstraint[i].op;
130598       int iCol = pInfo->aConstraint[i].iColumn;
130599 
130600       if( iCol==0 ){
130601         if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
130602         if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
130603         if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
130604         if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
130605         if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
130606       }
130607       if( iCol==4 ){
130608         if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
130609       }
130610     }
130611   }
130612 
130613   if( iEq>=0 ){
130614     pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
130615     pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
130616     pInfo->estimatedCost = 5;
130617   }else{
130618     pInfo->idxNum = 0;
130619     pInfo->estimatedCost = 20000;
130620     if( iGe>=0 ){
130621       pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
130622       pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
130623       pInfo->estimatedCost /= 2;
130624     }
130625     if( iLe>=0 ){
130626       pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
130627       pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
130628       pInfo->estimatedCost /= 2;
130629     }
130630   }
130631   if( iLangid>=0 ){
130632     pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
130633     pInfo->estimatedCost--;
130634   }
130635 
130636   return SQLITE_OK;
130637 }
130638 
130639 /*
130640 ** xOpen - Open a cursor.
130641 */
130642 static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
130643   Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
130644 
130645   UNUSED_PARAMETER(pVTab);
130646 
130647   pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
130648   if( !pCsr ) return SQLITE_NOMEM;
130649   memset(pCsr, 0, sizeof(Fts3auxCursor));
130650 
130651   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
130652   return SQLITE_OK;
130653 }
130654 
130655 /*
130656 ** xClose - Close a cursor.
130657 */
130658 static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
130659   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
130660   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130661 
130662   sqlite3Fts3SegmentsClose(pFts3);
130663   sqlite3Fts3SegReaderFinish(&pCsr->csr);
130664   sqlite3_free((void *)pCsr->filter.zTerm);
130665   sqlite3_free(pCsr->zStop);
130666   sqlite3_free(pCsr->aStat);
130667   sqlite3_free(pCsr);
130668   return SQLITE_OK;
130669 }
130670 
130671 static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
130672   if( nSize>pCsr->nStat ){
130673     struct Fts3auxColstats *aNew;
130674     aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
130675         sizeof(struct Fts3auxColstats) * nSize
130676     );
130677     if( aNew==0 ) return SQLITE_NOMEM;
130678     memset(&aNew[pCsr->nStat], 0,
130679         sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
130680     );
130681     pCsr->aStat = aNew;
130682     pCsr->nStat = nSize;
130683   }
130684   return SQLITE_OK;
130685 }
130686 
130687 /*
130688 ** xNext - Advance the cursor to the next row, if any.
130689 */
130690 static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
130691   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130692   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
130693   int rc;
130694 
130695   /* Increment our pretend rowid value. */
130696   pCsr->iRowid++;
130697 
130698   for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
130699     if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
130700   }
130701 
130702   rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
130703   if( rc==SQLITE_ROW ){
130704     int i = 0;
130705     int nDoclist = pCsr->csr.nDoclist;
130706     char *aDoclist = pCsr->csr.aDoclist;
130707     int iCol;
130708 
130709     int eState = 0;
130710 
130711     if( pCsr->zStop ){
130712       int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
130713       int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
130714       if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
130715         pCsr->isEof = 1;
130716         return SQLITE_OK;
130717       }
130718     }
130719 
130720     if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
130721     memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
130722     iCol = 0;
130723 
130724     while( i<nDoclist ){
130725       sqlite3_int64 v = 0;
130726 
130727       i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
130728       switch( eState ){
130729         /* State 0. In this state the integer just read was a docid. */
130730         case 0:
130731           pCsr->aStat[0].nDoc++;
130732           eState = 1;
130733           iCol = 0;
130734           break;
130735 
130736         /* State 1. In this state we are expecting either a 1, indicating
130737         ** that the following integer will be a column number, or the
130738         ** start of a position list for column 0.
130739         **
130740         ** The only difference between state 1 and state 2 is that if the
130741         ** integer encountered in state 1 is not 0 or 1, then we need to
130742         ** increment the column 0 "nDoc" count for this term.
130743         */
130744         case 1:
130745           assert( iCol==0 );
130746           if( v>1 ){
130747             pCsr->aStat[1].nDoc++;
130748           }
130749           eState = 2;
130750           /* fall through */
130751 
130752         case 2:
130753           if( v==0 ){       /* 0x00. Next integer will be a docid. */
130754             eState = 0;
130755           }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
130756             eState = 3;
130757           }else{            /* 2 or greater. A position. */
130758             pCsr->aStat[iCol+1].nOcc++;
130759             pCsr->aStat[0].nOcc++;
130760           }
130761           break;
130762 
130763         /* State 3. The integer just read is a column number. */
130764         default: assert( eState==3 );
130765           iCol = (int)v;
130766           if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
130767           pCsr->aStat[iCol+1].nDoc++;
130768           eState = 2;
130769           break;
130770       }
130771     }
130772 
130773     pCsr->iCol = 0;
130774     rc = SQLITE_OK;
130775   }else{
130776     pCsr->isEof = 1;
130777   }
130778   return rc;
130779 }
130780 
130781 /*
130782 ** xFilter - Initialize a cursor to point at the start of its data.
130783 */
130784 static int fts3auxFilterMethod(
130785   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
130786   int idxNum,                     /* Strategy index */
130787   const char *idxStr,             /* Unused */
130788   int nVal,                       /* Number of elements in apVal */
130789   sqlite3_value **apVal           /* Arguments for the indexing scheme */
130790 ){
130791   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130792   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
130793   int rc;
130794   int isScan = 0;
130795   int iLangVal = 0;               /* Language id to query */
130796 
130797   int iEq = -1;                   /* Index of term=? value in apVal */
130798   int iGe = -1;                   /* Index of term>=? value in apVal */
130799   int iLe = -1;                   /* Index of term<=? value in apVal */
130800   int iLangid = -1;               /* Index of languageid=? value in apVal */
130801   int iNext = 0;
130802 
130803   UNUSED_PARAMETER(nVal);
130804   UNUSED_PARAMETER(idxStr);
130805 
130806   assert( idxStr==0 );
130807   assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
130808        || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
130809        || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
130810   );
130811 
130812   if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
130813     iEq = iNext++;
130814   }else{
130815     isScan = 1;
130816     if( idxNum & FTS4AUX_GE_CONSTRAINT ){
130817       iGe = iNext++;
130818     }
130819     if( idxNum & FTS4AUX_LE_CONSTRAINT ){
130820       iLe = iNext++;
130821     }
130822   }
130823   if( iNext<nVal ){
130824     iLangid = iNext++;
130825   }
130826 
130827   /* In case this cursor is being reused, close and zero it. */
130828   testcase(pCsr->filter.zTerm);
130829   sqlite3Fts3SegReaderFinish(&pCsr->csr);
130830   sqlite3_free((void *)pCsr->filter.zTerm);
130831   sqlite3_free(pCsr->aStat);
130832   memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
130833 
130834   pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
130835   if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
130836 
130837   if( iEq>=0 || iGe>=0 ){
130838     const unsigned char *zStr = sqlite3_value_text(apVal[0]);
130839     assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
130840     if( zStr ){
130841       pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
130842       pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
130843       if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
130844     }
130845   }
130846 
130847   if( iLe>=0 ){
130848     pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
130849     pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
130850     if( pCsr->zStop==0 ) return SQLITE_NOMEM;
130851   }
130852 
130853   if( iLangid>=0 ){
130854     iLangVal = sqlite3_value_int(apVal[iLangid]);
130855 
130856     /* If the user specified a negative value for the languageid, use zero
130857     ** instead. This works, as the "languageid=?" constraint will also
130858     ** be tested by the VDBE layer. The test will always be false (since
130859     ** this module will not return a row with a negative languageid), and
130860     ** so the overall query will return zero rows.  */
130861     if( iLangVal<0 ) iLangVal = 0;
130862   }
130863   pCsr->iLangid = iLangVal;
130864 
130865   rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
130866       pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
130867   );
130868   if( rc==SQLITE_OK ){
130869     rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
130870   }
130871 
130872   if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
130873   return rc;
130874 }
130875 
130876 /*
130877 ** xEof - Return true if the cursor is at EOF, or false otherwise.
130878 */
130879 static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
130880   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130881   return pCsr->isEof;
130882 }
130883 
130884 /*
130885 ** xColumn - Return a column value.
130886 */
130887 static int fts3auxColumnMethod(
130888   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
130889   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
130890   int iCol                        /* Index of column to read value from */
130891 ){
130892   Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
130893 
130894   assert( p->isEof==0 );
130895   switch( iCol ){
130896     case 0: /* term */
130897       sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
130898       break;
130899 
130900     case 1: /* col */
130901       if( p->iCol ){
130902         sqlite3_result_int(pCtx, p->iCol-1);
130903       }else{
130904         sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
130905       }
130906       break;
130907 
130908     case 2: /* documents */
130909       sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
130910       break;
130911 
130912     case 3: /* occurrences */
130913       sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
130914       break;
130915 
130916     default: /* languageid */
130917       assert( iCol==4 );
130918       sqlite3_result_int(pCtx, p->iLangid);
130919       break;
130920   }
130921 
130922   return SQLITE_OK;
130923 }
130924 
130925 /*
130926 ** xRowid - Return the current rowid for the cursor.
130927 */
130928 static int fts3auxRowidMethod(
130929   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
130930   sqlite_int64 *pRowid            /* OUT: Rowid value */
130931 ){
130932   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130933   *pRowid = pCsr->iRowid;
130934   return SQLITE_OK;
130935 }
130936 
130937 /*
130938 ** Register the fts3aux module with database connection db. Return SQLITE_OK
130939 ** if successful or an error code if sqlite3_create_module() fails.
130940 */
130941 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
130942   static const sqlite3_module fts3aux_module = {
130943      0,                           /* iVersion      */
130944      fts3auxConnectMethod,        /* xCreate       */
130945      fts3auxConnectMethod,        /* xConnect      */
130946      fts3auxBestIndexMethod,      /* xBestIndex    */
130947      fts3auxDisconnectMethod,     /* xDisconnect   */
130948      fts3auxDisconnectMethod,     /* xDestroy      */
130949      fts3auxOpenMethod,           /* xOpen         */
130950      fts3auxCloseMethod,          /* xClose        */
130951      fts3auxFilterMethod,         /* xFilter       */
130952      fts3auxNextMethod,           /* xNext         */
130953      fts3auxEofMethod,            /* xEof          */
130954      fts3auxColumnMethod,         /* xColumn       */
130955      fts3auxRowidMethod,          /* xRowid        */
130956      0,                           /* xUpdate       */
130957      0,                           /* xBegin        */
130958      0,                           /* xSync         */
130959      0,                           /* xCommit       */
130960      0,                           /* xRollback     */
130961      0,                           /* xFindFunction */
130962      0,                           /* xRename       */
130963      0,                           /* xSavepoint    */
130964      0,                           /* xRelease      */
130965      0                            /* xRollbackTo   */
130966   };
130967   int rc;                         /* Return code */
130968 
130969   rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
130970   return rc;
130971 }
130972 
130973 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
130974 
130975 /************** End of fts3_aux.c ********************************************/
130976 /************** Begin file fts3_expr.c ***************************************/
130977 /*
130978 ** 2008 Nov 28
130979 **
130980 ** The author disclaims copyright to this source code.  In place of
130981 ** a legal notice, here is a blessing:
130982 **
130983 **    May you do good and not evil.
130984 **    May you find forgiveness for yourself and forgive others.
130985 **    May you share freely, never taking more than you give.
130986 **
130987 ******************************************************************************
130988 **
130989 ** This module contains code that implements a parser for fts3 query strings
130990 ** (the right-hand argument to the MATCH operator). Because the supported
130991 ** syntax is relatively simple, the whole tokenizer/parser system is
130992 ** hand-coded.
130993 */
130994 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
130995 
130996 /*
130997 ** By default, this module parses the legacy syntax that has been
130998 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
130999 ** is defined, then it uses the new syntax. The differences between
131000 ** the new and the old syntaxes are:
131001 **
131002 **  a) The new syntax supports parenthesis. The old does not.
131003 **
131004 **  b) The new syntax supports the AND and NOT operators. The old does not.
131005 **
131006 **  c) The old syntax supports the "-" token qualifier. This is not
131007 **     supported by the new syntax (it is replaced by the NOT operator).
131008 **
131009 **  d) When using the old syntax, the OR operator has a greater precedence
131010 **     than an implicit AND. When using the new, both implicity and explicit
131011 **     AND operators have a higher precedence than OR.
131012 **
131013 ** If compiled with SQLITE_TEST defined, then this module exports the
131014 ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
131015 ** to zero causes the module to use the old syntax. If it is set to
131016 ** non-zero the new syntax is activated. This is so both syntaxes can
131017 ** be tested using a single build of testfixture.
131018 **
131019 ** The following describes the syntax supported by the fts3 MATCH
131020 ** operator in a similar format to that used by the lemon parser
131021 ** generator. This module does not use actually lemon, it uses a
131022 ** custom parser.
131023 **
131024 **   query ::= andexpr (OR andexpr)*.
131025 **
131026 **   andexpr ::= notexpr (AND? notexpr)*.
131027 **
131028 **   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
131029 **   notexpr ::= LP query RP.
131030 **
131031 **   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
131032 **
131033 **   distance_opt ::= .
131034 **   distance_opt ::= / INTEGER.
131035 **
131036 **   phrase ::= TOKEN.
131037 **   phrase ::= COLUMN:TOKEN.
131038 **   phrase ::= "TOKEN TOKEN TOKEN...".
131039 */
131040 
131041 #ifdef SQLITE_TEST
131042 SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
131043 #else
131044 # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
131045 #  define sqlite3_fts3_enable_parentheses 1
131046 # else
131047 #  define sqlite3_fts3_enable_parentheses 0
131048 # endif
131049 #endif
131050 
131051 /*
131052 ** Default span for NEAR operators.
131053 */
131054 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
131055 
131056 /* #include <string.h> */
131057 /* #include <assert.h> */
131058 
131059 /*
131060 ** isNot:
131061 **   This variable is used by function getNextNode(). When getNextNode() is
131062 **   called, it sets ParseContext.isNot to true if the 'next node' is a
131063 **   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
131064 **   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
131065 **   zero.
131066 */
131067 typedef struct ParseContext ParseContext;
131068 struct ParseContext {
131069   sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
131070   int iLangid;                        /* Language id used with tokenizer */
131071   const char **azCol;                 /* Array of column names for fts3 table */
131072   int bFts4;                          /* True to allow FTS4-only syntax */
131073   int nCol;                           /* Number of entries in azCol[] */
131074   int iDefaultCol;                    /* Default column to query */
131075   int isNot;                          /* True if getNextNode() sees a unary - */
131076   sqlite3_context *pCtx;              /* Write error message here */
131077   int nNest;                          /* Number of nested brackets */
131078 };
131079 
131080 /*
131081 ** This function is equivalent to the standard isspace() function.
131082 **
131083 ** The standard isspace() can be awkward to use safely, because although it
131084 ** is defined to accept an argument of type int, its behavior when passed
131085 ** an integer that falls outside of the range of the unsigned char type
131086 ** is undefined (and sometimes, "undefined" means segfault). This wrapper
131087 ** is defined to accept an argument of type char, and always returns 0 for
131088 ** any values that fall outside of the range of the unsigned char type (i.e.
131089 ** negative values).
131090 */
131091 static int fts3isspace(char c){
131092   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
131093 }
131094 
131095 /*
131096 ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
131097 ** zero the memory before returning a pointer to it. If unsuccessful,
131098 ** return NULL.
131099 */
131100 static void *fts3MallocZero(int nByte){
131101   void *pRet = sqlite3_malloc(nByte);
131102   if( pRet ) memset(pRet, 0, nByte);
131103   return pRet;
131104 }
131105 
131106 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
131107   sqlite3_tokenizer *pTokenizer,
131108   int iLangid,
131109   const char *z,
131110   int n,
131111   sqlite3_tokenizer_cursor **ppCsr
131112 ){
131113   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
131114   sqlite3_tokenizer_cursor *pCsr = 0;
131115   int rc;
131116 
131117   rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
131118   assert( rc==SQLITE_OK || pCsr==0 );
131119   if( rc==SQLITE_OK ){
131120     pCsr->pTokenizer = pTokenizer;
131121     if( pModule->iVersion>=1 ){
131122       rc = pModule->xLanguageid(pCsr, iLangid);
131123       if( rc!=SQLITE_OK ){
131124         pModule->xClose(pCsr);
131125         pCsr = 0;
131126       }
131127     }
131128   }
131129   *ppCsr = pCsr;
131130   return rc;
131131 }
131132 
131133 /*
131134 ** Function getNextNode(), which is called by fts3ExprParse(), may itself
131135 ** call fts3ExprParse(). So this forward declaration is required.
131136 */
131137 static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
131138 
131139 /*
131140 ** Extract the next token from buffer z (length n) using the tokenizer
131141 ** and other information (column names etc.) in pParse. Create an Fts3Expr
131142 ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
131143 ** single token and set *ppExpr to point to it. If the end of the buffer is
131144 ** reached before a token is found, set *ppExpr to zero. It is the
131145 ** responsibility of the caller to eventually deallocate the allocated
131146 ** Fts3Expr structure (if any) by passing it to sqlite3_free().
131147 **
131148 ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
131149 ** fails.
131150 */
131151 static int getNextToken(
131152   ParseContext *pParse,                   /* fts3 query parse context */
131153   int iCol,                               /* Value for Fts3Phrase.iColumn */
131154   const char *z, int n,                   /* Input string */
131155   Fts3Expr **ppExpr,                      /* OUT: expression */
131156   int *pnConsumed                         /* OUT: Number of bytes consumed */
131157 ){
131158   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
131159   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
131160   int rc;
131161   sqlite3_tokenizer_cursor *pCursor;
131162   Fts3Expr *pRet = 0;
131163   int nConsumed = 0;
131164 
131165   rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
131166   if( rc==SQLITE_OK ){
131167     const char *zToken;
131168     int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
131169     int nByte;                               /* total space to allocate */
131170 
131171     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
131172 
131173     if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){
131174       int i;
131175       if( rc==SQLITE_DONE ) iStart = n;
131176       for(i=0; i<iStart; i++){
131177         if( z[i]=='(' ){
131178           pParse->nNest++;
131179           rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed);
131180           if( rc==SQLITE_OK && !pRet ){
131181             rc = SQLITE_DONE;
131182           }
131183           nConsumed = (int)(i + 1 + nConsumed);
131184           break;
131185         }
131186 
131187         if( z[i]==')' ){
131188           rc = SQLITE_DONE;
131189           pParse->nNest--;
131190           nConsumed = i+1;
131191           break;
131192         }
131193       }
131194     }
131195 
131196     if( nConsumed==0 && rc==SQLITE_OK ){
131197       nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
131198       pRet = (Fts3Expr *)fts3MallocZero(nByte);
131199       if( !pRet ){
131200         rc = SQLITE_NOMEM;
131201       }else{
131202         pRet->eType = FTSQUERY_PHRASE;
131203         pRet->pPhrase = (Fts3Phrase *)&pRet[1];
131204         pRet->pPhrase->nToken = 1;
131205         pRet->pPhrase->iColumn = iCol;
131206         pRet->pPhrase->aToken[0].n = nToken;
131207         pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
131208         memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
131209 
131210         if( iEnd<n && z[iEnd]=='*' ){
131211           pRet->pPhrase->aToken[0].isPrefix = 1;
131212           iEnd++;
131213         }
131214 
131215         while( 1 ){
131216           if( !sqlite3_fts3_enable_parentheses
131217            && iStart>0 && z[iStart-1]=='-'
131218           ){
131219             pParse->isNot = 1;
131220             iStart--;
131221           }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
131222             pRet->pPhrase->aToken[0].bFirst = 1;
131223             iStart--;
131224           }else{
131225             break;
131226           }
131227         }
131228 
131229       }
131230       nConsumed = iEnd;
131231     }
131232 
131233     pModule->xClose(pCursor);
131234   }
131235 
131236   *pnConsumed = nConsumed;
131237   *ppExpr = pRet;
131238   return rc;
131239 }
131240 
131241 
131242 /*
131243 ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
131244 ** then free the old allocation.
131245 */
131246 static void *fts3ReallocOrFree(void *pOrig, int nNew){
131247   void *pRet = sqlite3_realloc(pOrig, nNew);
131248   if( !pRet ){
131249     sqlite3_free(pOrig);
131250   }
131251   return pRet;
131252 }
131253 
131254 /*
131255 ** Buffer zInput, length nInput, contains the contents of a quoted string
131256 ** that appeared as part of an fts3 query expression. Neither quote character
131257 ** is included in the buffer. This function attempts to tokenize the entire
131258 ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
131259 ** containing the results.
131260 **
131261 ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
131262 ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
131263 ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
131264 ** to 0.
131265 */
131266 static int getNextString(
131267   ParseContext *pParse,                   /* fts3 query parse context */
131268   const char *zInput, int nInput,         /* Input string */
131269   Fts3Expr **ppExpr                       /* OUT: expression */
131270 ){
131271   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
131272   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
131273   int rc;
131274   Fts3Expr *p = 0;
131275   sqlite3_tokenizer_cursor *pCursor = 0;
131276   char *zTemp = 0;
131277   int nTemp = 0;
131278 
131279   const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
131280   int nToken = 0;
131281 
131282   /* The final Fts3Expr data structure, including the Fts3Phrase,
131283   ** Fts3PhraseToken structures token buffers are all stored as a single
131284   ** allocation so that the expression can be freed with a single call to
131285   ** sqlite3_free(). Setting this up requires a two pass approach.
131286   **
131287   ** The first pass, in the block below, uses a tokenizer cursor to iterate
131288   ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
131289   ** to assemble data in two dynamic buffers:
131290   **
131291   **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
131292   **             structure, followed by the array of Fts3PhraseToken
131293   **             structures. This pass only populates the Fts3PhraseToken array.
131294   **
131295   **   Buffer zTemp: Contains copies of all tokens.
131296   **
131297   ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
131298   ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
131299   ** structures.
131300   */
131301   rc = sqlite3Fts3OpenTokenizer(
131302       pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
131303   if( rc==SQLITE_OK ){
131304     int ii;
131305     for(ii=0; rc==SQLITE_OK; ii++){
131306       const char *zByte;
131307       int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
131308       rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
131309       if( rc==SQLITE_OK ){
131310         Fts3PhraseToken *pToken;
131311 
131312         p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
131313         if( !p ) goto no_mem;
131314 
131315         zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
131316         if( !zTemp ) goto no_mem;
131317 
131318         assert( nToken==ii );
131319         pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
131320         memset(pToken, 0, sizeof(Fts3PhraseToken));
131321 
131322         memcpy(&zTemp[nTemp], zByte, nByte);
131323         nTemp += nByte;
131324 
131325         pToken->n = nByte;
131326         pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
131327         pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
131328         nToken = ii+1;
131329       }
131330     }
131331 
131332     pModule->xClose(pCursor);
131333     pCursor = 0;
131334   }
131335 
131336   if( rc==SQLITE_DONE ){
131337     int jj;
131338     char *zBuf = 0;
131339 
131340     p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
131341     if( !p ) goto no_mem;
131342     memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
131343     p->eType = FTSQUERY_PHRASE;
131344     p->pPhrase = (Fts3Phrase *)&p[1];
131345     p->pPhrase->iColumn = pParse->iDefaultCol;
131346     p->pPhrase->nToken = nToken;
131347 
131348     zBuf = (char *)&p->pPhrase->aToken[nToken];
131349     if( zTemp ){
131350       memcpy(zBuf, zTemp, nTemp);
131351       sqlite3_free(zTemp);
131352     }else{
131353       assert( nTemp==0 );
131354     }
131355 
131356     for(jj=0; jj<p->pPhrase->nToken; jj++){
131357       p->pPhrase->aToken[jj].z = zBuf;
131358       zBuf += p->pPhrase->aToken[jj].n;
131359     }
131360     rc = SQLITE_OK;
131361   }
131362 
131363   *ppExpr = p;
131364   return rc;
131365 no_mem:
131366 
131367   if( pCursor ){
131368     pModule->xClose(pCursor);
131369   }
131370   sqlite3_free(zTemp);
131371   sqlite3_free(p);
131372   *ppExpr = 0;
131373   return SQLITE_NOMEM;
131374 }
131375 
131376 /*
131377 ** The output variable *ppExpr is populated with an allocated Fts3Expr
131378 ** structure, or set to 0 if the end of the input buffer is reached.
131379 **
131380 ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
131381 ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
131382 ** If SQLITE_ERROR is returned, pContext is populated with an error message.
131383 */
131384 static int getNextNode(
131385   ParseContext *pParse,                   /* fts3 query parse context */
131386   const char *z, int n,                   /* Input string */
131387   Fts3Expr **ppExpr,                      /* OUT: expression */
131388   int *pnConsumed                         /* OUT: Number of bytes consumed */
131389 ){
131390   static const struct Fts3Keyword {
131391     char *z;                              /* Keyword text */
131392     unsigned char n;                      /* Length of the keyword */
131393     unsigned char parenOnly;              /* Only valid in paren mode */
131394     unsigned char eType;                  /* Keyword code */
131395   } aKeyword[] = {
131396     { "OR" ,  2, 0, FTSQUERY_OR   },
131397     { "AND",  3, 1, FTSQUERY_AND  },
131398     { "NOT",  3, 1, FTSQUERY_NOT  },
131399     { "NEAR", 4, 0, FTSQUERY_NEAR }
131400   };
131401   int ii;
131402   int iCol;
131403   int iColLen;
131404   int rc;
131405   Fts3Expr *pRet = 0;
131406 
131407   const char *zInput = z;
131408   int nInput = n;
131409 
131410   pParse->isNot = 0;
131411 
131412   /* Skip over any whitespace before checking for a keyword, an open or
131413   ** close bracket, or a quoted string.
131414   */
131415   while( nInput>0 && fts3isspace(*zInput) ){
131416     nInput--;
131417     zInput++;
131418   }
131419   if( nInput==0 ){
131420     return SQLITE_DONE;
131421   }
131422 
131423   /* See if we are dealing with a keyword. */
131424   for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
131425     const struct Fts3Keyword *pKey = &aKeyword[ii];
131426 
131427     if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
131428       continue;
131429     }
131430 
131431     if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
131432       int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
131433       int nKey = pKey->n;
131434       char cNext;
131435 
131436       /* If this is a "NEAR" keyword, check for an explicit nearness. */
131437       if( pKey->eType==FTSQUERY_NEAR ){
131438         assert( nKey==4 );
131439         if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
131440           nNear = 0;
131441           for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
131442             nNear = nNear * 10 + (zInput[nKey] - '0');
131443           }
131444         }
131445       }
131446 
131447       /* At this point this is probably a keyword. But for that to be true,
131448       ** the next byte must contain either whitespace, an open or close
131449       ** parenthesis, a quote character, or EOF.
131450       */
131451       cNext = zInput[nKey];
131452       if( fts3isspace(cNext)
131453        || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
131454       ){
131455         pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
131456         if( !pRet ){
131457           return SQLITE_NOMEM;
131458         }
131459         pRet->eType = pKey->eType;
131460         pRet->nNear = nNear;
131461         *ppExpr = pRet;
131462         *pnConsumed = (int)((zInput - z) + nKey);
131463         return SQLITE_OK;
131464       }
131465 
131466       /* Turns out that wasn't a keyword after all. This happens if the
131467       ** user has supplied a token such as "ORacle". Continue.
131468       */
131469     }
131470   }
131471 
131472   /* See if we are dealing with a quoted phrase. If this is the case, then
131473   ** search for the closing quote and pass the whole string to getNextString()
131474   ** for processing. This is easy to do, as fts3 has no syntax for escaping
131475   ** a quote character embedded in a string.
131476   */
131477   if( *zInput=='"' ){
131478     for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
131479     *pnConsumed = (int)((zInput - z) + ii + 1);
131480     if( ii==nInput ){
131481       return SQLITE_ERROR;
131482     }
131483     return getNextString(pParse, &zInput[1], ii-1, ppExpr);
131484   }
131485 
131486 
131487   /* If control flows to this point, this must be a regular token, or
131488   ** the end of the input. Read a regular token using the sqlite3_tokenizer
131489   ** interface. Before doing so, figure out if there is an explicit
131490   ** column specifier for the token.
131491   **
131492   ** TODO: Strangely, it is not possible to associate a column specifier
131493   ** with a quoted phrase, only with a single token. Not sure if this was
131494   ** an implementation artifact or an intentional decision when fts3 was
131495   ** first implemented. Whichever it was, this module duplicates the
131496   ** limitation.
131497   */
131498   iCol = pParse->iDefaultCol;
131499   iColLen = 0;
131500   for(ii=0; ii<pParse->nCol; ii++){
131501     const char *zStr = pParse->azCol[ii];
131502     int nStr = (int)strlen(zStr);
131503     if( nInput>nStr && zInput[nStr]==':'
131504      && sqlite3_strnicmp(zStr, zInput, nStr)==0
131505     ){
131506       iCol = ii;
131507       iColLen = (int)((zInput - z) + nStr + 1);
131508       break;
131509     }
131510   }
131511   rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
131512   *pnConsumed += iColLen;
131513   return rc;
131514 }
131515 
131516 /*
131517 ** The argument is an Fts3Expr structure for a binary operator (any type
131518 ** except an FTSQUERY_PHRASE). Return an integer value representing the
131519 ** precedence of the operator. Lower values have a higher precedence (i.e.
131520 ** group more tightly). For example, in the C language, the == operator
131521 ** groups more tightly than ||, and would therefore have a higher precedence.
131522 **
131523 ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
131524 ** is defined), the order of the operators in precedence from highest to
131525 ** lowest is:
131526 **
131527 **   NEAR
131528 **   NOT
131529 **   AND (including implicit ANDs)
131530 **   OR
131531 **
131532 ** Note that when using the old query syntax, the OR operator has a higher
131533 ** precedence than the AND operator.
131534 */
131535 static int opPrecedence(Fts3Expr *p){
131536   assert( p->eType!=FTSQUERY_PHRASE );
131537   if( sqlite3_fts3_enable_parentheses ){
131538     return p->eType;
131539   }else if( p->eType==FTSQUERY_NEAR ){
131540     return 1;
131541   }else if( p->eType==FTSQUERY_OR ){
131542     return 2;
131543   }
131544   assert( p->eType==FTSQUERY_AND );
131545   return 3;
131546 }
131547 
131548 /*
131549 ** Argument ppHead contains a pointer to the current head of a query
131550 ** expression tree being parsed. pPrev is the expression node most recently
131551 ** inserted into the tree. This function adds pNew, which is always a binary
131552 ** operator node, into the expression tree based on the relative precedence
131553 ** of pNew and the existing nodes of the tree. This may result in the head
131554 ** of the tree changing, in which case *ppHead is set to the new root node.
131555 */
131556 static void insertBinaryOperator(
131557   Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
131558   Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
131559   Fts3Expr *pNew           /* New binary node to insert into expression tree */
131560 ){
131561   Fts3Expr *pSplit = pPrev;
131562   while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
131563     pSplit = pSplit->pParent;
131564   }
131565 
131566   if( pSplit->pParent ){
131567     assert( pSplit->pParent->pRight==pSplit );
131568     pSplit->pParent->pRight = pNew;
131569     pNew->pParent = pSplit->pParent;
131570   }else{
131571     *ppHead = pNew;
131572   }
131573   pNew->pLeft = pSplit;
131574   pSplit->pParent = pNew;
131575 }
131576 
131577 /*
131578 ** Parse the fts3 query expression found in buffer z, length n. This function
131579 ** returns either when the end of the buffer is reached or an unmatched
131580 ** closing bracket - ')' - is encountered.
131581 **
131582 ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
131583 ** parsed form of the expression and *pnConsumed is set to the number of
131584 ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
131585 ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
131586 */
131587 static int fts3ExprParse(
131588   ParseContext *pParse,                   /* fts3 query parse context */
131589   const char *z, int n,                   /* Text of MATCH query */
131590   Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
131591   int *pnConsumed                         /* OUT: Number of bytes consumed */
131592 ){
131593   Fts3Expr *pRet = 0;
131594   Fts3Expr *pPrev = 0;
131595   Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
131596   int nIn = n;
131597   const char *zIn = z;
131598   int rc = SQLITE_OK;
131599   int isRequirePhrase = 1;
131600 
131601   while( rc==SQLITE_OK ){
131602     Fts3Expr *p = 0;
131603     int nByte = 0;
131604     rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
131605     if( rc==SQLITE_OK ){
131606       int isPhrase;
131607 
131608       if( !sqlite3_fts3_enable_parentheses
131609        && p->eType==FTSQUERY_PHRASE && pParse->isNot
131610       ){
131611         /* Create an implicit NOT operator. */
131612         Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
131613         if( !pNot ){
131614           sqlite3Fts3ExprFree(p);
131615           rc = SQLITE_NOMEM;
131616           goto exprparse_out;
131617         }
131618         pNot->eType = FTSQUERY_NOT;
131619         pNot->pRight = p;
131620         p->pParent = pNot;
131621         if( pNotBranch ){
131622           pNot->pLeft = pNotBranch;
131623           pNotBranch->pParent = pNot;
131624         }
131625         pNotBranch = pNot;
131626         p = pPrev;
131627       }else{
131628         int eType = p->eType;
131629         isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
131630 
131631         /* The isRequirePhrase variable is set to true if a phrase or
131632         ** an expression contained in parenthesis is required. If a
131633         ** binary operator (AND, OR, NOT or NEAR) is encounted when
131634         ** isRequirePhrase is set, this is a syntax error.
131635         */
131636         if( !isPhrase && isRequirePhrase ){
131637           sqlite3Fts3ExprFree(p);
131638           rc = SQLITE_ERROR;
131639           goto exprparse_out;
131640         }
131641 
131642         if( isPhrase && !isRequirePhrase ){
131643           /* Insert an implicit AND operator. */
131644           Fts3Expr *pAnd;
131645           assert( pRet && pPrev );
131646           pAnd = fts3MallocZero(sizeof(Fts3Expr));
131647           if( !pAnd ){
131648             sqlite3Fts3ExprFree(p);
131649             rc = SQLITE_NOMEM;
131650             goto exprparse_out;
131651           }
131652           pAnd->eType = FTSQUERY_AND;
131653           insertBinaryOperator(&pRet, pPrev, pAnd);
131654           pPrev = pAnd;
131655         }
131656 
131657         /* This test catches attempts to make either operand of a NEAR
131658         ** operator something other than a phrase. For example, either of
131659         ** the following:
131660         **
131661         **    (bracketed expression) NEAR phrase
131662         **    phrase NEAR (bracketed expression)
131663         **
131664         ** Return an error in either case.
131665         */
131666         if( pPrev && (
131667             (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
131668          || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
131669         )){
131670           sqlite3Fts3ExprFree(p);
131671           rc = SQLITE_ERROR;
131672           goto exprparse_out;
131673         }
131674 
131675         if( isPhrase ){
131676           if( pRet ){
131677             assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
131678             pPrev->pRight = p;
131679             p->pParent = pPrev;
131680           }else{
131681             pRet = p;
131682           }
131683         }else{
131684           insertBinaryOperator(&pRet, pPrev, p);
131685         }
131686         isRequirePhrase = !isPhrase;
131687       }
131688       assert( nByte>0 );
131689     }
131690     assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
131691     nIn -= nByte;
131692     zIn += nByte;
131693     pPrev = p;
131694   }
131695 
131696   if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
131697     rc = SQLITE_ERROR;
131698   }
131699 
131700   if( rc==SQLITE_DONE ){
131701     rc = SQLITE_OK;
131702     if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
131703       if( !pRet ){
131704         rc = SQLITE_ERROR;
131705       }else{
131706         Fts3Expr *pIter = pNotBranch;
131707         while( pIter->pLeft ){
131708           pIter = pIter->pLeft;
131709         }
131710         pIter->pLeft = pRet;
131711         pRet->pParent = pIter;
131712         pRet = pNotBranch;
131713       }
131714     }
131715   }
131716   *pnConsumed = n - nIn;
131717 
131718 exprparse_out:
131719   if( rc!=SQLITE_OK ){
131720     sqlite3Fts3ExprFree(pRet);
131721     sqlite3Fts3ExprFree(pNotBranch);
131722     pRet = 0;
131723   }
131724   *ppExpr = pRet;
131725   return rc;
131726 }
131727 
131728 /*
131729 ** Return SQLITE_ERROR if the maximum depth of the expression tree passed
131730 ** as the only argument is more than nMaxDepth.
131731 */
131732 static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
131733   int rc = SQLITE_OK;
131734   if( p ){
131735     if( nMaxDepth<0 ){
131736       rc = SQLITE_TOOBIG;
131737     }else{
131738       rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
131739       if( rc==SQLITE_OK ){
131740         rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
131741       }
131742     }
131743   }
131744   return rc;
131745 }
131746 
131747 /*
131748 ** This function attempts to transform the expression tree at (*pp) to
131749 ** an equivalent but more balanced form. The tree is modified in place.
131750 ** If successful, SQLITE_OK is returned and (*pp) set to point to the
131751 ** new root expression node.
131752 **
131753 ** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
131754 **
131755 ** Otherwise, if an error occurs, an SQLite error code is returned and
131756 ** expression (*pp) freed.
131757 */
131758 static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
131759   int rc = SQLITE_OK;             /* Return code */
131760   Fts3Expr *pRoot = *pp;          /* Initial root node */
131761   Fts3Expr *pFree = 0;            /* List of free nodes. Linked by pParent. */
131762   int eType = pRoot->eType;       /* Type of node in this tree */
131763 
131764   if( nMaxDepth==0 ){
131765     rc = SQLITE_ERROR;
131766   }
131767 
131768   if( rc==SQLITE_OK && (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
131769     Fts3Expr **apLeaf;
131770     apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
131771     if( 0==apLeaf ){
131772       rc = SQLITE_NOMEM;
131773     }else{
131774       memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
131775     }
131776 
131777     if( rc==SQLITE_OK ){
131778       int i;
131779       Fts3Expr *p;
131780 
131781       /* Set $p to point to the left-most leaf in the tree of eType nodes. */
131782       for(p=pRoot; p->eType==eType; p=p->pLeft){
131783         assert( p->pParent==0 || p->pParent->pLeft==p );
131784         assert( p->pLeft && p->pRight );
131785       }
131786 
131787       /* This loop runs once for each leaf in the tree of eType nodes. */
131788       while( 1 ){
131789         int iLvl;
131790         Fts3Expr *pParent = p->pParent;     /* Current parent of p */
131791 
131792         assert( pParent==0 || pParent->pLeft==p );
131793         p->pParent = 0;
131794         if( pParent ){
131795           pParent->pLeft = 0;
131796         }else{
131797           pRoot = 0;
131798         }
131799         rc = fts3ExprBalance(&p, nMaxDepth-1);
131800         if( rc!=SQLITE_OK ) break;
131801 
131802         for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
131803           if( apLeaf[iLvl]==0 ){
131804             apLeaf[iLvl] = p;
131805             p = 0;
131806           }else{
131807             assert( pFree );
131808             pFree->pLeft = apLeaf[iLvl];
131809             pFree->pRight = p;
131810             pFree->pLeft->pParent = pFree;
131811             pFree->pRight->pParent = pFree;
131812 
131813             p = pFree;
131814             pFree = pFree->pParent;
131815             p->pParent = 0;
131816             apLeaf[iLvl] = 0;
131817           }
131818         }
131819         if( p ){
131820           sqlite3Fts3ExprFree(p);
131821           rc = SQLITE_TOOBIG;
131822           break;
131823         }
131824 
131825         /* If that was the last leaf node, break out of the loop */
131826         if( pParent==0 ) break;
131827 
131828         /* Set $p to point to the next leaf in the tree of eType nodes */
131829         for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
131830 
131831         /* Remove pParent from the original tree. */
131832         assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
131833         pParent->pRight->pParent = pParent->pParent;
131834         if( pParent->pParent ){
131835           pParent->pParent->pLeft = pParent->pRight;
131836         }else{
131837           assert( pParent==pRoot );
131838           pRoot = pParent->pRight;
131839         }
131840 
131841         /* Link pParent into the free node list. It will be used as an
131842         ** internal node of the new tree.  */
131843         pParent->pParent = pFree;
131844         pFree = pParent;
131845       }
131846 
131847       if( rc==SQLITE_OK ){
131848         p = 0;
131849         for(i=0; i<nMaxDepth; i++){
131850           if( apLeaf[i] ){
131851             if( p==0 ){
131852               p = apLeaf[i];
131853               p->pParent = 0;
131854             }else{
131855               assert( pFree!=0 );
131856               pFree->pRight = p;
131857               pFree->pLeft = apLeaf[i];
131858               pFree->pLeft->pParent = pFree;
131859               pFree->pRight->pParent = pFree;
131860 
131861               p = pFree;
131862               pFree = pFree->pParent;
131863               p->pParent = 0;
131864             }
131865           }
131866         }
131867         pRoot = p;
131868       }else{
131869         /* An error occurred. Delete the contents of the apLeaf[] array
131870         ** and pFree list. Everything else is cleaned up by the call to
131871         ** sqlite3Fts3ExprFree(pRoot) below.  */
131872         Fts3Expr *pDel;
131873         for(i=0; i<nMaxDepth; i++){
131874           sqlite3Fts3ExprFree(apLeaf[i]);
131875         }
131876         while( (pDel=pFree)!=0 ){
131877           pFree = pDel->pParent;
131878           sqlite3_free(pDel);
131879         }
131880       }
131881 
131882       assert( pFree==0 );
131883       sqlite3_free( apLeaf );
131884     }
131885   }
131886 
131887   if( rc!=SQLITE_OK ){
131888     sqlite3Fts3ExprFree(pRoot);
131889     pRoot = 0;
131890   }
131891   *pp = pRoot;
131892   return rc;
131893 }
131894 
131895 /*
131896 ** This function is similar to sqlite3Fts3ExprParse(), with the following
131897 ** differences:
131898 **
131899 **   1. It does not do expression rebalancing.
131900 **   2. It does not check that the expression does not exceed the
131901 **      maximum allowable depth.
131902 **   3. Even if it fails, *ppExpr may still be set to point to an
131903 **      expression tree. It should be deleted using sqlite3Fts3ExprFree()
131904 **      in this case.
131905 */
131906 static int fts3ExprParseUnbalanced(
131907   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
131908   int iLangid,                        /* Language id for tokenizer */
131909   char **azCol,                       /* Array of column names for fts3 table */
131910   int bFts4,                          /* True to allow FTS4-only syntax */
131911   int nCol,                           /* Number of entries in azCol[] */
131912   int iDefaultCol,                    /* Default column to query */
131913   const char *z, int n,               /* Text of MATCH query */
131914   Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
131915 ){
131916   int nParsed;
131917   int rc;
131918   ParseContext sParse;
131919 
131920   memset(&sParse, 0, sizeof(ParseContext));
131921   sParse.pTokenizer = pTokenizer;
131922   sParse.iLangid = iLangid;
131923   sParse.azCol = (const char **)azCol;
131924   sParse.nCol = nCol;
131925   sParse.iDefaultCol = iDefaultCol;
131926   sParse.bFts4 = bFts4;
131927   if( z==0 ){
131928     *ppExpr = 0;
131929     return SQLITE_OK;
131930   }
131931   if( n<0 ){
131932     n = (int)strlen(z);
131933   }
131934   rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
131935   assert( rc==SQLITE_OK || *ppExpr==0 );
131936 
131937   /* Check for mismatched parenthesis */
131938   if( rc==SQLITE_OK && sParse.nNest ){
131939     rc = SQLITE_ERROR;
131940   }
131941 
131942   return rc;
131943 }
131944 
131945 /*
131946 ** Parameters z and n contain a pointer to and length of a buffer containing
131947 ** an fts3 query expression, respectively. This function attempts to parse the
131948 ** query expression and create a tree of Fts3Expr structures representing the
131949 ** parsed expression. If successful, *ppExpr is set to point to the head
131950 ** of the parsed expression tree and SQLITE_OK is returned. If an error
131951 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
131952 ** error) is returned and *ppExpr is set to 0.
131953 **
131954 ** If parameter n is a negative number, then z is assumed to point to a
131955 ** nul-terminated string and the length is determined using strlen().
131956 **
131957 ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
131958 ** use to normalize query tokens while parsing the expression. The azCol[]
131959 ** array, which is assumed to contain nCol entries, should contain the names
131960 ** of each column in the target fts3 table, in order from left to right.
131961 ** Column names must be nul-terminated strings.
131962 **
131963 ** The iDefaultCol parameter should be passed the index of the table column
131964 ** that appears on the left-hand-side of the MATCH operator (the default
131965 ** column to match against for tokens for which a column name is not explicitly
131966 ** specified as part of the query string), or -1 if tokens may by default
131967 ** match any table column.
131968 */
131969 SQLITE_PRIVATE int sqlite3Fts3ExprParse(
131970   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
131971   int iLangid,                        /* Language id for tokenizer */
131972   char **azCol,                       /* Array of column names for fts3 table */
131973   int bFts4,                          /* True to allow FTS4-only syntax */
131974   int nCol,                           /* Number of entries in azCol[] */
131975   int iDefaultCol,                    /* Default column to query */
131976   const char *z, int n,               /* Text of MATCH query */
131977   Fts3Expr **ppExpr,                  /* OUT: Parsed query structure */
131978   char **pzErr                        /* OUT: Error message (sqlite3_malloc) */
131979 ){
131980   int rc = fts3ExprParseUnbalanced(
131981       pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
131982   );
131983 
131984   /* Rebalance the expression. And check that its depth does not exceed
131985   ** SQLITE_FTS3_MAX_EXPR_DEPTH.  */
131986   if( rc==SQLITE_OK && *ppExpr ){
131987     rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
131988     if( rc==SQLITE_OK ){
131989       rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
131990     }
131991   }
131992 
131993   if( rc!=SQLITE_OK ){
131994     sqlite3Fts3ExprFree(*ppExpr);
131995     *ppExpr = 0;
131996     if( rc==SQLITE_TOOBIG ){
131997       *pzErr = sqlite3_mprintf(
131998           "FTS expression tree is too large (maximum depth %d)",
131999           SQLITE_FTS3_MAX_EXPR_DEPTH
132000       );
132001       rc = SQLITE_ERROR;
132002     }else if( rc==SQLITE_ERROR ){
132003       *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z);
132004     }
132005   }
132006 
132007   return rc;
132008 }
132009 
132010 /*
132011 ** Free a single node of an expression tree.
132012 */
132013 static void fts3FreeExprNode(Fts3Expr *p){
132014   assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
132015   sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
132016   sqlite3_free(p->aMI);
132017   sqlite3_free(p);
132018 }
132019 
132020 /*
132021 ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
132022 **
132023 ** This function would be simpler if it recursively called itself. But
132024 ** that would mean passing a sufficiently large expression to ExprParse()
132025 ** could cause a stack overflow.
132026 */
132027 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
132028   Fts3Expr *p;
132029   assert( pDel==0 || pDel->pParent==0 );
132030   for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
132031     assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
132032   }
132033   while( p ){
132034     Fts3Expr *pParent = p->pParent;
132035     fts3FreeExprNode(p);
132036     if( pParent && p==pParent->pLeft && pParent->pRight ){
132037       p = pParent->pRight;
132038       while( p && (p->pLeft || p->pRight) ){
132039         assert( p==p->pParent->pRight || p==p->pParent->pLeft );
132040         p = (p->pLeft ? p->pLeft : p->pRight);
132041       }
132042     }else{
132043       p = pParent;
132044     }
132045   }
132046 }
132047 
132048 /****************************************************************************
132049 *****************************************************************************
132050 ** Everything after this point is just test code.
132051 */
132052 
132053 #ifdef SQLITE_TEST
132054 
132055 /* #include <stdio.h> */
132056 
132057 /*
132058 ** Function to query the hash-table of tokenizers (see README.tokenizers).
132059 */
132060 static int queryTestTokenizer(
132061   sqlite3 *db,
132062   const char *zName,
132063   const sqlite3_tokenizer_module **pp
132064 ){
132065   int rc;
132066   sqlite3_stmt *pStmt;
132067   const char zSql[] = "SELECT fts3_tokenizer(?)";
132068 
132069   *pp = 0;
132070   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
132071   if( rc!=SQLITE_OK ){
132072     return rc;
132073   }
132074 
132075   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
132076   if( SQLITE_ROW==sqlite3_step(pStmt) ){
132077     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
132078       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
132079     }
132080   }
132081 
132082   return sqlite3_finalize(pStmt);
132083 }
132084 
132085 /*
132086 ** Return a pointer to a buffer containing a text representation of the
132087 ** expression passed as the first argument. The buffer is obtained from
132088 ** sqlite3_malloc(). It is the responsibility of the caller to use
132089 ** sqlite3_free() to release the memory. If an OOM condition is encountered,
132090 ** NULL is returned.
132091 **
132092 ** If the second argument is not NULL, then its contents are prepended to
132093 ** the returned expression text and then freed using sqlite3_free().
132094 */
132095 static char *exprToString(Fts3Expr *pExpr, char *zBuf){
132096   if( pExpr==0 ){
132097     return sqlite3_mprintf("");
132098   }
132099   switch( pExpr->eType ){
132100     case FTSQUERY_PHRASE: {
132101       Fts3Phrase *pPhrase = pExpr->pPhrase;
132102       int i;
132103       zBuf = sqlite3_mprintf(
132104           "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
132105       for(i=0; zBuf && i<pPhrase->nToken; i++){
132106         zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
132107             pPhrase->aToken[i].n, pPhrase->aToken[i].z,
132108             (pPhrase->aToken[i].isPrefix?"+":"")
132109         );
132110       }
132111       return zBuf;
132112     }
132113 
132114     case FTSQUERY_NEAR:
132115       zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
132116       break;
132117     case FTSQUERY_NOT:
132118       zBuf = sqlite3_mprintf("%zNOT ", zBuf);
132119       break;
132120     case FTSQUERY_AND:
132121       zBuf = sqlite3_mprintf("%zAND ", zBuf);
132122       break;
132123     case FTSQUERY_OR:
132124       zBuf = sqlite3_mprintf("%zOR ", zBuf);
132125       break;
132126   }
132127 
132128   if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
132129   if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
132130   if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
132131 
132132   if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
132133   if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
132134 
132135   return zBuf;
132136 }
132137 
132138 /*
132139 ** This is the implementation of a scalar SQL function used to test the
132140 ** expression parser. It should be called as follows:
132141 **
132142 **   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
132143 **
132144 ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
132145 ** to parse the query expression (see README.tokenizers). The second argument
132146 ** is the query expression to parse. Each subsequent argument is the name
132147 ** of a column of the fts3 table that the query expression may refer to.
132148 ** For example:
132149 **
132150 **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
132151 */
132152 static void fts3ExprTest(
132153   sqlite3_context *context,
132154   int argc,
132155   sqlite3_value **argv
132156 ){
132157   sqlite3_tokenizer_module const *pModule = 0;
132158   sqlite3_tokenizer *pTokenizer = 0;
132159   int rc;
132160   char **azCol = 0;
132161   const char *zExpr;
132162   int nExpr;
132163   int nCol;
132164   int ii;
132165   Fts3Expr *pExpr;
132166   char *zBuf = 0;
132167   sqlite3 *db = sqlite3_context_db_handle(context);
132168 
132169   if( argc<3 ){
132170     sqlite3_result_error(context,
132171         "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
132172     );
132173     return;
132174   }
132175 
132176   rc = queryTestTokenizer(db,
132177                           (const char *)sqlite3_value_text(argv[0]), &pModule);
132178   if( rc==SQLITE_NOMEM ){
132179     sqlite3_result_error_nomem(context);
132180     goto exprtest_out;
132181   }else if( !pModule ){
132182     sqlite3_result_error(context, "No such tokenizer module", -1);
132183     goto exprtest_out;
132184   }
132185 
132186   rc = pModule->xCreate(0, 0, &pTokenizer);
132187   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
132188   if( rc==SQLITE_NOMEM ){
132189     sqlite3_result_error_nomem(context);
132190     goto exprtest_out;
132191   }
132192   pTokenizer->pModule = pModule;
132193 
132194   zExpr = (const char *)sqlite3_value_text(argv[1]);
132195   nExpr = sqlite3_value_bytes(argv[1]);
132196   nCol = argc-2;
132197   azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
132198   if( !azCol ){
132199     sqlite3_result_error_nomem(context);
132200     goto exprtest_out;
132201   }
132202   for(ii=0; ii<nCol; ii++){
132203     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
132204   }
132205 
132206   if( sqlite3_user_data(context) ){
132207     char *zDummy = 0;
132208     rc = sqlite3Fts3ExprParse(
132209         pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
132210     );
132211     assert( rc==SQLITE_OK || pExpr==0 );
132212     sqlite3_free(zDummy);
132213   }else{
132214     rc = fts3ExprParseUnbalanced(
132215         pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
132216     );
132217   }
132218 
132219   if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
132220     sqlite3Fts3ExprFree(pExpr);
132221     sqlite3_result_error(context, "Error parsing expression", -1);
132222   }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
132223     sqlite3_result_error_nomem(context);
132224   }else{
132225     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
132226     sqlite3_free(zBuf);
132227   }
132228 
132229   sqlite3Fts3ExprFree(pExpr);
132230 
132231 exprtest_out:
132232   if( pModule && pTokenizer ){
132233     rc = pModule->xDestroy(pTokenizer);
132234   }
132235   sqlite3_free(azCol);
132236 }
132237 
132238 /*
132239 ** Register the query expression parser test function fts3_exprtest()
132240 ** with database connection db.
132241 */
132242 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
132243   int rc = sqlite3_create_function(
132244       db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
132245   );
132246   if( rc==SQLITE_OK ){
132247     rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
132248         -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
132249     );
132250   }
132251   return rc;
132252 }
132253 
132254 #endif
132255 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
132256 
132257 /************** End of fts3_expr.c *******************************************/
132258 /************** Begin file fts3_hash.c ***************************************/
132259 /*
132260 ** 2001 September 22
132261 **
132262 ** The author disclaims copyright to this source code.  In place of
132263 ** a legal notice, here is a blessing:
132264 **
132265 **    May you do good and not evil.
132266 **    May you find forgiveness for yourself and forgive others.
132267 **    May you share freely, never taking more than you give.
132268 **
132269 *************************************************************************
132270 ** This is the implementation of generic hash-tables used in SQLite.
132271 ** We've modified it slightly to serve as a standalone hash table
132272 ** implementation for the full-text indexing module.
132273 */
132274 
132275 /*
132276 ** The code in this file is only compiled if:
132277 **
132278 **     * The FTS3 module is being built as an extension
132279 **       (in which case SQLITE_CORE is not defined), or
132280 **
132281 **     * The FTS3 module is being built into the core of
132282 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
132283 */
132284 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
132285 
132286 /* #include <assert.h> */
132287 /* #include <stdlib.h> */
132288 /* #include <string.h> */
132289 
132290 
132291 /*
132292 ** Malloc and Free functions
132293 */
132294 static void *fts3HashMalloc(int n){
132295   void *p = sqlite3_malloc(n);
132296   if( p ){
132297     memset(p, 0, n);
132298   }
132299   return p;
132300 }
132301 static void fts3HashFree(void *p){
132302   sqlite3_free(p);
132303 }
132304 
132305 /* Turn bulk memory into a hash table object by initializing the
132306 ** fields of the Hash structure.
132307 **
132308 ** "pNew" is a pointer to the hash table that is to be initialized.
132309 ** keyClass is one of the constants
132310 ** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass
132311 ** determines what kind of key the hash table will use.  "copyKey" is
132312 ** true if the hash table should make its own private copy of keys and
132313 ** false if it should just use the supplied pointer.
132314 */
132315 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
132316   assert( pNew!=0 );
132317   assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
132318   pNew->keyClass = keyClass;
132319   pNew->copyKey = copyKey;
132320   pNew->first = 0;
132321   pNew->count = 0;
132322   pNew->htsize = 0;
132323   pNew->ht = 0;
132324 }
132325 
132326 /* Remove all entries from a hash table.  Reclaim all memory.
132327 ** Call this routine to delete a hash table or to reset a hash table
132328 ** to the empty state.
132329 */
132330 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
132331   Fts3HashElem *elem;         /* For looping over all elements of the table */
132332 
132333   assert( pH!=0 );
132334   elem = pH->first;
132335   pH->first = 0;
132336   fts3HashFree(pH->ht);
132337   pH->ht = 0;
132338   pH->htsize = 0;
132339   while( elem ){
132340     Fts3HashElem *next_elem = elem->next;
132341     if( pH->copyKey && elem->pKey ){
132342       fts3HashFree(elem->pKey);
132343     }
132344     fts3HashFree(elem);
132345     elem = next_elem;
132346   }
132347   pH->count = 0;
132348 }
132349 
132350 /*
132351 ** Hash and comparison functions when the mode is FTS3_HASH_STRING
132352 */
132353 static int fts3StrHash(const void *pKey, int nKey){
132354   const char *z = (const char *)pKey;
132355   unsigned h = 0;
132356   if( nKey<=0 ) nKey = (int) strlen(z);
132357   while( nKey > 0  ){
132358     h = (h<<3) ^ h ^ *z++;
132359     nKey--;
132360   }
132361   return (int)(h & 0x7fffffff);
132362 }
132363 static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
132364   if( n1!=n2 ) return 1;
132365   return strncmp((const char*)pKey1,(const char*)pKey2,n1);
132366 }
132367 
132368 /*
132369 ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
132370 */
132371 static int fts3BinHash(const void *pKey, int nKey){
132372   int h = 0;
132373   const char *z = (const char *)pKey;
132374   while( nKey-- > 0 ){
132375     h = (h<<3) ^ h ^ *(z++);
132376   }
132377   return h & 0x7fffffff;
132378 }
132379 static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
132380   if( n1!=n2 ) return 1;
132381   return memcmp(pKey1,pKey2,n1);
132382 }
132383 
132384 /*
132385 ** Return a pointer to the appropriate hash function given the key class.
132386 **
132387 ** The C syntax in this function definition may be unfamilar to some
132388 ** programmers, so we provide the following additional explanation:
132389 **
132390 ** The name of the function is "ftsHashFunction".  The function takes a
132391 ** single parameter "keyClass".  The return value of ftsHashFunction()
132392 ** is a pointer to another function.  Specifically, the return value
132393 ** of ftsHashFunction() is a pointer to a function that takes two parameters
132394 ** with types "const void*" and "int" and returns an "int".
132395 */
132396 static int (*ftsHashFunction(int keyClass))(const void*,int){
132397   if( keyClass==FTS3_HASH_STRING ){
132398     return &fts3StrHash;
132399   }else{
132400     assert( keyClass==FTS3_HASH_BINARY );
132401     return &fts3BinHash;
132402   }
132403 }
132404 
132405 /*
132406 ** Return a pointer to the appropriate hash function given the key class.
132407 **
132408 ** For help in interpreted the obscure C code in the function definition,
132409 ** see the header comment on the previous function.
132410 */
132411 static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
132412   if( keyClass==FTS3_HASH_STRING ){
132413     return &fts3StrCompare;
132414   }else{
132415     assert( keyClass==FTS3_HASH_BINARY );
132416     return &fts3BinCompare;
132417   }
132418 }
132419 
132420 /* Link an element into the hash table
132421 */
132422 static void fts3HashInsertElement(
132423   Fts3Hash *pH,            /* The complete hash table */
132424   struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
132425   Fts3HashElem *pNew       /* The element to be inserted */
132426 ){
132427   Fts3HashElem *pHead;     /* First element already in pEntry */
132428   pHead = pEntry->chain;
132429   if( pHead ){
132430     pNew->next = pHead;
132431     pNew->prev = pHead->prev;
132432     if( pHead->prev ){ pHead->prev->next = pNew; }
132433     else             { pH->first = pNew; }
132434     pHead->prev = pNew;
132435   }else{
132436     pNew->next = pH->first;
132437     if( pH->first ){ pH->first->prev = pNew; }
132438     pNew->prev = 0;
132439     pH->first = pNew;
132440   }
132441   pEntry->count++;
132442   pEntry->chain = pNew;
132443 }
132444 
132445 
132446 /* Resize the hash table so that it cantains "new_size" buckets.
132447 ** "new_size" must be a power of 2.  The hash table might fail
132448 ** to resize if sqliteMalloc() fails.
132449 **
132450 ** Return non-zero if a memory allocation error occurs.
132451 */
132452 static int fts3Rehash(Fts3Hash *pH, int new_size){
132453   struct _fts3ht *new_ht;          /* The new hash table */
132454   Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
132455   int (*xHash)(const void*,int);   /* The hash function */
132456 
132457   assert( (new_size & (new_size-1))==0 );
132458   new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
132459   if( new_ht==0 ) return 1;
132460   fts3HashFree(pH->ht);
132461   pH->ht = new_ht;
132462   pH->htsize = new_size;
132463   xHash = ftsHashFunction(pH->keyClass);
132464   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
132465     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
132466     next_elem = elem->next;
132467     fts3HashInsertElement(pH, &new_ht[h], elem);
132468   }
132469   return 0;
132470 }
132471 
132472 /* This function (for internal use only) locates an element in an
132473 ** hash table that matches the given key.  The hash for this key has
132474 ** already been computed and is passed as the 4th parameter.
132475 */
132476 static Fts3HashElem *fts3FindElementByHash(
132477   const Fts3Hash *pH, /* The pH to be searched */
132478   const void *pKey,   /* The key we are searching for */
132479   int nKey,
132480   int h               /* The hash for this key. */
132481 ){
132482   Fts3HashElem *elem;            /* Used to loop thru the element list */
132483   int count;                     /* Number of elements left to test */
132484   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
132485 
132486   if( pH->ht ){
132487     struct _fts3ht *pEntry = &pH->ht[h];
132488     elem = pEntry->chain;
132489     count = pEntry->count;
132490     xCompare = ftsCompareFunction(pH->keyClass);
132491     while( count-- && elem ){
132492       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
132493         return elem;
132494       }
132495       elem = elem->next;
132496     }
132497   }
132498   return 0;
132499 }
132500 
132501 /* Remove a single entry from the hash table given a pointer to that
132502 ** element and a hash on the element's key.
132503 */
132504 static void fts3RemoveElementByHash(
132505   Fts3Hash *pH,         /* The pH containing "elem" */
132506   Fts3HashElem* elem,   /* The element to be removed from the pH */
132507   int h                 /* Hash value for the element */
132508 ){
132509   struct _fts3ht *pEntry;
132510   if( elem->prev ){
132511     elem->prev->next = elem->next;
132512   }else{
132513     pH->first = elem->next;
132514   }
132515   if( elem->next ){
132516     elem->next->prev = elem->prev;
132517   }
132518   pEntry = &pH->ht[h];
132519   if( pEntry->chain==elem ){
132520     pEntry->chain = elem->next;
132521   }
132522   pEntry->count--;
132523   if( pEntry->count<=0 ){
132524     pEntry->chain = 0;
132525   }
132526   if( pH->copyKey && elem->pKey ){
132527     fts3HashFree(elem->pKey);
132528   }
132529   fts3HashFree( elem );
132530   pH->count--;
132531   if( pH->count<=0 ){
132532     assert( pH->first==0 );
132533     assert( pH->count==0 );
132534     fts3HashClear(pH);
132535   }
132536 }
132537 
132538 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
132539   const Fts3Hash *pH,
132540   const void *pKey,
132541   int nKey
132542 ){
132543   int h;                          /* A hash on key */
132544   int (*xHash)(const void*,int);  /* The hash function */
132545 
132546   if( pH==0 || pH->ht==0 ) return 0;
132547   xHash = ftsHashFunction(pH->keyClass);
132548   assert( xHash!=0 );
132549   h = (*xHash)(pKey,nKey);
132550   assert( (pH->htsize & (pH->htsize-1))==0 );
132551   return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
132552 }
132553 
132554 /*
132555 ** Attempt to locate an element of the hash table pH with a key
132556 ** that matches pKey,nKey.  Return the data for this element if it is
132557 ** found, or NULL if there is no match.
132558 */
132559 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
132560   Fts3HashElem *pElem;            /* The element that matches key (if any) */
132561 
132562   pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
132563   return pElem ? pElem->data : 0;
132564 }
132565 
132566 /* Insert an element into the hash table pH.  The key is pKey,nKey
132567 ** and the data is "data".
132568 **
132569 ** If no element exists with a matching key, then a new
132570 ** element is created.  A copy of the key is made if the copyKey
132571 ** flag is set.  NULL is returned.
132572 **
132573 ** If another element already exists with the same key, then the
132574 ** new data replaces the old data and the old data is returned.
132575 ** The key is not copied in this instance.  If a malloc fails, then
132576 ** the new data is returned and the hash table is unchanged.
132577 **
132578 ** If the "data" parameter to this function is NULL, then the
132579 ** element corresponding to "key" is removed from the hash table.
132580 */
132581 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
132582   Fts3Hash *pH,        /* The hash table to insert into */
132583   const void *pKey,    /* The key */
132584   int nKey,            /* Number of bytes in the key */
132585   void *data           /* The data */
132586 ){
132587   int hraw;                 /* Raw hash value of the key */
132588   int h;                    /* the hash of the key modulo hash table size */
132589   Fts3HashElem *elem;       /* Used to loop thru the element list */
132590   Fts3HashElem *new_elem;   /* New element added to the pH */
132591   int (*xHash)(const void*,int);  /* The hash function */
132592 
132593   assert( pH!=0 );
132594   xHash = ftsHashFunction(pH->keyClass);
132595   assert( xHash!=0 );
132596   hraw = (*xHash)(pKey, nKey);
132597   assert( (pH->htsize & (pH->htsize-1))==0 );
132598   h = hraw & (pH->htsize-1);
132599   elem = fts3FindElementByHash(pH,pKey,nKey,h);
132600   if( elem ){
132601     void *old_data = elem->data;
132602     if( data==0 ){
132603       fts3RemoveElementByHash(pH,elem,h);
132604     }else{
132605       elem->data = data;
132606     }
132607     return old_data;
132608   }
132609   if( data==0 ) return 0;
132610   if( (pH->htsize==0 && fts3Rehash(pH,8))
132611    || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
132612   ){
132613     pH->count = 0;
132614     return data;
132615   }
132616   assert( pH->htsize>0 );
132617   new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
132618   if( new_elem==0 ) return data;
132619   if( pH->copyKey && pKey!=0 ){
132620     new_elem->pKey = fts3HashMalloc( nKey );
132621     if( new_elem->pKey==0 ){
132622       fts3HashFree(new_elem);
132623       return data;
132624     }
132625     memcpy((void*)new_elem->pKey, pKey, nKey);
132626   }else{
132627     new_elem->pKey = (void*)pKey;
132628   }
132629   new_elem->nKey = nKey;
132630   pH->count++;
132631   assert( pH->htsize>0 );
132632   assert( (pH->htsize & (pH->htsize-1))==0 );
132633   h = hraw & (pH->htsize-1);
132634   fts3HashInsertElement(pH, &pH->ht[h], new_elem);
132635   new_elem->data = data;
132636   return 0;
132637 }
132638 
132639 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
132640 
132641 /************** End of fts3_hash.c *******************************************/
132642 /************** Begin file fts3_porter.c *************************************/
132643 /*
132644 ** 2006 September 30
132645 **
132646 ** The author disclaims copyright to this source code.  In place of
132647 ** a legal notice, here is a blessing:
132648 **
132649 **    May you do good and not evil.
132650 **    May you find forgiveness for yourself and forgive others.
132651 **    May you share freely, never taking more than you give.
132652 **
132653 *************************************************************************
132654 ** Implementation of the full-text-search tokenizer that implements
132655 ** a Porter stemmer.
132656 */
132657 
132658 /*
132659 ** The code in this file is only compiled if:
132660 **
132661 **     * The FTS3 module is being built as an extension
132662 **       (in which case SQLITE_CORE is not defined), or
132663 **
132664 **     * The FTS3 module is being built into the core of
132665 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
132666 */
132667 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
132668 
132669 /* #include <assert.h> */
132670 /* #include <stdlib.h> */
132671 /* #include <stdio.h> */
132672 /* #include <string.h> */
132673 
132674 
132675 /*
132676 ** Class derived from sqlite3_tokenizer
132677 */
132678 typedef struct porter_tokenizer {
132679   sqlite3_tokenizer base;      /* Base class */
132680 } porter_tokenizer;
132681 
132682 /*
132683 ** Class derived from sqlite3_tokenizer_cursor
132684 */
132685 typedef struct porter_tokenizer_cursor {
132686   sqlite3_tokenizer_cursor base;
132687   const char *zInput;          /* input we are tokenizing */
132688   int nInput;                  /* size of the input */
132689   int iOffset;                 /* current position in zInput */
132690   int iToken;                  /* index of next token to be returned */
132691   char *zToken;                /* storage for current token */
132692   int nAllocated;              /* space allocated to zToken buffer */
132693 } porter_tokenizer_cursor;
132694 
132695 
132696 /*
132697 ** Create a new tokenizer instance.
132698 */
132699 static int porterCreate(
132700   int argc, const char * const *argv,
132701   sqlite3_tokenizer **ppTokenizer
132702 ){
132703   porter_tokenizer *t;
132704 
132705   UNUSED_PARAMETER(argc);
132706   UNUSED_PARAMETER(argv);
132707 
132708   t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
132709   if( t==NULL ) return SQLITE_NOMEM;
132710   memset(t, 0, sizeof(*t));
132711   *ppTokenizer = &t->base;
132712   return SQLITE_OK;
132713 }
132714 
132715 /*
132716 ** Destroy a tokenizer
132717 */
132718 static int porterDestroy(sqlite3_tokenizer *pTokenizer){
132719   sqlite3_free(pTokenizer);
132720   return SQLITE_OK;
132721 }
132722 
132723 /*
132724 ** Prepare to begin tokenizing a particular string.  The input
132725 ** string to be tokenized is zInput[0..nInput-1].  A cursor
132726 ** used to incrementally tokenize this string is returned in
132727 ** *ppCursor.
132728 */
132729 static int porterOpen(
132730   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
132731   const char *zInput, int nInput,        /* String to be tokenized */
132732   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
132733 ){
132734   porter_tokenizer_cursor *c;
132735 
132736   UNUSED_PARAMETER(pTokenizer);
132737 
132738   c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
132739   if( c==NULL ) return SQLITE_NOMEM;
132740 
132741   c->zInput = zInput;
132742   if( zInput==0 ){
132743     c->nInput = 0;
132744   }else if( nInput<0 ){
132745     c->nInput = (int)strlen(zInput);
132746   }else{
132747     c->nInput = nInput;
132748   }
132749   c->iOffset = 0;                 /* start tokenizing at the beginning */
132750   c->iToken = 0;
132751   c->zToken = NULL;               /* no space allocated, yet. */
132752   c->nAllocated = 0;
132753 
132754   *ppCursor = &c->base;
132755   return SQLITE_OK;
132756 }
132757 
132758 /*
132759 ** Close a tokenization cursor previously opened by a call to
132760 ** porterOpen() above.
132761 */
132762 static int porterClose(sqlite3_tokenizer_cursor *pCursor){
132763   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
132764   sqlite3_free(c->zToken);
132765   sqlite3_free(c);
132766   return SQLITE_OK;
132767 }
132768 /*
132769 ** Vowel or consonant
132770 */
132771 static const char cType[] = {
132772    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
132773    1, 1, 1, 2, 1
132774 };
132775 
132776 /*
132777 ** isConsonant() and isVowel() determine if their first character in
132778 ** the string they point to is a consonant or a vowel, according
132779 ** to Porter ruls.
132780 **
132781 ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
132782 ** 'Y' is a consonant unless it follows another consonant,
132783 ** in which case it is a vowel.
132784 **
132785 ** In these routine, the letters are in reverse order.  So the 'y' rule
132786 ** is that 'y' is a consonant unless it is followed by another
132787 ** consonent.
132788 */
132789 static int isVowel(const char*);
132790 static int isConsonant(const char *z){
132791   int j;
132792   char x = *z;
132793   if( x==0 ) return 0;
132794   assert( x>='a' && x<='z' );
132795   j = cType[x-'a'];
132796   if( j<2 ) return j;
132797   return z[1]==0 || isVowel(z + 1);
132798 }
132799 static int isVowel(const char *z){
132800   int j;
132801   char x = *z;
132802   if( x==0 ) return 0;
132803   assert( x>='a' && x<='z' );
132804   j = cType[x-'a'];
132805   if( j<2 ) return 1-j;
132806   return isConsonant(z + 1);
132807 }
132808 
132809 /*
132810 ** Let any sequence of one or more vowels be represented by V and let
132811 ** C be sequence of one or more consonants.  Then every word can be
132812 ** represented as:
132813 **
132814 **           [C] (VC){m} [V]
132815 **
132816 ** In prose:  A word is an optional consonant followed by zero or
132817 ** vowel-consonant pairs followed by an optional vowel.  "m" is the
132818 ** number of vowel consonant pairs.  This routine computes the value
132819 ** of m for the first i bytes of a word.
132820 **
132821 ** Return true if the m-value for z is 1 or more.  In other words,
132822 ** return true if z contains at least one vowel that is followed
132823 ** by a consonant.
132824 **
132825 ** In this routine z[] is in reverse order.  So we are really looking
132826 ** for an instance of of a consonant followed by a vowel.
132827 */
132828 static int m_gt_0(const char *z){
132829   while( isVowel(z) ){ z++; }
132830   if( *z==0 ) return 0;
132831   while( isConsonant(z) ){ z++; }
132832   return *z!=0;
132833 }
132834 
132835 /* Like mgt0 above except we are looking for a value of m which is
132836 ** exactly 1
132837 */
132838 static int m_eq_1(const char *z){
132839   while( isVowel(z) ){ z++; }
132840   if( *z==0 ) return 0;
132841   while( isConsonant(z) ){ z++; }
132842   if( *z==0 ) return 0;
132843   while( isVowel(z) ){ z++; }
132844   if( *z==0 ) return 1;
132845   while( isConsonant(z) ){ z++; }
132846   return *z==0;
132847 }
132848 
132849 /* Like mgt0 above except we are looking for a value of m>1 instead
132850 ** or m>0
132851 */
132852 static int m_gt_1(const char *z){
132853   while( isVowel(z) ){ z++; }
132854   if( *z==0 ) return 0;
132855   while( isConsonant(z) ){ z++; }
132856   if( *z==0 ) return 0;
132857   while( isVowel(z) ){ z++; }
132858   if( *z==0 ) return 0;
132859   while( isConsonant(z) ){ z++; }
132860   return *z!=0;
132861 }
132862 
132863 /*
132864 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
132865 */
132866 static int hasVowel(const char *z){
132867   while( isConsonant(z) ){ z++; }
132868   return *z!=0;
132869 }
132870 
132871 /*
132872 ** Return TRUE if the word ends in a double consonant.
132873 **
132874 ** The text is reversed here. So we are really looking at
132875 ** the first two characters of z[].
132876 */
132877 static int doubleConsonant(const char *z){
132878   return isConsonant(z) && z[0]==z[1];
132879 }
132880 
132881 /*
132882 ** Return TRUE if the word ends with three letters which
132883 ** are consonant-vowel-consonent and where the final consonant
132884 ** is not 'w', 'x', or 'y'.
132885 **
132886 ** The word is reversed here.  So we are really checking the
132887 ** first three letters and the first one cannot be in [wxy].
132888 */
132889 static int star_oh(const char *z){
132890   return
132891     isConsonant(z) &&
132892     z[0]!='w' && z[0]!='x' && z[0]!='y' &&
132893     isVowel(z+1) &&
132894     isConsonant(z+2);
132895 }
132896 
132897 /*
132898 ** If the word ends with zFrom and xCond() is true for the stem
132899 ** of the word that preceeds the zFrom ending, then change the
132900 ** ending to zTo.
132901 **
132902 ** The input word *pz and zFrom are both in reverse order.  zTo
132903 ** is in normal order.
132904 **
132905 ** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
132906 ** match.  Not that TRUE is returned even if xCond() fails and
132907 ** no substitution occurs.
132908 */
132909 static int stem(
132910   char **pz,             /* The word being stemmed (Reversed) */
132911   const char *zFrom,     /* If the ending matches this... (Reversed) */
132912   const char *zTo,       /* ... change the ending to this (not reversed) */
132913   int (*xCond)(const char*)   /* Condition that must be true */
132914 ){
132915   char *z = *pz;
132916   while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
132917   if( *zFrom!=0 ) return 0;
132918   if( xCond && !xCond(z) ) return 1;
132919   while( *zTo ){
132920     *(--z) = *(zTo++);
132921   }
132922   *pz = z;
132923   return 1;
132924 }
132925 
132926 /*
132927 ** This is the fallback stemmer used when the porter stemmer is
132928 ** inappropriate.  The input word is copied into the output with
132929 ** US-ASCII case folding.  If the input word is too long (more
132930 ** than 20 bytes if it contains no digits or more than 6 bytes if
132931 ** it contains digits) then word is truncated to 20 or 6 bytes
132932 ** by taking 10 or 3 bytes from the beginning and end.
132933 */
132934 static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
132935   int i, mx, j;
132936   int hasDigit = 0;
132937   for(i=0; i<nIn; i++){
132938     char c = zIn[i];
132939     if( c>='A' && c<='Z' ){
132940       zOut[i] = c - 'A' + 'a';
132941     }else{
132942       if( c>='0' && c<='9' ) hasDigit = 1;
132943       zOut[i] = c;
132944     }
132945   }
132946   mx = hasDigit ? 3 : 10;
132947   if( nIn>mx*2 ){
132948     for(j=mx, i=nIn-mx; i<nIn; i++, j++){
132949       zOut[j] = zOut[i];
132950     }
132951     i = j;
132952   }
132953   zOut[i] = 0;
132954   *pnOut = i;
132955 }
132956 
132957 
132958 /*
132959 ** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
132960 ** zOut is at least big enough to hold nIn bytes.  Write the actual
132961 ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
132962 **
132963 ** Any upper-case characters in the US-ASCII character set ([A-Z])
132964 ** are converted to lower case.  Upper-case UTF characters are
132965 ** unchanged.
132966 **
132967 ** Words that are longer than about 20 bytes are stemmed by retaining
132968 ** a few bytes from the beginning and the end of the word.  If the
132969 ** word contains digits, 3 bytes are taken from the beginning and
132970 ** 3 bytes from the end.  For long words without digits, 10 bytes
132971 ** are taken from each end.  US-ASCII case folding still applies.
132972 **
132973 ** If the input word contains not digits but does characters not
132974 ** in [a-zA-Z] then no stemming is attempted and this routine just
132975 ** copies the input into the input into the output with US-ASCII
132976 ** case folding.
132977 **
132978 ** Stemming never increases the length of the word.  So there is
132979 ** no chance of overflowing the zOut buffer.
132980 */
132981 static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
132982   int i, j;
132983   char zReverse[28];
132984   char *z, *z2;
132985   if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
132986     /* The word is too big or too small for the porter stemmer.
132987     ** Fallback to the copy stemmer */
132988     copy_stemmer(zIn, nIn, zOut, pnOut);
132989     return;
132990   }
132991   for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
132992     char c = zIn[i];
132993     if( c>='A' && c<='Z' ){
132994       zReverse[j] = c + 'a' - 'A';
132995     }else if( c>='a' && c<='z' ){
132996       zReverse[j] = c;
132997     }else{
132998       /* The use of a character not in [a-zA-Z] means that we fallback
132999       ** to the copy stemmer */
133000       copy_stemmer(zIn, nIn, zOut, pnOut);
133001       return;
133002     }
133003   }
133004   memset(&zReverse[sizeof(zReverse)-5], 0, 5);
133005   z = &zReverse[j+1];
133006 
133007 
133008   /* Step 1a */
133009   if( z[0]=='s' ){
133010     if(
133011      !stem(&z, "sess", "ss", 0) &&
133012      !stem(&z, "sei", "i", 0)  &&
133013      !stem(&z, "ss", "ss", 0)
133014     ){
133015       z++;
133016     }
133017   }
133018 
133019   /* Step 1b */
133020   z2 = z;
133021   if( stem(&z, "dee", "ee", m_gt_0) ){
133022     /* Do nothing.  The work was all in the test */
133023   }else if(
133024      (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
133025       && z!=z2
133026   ){
133027      if( stem(&z, "ta", "ate", 0) ||
133028          stem(&z, "lb", "ble", 0) ||
133029          stem(&z, "zi", "ize", 0) ){
133030        /* Do nothing.  The work was all in the test */
133031      }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
133032        z++;
133033      }else if( m_eq_1(z) && star_oh(z) ){
133034        *(--z) = 'e';
133035      }
133036   }
133037 
133038   /* Step 1c */
133039   if( z[0]=='y' && hasVowel(z+1) ){
133040     z[0] = 'i';
133041   }
133042 
133043   /* Step 2 */
133044   switch( z[1] ){
133045    case 'a':
133046      if( !stem(&z, "lanoita", "ate", m_gt_0) ){
133047        stem(&z, "lanoit", "tion", m_gt_0);
133048      }
133049      break;
133050    case 'c':
133051      if( !stem(&z, "icne", "ence", m_gt_0) ){
133052        stem(&z, "icna", "ance", m_gt_0);
133053      }
133054      break;
133055    case 'e':
133056      stem(&z, "rezi", "ize", m_gt_0);
133057      break;
133058    case 'g':
133059      stem(&z, "igol", "log", m_gt_0);
133060      break;
133061    case 'l':
133062      if( !stem(&z, "ilb", "ble", m_gt_0)
133063       && !stem(&z, "illa", "al", m_gt_0)
133064       && !stem(&z, "iltne", "ent", m_gt_0)
133065       && !stem(&z, "ile", "e", m_gt_0)
133066      ){
133067        stem(&z, "ilsuo", "ous", m_gt_0);
133068      }
133069      break;
133070    case 'o':
133071      if( !stem(&z, "noitazi", "ize", m_gt_0)
133072       && !stem(&z, "noita", "ate", m_gt_0)
133073      ){
133074        stem(&z, "rota", "ate", m_gt_0);
133075      }
133076      break;
133077    case 's':
133078      if( !stem(&z, "msila", "al", m_gt_0)
133079       && !stem(&z, "ssenevi", "ive", m_gt_0)
133080       && !stem(&z, "ssenluf", "ful", m_gt_0)
133081      ){
133082        stem(&z, "ssensuo", "ous", m_gt_0);
133083      }
133084      break;
133085    case 't':
133086      if( !stem(&z, "itila", "al", m_gt_0)
133087       && !stem(&z, "itivi", "ive", m_gt_0)
133088      ){
133089        stem(&z, "itilib", "ble", m_gt_0);
133090      }
133091      break;
133092   }
133093 
133094   /* Step 3 */
133095   switch( z[0] ){
133096    case 'e':
133097      if( !stem(&z, "etaci", "ic", m_gt_0)
133098       && !stem(&z, "evita", "", m_gt_0)
133099      ){
133100        stem(&z, "ezila", "al", m_gt_0);
133101      }
133102      break;
133103    case 'i':
133104      stem(&z, "itici", "ic", m_gt_0);
133105      break;
133106    case 'l':
133107      if( !stem(&z, "laci", "ic", m_gt_0) ){
133108        stem(&z, "luf", "", m_gt_0);
133109      }
133110      break;
133111    case 's':
133112      stem(&z, "ssen", "", m_gt_0);
133113      break;
133114   }
133115 
133116   /* Step 4 */
133117   switch( z[1] ){
133118    case 'a':
133119      if( z[0]=='l' && m_gt_1(z+2) ){
133120        z += 2;
133121      }
133122      break;
133123    case 'c':
133124      if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
133125        z += 4;
133126      }
133127      break;
133128    case 'e':
133129      if( z[0]=='r' && m_gt_1(z+2) ){
133130        z += 2;
133131      }
133132      break;
133133    case 'i':
133134      if( z[0]=='c' && m_gt_1(z+2) ){
133135        z += 2;
133136      }
133137      break;
133138    case 'l':
133139      if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
133140        z += 4;
133141      }
133142      break;
133143    case 'n':
133144      if( z[0]=='t' ){
133145        if( z[2]=='a' ){
133146          if( m_gt_1(z+3) ){
133147            z += 3;
133148          }
133149        }else if( z[2]=='e' ){
133150          if( !stem(&z, "tneme", "", m_gt_1)
133151           && !stem(&z, "tnem", "", m_gt_1)
133152          ){
133153            stem(&z, "tne", "", m_gt_1);
133154          }
133155        }
133156      }
133157      break;
133158    case 'o':
133159      if( z[0]=='u' ){
133160        if( m_gt_1(z+2) ){
133161          z += 2;
133162        }
133163      }else if( z[3]=='s' || z[3]=='t' ){
133164        stem(&z, "noi", "", m_gt_1);
133165      }
133166      break;
133167    case 's':
133168      if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
133169        z += 3;
133170      }
133171      break;
133172    case 't':
133173      if( !stem(&z, "eta", "", m_gt_1) ){
133174        stem(&z, "iti", "", m_gt_1);
133175      }
133176      break;
133177    case 'u':
133178      if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
133179        z += 3;
133180      }
133181      break;
133182    case 'v':
133183    case 'z':
133184      if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
133185        z += 3;
133186      }
133187      break;
133188   }
133189 
133190   /* Step 5a */
133191   if( z[0]=='e' ){
133192     if( m_gt_1(z+1) ){
133193       z++;
133194     }else if( m_eq_1(z+1) && !star_oh(z+1) ){
133195       z++;
133196     }
133197   }
133198 
133199   /* Step 5b */
133200   if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
133201     z++;
133202   }
133203 
133204   /* z[] is now the stemmed word in reverse order.  Flip it back
133205   ** around into forward order and return.
133206   */
133207   *pnOut = i = (int)strlen(z);
133208   zOut[i] = 0;
133209   while( *z ){
133210     zOut[--i] = *(z++);
133211   }
133212 }
133213 
133214 /*
133215 ** Characters that can be part of a token.  We assume any character
133216 ** whose value is greater than 0x80 (any UTF character) can be
133217 ** part of a token.  In other words, delimiters all must have
133218 ** values of 0x7f or lower.
133219 */
133220 static const char porterIdChar[] = {
133221 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
133222     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
133223     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
133224     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
133225     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
133226     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
133227 };
133228 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
133229 
133230 /*
133231 ** Extract the next token from a tokenization cursor.  The cursor must
133232 ** have been opened by a prior call to porterOpen().
133233 */
133234 static int porterNext(
133235   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
133236   const char **pzToken,               /* OUT: *pzToken is the token text */
133237   int *pnBytes,                       /* OUT: Number of bytes in token */
133238   int *piStartOffset,                 /* OUT: Starting offset of token */
133239   int *piEndOffset,                   /* OUT: Ending offset of token */
133240   int *piPosition                     /* OUT: Position integer of token */
133241 ){
133242   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
133243   const char *z = c->zInput;
133244 
133245   while( c->iOffset<c->nInput ){
133246     int iStartOffset, ch;
133247 
133248     /* Scan past delimiter characters */
133249     while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
133250       c->iOffset++;
133251     }
133252 
133253     /* Count non-delimiter characters. */
133254     iStartOffset = c->iOffset;
133255     while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
133256       c->iOffset++;
133257     }
133258 
133259     if( c->iOffset>iStartOffset ){
133260       int n = c->iOffset-iStartOffset;
133261       if( n>c->nAllocated ){
133262         char *pNew;
133263         c->nAllocated = n+20;
133264         pNew = sqlite3_realloc(c->zToken, c->nAllocated);
133265         if( !pNew ) return SQLITE_NOMEM;
133266         c->zToken = pNew;
133267       }
133268       porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
133269       *pzToken = c->zToken;
133270       *piStartOffset = iStartOffset;
133271       *piEndOffset = c->iOffset;
133272       *piPosition = c->iToken++;
133273       return SQLITE_OK;
133274     }
133275   }
133276   return SQLITE_DONE;
133277 }
133278 
133279 /*
133280 ** The set of routines that implement the porter-stemmer tokenizer
133281 */
133282 static const sqlite3_tokenizer_module porterTokenizerModule = {
133283   0,
133284   porterCreate,
133285   porterDestroy,
133286   porterOpen,
133287   porterClose,
133288   porterNext,
133289   0
133290 };
133291 
133292 /*
133293 ** Allocate a new porter tokenizer.  Return a pointer to the new
133294 ** tokenizer in *ppModule
133295 */
133296 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
133297   sqlite3_tokenizer_module const**ppModule
133298 ){
133299   *ppModule = &porterTokenizerModule;
133300 }
133301 
133302 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
133303 
133304 /************** End of fts3_porter.c *****************************************/
133305 /************** Begin file fts3_tokenizer.c **********************************/
133306 /*
133307 ** 2007 June 22
133308 **
133309 ** The author disclaims copyright to this source code.  In place of
133310 ** a legal notice, here is a blessing:
133311 **
133312 **    May you do good and not evil.
133313 **    May you find forgiveness for yourself and forgive others.
133314 **    May you share freely, never taking more than you give.
133315 **
133316 ******************************************************************************
133317 **
133318 ** This is part of an SQLite module implementing full-text search.
133319 ** This particular file implements the generic tokenizer interface.
133320 */
133321 
133322 /*
133323 ** The code in this file is only compiled if:
133324 **
133325 **     * The FTS3 module is being built as an extension
133326 **       (in which case SQLITE_CORE is not defined), or
133327 **
133328 **     * The FTS3 module is being built into the core of
133329 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
133330 */
133331 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133332 
133333 /* #include <assert.h> */
133334 /* #include <string.h> */
133335 
133336 /*
133337 ** Implementation of the SQL scalar function for accessing the underlying
133338 ** hash table. This function may be called as follows:
133339 **
133340 **   SELECT <function-name>(<key-name>);
133341 **   SELECT <function-name>(<key-name>, <pointer>);
133342 **
133343 ** where <function-name> is the name passed as the second argument
133344 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
133345 **
133346 ** If the <pointer> argument is specified, it must be a blob value
133347 ** containing a pointer to be stored as the hash data corresponding
133348 ** to the string <key-name>. If <pointer> is not specified, then
133349 ** the string <key-name> must already exist in the has table. Otherwise,
133350 ** an error is returned.
133351 **
133352 ** Whether or not the <pointer> argument is specified, the value returned
133353 ** is a blob containing the pointer stored as the hash data corresponding
133354 ** to string <key-name> (after the hash-table is updated, if applicable).
133355 */
133356 static void scalarFunc(
133357   sqlite3_context *context,
133358   int argc,
133359   sqlite3_value **argv
133360 ){
133361   Fts3Hash *pHash;
133362   void *pPtr = 0;
133363   const unsigned char *zName;
133364   int nName;
133365 
133366   assert( argc==1 || argc==2 );
133367 
133368   pHash = (Fts3Hash *)sqlite3_user_data(context);
133369 
133370   zName = sqlite3_value_text(argv[0]);
133371   nName = sqlite3_value_bytes(argv[0])+1;
133372 
133373   if( argc==2 ){
133374     void *pOld;
133375     int n = sqlite3_value_bytes(argv[1]);
133376     if( n!=sizeof(pPtr) ){
133377       sqlite3_result_error(context, "argument type mismatch", -1);
133378       return;
133379     }
133380     pPtr = *(void **)sqlite3_value_blob(argv[1]);
133381     pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
133382     if( pOld==pPtr ){
133383       sqlite3_result_error(context, "out of memory", -1);
133384       return;
133385     }
133386   }else{
133387     pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
133388     if( !pPtr ){
133389       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
133390       sqlite3_result_error(context, zErr, -1);
133391       sqlite3_free(zErr);
133392       return;
133393     }
133394   }
133395 
133396   sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
133397 }
133398 
133399 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
133400   static const char isFtsIdChar[] = {
133401       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
133402       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
133403       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
133404       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
133405       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
133406       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
133407       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
133408       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
133409   };
133410   return (c&0x80 || isFtsIdChar[(int)(c)]);
133411 }
133412 
133413 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
133414   const char *z1;
133415   const char *z2 = 0;
133416 
133417   /* Find the start of the next token. */
133418   z1 = zStr;
133419   while( z2==0 ){
133420     char c = *z1;
133421     switch( c ){
133422       case '\0': return 0;        /* No more tokens here */
133423       case '\'':
133424       case '"':
133425       case '`': {
133426         z2 = z1;
133427         while( *++z2 && (*z2!=c || *++z2==c) );
133428         break;
133429       }
133430       case '[':
133431         z2 = &z1[1];
133432         while( *z2 && z2[0]!=']' ) z2++;
133433         if( *z2 ) z2++;
133434         break;
133435 
133436       default:
133437         if( sqlite3Fts3IsIdChar(*z1) ){
133438           z2 = &z1[1];
133439           while( sqlite3Fts3IsIdChar(*z2) ) z2++;
133440         }else{
133441           z1++;
133442         }
133443     }
133444   }
133445 
133446   *pn = (int)(z2-z1);
133447   return z1;
133448 }
133449 
133450 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
133451   Fts3Hash *pHash,                /* Tokenizer hash table */
133452   const char *zArg,               /* Tokenizer name */
133453   sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
133454   char **pzErr                    /* OUT: Set to malloced error message */
133455 ){
133456   int rc;
133457   char *z = (char *)zArg;
133458   int n = 0;
133459   char *zCopy;
133460   char *zEnd;                     /* Pointer to nul-term of zCopy */
133461   sqlite3_tokenizer_module *m;
133462 
133463   zCopy = sqlite3_mprintf("%s", zArg);
133464   if( !zCopy ) return SQLITE_NOMEM;
133465   zEnd = &zCopy[strlen(zCopy)];
133466 
133467   z = (char *)sqlite3Fts3NextToken(zCopy, &n);
133468   z[n] = '\0';
133469   sqlite3Fts3Dequote(z);
133470 
133471   m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
133472   if( !m ){
133473     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
133474     rc = SQLITE_ERROR;
133475   }else{
133476     char const **aArg = 0;
133477     int iArg = 0;
133478     z = &z[n+1];
133479     while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
133480       int nNew = sizeof(char *)*(iArg+1);
133481       char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
133482       if( !aNew ){
133483         sqlite3_free(zCopy);
133484         sqlite3_free((void *)aArg);
133485         return SQLITE_NOMEM;
133486       }
133487       aArg = aNew;
133488       aArg[iArg++] = z;
133489       z[n] = '\0';
133490       sqlite3Fts3Dequote(z);
133491       z = &z[n+1];
133492     }
133493     rc = m->xCreate(iArg, aArg, ppTok);
133494     assert( rc!=SQLITE_OK || *ppTok );
133495     if( rc!=SQLITE_OK ){
133496       *pzErr = sqlite3_mprintf("unknown tokenizer");
133497     }else{
133498       (*ppTok)->pModule = m;
133499     }
133500     sqlite3_free((void *)aArg);
133501   }
133502 
133503   sqlite3_free(zCopy);
133504   return rc;
133505 }
133506 
133507 
133508 #ifdef SQLITE_TEST
133509 
133510 #include <tcl.h>
133511 /* #include <string.h> */
133512 
133513 /*
133514 ** Implementation of a special SQL scalar function for testing tokenizers
133515 ** designed to be used in concert with the Tcl testing framework. This
133516 ** function must be called with two or more arguments:
133517 **
133518 **   SELECT <function-name>(<key-name>, ..., <input-string>);
133519 **
133520 ** where <function-name> is the name passed as the second argument
133521 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
133522 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
133523 **
133524 ** The return value is a string that may be interpreted as a Tcl
133525 ** list. For each token in the <input-string>, three elements are
133526 ** added to the returned list. The first is the token position, the
133527 ** second is the token text (folded, stemmed, etc.) and the third is the
133528 ** substring of <input-string> associated with the token. For example,
133529 ** using the built-in "simple" tokenizer:
133530 **
133531 **   SELECT fts_tokenizer_test('simple', 'I don't see how');
133532 **
133533 ** will return the string:
133534 **
133535 **   "{0 i I 1 dont don't 2 see see 3 how how}"
133536 **
133537 */
133538 static void testFunc(
133539   sqlite3_context *context,
133540   int argc,
133541   sqlite3_value **argv
133542 ){
133543   Fts3Hash *pHash;
133544   sqlite3_tokenizer_module *p;
133545   sqlite3_tokenizer *pTokenizer = 0;
133546   sqlite3_tokenizer_cursor *pCsr = 0;
133547 
133548   const char *zErr = 0;
133549 
133550   const char *zName;
133551   int nName;
133552   const char *zInput;
133553   int nInput;
133554 
133555   const char *azArg[64];
133556 
133557   const char *zToken;
133558   int nToken = 0;
133559   int iStart = 0;
133560   int iEnd = 0;
133561   int iPos = 0;
133562   int i;
133563 
133564   Tcl_Obj *pRet;
133565 
133566   if( argc<2 ){
133567     sqlite3_result_error(context, "insufficient arguments", -1);
133568     return;
133569   }
133570 
133571   nName = sqlite3_value_bytes(argv[0]);
133572   zName = (const char *)sqlite3_value_text(argv[0]);
133573   nInput = sqlite3_value_bytes(argv[argc-1]);
133574   zInput = (const char *)sqlite3_value_text(argv[argc-1]);
133575 
133576   pHash = (Fts3Hash *)sqlite3_user_data(context);
133577   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
133578 
133579   if( !p ){
133580     char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
133581     sqlite3_result_error(context, zErr, -1);
133582     sqlite3_free(zErr);
133583     return;
133584   }
133585 
133586   pRet = Tcl_NewObj();
133587   Tcl_IncrRefCount(pRet);
133588 
133589   for(i=1; i<argc-1; i++){
133590     azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
133591   }
133592 
133593   if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
133594     zErr = "error in xCreate()";
133595     goto finish;
133596   }
133597   pTokenizer->pModule = p;
133598   if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
133599     zErr = "error in xOpen()";
133600     goto finish;
133601   }
133602 
133603   while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
133604     Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
133605     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
133606     zToken = &zInput[iStart];
133607     nToken = iEnd-iStart;
133608     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
133609   }
133610 
133611   if( SQLITE_OK!=p->xClose(pCsr) ){
133612     zErr = "error in xClose()";
133613     goto finish;
133614   }
133615   if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
133616     zErr = "error in xDestroy()";
133617     goto finish;
133618   }
133619 
133620 finish:
133621   if( zErr ){
133622     sqlite3_result_error(context, zErr, -1);
133623   }else{
133624     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
133625   }
133626   Tcl_DecrRefCount(pRet);
133627 }
133628 
133629 static
133630 int registerTokenizer(
133631   sqlite3 *db,
133632   char *zName,
133633   const sqlite3_tokenizer_module *p
133634 ){
133635   int rc;
133636   sqlite3_stmt *pStmt;
133637   const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
133638 
133639   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
133640   if( rc!=SQLITE_OK ){
133641     return rc;
133642   }
133643 
133644   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
133645   sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
133646   sqlite3_step(pStmt);
133647 
133648   return sqlite3_finalize(pStmt);
133649 }
133650 
133651 static
133652 int queryTokenizer(
133653   sqlite3 *db,
133654   char *zName,
133655   const sqlite3_tokenizer_module **pp
133656 ){
133657   int rc;
133658   sqlite3_stmt *pStmt;
133659   const char zSql[] = "SELECT fts3_tokenizer(?)";
133660 
133661   *pp = 0;
133662   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
133663   if( rc!=SQLITE_OK ){
133664     return rc;
133665   }
133666 
133667   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
133668   if( SQLITE_ROW==sqlite3_step(pStmt) ){
133669     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
133670       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
133671     }
133672   }
133673 
133674   return sqlite3_finalize(pStmt);
133675 }
133676 
133677 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
133678 
133679 /*
133680 ** Implementation of the scalar function fts3_tokenizer_internal_test().
133681 ** This function is used for testing only, it is not included in the
133682 ** build unless SQLITE_TEST is defined.
133683 **
133684 ** The purpose of this is to test that the fts3_tokenizer() function
133685 ** can be used as designed by the C-code in the queryTokenizer and
133686 ** registerTokenizer() functions above. These two functions are repeated
133687 ** in the README.tokenizer file as an example, so it is important to
133688 ** test them.
133689 **
133690 ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
133691 ** function with no arguments. An assert() will fail if a problem is
133692 ** detected. i.e.:
133693 **
133694 **     SELECT fts3_tokenizer_internal_test();
133695 **
133696 */
133697 static void intTestFunc(
133698   sqlite3_context *context,
133699   int argc,
133700   sqlite3_value **argv
133701 ){
133702   int rc;
133703   const sqlite3_tokenizer_module *p1;
133704   const sqlite3_tokenizer_module *p2;
133705   sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
133706 
133707   UNUSED_PARAMETER(argc);
133708   UNUSED_PARAMETER(argv);
133709 
133710   /* Test the query function */
133711   sqlite3Fts3SimpleTokenizerModule(&p1);
133712   rc = queryTokenizer(db, "simple", &p2);
133713   assert( rc==SQLITE_OK );
133714   assert( p1==p2 );
133715   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
133716   assert( rc==SQLITE_ERROR );
133717   assert( p2==0 );
133718   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
133719 
133720   /* Test the storage function */
133721   rc = registerTokenizer(db, "nosuchtokenizer", p1);
133722   assert( rc==SQLITE_OK );
133723   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
133724   assert( rc==SQLITE_OK );
133725   assert( p2==p1 );
133726 
133727   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
133728 }
133729 
133730 #endif
133731 
133732 /*
133733 ** Set up SQL objects in database db used to access the contents of
133734 ** the hash table pointed to by argument pHash. The hash table must
133735 ** been initialized to use string keys, and to take a private copy
133736 ** of the key when a value is inserted. i.e. by a call similar to:
133737 **
133738 **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
133739 **
133740 ** This function adds a scalar function (see header comment above
133741 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
133742 ** defined at compilation time, a temporary virtual table (see header
133743 ** comment above struct HashTableVtab) to the database schema. Both
133744 ** provide read/write access to the contents of *pHash.
133745 **
133746 ** The third argument to this function, zName, is used as the name
133747 ** of both the scalar and, if created, the virtual table.
133748 */
133749 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
133750   sqlite3 *db,
133751   Fts3Hash *pHash,
133752   const char *zName
133753 ){
133754   int rc = SQLITE_OK;
133755   void *p = (void *)pHash;
133756   const int any = SQLITE_ANY;
133757 
133758 #ifdef SQLITE_TEST
133759   char *zTest = 0;
133760   char *zTest2 = 0;
133761   void *pdb = (void *)db;
133762   zTest = sqlite3_mprintf("%s_test", zName);
133763   zTest2 = sqlite3_mprintf("%s_internal_test", zName);
133764   if( !zTest || !zTest2 ){
133765     rc = SQLITE_NOMEM;
133766   }
133767 #endif
133768 
133769   if( SQLITE_OK==rc ){
133770     rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
133771   }
133772   if( SQLITE_OK==rc ){
133773     rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
133774   }
133775 #ifdef SQLITE_TEST
133776   if( SQLITE_OK==rc ){
133777     rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
133778   }
133779   if( SQLITE_OK==rc ){
133780     rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
133781   }
133782 #endif
133783 
133784 #ifdef SQLITE_TEST
133785   sqlite3_free(zTest);
133786   sqlite3_free(zTest2);
133787 #endif
133788 
133789   return rc;
133790 }
133791 
133792 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
133793 
133794 /************** End of fts3_tokenizer.c **************************************/
133795 /************** Begin file fts3_tokenizer1.c *********************************/
133796 /*
133797 ** 2006 Oct 10
133798 **
133799 ** The author disclaims copyright to this source code.  In place of
133800 ** a legal notice, here is a blessing:
133801 **
133802 **    May you do good and not evil.
133803 **    May you find forgiveness for yourself and forgive others.
133804 **    May you share freely, never taking more than you give.
133805 **
133806 ******************************************************************************
133807 **
133808 ** Implementation of the "simple" full-text-search tokenizer.
133809 */
133810 
133811 /*
133812 ** The code in this file is only compiled if:
133813 **
133814 **     * The FTS3 module is being built as an extension
133815 **       (in which case SQLITE_CORE is not defined), or
133816 **
133817 **     * The FTS3 module is being built into the core of
133818 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
133819 */
133820 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133821 
133822 /* #include <assert.h> */
133823 /* #include <stdlib.h> */
133824 /* #include <stdio.h> */
133825 /* #include <string.h> */
133826 
133827 
133828 typedef struct simple_tokenizer {
133829   sqlite3_tokenizer base;
133830   char delim[128];             /* flag ASCII delimiters */
133831 } simple_tokenizer;
133832 
133833 typedef struct simple_tokenizer_cursor {
133834   sqlite3_tokenizer_cursor base;
133835   const char *pInput;          /* input we are tokenizing */
133836   int nBytes;                  /* size of the input */
133837   int iOffset;                 /* current position in pInput */
133838   int iToken;                  /* index of next token to be returned */
133839   char *pToken;                /* storage for current token */
133840   int nTokenAllocated;         /* space allocated to zToken buffer */
133841 } simple_tokenizer_cursor;
133842 
133843 
133844 static int simpleDelim(simple_tokenizer *t, unsigned char c){
133845   return c<0x80 && t->delim[c];
133846 }
133847 static int fts3_isalnum(int x){
133848   return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
133849 }
133850 
133851 /*
133852 ** Create a new tokenizer instance.
133853 */
133854 static int simpleCreate(
133855   int argc, const char * const *argv,
133856   sqlite3_tokenizer **ppTokenizer
133857 ){
133858   simple_tokenizer *t;
133859 
133860   t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
133861   if( t==NULL ) return SQLITE_NOMEM;
133862   memset(t, 0, sizeof(*t));
133863 
133864   /* TODO(shess) Delimiters need to remain the same from run to run,
133865   ** else we need to reindex.  One solution would be a meta-table to
133866   ** track such information in the database, then we'd only want this
133867   ** information on the initial create.
133868   */
133869   if( argc>1 ){
133870     int i, n = (int)strlen(argv[1]);
133871     for(i=0; i<n; i++){
133872       unsigned char ch = argv[1][i];
133873       /* We explicitly don't support UTF-8 delimiters for now. */
133874       if( ch>=0x80 ){
133875         sqlite3_free(t);
133876         return SQLITE_ERROR;
133877       }
133878       t->delim[ch] = 1;
133879     }
133880   } else {
133881     /* Mark non-alphanumeric ASCII characters as delimiters */
133882     int i;
133883     for(i=1; i<0x80; i++){
133884       t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
133885     }
133886   }
133887 
133888   *ppTokenizer = &t->base;
133889   return SQLITE_OK;
133890 }
133891 
133892 /*
133893 ** Destroy a tokenizer
133894 */
133895 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
133896   sqlite3_free(pTokenizer);
133897   return SQLITE_OK;
133898 }
133899 
133900 /*
133901 ** Prepare to begin tokenizing a particular string.  The input
133902 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
133903 ** used to incrementally tokenize this string is returned in
133904 ** *ppCursor.
133905 */
133906 static int simpleOpen(
133907   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
133908   const char *pInput, int nBytes,        /* String to be tokenized */
133909   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
133910 ){
133911   simple_tokenizer_cursor *c;
133912 
133913   UNUSED_PARAMETER(pTokenizer);
133914 
133915   c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
133916   if( c==NULL ) return SQLITE_NOMEM;
133917 
133918   c->pInput = pInput;
133919   if( pInput==0 ){
133920     c->nBytes = 0;
133921   }else if( nBytes<0 ){
133922     c->nBytes = (int)strlen(pInput);
133923   }else{
133924     c->nBytes = nBytes;
133925   }
133926   c->iOffset = 0;                 /* start tokenizing at the beginning */
133927   c->iToken = 0;
133928   c->pToken = NULL;               /* no space allocated, yet. */
133929   c->nTokenAllocated = 0;
133930 
133931   *ppCursor = &c->base;
133932   return SQLITE_OK;
133933 }
133934 
133935 /*
133936 ** Close a tokenization cursor previously opened by a call to
133937 ** simpleOpen() above.
133938 */
133939 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
133940   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
133941   sqlite3_free(c->pToken);
133942   sqlite3_free(c);
133943   return SQLITE_OK;
133944 }
133945 
133946 /*
133947 ** Extract the next token from a tokenization cursor.  The cursor must
133948 ** have been opened by a prior call to simpleOpen().
133949 */
133950 static int simpleNext(
133951   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
133952   const char **ppToken,               /* OUT: *ppToken is the token text */
133953   int *pnBytes,                       /* OUT: Number of bytes in token */
133954   int *piStartOffset,                 /* OUT: Starting offset of token */
133955   int *piEndOffset,                   /* OUT: Ending offset of token */
133956   int *piPosition                     /* OUT: Position integer of token */
133957 ){
133958   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
133959   simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
133960   unsigned char *p = (unsigned char *)c->pInput;
133961 
133962   while( c->iOffset<c->nBytes ){
133963     int iStartOffset;
133964 
133965     /* Scan past delimiter characters */
133966     while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
133967       c->iOffset++;
133968     }
133969 
133970     /* Count non-delimiter characters. */
133971     iStartOffset = c->iOffset;
133972     while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
133973       c->iOffset++;
133974     }
133975 
133976     if( c->iOffset>iStartOffset ){
133977       int i, n = c->iOffset-iStartOffset;
133978       if( n>c->nTokenAllocated ){
133979         char *pNew;
133980         c->nTokenAllocated = n+20;
133981         pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
133982         if( !pNew ) return SQLITE_NOMEM;
133983         c->pToken = pNew;
133984       }
133985       for(i=0; i<n; i++){
133986         /* TODO(shess) This needs expansion to handle UTF-8
133987         ** case-insensitivity.
133988         */
133989         unsigned char ch = p[iStartOffset+i];
133990         c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
133991       }
133992       *ppToken = c->pToken;
133993       *pnBytes = n;
133994       *piStartOffset = iStartOffset;
133995       *piEndOffset = c->iOffset;
133996       *piPosition = c->iToken++;
133997 
133998       return SQLITE_OK;
133999     }
134000   }
134001   return SQLITE_DONE;
134002 }
134003 
134004 /*
134005 ** The set of routines that implement the simple tokenizer
134006 */
134007 static const sqlite3_tokenizer_module simpleTokenizerModule = {
134008   0,
134009   simpleCreate,
134010   simpleDestroy,
134011   simpleOpen,
134012   simpleClose,
134013   simpleNext,
134014   0,
134015 };
134016 
134017 /*
134018 ** Allocate a new simple tokenizer.  Return a pointer to the new
134019 ** tokenizer in *ppModule
134020 */
134021 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
134022   sqlite3_tokenizer_module const**ppModule
134023 ){
134024   *ppModule = &simpleTokenizerModule;
134025 }
134026 
134027 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
134028 
134029 /************** End of fts3_tokenizer1.c *************************************/
134030 /************** Begin file fts3_tokenize_vtab.c ******************************/
134031 /*
134032 ** 2013 Apr 22
134033 **
134034 ** The author disclaims copyright to this source code.  In place of
134035 ** a legal notice, here is a blessing:
134036 **
134037 **    May you do good and not evil.
134038 **    May you find forgiveness for yourself and forgive others.
134039 **    May you share freely, never taking more than you give.
134040 **
134041 ******************************************************************************
134042 **
134043 ** This file contains code for the "fts3tokenize" virtual table module.
134044 ** An fts3tokenize virtual table is created as follows:
134045 **
134046 **   CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
134047 **       <tokenizer-name>, <arg-1>, ...
134048 **   );
134049 **
134050 ** The table created has the following schema:
134051 **
134052 **   CREATE TABLE <tbl>(input, token, start, end, position)
134053 **
134054 ** When queried, the query must include a WHERE clause of type:
134055 **
134056 **   input = <string>
134057 **
134058 ** The virtual table module tokenizes this <string>, using the FTS3
134059 ** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
134060 ** statement and returns one row for each token in the result. With
134061 ** fields set as follows:
134062 **
134063 **   input:   Always set to a copy of <string>
134064 **   token:   A token from the input.
134065 **   start:   Byte offset of the token within the input <string>.
134066 **   end:     Byte offset of the byte immediately following the end of the
134067 **            token within the input string.
134068 **   pos:     Token offset of token within input.
134069 **
134070 */
134071 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
134072 
134073 /* #include <string.h> */
134074 /* #include <assert.h> */
134075 
134076 typedef struct Fts3tokTable Fts3tokTable;
134077 typedef struct Fts3tokCursor Fts3tokCursor;
134078 
134079 /*
134080 ** Virtual table structure.
134081 */
134082 struct Fts3tokTable {
134083   sqlite3_vtab base;              /* Base class used by SQLite core */
134084   const sqlite3_tokenizer_module *pMod;
134085   sqlite3_tokenizer *pTok;
134086 };
134087 
134088 /*
134089 ** Virtual table cursor structure.
134090 */
134091 struct Fts3tokCursor {
134092   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
134093   char *zInput;                   /* Input string */
134094   sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
134095   int iRowid;                     /* Current 'rowid' value */
134096   const char *zToken;             /* Current 'token' value */
134097   int nToken;                     /* Size of zToken in bytes */
134098   int iStart;                     /* Current 'start' value */
134099   int iEnd;                       /* Current 'end' value */
134100   int iPos;                       /* Current 'pos' value */
134101 };
134102 
134103 /*
134104 ** Query FTS for the tokenizer implementation named zName.
134105 */
134106 static int fts3tokQueryTokenizer(
134107   Fts3Hash *pHash,
134108   const char *zName,
134109   const sqlite3_tokenizer_module **pp,
134110   char **pzErr
134111 ){
134112   sqlite3_tokenizer_module *p;
134113   int nName = (int)strlen(zName);
134114 
134115   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
134116   if( !p ){
134117     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
134118     return SQLITE_ERROR;
134119   }
134120 
134121   *pp = p;
134122   return SQLITE_OK;
134123 }
134124 
134125 /*
134126 ** The second argument, argv[], is an array of pointers to nul-terminated
134127 ** strings. This function makes a copy of the array and strings into a
134128 ** single block of memory. It then dequotes any of the strings that appear
134129 ** to be quoted.
134130 **
134131 ** If successful, output parameter *pazDequote is set to point at the
134132 ** array of dequoted strings and SQLITE_OK is returned. The caller is
134133 ** responsible for eventually calling sqlite3_free() to free the array
134134 ** in this case. Or, if an error occurs, an SQLite error code is returned.
134135 ** The final value of *pazDequote is undefined in this case.
134136 */
134137 static int fts3tokDequoteArray(
134138   int argc,                       /* Number of elements in argv[] */
134139   const char * const *argv,       /* Input array */
134140   char ***pazDequote              /* Output array */
134141 ){
134142   int rc = SQLITE_OK;             /* Return code */
134143   if( argc==0 ){
134144     *pazDequote = 0;
134145   }else{
134146     int i;
134147     int nByte = 0;
134148     char **azDequote;
134149 
134150     for(i=0; i<argc; i++){
134151       nByte += (int)(strlen(argv[i]) + 1);
134152     }
134153 
134154     *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
134155     if( azDequote==0 ){
134156       rc = SQLITE_NOMEM;
134157     }else{
134158       char *pSpace = (char *)&azDequote[argc];
134159       for(i=0; i<argc; i++){
134160         int n = (int)strlen(argv[i]);
134161         azDequote[i] = pSpace;
134162         memcpy(pSpace, argv[i], n+1);
134163         sqlite3Fts3Dequote(pSpace);
134164         pSpace += (n+1);
134165       }
134166     }
134167   }
134168 
134169   return rc;
134170 }
134171 
134172 /*
134173 ** Schema of the tokenizer table.
134174 */
134175 #define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
134176 
134177 /*
134178 ** This function does all the work for both the xConnect and xCreate methods.
134179 ** These tables have no persistent representation of their own, so xConnect
134180 ** and xCreate are identical operations.
134181 **
134182 **   argv[0]: module name
134183 **   argv[1]: database name
134184 **   argv[2]: table name
134185 **   argv[3]: first argument (tokenizer name)
134186 */
134187 static int fts3tokConnectMethod(
134188   sqlite3 *db,                    /* Database connection */
134189   void *pHash,                    /* Hash table of tokenizers */
134190   int argc,                       /* Number of elements in argv array */
134191   const char * const *argv,       /* xCreate/xConnect argument array */
134192   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
134193   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
134194 ){
134195   Fts3tokTable *pTab;
134196   const sqlite3_tokenizer_module *pMod = 0;
134197   sqlite3_tokenizer *pTok = 0;
134198   int rc;
134199   char **azDequote = 0;
134200   int nDequote;
134201 
134202   rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
134203   if( rc!=SQLITE_OK ) return rc;
134204 
134205   nDequote = argc-3;
134206   rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
134207 
134208   if( rc==SQLITE_OK ){
134209     const char *zModule;
134210     if( nDequote<1 ){
134211       zModule = "simple";
134212     }else{
134213       zModule = azDequote[0];
134214     }
134215     rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
134216   }
134217 
134218   assert( (rc==SQLITE_OK)==(pMod!=0) );
134219   if( rc==SQLITE_OK ){
134220     const char * const *azArg = (const char * const *)&azDequote[1];
134221     rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
134222   }
134223 
134224   if( rc==SQLITE_OK ){
134225     pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
134226     if( pTab==0 ){
134227       rc = SQLITE_NOMEM;
134228     }
134229   }
134230 
134231   if( rc==SQLITE_OK ){
134232     memset(pTab, 0, sizeof(Fts3tokTable));
134233     pTab->pMod = pMod;
134234     pTab->pTok = pTok;
134235     *ppVtab = &pTab->base;
134236   }else{
134237     if( pTok ){
134238       pMod->xDestroy(pTok);
134239     }
134240   }
134241 
134242   sqlite3_free(azDequote);
134243   return rc;
134244 }
134245 
134246 /*
134247 ** This function does the work for both the xDisconnect and xDestroy methods.
134248 ** These tables have no persistent representation of their own, so xDisconnect
134249 ** and xDestroy are identical operations.
134250 */
134251 static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
134252   Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
134253 
134254   pTab->pMod->xDestroy(pTab->pTok);
134255   sqlite3_free(pTab);
134256   return SQLITE_OK;
134257 }
134258 
134259 /*
134260 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
134261 */
134262 static int fts3tokBestIndexMethod(
134263   sqlite3_vtab *pVTab,
134264   sqlite3_index_info *pInfo
134265 ){
134266   int i;
134267   UNUSED_PARAMETER(pVTab);
134268 
134269   for(i=0; i<pInfo->nConstraint; i++){
134270     if( pInfo->aConstraint[i].usable
134271      && pInfo->aConstraint[i].iColumn==0
134272      && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
134273     ){
134274       pInfo->idxNum = 1;
134275       pInfo->aConstraintUsage[i].argvIndex = 1;
134276       pInfo->aConstraintUsage[i].omit = 1;
134277       pInfo->estimatedCost = 1;
134278       return SQLITE_OK;
134279     }
134280   }
134281 
134282   pInfo->idxNum = 0;
134283   assert( pInfo->estimatedCost>1000000.0 );
134284 
134285   return SQLITE_OK;
134286 }
134287 
134288 /*
134289 ** xOpen - Open a cursor.
134290 */
134291 static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
134292   Fts3tokCursor *pCsr;
134293   UNUSED_PARAMETER(pVTab);
134294 
134295   pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
134296   if( pCsr==0 ){
134297     return SQLITE_NOMEM;
134298   }
134299   memset(pCsr, 0, sizeof(Fts3tokCursor));
134300 
134301   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
134302   return SQLITE_OK;
134303 }
134304 
134305 /*
134306 ** Reset the tokenizer cursor passed as the only argument. As if it had
134307 ** just been returned by fts3tokOpenMethod().
134308 */
134309 static void fts3tokResetCursor(Fts3tokCursor *pCsr){
134310   if( pCsr->pCsr ){
134311     Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
134312     pTab->pMod->xClose(pCsr->pCsr);
134313     pCsr->pCsr = 0;
134314   }
134315   sqlite3_free(pCsr->zInput);
134316   pCsr->zInput = 0;
134317   pCsr->zToken = 0;
134318   pCsr->nToken = 0;
134319   pCsr->iStart = 0;
134320   pCsr->iEnd = 0;
134321   pCsr->iPos = 0;
134322   pCsr->iRowid = 0;
134323 }
134324 
134325 /*
134326 ** xClose - Close a cursor.
134327 */
134328 static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
134329   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
134330 
134331   fts3tokResetCursor(pCsr);
134332   sqlite3_free(pCsr);
134333   return SQLITE_OK;
134334 }
134335 
134336 /*
134337 ** xNext - Advance the cursor to the next row, if any.
134338 */
134339 static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
134340   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
134341   Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
134342   int rc;                         /* Return code */
134343 
134344   pCsr->iRowid++;
134345   rc = pTab->pMod->xNext(pCsr->pCsr,
134346       &pCsr->zToken, &pCsr->nToken,
134347       &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
134348   );
134349 
134350   if( rc!=SQLITE_OK ){
134351     fts3tokResetCursor(pCsr);
134352     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
134353   }
134354 
134355   return rc;
134356 }
134357 
134358 /*
134359 ** xFilter - Initialize a cursor to point at the start of its data.
134360 */
134361 static int fts3tokFilterMethod(
134362   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
134363   int idxNum,                     /* Strategy index */
134364   const char *idxStr,             /* Unused */
134365   int nVal,                       /* Number of elements in apVal */
134366   sqlite3_value **apVal           /* Arguments for the indexing scheme */
134367 ){
134368   int rc = SQLITE_ERROR;
134369   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
134370   Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
134371   UNUSED_PARAMETER(idxStr);
134372   UNUSED_PARAMETER(nVal);
134373 
134374   fts3tokResetCursor(pCsr);
134375   if( idxNum==1 ){
134376     const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
134377     int nByte = sqlite3_value_bytes(apVal[0]);
134378     pCsr->zInput = sqlite3_malloc(nByte+1);
134379     if( pCsr->zInput==0 ){
134380       rc = SQLITE_NOMEM;
134381     }else{
134382       memcpy(pCsr->zInput, zByte, nByte);
134383       pCsr->zInput[nByte] = 0;
134384       rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
134385       if( rc==SQLITE_OK ){
134386         pCsr->pCsr->pTokenizer = pTab->pTok;
134387       }
134388     }
134389   }
134390 
134391   if( rc!=SQLITE_OK ) return rc;
134392   return fts3tokNextMethod(pCursor);
134393 }
134394 
134395 /*
134396 ** xEof - Return true if the cursor is at EOF, or false otherwise.
134397 */
134398 static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
134399   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
134400   return (pCsr->zToken==0);
134401 }
134402 
134403 /*
134404 ** xColumn - Return a column value.
134405 */
134406 static int fts3tokColumnMethod(
134407   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
134408   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
134409   int iCol                        /* Index of column to read value from */
134410 ){
134411   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
134412 
134413   /* CREATE TABLE x(input, token, start, end, position) */
134414   switch( iCol ){
134415     case 0:
134416       sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
134417       break;
134418     case 1:
134419       sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
134420       break;
134421     case 2:
134422       sqlite3_result_int(pCtx, pCsr->iStart);
134423       break;
134424     case 3:
134425       sqlite3_result_int(pCtx, pCsr->iEnd);
134426       break;
134427     default:
134428       assert( iCol==4 );
134429       sqlite3_result_int(pCtx, pCsr->iPos);
134430       break;
134431   }
134432   return SQLITE_OK;
134433 }
134434 
134435 /*
134436 ** xRowid - Return the current rowid for the cursor.
134437 */
134438 static int fts3tokRowidMethod(
134439   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
134440   sqlite_int64 *pRowid            /* OUT: Rowid value */
134441 ){
134442   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
134443   *pRowid = (sqlite3_int64)pCsr->iRowid;
134444   return SQLITE_OK;
134445 }
134446 
134447 /*
134448 ** Register the fts3tok module with database connection db. Return SQLITE_OK
134449 ** if successful or an error code if sqlite3_create_module() fails.
134450 */
134451 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
134452   static const sqlite3_module fts3tok_module = {
134453      0,                           /* iVersion      */
134454      fts3tokConnectMethod,        /* xCreate       */
134455      fts3tokConnectMethod,        /* xConnect      */
134456      fts3tokBestIndexMethod,      /* xBestIndex    */
134457      fts3tokDisconnectMethod,     /* xDisconnect   */
134458      fts3tokDisconnectMethod,     /* xDestroy      */
134459      fts3tokOpenMethod,           /* xOpen         */
134460      fts3tokCloseMethod,          /* xClose        */
134461      fts3tokFilterMethod,         /* xFilter       */
134462      fts3tokNextMethod,           /* xNext         */
134463      fts3tokEofMethod,            /* xEof          */
134464      fts3tokColumnMethod,         /* xColumn       */
134465      fts3tokRowidMethod,          /* xRowid        */
134466      0,                           /* xUpdate       */
134467      0,                           /* xBegin        */
134468      0,                           /* xSync         */
134469      0,                           /* xCommit       */
134470      0,                           /* xRollback     */
134471      0,                           /* xFindFunction */
134472      0,                           /* xRename       */
134473      0,                           /* xSavepoint    */
134474      0,                           /* xRelease      */
134475      0                            /* xRollbackTo   */
134476   };
134477   int rc;                         /* Return code */
134478 
134479   rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
134480   return rc;
134481 }
134482 
134483 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
134484 
134485 /************** End of fts3_tokenize_vtab.c **********************************/
134486 /************** Begin file fts3_write.c **************************************/
134487 /*
134488 ** 2009 Oct 23
134489 **
134490 ** The author disclaims copyright to this source code.  In place of
134491 ** a legal notice, here is a blessing:
134492 **
134493 **    May you do good and not evil.
134494 **    May you find forgiveness for yourself and forgive others.
134495 **    May you share freely, never taking more than you give.
134496 **
134497 ******************************************************************************
134498 **
134499 ** This file is part of the SQLite FTS3 extension module. Specifically,
134500 ** this file contains code to insert, update and delete rows from FTS3
134501 ** tables. It also contains code to merge FTS3 b-tree segments. Some
134502 ** of the sub-routines used to merge segments are also used by the query
134503 ** code in fts3.c.
134504 */
134505 
134506 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
134507 
134508 /* #include <string.h> */
134509 /* #include <assert.h> */
134510 /* #include <stdlib.h> */
134511 
134512 
134513 #define FTS_MAX_APPENDABLE_HEIGHT 16
134514 
134515 /*
134516 ** When full-text index nodes are loaded from disk, the buffer that they
134517 ** are loaded into has the following number of bytes of padding at the end
134518 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
134519 ** of 920 bytes is allocated for it.
134520 **
134521 ** This means that if we have a pointer into a buffer containing node data,
134522 ** it is always safe to read up to two varints from it without risking an
134523 ** overread, even if the node data is corrupted.
134524 */
134525 #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
134526 
134527 /*
134528 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
134529 ** memory incrementally instead of all at once. This can be a big performance
134530 ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
134531 ** method before retrieving all query results (as may happen, for example,
134532 ** if a query has a LIMIT clause).
134533 **
134534 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
134535 ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
134536 ** The code is written so that the hard lower-limit for each of these values
134537 ** is 1. Clearly such small values would be inefficient, but can be useful
134538 ** for testing purposes.
134539 **
134540 ** If this module is built with SQLITE_TEST defined, these constants may
134541 ** be overridden at runtime for testing purposes. File fts3_test.c contains
134542 ** a Tcl interface to read and write the values.
134543 */
134544 #ifdef SQLITE_TEST
134545 int test_fts3_node_chunksize = (4*1024);
134546 int test_fts3_node_chunk_threshold = (4*1024)*4;
134547 # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
134548 # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
134549 #else
134550 # define FTS3_NODE_CHUNKSIZE (4*1024)
134551 # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
134552 #endif
134553 
134554 /*
134555 ** The two values that may be meaningfully bound to the :1 parameter in
134556 ** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
134557 */
134558 #define FTS_STAT_DOCTOTAL      0
134559 #define FTS_STAT_INCRMERGEHINT 1
134560 #define FTS_STAT_AUTOINCRMERGE 2
134561 
134562 /*
134563 ** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
134564 ** and incremental merge operation that takes place. This is used for
134565 ** debugging FTS only, it should not usually be turned on in production
134566 ** systems.
134567 */
134568 #ifdef FTS3_LOG_MERGES
134569 static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
134570   sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
134571 }
134572 #else
134573 #define fts3LogMerge(x, y)
134574 #endif
134575 
134576 
134577 typedef struct PendingList PendingList;
134578 typedef struct SegmentNode SegmentNode;
134579 typedef struct SegmentWriter SegmentWriter;
134580 
134581 /*
134582 ** An instance of the following data structure is used to build doclists
134583 ** incrementally. See function fts3PendingListAppend() for details.
134584 */
134585 struct PendingList {
134586   int nData;
134587   char *aData;
134588   int nSpace;
134589   sqlite3_int64 iLastDocid;
134590   sqlite3_int64 iLastCol;
134591   sqlite3_int64 iLastPos;
134592 };
134593 
134594 
134595 /*
134596 ** Each cursor has a (possibly empty) linked list of the following objects.
134597 */
134598 struct Fts3DeferredToken {
134599   Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
134600   int iCol;                       /* Column token must occur in */
134601   Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
134602   PendingList *pList;             /* Doclist is assembled here */
134603 };
134604 
134605 /*
134606 ** An instance of this structure is used to iterate through the terms on
134607 ** a contiguous set of segment b-tree leaf nodes. Although the details of
134608 ** this structure are only manipulated by code in this file, opaque handles
134609 ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
134610 ** terms when querying the full-text index. See functions:
134611 **
134612 **   sqlite3Fts3SegReaderNew()
134613 **   sqlite3Fts3SegReaderFree()
134614 **   sqlite3Fts3SegReaderIterate()
134615 **
134616 ** Methods used to manipulate Fts3SegReader structures:
134617 **
134618 **   fts3SegReaderNext()
134619 **   fts3SegReaderFirstDocid()
134620 **   fts3SegReaderNextDocid()
134621 */
134622 struct Fts3SegReader {
134623   int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
134624   u8 bLookup;                     /* True for a lookup only */
134625   u8 rootOnly;                    /* True for a root-only reader */
134626 
134627   sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
134628   sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
134629   sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
134630   sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
134631 
134632   char *aNode;                    /* Pointer to node data (or NULL) */
134633   int nNode;                      /* Size of buffer at aNode (or 0) */
134634   int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
134635   sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
134636 
134637   Fts3HashElem **ppNextElem;
134638 
134639   /* Variables set by fts3SegReaderNext(). These may be read directly
134640   ** by the caller. They are valid from the time SegmentReaderNew() returns
134641   ** until SegmentReaderNext() returns something other than SQLITE_OK
134642   ** (i.e. SQLITE_DONE).
134643   */
134644   int nTerm;                      /* Number of bytes in current term */
134645   char *zTerm;                    /* Pointer to current term */
134646   int nTermAlloc;                 /* Allocated size of zTerm buffer */
134647   char *aDoclist;                 /* Pointer to doclist of current entry */
134648   int nDoclist;                   /* Size of doclist in current entry */
134649 
134650   /* The following variables are used by fts3SegReaderNextDocid() to iterate
134651   ** through the current doclist (aDoclist/nDoclist).
134652   */
134653   char *pOffsetList;
134654   int nOffsetList;                /* For descending pending seg-readers only */
134655   sqlite3_int64 iDocid;
134656 };
134657 
134658 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
134659 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
134660 
134661 /*
134662 ** An instance of this structure is used to create a segment b-tree in the
134663 ** database. The internal details of this type are only accessed by the
134664 ** following functions:
134665 **
134666 **   fts3SegWriterAdd()
134667 **   fts3SegWriterFlush()
134668 **   fts3SegWriterFree()
134669 */
134670 struct SegmentWriter {
134671   SegmentNode *pTree;             /* Pointer to interior tree structure */
134672   sqlite3_int64 iFirst;           /* First slot in %_segments written */
134673   sqlite3_int64 iFree;            /* Next free slot in %_segments */
134674   char *zTerm;                    /* Pointer to previous term buffer */
134675   int nTerm;                      /* Number of bytes in zTerm */
134676   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
134677   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
134678   int nSize;                      /* Size of allocation at aData */
134679   int nData;                      /* Bytes of data in aData */
134680   char *aData;                    /* Pointer to block from malloc() */
134681 };
134682 
134683 /*
134684 ** Type SegmentNode is used by the following three functions to create
134685 ** the interior part of the segment b+-tree structures (everything except
134686 ** the leaf nodes). These functions and type are only ever used by code
134687 ** within the fts3SegWriterXXX() family of functions described above.
134688 **
134689 **   fts3NodeAddTerm()
134690 **   fts3NodeWrite()
134691 **   fts3NodeFree()
134692 **
134693 ** When a b+tree is written to the database (either as a result of a merge
134694 ** or the pending-terms table being flushed), leaves are written into the
134695 ** database file as soon as they are completely populated. The interior of
134696 ** the tree is assembled in memory and written out only once all leaves have
134697 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
134698 ** very large, meaning that the interior of the tree consumes relatively
134699 ** little memory.
134700 */
134701 struct SegmentNode {
134702   SegmentNode *pParent;           /* Parent node (or NULL for root node) */
134703   SegmentNode *pRight;            /* Pointer to right-sibling */
134704   SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
134705   int nEntry;                     /* Number of terms written to node so far */
134706   char *zTerm;                    /* Pointer to previous term buffer */
134707   int nTerm;                      /* Number of bytes in zTerm */
134708   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
134709   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
134710   int nData;                      /* Bytes of valid data so far */
134711   char *aData;                    /* Node data */
134712 };
134713 
134714 /*
134715 ** Valid values for the second argument to fts3SqlStmt().
134716 */
134717 #define SQL_DELETE_CONTENT             0
134718 #define SQL_IS_EMPTY                   1
134719 #define SQL_DELETE_ALL_CONTENT         2
134720 #define SQL_DELETE_ALL_SEGMENTS        3
134721 #define SQL_DELETE_ALL_SEGDIR          4
134722 #define SQL_DELETE_ALL_DOCSIZE         5
134723 #define SQL_DELETE_ALL_STAT            6
134724 #define SQL_SELECT_CONTENT_BY_ROWID    7
134725 #define SQL_NEXT_SEGMENT_INDEX         8
134726 #define SQL_INSERT_SEGMENTS            9
134727 #define SQL_NEXT_SEGMENTS_ID          10
134728 #define SQL_INSERT_SEGDIR             11
134729 #define SQL_SELECT_LEVEL              12
134730 #define SQL_SELECT_LEVEL_RANGE        13
134731 #define SQL_SELECT_LEVEL_COUNT        14
134732 #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
134733 #define SQL_DELETE_SEGDIR_LEVEL       16
134734 #define SQL_DELETE_SEGMENTS_RANGE     17
134735 #define SQL_CONTENT_INSERT            18
134736 #define SQL_DELETE_DOCSIZE            19
134737 #define SQL_REPLACE_DOCSIZE           20
134738 #define SQL_SELECT_DOCSIZE            21
134739 #define SQL_SELECT_STAT               22
134740 #define SQL_REPLACE_STAT              23
134741 
134742 #define SQL_SELECT_ALL_PREFIX_LEVEL   24
134743 #define SQL_DELETE_ALL_TERMS_SEGDIR   25
134744 #define SQL_DELETE_SEGDIR_RANGE       26
134745 #define SQL_SELECT_ALL_LANGID         27
134746 #define SQL_FIND_MERGE_LEVEL          28
134747 #define SQL_MAX_LEAF_NODE_ESTIMATE    29
134748 #define SQL_DELETE_SEGDIR_ENTRY       30
134749 #define SQL_SHIFT_SEGDIR_ENTRY        31
134750 #define SQL_SELECT_SEGDIR             32
134751 #define SQL_CHOMP_SEGDIR              33
134752 #define SQL_SEGMENT_IS_APPENDABLE     34
134753 #define SQL_SELECT_INDEXES            35
134754 #define SQL_SELECT_MXLEVEL            36
134755 
134756 /*
134757 ** This function is used to obtain an SQLite prepared statement handle
134758 ** for the statement identified by the second argument. If successful,
134759 ** *pp is set to the requested statement handle and SQLITE_OK returned.
134760 ** Otherwise, an SQLite error code is returned and *pp is set to 0.
134761 **
134762 ** If argument apVal is not NULL, then it must point to an array with
134763 ** at least as many entries as the requested statement has bound
134764 ** parameters. The values are bound to the statements parameters before
134765 ** returning.
134766 */
134767 static int fts3SqlStmt(
134768   Fts3Table *p,                   /* Virtual table handle */
134769   int eStmt,                      /* One of the SQL_XXX constants above */
134770   sqlite3_stmt **pp,              /* OUT: Statement handle */
134771   sqlite3_value **apVal           /* Values to bind to statement */
134772 ){
134773   const char *azSql[] = {
134774 /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
134775 /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
134776 /* 2  */  "DELETE FROM %Q.'%q_content'",
134777 /* 3  */  "DELETE FROM %Q.'%q_segments'",
134778 /* 4  */  "DELETE FROM %Q.'%q_segdir'",
134779 /* 5  */  "DELETE FROM %Q.'%q_docsize'",
134780 /* 6  */  "DELETE FROM %Q.'%q_stat'",
134781 /* 7  */  "SELECT %s WHERE rowid=?",
134782 /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
134783 /* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
134784 /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
134785 /* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
134786 
134787           /* Return segments in order from oldest to newest.*/
134788 /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
134789             "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
134790 /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
134791             "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
134792             "ORDER BY level DESC, idx ASC",
134793 
134794 /* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
134795 /* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
134796 
134797 /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
134798 /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
134799 /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
134800 /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
134801 /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
134802 /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
134803 /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
134804 /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
134805 /* 24 */  "",
134806 /* 25 */  "",
134807 
134808 /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
134809 /* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
134810 
134811 /* This statement is used to determine which level to read the input from
134812 ** when performing an incremental merge. It returns the absolute level number
134813 ** of the oldest level in the db that contains at least ? segments. Or,
134814 ** if no level in the FTS index contains more than ? segments, the statement
134815 ** returns zero rows.  */
134816 /* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
134817          "  ORDER BY (level %% 1024) ASC LIMIT 1",
134818 
134819 /* Estimate the upper limit on the number of leaf nodes in a new segment
134820 ** created by merging the oldest :2 segments from absolute level :1. See
134821 ** function sqlite3Fts3Incrmerge() for details.  */
134822 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
134823          "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
134824 
134825 /* SQL_DELETE_SEGDIR_ENTRY
134826 **   Delete the %_segdir entry on absolute level :1 with index :2.  */
134827 /* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
134828 
134829 /* SQL_SHIFT_SEGDIR_ENTRY
134830 **   Modify the idx value for the segment with idx=:3 on absolute level :2
134831 **   to :1.  */
134832 /* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
134833 
134834 /* SQL_SELECT_SEGDIR
134835 **   Read a single entry from the %_segdir table. The entry from absolute
134836 **   level :1 with index value :2.  */
134837 /* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
134838             "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
134839 
134840 /* SQL_CHOMP_SEGDIR
134841 **   Update the start_block (:1) and root (:2) fields of the %_segdir
134842 **   entry located on absolute level :3 with index :4.  */
134843 /* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
134844             "WHERE level = ? AND idx = ?",
134845 
134846 /* SQL_SEGMENT_IS_APPENDABLE
134847 **   Return a single row if the segment with end_block=? is appendable. Or
134848 **   no rows otherwise.  */
134849 /* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
134850 
134851 /* SQL_SELECT_INDEXES
134852 **   Return the list of valid segment indexes for absolute level ?  */
134853 /* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
134854 
134855 /* SQL_SELECT_MXLEVEL
134856 **   Return the largest relative level in the FTS index or indexes.  */
134857 /* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
134858   };
134859   int rc = SQLITE_OK;
134860   sqlite3_stmt *pStmt;
134861 
134862   assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
134863   assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
134864 
134865   pStmt = p->aStmt[eStmt];
134866   if( !pStmt ){
134867     char *zSql;
134868     if( eStmt==SQL_CONTENT_INSERT ){
134869       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
134870     }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
134871       zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
134872     }else{
134873       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
134874     }
134875     if( !zSql ){
134876       rc = SQLITE_NOMEM;
134877     }else{
134878       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
134879       sqlite3_free(zSql);
134880       assert( rc==SQLITE_OK || pStmt==0 );
134881       p->aStmt[eStmt] = pStmt;
134882     }
134883   }
134884   if( apVal ){
134885     int i;
134886     int nParam = sqlite3_bind_parameter_count(pStmt);
134887     for(i=0; rc==SQLITE_OK && i<nParam; i++){
134888       rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
134889     }
134890   }
134891   *pp = pStmt;
134892   return rc;
134893 }
134894 
134895 
134896 static int fts3SelectDocsize(
134897   Fts3Table *pTab,                /* FTS3 table handle */
134898   sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
134899   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
134900 ){
134901   sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
134902   int rc;                         /* Return code */
134903 
134904   rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
134905   if( rc==SQLITE_OK ){
134906     sqlite3_bind_int64(pStmt, 1, iDocid);
134907     rc = sqlite3_step(pStmt);
134908     if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
134909       rc = sqlite3_reset(pStmt);
134910       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
134911       pStmt = 0;
134912     }else{
134913       rc = SQLITE_OK;
134914     }
134915   }
134916 
134917   *ppStmt = pStmt;
134918   return rc;
134919 }
134920 
134921 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
134922   Fts3Table *pTab,                /* Fts3 table handle */
134923   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
134924 ){
134925   sqlite3_stmt *pStmt = 0;
134926   int rc;
134927   rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
134928   if( rc==SQLITE_OK ){
134929     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
134930     if( sqlite3_step(pStmt)!=SQLITE_ROW
134931      || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
134932     ){
134933       rc = sqlite3_reset(pStmt);
134934       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
134935       pStmt = 0;
134936     }
134937   }
134938   *ppStmt = pStmt;
134939   return rc;
134940 }
134941 
134942 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
134943   Fts3Table *pTab,                /* Fts3 table handle */
134944   sqlite3_int64 iDocid,           /* Docid to read size data for */
134945   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
134946 ){
134947   return fts3SelectDocsize(pTab, iDocid, ppStmt);
134948 }
134949 
134950 /*
134951 ** Similar to fts3SqlStmt(). Except, after binding the parameters in
134952 ** array apVal[] to the SQL statement identified by eStmt, the statement
134953 ** is executed.
134954 **
134955 ** Returns SQLITE_OK if the statement is successfully executed, or an
134956 ** SQLite error code otherwise.
134957 */
134958 static void fts3SqlExec(
134959   int *pRC,                /* Result code */
134960   Fts3Table *p,            /* The FTS3 table */
134961   int eStmt,               /* Index of statement to evaluate */
134962   sqlite3_value **apVal    /* Parameters to bind */
134963 ){
134964   sqlite3_stmt *pStmt;
134965   int rc;
134966   if( *pRC ) return;
134967   rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
134968   if( rc==SQLITE_OK ){
134969     sqlite3_step(pStmt);
134970     rc = sqlite3_reset(pStmt);
134971   }
134972   *pRC = rc;
134973 }
134974 
134975 
134976 /*
134977 ** This function ensures that the caller has obtained an exclusive
134978 ** shared-cache table-lock on the %_segdir table. This is required before
134979 ** writing data to the fts3 table. If this lock is not acquired first, then
134980 ** the caller may end up attempting to take this lock as part of committing
134981 ** a transaction, causing SQLite to return SQLITE_LOCKED or
134982 ** LOCKED_SHAREDCACHEto a COMMIT command.
134983 **
134984 ** It is best to avoid this because if FTS3 returns any error when
134985 ** committing a transaction, the whole transaction will be rolled back.
134986 ** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
134987 ** It can still happen if the user locks the underlying tables directly
134988 ** instead of accessing them via FTS.
134989 */
134990 static int fts3Writelock(Fts3Table *p){
134991   int rc = SQLITE_OK;
134992 
134993   if( p->nPendingData==0 ){
134994     sqlite3_stmt *pStmt;
134995     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
134996     if( rc==SQLITE_OK ){
134997       sqlite3_bind_null(pStmt, 1);
134998       sqlite3_step(pStmt);
134999       rc = sqlite3_reset(pStmt);
135000     }
135001   }
135002 
135003   return rc;
135004 }
135005 
135006 /*
135007 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
135008 ** Within each language id, a separate index is maintained to store the
135009 ** document terms, and each configured prefix size (configured the FTS
135010 ** "prefix=" option). And each index consists of multiple levels ("relative
135011 ** levels").
135012 **
135013 ** All three of these values (the language id, the specific index and the
135014 ** level within the index) are encoded in 64-bit integer values stored
135015 ** in the %_segdir table on disk. This function is used to convert three
135016 ** separate component values into the single 64-bit integer value that
135017 ** can be used to query the %_segdir table.
135018 **
135019 ** Specifically, each language-id/index combination is allocated 1024
135020 ** 64-bit integer level values ("absolute levels"). The main terms index
135021 ** for language-id 0 is allocate values 0-1023. The first prefix index
135022 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
135023 ** Language 1 indexes are allocated immediately following language 0.
135024 **
135025 ** So, for a system with nPrefix prefix indexes configured, the block of
135026 ** absolute levels that corresponds to language-id iLangid and index
135027 ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
135028 */
135029 static sqlite3_int64 getAbsoluteLevel(
135030   Fts3Table *p,                   /* FTS3 table handle */
135031   int iLangid,                    /* Language id */
135032   int iIndex,                     /* Index in p->aIndex[] */
135033   int iLevel                      /* Level of segments */
135034 ){
135035   sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
135036   assert( iLangid>=0 );
135037   assert( p->nIndex>0 );
135038   assert( iIndex>=0 && iIndex<p->nIndex );
135039 
135040   iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
135041   return iBase + iLevel;
135042 }
135043 
135044 /*
135045 ** Set *ppStmt to a statement handle that may be used to iterate through
135046 ** all rows in the %_segdir table, from oldest to newest. If successful,
135047 ** return SQLITE_OK. If an error occurs while preparing the statement,
135048 ** return an SQLite error code.
135049 **
135050 ** There is only ever one instance of this SQL statement compiled for
135051 ** each FTS3 table.
135052 **
135053 ** The statement returns the following columns from the %_segdir table:
135054 **
135055 **   0: idx
135056 **   1: start_block
135057 **   2: leaves_end_block
135058 **   3: end_block
135059 **   4: root
135060 */
135061 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
135062   Fts3Table *p,                   /* FTS3 table */
135063   int iLangid,                    /* Language being queried */
135064   int iIndex,                     /* Index for p->aIndex[] */
135065   int iLevel,                     /* Level to select (relative level) */
135066   sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
135067 ){
135068   int rc;
135069   sqlite3_stmt *pStmt = 0;
135070 
135071   assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
135072   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
135073   assert( iIndex>=0 && iIndex<p->nIndex );
135074 
135075   if( iLevel<0 ){
135076     /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
135077     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
135078     if( rc==SQLITE_OK ){
135079       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
135080       sqlite3_bind_int64(pStmt, 2,
135081           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
135082       );
135083     }
135084   }else{
135085     /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
135086     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
135087     if( rc==SQLITE_OK ){
135088       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
135089     }
135090   }
135091   *ppStmt = pStmt;
135092   return rc;
135093 }
135094 
135095 
135096 /*
135097 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
135098 ** if successful, or an SQLite error code otherwise.
135099 **
135100 ** This function also serves to allocate the PendingList structure itself.
135101 ** For example, to create a new PendingList structure containing two
135102 ** varints:
135103 **
135104 **   PendingList *p = 0;
135105 **   fts3PendingListAppendVarint(&p, 1);
135106 **   fts3PendingListAppendVarint(&p, 2);
135107 */
135108 static int fts3PendingListAppendVarint(
135109   PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
135110   sqlite3_int64 i                 /* Value to append to data */
135111 ){
135112   PendingList *p = *pp;
135113 
135114   /* Allocate or grow the PendingList as required. */
135115   if( !p ){
135116     p = sqlite3_malloc(sizeof(*p) + 100);
135117     if( !p ){
135118       return SQLITE_NOMEM;
135119     }
135120     p->nSpace = 100;
135121     p->aData = (char *)&p[1];
135122     p->nData = 0;
135123   }
135124   else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
135125     int nNew = p->nSpace * 2;
135126     p = sqlite3_realloc(p, sizeof(*p) + nNew);
135127     if( !p ){
135128       sqlite3_free(*pp);
135129       *pp = 0;
135130       return SQLITE_NOMEM;
135131     }
135132     p->nSpace = nNew;
135133     p->aData = (char *)&p[1];
135134   }
135135 
135136   /* Append the new serialized varint to the end of the list. */
135137   p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
135138   p->aData[p->nData] = '\0';
135139   *pp = p;
135140   return SQLITE_OK;
135141 }
135142 
135143 /*
135144 ** Add a docid/column/position entry to a PendingList structure. Non-zero
135145 ** is returned if the structure is sqlite3_realloced as part of adding
135146 ** the entry. Otherwise, zero.
135147 **
135148 ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
135149 ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
135150 ** it is set to SQLITE_OK.
135151 */
135152 static int fts3PendingListAppend(
135153   PendingList **pp,               /* IN/OUT: PendingList structure */
135154   sqlite3_int64 iDocid,           /* Docid for entry to add */
135155   sqlite3_int64 iCol,             /* Column for entry to add */
135156   sqlite3_int64 iPos,             /* Position of term for entry to add */
135157   int *pRc                        /* OUT: Return code */
135158 ){
135159   PendingList *p = *pp;
135160   int rc = SQLITE_OK;
135161 
135162   assert( !p || p->iLastDocid<=iDocid );
135163 
135164   if( !p || p->iLastDocid!=iDocid ){
135165     sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
135166     if( p ){
135167       assert( p->nData<p->nSpace );
135168       assert( p->aData[p->nData]==0 );
135169       p->nData++;
135170     }
135171     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
135172       goto pendinglistappend_out;
135173     }
135174     p->iLastCol = -1;
135175     p->iLastPos = 0;
135176     p->iLastDocid = iDocid;
135177   }
135178   if( iCol>0 && p->iLastCol!=iCol ){
135179     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
135180      || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
135181     ){
135182       goto pendinglistappend_out;
135183     }
135184     p->iLastCol = iCol;
135185     p->iLastPos = 0;
135186   }
135187   if( iCol>=0 ){
135188     assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
135189     rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
135190     if( rc==SQLITE_OK ){
135191       p->iLastPos = iPos;
135192     }
135193   }
135194 
135195  pendinglistappend_out:
135196   *pRc = rc;
135197   if( p!=*pp ){
135198     *pp = p;
135199     return 1;
135200   }
135201   return 0;
135202 }
135203 
135204 /*
135205 ** Free a PendingList object allocated by fts3PendingListAppend().
135206 */
135207 static void fts3PendingListDelete(PendingList *pList){
135208   sqlite3_free(pList);
135209 }
135210 
135211 /*
135212 ** Add an entry to one of the pending-terms hash tables.
135213 */
135214 static int fts3PendingTermsAddOne(
135215   Fts3Table *p,
135216   int iCol,
135217   int iPos,
135218   Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
135219   const char *zToken,
135220   int nToken
135221 ){
135222   PendingList *pList;
135223   int rc = SQLITE_OK;
135224 
135225   pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
135226   if( pList ){
135227     p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
135228   }
135229   if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
135230     if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
135231       /* Malloc failed while inserting the new entry. This can only
135232       ** happen if there was no previous entry for this token.
135233       */
135234       assert( 0==fts3HashFind(pHash, zToken, nToken) );
135235       sqlite3_free(pList);
135236       rc = SQLITE_NOMEM;
135237     }
135238   }
135239   if( rc==SQLITE_OK ){
135240     p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
135241   }
135242   return rc;
135243 }
135244 
135245 /*
135246 ** Tokenize the nul-terminated string zText and add all tokens to the
135247 ** pending-terms hash-table. The docid used is that currently stored in
135248 ** p->iPrevDocid, and the column is specified by argument iCol.
135249 **
135250 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
135251 */
135252 static int fts3PendingTermsAdd(
135253   Fts3Table *p,                   /* Table into which text will be inserted */
135254   int iLangid,                    /* Language id to use */
135255   const char *zText,              /* Text of document to be inserted */
135256   int iCol,                       /* Column into which text is being inserted */
135257   u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
135258 ){
135259   int rc;
135260   int iStart = 0;
135261   int iEnd = 0;
135262   int iPos = 0;
135263   int nWord = 0;
135264 
135265   char const *zToken;
135266   int nToken = 0;
135267 
135268   sqlite3_tokenizer *pTokenizer = p->pTokenizer;
135269   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
135270   sqlite3_tokenizer_cursor *pCsr;
135271   int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
135272       const char**,int*,int*,int*,int*);
135273 
135274   assert( pTokenizer && pModule );
135275 
135276   /* If the user has inserted a NULL value, this function may be called with
135277   ** zText==0. In this case, add zero token entries to the hash table and
135278   ** return early. */
135279   if( zText==0 ){
135280     *pnWord = 0;
135281     return SQLITE_OK;
135282   }
135283 
135284   rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
135285   if( rc!=SQLITE_OK ){
135286     return rc;
135287   }
135288 
135289   xNext = pModule->xNext;
135290   while( SQLITE_OK==rc
135291       && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
135292   ){
135293     int i;
135294     if( iPos>=nWord ) nWord = iPos+1;
135295 
135296     /* Positions cannot be negative; we use -1 as a terminator internally.
135297     ** Tokens must have a non-zero length.
135298     */
135299     if( iPos<0 || !zToken || nToken<=0 ){
135300       rc = SQLITE_ERROR;
135301       break;
135302     }
135303 
135304     /* Add the term to the terms index */
135305     rc = fts3PendingTermsAddOne(
135306         p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
135307     );
135308 
135309     /* Add the term to each of the prefix indexes that it is not too
135310     ** short for. */
135311     for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
135312       struct Fts3Index *pIndex = &p->aIndex[i];
135313       if( nToken<pIndex->nPrefix ) continue;
135314       rc = fts3PendingTermsAddOne(
135315           p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
135316       );
135317     }
135318   }
135319 
135320   pModule->xClose(pCsr);
135321   *pnWord += nWord;
135322   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
135323 }
135324 
135325 /*
135326 ** Calling this function indicates that subsequent calls to
135327 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
135328 ** contents of the document with docid iDocid.
135329 */
135330 static int fts3PendingTermsDocid(
135331   Fts3Table *p,                   /* Full-text table handle */
135332   int iLangid,                    /* Language id of row being written */
135333   sqlite_int64 iDocid             /* Docid of row being written */
135334 ){
135335   assert( iLangid>=0 );
135336 
135337   /* TODO(shess) Explore whether partially flushing the buffer on
135338   ** forced-flush would provide better performance.  I suspect that if
135339   ** we ordered the doclists by size and flushed the largest until the
135340   ** buffer was half empty, that would let the less frequent terms
135341   ** generate longer doclists.
135342   */
135343   if( iDocid<=p->iPrevDocid
135344    || p->iPrevLangid!=iLangid
135345    || p->nPendingData>p->nMaxPendingData
135346   ){
135347     int rc = sqlite3Fts3PendingTermsFlush(p);
135348     if( rc!=SQLITE_OK ) return rc;
135349   }
135350   p->iPrevDocid = iDocid;
135351   p->iPrevLangid = iLangid;
135352   return SQLITE_OK;
135353 }
135354 
135355 /*
135356 ** Discard the contents of the pending-terms hash tables.
135357 */
135358 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
135359   int i;
135360   for(i=0; i<p->nIndex; i++){
135361     Fts3HashElem *pElem;
135362     Fts3Hash *pHash = &p->aIndex[i].hPending;
135363     for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
135364       PendingList *pList = (PendingList *)fts3HashData(pElem);
135365       fts3PendingListDelete(pList);
135366     }
135367     fts3HashClear(pHash);
135368   }
135369   p->nPendingData = 0;
135370 }
135371 
135372 /*
135373 ** This function is called by the xUpdate() method as part of an INSERT
135374 ** operation. It adds entries for each term in the new record to the
135375 ** pendingTerms hash table.
135376 **
135377 ** Argument apVal is the same as the similarly named argument passed to
135378 ** fts3InsertData(). Parameter iDocid is the docid of the new row.
135379 */
135380 static int fts3InsertTerms(
135381   Fts3Table *p,
135382   int iLangid,
135383   sqlite3_value **apVal,
135384   u32 *aSz
135385 ){
135386   int i;                          /* Iterator variable */
135387   for(i=2; i<p->nColumn+2; i++){
135388     int iCol = i-2;
135389     if( p->abNotindexed[iCol]==0 ){
135390       const char *zText = (const char *)sqlite3_value_text(apVal[i]);
135391       int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
135392       if( rc!=SQLITE_OK ){
135393         return rc;
135394       }
135395       aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
135396     }
135397   }
135398   return SQLITE_OK;
135399 }
135400 
135401 /*
135402 ** This function is called by the xUpdate() method for an INSERT operation.
135403 ** The apVal parameter is passed a copy of the apVal argument passed by
135404 ** SQLite to the xUpdate() method. i.e:
135405 **
135406 **   apVal[0]                Not used for INSERT.
135407 **   apVal[1]                rowid
135408 **   apVal[2]                Left-most user-defined column
135409 **   ...
135410 **   apVal[p->nColumn+1]     Right-most user-defined column
135411 **   apVal[p->nColumn+2]     Hidden column with same name as table
135412 **   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
135413 **   apVal[p->nColumn+4]     Hidden languageid column
135414 */
135415 static int fts3InsertData(
135416   Fts3Table *p,                   /* Full-text table */
135417   sqlite3_value **apVal,          /* Array of values to insert */
135418   sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
135419 ){
135420   int rc;                         /* Return code */
135421   sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
135422 
135423   if( p->zContentTbl ){
135424     sqlite3_value *pRowid = apVal[p->nColumn+3];
135425     if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
135426       pRowid = apVal[1];
135427     }
135428     if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
135429       return SQLITE_CONSTRAINT;
135430     }
135431     *piDocid = sqlite3_value_int64(pRowid);
135432     return SQLITE_OK;
135433   }
135434 
135435   /* Locate the statement handle used to insert data into the %_content
135436   ** table. The SQL for this statement is:
135437   **
135438   **   INSERT INTO %_content VALUES(?, ?, ?, ...)
135439   **
135440   ** The statement features N '?' variables, where N is the number of user
135441   ** defined columns in the FTS3 table, plus one for the docid field.
135442   */
135443   rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
135444   if( rc==SQLITE_OK && p->zLanguageid ){
135445     rc = sqlite3_bind_int(
135446         pContentInsert, p->nColumn+2,
135447         sqlite3_value_int(apVal[p->nColumn+4])
135448     );
135449   }
135450   if( rc!=SQLITE_OK ) return rc;
135451 
135452   /* There is a quirk here. The users INSERT statement may have specified
135453   ** a value for the "rowid" field, for the "docid" field, or for both.
135454   ** Which is a problem, since "rowid" and "docid" are aliases for the
135455   ** same value. For example:
135456   **
135457   **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
135458   **
135459   ** In FTS3, this is an error. It is an error to specify non-NULL values
135460   ** for both docid and some other rowid alias.
135461   */
135462   if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
135463     if( SQLITE_NULL==sqlite3_value_type(apVal[0])
135464      && SQLITE_NULL!=sqlite3_value_type(apVal[1])
135465     ){
135466       /* A rowid/docid conflict. */
135467       return SQLITE_ERROR;
135468     }
135469     rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
135470     if( rc!=SQLITE_OK ) return rc;
135471   }
135472 
135473   /* Execute the statement to insert the record. Set *piDocid to the
135474   ** new docid value.
135475   */
135476   sqlite3_step(pContentInsert);
135477   rc = sqlite3_reset(pContentInsert);
135478 
135479   *piDocid = sqlite3_last_insert_rowid(p->db);
135480   return rc;
135481 }
135482 
135483 
135484 
135485 /*
135486 ** Remove all data from the FTS3 table. Clear the hash table containing
135487 ** pending terms.
135488 */
135489 static int fts3DeleteAll(Fts3Table *p, int bContent){
135490   int rc = SQLITE_OK;             /* Return code */
135491 
135492   /* Discard the contents of the pending-terms hash table. */
135493   sqlite3Fts3PendingTermsClear(p);
135494 
135495   /* Delete everything from the shadow tables. Except, leave %_content as
135496   ** is if bContent is false.  */
135497   assert( p->zContentTbl==0 || bContent==0 );
135498   if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
135499   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
135500   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
135501   if( p->bHasDocsize ){
135502     fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
135503   }
135504   if( p->bHasStat ){
135505     fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
135506   }
135507   return rc;
135508 }
135509 
135510 /*
135511 **
135512 */
135513 static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
135514   int iLangid = 0;
135515   if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
135516   return iLangid;
135517 }
135518 
135519 /*
135520 ** The first element in the apVal[] array is assumed to contain the docid
135521 ** (an integer) of a row about to be deleted. Remove all terms from the
135522 ** full-text index.
135523 */
135524 static void fts3DeleteTerms(
135525   int *pRC,               /* Result code */
135526   Fts3Table *p,           /* The FTS table to delete from */
135527   sqlite3_value *pRowid,  /* The docid to be deleted */
135528   u32 *aSz,               /* Sizes of deleted document written here */
135529   int *pbFound            /* OUT: Set to true if row really does exist */
135530 ){
135531   int rc;
135532   sqlite3_stmt *pSelect;
135533 
135534   assert( *pbFound==0 );
135535   if( *pRC ) return;
135536   rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
135537   if( rc==SQLITE_OK ){
135538     if( SQLITE_ROW==sqlite3_step(pSelect) ){
135539       int i;
135540       int iLangid = langidFromSelect(p, pSelect);
135541       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
135542       for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
135543         int iCol = i-1;
135544         if( p->abNotindexed[iCol]==0 ){
135545           const char *zText = (const char *)sqlite3_column_text(pSelect, i);
135546           rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
135547           aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
135548         }
135549       }
135550       if( rc!=SQLITE_OK ){
135551         sqlite3_reset(pSelect);
135552         *pRC = rc;
135553         return;
135554       }
135555       *pbFound = 1;
135556     }
135557     rc = sqlite3_reset(pSelect);
135558   }else{
135559     sqlite3_reset(pSelect);
135560   }
135561   *pRC = rc;
135562 }
135563 
135564 /*
135565 ** Forward declaration to account for the circular dependency between
135566 ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
135567 */
135568 static int fts3SegmentMerge(Fts3Table *, int, int, int);
135569 
135570 /*
135571 ** This function allocates a new level iLevel index in the segdir table.
135572 ** Usually, indexes are allocated within a level sequentially starting
135573 ** with 0, so the allocated index is one greater than the value returned
135574 ** by:
135575 **
135576 **   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
135577 **
135578 ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
135579 ** level, they are merged into a single level (iLevel+1) segment and the
135580 ** allocated index is 0.
135581 **
135582 ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
135583 ** returned. Otherwise, an SQLite error code is returned.
135584 */
135585 static int fts3AllocateSegdirIdx(
135586   Fts3Table *p,
135587   int iLangid,                    /* Language id */
135588   int iIndex,                     /* Index for p->aIndex */
135589   int iLevel,
135590   int *piIdx
135591 ){
135592   int rc;                         /* Return Code */
135593   sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
135594   int iNext = 0;                  /* Result of query pNextIdx */
135595 
135596   assert( iLangid>=0 );
135597   assert( p->nIndex>=1 );
135598 
135599   /* Set variable iNext to the next available segdir index at level iLevel. */
135600   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
135601   if( rc==SQLITE_OK ){
135602     sqlite3_bind_int64(
135603         pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
135604     );
135605     if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
135606       iNext = sqlite3_column_int(pNextIdx, 0);
135607     }
135608     rc = sqlite3_reset(pNextIdx);
135609   }
135610 
135611   if( rc==SQLITE_OK ){
135612     /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
135613     ** full, merge all segments in level iLevel into a single iLevel+1
135614     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
135615     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
135616     */
135617     if( iNext>=FTS3_MERGE_COUNT ){
135618       fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
135619       rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
135620       *piIdx = 0;
135621     }else{
135622       *piIdx = iNext;
135623     }
135624   }
135625 
135626   return rc;
135627 }
135628 
135629 /*
135630 ** The %_segments table is declared as follows:
135631 **
135632 **   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
135633 **
135634 ** This function reads data from a single row of the %_segments table. The
135635 ** specific row is identified by the iBlockid parameter. If paBlob is not
135636 ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
135637 ** with the contents of the blob stored in the "block" column of the
135638 ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
135639 ** to the size of the blob in bytes before returning.
135640 **
135641 ** If an error occurs, or the table does not contain the specified row,
135642 ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
135643 ** paBlob is non-NULL, then it is the responsibility of the caller to
135644 ** eventually free the returned buffer.
135645 **
135646 ** This function may leave an open sqlite3_blob* handle in the
135647 ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
135648 ** to this function. The handle may be closed by calling the
135649 ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
135650 ** performance improvement, but the blob handle should always be closed
135651 ** before control is returned to the user (to prevent a lock being held
135652 ** on the database file for longer than necessary). Thus, any virtual table
135653 ** method (xFilter etc.) that may directly or indirectly call this function
135654 ** must call sqlite3Fts3SegmentsClose() before returning.
135655 */
135656 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
135657   Fts3Table *p,                   /* FTS3 table handle */
135658   sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
135659   char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
135660   int *pnBlob,                    /* OUT: Size of blob data */
135661   int *pnLoad                     /* OUT: Bytes actually loaded */
135662 ){
135663   int rc;                         /* Return code */
135664 
135665   /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
135666   assert( pnBlob );
135667 
135668   if( p->pSegments ){
135669     rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
135670   }else{
135671     if( 0==p->zSegmentsTbl ){
135672       p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
135673       if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
135674     }
135675     rc = sqlite3_blob_open(
135676        p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
135677     );
135678   }
135679 
135680   if( rc==SQLITE_OK ){
135681     int nByte = sqlite3_blob_bytes(p->pSegments);
135682     *pnBlob = nByte;
135683     if( paBlob ){
135684       char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
135685       if( !aByte ){
135686         rc = SQLITE_NOMEM;
135687       }else{
135688         if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
135689           nByte = FTS3_NODE_CHUNKSIZE;
135690           *pnLoad = nByte;
135691         }
135692         rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
135693         memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
135694         if( rc!=SQLITE_OK ){
135695           sqlite3_free(aByte);
135696           aByte = 0;
135697         }
135698       }
135699       *paBlob = aByte;
135700     }
135701   }
135702 
135703   return rc;
135704 }
135705 
135706 /*
135707 ** Close the blob handle at p->pSegments, if it is open. See comments above
135708 ** the sqlite3Fts3ReadBlock() function for details.
135709 */
135710 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
135711   sqlite3_blob_close(p->pSegments);
135712   p->pSegments = 0;
135713 }
135714 
135715 static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
135716   int nRead;                      /* Number of bytes to read */
135717   int rc;                         /* Return code */
135718 
135719   nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
135720   rc = sqlite3_blob_read(
135721       pReader->pBlob,
135722       &pReader->aNode[pReader->nPopulate],
135723       nRead,
135724       pReader->nPopulate
135725   );
135726 
135727   if( rc==SQLITE_OK ){
135728     pReader->nPopulate += nRead;
135729     memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
135730     if( pReader->nPopulate==pReader->nNode ){
135731       sqlite3_blob_close(pReader->pBlob);
135732       pReader->pBlob = 0;
135733       pReader->nPopulate = 0;
135734     }
135735   }
135736   return rc;
135737 }
135738 
135739 static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
135740   int rc = SQLITE_OK;
135741   assert( !pReader->pBlob
135742        || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
135743   );
135744   while( pReader->pBlob && rc==SQLITE_OK
135745      &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
135746   ){
135747     rc = fts3SegReaderIncrRead(pReader);
135748   }
135749   return rc;
135750 }
135751 
135752 /*
135753 ** Set an Fts3SegReader cursor to point at EOF.
135754 */
135755 static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
135756   if( !fts3SegReaderIsRootOnly(pSeg) ){
135757     sqlite3_free(pSeg->aNode);
135758     sqlite3_blob_close(pSeg->pBlob);
135759     pSeg->pBlob = 0;
135760   }
135761   pSeg->aNode = 0;
135762 }
135763 
135764 /*
135765 ** Move the iterator passed as the first argument to the next term in the
135766 ** segment. If successful, SQLITE_OK is returned. If there is no next term,
135767 ** SQLITE_DONE. Otherwise, an SQLite error code.
135768 */
135769 static int fts3SegReaderNext(
135770   Fts3Table *p,
135771   Fts3SegReader *pReader,
135772   int bIncr
135773 ){
135774   int rc;                         /* Return code of various sub-routines */
135775   char *pNext;                    /* Cursor variable */
135776   int nPrefix;                    /* Number of bytes in term prefix */
135777   int nSuffix;                    /* Number of bytes in term suffix */
135778 
135779   if( !pReader->aDoclist ){
135780     pNext = pReader->aNode;
135781   }else{
135782     pNext = &pReader->aDoclist[pReader->nDoclist];
135783   }
135784 
135785   if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
135786 
135787     if( fts3SegReaderIsPending(pReader) ){
135788       Fts3HashElem *pElem = *(pReader->ppNextElem);
135789       if( pElem==0 ){
135790         pReader->aNode = 0;
135791       }else{
135792         PendingList *pList = (PendingList *)fts3HashData(pElem);
135793         pReader->zTerm = (char *)fts3HashKey(pElem);
135794         pReader->nTerm = fts3HashKeysize(pElem);
135795         pReader->nNode = pReader->nDoclist = pList->nData + 1;
135796         pReader->aNode = pReader->aDoclist = pList->aData;
135797         pReader->ppNextElem++;
135798         assert( pReader->aNode );
135799       }
135800       return SQLITE_OK;
135801     }
135802 
135803     fts3SegReaderSetEof(pReader);
135804 
135805     /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
135806     ** blocks have already been traversed.  */
135807     assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
135808     if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
135809       return SQLITE_OK;
135810     }
135811 
135812     rc = sqlite3Fts3ReadBlock(
135813         p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
135814         (bIncr ? &pReader->nPopulate : 0)
135815     );
135816     if( rc!=SQLITE_OK ) return rc;
135817     assert( pReader->pBlob==0 );
135818     if( bIncr && pReader->nPopulate<pReader->nNode ){
135819       pReader->pBlob = p->pSegments;
135820       p->pSegments = 0;
135821     }
135822     pNext = pReader->aNode;
135823   }
135824 
135825   assert( !fts3SegReaderIsPending(pReader) );
135826 
135827   rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
135828   if( rc!=SQLITE_OK ) return rc;
135829 
135830   /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
135831   ** safe (no risk of overread) even if the node data is corrupted. */
135832   pNext += fts3GetVarint32(pNext, &nPrefix);
135833   pNext += fts3GetVarint32(pNext, &nSuffix);
135834   if( nPrefix<0 || nSuffix<=0
135835    || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
135836   ){
135837     return FTS_CORRUPT_VTAB;
135838   }
135839 
135840   if( nPrefix+nSuffix>pReader->nTermAlloc ){
135841     int nNew = (nPrefix+nSuffix)*2;
135842     char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
135843     if( !zNew ){
135844       return SQLITE_NOMEM;
135845     }
135846     pReader->zTerm = zNew;
135847     pReader->nTermAlloc = nNew;
135848   }
135849 
135850   rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
135851   if( rc!=SQLITE_OK ) return rc;
135852 
135853   memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
135854   pReader->nTerm = nPrefix+nSuffix;
135855   pNext += nSuffix;
135856   pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
135857   pReader->aDoclist = pNext;
135858   pReader->pOffsetList = 0;
135859 
135860   /* Check that the doclist does not appear to extend past the end of the
135861   ** b-tree node. And that the final byte of the doclist is 0x00. If either
135862   ** of these statements is untrue, then the data structure is corrupt.
135863   */
135864   if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
135865    || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
135866   ){
135867     return FTS_CORRUPT_VTAB;
135868   }
135869   return SQLITE_OK;
135870 }
135871 
135872 /*
135873 ** Set the SegReader to point to the first docid in the doclist associated
135874 ** with the current term.
135875 */
135876 static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
135877   int rc = SQLITE_OK;
135878   assert( pReader->aDoclist );
135879   assert( !pReader->pOffsetList );
135880   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
135881     u8 bEof = 0;
135882     pReader->iDocid = 0;
135883     pReader->nOffsetList = 0;
135884     sqlite3Fts3DoclistPrev(0,
135885         pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
135886         &pReader->iDocid, &pReader->nOffsetList, &bEof
135887     );
135888   }else{
135889     rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
135890     if( rc==SQLITE_OK ){
135891       int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
135892       pReader->pOffsetList = &pReader->aDoclist[n];
135893     }
135894   }
135895   return rc;
135896 }
135897 
135898 /*
135899 ** Advance the SegReader to point to the next docid in the doclist
135900 ** associated with the current term.
135901 **
135902 ** If arguments ppOffsetList and pnOffsetList are not NULL, then
135903 ** *ppOffsetList is set to point to the first column-offset list
135904 ** in the doclist entry (i.e. immediately past the docid varint).
135905 ** *pnOffsetList is set to the length of the set of column-offset
135906 ** lists, not including the nul-terminator byte. For example:
135907 */
135908 static int fts3SegReaderNextDocid(
135909   Fts3Table *pTab,
135910   Fts3SegReader *pReader,         /* Reader to advance to next docid */
135911   char **ppOffsetList,            /* OUT: Pointer to current position-list */
135912   int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
135913 ){
135914   int rc = SQLITE_OK;
135915   char *p = pReader->pOffsetList;
135916   char c = 0;
135917 
135918   assert( p );
135919 
135920   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
135921     /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
135922     ** Pending-terms doclists are always built up in ascending order, so
135923     ** we have to iterate through them backwards here. */
135924     u8 bEof = 0;
135925     if( ppOffsetList ){
135926       *ppOffsetList = pReader->pOffsetList;
135927       *pnOffsetList = pReader->nOffsetList - 1;
135928     }
135929     sqlite3Fts3DoclistPrev(0,
135930         pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
135931         &pReader->nOffsetList, &bEof
135932     );
135933     if( bEof ){
135934       pReader->pOffsetList = 0;
135935     }else{
135936       pReader->pOffsetList = p;
135937     }
135938   }else{
135939     char *pEnd = &pReader->aDoclist[pReader->nDoclist];
135940 
135941     /* Pointer p currently points at the first byte of an offset list. The
135942     ** following block advances it to point one byte past the end of
135943     ** the same offset list. */
135944     while( 1 ){
135945 
135946       /* The following line of code (and the "p++" below the while() loop) is
135947       ** normally all that is required to move pointer p to the desired
135948       ** position. The exception is if this node is being loaded from disk
135949       ** incrementally and pointer "p" now points to the first byte past
135950       ** the populated part of pReader->aNode[].
135951       */
135952       while( *p | c ) c = *p++ & 0x80;
135953       assert( *p==0 );
135954 
135955       if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
135956       rc = fts3SegReaderIncrRead(pReader);
135957       if( rc!=SQLITE_OK ) return rc;
135958     }
135959     p++;
135960 
135961     /* If required, populate the output variables with a pointer to and the
135962     ** size of the previous offset-list.
135963     */
135964     if( ppOffsetList ){
135965       *ppOffsetList = pReader->pOffsetList;
135966       *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
135967     }
135968 
135969     /* List may have been edited in place by fts3EvalNearTrim() */
135970     while( p<pEnd && *p==0 ) p++;
135971 
135972     /* If there are no more entries in the doclist, set pOffsetList to
135973     ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
135974     ** Fts3SegReader.pOffsetList to point to the next offset list before
135975     ** returning.
135976     */
135977     if( p>=pEnd ){
135978       pReader->pOffsetList = 0;
135979     }else{
135980       rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
135981       if( rc==SQLITE_OK ){
135982         sqlite3_int64 iDelta;
135983         pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
135984         if( pTab->bDescIdx ){
135985           pReader->iDocid -= iDelta;
135986         }else{
135987           pReader->iDocid += iDelta;
135988         }
135989       }
135990     }
135991   }
135992 
135993   return SQLITE_OK;
135994 }
135995 
135996 
135997 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
135998   Fts3Cursor *pCsr,
135999   Fts3MultiSegReader *pMsr,
136000   int *pnOvfl
136001 ){
136002   Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
136003   int nOvfl = 0;
136004   int ii;
136005   int rc = SQLITE_OK;
136006   int pgsz = p->nPgsz;
136007 
136008   assert( p->bFts4 );
136009   assert( pgsz>0 );
136010 
136011   for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
136012     Fts3SegReader *pReader = pMsr->apSegment[ii];
136013     if( !fts3SegReaderIsPending(pReader)
136014      && !fts3SegReaderIsRootOnly(pReader)
136015     ){
136016       sqlite3_int64 jj;
136017       for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
136018         int nBlob;
136019         rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
136020         if( rc!=SQLITE_OK ) break;
136021         if( (nBlob+35)>pgsz ){
136022           nOvfl += (nBlob + 34)/pgsz;
136023         }
136024       }
136025     }
136026   }
136027   *pnOvfl = nOvfl;
136028   return rc;
136029 }
136030 
136031 /*
136032 ** Free all allocations associated with the iterator passed as the
136033 ** second argument.
136034 */
136035 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
136036   if( pReader && !fts3SegReaderIsPending(pReader) ){
136037     sqlite3_free(pReader->zTerm);
136038     if( !fts3SegReaderIsRootOnly(pReader) ){
136039       sqlite3_free(pReader->aNode);
136040       sqlite3_blob_close(pReader->pBlob);
136041     }
136042   }
136043   sqlite3_free(pReader);
136044 }
136045 
136046 /*
136047 ** Allocate a new SegReader object.
136048 */
136049 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
136050   int iAge,                       /* Segment "age". */
136051   int bLookup,                    /* True for a lookup only */
136052   sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
136053   sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
136054   sqlite3_int64 iEndBlock,        /* Final block of segment */
136055   const char *zRoot,              /* Buffer containing root node */
136056   int nRoot,                      /* Size of buffer containing root node */
136057   Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
136058 ){
136059   Fts3SegReader *pReader;         /* Newly allocated SegReader object */
136060   int nExtra = 0;                 /* Bytes to allocate segment root node */
136061 
136062   assert( iStartLeaf<=iEndLeaf );
136063   if( iStartLeaf==0 ){
136064     nExtra = nRoot + FTS3_NODE_PADDING;
136065   }
136066 
136067   pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
136068   if( !pReader ){
136069     return SQLITE_NOMEM;
136070   }
136071   memset(pReader, 0, sizeof(Fts3SegReader));
136072   pReader->iIdx = iAge;
136073   pReader->bLookup = bLookup!=0;
136074   pReader->iStartBlock = iStartLeaf;
136075   pReader->iLeafEndBlock = iEndLeaf;
136076   pReader->iEndBlock = iEndBlock;
136077 
136078   if( nExtra ){
136079     /* The entire segment is stored in the root node. */
136080     pReader->aNode = (char *)&pReader[1];
136081     pReader->rootOnly = 1;
136082     pReader->nNode = nRoot;
136083     memcpy(pReader->aNode, zRoot, nRoot);
136084     memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
136085   }else{
136086     pReader->iCurrentBlock = iStartLeaf-1;
136087   }
136088   *ppReader = pReader;
136089   return SQLITE_OK;
136090 }
136091 
136092 /*
136093 ** This is a comparison function used as a qsort() callback when sorting
136094 ** an array of pending terms by term. This occurs as part of flushing
136095 ** the contents of the pending-terms hash table to the database.
136096 */
136097 static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
136098   char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
136099   char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
136100   int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
136101   int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
136102 
136103   int n = (n1<n2 ? n1 : n2);
136104   int c = memcmp(z1, z2, n);
136105   if( c==0 ){
136106     c = n1 - n2;
136107   }
136108   return c;
136109 }
136110 
136111 /*
136112 ** This function is used to allocate an Fts3SegReader that iterates through
136113 ** a subset of the terms stored in the Fts3Table.pendingTerms array.
136114 **
136115 ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
136116 ** through each term in the pending-terms table. Or, if isPrefixIter is
136117 ** non-zero, it iterates through each term and its prefixes. For example, if
136118 ** the pending terms hash table contains the terms "sqlite", "mysql" and
136119 ** "firebird", then the iterator visits the following 'terms' (in the order
136120 ** shown):
136121 **
136122 **   f fi fir fire fireb firebi firebir firebird
136123 **   m my mys mysq mysql
136124 **   s sq sql sqli sqlit sqlite
136125 **
136126 ** Whereas if isPrefixIter is zero, the terms visited are:
136127 **
136128 **   firebird mysql sqlite
136129 */
136130 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
136131   Fts3Table *p,                   /* Virtual table handle */
136132   int iIndex,                     /* Index for p->aIndex */
136133   const char *zTerm,              /* Term to search for */
136134   int nTerm,                      /* Size of buffer zTerm */
136135   int bPrefix,                    /* True for a prefix iterator */
136136   Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
136137 ){
136138   Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
136139   Fts3HashElem *pE;               /* Iterator variable */
136140   Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
136141   int nElem = 0;                  /* Size of array at aElem */
136142   int rc = SQLITE_OK;             /* Return Code */
136143   Fts3Hash *pHash;
136144 
136145   pHash = &p->aIndex[iIndex].hPending;
136146   if( bPrefix ){
136147     int nAlloc = 0;               /* Size of allocated array at aElem */
136148 
136149     for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
136150       char *zKey = (char *)fts3HashKey(pE);
136151       int nKey = fts3HashKeysize(pE);
136152       if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
136153         if( nElem==nAlloc ){
136154           Fts3HashElem **aElem2;
136155           nAlloc += 16;
136156           aElem2 = (Fts3HashElem **)sqlite3_realloc(
136157               aElem, nAlloc*sizeof(Fts3HashElem *)
136158           );
136159           if( !aElem2 ){
136160             rc = SQLITE_NOMEM;
136161             nElem = 0;
136162             break;
136163           }
136164           aElem = aElem2;
136165         }
136166 
136167         aElem[nElem++] = pE;
136168       }
136169     }
136170 
136171     /* If more than one term matches the prefix, sort the Fts3HashElem
136172     ** objects in term order using qsort(). This uses the same comparison
136173     ** callback as is used when flushing terms to disk.
136174     */
136175     if( nElem>1 ){
136176       qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
136177     }
136178 
136179   }else{
136180     /* The query is a simple term lookup that matches at most one term in
136181     ** the index. All that is required is a straight hash-lookup.
136182     **
136183     ** Because the stack address of pE may be accessed via the aElem pointer
136184     ** below, the "Fts3HashElem *pE" must be declared so that it is valid
136185     ** within this entire function, not just this "else{...}" block.
136186     */
136187     pE = fts3HashFindElem(pHash, zTerm, nTerm);
136188     if( pE ){
136189       aElem = &pE;
136190       nElem = 1;
136191     }
136192   }
136193 
136194   if( nElem>0 ){
136195     int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
136196     pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
136197     if( !pReader ){
136198       rc = SQLITE_NOMEM;
136199     }else{
136200       memset(pReader, 0, nByte);
136201       pReader->iIdx = 0x7FFFFFFF;
136202       pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
136203       memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
136204     }
136205   }
136206 
136207   if( bPrefix ){
136208     sqlite3_free(aElem);
136209   }
136210   *ppReader = pReader;
136211   return rc;
136212 }
136213 
136214 /*
136215 ** Compare the entries pointed to by two Fts3SegReader structures.
136216 ** Comparison is as follows:
136217 **
136218 **   1) EOF is greater than not EOF.
136219 **
136220 **   2) The current terms (if any) are compared using memcmp(). If one
136221 **      term is a prefix of another, the longer term is considered the
136222 **      larger.
136223 **
136224 **   3) By segment age. An older segment is considered larger.
136225 */
136226 static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
136227   int rc;
136228   if( pLhs->aNode && pRhs->aNode ){
136229     int rc2 = pLhs->nTerm - pRhs->nTerm;
136230     if( rc2<0 ){
136231       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
136232     }else{
136233       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
136234     }
136235     if( rc==0 ){
136236       rc = rc2;
136237     }
136238   }else{
136239     rc = (pLhs->aNode==0) - (pRhs->aNode==0);
136240   }
136241   if( rc==0 ){
136242     rc = pRhs->iIdx - pLhs->iIdx;
136243   }
136244   assert( rc!=0 );
136245   return rc;
136246 }
136247 
136248 /*
136249 ** A different comparison function for SegReader structures. In this
136250 ** version, it is assumed that each SegReader points to an entry in
136251 ** a doclist for identical terms. Comparison is made as follows:
136252 **
136253 **   1) EOF (end of doclist in this case) is greater than not EOF.
136254 **
136255 **   2) By current docid.
136256 **
136257 **   3) By segment age. An older segment is considered larger.
136258 */
136259 static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
136260   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
136261   if( rc==0 ){
136262     if( pLhs->iDocid==pRhs->iDocid ){
136263       rc = pRhs->iIdx - pLhs->iIdx;
136264     }else{
136265       rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
136266     }
136267   }
136268   assert( pLhs->aNode && pRhs->aNode );
136269   return rc;
136270 }
136271 static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
136272   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
136273   if( rc==0 ){
136274     if( pLhs->iDocid==pRhs->iDocid ){
136275       rc = pRhs->iIdx - pLhs->iIdx;
136276     }else{
136277       rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
136278     }
136279   }
136280   assert( pLhs->aNode && pRhs->aNode );
136281   return rc;
136282 }
136283 
136284 /*
136285 ** Compare the term that the Fts3SegReader object passed as the first argument
136286 ** points to with the term specified by arguments zTerm and nTerm.
136287 **
136288 ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
136289 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
136290 ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
136291 */
136292 static int fts3SegReaderTermCmp(
136293   Fts3SegReader *pSeg,            /* Segment reader object */
136294   const char *zTerm,              /* Term to compare to */
136295   int nTerm                       /* Size of term zTerm in bytes */
136296 ){
136297   int res = 0;
136298   if( pSeg->aNode ){
136299     if( pSeg->nTerm>nTerm ){
136300       res = memcmp(pSeg->zTerm, zTerm, nTerm);
136301     }else{
136302       res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
136303     }
136304     if( res==0 ){
136305       res = pSeg->nTerm-nTerm;
136306     }
136307   }
136308   return res;
136309 }
136310 
136311 /*
136312 ** Argument apSegment is an array of nSegment elements. It is known that
136313 ** the final (nSegment-nSuspect) members are already in sorted order
136314 ** (according to the comparison function provided). This function shuffles
136315 ** the array around until all entries are in sorted order.
136316 */
136317 static void fts3SegReaderSort(
136318   Fts3SegReader **apSegment,                     /* Array to sort entries of */
136319   int nSegment,                                  /* Size of apSegment array */
136320   int nSuspect,                                  /* Unsorted entry count */
136321   int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
136322 ){
136323   int i;                          /* Iterator variable */
136324 
136325   assert( nSuspect<=nSegment );
136326 
136327   if( nSuspect==nSegment ) nSuspect--;
136328   for(i=nSuspect-1; i>=0; i--){
136329     int j;
136330     for(j=i; j<(nSegment-1); j++){
136331       Fts3SegReader *pTmp;
136332       if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
136333       pTmp = apSegment[j+1];
136334       apSegment[j+1] = apSegment[j];
136335       apSegment[j] = pTmp;
136336     }
136337   }
136338 
136339 #ifndef NDEBUG
136340   /* Check that the list really is sorted now. */
136341   for(i=0; i<(nSuspect-1); i++){
136342     assert( xCmp(apSegment[i], apSegment[i+1])<0 );
136343   }
136344 #endif
136345 }
136346 
136347 /*
136348 ** Insert a record into the %_segments table.
136349 */
136350 static int fts3WriteSegment(
136351   Fts3Table *p,                   /* Virtual table handle */
136352   sqlite3_int64 iBlock,           /* Block id for new block */
136353   char *z,                        /* Pointer to buffer containing block data */
136354   int n                           /* Size of buffer z in bytes */
136355 ){
136356   sqlite3_stmt *pStmt;
136357   int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
136358   if( rc==SQLITE_OK ){
136359     sqlite3_bind_int64(pStmt, 1, iBlock);
136360     sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
136361     sqlite3_step(pStmt);
136362     rc = sqlite3_reset(pStmt);
136363   }
136364   return rc;
136365 }
136366 
136367 /*
136368 ** Find the largest relative level number in the table. If successful, set
136369 ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
136370 ** set *pnMax to zero and return an SQLite error code.
136371 */
136372 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
136373   int rc;
136374   int mxLevel = 0;
136375   sqlite3_stmt *pStmt = 0;
136376 
136377   rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
136378   if( rc==SQLITE_OK ){
136379     if( SQLITE_ROW==sqlite3_step(pStmt) ){
136380       mxLevel = sqlite3_column_int(pStmt, 0);
136381     }
136382     rc = sqlite3_reset(pStmt);
136383   }
136384   *pnMax = mxLevel;
136385   return rc;
136386 }
136387 
136388 /*
136389 ** Insert a record into the %_segdir table.
136390 */
136391 static int fts3WriteSegdir(
136392   Fts3Table *p,                   /* Virtual table handle */
136393   sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
136394   int iIdx,                       /* Value for "idx" field */
136395   sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
136396   sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
136397   sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
136398   char *zRoot,                    /* Blob value for "root" field */
136399   int nRoot                       /* Number of bytes in buffer zRoot */
136400 ){
136401   sqlite3_stmt *pStmt;
136402   int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
136403   if( rc==SQLITE_OK ){
136404     sqlite3_bind_int64(pStmt, 1, iLevel);
136405     sqlite3_bind_int(pStmt, 2, iIdx);
136406     sqlite3_bind_int64(pStmt, 3, iStartBlock);
136407     sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
136408     sqlite3_bind_int64(pStmt, 5, iEndBlock);
136409     sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
136410     sqlite3_step(pStmt);
136411     rc = sqlite3_reset(pStmt);
136412   }
136413   return rc;
136414 }
136415 
136416 /*
136417 ** Return the size of the common prefix (if any) shared by zPrev and
136418 ** zNext, in bytes. For example,
136419 **
136420 **   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
136421 **   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
136422 **   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
136423 */
136424 static int fts3PrefixCompress(
136425   const char *zPrev,              /* Buffer containing previous term */
136426   int nPrev,                      /* Size of buffer zPrev in bytes */
136427   const char *zNext,              /* Buffer containing next term */
136428   int nNext                       /* Size of buffer zNext in bytes */
136429 ){
136430   int n;
136431   UNUSED_PARAMETER(nNext);
136432   for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
136433   return n;
136434 }
136435 
136436 /*
136437 ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
136438 ** (according to memcmp) than the previous term.
136439 */
136440 static int fts3NodeAddTerm(
136441   Fts3Table *p,                   /* Virtual table handle */
136442   SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */
136443   int isCopyTerm,                 /* True if zTerm/nTerm is transient */
136444   const char *zTerm,              /* Pointer to buffer containing term */
136445   int nTerm                       /* Size of term in bytes */
136446 ){
136447   SegmentNode *pTree = *ppTree;
136448   int rc;
136449   SegmentNode *pNew;
136450 
136451   /* First try to append the term to the current node. Return early if
136452   ** this is possible.
136453   */
136454   if( pTree ){
136455     int nData = pTree->nData;     /* Current size of node in bytes */
136456     int nReq = nData;             /* Required space after adding zTerm */
136457     int nPrefix;                  /* Number of bytes of prefix compression */
136458     int nSuffix;                  /* Suffix length */
136459 
136460     nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
136461     nSuffix = nTerm-nPrefix;
136462 
136463     nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
136464     if( nReq<=p->nNodeSize || !pTree->zTerm ){
136465 
136466       if( nReq>p->nNodeSize ){
136467         /* An unusual case: this is the first term to be added to the node
136468         ** and the static node buffer (p->nNodeSize bytes) is not large
136469         ** enough. Use a separately malloced buffer instead This wastes
136470         ** p->nNodeSize bytes, but since this scenario only comes about when
136471         ** the database contain two terms that share a prefix of almost 2KB,
136472         ** this is not expected to be a serious problem.
136473         */
136474         assert( pTree->aData==(char *)&pTree[1] );
136475         pTree->aData = (char *)sqlite3_malloc(nReq);
136476         if( !pTree->aData ){
136477           return SQLITE_NOMEM;
136478         }
136479       }
136480 
136481       if( pTree->zTerm ){
136482         /* There is no prefix-length field for first term in a node */
136483         nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
136484       }
136485 
136486       nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
136487       memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
136488       pTree->nData = nData + nSuffix;
136489       pTree->nEntry++;
136490 
136491       if( isCopyTerm ){
136492         if( pTree->nMalloc<nTerm ){
136493           char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
136494           if( !zNew ){
136495             return SQLITE_NOMEM;
136496           }
136497           pTree->nMalloc = nTerm*2;
136498           pTree->zMalloc = zNew;
136499         }
136500         pTree->zTerm = pTree->zMalloc;
136501         memcpy(pTree->zTerm, zTerm, nTerm);
136502         pTree->nTerm = nTerm;
136503       }else{
136504         pTree->zTerm = (char *)zTerm;
136505         pTree->nTerm = nTerm;
136506       }
136507       return SQLITE_OK;
136508     }
136509   }
136510 
136511   /* If control flows to here, it was not possible to append zTerm to the
136512   ** current node. Create a new node (a right-sibling of the current node).
136513   ** If this is the first node in the tree, the term is added to it.
136514   **
136515   ** Otherwise, the term is not added to the new node, it is left empty for
136516   ** now. Instead, the term is inserted into the parent of pTree. If pTree
136517   ** has no parent, one is created here.
136518   */
136519   pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
136520   if( !pNew ){
136521     return SQLITE_NOMEM;
136522   }
136523   memset(pNew, 0, sizeof(SegmentNode));
136524   pNew->nData = 1 + FTS3_VARINT_MAX;
136525   pNew->aData = (char *)&pNew[1];
136526 
136527   if( pTree ){
136528     SegmentNode *pParent = pTree->pParent;
136529     rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
136530     if( pTree->pParent==0 ){
136531       pTree->pParent = pParent;
136532     }
136533     pTree->pRight = pNew;
136534     pNew->pLeftmost = pTree->pLeftmost;
136535     pNew->pParent = pParent;
136536     pNew->zMalloc = pTree->zMalloc;
136537     pNew->nMalloc = pTree->nMalloc;
136538     pTree->zMalloc = 0;
136539   }else{
136540     pNew->pLeftmost = pNew;
136541     rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
136542   }
136543 
136544   *ppTree = pNew;
136545   return rc;
136546 }
136547 
136548 /*
136549 ** Helper function for fts3NodeWrite().
136550 */
136551 static int fts3TreeFinishNode(
136552   SegmentNode *pTree,
136553   int iHeight,
136554   sqlite3_int64 iLeftChild
136555 ){
136556   int nStart;
136557   assert( iHeight>=1 && iHeight<128 );
136558   nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
136559   pTree->aData[nStart] = (char)iHeight;
136560   sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
136561   return nStart;
136562 }
136563 
136564 /*
136565 ** Write the buffer for the segment node pTree and all of its peers to the
136566 ** database. Then call this function recursively to write the parent of
136567 ** pTree and its peers to the database.
136568 **
136569 ** Except, if pTree is a root node, do not write it to the database. Instead,
136570 ** set output variables *paRoot and *pnRoot to contain the root node.
136571 **
136572 ** If successful, SQLITE_OK is returned and output variable *piLast is
136573 ** set to the largest blockid written to the database (or zero if no
136574 ** blocks were written to the db). Otherwise, an SQLite error code is
136575 ** returned.
136576 */
136577 static int fts3NodeWrite(
136578   Fts3Table *p,                   /* Virtual table handle */
136579   SegmentNode *pTree,             /* SegmentNode handle */
136580   int iHeight,                    /* Height of this node in tree */
136581   sqlite3_int64 iLeaf,            /* Block id of first leaf node */
136582   sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
136583   sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
136584   char **paRoot,                  /* OUT: Data for root node */
136585   int *pnRoot                     /* OUT: Size of root node in bytes */
136586 ){
136587   int rc = SQLITE_OK;
136588 
136589   if( !pTree->pParent ){
136590     /* Root node of the tree. */
136591     int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
136592     *piLast = iFree-1;
136593     *pnRoot = pTree->nData - nStart;
136594     *paRoot = &pTree->aData[nStart];
136595   }else{
136596     SegmentNode *pIter;
136597     sqlite3_int64 iNextFree = iFree;
136598     sqlite3_int64 iNextLeaf = iLeaf;
136599     for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
136600       int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
136601       int nWrite = pIter->nData - nStart;
136602 
136603       rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
136604       iNextFree++;
136605       iNextLeaf += (pIter->nEntry+1);
136606     }
136607     if( rc==SQLITE_OK ){
136608       assert( iNextLeaf==iFree );
136609       rc = fts3NodeWrite(
136610           p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
136611       );
136612     }
136613   }
136614 
136615   return rc;
136616 }
136617 
136618 /*
136619 ** Free all memory allocations associated with the tree pTree.
136620 */
136621 static void fts3NodeFree(SegmentNode *pTree){
136622   if( pTree ){
136623     SegmentNode *p = pTree->pLeftmost;
136624     fts3NodeFree(p->pParent);
136625     while( p ){
136626       SegmentNode *pRight = p->pRight;
136627       if( p->aData!=(char *)&p[1] ){
136628         sqlite3_free(p->aData);
136629       }
136630       assert( pRight==0 || p->zMalloc==0 );
136631       sqlite3_free(p->zMalloc);
136632       sqlite3_free(p);
136633       p = pRight;
136634     }
136635   }
136636 }
136637 
136638 /*
136639 ** Add a term to the segment being constructed by the SegmentWriter object
136640 ** *ppWriter. When adding the first term to a segment, *ppWriter should
136641 ** be passed NULL. This function will allocate a new SegmentWriter object
136642 ** and return it via the input/output variable *ppWriter in this case.
136643 **
136644 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
136645 */
136646 static int fts3SegWriterAdd(
136647   Fts3Table *p,                   /* Virtual table handle */
136648   SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */
136649   int isCopyTerm,                 /* True if buffer zTerm must be copied */
136650   const char *zTerm,              /* Pointer to buffer containing term */
136651   int nTerm,                      /* Size of term in bytes */
136652   const char *aDoclist,           /* Pointer to buffer containing doclist */
136653   int nDoclist                    /* Size of doclist in bytes */
136654 ){
136655   int nPrefix;                    /* Size of term prefix in bytes */
136656   int nSuffix;                    /* Size of term suffix in bytes */
136657   int nReq;                       /* Number of bytes required on leaf page */
136658   int nData;
136659   SegmentWriter *pWriter = *ppWriter;
136660 
136661   if( !pWriter ){
136662     int rc;
136663     sqlite3_stmt *pStmt;
136664 
136665     /* Allocate the SegmentWriter structure */
136666     pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
136667     if( !pWriter ) return SQLITE_NOMEM;
136668     memset(pWriter, 0, sizeof(SegmentWriter));
136669     *ppWriter = pWriter;
136670 
136671     /* Allocate a buffer in which to accumulate data */
136672     pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
136673     if( !pWriter->aData ) return SQLITE_NOMEM;
136674     pWriter->nSize = p->nNodeSize;
136675 
136676     /* Find the next free blockid in the %_segments table */
136677     rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
136678     if( rc!=SQLITE_OK ) return rc;
136679     if( SQLITE_ROW==sqlite3_step(pStmt) ){
136680       pWriter->iFree = sqlite3_column_int64(pStmt, 0);
136681       pWriter->iFirst = pWriter->iFree;
136682     }
136683     rc = sqlite3_reset(pStmt);
136684     if( rc!=SQLITE_OK ) return rc;
136685   }
136686   nData = pWriter->nData;
136687 
136688   nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
136689   nSuffix = nTerm-nPrefix;
136690 
136691   /* Figure out how many bytes are required by this new entry */
136692   nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
136693     sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
136694     nSuffix +                               /* Term suffix */
136695     sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
136696     nDoclist;                               /* Doclist data */
136697 
136698   if( nData>0 && nData+nReq>p->nNodeSize ){
136699     int rc;
136700 
136701     /* The current leaf node is full. Write it out to the database. */
136702     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
136703     if( rc!=SQLITE_OK ) return rc;
136704     p->nLeafAdd++;
136705 
136706     /* Add the current term to the interior node tree. The term added to
136707     ** the interior tree must:
136708     **
136709     **   a) be greater than the largest term on the leaf node just written
136710     **      to the database (still available in pWriter->zTerm), and
136711     **
136712     **   b) be less than or equal to the term about to be added to the new
136713     **      leaf node (zTerm/nTerm).
136714     **
136715     ** In other words, it must be the prefix of zTerm 1 byte longer than
136716     ** the common prefix (if any) of zTerm and pWriter->zTerm.
136717     */
136718     assert( nPrefix<nTerm );
136719     rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
136720     if( rc!=SQLITE_OK ) return rc;
136721 
136722     nData = 0;
136723     pWriter->nTerm = 0;
136724 
136725     nPrefix = 0;
136726     nSuffix = nTerm;
136727     nReq = 1 +                              /* varint containing prefix size */
136728       sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
136729       nTerm +                               /* Term suffix */
136730       sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
136731       nDoclist;                             /* Doclist data */
136732   }
136733 
136734   /* If the buffer currently allocated is too small for this entry, realloc
136735   ** the buffer to make it large enough.
136736   */
136737   if( nReq>pWriter->nSize ){
136738     char *aNew = sqlite3_realloc(pWriter->aData, nReq);
136739     if( !aNew ) return SQLITE_NOMEM;
136740     pWriter->aData = aNew;
136741     pWriter->nSize = nReq;
136742   }
136743   assert( nData+nReq<=pWriter->nSize );
136744 
136745   /* Append the prefix-compressed term and doclist to the buffer. */
136746   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
136747   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
136748   memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
136749   nData += nSuffix;
136750   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
136751   memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
136752   pWriter->nData = nData + nDoclist;
136753 
136754   /* Save the current term so that it can be used to prefix-compress the next.
136755   ** If the isCopyTerm parameter is true, then the buffer pointed to by
136756   ** zTerm is transient, so take a copy of the term data. Otherwise, just
136757   ** store a copy of the pointer.
136758   */
136759   if( isCopyTerm ){
136760     if( nTerm>pWriter->nMalloc ){
136761       char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
136762       if( !zNew ){
136763         return SQLITE_NOMEM;
136764       }
136765       pWriter->nMalloc = nTerm*2;
136766       pWriter->zMalloc = zNew;
136767       pWriter->zTerm = zNew;
136768     }
136769     assert( pWriter->zTerm==pWriter->zMalloc );
136770     memcpy(pWriter->zTerm, zTerm, nTerm);
136771   }else{
136772     pWriter->zTerm = (char *)zTerm;
136773   }
136774   pWriter->nTerm = nTerm;
136775 
136776   return SQLITE_OK;
136777 }
136778 
136779 /*
136780 ** Flush all data associated with the SegmentWriter object pWriter to the
136781 ** database. This function must be called after all terms have been added
136782 ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
136783 ** returned. Otherwise, an SQLite error code.
136784 */
136785 static int fts3SegWriterFlush(
136786   Fts3Table *p,                   /* Virtual table handle */
136787   SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
136788   sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
136789   int iIdx                        /* Value for 'idx' column of %_segdir */
136790 ){
136791   int rc;                         /* Return code */
136792   if( pWriter->pTree ){
136793     sqlite3_int64 iLast = 0;      /* Largest block id written to database */
136794     sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
136795     char *zRoot = NULL;           /* Pointer to buffer containing root node */
136796     int nRoot = 0;                /* Size of buffer zRoot */
136797 
136798     iLastLeaf = pWriter->iFree;
136799     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
136800     if( rc==SQLITE_OK ){
136801       rc = fts3NodeWrite(p, pWriter->pTree, 1,
136802           pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
136803     }
136804     if( rc==SQLITE_OK ){
136805       rc = fts3WriteSegdir(
136806           p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
136807     }
136808   }else{
136809     /* The entire tree fits on the root node. Write it to the segdir table. */
136810     rc = fts3WriteSegdir(
136811         p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
136812   }
136813   p->nLeafAdd++;
136814   return rc;
136815 }
136816 
136817 /*
136818 ** Release all memory held by the SegmentWriter object passed as the
136819 ** first argument.
136820 */
136821 static void fts3SegWriterFree(SegmentWriter *pWriter){
136822   if( pWriter ){
136823     sqlite3_free(pWriter->aData);
136824     sqlite3_free(pWriter->zMalloc);
136825     fts3NodeFree(pWriter->pTree);
136826     sqlite3_free(pWriter);
136827   }
136828 }
136829 
136830 /*
136831 ** The first value in the apVal[] array is assumed to contain an integer.
136832 ** This function tests if there exist any documents with docid values that
136833 ** are different from that integer. i.e. if deleting the document with docid
136834 ** pRowid would mean the FTS3 table were empty.
136835 **
136836 ** If successful, *pisEmpty is set to true if the table is empty except for
136837 ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
136838 ** error occurs, an SQLite error code is returned.
136839 */
136840 static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
136841   sqlite3_stmt *pStmt;
136842   int rc;
136843   if( p->zContentTbl ){
136844     /* If using the content=xxx option, assume the table is never empty */
136845     *pisEmpty = 0;
136846     rc = SQLITE_OK;
136847   }else{
136848     rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
136849     if( rc==SQLITE_OK ){
136850       if( SQLITE_ROW==sqlite3_step(pStmt) ){
136851         *pisEmpty = sqlite3_column_int(pStmt, 0);
136852       }
136853       rc = sqlite3_reset(pStmt);
136854     }
136855   }
136856   return rc;
136857 }
136858 
136859 /*
136860 ** Set *pnMax to the largest segment level in the database for the index
136861 ** iIndex.
136862 **
136863 ** Segment levels are stored in the 'level' column of the %_segdir table.
136864 **
136865 ** Return SQLITE_OK if successful, or an SQLite error code if not.
136866 */
136867 static int fts3SegmentMaxLevel(
136868   Fts3Table *p,
136869   int iLangid,
136870   int iIndex,
136871   sqlite3_int64 *pnMax
136872 ){
136873   sqlite3_stmt *pStmt;
136874   int rc;
136875   assert( iIndex>=0 && iIndex<p->nIndex );
136876 
136877   /* Set pStmt to the compiled version of:
136878   **
136879   **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
136880   **
136881   ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
136882   */
136883   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
136884   if( rc!=SQLITE_OK ) return rc;
136885   sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
136886   sqlite3_bind_int64(pStmt, 2,
136887       getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
136888   );
136889   if( SQLITE_ROW==sqlite3_step(pStmt) ){
136890     *pnMax = sqlite3_column_int64(pStmt, 0);
136891   }
136892   return sqlite3_reset(pStmt);
136893 }
136894 
136895 /*
136896 ** Delete all entries in the %_segments table associated with the segment
136897 ** opened with seg-reader pSeg. This function does not affect the contents
136898 ** of the %_segdir table.
136899 */
136900 static int fts3DeleteSegment(
136901   Fts3Table *p,                   /* FTS table handle */
136902   Fts3SegReader *pSeg             /* Segment to delete */
136903 ){
136904   int rc = SQLITE_OK;             /* Return code */
136905   if( pSeg->iStartBlock ){
136906     sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
136907     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
136908     if( rc==SQLITE_OK ){
136909       sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
136910       sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
136911       sqlite3_step(pDelete);
136912       rc = sqlite3_reset(pDelete);
136913     }
136914   }
136915   return rc;
136916 }
136917 
136918 /*
136919 ** This function is used after merging multiple segments into a single large
136920 ** segment to delete the old, now redundant, segment b-trees. Specifically,
136921 ** it:
136922 **
136923 **   1) Deletes all %_segments entries for the segments associated with
136924 **      each of the SegReader objects in the array passed as the third
136925 **      argument, and
136926 **
136927 **   2) deletes all %_segdir entries with level iLevel, or all %_segdir
136928 **      entries regardless of level if (iLevel<0).
136929 **
136930 ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
136931 */
136932 static int fts3DeleteSegdir(
136933   Fts3Table *p,                   /* Virtual table handle */
136934   int iLangid,                    /* Language id */
136935   int iIndex,                     /* Index for p->aIndex */
136936   int iLevel,                     /* Level of %_segdir entries to delete */
136937   Fts3SegReader **apSegment,      /* Array of SegReader objects */
136938   int nReader                     /* Size of array apSegment */
136939 ){
136940   int rc = SQLITE_OK;             /* Return Code */
136941   int i;                          /* Iterator variable */
136942   sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
136943 
136944   for(i=0; rc==SQLITE_OK && i<nReader; i++){
136945     rc = fts3DeleteSegment(p, apSegment[i]);
136946   }
136947   if( rc!=SQLITE_OK ){
136948     return rc;
136949   }
136950 
136951   assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
136952   if( iLevel==FTS3_SEGCURSOR_ALL ){
136953     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
136954     if( rc==SQLITE_OK ){
136955       sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
136956       sqlite3_bind_int64(pDelete, 2,
136957           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
136958       );
136959     }
136960   }else{
136961     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
136962     if( rc==SQLITE_OK ){
136963       sqlite3_bind_int64(
136964           pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
136965       );
136966     }
136967   }
136968 
136969   if( rc==SQLITE_OK ){
136970     sqlite3_step(pDelete);
136971     rc = sqlite3_reset(pDelete);
136972   }
136973 
136974   return rc;
136975 }
136976 
136977 /*
136978 ** When this function is called, buffer *ppList (size *pnList bytes) contains
136979 ** a position list that may (or may not) feature multiple columns. This
136980 ** function adjusts the pointer *ppList and the length *pnList so that they
136981 ** identify the subset of the position list that corresponds to column iCol.
136982 **
136983 ** If there are no entries in the input position list for column iCol, then
136984 ** *pnList is set to zero before returning.
136985 **
136986 ** If parameter bZero is non-zero, then any part of the input list following
136987 ** the end of the output list is zeroed before returning.
136988 */
136989 static void fts3ColumnFilter(
136990   int iCol,                       /* Column to filter on */
136991   int bZero,                      /* Zero out anything following *ppList */
136992   char **ppList,                  /* IN/OUT: Pointer to position list */
136993   int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
136994 ){
136995   char *pList = *ppList;
136996   int nList = *pnList;
136997   char *pEnd = &pList[nList];
136998   int iCurrent = 0;
136999   char *p = pList;
137000 
137001   assert( iCol>=0 );
137002   while( 1 ){
137003     char c = 0;
137004     while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
137005 
137006     if( iCol==iCurrent ){
137007       nList = (int)(p - pList);
137008       break;
137009     }
137010 
137011     nList -= (int)(p - pList);
137012     pList = p;
137013     if( nList==0 ){
137014       break;
137015     }
137016     p = &pList[1];
137017     p += fts3GetVarint32(p, &iCurrent);
137018   }
137019 
137020   if( bZero && &pList[nList]!=pEnd ){
137021     memset(&pList[nList], 0, pEnd - &pList[nList]);
137022   }
137023   *ppList = pList;
137024   *pnList = nList;
137025 }
137026 
137027 /*
137028 ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
137029 ** existing data). Grow the buffer if required.
137030 **
137031 ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
137032 ** trying to resize the buffer, return SQLITE_NOMEM.
137033 */
137034 static int fts3MsrBufferData(
137035   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
137036   char *pList,
137037   int nList
137038 ){
137039   if( nList>pMsr->nBuffer ){
137040     char *pNew;
137041     pMsr->nBuffer = nList*2;
137042     pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
137043     if( !pNew ) return SQLITE_NOMEM;
137044     pMsr->aBuffer = pNew;
137045   }
137046 
137047   memcpy(pMsr->aBuffer, pList, nList);
137048   return SQLITE_OK;
137049 }
137050 
137051 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
137052   Fts3Table *p,                   /* Virtual table handle */
137053   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
137054   sqlite3_int64 *piDocid,         /* OUT: Docid value */
137055   char **paPoslist,               /* OUT: Pointer to position list */
137056   int *pnPoslist                  /* OUT: Size of position list in bytes */
137057 ){
137058   int nMerge = pMsr->nAdvance;
137059   Fts3SegReader **apSegment = pMsr->apSegment;
137060   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
137061     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
137062   );
137063 
137064   if( nMerge==0 ){
137065     *paPoslist = 0;
137066     return SQLITE_OK;
137067   }
137068 
137069   while( 1 ){
137070     Fts3SegReader *pSeg;
137071     pSeg = pMsr->apSegment[0];
137072 
137073     if( pSeg->pOffsetList==0 ){
137074       *paPoslist = 0;
137075       break;
137076     }else{
137077       int rc;
137078       char *pList;
137079       int nList;
137080       int j;
137081       sqlite3_int64 iDocid = apSegment[0]->iDocid;
137082 
137083       rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
137084       j = 1;
137085       while( rc==SQLITE_OK
137086         && j<nMerge
137087         && apSegment[j]->pOffsetList
137088         && apSegment[j]->iDocid==iDocid
137089       ){
137090         rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
137091         j++;
137092       }
137093       if( rc!=SQLITE_OK ) return rc;
137094       fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
137095 
137096       if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
137097         rc = fts3MsrBufferData(pMsr, pList, nList+1);
137098         if( rc!=SQLITE_OK ) return rc;
137099         assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
137100         pList = pMsr->aBuffer;
137101       }
137102 
137103       if( pMsr->iColFilter>=0 ){
137104         fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
137105       }
137106 
137107       if( nList>0 ){
137108         *paPoslist = pList;
137109         *piDocid = iDocid;
137110         *pnPoslist = nList;
137111         break;
137112       }
137113     }
137114   }
137115 
137116   return SQLITE_OK;
137117 }
137118 
137119 static int fts3SegReaderStart(
137120   Fts3Table *p,                   /* Virtual table handle */
137121   Fts3MultiSegReader *pCsr,       /* Cursor object */
137122   const char *zTerm,              /* Term searched for (or NULL) */
137123   int nTerm                       /* Length of zTerm in bytes */
137124 ){
137125   int i;
137126   int nSeg = pCsr->nSegment;
137127 
137128   /* If the Fts3SegFilter defines a specific term (or term prefix) to search
137129   ** for, then advance each segment iterator until it points to a term of
137130   ** equal or greater value than the specified term. This prevents many
137131   ** unnecessary merge/sort operations for the case where single segment
137132   ** b-tree leaf nodes contain more than one term.
137133   */
137134   for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
137135     int res = 0;
137136     Fts3SegReader *pSeg = pCsr->apSegment[i];
137137     do {
137138       int rc = fts3SegReaderNext(p, pSeg, 0);
137139       if( rc!=SQLITE_OK ) return rc;
137140     }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
137141 
137142     if( pSeg->bLookup && res!=0 ){
137143       fts3SegReaderSetEof(pSeg);
137144     }
137145   }
137146   fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
137147 
137148   return SQLITE_OK;
137149 }
137150 
137151 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
137152   Fts3Table *p,                   /* Virtual table handle */
137153   Fts3MultiSegReader *pCsr,       /* Cursor object */
137154   Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
137155 ){
137156   pCsr->pFilter = pFilter;
137157   return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
137158 }
137159 
137160 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
137161   Fts3Table *p,                   /* Virtual table handle */
137162   Fts3MultiSegReader *pCsr,       /* Cursor object */
137163   int iCol,                       /* Column to match on. */
137164   const char *zTerm,              /* Term to iterate through a doclist for */
137165   int nTerm                       /* Number of bytes in zTerm */
137166 ){
137167   int i;
137168   int rc;
137169   int nSegment = pCsr->nSegment;
137170   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
137171     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
137172   );
137173 
137174   assert( pCsr->pFilter==0 );
137175   assert( zTerm && nTerm>0 );
137176 
137177   /* Advance each segment iterator until it points to the term zTerm/nTerm. */
137178   rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
137179   if( rc!=SQLITE_OK ) return rc;
137180 
137181   /* Determine how many of the segments actually point to zTerm/nTerm. */
137182   for(i=0; i<nSegment; i++){
137183     Fts3SegReader *pSeg = pCsr->apSegment[i];
137184     if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
137185       break;
137186     }
137187   }
137188   pCsr->nAdvance = i;
137189 
137190   /* Advance each of the segments to point to the first docid. */
137191   for(i=0; i<pCsr->nAdvance; i++){
137192     rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
137193     if( rc!=SQLITE_OK ) return rc;
137194   }
137195   fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
137196 
137197   assert( iCol<0 || iCol<p->nColumn );
137198   pCsr->iColFilter = iCol;
137199 
137200   return SQLITE_OK;
137201 }
137202 
137203 /*
137204 ** This function is called on a MultiSegReader that has been started using
137205 ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
137206 ** have been made. Calling this function puts the MultiSegReader in such
137207 ** a state that if the next two calls are:
137208 **
137209 **   sqlite3Fts3SegReaderStart()
137210 **   sqlite3Fts3SegReaderStep()
137211 **
137212 ** then the entire doclist for the term is available in
137213 ** MultiSegReader.aDoclist/nDoclist.
137214 */
137215 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
137216   int i;                          /* Used to iterate through segment-readers */
137217 
137218   assert( pCsr->zTerm==0 );
137219   assert( pCsr->nTerm==0 );
137220   assert( pCsr->aDoclist==0 );
137221   assert( pCsr->nDoclist==0 );
137222 
137223   pCsr->nAdvance = 0;
137224   pCsr->bRestart = 1;
137225   for(i=0; i<pCsr->nSegment; i++){
137226     pCsr->apSegment[i]->pOffsetList = 0;
137227     pCsr->apSegment[i]->nOffsetList = 0;
137228     pCsr->apSegment[i]->iDocid = 0;
137229   }
137230 
137231   return SQLITE_OK;
137232 }
137233 
137234 
137235 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
137236   Fts3Table *p,                   /* Virtual table handle */
137237   Fts3MultiSegReader *pCsr        /* Cursor object */
137238 ){
137239   int rc = SQLITE_OK;
137240 
137241   int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
137242   int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
137243   int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
137244   int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
137245   int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
137246   int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
137247 
137248   Fts3SegReader **apSegment = pCsr->apSegment;
137249   int nSegment = pCsr->nSegment;
137250   Fts3SegFilter *pFilter = pCsr->pFilter;
137251   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
137252     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
137253   );
137254 
137255   if( pCsr->nSegment==0 ) return SQLITE_OK;
137256 
137257   do {
137258     int nMerge;
137259     int i;
137260 
137261     /* Advance the first pCsr->nAdvance entries in the apSegment[] array
137262     ** forward. Then sort the list in order of current term again.
137263     */
137264     for(i=0; i<pCsr->nAdvance; i++){
137265       Fts3SegReader *pSeg = apSegment[i];
137266       if( pSeg->bLookup ){
137267         fts3SegReaderSetEof(pSeg);
137268       }else{
137269         rc = fts3SegReaderNext(p, pSeg, 0);
137270       }
137271       if( rc!=SQLITE_OK ) return rc;
137272     }
137273     fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
137274     pCsr->nAdvance = 0;
137275 
137276     /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
137277     assert( rc==SQLITE_OK );
137278     if( apSegment[0]->aNode==0 ) break;
137279 
137280     pCsr->nTerm = apSegment[0]->nTerm;
137281     pCsr->zTerm = apSegment[0]->zTerm;
137282 
137283     /* If this is a prefix-search, and if the term that apSegment[0] points
137284     ** to does not share a suffix with pFilter->zTerm/nTerm, then all
137285     ** required callbacks have been made. In this case exit early.
137286     **
137287     ** Similarly, if this is a search for an exact match, and the first term
137288     ** of segment apSegment[0] is not a match, exit early.
137289     */
137290     if( pFilter->zTerm && !isScan ){
137291       if( pCsr->nTerm<pFilter->nTerm
137292        || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
137293        || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
137294       ){
137295         break;
137296       }
137297     }
137298 
137299     nMerge = 1;
137300     while( nMerge<nSegment
137301         && apSegment[nMerge]->aNode
137302         && apSegment[nMerge]->nTerm==pCsr->nTerm
137303         && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
137304     ){
137305       nMerge++;
137306     }
137307 
137308     assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
137309     if( nMerge==1
137310      && !isIgnoreEmpty
137311      && !isFirst
137312      && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
137313     ){
137314       pCsr->nDoclist = apSegment[0]->nDoclist;
137315       if( fts3SegReaderIsPending(apSegment[0]) ){
137316         rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
137317         pCsr->aDoclist = pCsr->aBuffer;
137318       }else{
137319         pCsr->aDoclist = apSegment[0]->aDoclist;
137320       }
137321       if( rc==SQLITE_OK ) rc = SQLITE_ROW;
137322     }else{
137323       int nDoclist = 0;           /* Size of doclist */
137324       sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
137325 
137326       /* The current term of the first nMerge entries in the array
137327       ** of Fts3SegReader objects is the same. The doclists must be merged
137328       ** and a single term returned with the merged doclist.
137329       */
137330       for(i=0; i<nMerge; i++){
137331         fts3SegReaderFirstDocid(p, apSegment[i]);
137332       }
137333       fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
137334       while( apSegment[0]->pOffsetList ){
137335         int j;                    /* Number of segments that share a docid */
137336         char *pList = 0;
137337         int nList = 0;
137338         int nByte;
137339         sqlite3_int64 iDocid = apSegment[0]->iDocid;
137340         fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
137341         j = 1;
137342         while( j<nMerge
137343             && apSegment[j]->pOffsetList
137344             && apSegment[j]->iDocid==iDocid
137345         ){
137346           fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
137347           j++;
137348         }
137349 
137350         if( isColFilter ){
137351           fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
137352         }
137353 
137354         if( !isIgnoreEmpty || nList>0 ){
137355 
137356           /* Calculate the 'docid' delta value to write into the merged
137357           ** doclist. */
137358           sqlite3_int64 iDelta;
137359           if( p->bDescIdx && nDoclist>0 ){
137360             iDelta = iPrev - iDocid;
137361           }else{
137362             iDelta = iDocid - iPrev;
137363           }
137364           assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
137365           assert( nDoclist>0 || iDelta==iDocid );
137366 
137367           nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
137368           if( nDoclist+nByte>pCsr->nBuffer ){
137369             char *aNew;
137370             pCsr->nBuffer = (nDoclist+nByte)*2;
137371             aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
137372             if( !aNew ){
137373               return SQLITE_NOMEM;
137374             }
137375             pCsr->aBuffer = aNew;
137376           }
137377 
137378           if( isFirst ){
137379             char *a = &pCsr->aBuffer[nDoclist];
137380             int nWrite;
137381 
137382             nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
137383             if( nWrite ){
137384               iPrev = iDocid;
137385               nDoclist += nWrite;
137386             }
137387           }else{
137388             nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
137389             iPrev = iDocid;
137390             if( isRequirePos ){
137391               memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
137392               nDoclist += nList;
137393               pCsr->aBuffer[nDoclist++] = '\0';
137394             }
137395           }
137396         }
137397 
137398         fts3SegReaderSort(apSegment, nMerge, j, xCmp);
137399       }
137400       if( nDoclist>0 ){
137401         pCsr->aDoclist = pCsr->aBuffer;
137402         pCsr->nDoclist = nDoclist;
137403         rc = SQLITE_ROW;
137404       }
137405     }
137406     pCsr->nAdvance = nMerge;
137407   }while( rc==SQLITE_OK );
137408 
137409   return rc;
137410 }
137411 
137412 
137413 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
137414   Fts3MultiSegReader *pCsr       /* Cursor object */
137415 ){
137416   if( pCsr ){
137417     int i;
137418     for(i=0; i<pCsr->nSegment; i++){
137419       sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
137420     }
137421     sqlite3_free(pCsr->apSegment);
137422     sqlite3_free(pCsr->aBuffer);
137423 
137424     pCsr->nSegment = 0;
137425     pCsr->apSegment = 0;
137426     pCsr->aBuffer = 0;
137427   }
137428 }
137429 
137430 /*
137431 ** Merge all level iLevel segments in the database into a single
137432 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
137433 ** single segment with a level equal to the numerically largest level
137434 ** currently present in the database.
137435 **
137436 ** If this function is called with iLevel<0, but there is only one
137437 ** segment in the database, SQLITE_DONE is returned immediately.
137438 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
137439 ** an SQLite error code is returned.
137440 */
137441 static int fts3SegmentMerge(
137442   Fts3Table *p,
137443   int iLangid,                    /* Language id to merge */
137444   int iIndex,                     /* Index in p->aIndex[] to merge */
137445   int iLevel                      /* Level to merge */
137446 ){
137447   int rc;                         /* Return code */
137448   int iIdx = 0;                   /* Index of new segment */
137449   sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
137450   SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
137451   Fts3SegFilter filter;           /* Segment term filter condition */
137452   Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
137453   int bIgnoreEmpty = 0;           /* True to ignore empty segments */
137454 
137455   assert( iLevel==FTS3_SEGCURSOR_ALL
137456        || iLevel==FTS3_SEGCURSOR_PENDING
137457        || iLevel>=0
137458   );
137459   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
137460   assert( iIndex>=0 && iIndex<p->nIndex );
137461 
137462   rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
137463   if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
137464 
137465   if( iLevel==FTS3_SEGCURSOR_ALL ){
137466     /* This call is to merge all segments in the database to a single
137467     ** segment. The level of the new segment is equal to the numerically
137468     ** greatest segment level currently present in the database for this
137469     ** index. The idx of the new segment is always 0.  */
137470     if( csr.nSegment==1 ){
137471       rc = SQLITE_DONE;
137472       goto finished;
137473     }
137474     rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
137475     bIgnoreEmpty = 1;
137476 
137477   }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
137478     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
137479     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
137480   }else{
137481     /* This call is to merge all segments at level iLevel. find the next
137482     ** available segment index at level iLevel+1. The call to
137483     ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
137484     ** a single iLevel+2 segment if necessary.  */
137485     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
137486     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
137487   }
137488   if( rc!=SQLITE_OK ) goto finished;
137489   assert( csr.nSegment>0 );
137490   assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
137491   assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
137492 
137493   memset(&filter, 0, sizeof(Fts3SegFilter));
137494   filter.flags = FTS3_SEGMENT_REQUIRE_POS;
137495   filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
137496 
137497   rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
137498   while( SQLITE_OK==rc ){
137499     rc = sqlite3Fts3SegReaderStep(p, &csr);
137500     if( rc!=SQLITE_ROW ) break;
137501     rc = fts3SegWriterAdd(p, &pWriter, 1,
137502         csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
137503   }
137504   if( rc!=SQLITE_OK ) goto finished;
137505   assert( pWriter );
137506 
137507   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
137508     rc = fts3DeleteSegdir(
137509         p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
137510     );
137511     if( rc!=SQLITE_OK ) goto finished;
137512   }
137513   rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
137514 
137515  finished:
137516   fts3SegWriterFree(pWriter);
137517   sqlite3Fts3SegReaderFinish(&csr);
137518   return rc;
137519 }
137520 
137521 
137522 /*
137523 ** Flush the contents of pendingTerms to level 0 segments.
137524 */
137525 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
137526   int rc = SQLITE_OK;
137527   int i;
137528 
137529   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
137530     rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
137531     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
137532   }
137533   sqlite3Fts3PendingTermsClear(p);
137534 
137535   /* Determine the auto-incr-merge setting if unknown.  If enabled,
137536   ** estimate the number of leaf blocks of content to be written
137537   */
137538   if( rc==SQLITE_OK && p->bHasStat
137539    && p->bAutoincrmerge==0xff && p->nLeafAdd>0
137540   ){
137541     sqlite3_stmt *pStmt = 0;
137542     rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
137543     if( rc==SQLITE_OK ){
137544       sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
137545       rc = sqlite3_step(pStmt);
137546       p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
137547       rc = sqlite3_reset(pStmt);
137548     }
137549   }
137550   return rc;
137551 }
137552 
137553 /*
137554 ** Encode N integers as varints into a blob.
137555 */
137556 static void fts3EncodeIntArray(
137557   int N,             /* The number of integers to encode */
137558   u32 *a,            /* The integer values */
137559   char *zBuf,        /* Write the BLOB here */
137560   int *pNBuf         /* Write number of bytes if zBuf[] used here */
137561 ){
137562   int i, j;
137563   for(i=j=0; i<N; i++){
137564     j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
137565   }
137566   *pNBuf = j;
137567 }
137568 
137569 /*
137570 ** Decode a blob of varints into N integers
137571 */
137572 static void fts3DecodeIntArray(
137573   int N,             /* The number of integers to decode */
137574   u32 *a,            /* Write the integer values */
137575   const char *zBuf,  /* The BLOB containing the varints */
137576   int nBuf           /* size of the BLOB */
137577 ){
137578   int i, j;
137579   UNUSED_PARAMETER(nBuf);
137580   for(i=j=0; i<N; i++){
137581     sqlite3_int64 x;
137582     j += sqlite3Fts3GetVarint(&zBuf[j], &x);
137583     assert(j<=nBuf);
137584     a[i] = (u32)(x & 0xffffffff);
137585   }
137586 }
137587 
137588 /*
137589 ** Insert the sizes (in tokens) for each column of the document
137590 ** with docid equal to p->iPrevDocid.  The sizes are encoded as
137591 ** a blob of varints.
137592 */
137593 static void fts3InsertDocsize(
137594   int *pRC,                       /* Result code */
137595   Fts3Table *p,                   /* Table into which to insert */
137596   u32 *aSz                        /* Sizes of each column, in tokens */
137597 ){
137598   char *pBlob;             /* The BLOB encoding of the document size */
137599   int nBlob;               /* Number of bytes in the BLOB */
137600   sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
137601   int rc;                  /* Result code from subfunctions */
137602 
137603   if( *pRC ) return;
137604   pBlob = sqlite3_malloc( 10*p->nColumn );
137605   if( pBlob==0 ){
137606     *pRC = SQLITE_NOMEM;
137607     return;
137608   }
137609   fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
137610   rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
137611   if( rc ){
137612     sqlite3_free(pBlob);
137613     *pRC = rc;
137614     return;
137615   }
137616   sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
137617   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
137618   sqlite3_step(pStmt);
137619   *pRC = sqlite3_reset(pStmt);
137620 }
137621 
137622 /*
137623 ** Record 0 of the %_stat table contains a blob consisting of N varints,
137624 ** where N is the number of user defined columns in the fts3 table plus
137625 ** two. If nCol is the number of user defined columns, then values of the
137626 ** varints are set as follows:
137627 **
137628 **   Varint 0:       Total number of rows in the table.
137629 **
137630 **   Varint 1..nCol: For each column, the total number of tokens stored in
137631 **                   the column for all rows of the table.
137632 **
137633 **   Varint 1+nCol:  The total size, in bytes, of all text values in all
137634 **                   columns of all rows of the table.
137635 **
137636 */
137637 static void fts3UpdateDocTotals(
137638   int *pRC,                       /* The result code */
137639   Fts3Table *p,                   /* Table being updated */
137640   u32 *aSzIns,                    /* Size increases */
137641   u32 *aSzDel,                    /* Size decreases */
137642   int nChng                       /* Change in the number of documents */
137643 ){
137644   char *pBlob;             /* Storage for BLOB written into %_stat */
137645   int nBlob;               /* Size of BLOB written into %_stat */
137646   u32 *a;                  /* Array of integers that becomes the BLOB */
137647   sqlite3_stmt *pStmt;     /* Statement for reading and writing */
137648   int i;                   /* Loop counter */
137649   int rc;                  /* Result code from subfunctions */
137650 
137651   const int nStat = p->nColumn+2;
137652 
137653   if( *pRC ) return;
137654   a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
137655   if( a==0 ){
137656     *pRC = SQLITE_NOMEM;
137657     return;
137658   }
137659   pBlob = (char*)&a[nStat];
137660   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
137661   if( rc ){
137662     sqlite3_free(a);
137663     *pRC = rc;
137664     return;
137665   }
137666   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
137667   if( sqlite3_step(pStmt)==SQLITE_ROW ){
137668     fts3DecodeIntArray(nStat, a,
137669          sqlite3_column_blob(pStmt, 0),
137670          sqlite3_column_bytes(pStmt, 0));
137671   }else{
137672     memset(a, 0, sizeof(u32)*(nStat) );
137673   }
137674   rc = sqlite3_reset(pStmt);
137675   if( rc!=SQLITE_OK ){
137676     sqlite3_free(a);
137677     *pRC = rc;
137678     return;
137679   }
137680   if( nChng<0 && a[0]<(u32)(-nChng) ){
137681     a[0] = 0;
137682   }else{
137683     a[0] += nChng;
137684   }
137685   for(i=0; i<p->nColumn+1; i++){
137686     u32 x = a[i+1];
137687     if( x+aSzIns[i] < aSzDel[i] ){
137688       x = 0;
137689     }else{
137690       x = x + aSzIns[i] - aSzDel[i];
137691     }
137692     a[i+1] = x;
137693   }
137694   fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
137695   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
137696   if( rc ){
137697     sqlite3_free(a);
137698     *pRC = rc;
137699     return;
137700   }
137701   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
137702   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
137703   sqlite3_step(pStmt);
137704   *pRC = sqlite3_reset(pStmt);
137705   sqlite3_free(a);
137706 }
137707 
137708 /*
137709 ** Merge the entire database so that there is one segment for each
137710 ** iIndex/iLangid combination.
137711 */
137712 static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
137713   int bSeenDone = 0;
137714   int rc;
137715   sqlite3_stmt *pAllLangid = 0;
137716 
137717   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
137718   if( rc==SQLITE_OK ){
137719     int rc2;
137720     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
137721     while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
137722       int i;
137723       int iLangid = sqlite3_column_int(pAllLangid, 0);
137724       for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
137725         rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
137726         if( rc==SQLITE_DONE ){
137727           bSeenDone = 1;
137728           rc = SQLITE_OK;
137729         }
137730       }
137731     }
137732     rc2 = sqlite3_reset(pAllLangid);
137733     if( rc==SQLITE_OK ) rc = rc2;
137734   }
137735 
137736   sqlite3Fts3SegmentsClose(p);
137737   sqlite3Fts3PendingTermsClear(p);
137738 
137739   return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
137740 }
137741 
137742 /*
137743 ** This function is called when the user executes the following statement:
137744 **
137745 **     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
137746 **
137747 ** The entire FTS index is discarded and rebuilt. If the table is one
137748 ** created using the content=xxx option, then the new index is based on
137749 ** the current contents of the xxx table. Otherwise, it is rebuilt based
137750 ** on the contents of the %_content table.
137751 */
137752 static int fts3DoRebuild(Fts3Table *p){
137753   int rc;                         /* Return Code */
137754 
137755   rc = fts3DeleteAll(p, 0);
137756   if( rc==SQLITE_OK ){
137757     u32 *aSz = 0;
137758     u32 *aSzIns = 0;
137759     u32 *aSzDel = 0;
137760     sqlite3_stmt *pStmt = 0;
137761     int nEntry = 0;
137762 
137763     /* Compose and prepare an SQL statement to loop through the content table */
137764     char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
137765     if( !zSql ){
137766       rc = SQLITE_NOMEM;
137767     }else{
137768       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
137769       sqlite3_free(zSql);
137770     }
137771 
137772     if( rc==SQLITE_OK ){
137773       int nByte = sizeof(u32) * (p->nColumn+1)*3;
137774       aSz = (u32 *)sqlite3_malloc(nByte);
137775       if( aSz==0 ){
137776         rc = SQLITE_NOMEM;
137777       }else{
137778         memset(aSz, 0, nByte);
137779         aSzIns = &aSz[p->nColumn+1];
137780         aSzDel = &aSzIns[p->nColumn+1];
137781       }
137782     }
137783 
137784     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
137785       int iCol;
137786       int iLangid = langidFromSelect(p, pStmt);
137787       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
137788       memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
137789       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
137790         if( p->abNotindexed[iCol]==0 ){
137791           const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
137792           rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
137793           aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
137794         }
137795       }
137796       if( p->bHasDocsize ){
137797         fts3InsertDocsize(&rc, p, aSz);
137798       }
137799       if( rc!=SQLITE_OK ){
137800         sqlite3_finalize(pStmt);
137801         pStmt = 0;
137802       }else{
137803         nEntry++;
137804         for(iCol=0; iCol<=p->nColumn; iCol++){
137805           aSzIns[iCol] += aSz[iCol];
137806         }
137807       }
137808     }
137809     if( p->bFts4 ){
137810       fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
137811     }
137812     sqlite3_free(aSz);
137813 
137814     if( pStmt ){
137815       int rc2 = sqlite3_finalize(pStmt);
137816       if( rc==SQLITE_OK ){
137817         rc = rc2;
137818       }
137819     }
137820   }
137821 
137822   return rc;
137823 }
137824 
137825 
137826 /*
137827 ** This function opens a cursor used to read the input data for an
137828 ** incremental merge operation. Specifically, it opens a cursor to scan
137829 ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
137830 ** level iAbsLevel.
137831 */
137832 static int fts3IncrmergeCsr(
137833   Fts3Table *p,                   /* FTS3 table handle */
137834   sqlite3_int64 iAbsLevel,        /* Absolute level to open */
137835   int nSeg,                       /* Number of segments to merge */
137836   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
137837 ){
137838   int rc;                         /* Return Code */
137839   sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */
137840   int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
137841 
137842   /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
137843   memset(pCsr, 0, sizeof(*pCsr));
137844   nByte = sizeof(Fts3SegReader *) * nSeg;
137845   pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
137846 
137847   if( pCsr->apSegment==0 ){
137848     rc = SQLITE_NOMEM;
137849   }else{
137850     memset(pCsr->apSegment, 0, nByte);
137851     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
137852   }
137853   if( rc==SQLITE_OK ){
137854     int i;
137855     int rc2;
137856     sqlite3_bind_int64(pStmt, 1, iAbsLevel);
137857     assert( pCsr->nSegment==0 );
137858     for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
137859       rc = sqlite3Fts3SegReaderNew(i, 0,
137860           sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
137861           sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
137862           sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
137863           sqlite3_column_blob(pStmt, 4),         /* segdir.root */
137864           sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
137865           &pCsr->apSegment[i]
137866       );
137867       pCsr->nSegment++;
137868     }
137869     rc2 = sqlite3_reset(pStmt);
137870     if( rc==SQLITE_OK ) rc = rc2;
137871   }
137872 
137873   return rc;
137874 }
137875 
137876 typedef struct IncrmergeWriter IncrmergeWriter;
137877 typedef struct NodeWriter NodeWriter;
137878 typedef struct Blob Blob;
137879 typedef struct NodeReader NodeReader;
137880 
137881 /*
137882 ** An instance of the following structure is used as a dynamic buffer
137883 ** to build up nodes or other blobs of data in.
137884 **
137885 ** The function blobGrowBuffer() is used to extend the allocation.
137886 */
137887 struct Blob {
137888   char *a;                        /* Pointer to allocation */
137889   int n;                          /* Number of valid bytes of data in a[] */
137890   int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
137891 };
137892 
137893 /*
137894 ** This structure is used to build up buffers containing segment b-tree
137895 ** nodes (blocks).
137896 */
137897 struct NodeWriter {
137898   sqlite3_int64 iBlock;           /* Current block id */
137899   Blob key;                       /* Last key written to the current block */
137900   Blob block;                     /* Current block image */
137901 };
137902 
137903 /*
137904 ** An object of this type contains the state required to create or append
137905 ** to an appendable b-tree segment.
137906 */
137907 struct IncrmergeWriter {
137908   int nLeafEst;                   /* Space allocated for leaf blocks */
137909   int nWork;                      /* Number of leaf pages flushed */
137910   sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
137911   int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
137912   sqlite3_int64 iStart;           /* Block number of first allocated block */
137913   sqlite3_int64 iEnd;             /* Block number of last allocated block */
137914   NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
137915 };
137916 
137917 /*
137918 ** An object of the following type is used to read data from a single
137919 ** FTS segment node. See the following functions:
137920 **
137921 **     nodeReaderInit()
137922 **     nodeReaderNext()
137923 **     nodeReaderRelease()
137924 */
137925 struct NodeReader {
137926   const char *aNode;
137927   int nNode;
137928   int iOff;                       /* Current offset within aNode[] */
137929 
137930   /* Output variables. Containing the current node entry. */
137931   sqlite3_int64 iChild;           /* Pointer to child node */
137932   Blob term;                      /* Current term */
137933   const char *aDoclist;           /* Pointer to doclist */
137934   int nDoclist;                   /* Size of doclist in bytes */
137935 };
137936 
137937 /*
137938 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
137939 ** Otherwise, if the allocation at pBlob->a is not already at least nMin
137940 ** bytes in size, extend (realloc) it to be so.
137941 **
137942 ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
137943 ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
137944 ** to reflect the new size of the pBlob->a[] buffer.
137945 */
137946 static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
137947   if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
137948     int nAlloc = nMin;
137949     char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
137950     if( a ){
137951       pBlob->nAlloc = nAlloc;
137952       pBlob->a = a;
137953     }else{
137954       *pRc = SQLITE_NOMEM;
137955     }
137956   }
137957 }
137958 
137959 /*
137960 ** Attempt to advance the node-reader object passed as the first argument to
137961 ** the next entry on the node.
137962 **
137963 ** Return an error code if an error occurs (SQLITE_NOMEM is possible).
137964 ** Otherwise return SQLITE_OK. If there is no next entry on the node
137965 ** (e.g. because the current entry is the last) set NodeReader->aNode to
137966 ** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
137967 ** variables for the new entry.
137968 */
137969 static int nodeReaderNext(NodeReader *p){
137970   int bFirst = (p->term.n==0);    /* True for first term on the node */
137971   int nPrefix = 0;                /* Bytes to copy from previous term */
137972   int nSuffix = 0;                /* Bytes to append to the prefix */
137973   int rc = SQLITE_OK;             /* Return code */
137974 
137975   assert( p->aNode );
137976   if( p->iChild && bFirst==0 ) p->iChild++;
137977   if( p->iOff>=p->nNode ){
137978     /* EOF */
137979     p->aNode = 0;
137980   }else{
137981     if( bFirst==0 ){
137982       p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
137983     }
137984     p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
137985 
137986     blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
137987     if( rc==SQLITE_OK ){
137988       memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
137989       p->term.n = nPrefix+nSuffix;
137990       p->iOff += nSuffix;
137991       if( p->iChild==0 ){
137992         p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
137993         p->aDoclist = &p->aNode[p->iOff];
137994         p->iOff += p->nDoclist;
137995       }
137996     }
137997   }
137998 
137999   assert( p->iOff<=p->nNode );
138000 
138001   return rc;
138002 }
138003 
138004 /*
138005 ** Release all dynamic resources held by node-reader object *p.
138006 */
138007 static void nodeReaderRelease(NodeReader *p){
138008   sqlite3_free(p->term.a);
138009 }
138010 
138011 /*
138012 ** Initialize a node-reader object to read the node in buffer aNode/nNode.
138013 **
138014 ** If successful, SQLITE_OK is returned and the NodeReader object set to
138015 ** point to the first entry on the node (if any). Otherwise, an SQLite
138016 ** error code is returned.
138017 */
138018 static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
138019   memset(p, 0, sizeof(NodeReader));
138020   p->aNode = aNode;
138021   p->nNode = nNode;
138022 
138023   /* Figure out if this is a leaf or an internal node. */
138024   if( p->aNode[0] ){
138025     /* An internal node. */
138026     p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
138027   }else{
138028     p->iOff = 1;
138029   }
138030 
138031   return nodeReaderNext(p);
138032 }
138033 
138034 /*
138035 ** This function is called while writing an FTS segment each time a leaf o
138036 ** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
138037 ** to be greater than the largest key on the node just written, but smaller
138038 ** than or equal to the first key that will be written to the next leaf
138039 ** node.
138040 **
138041 ** The block id of the leaf node just written to disk may be found in
138042 ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
138043 */
138044 static int fts3IncrmergePush(
138045   Fts3Table *p,                   /* Fts3 table handle */
138046   IncrmergeWriter *pWriter,       /* Writer object */
138047   const char *zTerm,              /* Term to write to internal node */
138048   int nTerm                       /* Bytes at zTerm */
138049 ){
138050   sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
138051   int iLayer;
138052 
138053   assert( nTerm>0 );
138054   for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
138055     sqlite3_int64 iNextPtr = 0;
138056     NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
138057     int rc = SQLITE_OK;
138058     int nPrefix;
138059     int nSuffix;
138060     int nSpace;
138061 
138062     /* Figure out how much space the key will consume if it is written to
138063     ** the current node of layer iLayer. Due to the prefix compression,
138064     ** the space required changes depending on which node the key is to
138065     ** be added to.  */
138066     nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
138067     nSuffix = nTerm - nPrefix;
138068     nSpace  = sqlite3Fts3VarintLen(nPrefix);
138069     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
138070 
138071     if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
138072       /* If the current node of layer iLayer contains zero keys, or if adding
138073       ** the key to it will not cause it to grow to larger than nNodeSize
138074       ** bytes in size, write the key here.  */
138075 
138076       Blob *pBlk = &pNode->block;
138077       if( pBlk->n==0 ){
138078         blobGrowBuffer(pBlk, p->nNodeSize, &rc);
138079         if( rc==SQLITE_OK ){
138080           pBlk->a[0] = (char)iLayer;
138081           pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
138082         }
138083       }
138084       blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
138085       blobGrowBuffer(&pNode->key, nTerm, &rc);
138086 
138087       if( rc==SQLITE_OK ){
138088         if( pNode->key.n ){
138089           pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
138090         }
138091         pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
138092         memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
138093         pBlk->n += nSuffix;
138094 
138095         memcpy(pNode->key.a, zTerm, nTerm);
138096         pNode->key.n = nTerm;
138097       }
138098     }else{
138099       /* Otherwise, flush the current node of layer iLayer to disk.
138100       ** Then allocate a new, empty sibling node. The key will be written
138101       ** into the parent of this node. */
138102       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
138103 
138104       assert( pNode->block.nAlloc>=p->nNodeSize );
138105       pNode->block.a[0] = (char)iLayer;
138106       pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
138107 
138108       iNextPtr = pNode->iBlock;
138109       pNode->iBlock++;
138110       pNode->key.n = 0;
138111     }
138112 
138113     if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
138114     iPtr = iNextPtr;
138115   }
138116 
138117   assert( 0 );
138118   return 0;
138119 }
138120 
138121 /*
138122 ** Append a term and (optionally) doclist to the FTS segment node currently
138123 ** stored in blob *pNode. The node need not contain any terms, but the
138124 ** header must be written before this function is called.
138125 **
138126 ** A node header is a single 0x00 byte for a leaf node, or a height varint
138127 ** followed by the left-hand-child varint for an internal node.
138128 **
138129 ** The term to be appended is passed via arguments zTerm/nTerm. For a
138130 ** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
138131 ** node, both aDoclist and nDoclist must be passed 0.
138132 **
138133 ** If the size of the value in blob pPrev is zero, then this is the first
138134 ** term written to the node. Otherwise, pPrev contains a copy of the
138135 ** previous term. Before this function returns, it is updated to contain a
138136 ** copy of zTerm/nTerm.
138137 **
138138 ** It is assumed that the buffer associated with pNode is already large
138139 ** enough to accommodate the new entry. The buffer associated with pPrev
138140 ** is extended by this function if requrired.
138141 **
138142 ** If an error (i.e. OOM condition) occurs, an SQLite error code is
138143 ** returned. Otherwise, SQLITE_OK.
138144 */
138145 static int fts3AppendToNode(
138146   Blob *pNode,                    /* Current node image to append to */
138147   Blob *pPrev,                    /* Buffer containing previous term written */
138148   const char *zTerm,              /* New term to write */
138149   int nTerm,                      /* Size of zTerm in bytes */
138150   const char *aDoclist,           /* Doclist (or NULL) to write */
138151   int nDoclist                    /* Size of aDoclist in bytes */
138152 ){
138153   int rc = SQLITE_OK;             /* Return code */
138154   int bFirst = (pPrev->n==0);     /* True if this is the first term written */
138155   int nPrefix;                    /* Size of term prefix in bytes */
138156   int nSuffix;                    /* Size of term suffix in bytes */
138157 
138158   /* Node must have already been started. There must be a doclist for a
138159   ** leaf node, and there must not be a doclist for an internal node.  */
138160   assert( pNode->n>0 );
138161   assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
138162 
138163   blobGrowBuffer(pPrev, nTerm, &rc);
138164   if( rc!=SQLITE_OK ) return rc;
138165 
138166   nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
138167   nSuffix = nTerm - nPrefix;
138168   memcpy(pPrev->a, zTerm, nTerm);
138169   pPrev->n = nTerm;
138170 
138171   if( bFirst==0 ){
138172     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
138173   }
138174   pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
138175   memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
138176   pNode->n += nSuffix;
138177 
138178   if( aDoclist ){
138179     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
138180     memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
138181     pNode->n += nDoclist;
138182   }
138183 
138184   assert( pNode->n<=pNode->nAlloc );
138185 
138186   return SQLITE_OK;
138187 }
138188 
138189 /*
138190 ** Append the current term and doclist pointed to by cursor pCsr to the
138191 ** appendable b-tree segment opened for writing by pWriter.
138192 **
138193 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
138194 */
138195 static int fts3IncrmergeAppend(
138196   Fts3Table *p,                   /* Fts3 table handle */
138197   IncrmergeWriter *pWriter,       /* Writer object */
138198   Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
138199 ){
138200   const char *zTerm = pCsr->zTerm;
138201   int nTerm = pCsr->nTerm;
138202   const char *aDoclist = pCsr->aDoclist;
138203   int nDoclist = pCsr->nDoclist;
138204   int rc = SQLITE_OK;           /* Return code */
138205   int nSpace;                   /* Total space in bytes required on leaf */
138206   int nPrefix;                  /* Size of prefix shared with previous term */
138207   int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
138208   NodeWriter *pLeaf;            /* Object used to write leaf nodes */
138209 
138210   pLeaf = &pWriter->aNodeWriter[0];
138211   nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
138212   nSuffix = nTerm - nPrefix;
138213 
138214   nSpace  = sqlite3Fts3VarintLen(nPrefix);
138215   nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
138216   nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
138217 
138218   /* If the current block is not empty, and if adding this term/doclist
138219   ** to the current block would make it larger than Fts3Table.nNodeSize
138220   ** bytes, write this block out to the database. */
138221   if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
138222     rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
138223     pWriter->nWork++;
138224 
138225     /* Add the current term to the parent node. The term added to the
138226     ** parent must:
138227     **
138228     **   a) be greater than the largest term on the leaf node just written
138229     **      to the database (still available in pLeaf->key), and
138230     **
138231     **   b) be less than or equal to the term about to be added to the new
138232     **      leaf node (zTerm/nTerm).
138233     **
138234     ** In other words, it must be the prefix of zTerm 1 byte longer than
138235     ** the common prefix (if any) of zTerm and pWriter->zTerm.
138236     */
138237     if( rc==SQLITE_OK ){
138238       rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
138239     }
138240 
138241     /* Advance to the next output block */
138242     pLeaf->iBlock++;
138243     pLeaf->key.n = 0;
138244     pLeaf->block.n = 0;
138245 
138246     nSuffix = nTerm;
138247     nSpace  = 1;
138248     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
138249     nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
138250   }
138251 
138252   blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
138253 
138254   if( rc==SQLITE_OK ){
138255     if( pLeaf->block.n==0 ){
138256       pLeaf->block.n = 1;
138257       pLeaf->block.a[0] = '\0';
138258     }
138259     rc = fts3AppendToNode(
138260         &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
138261     );
138262   }
138263 
138264   return rc;
138265 }
138266 
138267 /*
138268 ** This function is called to release all dynamic resources held by the
138269 ** merge-writer object pWriter, and if no error has occurred, to flush
138270 ** all outstanding node buffers held by pWriter to disk.
138271 **
138272 ** If *pRc is not SQLITE_OK when this function is called, then no attempt
138273 ** is made to write any data to disk. Instead, this function serves only
138274 ** to release outstanding resources.
138275 **
138276 ** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
138277 ** flushing buffers to disk, *pRc is set to an SQLite error code before
138278 ** returning.
138279 */
138280 static void fts3IncrmergeRelease(
138281   Fts3Table *p,                   /* FTS3 table handle */
138282   IncrmergeWriter *pWriter,       /* Merge-writer object */
138283   int *pRc                        /* IN/OUT: Error code */
138284 ){
138285   int i;                          /* Used to iterate through non-root layers */
138286   int iRoot;                      /* Index of root in pWriter->aNodeWriter */
138287   NodeWriter *pRoot;              /* NodeWriter for root node */
138288   int rc = *pRc;                  /* Error code */
138289 
138290   /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
138291   ** root node. If the segment fits entirely on a single leaf node, iRoot
138292   ** will be set to 0. If the root node is the parent of the leaves, iRoot
138293   ** will be 1. And so on.  */
138294   for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
138295     NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
138296     if( pNode->block.n>0 ) break;
138297     assert( *pRc || pNode->block.nAlloc==0 );
138298     assert( *pRc || pNode->key.nAlloc==0 );
138299     sqlite3_free(pNode->block.a);
138300     sqlite3_free(pNode->key.a);
138301   }
138302 
138303   /* Empty output segment. This is a no-op. */
138304   if( iRoot<0 ) return;
138305 
138306   /* The entire output segment fits on a single node. Normally, this means
138307   ** the node would be stored as a blob in the "root" column of the %_segdir
138308   ** table. However, this is not permitted in this case. The problem is that
138309   ** space has already been reserved in the %_segments table, and so the
138310   ** start_block and end_block fields of the %_segdir table must be populated.
138311   ** And, by design or by accident, released versions of FTS cannot handle
138312   ** segments that fit entirely on the root node with start_block!=0.
138313   **
138314   ** Instead, create a synthetic root node that contains nothing but a
138315   ** pointer to the single content node. So that the segment consists of a
138316   ** single leaf and a single interior (root) node.
138317   **
138318   ** Todo: Better might be to defer allocating space in the %_segments
138319   ** table until we are sure it is needed.
138320   */
138321   if( iRoot==0 ){
138322     Blob *pBlock = &pWriter->aNodeWriter[1].block;
138323     blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
138324     if( rc==SQLITE_OK ){
138325       pBlock->a[0] = 0x01;
138326       pBlock->n = 1 + sqlite3Fts3PutVarint(
138327           &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
138328       );
138329     }
138330     iRoot = 1;
138331   }
138332   pRoot = &pWriter->aNodeWriter[iRoot];
138333 
138334   /* Flush all currently outstanding nodes to disk. */
138335   for(i=0; i<iRoot; i++){
138336     NodeWriter *pNode = &pWriter->aNodeWriter[i];
138337     if( pNode->block.n>0 && rc==SQLITE_OK ){
138338       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
138339     }
138340     sqlite3_free(pNode->block.a);
138341     sqlite3_free(pNode->key.a);
138342   }
138343 
138344   /* Write the %_segdir record. */
138345   if( rc==SQLITE_OK ){
138346     rc = fts3WriteSegdir(p,
138347         pWriter->iAbsLevel+1,               /* level */
138348         pWriter->iIdx,                      /* idx */
138349         pWriter->iStart,                    /* start_block */
138350         pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
138351         pWriter->iEnd,                      /* end_block */
138352         pRoot->block.a, pRoot->block.n      /* root */
138353     );
138354   }
138355   sqlite3_free(pRoot->block.a);
138356   sqlite3_free(pRoot->key.a);
138357 
138358   *pRc = rc;
138359 }
138360 
138361 /*
138362 ** Compare the term in buffer zLhs (size in bytes nLhs) with that in
138363 ** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
138364 ** the other, it is considered to be smaller than the other.
138365 **
138366 ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
138367 ** if it is greater.
138368 */
138369 static int fts3TermCmp(
138370   const char *zLhs, int nLhs,     /* LHS of comparison */
138371   const char *zRhs, int nRhs      /* RHS of comparison */
138372 ){
138373   int nCmp = MIN(nLhs, nRhs);
138374   int res;
138375 
138376   res = memcmp(zLhs, zRhs, nCmp);
138377   if( res==0 ) res = nLhs - nRhs;
138378 
138379   return res;
138380 }
138381 
138382 
138383 /*
138384 ** Query to see if the entry in the %_segments table with blockid iEnd is
138385 ** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
138386 ** returning. Otherwise, set *pbRes to 0.
138387 **
138388 ** Or, if an error occurs while querying the database, return an SQLite
138389 ** error code. The final value of *pbRes is undefined in this case.
138390 **
138391 ** This is used to test if a segment is an "appendable" segment. If it
138392 ** is, then a NULL entry has been inserted into the %_segments table
138393 ** with blockid %_segdir.end_block.
138394 */
138395 static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
138396   int bRes = 0;                   /* Result to set *pbRes to */
138397   sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
138398   int rc;                         /* Return code */
138399 
138400   rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
138401   if( rc==SQLITE_OK ){
138402     sqlite3_bind_int64(pCheck, 1, iEnd);
138403     if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
138404     rc = sqlite3_reset(pCheck);
138405   }
138406 
138407   *pbRes = bRes;
138408   return rc;
138409 }
138410 
138411 /*
138412 ** This function is called when initializing an incremental-merge operation.
138413 ** It checks if the existing segment with index value iIdx at absolute level
138414 ** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
138415 ** merge-writer object *pWriter is initialized to write to it.
138416 **
138417 ** An existing segment can be appended to by an incremental merge if:
138418 **
138419 **   * It was initially created as an appendable segment (with all required
138420 **     space pre-allocated), and
138421 **
138422 **   * The first key read from the input (arguments zKey and nKey) is
138423 **     greater than the largest key currently stored in the potential
138424 **     output segment.
138425 */
138426 static int fts3IncrmergeLoad(
138427   Fts3Table *p,                   /* Fts3 table handle */
138428   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
138429   int iIdx,                       /* Index of candidate output segment */
138430   const char *zKey,               /* First key to write */
138431   int nKey,                       /* Number of bytes in nKey */
138432   IncrmergeWriter *pWriter        /* Populate this object */
138433 ){
138434   int rc;                         /* Return code */
138435   sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
138436 
138437   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
138438   if( rc==SQLITE_OK ){
138439     sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
138440     sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
138441     sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
138442     const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
138443     int nRoot = 0;                /* Size of aRoot[] in bytes */
138444     int rc2;                      /* Return code from sqlite3_reset() */
138445     int bAppendable = 0;          /* Set to true if segment is appendable */
138446 
138447     /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
138448     sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
138449     sqlite3_bind_int(pSelect, 2, iIdx);
138450     if( sqlite3_step(pSelect)==SQLITE_ROW ){
138451       iStart = sqlite3_column_int64(pSelect, 1);
138452       iLeafEnd = sqlite3_column_int64(pSelect, 2);
138453       iEnd = sqlite3_column_int64(pSelect, 3);
138454       nRoot = sqlite3_column_bytes(pSelect, 4);
138455       aRoot = sqlite3_column_blob(pSelect, 4);
138456     }else{
138457       return sqlite3_reset(pSelect);
138458     }
138459 
138460     /* Check for the zero-length marker in the %_segments table */
138461     rc = fts3IsAppendable(p, iEnd, &bAppendable);
138462 
138463     /* Check that zKey/nKey is larger than the largest key the candidate */
138464     if( rc==SQLITE_OK && bAppendable ){
138465       char *aLeaf = 0;
138466       int nLeaf = 0;
138467 
138468       rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
138469       if( rc==SQLITE_OK ){
138470         NodeReader reader;
138471         for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
138472             rc==SQLITE_OK && reader.aNode;
138473             rc = nodeReaderNext(&reader)
138474         ){
138475           assert( reader.aNode );
138476         }
138477         if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
138478           bAppendable = 0;
138479         }
138480         nodeReaderRelease(&reader);
138481       }
138482       sqlite3_free(aLeaf);
138483     }
138484 
138485     if( rc==SQLITE_OK && bAppendable ){
138486       /* It is possible to append to this segment. Set up the IncrmergeWriter
138487       ** object to do so.  */
138488       int i;
138489       int nHeight = (int)aRoot[0];
138490       NodeWriter *pNode;
138491 
138492       pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
138493       pWriter->iStart = iStart;
138494       pWriter->iEnd = iEnd;
138495       pWriter->iAbsLevel = iAbsLevel;
138496       pWriter->iIdx = iIdx;
138497 
138498       for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
138499         pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
138500       }
138501 
138502       pNode = &pWriter->aNodeWriter[nHeight];
138503       pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
138504       blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
138505       if( rc==SQLITE_OK ){
138506         memcpy(pNode->block.a, aRoot, nRoot);
138507         pNode->block.n = nRoot;
138508       }
138509 
138510       for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
138511         NodeReader reader;
138512         pNode = &pWriter->aNodeWriter[i];
138513 
138514         rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
138515         while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
138516         blobGrowBuffer(&pNode->key, reader.term.n, &rc);
138517         if( rc==SQLITE_OK ){
138518           memcpy(pNode->key.a, reader.term.a, reader.term.n);
138519           pNode->key.n = reader.term.n;
138520           if( i>0 ){
138521             char *aBlock = 0;
138522             int nBlock = 0;
138523             pNode = &pWriter->aNodeWriter[i-1];
138524             pNode->iBlock = reader.iChild;
138525             rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
138526             blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
138527             if( rc==SQLITE_OK ){
138528               memcpy(pNode->block.a, aBlock, nBlock);
138529               pNode->block.n = nBlock;
138530             }
138531             sqlite3_free(aBlock);
138532           }
138533         }
138534         nodeReaderRelease(&reader);
138535       }
138536     }
138537 
138538     rc2 = sqlite3_reset(pSelect);
138539     if( rc==SQLITE_OK ) rc = rc2;
138540   }
138541 
138542   return rc;
138543 }
138544 
138545 /*
138546 ** Determine the largest segment index value that exists within absolute
138547 ** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
138548 ** one before returning SQLITE_OK. Or, if there are no segments at all
138549 ** within level iAbsLevel, set *piIdx to zero.
138550 **
138551 ** If an error occurs, return an SQLite error code. The final value of
138552 ** *piIdx is undefined in this case.
138553 */
138554 static int fts3IncrmergeOutputIdx(
138555   Fts3Table *p,                   /* FTS Table handle */
138556   sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
138557   int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
138558 ){
138559   int rc;
138560   sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
138561 
138562   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
138563   if( rc==SQLITE_OK ){
138564     sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
138565     sqlite3_step(pOutputIdx);
138566     *piIdx = sqlite3_column_int(pOutputIdx, 0);
138567     rc = sqlite3_reset(pOutputIdx);
138568   }
138569 
138570   return rc;
138571 }
138572 
138573 /*
138574 ** Allocate an appendable output segment on absolute level iAbsLevel+1
138575 ** with idx value iIdx.
138576 **
138577 ** In the %_segdir table, a segment is defined by the values in three
138578 ** columns:
138579 **
138580 **     start_block
138581 **     leaves_end_block
138582 **     end_block
138583 **
138584 ** When an appendable segment is allocated, it is estimated that the
138585 ** maximum number of leaf blocks that may be required is the sum of the
138586 ** number of leaf blocks consumed by the input segments, plus the number
138587 ** of input segments, multiplied by two. This value is stored in stack
138588 ** variable nLeafEst.
138589 **
138590 ** A total of 16*nLeafEst blocks are allocated when an appendable segment
138591 ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
138592 ** array of leaf nodes starts at the first block allocated. The array
138593 ** of interior nodes that are parents of the leaf nodes start at block
138594 ** (start_block + (1 + end_block - start_block) / 16). And so on.
138595 **
138596 ** In the actual code below, the value "16" is replaced with the
138597 ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
138598 */
138599 static int fts3IncrmergeWriter(
138600   Fts3Table *p,                   /* Fts3 table handle */
138601   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
138602   int iIdx,                       /* Index of new output segment */
138603   Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
138604   IncrmergeWriter *pWriter        /* Populate this object */
138605 ){
138606   int rc;                         /* Return Code */
138607   int i;                          /* Iterator variable */
138608   int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
138609   sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
138610   sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
138611 
138612   /* Calculate nLeafEst. */
138613   rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
138614   if( rc==SQLITE_OK ){
138615     sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
138616     sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
138617     if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
138618       nLeafEst = sqlite3_column_int(pLeafEst, 0);
138619     }
138620     rc = sqlite3_reset(pLeafEst);
138621   }
138622   if( rc!=SQLITE_OK ) return rc;
138623 
138624   /* Calculate the first block to use in the output segment */
138625   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
138626   if( rc==SQLITE_OK ){
138627     if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
138628       pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
138629       pWriter->iEnd = pWriter->iStart - 1;
138630       pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
138631     }
138632     rc = sqlite3_reset(pFirstBlock);
138633   }
138634   if( rc!=SQLITE_OK ) return rc;
138635 
138636   /* Insert the marker in the %_segments table to make sure nobody tries
138637   ** to steal the space just allocated. This is also used to identify
138638   ** appendable segments.  */
138639   rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
138640   if( rc!=SQLITE_OK ) return rc;
138641 
138642   pWriter->iAbsLevel = iAbsLevel;
138643   pWriter->nLeafEst = nLeafEst;
138644   pWriter->iIdx = iIdx;
138645 
138646   /* Set up the array of NodeWriter objects */
138647   for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
138648     pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
138649   }
138650   return SQLITE_OK;
138651 }
138652 
138653 /*
138654 ** Remove an entry from the %_segdir table. This involves running the
138655 ** following two statements:
138656 **
138657 **   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
138658 **   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
138659 **
138660 ** The DELETE statement removes the specific %_segdir level. The UPDATE
138661 ** statement ensures that the remaining segments have contiguously allocated
138662 ** idx values.
138663 */
138664 static int fts3RemoveSegdirEntry(
138665   Fts3Table *p,                   /* FTS3 table handle */
138666   sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
138667   int iIdx                        /* Index of %_segdir entry to delete */
138668 ){
138669   int rc;                         /* Return code */
138670   sqlite3_stmt *pDelete = 0;      /* DELETE statement */
138671 
138672   rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
138673   if( rc==SQLITE_OK ){
138674     sqlite3_bind_int64(pDelete, 1, iAbsLevel);
138675     sqlite3_bind_int(pDelete, 2, iIdx);
138676     sqlite3_step(pDelete);
138677     rc = sqlite3_reset(pDelete);
138678   }
138679 
138680   return rc;
138681 }
138682 
138683 /*
138684 ** One or more segments have just been removed from absolute level iAbsLevel.
138685 ** Update the 'idx' values of the remaining segments in the level so that
138686 ** the idx values are a contiguous sequence starting from 0.
138687 */
138688 static int fts3RepackSegdirLevel(
138689   Fts3Table *p,                   /* FTS3 table handle */
138690   sqlite3_int64 iAbsLevel         /* Absolute level to repack */
138691 ){
138692   int rc;                         /* Return code */
138693   int *aIdx = 0;                  /* Array of remaining idx values */
138694   int nIdx = 0;                   /* Valid entries in aIdx[] */
138695   int nAlloc = 0;                 /* Allocated size of aIdx[] */
138696   int i;                          /* Iterator variable */
138697   sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
138698   sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
138699 
138700   rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
138701   if( rc==SQLITE_OK ){
138702     int rc2;
138703     sqlite3_bind_int64(pSelect, 1, iAbsLevel);
138704     while( SQLITE_ROW==sqlite3_step(pSelect) ){
138705       if( nIdx>=nAlloc ){
138706         int *aNew;
138707         nAlloc += 16;
138708         aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
138709         if( !aNew ){
138710           rc = SQLITE_NOMEM;
138711           break;
138712         }
138713         aIdx = aNew;
138714       }
138715       aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
138716     }
138717     rc2 = sqlite3_reset(pSelect);
138718     if( rc==SQLITE_OK ) rc = rc2;
138719   }
138720 
138721   if( rc==SQLITE_OK ){
138722     rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
138723   }
138724   if( rc==SQLITE_OK ){
138725     sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
138726   }
138727 
138728   assert( p->bIgnoreSavepoint==0 );
138729   p->bIgnoreSavepoint = 1;
138730   for(i=0; rc==SQLITE_OK && i<nIdx; i++){
138731     if( aIdx[i]!=i ){
138732       sqlite3_bind_int(pUpdate, 3, aIdx[i]);
138733       sqlite3_bind_int(pUpdate, 1, i);
138734       sqlite3_step(pUpdate);
138735       rc = sqlite3_reset(pUpdate);
138736     }
138737   }
138738   p->bIgnoreSavepoint = 0;
138739 
138740   sqlite3_free(aIdx);
138741   return rc;
138742 }
138743 
138744 static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
138745   pNode->a[0] = (char)iHeight;
138746   if( iChild ){
138747     assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
138748     pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
138749   }else{
138750     assert( pNode->nAlloc>=1 );
138751     pNode->n = 1;
138752   }
138753 }
138754 
138755 /*
138756 ** The first two arguments are a pointer to and the size of a segment b-tree
138757 ** node. The node may be a leaf or an internal node.
138758 **
138759 ** This function creates a new node image in blob object *pNew by copying
138760 ** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
138761 ** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
138762 */
138763 static int fts3TruncateNode(
138764   const char *aNode,              /* Current node image */
138765   int nNode,                      /* Size of aNode in bytes */
138766   Blob *pNew,                     /* OUT: Write new node image here */
138767   const char *zTerm,              /* Omit all terms smaller than this */
138768   int nTerm,                      /* Size of zTerm in bytes */
138769   sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
138770 ){
138771   NodeReader reader;              /* Reader object */
138772   Blob prev = {0, 0, 0};          /* Previous term written to new node */
138773   int rc = SQLITE_OK;             /* Return code */
138774   int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
138775 
138776   /* Allocate required output space */
138777   blobGrowBuffer(pNew, nNode, &rc);
138778   if( rc!=SQLITE_OK ) return rc;
138779   pNew->n = 0;
138780 
138781   /* Populate new node buffer */
138782   for(rc = nodeReaderInit(&reader, aNode, nNode);
138783       rc==SQLITE_OK && reader.aNode;
138784       rc = nodeReaderNext(&reader)
138785   ){
138786     if( pNew->n==0 ){
138787       int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
138788       if( res<0 || (bLeaf==0 && res==0) ) continue;
138789       fts3StartNode(pNew, (int)aNode[0], reader.iChild);
138790       *piBlock = reader.iChild;
138791     }
138792     rc = fts3AppendToNode(
138793         pNew, &prev, reader.term.a, reader.term.n,
138794         reader.aDoclist, reader.nDoclist
138795     );
138796     if( rc!=SQLITE_OK ) break;
138797   }
138798   if( pNew->n==0 ){
138799     fts3StartNode(pNew, (int)aNode[0], reader.iChild);
138800     *piBlock = reader.iChild;
138801   }
138802   assert( pNew->n<=pNew->nAlloc );
138803 
138804   nodeReaderRelease(&reader);
138805   sqlite3_free(prev.a);
138806   return rc;
138807 }
138808 
138809 /*
138810 ** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
138811 ** level iAbsLevel. This may involve deleting entries from the %_segments
138812 ** table, and modifying existing entries in both the %_segments and %_segdir
138813 ** tables.
138814 **
138815 ** SQLITE_OK is returned if the segment is updated successfully. Or an
138816 ** SQLite error code otherwise.
138817 */
138818 static int fts3TruncateSegment(
138819   Fts3Table *p,                   /* FTS3 table handle */
138820   sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
138821   int iIdx,                       /* Index within level of segment to modify */
138822   const char *zTerm,              /* Remove terms smaller than this */
138823   int nTerm                      /* Number of bytes in buffer zTerm */
138824 ){
138825   int rc = SQLITE_OK;             /* Return code */
138826   Blob root = {0,0,0};            /* New root page image */
138827   Blob block = {0,0,0};           /* Buffer used for any other block */
138828   sqlite3_int64 iBlock = 0;       /* Block id */
138829   sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
138830   sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
138831   sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
138832 
138833   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
138834   if( rc==SQLITE_OK ){
138835     int rc2;                      /* sqlite3_reset() return code */
138836     sqlite3_bind_int64(pFetch, 1, iAbsLevel);
138837     sqlite3_bind_int(pFetch, 2, iIdx);
138838     if( SQLITE_ROW==sqlite3_step(pFetch) ){
138839       const char *aRoot = sqlite3_column_blob(pFetch, 4);
138840       int nRoot = sqlite3_column_bytes(pFetch, 4);
138841       iOldStart = sqlite3_column_int64(pFetch, 1);
138842       rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
138843     }
138844     rc2 = sqlite3_reset(pFetch);
138845     if( rc==SQLITE_OK ) rc = rc2;
138846   }
138847 
138848   while( rc==SQLITE_OK && iBlock ){
138849     char *aBlock = 0;
138850     int nBlock = 0;
138851     iNewStart = iBlock;
138852 
138853     rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
138854     if( rc==SQLITE_OK ){
138855       rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
138856     }
138857     if( rc==SQLITE_OK ){
138858       rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
138859     }
138860     sqlite3_free(aBlock);
138861   }
138862 
138863   /* Variable iNewStart now contains the first valid leaf node. */
138864   if( rc==SQLITE_OK && iNewStart ){
138865     sqlite3_stmt *pDel = 0;
138866     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
138867     if( rc==SQLITE_OK ){
138868       sqlite3_bind_int64(pDel, 1, iOldStart);
138869       sqlite3_bind_int64(pDel, 2, iNewStart-1);
138870       sqlite3_step(pDel);
138871       rc = sqlite3_reset(pDel);
138872     }
138873   }
138874 
138875   if( rc==SQLITE_OK ){
138876     sqlite3_stmt *pChomp = 0;
138877     rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
138878     if( rc==SQLITE_OK ){
138879       sqlite3_bind_int64(pChomp, 1, iNewStart);
138880       sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
138881       sqlite3_bind_int64(pChomp, 3, iAbsLevel);
138882       sqlite3_bind_int(pChomp, 4, iIdx);
138883       sqlite3_step(pChomp);
138884       rc = sqlite3_reset(pChomp);
138885     }
138886   }
138887 
138888   sqlite3_free(root.a);
138889   sqlite3_free(block.a);
138890   return rc;
138891 }
138892 
138893 /*
138894 ** This function is called after an incrmental-merge operation has run to
138895 ** merge (or partially merge) two or more segments from absolute level
138896 ** iAbsLevel.
138897 **
138898 ** Each input segment is either removed from the db completely (if all of
138899 ** its data was copied to the output segment by the incrmerge operation)
138900 ** or modified in place so that it no longer contains those entries that
138901 ** have been duplicated in the output segment.
138902 */
138903 static int fts3IncrmergeChomp(
138904   Fts3Table *p,                   /* FTS table handle */
138905   sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
138906   Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
138907   int *pnRem                      /* Number of segments not deleted */
138908 ){
138909   int i;
138910   int nRem = 0;
138911   int rc = SQLITE_OK;
138912 
138913   for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
138914     Fts3SegReader *pSeg = 0;
138915     int j;
138916 
138917     /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
138918     ** somewhere in the pCsr->apSegment[] array.  */
138919     for(j=0; ALWAYS(j<pCsr->nSegment); j++){
138920       pSeg = pCsr->apSegment[j];
138921       if( pSeg->iIdx==i ) break;
138922     }
138923     assert( j<pCsr->nSegment && pSeg->iIdx==i );
138924 
138925     if( pSeg->aNode==0 ){
138926       /* Seg-reader is at EOF. Remove the entire input segment. */
138927       rc = fts3DeleteSegment(p, pSeg);
138928       if( rc==SQLITE_OK ){
138929         rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
138930       }
138931       *pnRem = 0;
138932     }else{
138933       /* The incremental merge did not copy all the data from this
138934       ** segment to the upper level. The segment is modified in place
138935       ** so that it contains no keys smaller than zTerm/nTerm. */
138936       const char *zTerm = pSeg->zTerm;
138937       int nTerm = pSeg->nTerm;
138938       rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
138939       nRem++;
138940     }
138941   }
138942 
138943   if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
138944     rc = fts3RepackSegdirLevel(p, iAbsLevel);
138945   }
138946 
138947   *pnRem = nRem;
138948   return rc;
138949 }
138950 
138951 /*
138952 ** Store an incr-merge hint in the database.
138953 */
138954 static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
138955   sqlite3_stmt *pReplace = 0;
138956   int rc;                         /* Return code */
138957 
138958   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
138959   if( rc==SQLITE_OK ){
138960     sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
138961     sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
138962     sqlite3_step(pReplace);
138963     rc = sqlite3_reset(pReplace);
138964   }
138965 
138966   return rc;
138967 }
138968 
138969 /*
138970 ** Load an incr-merge hint from the database. The incr-merge hint, if one
138971 ** exists, is stored in the rowid==1 row of the %_stat table.
138972 **
138973 ** If successful, populate blob *pHint with the value read from the %_stat
138974 ** table and return SQLITE_OK. Otherwise, if an error occurs, return an
138975 ** SQLite error code.
138976 */
138977 static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
138978   sqlite3_stmt *pSelect = 0;
138979   int rc;
138980 
138981   pHint->n = 0;
138982   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
138983   if( rc==SQLITE_OK ){
138984     int rc2;
138985     sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
138986     if( SQLITE_ROW==sqlite3_step(pSelect) ){
138987       const char *aHint = sqlite3_column_blob(pSelect, 0);
138988       int nHint = sqlite3_column_bytes(pSelect, 0);
138989       if( aHint ){
138990         blobGrowBuffer(pHint, nHint, &rc);
138991         if( rc==SQLITE_OK ){
138992           memcpy(pHint->a, aHint, nHint);
138993           pHint->n = nHint;
138994         }
138995       }
138996     }
138997     rc2 = sqlite3_reset(pSelect);
138998     if( rc==SQLITE_OK ) rc = rc2;
138999   }
139000 
139001   return rc;
139002 }
139003 
139004 /*
139005 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
139006 ** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
139007 ** consists of two varints, the absolute level number of the input segments
139008 ** and the number of input segments.
139009 **
139010 ** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
139011 ** set *pRc to an SQLite error code before returning.
139012 */
139013 static void fts3IncrmergeHintPush(
139014   Blob *pHint,                    /* Hint blob to append to */
139015   i64 iAbsLevel,                  /* First varint to store in hint */
139016   int nInput,                     /* Second varint to store in hint */
139017   int *pRc                        /* IN/OUT: Error code */
139018 ){
139019   blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
139020   if( *pRc==SQLITE_OK ){
139021     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
139022     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
139023   }
139024 }
139025 
139026 /*
139027 ** Read the last entry (most recently pushed) from the hint blob *pHint
139028 ** and then remove the entry. Write the two values read to *piAbsLevel and
139029 ** *pnInput before returning.
139030 **
139031 ** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
139032 ** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
139033 */
139034 static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
139035   const int nHint = pHint->n;
139036   int i;
139037 
139038   i = pHint->n-2;
139039   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
139040   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
139041 
139042   pHint->n = i;
139043   i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
139044   i += fts3GetVarint32(&pHint->a[i], pnInput);
139045   if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
139046 
139047   return SQLITE_OK;
139048 }
139049 
139050 
139051 /*
139052 ** Attempt an incremental merge that writes nMerge leaf blocks.
139053 **
139054 ** Incremental merges happen nMin segments at a time. The two
139055 ** segments to be merged are the nMin oldest segments (the ones with
139056 ** the smallest indexes) in the highest level that contains at least
139057 ** nMin segments. Multiple merges might occur in an attempt to write the
139058 ** quota of nMerge leaf blocks.
139059 */
139060 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
139061   int rc;                         /* Return code */
139062   int nRem = nMerge;              /* Number of leaf pages yet to  be written */
139063   Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
139064   Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
139065   IncrmergeWriter *pWriter;       /* Writer object */
139066   int nSeg = 0;                   /* Number of input segments */
139067   sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
139068   Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
139069   int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
139070 
139071   /* Allocate space for the cursor, filter and writer objects */
139072   const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
139073   pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
139074   if( !pWriter ) return SQLITE_NOMEM;
139075   pFilter = (Fts3SegFilter *)&pWriter[1];
139076   pCsr = (Fts3MultiSegReader *)&pFilter[1];
139077 
139078   rc = fts3IncrmergeHintLoad(p, &hint);
139079   while( rc==SQLITE_OK && nRem>0 ){
139080     const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
139081     sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
139082     int bUseHint = 0;             /* True if attempting to append */
139083 
139084     /* Search the %_segdir table for the absolute level with the smallest
139085     ** relative level number that contains at least nMin segments, if any.
139086     ** If one is found, set iAbsLevel to the absolute level number and
139087     ** nSeg to nMin. If no level with at least nMin segments can be found,
139088     ** set nSeg to -1.
139089     */
139090     rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
139091     sqlite3_bind_int(pFindLevel, 1, nMin);
139092     if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
139093       iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
139094       nSeg = nMin;
139095     }else{
139096       nSeg = -1;
139097     }
139098     rc = sqlite3_reset(pFindLevel);
139099 
139100     /* If the hint read from the %_stat table is not empty, check if the
139101     ** last entry in it specifies a relative level smaller than or equal
139102     ** to the level identified by the block above (if any). If so, this
139103     ** iteration of the loop will work on merging at the hinted level.
139104     */
139105     if( rc==SQLITE_OK && hint.n ){
139106       int nHint = hint.n;
139107       sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
139108       int nHintSeg = 0;                     /* Hint number of segments */
139109 
139110       rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
139111       if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
139112         iAbsLevel = iHintAbsLevel;
139113         nSeg = nHintSeg;
139114         bUseHint = 1;
139115         bDirtyHint = 1;
139116       }else{
139117         /* This undoes the effect of the HintPop() above - so that no entry
139118         ** is removed from the hint blob.  */
139119         hint.n = nHint;
139120       }
139121     }
139122 
139123     /* If nSeg is less that zero, then there is no level with at least
139124     ** nMin segments and no hint in the %_stat table. No work to do.
139125     ** Exit early in this case.  */
139126     if( nSeg<0 ) break;
139127 
139128     /* Open a cursor to iterate through the contents of the oldest nSeg
139129     ** indexes of absolute level iAbsLevel. If this cursor is opened using
139130     ** the 'hint' parameters, it is possible that there are less than nSeg
139131     ** segments available in level iAbsLevel. In this case, no work is
139132     ** done on iAbsLevel - fall through to the next iteration of the loop
139133     ** to start work on some other level.  */
139134     memset(pWriter, 0, nAlloc);
139135     pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
139136     if( rc==SQLITE_OK ){
139137       rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
139138     }
139139     if( SQLITE_OK==rc && pCsr->nSegment==nSeg
139140      && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
139141      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
139142     ){
139143       int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
139144       rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
139145       if( rc==SQLITE_OK ){
139146         if( bUseHint && iIdx>0 ){
139147           const char *zKey = pCsr->zTerm;
139148           int nKey = pCsr->nTerm;
139149           rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
139150         }else{
139151           rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
139152         }
139153       }
139154 
139155       if( rc==SQLITE_OK && pWriter->nLeafEst ){
139156         fts3LogMerge(nSeg, iAbsLevel);
139157         do {
139158           rc = fts3IncrmergeAppend(p, pWriter, pCsr);
139159           if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
139160           if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
139161         }while( rc==SQLITE_ROW );
139162 
139163         /* Update or delete the input segments */
139164         if( rc==SQLITE_OK ){
139165           nRem -= (1 + pWriter->nWork);
139166           rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
139167           if( nSeg!=0 ){
139168             bDirtyHint = 1;
139169             fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
139170           }
139171         }
139172       }
139173 
139174       fts3IncrmergeRelease(p, pWriter, &rc);
139175     }
139176 
139177     sqlite3Fts3SegReaderFinish(pCsr);
139178   }
139179 
139180   /* Write the hint values into the %_stat table for the next incr-merger */
139181   if( bDirtyHint && rc==SQLITE_OK ){
139182     rc = fts3IncrmergeHintStore(p, &hint);
139183   }
139184 
139185   sqlite3_free(pWriter);
139186   sqlite3_free(hint.a);
139187   return rc;
139188 }
139189 
139190 /*
139191 ** Convert the text beginning at *pz into an integer and return
139192 ** its value.  Advance *pz to point to the first character past
139193 ** the integer.
139194 */
139195 static int fts3Getint(const char **pz){
139196   const char *z = *pz;
139197   int i = 0;
139198   while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
139199   *pz = z;
139200   return i;
139201 }
139202 
139203 /*
139204 ** Process statements of the form:
139205 **
139206 **    INSERT INTO table(table) VALUES('merge=A,B');
139207 **
139208 ** A and B are integers that decode to be the number of leaf pages
139209 ** written for the merge, and the minimum number of segments on a level
139210 ** before it will be selected for a merge, respectively.
139211 */
139212 static int fts3DoIncrmerge(
139213   Fts3Table *p,                   /* FTS3 table handle */
139214   const char *zParam              /* Nul-terminated string containing "A,B" */
139215 ){
139216   int rc;
139217   int nMin = (FTS3_MERGE_COUNT / 2);
139218   int nMerge = 0;
139219   const char *z = zParam;
139220 
139221   /* Read the first integer value */
139222   nMerge = fts3Getint(&z);
139223 
139224   /* If the first integer value is followed by a ',',  read the second
139225   ** integer value. */
139226   if( z[0]==',' && z[1]!='\0' ){
139227     z++;
139228     nMin = fts3Getint(&z);
139229   }
139230 
139231   if( z[0]!='\0' || nMin<2 ){
139232     rc = SQLITE_ERROR;
139233   }else{
139234     rc = SQLITE_OK;
139235     if( !p->bHasStat ){
139236       assert( p->bFts4==0 );
139237       sqlite3Fts3CreateStatTable(&rc, p);
139238     }
139239     if( rc==SQLITE_OK ){
139240       rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
139241     }
139242     sqlite3Fts3SegmentsClose(p);
139243   }
139244   return rc;
139245 }
139246 
139247 /*
139248 ** Process statements of the form:
139249 **
139250 **    INSERT INTO table(table) VALUES('automerge=X');
139251 **
139252 ** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
139253 ** turn it on.  The setting is persistent.
139254 */
139255 static int fts3DoAutoincrmerge(
139256   Fts3Table *p,                   /* FTS3 table handle */
139257   const char *zParam              /* Nul-terminated string containing boolean */
139258 ){
139259   int rc = SQLITE_OK;
139260   sqlite3_stmt *pStmt = 0;
139261   p->bAutoincrmerge = fts3Getint(&zParam)!=0;
139262   if( !p->bHasStat ){
139263     assert( p->bFts4==0 );
139264     sqlite3Fts3CreateStatTable(&rc, p);
139265     if( rc ) return rc;
139266   }
139267   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
139268   if( rc ) return rc;
139269   sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
139270   sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
139271   sqlite3_step(pStmt);
139272   rc = sqlite3_reset(pStmt);
139273   return rc;
139274 }
139275 
139276 /*
139277 ** Return a 64-bit checksum for the FTS index entry specified by the
139278 ** arguments to this function.
139279 */
139280 static u64 fts3ChecksumEntry(
139281   const char *zTerm,              /* Pointer to buffer containing term */
139282   int nTerm,                      /* Size of zTerm in bytes */
139283   int iLangid,                    /* Language id for current row */
139284   int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
139285   i64 iDocid,                     /* Docid for current row. */
139286   int iCol,                       /* Column number */
139287   int iPos                        /* Position */
139288 ){
139289   int i;
139290   u64 ret = (u64)iDocid;
139291 
139292   ret += (ret<<3) + iLangid;
139293   ret += (ret<<3) + iIndex;
139294   ret += (ret<<3) + iCol;
139295   ret += (ret<<3) + iPos;
139296   for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
139297 
139298   return ret;
139299 }
139300 
139301 /*
139302 ** Return a checksum of all entries in the FTS index that correspond to
139303 ** language id iLangid. The checksum is calculated by XORing the checksums
139304 ** of each individual entry (see fts3ChecksumEntry()) together.
139305 **
139306 ** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
139307 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
139308 ** return value is undefined in this case.
139309 */
139310 static u64 fts3ChecksumIndex(
139311   Fts3Table *p,                   /* FTS3 table handle */
139312   int iLangid,                    /* Language id to return cksum for */
139313   int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
139314   int *pRc                        /* OUT: Return code */
139315 ){
139316   Fts3SegFilter filter;
139317   Fts3MultiSegReader csr;
139318   int rc;
139319   u64 cksum = 0;
139320 
139321   assert( *pRc==SQLITE_OK );
139322 
139323   memset(&filter, 0, sizeof(filter));
139324   memset(&csr, 0, sizeof(csr));
139325   filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
139326   filter.flags |= FTS3_SEGMENT_SCAN;
139327 
139328   rc = sqlite3Fts3SegReaderCursor(
139329       p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
139330   );
139331   if( rc==SQLITE_OK ){
139332     rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
139333   }
139334 
139335   if( rc==SQLITE_OK ){
139336     while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
139337       char *pCsr = csr.aDoclist;
139338       char *pEnd = &pCsr[csr.nDoclist];
139339 
139340       i64 iDocid = 0;
139341       i64 iCol = 0;
139342       i64 iPos = 0;
139343 
139344       pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
139345       while( pCsr<pEnd ){
139346         i64 iVal = 0;
139347         pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
139348         if( pCsr<pEnd ){
139349           if( iVal==0 || iVal==1 ){
139350             iCol = 0;
139351             iPos = 0;
139352             if( iVal ){
139353               pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
139354             }else{
139355               pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
139356               iDocid += iVal;
139357             }
139358           }else{
139359             iPos += (iVal - 2);
139360             cksum = cksum ^ fts3ChecksumEntry(
139361                 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
139362                 (int)iCol, (int)iPos
139363             );
139364           }
139365         }
139366       }
139367     }
139368   }
139369   sqlite3Fts3SegReaderFinish(&csr);
139370 
139371   *pRc = rc;
139372   return cksum;
139373 }
139374 
139375 /*
139376 ** Check if the contents of the FTS index match the current contents of the
139377 ** content table. If no error occurs and the contents do match, set *pbOk
139378 ** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
139379 ** to false before returning.
139380 **
139381 ** If an error occurs (e.g. an OOM or IO error), return an SQLite error
139382 ** code. The final value of *pbOk is undefined in this case.
139383 */
139384 static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
139385   int rc = SQLITE_OK;             /* Return code */
139386   u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
139387   u64 cksum2 = 0;                 /* Checksum based on %_content contents */
139388   sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
139389 
139390   /* This block calculates the checksum according to the FTS index. */
139391   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
139392   if( rc==SQLITE_OK ){
139393     int rc2;
139394     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
139395     while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
139396       int iLangid = sqlite3_column_int(pAllLangid, 0);
139397       int i;
139398       for(i=0; i<p->nIndex; i++){
139399         cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
139400       }
139401     }
139402     rc2 = sqlite3_reset(pAllLangid);
139403     if( rc==SQLITE_OK ) rc = rc2;
139404   }
139405 
139406   /* This block calculates the checksum according to the %_content table */
139407   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
139408   if( rc==SQLITE_OK ){
139409     sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
139410     sqlite3_stmt *pStmt = 0;
139411     char *zSql;
139412 
139413     zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
139414     if( !zSql ){
139415       rc = SQLITE_NOMEM;
139416     }else{
139417       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
139418       sqlite3_free(zSql);
139419     }
139420 
139421     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
139422       i64 iDocid = sqlite3_column_int64(pStmt, 0);
139423       int iLang = langidFromSelect(p, pStmt);
139424       int iCol;
139425 
139426       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
139427         const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
139428         int nText = sqlite3_column_bytes(pStmt, iCol+1);
139429         sqlite3_tokenizer_cursor *pT = 0;
139430 
139431         rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
139432         while( rc==SQLITE_OK ){
139433           char const *zToken;       /* Buffer containing token */
139434           int nToken = 0;           /* Number of bytes in token */
139435           int iDum1 = 0, iDum2 = 0; /* Dummy variables */
139436           int iPos = 0;             /* Position of token in zText */
139437 
139438           rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
139439           if( rc==SQLITE_OK ){
139440             int i;
139441             cksum2 = cksum2 ^ fts3ChecksumEntry(
139442                 zToken, nToken, iLang, 0, iDocid, iCol, iPos
139443             );
139444             for(i=1; i<p->nIndex; i++){
139445               if( p->aIndex[i].nPrefix<=nToken ){
139446                 cksum2 = cksum2 ^ fts3ChecksumEntry(
139447                   zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
139448                 );
139449               }
139450             }
139451           }
139452         }
139453         if( pT ) pModule->xClose(pT);
139454         if( rc==SQLITE_DONE ) rc = SQLITE_OK;
139455       }
139456     }
139457 
139458     sqlite3_finalize(pStmt);
139459   }
139460 
139461   *pbOk = (cksum1==cksum2);
139462   return rc;
139463 }
139464 
139465 /*
139466 ** Run the integrity-check. If no error occurs and the current contents of
139467 ** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
139468 ** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
139469 **
139470 ** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
139471 ** error code.
139472 **
139473 ** The integrity-check works as follows. For each token and indexed token
139474 ** prefix in the document set, a 64-bit checksum is calculated (by code
139475 ** in fts3ChecksumEntry()) based on the following:
139476 **
139477 **     + The index number (0 for the main index, 1 for the first prefix
139478 **       index etc.),
139479 **     + The token (or token prefix) text itself,
139480 **     + The language-id of the row it appears in,
139481 **     + The docid of the row it appears in,
139482 **     + The column it appears in, and
139483 **     + The tokens position within that column.
139484 **
139485 ** The checksums for all entries in the index are XORed together to create
139486 ** a single checksum for the entire index.
139487 **
139488 ** The integrity-check code calculates the same checksum in two ways:
139489 **
139490 **     1. By scanning the contents of the FTS index, and
139491 **     2. By scanning and tokenizing the content table.
139492 **
139493 ** If the two checksums are identical, the integrity-check is deemed to have
139494 ** passed.
139495 */
139496 static int fts3DoIntegrityCheck(
139497   Fts3Table *p                    /* FTS3 table handle */
139498 ){
139499   int rc;
139500   int bOk = 0;
139501   rc = fts3IntegrityCheck(p, &bOk);
139502   if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
139503   return rc;
139504 }
139505 
139506 /*
139507 ** Handle a 'special' INSERT of the form:
139508 **
139509 **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
139510 **
139511 ** Argument pVal contains the result of <expr>. Currently the only
139512 ** meaningful value to insert is the text 'optimize'.
139513 */
139514 static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
139515   int rc;                         /* Return Code */
139516   const char *zVal = (const char *)sqlite3_value_text(pVal);
139517   int nVal = sqlite3_value_bytes(pVal);
139518 
139519   if( !zVal ){
139520     return SQLITE_NOMEM;
139521   }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
139522     rc = fts3DoOptimize(p, 0);
139523   }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
139524     rc = fts3DoRebuild(p);
139525   }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
139526     rc = fts3DoIntegrityCheck(p);
139527   }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
139528     rc = fts3DoIncrmerge(p, &zVal[6]);
139529   }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
139530     rc = fts3DoAutoincrmerge(p, &zVal[10]);
139531 #ifdef SQLITE_TEST
139532   }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
139533     p->nNodeSize = atoi(&zVal[9]);
139534     rc = SQLITE_OK;
139535   }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
139536     p->nMaxPendingData = atoi(&zVal[11]);
139537     rc = SQLITE_OK;
139538   }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
139539     p->bNoIncrDoclist = atoi(&zVal[21]);
139540     rc = SQLITE_OK;
139541 #endif
139542   }else{
139543     rc = SQLITE_ERROR;
139544   }
139545 
139546   return rc;
139547 }
139548 
139549 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
139550 /*
139551 ** Delete all cached deferred doclists. Deferred doclists are cached
139552 ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
139553 */
139554 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
139555   Fts3DeferredToken *pDef;
139556   for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
139557     fts3PendingListDelete(pDef->pList);
139558     pDef->pList = 0;
139559   }
139560 }
139561 
139562 /*
139563 ** Free all entries in the pCsr->pDeffered list. Entries are added to
139564 ** this list using sqlite3Fts3DeferToken().
139565 */
139566 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
139567   Fts3DeferredToken *pDef;
139568   Fts3DeferredToken *pNext;
139569   for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
139570     pNext = pDef->pNext;
139571     fts3PendingListDelete(pDef->pList);
139572     sqlite3_free(pDef);
139573   }
139574   pCsr->pDeferred = 0;
139575 }
139576 
139577 /*
139578 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
139579 ** based on the row that pCsr currently points to.
139580 **
139581 ** A deferred-doclist is like any other doclist with position information
139582 ** included, except that it only contains entries for a single row of the
139583 ** table, not for all rows.
139584 */
139585 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
139586   int rc = SQLITE_OK;             /* Return code */
139587   if( pCsr->pDeferred ){
139588     int i;                        /* Used to iterate through table columns */
139589     sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
139590     Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
139591 
139592     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
139593     sqlite3_tokenizer *pT = p->pTokenizer;
139594     sqlite3_tokenizer_module const *pModule = pT->pModule;
139595 
139596     assert( pCsr->isRequireSeek==0 );
139597     iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
139598 
139599     for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
139600       if( p->abNotindexed[i]==0 ){
139601         const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
139602         sqlite3_tokenizer_cursor *pTC = 0;
139603 
139604         rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
139605         while( rc==SQLITE_OK ){
139606           char const *zToken;       /* Buffer containing token */
139607           int nToken = 0;           /* Number of bytes in token */
139608           int iDum1 = 0, iDum2 = 0; /* Dummy variables */
139609           int iPos = 0;             /* Position of token in zText */
139610 
139611           rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
139612           for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
139613             Fts3PhraseToken *pPT = pDef->pToken;
139614             if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
139615                 && (pPT->bFirst==0 || iPos==0)
139616                 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
139617                 && (0==memcmp(zToken, pPT->z, pPT->n))
139618               ){
139619               fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
139620             }
139621           }
139622         }
139623         if( pTC ) pModule->xClose(pTC);
139624         if( rc==SQLITE_DONE ) rc = SQLITE_OK;
139625       }
139626     }
139627 
139628     for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
139629       if( pDef->pList ){
139630         rc = fts3PendingListAppendVarint(&pDef->pList, 0);
139631       }
139632     }
139633   }
139634 
139635   return rc;
139636 }
139637 
139638 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
139639   Fts3DeferredToken *p,
139640   char **ppData,
139641   int *pnData
139642 ){
139643   char *pRet;
139644   int nSkip;
139645   sqlite3_int64 dummy;
139646 
139647   *ppData = 0;
139648   *pnData = 0;
139649 
139650   if( p->pList==0 ){
139651     return SQLITE_OK;
139652   }
139653 
139654   pRet = (char *)sqlite3_malloc(p->pList->nData);
139655   if( !pRet ) return SQLITE_NOMEM;
139656 
139657   nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
139658   *pnData = p->pList->nData - nSkip;
139659   *ppData = pRet;
139660 
139661   memcpy(pRet, &p->pList->aData[nSkip], *pnData);
139662   return SQLITE_OK;
139663 }
139664 
139665 /*
139666 ** Add an entry for token pToken to the pCsr->pDeferred list.
139667 */
139668 SQLITE_PRIVATE int sqlite3Fts3DeferToken(
139669   Fts3Cursor *pCsr,               /* Fts3 table cursor */
139670   Fts3PhraseToken *pToken,        /* Token to defer */
139671   int iCol                        /* Column that token must appear in (or -1) */
139672 ){
139673   Fts3DeferredToken *pDeferred;
139674   pDeferred = sqlite3_malloc(sizeof(*pDeferred));
139675   if( !pDeferred ){
139676     return SQLITE_NOMEM;
139677   }
139678   memset(pDeferred, 0, sizeof(*pDeferred));
139679   pDeferred->pToken = pToken;
139680   pDeferred->pNext = pCsr->pDeferred;
139681   pDeferred->iCol = iCol;
139682   pCsr->pDeferred = pDeferred;
139683 
139684   assert( pToken->pDeferred==0 );
139685   pToken->pDeferred = pDeferred;
139686 
139687   return SQLITE_OK;
139688 }
139689 #endif
139690 
139691 /*
139692 ** SQLite value pRowid contains the rowid of a row that may or may not be
139693 ** present in the FTS3 table. If it is, delete it and adjust the contents
139694 ** of subsiduary data structures accordingly.
139695 */
139696 static int fts3DeleteByRowid(
139697   Fts3Table *p,
139698   sqlite3_value *pRowid,
139699   int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
139700   u32 *aSzDel
139701 ){
139702   int rc = SQLITE_OK;             /* Return code */
139703   int bFound = 0;                 /* True if *pRowid really is in the table */
139704 
139705   fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
139706   if( bFound && rc==SQLITE_OK ){
139707     int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
139708     rc = fts3IsEmpty(p, pRowid, &isEmpty);
139709     if( rc==SQLITE_OK ){
139710       if( isEmpty ){
139711         /* Deleting this row means the whole table is empty. In this case
139712         ** delete the contents of all three tables and throw away any
139713         ** data in the pendingTerms hash table.  */
139714         rc = fts3DeleteAll(p, 1);
139715         *pnChng = 0;
139716         memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
139717       }else{
139718         *pnChng = *pnChng - 1;
139719         if( p->zContentTbl==0 ){
139720           fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
139721         }
139722         if( p->bHasDocsize ){
139723           fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
139724         }
139725       }
139726     }
139727   }
139728 
139729   return rc;
139730 }
139731 
139732 /*
139733 ** This function does the work for the xUpdate method of FTS3 virtual
139734 ** tables. The schema of the virtual table being:
139735 **
139736 **     CREATE TABLE <table name>(
139737 **       <user columns>,
139738 **       <table name> HIDDEN,
139739 **       docid HIDDEN,
139740 **       <langid> HIDDEN
139741 **     );
139742 **
139743 **
139744 */
139745 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
139746   sqlite3_vtab *pVtab,            /* FTS3 vtab object */
139747   int nArg,                       /* Size of argument array */
139748   sqlite3_value **apVal,          /* Array of arguments */
139749   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
139750 ){
139751   Fts3Table *p = (Fts3Table *)pVtab;
139752   int rc = SQLITE_OK;             /* Return Code */
139753   int isRemove = 0;               /* True for an UPDATE or DELETE */
139754   u32 *aSzIns = 0;                /* Sizes of inserted documents */
139755   u32 *aSzDel = 0;                /* Sizes of deleted documents */
139756   int nChng = 0;                  /* Net change in number of documents */
139757   int bInsertDone = 0;
139758 
139759   assert( p->pSegments==0 );
139760   assert(
139761       nArg==1                     /* DELETE operations */
139762    || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
139763   );
139764 
139765   /* Check for a "special" INSERT operation. One of the form:
139766   **
139767   **   INSERT INTO xyz(xyz) VALUES('command');
139768   */
139769   if( nArg>1
139770    && sqlite3_value_type(apVal[0])==SQLITE_NULL
139771    && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
139772   ){
139773     rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
139774     goto update_out;
139775   }
139776 
139777   if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
139778     rc = SQLITE_CONSTRAINT;
139779     goto update_out;
139780   }
139781 
139782   /* Allocate space to hold the change in document sizes */
139783   aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
139784   if( aSzDel==0 ){
139785     rc = SQLITE_NOMEM;
139786     goto update_out;
139787   }
139788   aSzIns = &aSzDel[p->nColumn+1];
139789   memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
139790 
139791   rc = fts3Writelock(p);
139792   if( rc!=SQLITE_OK ) goto update_out;
139793 
139794   /* If this is an INSERT operation, or an UPDATE that modifies the rowid
139795   ** value, then this operation requires constraint handling.
139796   **
139797   ** If the on-conflict mode is REPLACE, this means that the existing row
139798   ** should be deleted from the database before inserting the new row. Or,
139799   ** if the on-conflict mode is other than REPLACE, then this method must
139800   ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
139801   ** modify the database file.
139802   */
139803   if( nArg>1 && p->zContentTbl==0 ){
139804     /* Find the value object that holds the new rowid value. */
139805     sqlite3_value *pNewRowid = apVal[3+p->nColumn];
139806     if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
139807       pNewRowid = apVal[1];
139808     }
139809 
139810     if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
139811         sqlite3_value_type(apVal[0])==SQLITE_NULL
139812      || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
139813     )){
139814       /* The new rowid is not NULL (in this case the rowid will be
139815       ** automatically assigned and there is no chance of a conflict), and
139816       ** the statement is either an INSERT or an UPDATE that modifies the
139817       ** rowid column. So if the conflict mode is REPLACE, then delete any
139818       ** existing row with rowid=pNewRowid.
139819       **
139820       ** Or, if the conflict mode is not REPLACE, insert the new record into
139821       ** the %_content table. If we hit the duplicate rowid constraint (or any
139822       ** other error) while doing so, return immediately.
139823       **
139824       ** This branch may also run if pNewRowid contains a value that cannot
139825       ** be losslessly converted to an integer. In this case, the eventual
139826       ** call to fts3InsertData() (either just below or further on in this
139827       ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
139828       ** invoked, it will delete zero rows (since no row will have
139829       ** docid=$pNewRowid if $pNewRowid is not an integer value).
139830       */
139831       if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
139832         rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
139833       }else{
139834         rc = fts3InsertData(p, apVal, pRowid);
139835         bInsertDone = 1;
139836       }
139837     }
139838   }
139839   if( rc!=SQLITE_OK ){
139840     goto update_out;
139841   }
139842 
139843   /* If this is a DELETE or UPDATE operation, remove the old record. */
139844   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
139845     assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
139846     rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
139847     isRemove = 1;
139848   }
139849 
139850   /* If this is an INSERT or UPDATE operation, insert the new record. */
139851   if( nArg>1 && rc==SQLITE_OK ){
139852     int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
139853     if( bInsertDone==0 ){
139854       rc = fts3InsertData(p, apVal, pRowid);
139855       if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
139856         rc = FTS_CORRUPT_VTAB;
139857       }
139858     }
139859     if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
139860       rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
139861     }
139862     if( rc==SQLITE_OK ){
139863       assert( p->iPrevDocid==*pRowid );
139864       rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
139865     }
139866     if( p->bHasDocsize ){
139867       fts3InsertDocsize(&rc, p, aSzIns);
139868     }
139869     nChng++;
139870   }
139871 
139872   if( p->bFts4 ){
139873     fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
139874   }
139875 
139876  update_out:
139877   sqlite3_free(aSzDel);
139878   sqlite3Fts3SegmentsClose(p);
139879   return rc;
139880 }
139881 
139882 /*
139883 ** Flush any data in the pending-terms hash table to disk. If successful,
139884 ** merge all segments in the database (including the new segment, if
139885 ** there was any data to flush) into a single segment.
139886 */
139887 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
139888   int rc;
139889   rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
139890   if( rc==SQLITE_OK ){
139891     rc = fts3DoOptimize(p, 1);
139892     if( rc==SQLITE_OK || rc==SQLITE_DONE ){
139893       int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
139894       if( rc2!=SQLITE_OK ) rc = rc2;
139895     }else{
139896       sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
139897       sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
139898     }
139899   }
139900   sqlite3Fts3SegmentsClose(p);
139901   return rc;
139902 }
139903 
139904 #endif
139905 
139906 /************** End of fts3_write.c ******************************************/
139907 /************** Begin file fts3_snippet.c ************************************/
139908 /*
139909 ** 2009 Oct 23
139910 **
139911 ** The author disclaims copyright to this source code.  In place of
139912 ** a legal notice, here is a blessing:
139913 **
139914 **    May you do good and not evil.
139915 **    May you find forgiveness for yourself and forgive others.
139916 **    May you share freely, never taking more than you give.
139917 **
139918 ******************************************************************************
139919 */
139920 
139921 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
139922 
139923 /* #include <string.h> */
139924 /* #include <assert.h> */
139925 
139926 /*
139927 ** Characters that may appear in the second argument to matchinfo().
139928 */
139929 #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
139930 #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
139931 #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
139932 #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
139933 #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
139934 #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
139935 #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
139936 
139937 /*
139938 ** The default value for the second argument to matchinfo().
139939 */
139940 #define FTS3_MATCHINFO_DEFAULT   "pcx"
139941 
139942 
139943 /*
139944 ** Used as an fts3ExprIterate() context when loading phrase doclists to
139945 ** Fts3Expr.aDoclist[]/nDoclist.
139946 */
139947 typedef struct LoadDoclistCtx LoadDoclistCtx;
139948 struct LoadDoclistCtx {
139949   Fts3Cursor *pCsr;               /* FTS3 Cursor */
139950   int nPhrase;                    /* Number of phrases seen so far */
139951   int nToken;                     /* Number of tokens seen so far */
139952 };
139953 
139954 /*
139955 ** The following types are used as part of the implementation of the
139956 ** fts3BestSnippet() routine.
139957 */
139958 typedef struct SnippetIter SnippetIter;
139959 typedef struct SnippetPhrase SnippetPhrase;
139960 typedef struct SnippetFragment SnippetFragment;
139961 
139962 struct SnippetIter {
139963   Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
139964   int iCol;                       /* Extract snippet from this column */
139965   int nSnippet;                   /* Requested snippet length (in tokens) */
139966   int nPhrase;                    /* Number of phrases in query */
139967   SnippetPhrase *aPhrase;         /* Array of size nPhrase */
139968   int iCurrent;                   /* First token of current snippet */
139969 };
139970 
139971 struct SnippetPhrase {
139972   int nToken;                     /* Number of tokens in phrase */
139973   char *pList;                    /* Pointer to start of phrase position list */
139974   int iHead;                      /* Next value in position list */
139975   char *pHead;                    /* Position list data following iHead */
139976   int iTail;                      /* Next value in trailing position list */
139977   char *pTail;                    /* Position list data following iTail */
139978 };
139979 
139980 struct SnippetFragment {
139981   int iCol;                       /* Column snippet is extracted from */
139982   int iPos;                       /* Index of first token in snippet */
139983   u64 covered;                    /* Mask of query phrases covered */
139984   u64 hlmask;                     /* Mask of snippet terms to highlight */
139985 };
139986 
139987 /*
139988 ** This type is used as an fts3ExprIterate() context object while
139989 ** accumulating the data returned by the matchinfo() function.
139990 */
139991 typedef struct MatchInfo MatchInfo;
139992 struct MatchInfo {
139993   Fts3Cursor *pCursor;            /* FTS3 Cursor */
139994   int nCol;                       /* Number of columns in table */
139995   int nPhrase;                    /* Number of matchable phrases in query */
139996   sqlite3_int64 nDoc;             /* Number of docs in database */
139997   u32 *aMatchinfo;                /* Pre-allocated buffer */
139998 };
139999 
140000 
140001 
140002 /*
140003 ** The snippet() and offsets() functions both return text values. An instance
140004 ** of the following structure is used to accumulate those values while the
140005 ** functions are running. See fts3StringAppend() for details.
140006 */
140007 typedef struct StrBuffer StrBuffer;
140008 struct StrBuffer {
140009   char *z;                        /* Pointer to buffer containing string */
140010   int n;                          /* Length of z in bytes (excl. nul-term) */
140011   int nAlloc;                     /* Allocated size of buffer z in bytes */
140012 };
140013 
140014 
140015 /*
140016 ** This function is used to help iterate through a position-list. A position
140017 ** list is a list of unique integers, sorted from smallest to largest. Each
140018 ** element of the list is represented by an FTS3 varint that takes the value
140019 ** of the difference between the current element and the previous one plus
140020 ** two. For example, to store the position-list:
140021 **
140022 **     4 9 113
140023 **
140024 ** the three varints:
140025 **
140026 **     6 7 106
140027 **
140028 ** are encoded.
140029 **
140030 ** When this function is called, *pp points to the start of an element of
140031 ** the list. *piPos contains the value of the previous entry in the list.
140032 ** After it returns, *piPos contains the value of the next element of the
140033 ** list and *pp is advanced to the following varint.
140034 */
140035 static void fts3GetDeltaPosition(char **pp, int *piPos){
140036   int iVal;
140037   *pp += fts3GetVarint32(*pp, &iVal);
140038   *piPos += (iVal-2);
140039 }
140040 
140041 /*
140042 ** Helper function for fts3ExprIterate() (see below).
140043 */
140044 static int fts3ExprIterate2(
140045   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
140046   int *piPhrase,                  /* Pointer to phrase counter */
140047   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
140048   void *pCtx                      /* Second argument to pass to callback */
140049 ){
140050   int rc;                         /* Return code */
140051   int eType = pExpr->eType;       /* Type of expression node pExpr */
140052 
140053   if( eType!=FTSQUERY_PHRASE ){
140054     assert( pExpr->pLeft && pExpr->pRight );
140055     rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
140056     if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
140057       rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
140058     }
140059   }else{
140060     rc = x(pExpr, *piPhrase, pCtx);
140061     (*piPhrase)++;
140062   }
140063   return rc;
140064 }
140065 
140066 /*
140067 ** Iterate through all phrase nodes in an FTS3 query, except those that
140068 ** are part of a sub-tree that is the right-hand-side of a NOT operator.
140069 ** For each phrase node found, the supplied callback function is invoked.
140070 **
140071 ** If the callback function returns anything other than SQLITE_OK,
140072 ** the iteration is abandoned and the error code returned immediately.
140073 ** Otherwise, SQLITE_OK is returned after a callback has been made for
140074 ** all eligible phrase nodes.
140075 */
140076 static int fts3ExprIterate(
140077   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
140078   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
140079   void *pCtx                      /* Second argument to pass to callback */
140080 ){
140081   int iPhrase = 0;                /* Variable used as the phrase counter */
140082   return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
140083 }
140084 
140085 /*
140086 ** This is an fts3ExprIterate() callback used while loading the doclists
140087 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
140088 ** fts3ExprLoadDoclists().
140089 */
140090 static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
140091   int rc = SQLITE_OK;
140092   Fts3Phrase *pPhrase = pExpr->pPhrase;
140093   LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
140094 
140095   UNUSED_PARAMETER(iPhrase);
140096 
140097   p->nPhrase++;
140098   p->nToken += pPhrase->nToken;
140099 
140100   return rc;
140101 }
140102 
140103 /*
140104 ** Load the doclists for each phrase in the query associated with FTS3 cursor
140105 ** pCsr.
140106 **
140107 ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
140108 ** phrases in the expression (all phrases except those directly or
140109 ** indirectly descended from the right-hand-side of a NOT operator). If
140110 ** pnToken is not NULL, then it is set to the number of tokens in all
140111 ** matchable phrases of the expression.
140112 */
140113 static int fts3ExprLoadDoclists(
140114   Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
140115   int *pnPhrase,                  /* OUT: Number of phrases in query */
140116   int *pnToken                    /* OUT: Number of tokens in query */
140117 ){
140118   int rc;                         /* Return Code */
140119   LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
140120   sCtx.pCsr = pCsr;
140121   rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
140122   if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
140123   if( pnToken ) *pnToken = sCtx.nToken;
140124   return rc;
140125 }
140126 
140127 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
140128   (*(int *)ctx)++;
140129   UNUSED_PARAMETER(pExpr);
140130   UNUSED_PARAMETER(iPhrase);
140131   return SQLITE_OK;
140132 }
140133 static int fts3ExprPhraseCount(Fts3Expr *pExpr){
140134   int nPhrase = 0;
140135   (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
140136   return nPhrase;
140137 }
140138 
140139 /*
140140 ** Advance the position list iterator specified by the first two
140141 ** arguments so that it points to the first element with a value greater
140142 ** than or equal to parameter iNext.
140143 */
140144 static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
140145   char *pIter = *ppIter;
140146   if( pIter ){
140147     int iIter = *piIter;
140148 
140149     while( iIter<iNext ){
140150       if( 0==(*pIter & 0xFE) ){
140151         iIter = -1;
140152         pIter = 0;
140153         break;
140154       }
140155       fts3GetDeltaPosition(&pIter, &iIter);
140156     }
140157 
140158     *piIter = iIter;
140159     *ppIter = pIter;
140160   }
140161 }
140162 
140163 /*
140164 ** Advance the snippet iterator to the next candidate snippet.
140165 */
140166 static int fts3SnippetNextCandidate(SnippetIter *pIter){
140167   int i;                          /* Loop counter */
140168 
140169   if( pIter->iCurrent<0 ){
140170     /* The SnippetIter object has just been initialized. The first snippet
140171     ** candidate always starts at offset 0 (even if this candidate has a
140172     ** score of 0.0).
140173     */
140174     pIter->iCurrent = 0;
140175 
140176     /* Advance the 'head' iterator of each phrase to the first offset that
140177     ** is greater than or equal to (iNext+nSnippet).
140178     */
140179     for(i=0; i<pIter->nPhrase; i++){
140180       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
140181       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
140182     }
140183   }else{
140184     int iStart;
140185     int iEnd = 0x7FFFFFFF;
140186 
140187     for(i=0; i<pIter->nPhrase; i++){
140188       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
140189       if( pPhrase->pHead && pPhrase->iHead<iEnd ){
140190         iEnd = pPhrase->iHead;
140191       }
140192     }
140193     if( iEnd==0x7FFFFFFF ){
140194       return 1;
140195     }
140196 
140197     pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
140198     for(i=0; i<pIter->nPhrase; i++){
140199       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
140200       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
140201       fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
140202     }
140203   }
140204 
140205   return 0;
140206 }
140207 
140208 /*
140209 ** Retrieve information about the current candidate snippet of snippet
140210 ** iterator pIter.
140211 */
140212 static void fts3SnippetDetails(
140213   SnippetIter *pIter,             /* Snippet iterator */
140214   u64 mCovered,                   /* Bitmask of phrases already covered */
140215   int *piToken,                   /* OUT: First token of proposed snippet */
140216   int *piScore,                   /* OUT: "Score" for this snippet */
140217   u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
140218   u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
140219 ){
140220   int iStart = pIter->iCurrent;   /* First token of snippet */
140221   int iScore = 0;                 /* Score of this snippet */
140222   int i;                          /* Loop counter */
140223   u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
140224   u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
140225 
140226   for(i=0; i<pIter->nPhrase; i++){
140227     SnippetPhrase *pPhrase = &pIter->aPhrase[i];
140228     if( pPhrase->pTail ){
140229       char *pCsr = pPhrase->pTail;
140230       int iCsr = pPhrase->iTail;
140231 
140232       while( iCsr<(iStart+pIter->nSnippet) ){
140233         int j;
140234         u64 mPhrase = (u64)1 << i;
140235         u64 mPos = (u64)1 << (iCsr - iStart);
140236         assert( iCsr>=iStart );
140237         if( (mCover|mCovered)&mPhrase ){
140238           iScore++;
140239         }else{
140240           iScore += 1000;
140241         }
140242         mCover |= mPhrase;
140243 
140244         for(j=0; j<pPhrase->nToken; j++){
140245           mHighlight |= (mPos>>j);
140246         }
140247 
140248         if( 0==(*pCsr & 0x0FE) ) break;
140249         fts3GetDeltaPosition(&pCsr, &iCsr);
140250       }
140251     }
140252   }
140253 
140254   /* Set the output variables before returning. */
140255   *piToken = iStart;
140256   *piScore = iScore;
140257   *pmCover = mCover;
140258   *pmHighlight = mHighlight;
140259 }
140260 
140261 /*
140262 ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
140263 ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
140264 */
140265 static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
140266   SnippetIter *p = (SnippetIter *)ctx;
140267   SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
140268   char *pCsr;
140269   int rc;
140270 
140271   pPhrase->nToken = pExpr->pPhrase->nToken;
140272   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
140273   assert( rc==SQLITE_OK || pCsr==0 );
140274   if( pCsr ){
140275     int iFirst = 0;
140276     pPhrase->pList = pCsr;
140277     fts3GetDeltaPosition(&pCsr, &iFirst);
140278     assert( iFirst>=0 );
140279     pPhrase->pHead = pCsr;
140280     pPhrase->pTail = pCsr;
140281     pPhrase->iHead = iFirst;
140282     pPhrase->iTail = iFirst;
140283   }else{
140284     assert( rc!=SQLITE_OK || (
140285        pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
140286     ));
140287   }
140288 
140289   return rc;
140290 }
140291 
140292 /*
140293 ** Select the fragment of text consisting of nFragment contiguous tokens
140294 ** from column iCol that represent the "best" snippet. The best snippet
140295 ** is the snippet with the highest score, where scores are calculated
140296 ** by adding:
140297 **
140298 **   (a) +1 point for each occurrence of a matchable phrase in the snippet.
140299 **
140300 **   (b) +1000 points for the first occurrence of each matchable phrase in
140301 **       the snippet for which the corresponding mCovered bit is not set.
140302 **
140303 ** The selected snippet parameters are stored in structure *pFragment before
140304 ** returning. The score of the selected snippet is stored in *piScore
140305 ** before returning.
140306 */
140307 static int fts3BestSnippet(
140308   int nSnippet,                   /* Desired snippet length */
140309   Fts3Cursor *pCsr,               /* Cursor to create snippet for */
140310   int iCol,                       /* Index of column to create snippet from */
140311   u64 mCovered,                   /* Mask of phrases already covered */
140312   u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
140313   SnippetFragment *pFragment,     /* OUT: Best snippet found */
140314   int *piScore                    /* OUT: Score of snippet pFragment */
140315 ){
140316   int rc;                         /* Return Code */
140317   int nList;                      /* Number of phrases in expression */
140318   SnippetIter sIter;              /* Iterates through snippet candidates */
140319   int nByte;                      /* Number of bytes of space to allocate */
140320   int iBestScore = -1;            /* Best snippet score found so far */
140321   int i;                          /* Loop counter */
140322 
140323   memset(&sIter, 0, sizeof(sIter));
140324 
140325   /* Iterate through the phrases in the expression to count them. The same
140326   ** callback makes sure the doclists are loaded for each phrase.
140327   */
140328   rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
140329   if( rc!=SQLITE_OK ){
140330     return rc;
140331   }
140332 
140333   /* Now that it is known how many phrases there are, allocate and zero
140334   ** the required space using malloc().
140335   */
140336   nByte = sizeof(SnippetPhrase) * nList;
140337   sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
140338   if( !sIter.aPhrase ){
140339     return SQLITE_NOMEM;
140340   }
140341   memset(sIter.aPhrase, 0, nByte);
140342 
140343   /* Initialize the contents of the SnippetIter object. Then iterate through
140344   ** the set of phrases in the expression to populate the aPhrase[] array.
140345   */
140346   sIter.pCsr = pCsr;
140347   sIter.iCol = iCol;
140348   sIter.nSnippet = nSnippet;
140349   sIter.nPhrase = nList;
140350   sIter.iCurrent = -1;
140351   (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
140352 
140353   /* Set the *pmSeen output variable. */
140354   for(i=0; i<nList; i++){
140355     if( sIter.aPhrase[i].pHead ){
140356       *pmSeen |= (u64)1 << i;
140357     }
140358   }
140359 
140360   /* Loop through all candidate snippets. Store the best snippet in
140361   ** *pFragment. Store its associated 'score' in iBestScore.
140362   */
140363   pFragment->iCol = iCol;
140364   while( !fts3SnippetNextCandidate(&sIter) ){
140365     int iPos;
140366     int iScore;
140367     u64 mCover;
140368     u64 mHighlight;
140369     fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
140370     assert( iScore>=0 );
140371     if( iScore>iBestScore ){
140372       pFragment->iPos = iPos;
140373       pFragment->hlmask = mHighlight;
140374       pFragment->covered = mCover;
140375       iBestScore = iScore;
140376     }
140377   }
140378 
140379   sqlite3_free(sIter.aPhrase);
140380   *piScore = iBestScore;
140381   return SQLITE_OK;
140382 }
140383 
140384 
140385 /*
140386 ** Append a string to the string-buffer passed as the first argument.
140387 **
140388 ** If nAppend is negative, then the length of the string zAppend is
140389 ** determined using strlen().
140390 */
140391 static int fts3StringAppend(
140392   StrBuffer *pStr,                /* Buffer to append to */
140393   const char *zAppend,            /* Pointer to data to append to buffer */
140394   int nAppend                     /* Size of zAppend in bytes (or -1) */
140395 ){
140396   if( nAppend<0 ){
140397     nAppend = (int)strlen(zAppend);
140398   }
140399 
140400   /* If there is insufficient space allocated at StrBuffer.z, use realloc()
140401   ** to grow the buffer until so that it is big enough to accomadate the
140402   ** appended data.
140403   */
140404   if( pStr->n+nAppend+1>=pStr->nAlloc ){
140405     int nAlloc = pStr->nAlloc+nAppend+100;
140406     char *zNew = sqlite3_realloc(pStr->z, nAlloc);
140407     if( !zNew ){
140408       return SQLITE_NOMEM;
140409     }
140410     pStr->z = zNew;
140411     pStr->nAlloc = nAlloc;
140412   }
140413   assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
140414 
140415   /* Append the data to the string buffer. */
140416   memcpy(&pStr->z[pStr->n], zAppend, nAppend);
140417   pStr->n += nAppend;
140418   pStr->z[pStr->n] = '\0';
140419 
140420   return SQLITE_OK;
140421 }
140422 
140423 /*
140424 ** The fts3BestSnippet() function often selects snippets that end with a
140425 ** query term. That is, the final term of the snippet is always a term
140426 ** that requires highlighting. For example, if 'X' is a highlighted term
140427 ** and '.' is a non-highlighted term, BestSnippet() may select:
140428 **
140429 **     ........X.....X
140430 **
140431 ** This function "shifts" the beginning of the snippet forward in the
140432 ** document so that there are approximately the same number of
140433 ** non-highlighted terms to the right of the final highlighted term as there
140434 ** are to the left of the first highlighted term. For example, to this:
140435 **
140436 **     ....X.....X....
140437 **
140438 ** This is done as part of extracting the snippet text, not when selecting
140439 ** the snippet. Snippet selection is done based on doclists only, so there
140440 ** is no way for fts3BestSnippet() to know whether or not the document
140441 ** actually contains terms that follow the final highlighted term.
140442 */
140443 static int fts3SnippetShift(
140444   Fts3Table *pTab,                /* FTS3 table snippet comes from */
140445   int iLangid,                    /* Language id to use in tokenizing */
140446   int nSnippet,                   /* Number of tokens desired for snippet */
140447   const char *zDoc,               /* Document text to extract snippet from */
140448   int nDoc,                       /* Size of buffer zDoc in bytes */
140449   int *piPos,                     /* IN/OUT: First token of snippet */
140450   u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
140451 ){
140452   u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
140453 
140454   if( hlmask ){
140455     int nLeft;                    /* Tokens to the left of first highlight */
140456     int nRight;                   /* Tokens to the right of last highlight */
140457     int nDesired;                 /* Ideal number of tokens to shift forward */
140458 
140459     for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
140460     for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
140461     nDesired = (nLeft-nRight)/2;
140462 
140463     /* Ideally, the start of the snippet should be pushed forward in the
140464     ** document nDesired tokens. This block checks if there are actually
140465     ** nDesired tokens to the right of the snippet. If so, *piPos and
140466     ** *pHlMask are updated to shift the snippet nDesired tokens to the
140467     ** right. Otherwise, the snippet is shifted by the number of tokens
140468     ** available.
140469     */
140470     if( nDesired>0 ){
140471       int nShift;                 /* Number of tokens to shift snippet by */
140472       int iCurrent = 0;           /* Token counter */
140473       int rc;                     /* Return Code */
140474       sqlite3_tokenizer_module *pMod;
140475       sqlite3_tokenizer_cursor *pC;
140476       pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
140477 
140478       /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
140479       ** or more tokens in zDoc/nDoc.
140480       */
140481       rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
140482       if( rc!=SQLITE_OK ){
140483         return rc;
140484       }
140485       while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
140486         const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
140487         rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
140488       }
140489       pMod->xClose(pC);
140490       if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
140491 
140492       nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
140493       assert( nShift<=nDesired );
140494       if( nShift>0 ){
140495         *piPos += nShift;
140496         *pHlmask = hlmask >> nShift;
140497       }
140498     }
140499   }
140500   return SQLITE_OK;
140501 }
140502 
140503 /*
140504 ** Extract the snippet text for fragment pFragment from cursor pCsr and
140505 ** append it to string buffer pOut.
140506 */
140507 static int fts3SnippetText(
140508   Fts3Cursor *pCsr,               /* FTS3 Cursor */
140509   SnippetFragment *pFragment,     /* Snippet to extract */
140510   int iFragment,                  /* Fragment number */
140511   int isLast,                     /* True for final fragment in snippet */
140512   int nSnippet,                   /* Number of tokens in extracted snippet */
140513   const char *zOpen,              /* String inserted before highlighted term */
140514   const char *zClose,             /* String inserted after highlighted term */
140515   const char *zEllipsis,          /* String inserted between snippets */
140516   StrBuffer *pOut                 /* Write output here */
140517 ){
140518   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140519   int rc;                         /* Return code */
140520   const char *zDoc;               /* Document text to extract snippet from */
140521   int nDoc;                       /* Size of zDoc in bytes */
140522   int iCurrent = 0;               /* Current token number of document */
140523   int iEnd = 0;                   /* Byte offset of end of current token */
140524   int isShiftDone = 0;            /* True after snippet is shifted */
140525   int iPos = pFragment->iPos;     /* First token of snippet */
140526   u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
140527   int iCol = pFragment->iCol+1;   /* Query column to extract text from */
140528   sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
140529   sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
140530 
140531   zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
140532   if( zDoc==0 ){
140533     if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
140534       return SQLITE_NOMEM;
140535     }
140536     return SQLITE_OK;
140537   }
140538   nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
140539 
140540   /* Open a token cursor on the document. */
140541   pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
140542   rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
140543   if( rc!=SQLITE_OK ){
140544     return rc;
140545   }
140546 
140547   while( rc==SQLITE_OK ){
140548     const char *ZDUMMY;           /* Dummy argument used with tokenizer */
140549     int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
140550     int iBegin = 0;               /* Offset in zDoc of start of token */
140551     int iFin = 0;                 /* Offset in zDoc of end of token */
140552     int isHighlight = 0;          /* True for highlighted terms */
140553 
140554     /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
140555     ** in the FTS code the variable that the third argument to xNext points to
140556     ** is initialized to zero before the first (*but not necessarily
140557     ** subsequent*) call to xNext(). This is done for a particular application
140558     ** that needs to know whether or not the tokenizer is being used for
140559     ** snippet generation or for some other purpose.
140560     **
140561     ** Extreme care is required when writing code to depend on this
140562     ** initialization. It is not a documented part of the tokenizer interface.
140563     ** If a tokenizer is used directly by any code outside of FTS, this
140564     ** convention might not be respected.  */
140565     rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
140566     if( rc!=SQLITE_OK ){
140567       if( rc==SQLITE_DONE ){
140568         /* Special case - the last token of the snippet is also the last token
140569         ** of the column. Append any punctuation that occurred between the end
140570         ** of the previous token and the end of the document to the output.
140571         ** Then break out of the loop. */
140572         rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
140573       }
140574       break;
140575     }
140576     if( iCurrent<iPos ){ continue; }
140577 
140578     if( !isShiftDone ){
140579       int n = nDoc - iBegin;
140580       rc = fts3SnippetShift(
140581           pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
140582       );
140583       isShiftDone = 1;
140584 
140585       /* Now that the shift has been done, check if the initial "..." are
140586       ** required. They are required if (a) this is not the first fragment,
140587       ** or (b) this fragment does not begin at position 0 of its column.
140588       */
140589       if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
140590         rc = fts3StringAppend(pOut, zEllipsis, -1);
140591       }
140592       if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
140593     }
140594 
140595     if( iCurrent>=(iPos+nSnippet) ){
140596       if( isLast ){
140597         rc = fts3StringAppend(pOut, zEllipsis, -1);
140598       }
140599       break;
140600     }
140601 
140602     /* Set isHighlight to true if this term should be highlighted. */
140603     isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
140604 
140605     if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
140606     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
140607     if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
140608     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
140609 
140610     iEnd = iFin;
140611   }
140612 
140613   pMod->xClose(pC);
140614   return rc;
140615 }
140616 
140617 
140618 /*
140619 ** This function is used to count the entries in a column-list (a
140620 ** delta-encoded list of term offsets within a single column of a single
140621 ** row). When this function is called, *ppCollist should point to the
140622 ** beginning of the first varint in the column-list (the varint that
140623 ** contains the position of the first matching term in the column data).
140624 ** Before returning, *ppCollist is set to point to the first byte after
140625 ** the last varint in the column-list (either the 0x00 signifying the end
140626 ** of the position-list, or the 0x01 that precedes the column number of
140627 ** the next column in the position-list).
140628 **
140629 ** The number of elements in the column-list is returned.
140630 */
140631 static int fts3ColumnlistCount(char **ppCollist){
140632   char *pEnd = *ppCollist;
140633   char c = 0;
140634   int nEntry = 0;
140635 
140636   /* A column-list is terminated by either a 0x01 or 0x00. */
140637   while( 0xFE & (*pEnd | c) ){
140638     c = *pEnd++ & 0x80;
140639     if( !c ) nEntry++;
140640   }
140641 
140642   *ppCollist = pEnd;
140643   return nEntry;
140644 }
140645 
140646 /*
140647 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
140648 ** for a single query.
140649 **
140650 ** fts3ExprIterate() callback to load the 'global' elements of a
140651 ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
140652 ** of the matchinfo array that are constant for all rows returned by the
140653 ** current query.
140654 **
140655 ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
140656 ** function populates Matchinfo.aMatchinfo[] as follows:
140657 **
140658 **   for(iCol=0; iCol<nCol; iCol++){
140659 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
140660 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
140661 **   }
140662 **
140663 ** where X is the number of matches for phrase iPhrase is column iCol of all
140664 ** rows of the table. Y is the number of rows for which column iCol contains
140665 ** at least one instance of phrase iPhrase.
140666 **
140667 ** If the phrase pExpr consists entirely of deferred tokens, then all X and
140668 ** Y values are set to nDoc, where nDoc is the number of documents in the
140669 ** file system. This is done because the full-text index doclist is required
140670 ** to calculate these values properly, and the full-text index doclist is
140671 ** not available for deferred tokens.
140672 */
140673 static int fts3ExprGlobalHitsCb(
140674   Fts3Expr *pExpr,                /* Phrase expression node */
140675   int iPhrase,                    /* Phrase number (numbered from zero) */
140676   void *pCtx                      /* Pointer to MatchInfo structure */
140677 ){
140678   MatchInfo *p = (MatchInfo *)pCtx;
140679   return sqlite3Fts3EvalPhraseStats(
140680       p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
140681   );
140682 }
140683 
140684 /*
140685 ** fts3ExprIterate() callback used to collect the "local" part of the
140686 ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
140687 ** array that are different for each row returned by the query.
140688 */
140689 static int fts3ExprLocalHitsCb(
140690   Fts3Expr *pExpr,                /* Phrase expression node */
140691   int iPhrase,                    /* Phrase number */
140692   void *pCtx                      /* Pointer to MatchInfo structure */
140693 ){
140694   int rc = SQLITE_OK;
140695   MatchInfo *p = (MatchInfo *)pCtx;
140696   int iStart = iPhrase * p->nCol * 3;
140697   int i;
140698 
140699   for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
140700     char *pCsr;
140701     rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
140702     if( pCsr ){
140703       p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
140704     }else{
140705       p->aMatchinfo[iStart+i*3] = 0;
140706     }
140707   }
140708 
140709   return rc;
140710 }
140711 
140712 static int fts3MatchinfoCheck(
140713   Fts3Table *pTab,
140714   char cArg,
140715   char **pzErr
140716 ){
140717   if( (cArg==FTS3_MATCHINFO_NPHRASE)
140718    || (cArg==FTS3_MATCHINFO_NCOL)
140719    || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
140720    || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
140721    || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
140722    || (cArg==FTS3_MATCHINFO_LCS)
140723    || (cArg==FTS3_MATCHINFO_HITS)
140724   ){
140725     return SQLITE_OK;
140726   }
140727   *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
140728   return SQLITE_ERROR;
140729 }
140730 
140731 static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
140732   int nVal;                       /* Number of integers output by cArg */
140733 
140734   switch( cArg ){
140735     case FTS3_MATCHINFO_NDOC:
140736     case FTS3_MATCHINFO_NPHRASE:
140737     case FTS3_MATCHINFO_NCOL:
140738       nVal = 1;
140739       break;
140740 
140741     case FTS3_MATCHINFO_AVGLENGTH:
140742     case FTS3_MATCHINFO_LENGTH:
140743     case FTS3_MATCHINFO_LCS:
140744       nVal = pInfo->nCol;
140745       break;
140746 
140747     default:
140748       assert( cArg==FTS3_MATCHINFO_HITS );
140749       nVal = pInfo->nCol * pInfo->nPhrase * 3;
140750       break;
140751   }
140752 
140753   return nVal;
140754 }
140755 
140756 static int fts3MatchinfoSelectDoctotal(
140757   Fts3Table *pTab,
140758   sqlite3_stmt **ppStmt,
140759   sqlite3_int64 *pnDoc,
140760   const char **paLen
140761 ){
140762   sqlite3_stmt *pStmt;
140763   const char *a;
140764   sqlite3_int64 nDoc;
140765 
140766   if( !*ppStmt ){
140767     int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
140768     if( rc!=SQLITE_OK ) return rc;
140769   }
140770   pStmt = *ppStmt;
140771   assert( sqlite3_data_count(pStmt)==1 );
140772 
140773   a = sqlite3_column_blob(pStmt, 0);
140774   a += sqlite3Fts3GetVarint(a, &nDoc);
140775   if( nDoc==0 ) return FTS_CORRUPT_VTAB;
140776   *pnDoc = (u32)nDoc;
140777 
140778   if( paLen ) *paLen = a;
140779   return SQLITE_OK;
140780 }
140781 
140782 /*
140783 ** An instance of the following structure is used to store state while
140784 ** iterating through a multi-column position-list corresponding to the
140785 ** hits for a single phrase on a single row in order to calculate the
140786 ** values for a matchinfo() FTS3_MATCHINFO_LCS request.
140787 */
140788 typedef struct LcsIterator LcsIterator;
140789 struct LcsIterator {
140790   Fts3Expr *pExpr;                /* Pointer to phrase expression */
140791   int iPosOffset;                 /* Tokens count up to end of this phrase */
140792   char *pRead;                    /* Cursor used to iterate through aDoclist */
140793   int iPos;                       /* Current position */
140794 };
140795 
140796 /*
140797 ** If LcsIterator.iCol is set to the following value, the iterator has
140798 ** finished iterating through all offsets for all columns.
140799 */
140800 #define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
140801 
140802 static int fts3MatchinfoLcsCb(
140803   Fts3Expr *pExpr,                /* Phrase expression node */
140804   int iPhrase,                    /* Phrase number (numbered from zero) */
140805   void *pCtx                      /* Pointer to MatchInfo structure */
140806 ){
140807   LcsIterator *aIter = (LcsIterator *)pCtx;
140808   aIter[iPhrase].pExpr = pExpr;
140809   return SQLITE_OK;
140810 }
140811 
140812 /*
140813 ** Advance the iterator passed as an argument to the next position. Return
140814 ** 1 if the iterator is at EOF or if it now points to the start of the
140815 ** position list for the next column.
140816 */
140817 static int fts3LcsIteratorAdvance(LcsIterator *pIter){
140818   char *pRead = pIter->pRead;
140819   sqlite3_int64 iRead;
140820   int rc = 0;
140821 
140822   pRead += sqlite3Fts3GetVarint(pRead, &iRead);
140823   if( iRead==0 || iRead==1 ){
140824     pRead = 0;
140825     rc = 1;
140826   }else{
140827     pIter->iPos += (int)(iRead-2);
140828   }
140829 
140830   pIter->pRead = pRead;
140831   return rc;
140832 }
140833 
140834 /*
140835 ** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
140836 **
140837 ** If the call is successful, the longest-common-substring lengths for each
140838 ** column are written into the first nCol elements of the pInfo->aMatchinfo[]
140839 ** array before returning. SQLITE_OK is returned in this case.
140840 **
140841 ** Otherwise, if an error occurs, an SQLite error code is returned and the
140842 ** data written to the first nCol elements of pInfo->aMatchinfo[] is
140843 ** undefined.
140844 */
140845 static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
140846   LcsIterator *aIter;
140847   int i;
140848   int iCol;
140849   int nToken = 0;
140850 
140851   /* Allocate and populate the array of LcsIterator objects. The array
140852   ** contains one element for each matchable phrase in the query.
140853   **/
140854   aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
140855   if( !aIter ) return SQLITE_NOMEM;
140856   memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
140857   (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
140858 
140859   for(i=0; i<pInfo->nPhrase; i++){
140860     LcsIterator *pIter = &aIter[i];
140861     nToken -= pIter->pExpr->pPhrase->nToken;
140862     pIter->iPosOffset = nToken;
140863   }
140864 
140865   for(iCol=0; iCol<pInfo->nCol; iCol++){
140866     int nLcs = 0;                 /* LCS value for this column */
140867     int nLive = 0;                /* Number of iterators in aIter not at EOF */
140868 
140869     for(i=0; i<pInfo->nPhrase; i++){
140870       int rc;
140871       LcsIterator *pIt = &aIter[i];
140872       rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
140873       if( rc!=SQLITE_OK ) return rc;
140874       if( pIt->pRead ){
140875         pIt->iPos = pIt->iPosOffset;
140876         fts3LcsIteratorAdvance(&aIter[i]);
140877         nLive++;
140878       }
140879     }
140880 
140881     while( nLive>0 ){
140882       LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
140883       int nThisLcs = 0;           /* LCS for the current iterator positions */
140884 
140885       for(i=0; i<pInfo->nPhrase; i++){
140886         LcsIterator *pIter = &aIter[i];
140887         if( pIter->pRead==0 ){
140888           /* This iterator is already at EOF for this column. */
140889           nThisLcs = 0;
140890         }else{
140891           if( pAdv==0 || pIter->iPos<pAdv->iPos ){
140892             pAdv = pIter;
140893           }
140894           if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
140895             nThisLcs++;
140896           }else{
140897             nThisLcs = 1;
140898           }
140899           if( nThisLcs>nLcs ) nLcs = nThisLcs;
140900         }
140901       }
140902       if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
140903     }
140904 
140905     pInfo->aMatchinfo[iCol] = nLcs;
140906   }
140907 
140908   sqlite3_free(aIter);
140909   return SQLITE_OK;
140910 }
140911 
140912 /*
140913 ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
140914 ** be returned by the matchinfo() function. Argument zArg contains the
140915 ** format string passed as the second argument to matchinfo (or the
140916 ** default value "pcx" if no second argument was specified). The format
140917 ** string has already been validated and the pInfo->aMatchinfo[] array
140918 ** is guaranteed to be large enough for the output.
140919 **
140920 ** If bGlobal is true, then populate all fields of the matchinfo() output.
140921 ** If it is false, then assume that those fields that do not change between
140922 ** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
140923 ** have already been populated.
140924 **
140925 ** Return SQLITE_OK if successful, or an SQLite error code if an error
140926 ** occurs. If a value other than SQLITE_OK is returned, the state the
140927 ** pInfo->aMatchinfo[] buffer is left in is undefined.
140928 */
140929 static int fts3MatchinfoValues(
140930   Fts3Cursor *pCsr,               /* FTS3 cursor object */
140931   int bGlobal,                    /* True to grab the global stats */
140932   MatchInfo *pInfo,               /* Matchinfo context object */
140933   const char *zArg                /* Matchinfo format string */
140934 ){
140935   int rc = SQLITE_OK;
140936   int i;
140937   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140938   sqlite3_stmt *pSelect = 0;
140939 
140940   for(i=0; rc==SQLITE_OK && zArg[i]; i++){
140941 
140942     switch( zArg[i] ){
140943       case FTS3_MATCHINFO_NPHRASE:
140944         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
140945         break;
140946 
140947       case FTS3_MATCHINFO_NCOL:
140948         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
140949         break;
140950 
140951       case FTS3_MATCHINFO_NDOC:
140952         if( bGlobal ){
140953           sqlite3_int64 nDoc = 0;
140954           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
140955           pInfo->aMatchinfo[0] = (u32)nDoc;
140956         }
140957         break;
140958 
140959       case FTS3_MATCHINFO_AVGLENGTH:
140960         if( bGlobal ){
140961           sqlite3_int64 nDoc;     /* Number of rows in table */
140962           const char *a;          /* Aggregate column length array */
140963 
140964           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
140965           if( rc==SQLITE_OK ){
140966             int iCol;
140967             for(iCol=0; iCol<pInfo->nCol; iCol++){
140968               u32 iVal;
140969               sqlite3_int64 nToken;
140970               a += sqlite3Fts3GetVarint(a, &nToken);
140971               iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
140972               pInfo->aMatchinfo[iCol] = iVal;
140973             }
140974           }
140975         }
140976         break;
140977 
140978       case FTS3_MATCHINFO_LENGTH: {
140979         sqlite3_stmt *pSelectDocsize = 0;
140980         rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
140981         if( rc==SQLITE_OK ){
140982           int iCol;
140983           const char *a = sqlite3_column_blob(pSelectDocsize, 0);
140984           for(iCol=0; iCol<pInfo->nCol; iCol++){
140985             sqlite3_int64 nToken;
140986             a += sqlite3Fts3GetVarint(a, &nToken);
140987             pInfo->aMatchinfo[iCol] = (u32)nToken;
140988           }
140989         }
140990         sqlite3_reset(pSelectDocsize);
140991         break;
140992       }
140993 
140994       case FTS3_MATCHINFO_LCS:
140995         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
140996         if( rc==SQLITE_OK ){
140997           rc = fts3MatchinfoLcs(pCsr, pInfo);
140998         }
140999         break;
141000 
141001       default: {
141002         Fts3Expr *pExpr;
141003         assert( zArg[i]==FTS3_MATCHINFO_HITS );
141004         pExpr = pCsr->pExpr;
141005         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
141006         if( rc!=SQLITE_OK ) break;
141007         if( bGlobal ){
141008           if( pCsr->pDeferred ){
141009             rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
141010             if( rc!=SQLITE_OK ) break;
141011           }
141012           rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
141013           if( rc!=SQLITE_OK ) break;
141014         }
141015         (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
141016         break;
141017       }
141018     }
141019 
141020     pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
141021   }
141022 
141023   sqlite3_reset(pSelect);
141024   return rc;
141025 }
141026 
141027 
141028 /*
141029 ** Populate pCsr->aMatchinfo[] with data for the current row. The
141030 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
141031 */
141032 static int fts3GetMatchinfo(
141033   Fts3Cursor *pCsr,               /* FTS3 Cursor object */
141034   const char *zArg                /* Second argument to matchinfo() function */
141035 ){
141036   MatchInfo sInfo;
141037   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
141038   int rc = SQLITE_OK;
141039   int bGlobal = 0;                /* Collect 'global' stats as well as local */
141040 
141041   memset(&sInfo, 0, sizeof(MatchInfo));
141042   sInfo.pCursor = pCsr;
141043   sInfo.nCol = pTab->nColumn;
141044 
141045   /* If there is cached matchinfo() data, but the format string for the
141046   ** cache does not match the format string for this request, discard
141047   ** the cached data. */
141048   if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
141049     assert( pCsr->aMatchinfo );
141050     sqlite3_free(pCsr->aMatchinfo);
141051     pCsr->zMatchinfo = 0;
141052     pCsr->aMatchinfo = 0;
141053   }
141054 
141055   /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
141056   ** matchinfo function has been called for this query. In this case
141057   ** allocate the array used to accumulate the matchinfo data and
141058   ** initialize those elements that are constant for every row.
141059   */
141060   if( pCsr->aMatchinfo==0 ){
141061     int nMatchinfo = 0;           /* Number of u32 elements in match-info */
141062     int nArg;                     /* Bytes in zArg */
141063     int i;                        /* Used to iterate through zArg */
141064 
141065     /* Determine the number of phrases in the query */
141066     pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
141067     sInfo.nPhrase = pCsr->nPhrase;
141068 
141069     /* Determine the number of integers in the buffer returned by this call. */
141070     for(i=0; zArg[i]; i++){
141071       nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
141072     }
141073 
141074     /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
141075     nArg = (int)strlen(zArg);
141076     pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
141077     if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
141078 
141079     pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
141080     pCsr->nMatchinfo = nMatchinfo;
141081     memcpy(pCsr->zMatchinfo, zArg, nArg+1);
141082     memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
141083     pCsr->isMatchinfoNeeded = 1;
141084     bGlobal = 1;
141085   }
141086 
141087   sInfo.aMatchinfo = pCsr->aMatchinfo;
141088   sInfo.nPhrase = pCsr->nPhrase;
141089   if( pCsr->isMatchinfoNeeded ){
141090     rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
141091     pCsr->isMatchinfoNeeded = 0;
141092   }
141093 
141094   return rc;
141095 }
141096 
141097 /*
141098 ** Implementation of snippet() function.
141099 */
141100 SQLITE_PRIVATE void sqlite3Fts3Snippet(
141101   sqlite3_context *pCtx,          /* SQLite function call context */
141102   Fts3Cursor *pCsr,               /* Cursor object */
141103   const char *zStart,             /* Snippet start text - "<b>" */
141104   const char *zEnd,               /* Snippet end text - "</b>" */
141105   const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
141106   int iCol,                       /* Extract snippet from this column */
141107   int nToken                      /* Approximate number of tokens in snippet */
141108 ){
141109   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
141110   int rc = SQLITE_OK;
141111   int i;
141112   StrBuffer res = {0, 0, 0};
141113 
141114   /* The returned text includes up to four fragments of text extracted from
141115   ** the data in the current row. The first iteration of the for(...) loop
141116   ** below attempts to locate a single fragment of text nToken tokens in
141117   ** size that contains at least one instance of all phrases in the query
141118   ** expression that appear in the current row. If such a fragment of text
141119   ** cannot be found, the second iteration of the loop attempts to locate
141120   ** a pair of fragments, and so on.
141121   */
141122   int nSnippet = 0;               /* Number of fragments in this snippet */
141123   SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
141124   int nFToken = -1;               /* Number of tokens in each fragment */
141125 
141126   if( !pCsr->pExpr ){
141127     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
141128     return;
141129   }
141130 
141131   for(nSnippet=1; 1; nSnippet++){
141132 
141133     int iSnip;                    /* Loop counter 0..nSnippet-1 */
141134     u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
141135     u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
141136 
141137     if( nToken>=0 ){
141138       nFToken = (nToken+nSnippet-1) / nSnippet;
141139     }else{
141140       nFToken = -1 * nToken;
141141     }
141142 
141143     for(iSnip=0; iSnip<nSnippet; iSnip++){
141144       int iBestScore = -1;        /* Best score of columns checked so far */
141145       int iRead;                  /* Used to iterate through columns */
141146       SnippetFragment *pFragment = &aSnippet[iSnip];
141147 
141148       memset(pFragment, 0, sizeof(*pFragment));
141149 
141150       /* Loop through all columns of the table being considered for snippets.
141151       ** If the iCol argument to this function was negative, this means all
141152       ** columns of the FTS3 table. Otherwise, only column iCol is considered.
141153       */
141154       for(iRead=0; iRead<pTab->nColumn; iRead++){
141155         SnippetFragment sF = {0, 0, 0, 0};
141156         int iS;
141157         if( iCol>=0 && iRead!=iCol ) continue;
141158 
141159         /* Find the best snippet of nFToken tokens in column iRead. */
141160         rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
141161         if( rc!=SQLITE_OK ){
141162           goto snippet_out;
141163         }
141164         if( iS>iBestScore ){
141165           *pFragment = sF;
141166           iBestScore = iS;
141167         }
141168       }
141169 
141170       mCovered |= pFragment->covered;
141171     }
141172 
141173     /* If all query phrases seen by fts3BestSnippet() are present in at least
141174     ** one of the nSnippet snippet fragments, break out of the loop.
141175     */
141176     assert( (mCovered&mSeen)==mCovered );
141177     if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
141178   }
141179 
141180   assert( nFToken>0 );
141181 
141182   for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
141183     rc = fts3SnippetText(pCsr, &aSnippet[i],
141184         i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
141185     );
141186   }
141187 
141188  snippet_out:
141189   sqlite3Fts3SegmentsClose(pTab);
141190   if( rc!=SQLITE_OK ){
141191     sqlite3_result_error_code(pCtx, rc);
141192     sqlite3_free(res.z);
141193   }else{
141194     sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
141195   }
141196 }
141197 
141198 
141199 typedef struct TermOffset TermOffset;
141200 typedef struct TermOffsetCtx TermOffsetCtx;
141201 
141202 struct TermOffset {
141203   char *pList;                    /* Position-list */
141204   int iPos;                       /* Position just read from pList */
141205   int iOff;                       /* Offset of this term from read positions */
141206 };
141207 
141208 struct TermOffsetCtx {
141209   Fts3Cursor *pCsr;
141210   int iCol;                       /* Column of table to populate aTerm for */
141211   int iTerm;
141212   sqlite3_int64 iDocid;
141213   TermOffset *aTerm;
141214 };
141215 
141216 /*
141217 ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
141218 */
141219 static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
141220   TermOffsetCtx *p = (TermOffsetCtx *)ctx;
141221   int nTerm;                      /* Number of tokens in phrase */
141222   int iTerm;                      /* For looping through nTerm phrase terms */
141223   char *pList;                    /* Pointer to position list for phrase */
141224   int iPos = 0;                   /* First position in position-list */
141225   int rc;
141226 
141227   UNUSED_PARAMETER(iPhrase);
141228   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
141229   nTerm = pExpr->pPhrase->nToken;
141230   if( pList ){
141231     fts3GetDeltaPosition(&pList, &iPos);
141232     assert( iPos>=0 );
141233   }
141234 
141235   for(iTerm=0; iTerm<nTerm; iTerm++){
141236     TermOffset *pT = &p->aTerm[p->iTerm++];
141237     pT->iOff = nTerm-iTerm-1;
141238     pT->pList = pList;
141239     pT->iPos = iPos;
141240   }
141241 
141242   return rc;
141243 }
141244 
141245 /*
141246 ** Implementation of offsets() function.
141247 */
141248 SQLITE_PRIVATE void sqlite3Fts3Offsets(
141249   sqlite3_context *pCtx,          /* SQLite function call context */
141250   Fts3Cursor *pCsr                /* Cursor object */
141251 ){
141252   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
141253   sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
141254   int rc;                         /* Return Code */
141255   int nToken;                     /* Number of tokens in query */
141256   int iCol;                       /* Column currently being processed */
141257   StrBuffer res = {0, 0, 0};      /* Result string */
141258   TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
141259 
141260   if( !pCsr->pExpr ){
141261     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
141262     return;
141263   }
141264 
141265   memset(&sCtx, 0, sizeof(sCtx));
141266   assert( pCsr->isRequireSeek==0 );
141267 
141268   /* Count the number of terms in the query */
141269   rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
141270   if( rc!=SQLITE_OK ) goto offsets_out;
141271 
141272   /* Allocate the array of TermOffset iterators. */
141273   sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
141274   if( 0==sCtx.aTerm ){
141275     rc = SQLITE_NOMEM;
141276     goto offsets_out;
141277   }
141278   sCtx.iDocid = pCsr->iPrevId;
141279   sCtx.pCsr = pCsr;
141280 
141281   /* Loop through the table columns, appending offset information to
141282   ** string-buffer res for each column.
141283   */
141284   for(iCol=0; iCol<pTab->nColumn; iCol++){
141285     sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
141286     const char *ZDUMMY;           /* Dummy argument used with xNext() */
141287     int NDUMMY = 0;               /* Dummy argument used with xNext() */
141288     int iStart = 0;
141289     int iEnd = 0;
141290     int iCurrent = 0;
141291     const char *zDoc;
141292     int nDoc;
141293 
141294     /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
141295     ** no way that this operation can fail, so the return code from
141296     ** fts3ExprIterate() can be discarded.
141297     */
141298     sCtx.iCol = iCol;
141299     sCtx.iTerm = 0;
141300     (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
141301 
141302     /* Retreive the text stored in column iCol. If an SQL NULL is stored
141303     ** in column iCol, jump immediately to the next iteration of the loop.
141304     ** If an OOM occurs while retrieving the data (this can happen if SQLite
141305     ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
141306     ** to the caller.
141307     */
141308     zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
141309     nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
141310     if( zDoc==0 ){
141311       if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
141312         continue;
141313       }
141314       rc = SQLITE_NOMEM;
141315       goto offsets_out;
141316     }
141317 
141318     /* Initialize a tokenizer iterator to iterate through column iCol. */
141319     rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
141320         zDoc, nDoc, &pC
141321     );
141322     if( rc!=SQLITE_OK ) goto offsets_out;
141323 
141324     rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
141325     while( rc==SQLITE_OK ){
141326       int i;                      /* Used to loop through terms */
141327       int iMinPos = 0x7FFFFFFF;   /* Position of next token */
141328       TermOffset *pTerm = 0;      /* TermOffset associated with next token */
141329 
141330       for(i=0; i<nToken; i++){
141331         TermOffset *pT = &sCtx.aTerm[i];
141332         if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
141333           iMinPos = pT->iPos-pT->iOff;
141334           pTerm = pT;
141335         }
141336       }
141337 
141338       if( !pTerm ){
141339         /* All offsets for this column have been gathered. */
141340         rc = SQLITE_DONE;
141341       }else{
141342         assert( iCurrent<=iMinPos );
141343         if( 0==(0xFE&*pTerm->pList) ){
141344           pTerm->pList = 0;
141345         }else{
141346           fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
141347         }
141348         while( rc==SQLITE_OK && iCurrent<iMinPos ){
141349           rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
141350         }
141351         if( rc==SQLITE_OK ){
141352           char aBuffer[64];
141353           sqlite3_snprintf(sizeof(aBuffer), aBuffer,
141354               "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
141355           );
141356           rc = fts3StringAppend(&res, aBuffer, -1);
141357         }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
141358           rc = FTS_CORRUPT_VTAB;
141359         }
141360       }
141361     }
141362     if( rc==SQLITE_DONE ){
141363       rc = SQLITE_OK;
141364     }
141365 
141366     pMod->xClose(pC);
141367     if( rc!=SQLITE_OK ) goto offsets_out;
141368   }
141369 
141370  offsets_out:
141371   sqlite3_free(sCtx.aTerm);
141372   assert( rc!=SQLITE_DONE );
141373   sqlite3Fts3SegmentsClose(pTab);
141374   if( rc!=SQLITE_OK ){
141375     sqlite3_result_error_code(pCtx,  rc);
141376     sqlite3_free(res.z);
141377   }else{
141378     sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
141379   }
141380   return;
141381 }
141382 
141383 /*
141384 ** Implementation of matchinfo() function.
141385 */
141386 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
141387   sqlite3_context *pContext,      /* Function call context */
141388   Fts3Cursor *pCsr,               /* FTS3 table cursor */
141389   const char *zArg                /* Second arg to matchinfo() function */
141390 ){
141391   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
141392   int rc;
141393   int i;
141394   const char *zFormat;
141395 
141396   if( zArg ){
141397     for(i=0; zArg[i]; i++){
141398       char *zErr = 0;
141399       if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
141400         sqlite3_result_error(pContext, zErr, -1);
141401         sqlite3_free(zErr);
141402         return;
141403       }
141404     }
141405     zFormat = zArg;
141406   }else{
141407     zFormat = FTS3_MATCHINFO_DEFAULT;
141408   }
141409 
141410   if( !pCsr->pExpr ){
141411     sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
141412     return;
141413   }
141414 
141415   /* Retrieve matchinfo() data. */
141416   rc = fts3GetMatchinfo(pCsr, zFormat);
141417   sqlite3Fts3SegmentsClose(pTab);
141418 
141419   if( rc!=SQLITE_OK ){
141420     sqlite3_result_error_code(pContext, rc);
141421   }else{
141422     int n = pCsr->nMatchinfo * sizeof(u32);
141423     sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
141424   }
141425 }
141426 
141427 #endif
141428 
141429 /************** End of fts3_snippet.c ****************************************/
141430 /************** Begin file fts3_unicode.c ************************************/
141431 /*
141432 ** 2012 May 24
141433 **
141434 ** The author disclaims copyright to this source code.  In place of
141435 ** a legal notice, here is a blessing:
141436 **
141437 **    May you do good and not evil.
141438 **    May you find forgiveness for yourself and forgive others.
141439 **    May you share freely, never taking more than you give.
141440 **
141441 ******************************************************************************
141442 **
141443 ** Implementation of the "unicode" full-text-search tokenizer.
141444 */
141445 
141446 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
141447 
141448 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
141449 
141450 /* #include <assert.h> */
141451 /* #include <stdlib.h> */
141452 /* #include <stdio.h> */
141453 /* #include <string.h> */
141454 
141455 
141456 /*
141457 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
141458 ** from the sqlite3 source file utf.c. If this file is compiled as part
141459 ** of the amalgamation, they are not required.
141460 */
141461 #ifndef SQLITE_AMALGAMATION
141462 
141463 static const unsigned char sqlite3Utf8Trans1[] = {
141464   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
141465   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
141466   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
141467   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
141468   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
141469   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
141470   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
141471   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
141472 };
141473 
141474 #define READ_UTF8(zIn, zTerm, c)                           \
141475   c = *(zIn++);                                            \
141476   if( c>=0xc0 ){                                           \
141477     c = sqlite3Utf8Trans1[c-0xc0];                         \
141478     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
141479       c = (c<<6) + (0x3f & *(zIn++));                      \
141480     }                                                      \
141481     if( c<0x80                                             \
141482         || (c&0xFFFFF800)==0xD800                          \
141483         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
141484   }
141485 
141486 #define WRITE_UTF8(zOut, c) {                          \
141487   if( c<0x00080 ){                                     \
141488     *zOut++ = (u8)(c&0xFF);                            \
141489   }                                                    \
141490   else if( c<0x00800 ){                                \
141491     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
141492     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
141493   }                                                    \
141494   else if( c<0x10000 ){                                \
141495     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
141496     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
141497     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
141498   }else{                                               \
141499     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
141500     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
141501     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
141502     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
141503   }                                                    \
141504 }
141505 
141506 #endif /* ifndef SQLITE_AMALGAMATION */
141507 
141508 typedef struct unicode_tokenizer unicode_tokenizer;
141509 typedef struct unicode_cursor unicode_cursor;
141510 
141511 struct unicode_tokenizer {
141512   sqlite3_tokenizer base;
141513   int bRemoveDiacritic;
141514   int nException;
141515   int *aiException;
141516 };
141517 
141518 struct unicode_cursor {
141519   sqlite3_tokenizer_cursor base;
141520   const unsigned char *aInput;    /* Input text being tokenized */
141521   int nInput;                     /* Size of aInput[] in bytes */
141522   int iOff;                       /* Current offset within aInput[] */
141523   int iToken;                     /* Index of next token to be returned */
141524   char *zToken;                   /* storage for current token */
141525   int nAlloc;                     /* space allocated at zToken */
141526 };
141527 
141528 
141529 /*
141530 ** Destroy a tokenizer allocated by unicodeCreate().
141531 */
141532 static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
141533   if( pTokenizer ){
141534     unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
141535     sqlite3_free(p->aiException);
141536     sqlite3_free(p);
141537   }
141538   return SQLITE_OK;
141539 }
141540 
141541 /*
141542 ** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
141543 ** statement has specified that the tokenizer for this table shall consider
141544 ** all characters in string zIn/nIn to be separators (if bAlnum==0) or
141545 ** token characters (if bAlnum==1).
141546 **
141547 ** For each codepoint in the zIn/nIn string, this function checks if the
141548 ** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
141549 ** If so, no action is taken. Otherwise, the codepoint is added to the
141550 ** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
141551 ** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
141552 ** codepoints in the aiException[] array.
141553 **
141554 ** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
141555 ** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
141556 ** It is not possible to change the behavior of the tokenizer with respect
141557 ** to these codepoints.
141558 */
141559 static int unicodeAddExceptions(
141560   unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
141561   int bAlnum,                     /* Replace Isalnum() return value with this */
141562   const char *zIn,                /* Array of characters to make exceptions */
141563   int nIn                         /* Length of z in bytes */
141564 ){
141565   const unsigned char *z = (const unsigned char *)zIn;
141566   const unsigned char *zTerm = &z[nIn];
141567   int iCode;
141568   int nEntry = 0;
141569 
141570   assert( bAlnum==0 || bAlnum==1 );
141571 
141572   while( z<zTerm ){
141573     READ_UTF8(z, zTerm, iCode);
141574     assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
141575     if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
141576      && sqlite3FtsUnicodeIsdiacritic(iCode)==0
141577     ){
141578       nEntry++;
141579     }
141580   }
141581 
141582   if( nEntry ){
141583     int *aNew;                    /* New aiException[] array */
141584     int nNew;                     /* Number of valid entries in array aNew[] */
141585 
141586     aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
141587     if( aNew==0 ) return SQLITE_NOMEM;
141588     nNew = p->nException;
141589 
141590     z = (const unsigned char *)zIn;
141591     while( z<zTerm ){
141592       READ_UTF8(z, zTerm, iCode);
141593       if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
141594        && sqlite3FtsUnicodeIsdiacritic(iCode)==0
141595       ){
141596         int i, j;
141597         for(i=0; i<nNew && aNew[i]<iCode; i++);
141598         for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
141599         aNew[i] = iCode;
141600         nNew++;
141601       }
141602     }
141603     p->aiException = aNew;
141604     p->nException = nNew;
141605   }
141606 
141607   return SQLITE_OK;
141608 }
141609 
141610 /*
141611 ** Return true if the p->aiException[] array contains the value iCode.
141612 */
141613 static int unicodeIsException(unicode_tokenizer *p, int iCode){
141614   if( p->nException>0 ){
141615     int *a = p->aiException;
141616     int iLo = 0;
141617     int iHi = p->nException-1;
141618 
141619     while( iHi>=iLo ){
141620       int iTest = (iHi + iLo) / 2;
141621       if( iCode==a[iTest] ){
141622         return 1;
141623       }else if( iCode>a[iTest] ){
141624         iLo = iTest+1;
141625       }else{
141626         iHi = iTest-1;
141627       }
141628     }
141629   }
141630 
141631   return 0;
141632 }
141633 
141634 /*
141635 ** Return true if, for the purposes of tokenization, codepoint iCode is
141636 ** considered a token character (not a separator).
141637 */
141638 static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
141639   assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
141640   return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
141641 }
141642 
141643 /*
141644 ** Create a new tokenizer instance.
141645 */
141646 static int unicodeCreate(
141647   int nArg,                       /* Size of array argv[] */
141648   const char * const *azArg,      /* Tokenizer creation arguments */
141649   sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
141650 ){
141651   unicode_tokenizer *pNew;        /* New tokenizer object */
141652   int i;
141653   int rc = SQLITE_OK;
141654 
141655   pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
141656   if( pNew==NULL ) return SQLITE_NOMEM;
141657   memset(pNew, 0, sizeof(unicode_tokenizer));
141658   pNew->bRemoveDiacritic = 1;
141659 
141660   for(i=0; rc==SQLITE_OK && i<nArg; i++){
141661     const char *z = azArg[i];
141662     int n = strlen(z);
141663 
141664     if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
141665       pNew->bRemoveDiacritic = 1;
141666     }
141667     else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
141668       pNew->bRemoveDiacritic = 0;
141669     }
141670     else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
141671       rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
141672     }
141673     else if( n>=11 && memcmp("separators=", z, 11)==0 ){
141674       rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
141675     }
141676     else{
141677       /* Unrecognized argument */
141678       rc  = SQLITE_ERROR;
141679     }
141680   }
141681 
141682   if( rc!=SQLITE_OK ){
141683     unicodeDestroy((sqlite3_tokenizer *)pNew);
141684     pNew = 0;
141685   }
141686   *pp = (sqlite3_tokenizer *)pNew;
141687   return rc;
141688 }
141689 
141690 /*
141691 ** Prepare to begin tokenizing a particular string.  The input
141692 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
141693 ** used to incrementally tokenize this string is returned in
141694 ** *ppCursor.
141695 */
141696 static int unicodeOpen(
141697   sqlite3_tokenizer *p,           /* The tokenizer */
141698   const char *aInput,             /* Input string */
141699   int nInput,                     /* Size of string aInput in bytes */
141700   sqlite3_tokenizer_cursor **pp   /* OUT: New cursor object */
141701 ){
141702   unicode_cursor *pCsr;
141703 
141704   pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
141705   if( pCsr==0 ){
141706     return SQLITE_NOMEM;
141707   }
141708   memset(pCsr, 0, sizeof(unicode_cursor));
141709 
141710   pCsr->aInput = (const unsigned char *)aInput;
141711   if( aInput==0 ){
141712     pCsr->nInput = 0;
141713   }else if( nInput<0 ){
141714     pCsr->nInput = (int)strlen(aInput);
141715   }else{
141716     pCsr->nInput = nInput;
141717   }
141718 
141719   *pp = &pCsr->base;
141720   UNUSED_PARAMETER(p);
141721   return SQLITE_OK;
141722 }
141723 
141724 /*
141725 ** Close a tokenization cursor previously opened by a call to
141726 ** simpleOpen() above.
141727 */
141728 static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
141729   unicode_cursor *pCsr = (unicode_cursor *) pCursor;
141730   sqlite3_free(pCsr->zToken);
141731   sqlite3_free(pCsr);
141732   return SQLITE_OK;
141733 }
141734 
141735 /*
141736 ** Extract the next token from a tokenization cursor.  The cursor must
141737 ** have been opened by a prior call to simpleOpen().
141738 */
141739 static int unicodeNext(
141740   sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
141741   const char **paToken,           /* OUT: Token text */
141742   int *pnToken,                   /* OUT: Number of bytes at *paToken */
141743   int *piStart,                   /* OUT: Starting offset of token */
141744   int *piEnd,                     /* OUT: Ending offset of token */
141745   int *piPos                      /* OUT: Position integer of token */
141746 ){
141747   unicode_cursor *pCsr = (unicode_cursor *)pC;
141748   unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
141749   int iCode;
141750   char *zOut;
141751   const unsigned char *z = &pCsr->aInput[pCsr->iOff];
141752   const unsigned char *zStart = z;
141753   const unsigned char *zEnd;
141754   const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
141755 
141756   /* Scan past any delimiter characters before the start of the next token.
141757   ** Return SQLITE_DONE early if this takes us all the way to the end of
141758   ** the input.  */
141759   while( z<zTerm ){
141760     READ_UTF8(z, zTerm, iCode);
141761     if( unicodeIsAlnum(p, iCode) ) break;
141762     zStart = z;
141763   }
141764   if( zStart>=zTerm ) return SQLITE_DONE;
141765 
141766   zOut = pCsr->zToken;
141767   do {
141768     int iOut;
141769 
141770     /* Grow the output buffer if required. */
141771     if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
141772       char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
141773       if( !zNew ) return SQLITE_NOMEM;
141774       zOut = &zNew[zOut - pCsr->zToken];
141775       pCsr->zToken = zNew;
141776       pCsr->nAlloc += 64;
141777     }
141778 
141779     /* Write the folded case of the last character read to the output */
141780     zEnd = z;
141781     iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
141782     if( iOut ){
141783       WRITE_UTF8(zOut, iOut);
141784     }
141785 
141786     /* If the cursor is not at EOF, read the next character */
141787     if( z>=zTerm ) break;
141788     READ_UTF8(z, zTerm, iCode);
141789   }while( unicodeIsAlnum(p, iCode)
141790        || sqlite3FtsUnicodeIsdiacritic(iCode)
141791   );
141792 
141793   /* Set the output variables and return. */
141794   pCsr->iOff = (z - pCsr->aInput);
141795   *paToken = pCsr->zToken;
141796   *pnToken = zOut - pCsr->zToken;
141797   *piStart = (zStart - pCsr->aInput);
141798   *piEnd = (zEnd - pCsr->aInput);
141799   *piPos = pCsr->iToken++;
141800   return SQLITE_OK;
141801 }
141802 
141803 /*
141804 ** Set *ppModule to a pointer to the sqlite3_tokenizer_module
141805 ** structure for the unicode tokenizer.
141806 */
141807 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
141808   static const sqlite3_tokenizer_module module = {
141809     0,
141810     unicodeCreate,
141811     unicodeDestroy,
141812     unicodeOpen,
141813     unicodeClose,
141814     unicodeNext,
141815     0,
141816   };
141817   *ppModule = &module;
141818 }
141819 
141820 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
141821 #endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
141822 
141823 /************** End of fts3_unicode.c ****************************************/
141824 /************** Begin file fts3_unicode2.c ***********************************/
141825 /*
141826 ** 2012 May 25
141827 **
141828 ** The author disclaims copyright to this source code.  In place of
141829 ** a legal notice, here is a blessing:
141830 **
141831 **    May you do good and not evil.
141832 **    May you find forgiveness for yourself and forgive others.
141833 **    May you share freely, never taking more than you give.
141834 **
141835 ******************************************************************************
141836 */
141837 
141838 /*
141839 ** DO NOT EDIT THIS MACHINE GENERATED FILE.
141840 */
141841 
141842 #if defined(SQLITE_ENABLE_FTS4_UNICODE61)
141843 #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
141844 
141845 /* #include <assert.h> */
141846 
141847 /*
141848 ** Return true if the argument corresponds to a unicode codepoint
141849 ** classified as either a letter or a number. Otherwise false.
141850 **
141851 ** The results are undefined if the value passed to this function
141852 ** is less than zero.
141853 */
141854 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
141855   /* Each unsigned integer in the following array corresponds to a contiguous
141856   ** range of unicode codepoints that are not either letters or numbers (i.e.
141857   ** codepoints for which this function should return 0).
141858   **
141859   ** The most significant 22 bits in each 32-bit value contain the first
141860   ** codepoint in the range. The least significant 10 bits are used to store
141861   ** the size of the range (always at least 1). In other words, the value
141862   ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
141863   ** C. It is not possible to represent a range larger than 1023 codepoints
141864   ** using this format.
141865   */
141866   const static unsigned int aEntry[] = {
141867     0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
141868     0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
141869     0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
141870     0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
141871     0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
141872     0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
141873     0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
141874     0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
141875     0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
141876     0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
141877     0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
141878     0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
141879     0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
141880     0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
141881     0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
141882     0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
141883     0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
141884     0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
141885     0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
141886     0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
141887     0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
141888     0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
141889     0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
141890     0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
141891     0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
141892     0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
141893     0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
141894     0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
141895     0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
141896     0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
141897     0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
141898     0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
141899     0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
141900     0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
141901     0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
141902     0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
141903     0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
141904     0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
141905     0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
141906     0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
141907     0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
141908     0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
141909     0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
141910     0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
141911     0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
141912     0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
141913     0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
141914     0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
141915     0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
141916     0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
141917     0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
141918     0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
141919     0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
141920     0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
141921     0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
141922     0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
141923     0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
141924     0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
141925     0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
141926     0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
141927     0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
141928     0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
141929     0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
141930     0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
141931     0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
141932     0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
141933     0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
141934     0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
141935     0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
141936     0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
141937     0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
141938     0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
141939     0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
141940     0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
141941     0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
141942     0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
141943     0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
141944     0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
141945     0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
141946     0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
141947     0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
141948     0x380400F0,
141949   };
141950   static const unsigned int aAscii[4] = {
141951     0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
141952   };
141953 
141954   if( c<128 ){
141955     return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
141956   }else if( c<(1<<22) ){
141957     unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
141958     int iRes;
141959     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
141960     int iLo = 0;
141961     while( iHi>=iLo ){
141962       int iTest = (iHi + iLo) / 2;
141963       if( key >= aEntry[iTest] ){
141964         iRes = iTest;
141965         iLo = iTest+1;
141966       }else{
141967         iHi = iTest-1;
141968       }
141969     }
141970     assert( aEntry[0]<key );
141971     assert( key>=aEntry[iRes] );
141972     return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
141973   }
141974   return 1;
141975 }
141976 
141977 
141978 /*
141979 ** If the argument is a codepoint corresponding to a lowercase letter
141980 ** in the ASCII range with a diacritic added, return the codepoint
141981 ** of the ASCII letter only. For example, if passed 235 - "LATIN
141982 ** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
141983 ** E"). The resuls of passing a codepoint that corresponds to an
141984 ** uppercase letter are undefined.
141985 */
141986 static int remove_diacritic(int c){
141987   unsigned short aDia[] = {
141988         0,  1797,  1848,  1859,  1891,  1928,  1940,  1995,
141989      2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286,
141990      2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732,
141991      2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336,
141992      3456,  3696,  3712,  3728,  3744,  3896,  3912,  3928,
141993      3968,  4008,  4040,  4106,  4138,  4170,  4202,  4234,
141994      4266,  4296,  4312,  4344,  4408,  4424,  4472,  4504,
141995      6148,  6198,  6264,  6280,  6360,  6429,  6505,  6529,
141996     61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726,
141997     61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122,
141998     62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536,
141999     62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730,
142000     62924, 63050, 63082, 63274, 63390,
142001   };
142002   char aChar[] = {
142003     '\0', 'a',  'c',  'e',  'i',  'n',  'o',  'u',  'y',  'y',  'a',  'c',
142004     'd',  'e',  'e',  'g',  'h',  'i',  'j',  'k',  'l',  'n',  'o',  'r',
142005     's',  't',  'u',  'u',  'w',  'y',  'z',  'o',  'u',  'a',  'i',  'o',
142006     'u',  'g',  'k',  'o',  'j',  'g',  'n',  'a',  'e',  'i',  'o',  'r',
142007     'u',  's',  't',  'h',  'a',  'e',  'o',  'y',  '\0', '\0', '\0', '\0',
142008     '\0', '\0', '\0', '\0', 'a',  'b',  'd',  'd',  'e',  'f',  'g',  'h',
142009     'h',  'i',  'k',  'l',  'l',  'm',  'n',  'p',  'r',  'r',  's',  't',
142010     'u',  'v',  'w',  'w',  'x',  'y',  'z',  'h',  't',  'w',  'y',  'a',
142011     'e',  'i',  'o',  'u',  'y',
142012   };
142013 
142014   unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
142015   int iRes = 0;
142016   int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
142017   int iLo = 0;
142018   while( iHi>=iLo ){
142019     int iTest = (iHi + iLo) / 2;
142020     if( key >= aDia[iTest] ){
142021       iRes = iTest;
142022       iLo = iTest+1;
142023     }else{
142024       iHi = iTest-1;
142025     }
142026   }
142027   assert( key>=aDia[iRes] );
142028   return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
142029 };
142030 
142031 
142032 /*
142033 ** Return true if the argument interpreted as a unicode codepoint
142034 ** is a diacritical modifier character.
142035 */
142036 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
142037   unsigned int mask0 = 0x08029FDF;
142038   unsigned int mask1 = 0x000361F8;
142039   if( c<768 || c>817 ) return 0;
142040   return (c < 768+32) ?
142041       (mask0 & (1 << (c-768))) :
142042       (mask1 & (1 << (c-768-32)));
142043 }
142044 
142045 
142046 /*
142047 ** Interpret the argument as a unicode codepoint. If the codepoint
142048 ** is an upper case character that has a lower case equivalent,
142049 ** return the codepoint corresponding to the lower case version.
142050 ** Otherwise, return a copy of the argument.
142051 **
142052 ** The results are undefined if the value passed to this function
142053 ** is less than zero.
142054 */
142055 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
142056   /* Each entry in the following array defines a rule for folding a range
142057   ** of codepoints to lower case. The rule applies to a range of nRange
142058   ** codepoints starting at codepoint iCode.
142059   **
142060   ** If the least significant bit in flags is clear, then the rule applies
142061   ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
142062   ** need to be folded). Or, if it is set, then the rule only applies to
142063   ** every second codepoint in the range, starting with codepoint C.
142064   **
142065   ** The 7 most significant bits in flags are an index into the aiOff[]
142066   ** array. If a specific codepoint C does require folding, then its lower
142067   ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
142068   **
142069   ** The contents of this array are generated by parsing the CaseFolding.txt
142070   ** file distributed as part of the "Unicode Character Database". See
142071   ** http://www.unicode.org for details.
142072   */
142073   static const struct TableEntry {
142074     unsigned short iCode;
142075     unsigned char flags;
142076     unsigned char nRange;
142077   } aEntry[] = {
142078     {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
142079     {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
142080     {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
142081     {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
142082     {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
142083     {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
142084     {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
142085     {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
142086     {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
142087     {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
142088     {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
142089     {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
142090     {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
142091     {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
142092     {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
142093     {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
142094     {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
142095     {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
142096     {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
142097     {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
142098     {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
142099     {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
142100     {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
142101     {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
142102     {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
142103     {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
142104     {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
142105     {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
142106     {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
142107     {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
142108     {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
142109     {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
142110     {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
142111     {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
142112     {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
142113     {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
142114     {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
142115     {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
142116     {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
142117     {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
142118     {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
142119     {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
142120     {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
142121     {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
142122     {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
142123     {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
142124     {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
142125     {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
142126     {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
142127     {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
142128     {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
142129     {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
142130     {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
142131     {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
142132     {65313, 14, 26},
142133   };
142134   static const unsigned short aiOff[] = {
142135    1,     2,     8,     15,    16,    26,    28,    32,
142136    37,    38,    40,    48,    63,    64,    69,    71,
142137    79,    80,    116,   202,   203,   205,   206,   207,
142138    209,   210,   211,   213,   214,   217,   218,   219,
142139    775,   7264,  10792, 10795, 23228, 23256, 30204, 54721,
142140    54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
142141    57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
142142    65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
142143    65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
142144    65514, 65521, 65527, 65528, 65529,
142145   };
142146 
142147   int ret = c;
142148 
142149   assert( c>=0 );
142150   assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
142151 
142152   if( c<128 ){
142153     if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
142154   }else if( c<65536 ){
142155     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
142156     int iLo = 0;
142157     int iRes = -1;
142158 
142159     while( iHi>=iLo ){
142160       int iTest = (iHi + iLo) / 2;
142161       int cmp = (c - aEntry[iTest].iCode);
142162       if( cmp>=0 ){
142163         iRes = iTest;
142164         iLo = iTest+1;
142165       }else{
142166         iHi = iTest-1;
142167       }
142168     }
142169     assert( iRes<0 || c>=aEntry[iRes].iCode );
142170 
142171     if( iRes>=0 ){
142172       const struct TableEntry *p = &aEntry[iRes];
142173       if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
142174         ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
142175         assert( ret>0 );
142176       }
142177     }
142178 
142179     if( bRemoveDiacritic ) ret = remove_diacritic(ret);
142180   }
142181 
142182   else if( c>=66560 && c<66600 ){
142183     ret = c + 40;
142184   }
142185 
142186   return ret;
142187 }
142188 #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
142189 #endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
142190 
142191 /************** End of fts3_unicode2.c ***************************************/
142192 /************** Begin file rtree.c *******************************************/
142193 /*
142194 ** 2001 September 15
142195 **
142196 ** The author disclaims copyright to this source code.  In place of
142197 ** a legal notice, here is a blessing:
142198 **
142199 **    May you do good and not evil.
142200 **    May you find forgiveness for yourself and forgive others.
142201 **    May you share freely, never taking more than you give.
142202 **
142203 *************************************************************************
142204 ** This file contains code for implementations of the r-tree and r*-tree
142205 ** algorithms packaged as an SQLite virtual table module.
142206 */
142207 
142208 /*
142209 ** Database Format of R-Tree Tables
142210 ** --------------------------------
142211 **
142212 ** The data structure for a single virtual r-tree table is stored in three
142213 ** native SQLite tables declared as follows. In each case, the '%' character
142214 ** in the table name is replaced with the user-supplied name of the r-tree
142215 ** table.
142216 **
142217 **   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
142218 **   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
142219 **   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
142220 **
142221 ** The data for each node of the r-tree structure is stored in the %_node
142222 ** table. For each node that is not the root node of the r-tree, there is
142223 ** an entry in the %_parent table associating the node with its parent.
142224 ** And for each row of data in the table, there is an entry in the %_rowid
142225 ** table that maps from the entries rowid to the id of the node that it
142226 ** is stored on.
142227 **
142228 ** The root node of an r-tree always exists, even if the r-tree table is
142229 ** empty. The nodeno of the root node is always 1. All other nodes in the
142230 ** table must be the same size as the root node. The content of each node
142231 ** is formatted as follows:
142232 **
142233 **   1. If the node is the root node (node 1), then the first 2 bytes
142234 **      of the node contain the tree depth as a big-endian integer.
142235 **      For non-root nodes, the first 2 bytes are left unused.
142236 **
142237 **   2. The next 2 bytes contain the number of entries currently
142238 **      stored in the node.
142239 **
142240 **   3. The remainder of the node contains the node entries. Each entry
142241 **      consists of a single 8-byte integer followed by an even number
142242 **      of 4-byte coordinates. For leaf nodes the integer is the rowid
142243 **      of a record. For internal nodes it is the node number of a
142244 **      child page.
142245 */
142246 
142247 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
142248 
142249 /*
142250 ** This file contains an implementation of a couple of different variants
142251 ** of the r-tree algorithm. See the README file for further details. The
142252 ** same data-structure is used for all, but the algorithms for insert and
142253 ** delete operations vary. The variants used are selected at compile time
142254 ** by defining the following symbols:
142255 */
142256 
142257 /* Either, both or none of the following may be set to activate
142258 ** r*tree variant algorithms.
142259 */
142260 #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
142261 #define VARIANT_RSTARTREE_REINSERT      1
142262 
142263 /*
142264 ** Exactly one of the following must be set to 1.
142265 */
142266 #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
142267 #define VARIANT_GUTTMAN_LINEAR_SPLIT    0
142268 #define VARIANT_RSTARTREE_SPLIT         1
142269 
142270 #define VARIANT_GUTTMAN_SPLIT \
142271         (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
142272 
142273 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
142274   #define PickNext QuadraticPickNext
142275   #define PickSeeds QuadraticPickSeeds
142276   #define AssignCells splitNodeGuttman
142277 #endif
142278 #if VARIANT_GUTTMAN_LINEAR_SPLIT
142279   #define PickNext LinearPickNext
142280   #define PickSeeds LinearPickSeeds
142281   #define AssignCells splitNodeGuttman
142282 #endif
142283 #if VARIANT_RSTARTREE_SPLIT
142284   #define AssignCells splitNodeStartree
142285 #endif
142286 
142287 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
142288 # define NDEBUG 1
142289 #endif
142290 
142291 #ifndef SQLITE_CORE
142292   SQLITE_EXTENSION_INIT1
142293 #else
142294 #endif
142295 
142296 /* #include <string.h> */
142297 /* #include <assert.h> */
142298 
142299 #ifndef SQLITE_AMALGAMATION
142300 #include "sqlite3rtree.h"
142301 typedef sqlite3_int64 i64;
142302 typedef unsigned char u8;
142303 typedef unsigned int u32;
142304 #endif
142305 
142306 /*  The following macro is used to suppress compiler warnings.
142307 */
142308 #ifndef UNUSED_PARAMETER
142309 # define UNUSED_PARAMETER(x) (void)(x)
142310 #endif
142311 
142312 typedef struct Rtree Rtree;
142313 typedef struct RtreeCursor RtreeCursor;
142314 typedef struct RtreeNode RtreeNode;
142315 typedef struct RtreeCell RtreeCell;
142316 typedef struct RtreeConstraint RtreeConstraint;
142317 typedef struct RtreeMatchArg RtreeMatchArg;
142318 typedef struct RtreeGeomCallback RtreeGeomCallback;
142319 typedef union RtreeCoord RtreeCoord;
142320 
142321 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
142322 #define RTREE_MAX_DIMENSIONS 5
142323 
142324 /* Size of hash table Rtree.aHash. This hash table is not expected to
142325 ** ever contain very many entries, so a fixed number of buckets is
142326 ** used.
142327 */
142328 #define HASHSIZE 128
142329 
142330 /* The xBestIndex method of this virtual table requires an estimate of
142331 ** the number of rows in the virtual table to calculate the costs of
142332 ** various strategies. If possible, this estimate is loaded from the
142333 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
142334 ** Otherwise, if no sqlite_stat1 entry is available, use
142335 ** RTREE_DEFAULT_ROWEST.
142336 */
142337 #define RTREE_DEFAULT_ROWEST 1048576
142338 #define RTREE_MIN_ROWEST         100
142339 
142340 /*
142341 ** An rtree virtual-table object.
142342 */
142343 struct Rtree {
142344   sqlite3_vtab base;
142345   sqlite3 *db;                /* Host database connection */
142346   int iNodeSize;              /* Size in bytes of each node in the node table */
142347   int nDim;                   /* Number of dimensions */
142348   int nBytesPerCell;          /* Bytes consumed per cell */
142349   int iDepth;                 /* Current depth of the r-tree structure */
142350   char *zDb;                  /* Name of database containing r-tree table */
142351   char *zName;                /* Name of r-tree table */
142352   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
142353   int nBusy;                  /* Current number of users of this structure */
142354   i64 nRowEst;                /* Estimated number of rows in this table */
142355 
142356   /* List of nodes removed during a CondenseTree operation. List is
142357   ** linked together via the pointer normally used for hash chains -
142358   ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
142359   ** headed by the node (leaf nodes have RtreeNode.iNode==0).
142360   */
142361   RtreeNode *pDeleted;
142362   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
142363 
142364   /* Statements to read/write/delete a record from xxx_node */
142365   sqlite3_stmt *pReadNode;
142366   sqlite3_stmt *pWriteNode;
142367   sqlite3_stmt *pDeleteNode;
142368 
142369   /* Statements to read/write/delete a record from xxx_rowid */
142370   sqlite3_stmt *pReadRowid;
142371   sqlite3_stmt *pWriteRowid;
142372   sqlite3_stmt *pDeleteRowid;
142373 
142374   /* Statements to read/write/delete a record from xxx_parent */
142375   sqlite3_stmt *pReadParent;
142376   sqlite3_stmt *pWriteParent;
142377   sqlite3_stmt *pDeleteParent;
142378 
142379   int eCoordType;
142380 };
142381 
142382 /* Possible values for eCoordType: */
142383 #define RTREE_COORD_REAL32 0
142384 #define RTREE_COORD_INT32  1
142385 
142386 /*
142387 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
142388 ** only deal with integer coordinates.  No floating point operations
142389 ** will be done.
142390 */
142391 #ifdef SQLITE_RTREE_INT_ONLY
142392   typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
142393   typedef int RtreeValue;                  /* Low accuracy coordinate */
142394 #else
142395   typedef double RtreeDValue;              /* High accuracy coordinate */
142396   typedef float RtreeValue;                /* Low accuracy coordinate */
142397 #endif
142398 
142399 /*
142400 ** The minimum number of cells allowed for a node is a third of the
142401 ** maximum. In Gutman's notation:
142402 **
142403 **     m = M/3
142404 **
142405 ** If an R*-tree "Reinsert" operation is required, the same number of
142406 ** cells are removed from the overfull node and reinserted into the tree.
142407 */
142408 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
142409 #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
142410 #define RTREE_MAXCELLS 51
142411 
142412 /*
142413 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
142414 ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
142415 ** Therefore all non-root nodes must contain at least 3 entries. Since
142416 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
142417 ** 40 or less.
142418 */
142419 #define RTREE_MAX_DEPTH 40
142420 
142421 /*
142422 ** An rtree cursor object.
142423 */
142424 struct RtreeCursor {
142425   sqlite3_vtab_cursor base;
142426   RtreeNode *pNode;                 /* Node cursor is currently pointing at */
142427   int iCell;                        /* Index of current cell in pNode */
142428   int iStrategy;                    /* Copy of idxNum search parameter */
142429   int nConstraint;                  /* Number of entries in aConstraint */
142430   RtreeConstraint *aConstraint;     /* Search constraints. */
142431 };
142432 
142433 union RtreeCoord {
142434   RtreeValue f;
142435   int i;
142436 };
142437 
142438 /*
142439 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
142440 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
142441 ** variable pRtree points to the Rtree structure associated with the
142442 ** RtreeCoord.
142443 */
142444 #ifdef SQLITE_RTREE_INT_ONLY
142445 # define DCOORD(coord) ((RtreeDValue)coord.i)
142446 #else
142447 # define DCOORD(coord) (                           \
142448     (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
142449       ((double)coord.f) :                           \
142450       ((double)coord.i)                             \
142451   )
142452 #endif
142453 
142454 /*
142455 ** A search constraint.
142456 */
142457 struct RtreeConstraint {
142458   int iCoord;                     /* Index of constrained coordinate */
142459   int op;                         /* Constraining operation */
142460   RtreeDValue rValue;             /* Constraint value. */
142461   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
142462   sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
142463 };
142464 
142465 /* Possible values for RtreeConstraint.op */
142466 #define RTREE_EQ    0x41
142467 #define RTREE_LE    0x42
142468 #define RTREE_LT    0x43
142469 #define RTREE_GE    0x44
142470 #define RTREE_GT    0x45
142471 #define RTREE_MATCH 0x46
142472 
142473 /*
142474 ** An rtree structure node.
142475 */
142476 struct RtreeNode {
142477   RtreeNode *pParent;               /* Parent node */
142478   i64 iNode;
142479   int nRef;
142480   int isDirty;
142481   u8 *zData;
142482   RtreeNode *pNext;                 /* Next node in this hash chain */
142483 };
142484 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
142485 
142486 /*
142487 ** Structure to store a deserialized rtree record.
142488 */
142489 struct RtreeCell {
142490   i64 iRowid;
142491   RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
142492 };
142493 
142494 
142495 /*
142496 ** Value for the first field of every RtreeMatchArg object. The MATCH
142497 ** operator tests that the first field of a blob operand matches this
142498 ** value to avoid operating on invalid blobs (which could cause a segfault).
142499 */
142500 #define RTREE_GEOMETRY_MAGIC 0x891245AB
142501 
142502 /*
142503 ** An instance of this structure must be supplied as a blob argument to
142504 ** the right-hand-side of an SQL MATCH operator used to constrain an
142505 ** r-tree query.
142506 */
142507 struct RtreeMatchArg {
142508   u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
142509   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
142510   void *pContext;
142511   int nParam;
142512   RtreeDValue aParam[1];
142513 };
142514 
142515 /*
142516 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
142517 ** a single instance of the following structure is allocated. It is used
142518 ** as the context for the user-function created by by s_r_g_c(). The object
142519 ** is eventually deleted by the destructor mechanism provided by
142520 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
142521 ** the geometry callback function).
142522 */
142523 struct RtreeGeomCallback {
142524   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
142525   void *pContext;
142526 };
142527 
142528 #ifndef MAX
142529 # define MAX(x,y) ((x) < (y) ? (y) : (x))
142530 #endif
142531 #ifndef MIN
142532 # define MIN(x,y) ((x) > (y) ? (y) : (x))
142533 #endif
142534 
142535 /*
142536 ** Functions to deserialize a 16 bit integer, 32 bit real number and
142537 ** 64 bit integer. The deserialized value is returned.
142538 */
142539 static int readInt16(u8 *p){
142540   return (p[0]<<8) + p[1];
142541 }
142542 static void readCoord(u8 *p, RtreeCoord *pCoord){
142543   u32 i = (
142544     (((u32)p[0]) << 24) +
142545     (((u32)p[1]) << 16) +
142546     (((u32)p[2]) <<  8) +
142547     (((u32)p[3]) <<  0)
142548   );
142549   *(u32 *)pCoord = i;
142550 }
142551 static i64 readInt64(u8 *p){
142552   return (
142553     (((i64)p[0]) << 56) +
142554     (((i64)p[1]) << 48) +
142555     (((i64)p[2]) << 40) +
142556     (((i64)p[3]) << 32) +
142557     (((i64)p[4]) << 24) +
142558     (((i64)p[5]) << 16) +
142559     (((i64)p[6]) <<  8) +
142560     (((i64)p[7]) <<  0)
142561   );
142562 }
142563 
142564 /*
142565 ** Functions to serialize a 16 bit integer, 32 bit real number and
142566 ** 64 bit integer. The value returned is the number of bytes written
142567 ** to the argument buffer (always 2, 4 and 8 respectively).
142568 */
142569 static int writeInt16(u8 *p, int i){
142570   p[0] = (i>> 8)&0xFF;
142571   p[1] = (i>> 0)&0xFF;
142572   return 2;
142573 }
142574 static int writeCoord(u8 *p, RtreeCoord *pCoord){
142575   u32 i;
142576   assert( sizeof(RtreeCoord)==4 );
142577   assert( sizeof(u32)==4 );
142578   i = *(u32 *)pCoord;
142579   p[0] = (i>>24)&0xFF;
142580   p[1] = (i>>16)&0xFF;
142581   p[2] = (i>> 8)&0xFF;
142582   p[3] = (i>> 0)&0xFF;
142583   return 4;
142584 }
142585 static int writeInt64(u8 *p, i64 i){
142586   p[0] = (i>>56)&0xFF;
142587   p[1] = (i>>48)&0xFF;
142588   p[2] = (i>>40)&0xFF;
142589   p[3] = (i>>32)&0xFF;
142590   p[4] = (i>>24)&0xFF;
142591   p[5] = (i>>16)&0xFF;
142592   p[6] = (i>> 8)&0xFF;
142593   p[7] = (i>> 0)&0xFF;
142594   return 8;
142595 }
142596 
142597 /*
142598 ** Increment the reference count of node p.
142599 */
142600 static void nodeReference(RtreeNode *p){
142601   if( p ){
142602     p->nRef++;
142603   }
142604 }
142605 
142606 /*
142607 ** Clear the content of node p (set all bytes to 0x00).
142608 */
142609 static void nodeZero(Rtree *pRtree, RtreeNode *p){
142610   memset(&p->zData[2], 0, pRtree->iNodeSize-2);
142611   p->isDirty = 1;
142612 }
142613 
142614 /*
142615 ** Given a node number iNode, return the corresponding key to use
142616 ** in the Rtree.aHash table.
142617 */
142618 static int nodeHash(i64 iNode){
142619   return (
142620     (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^
142621     (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
142622   ) % HASHSIZE;
142623 }
142624 
142625 /*
142626 ** Search the node hash table for node iNode. If found, return a pointer
142627 ** to it. Otherwise, return 0.
142628 */
142629 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
142630   RtreeNode *p;
142631   for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
142632   return p;
142633 }
142634 
142635 /*
142636 ** Add node pNode to the node hash table.
142637 */
142638 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
142639   int iHash;
142640   assert( pNode->pNext==0 );
142641   iHash = nodeHash(pNode->iNode);
142642   pNode->pNext = pRtree->aHash[iHash];
142643   pRtree->aHash[iHash] = pNode;
142644 }
142645 
142646 /*
142647 ** Remove node pNode from the node hash table.
142648 */
142649 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
142650   RtreeNode **pp;
142651   if( pNode->iNode!=0 ){
142652     pp = &pRtree->aHash[nodeHash(pNode->iNode)];
142653     for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
142654     *pp = pNode->pNext;
142655     pNode->pNext = 0;
142656   }
142657 }
142658 
142659 /*
142660 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
142661 ** indicating that node has not yet been assigned a node number. It is
142662 ** assigned a node number when nodeWrite() is called to write the
142663 ** node contents out to the database.
142664 */
142665 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
142666   RtreeNode *pNode;
142667   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
142668   if( pNode ){
142669     memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
142670     pNode->zData = (u8 *)&pNode[1];
142671     pNode->nRef = 1;
142672     pNode->pParent = pParent;
142673     pNode->isDirty = 1;
142674     nodeReference(pParent);
142675   }
142676   return pNode;
142677 }
142678 
142679 /*
142680 ** Obtain a reference to an r-tree node.
142681 */
142682 static int
142683 nodeAcquire(
142684   Rtree *pRtree,             /* R-tree structure */
142685   i64 iNode,                 /* Node number to load */
142686   RtreeNode *pParent,        /* Either the parent node or NULL */
142687   RtreeNode **ppNode         /* OUT: Acquired node */
142688 ){
142689   int rc;
142690   int rc2 = SQLITE_OK;
142691   RtreeNode *pNode;
142692 
142693   /* Check if the requested node is already in the hash table. If so,
142694   ** increase its reference count and return it.
142695   */
142696   if( (pNode = nodeHashLookup(pRtree, iNode)) ){
142697     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
142698     if( pParent && !pNode->pParent ){
142699       nodeReference(pParent);
142700       pNode->pParent = pParent;
142701     }
142702     pNode->nRef++;
142703     *ppNode = pNode;
142704     return SQLITE_OK;
142705   }
142706 
142707   sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
142708   rc = sqlite3_step(pRtree->pReadNode);
142709   if( rc==SQLITE_ROW ){
142710     const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
142711     if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
142712       pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
142713       if( !pNode ){
142714         rc2 = SQLITE_NOMEM;
142715       }else{
142716         pNode->pParent = pParent;
142717         pNode->zData = (u8 *)&pNode[1];
142718         pNode->nRef = 1;
142719         pNode->iNode = iNode;
142720         pNode->isDirty = 0;
142721         pNode->pNext = 0;
142722         memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
142723         nodeReference(pParent);
142724       }
142725     }
142726   }
142727   rc = sqlite3_reset(pRtree->pReadNode);
142728   if( rc==SQLITE_OK ) rc = rc2;
142729 
142730   /* If the root node was just loaded, set pRtree->iDepth to the height
142731   ** of the r-tree structure. A height of zero means all data is stored on
142732   ** the root node. A height of one means the children of the root node
142733   ** are the leaves, and so on. If the depth as specified on the root node
142734   ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
142735   */
142736   if( pNode && iNode==1 ){
142737     pRtree->iDepth = readInt16(pNode->zData);
142738     if( pRtree->iDepth>RTREE_MAX_DEPTH ){
142739       rc = SQLITE_CORRUPT_VTAB;
142740     }
142741   }
142742 
142743   /* If no error has occurred so far, check if the "number of entries"
142744   ** field on the node is too large. If so, set the return code to
142745   ** SQLITE_CORRUPT_VTAB.
142746   */
142747   if( pNode && rc==SQLITE_OK ){
142748     if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
142749       rc = SQLITE_CORRUPT_VTAB;
142750     }
142751   }
142752 
142753   if( rc==SQLITE_OK ){
142754     if( pNode!=0 ){
142755       nodeHashInsert(pRtree, pNode);
142756     }else{
142757       rc = SQLITE_CORRUPT_VTAB;
142758     }
142759     *ppNode = pNode;
142760   }else{
142761     sqlite3_free(pNode);
142762     *ppNode = 0;
142763   }
142764 
142765   return rc;
142766 }
142767 
142768 /*
142769 ** Overwrite cell iCell of node pNode with the contents of pCell.
142770 */
142771 static void nodeOverwriteCell(
142772   Rtree *pRtree,
142773   RtreeNode *pNode,
142774   RtreeCell *pCell,
142775   int iCell
142776 ){
142777   int ii;
142778   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
142779   p += writeInt64(p, pCell->iRowid);
142780   for(ii=0; ii<(pRtree->nDim*2); ii++){
142781     p += writeCoord(p, &pCell->aCoord[ii]);
142782   }
142783   pNode->isDirty = 1;
142784 }
142785 
142786 /*
142787 ** Remove cell the cell with index iCell from node pNode.
142788 */
142789 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
142790   u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
142791   u8 *pSrc = &pDst[pRtree->nBytesPerCell];
142792   int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
142793   memmove(pDst, pSrc, nByte);
142794   writeInt16(&pNode->zData[2], NCELL(pNode)-1);
142795   pNode->isDirty = 1;
142796 }
142797 
142798 /*
142799 ** Insert the contents of cell pCell into node pNode. If the insert
142800 ** is successful, return SQLITE_OK.
142801 **
142802 ** If there is not enough free space in pNode, return SQLITE_FULL.
142803 */
142804 static int
142805 nodeInsertCell(
142806   Rtree *pRtree,
142807   RtreeNode *pNode,
142808   RtreeCell *pCell
142809 ){
142810   int nCell;                    /* Current number of cells in pNode */
142811   int nMaxCell;                 /* Maximum number of cells for pNode */
142812 
142813   nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
142814   nCell = NCELL(pNode);
142815 
142816   assert( nCell<=nMaxCell );
142817   if( nCell<nMaxCell ){
142818     nodeOverwriteCell(pRtree, pNode, pCell, nCell);
142819     writeInt16(&pNode->zData[2], nCell+1);
142820     pNode->isDirty = 1;
142821   }
142822 
142823   return (nCell==nMaxCell);
142824 }
142825 
142826 /*
142827 ** If the node is dirty, write it out to the database.
142828 */
142829 static int
142830 nodeWrite(Rtree *pRtree, RtreeNode *pNode){
142831   int rc = SQLITE_OK;
142832   if( pNode->isDirty ){
142833     sqlite3_stmt *p = pRtree->pWriteNode;
142834     if( pNode->iNode ){
142835       sqlite3_bind_int64(p, 1, pNode->iNode);
142836     }else{
142837       sqlite3_bind_null(p, 1);
142838     }
142839     sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
142840     sqlite3_step(p);
142841     pNode->isDirty = 0;
142842     rc = sqlite3_reset(p);
142843     if( pNode->iNode==0 && rc==SQLITE_OK ){
142844       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
142845       nodeHashInsert(pRtree, pNode);
142846     }
142847   }
142848   return rc;
142849 }
142850 
142851 /*
142852 ** Release a reference to a node. If the node is dirty and the reference
142853 ** count drops to zero, the node data is written to the database.
142854 */
142855 static int
142856 nodeRelease(Rtree *pRtree, RtreeNode *pNode){
142857   int rc = SQLITE_OK;
142858   if( pNode ){
142859     assert( pNode->nRef>0 );
142860     pNode->nRef--;
142861     if( pNode->nRef==0 ){
142862       if( pNode->iNode==1 ){
142863         pRtree->iDepth = -1;
142864       }
142865       if( pNode->pParent ){
142866         rc = nodeRelease(pRtree, pNode->pParent);
142867       }
142868       if( rc==SQLITE_OK ){
142869         rc = nodeWrite(pRtree, pNode);
142870       }
142871       nodeHashDelete(pRtree, pNode);
142872       sqlite3_free(pNode);
142873     }
142874   }
142875   return rc;
142876 }
142877 
142878 /*
142879 ** Return the 64-bit integer value associated with cell iCell of
142880 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
142881 ** an internal node, then the 64-bit integer is a child page number.
142882 */
142883 static i64 nodeGetRowid(
142884   Rtree *pRtree,
142885   RtreeNode *pNode,
142886   int iCell
142887 ){
142888   assert( iCell<NCELL(pNode) );
142889   return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
142890 }
142891 
142892 /*
142893 ** Return coordinate iCoord from cell iCell in node pNode.
142894 */
142895 static void nodeGetCoord(
142896   Rtree *pRtree,
142897   RtreeNode *pNode,
142898   int iCell,
142899   int iCoord,
142900   RtreeCoord *pCoord           /* Space to write result to */
142901 ){
142902   readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
142903 }
142904 
142905 /*
142906 ** Deserialize cell iCell of node pNode. Populate the structure pointed
142907 ** to by pCell with the results.
142908 */
142909 static void nodeGetCell(
142910   Rtree *pRtree,
142911   RtreeNode *pNode,
142912   int iCell,
142913   RtreeCell *pCell
142914 ){
142915   int ii;
142916   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
142917   for(ii=0; ii<pRtree->nDim*2; ii++){
142918     nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
142919   }
142920 }
142921 
142922 
142923 /* Forward declaration for the function that does the work of
142924 ** the virtual table module xCreate() and xConnect() methods.
142925 */
142926 static int rtreeInit(
142927   sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
142928 );
142929 
142930 /*
142931 ** Rtree virtual table module xCreate method.
142932 */
142933 static int rtreeCreate(
142934   sqlite3 *db,
142935   void *pAux,
142936   int argc, const char *const*argv,
142937   sqlite3_vtab **ppVtab,
142938   char **pzErr
142939 ){
142940   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
142941 }
142942 
142943 /*
142944 ** Rtree virtual table module xConnect method.
142945 */
142946 static int rtreeConnect(
142947   sqlite3 *db,
142948   void *pAux,
142949   int argc, const char *const*argv,
142950   sqlite3_vtab **ppVtab,
142951   char **pzErr
142952 ){
142953   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
142954 }
142955 
142956 /*
142957 ** Increment the r-tree reference count.
142958 */
142959 static void rtreeReference(Rtree *pRtree){
142960   pRtree->nBusy++;
142961 }
142962 
142963 /*
142964 ** Decrement the r-tree reference count. When the reference count reaches
142965 ** zero the structure is deleted.
142966 */
142967 static void rtreeRelease(Rtree *pRtree){
142968   pRtree->nBusy--;
142969   if( pRtree->nBusy==0 ){
142970     sqlite3_finalize(pRtree->pReadNode);
142971     sqlite3_finalize(pRtree->pWriteNode);
142972     sqlite3_finalize(pRtree->pDeleteNode);
142973     sqlite3_finalize(pRtree->pReadRowid);
142974     sqlite3_finalize(pRtree->pWriteRowid);
142975     sqlite3_finalize(pRtree->pDeleteRowid);
142976     sqlite3_finalize(pRtree->pReadParent);
142977     sqlite3_finalize(pRtree->pWriteParent);
142978     sqlite3_finalize(pRtree->pDeleteParent);
142979     sqlite3_free(pRtree);
142980   }
142981 }
142982 
142983 /*
142984 ** Rtree virtual table module xDisconnect method.
142985 */
142986 static int rtreeDisconnect(sqlite3_vtab *pVtab){
142987   rtreeRelease((Rtree *)pVtab);
142988   return SQLITE_OK;
142989 }
142990 
142991 /*
142992 ** Rtree virtual table module xDestroy method.
142993 */
142994 static int rtreeDestroy(sqlite3_vtab *pVtab){
142995   Rtree *pRtree = (Rtree *)pVtab;
142996   int rc;
142997   char *zCreate = sqlite3_mprintf(
142998     "DROP TABLE '%q'.'%q_node';"
142999     "DROP TABLE '%q'.'%q_rowid';"
143000     "DROP TABLE '%q'.'%q_parent';",
143001     pRtree->zDb, pRtree->zName,
143002     pRtree->zDb, pRtree->zName,
143003     pRtree->zDb, pRtree->zName
143004   );
143005   if( !zCreate ){
143006     rc = SQLITE_NOMEM;
143007   }else{
143008     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
143009     sqlite3_free(zCreate);
143010   }
143011   if( rc==SQLITE_OK ){
143012     rtreeRelease(pRtree);
143013   }
143014 
143015   return rc;
143016 }
143017 
143018 /*
143019 ** Rtree virtual table module xOpen method.
143020 */
143021 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
143022   int rc = SQLITE_NOMEM;
143023   RtreeCursor *pCsr;
143024 
143025   pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
143026   if( pCsr ){
143027     memset(pCsr, 0, sizeof(RtreeCursor));
143028     pCsr->base.pVtab = pVTab;
143029     rc = SQLITE_OK;
143030   }
143031   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
143032 
143033   return rc;
143034 }
143035 
143036 
143037 /*
143038 ** Free the RtreeCursor.aConstraint[] array and its contents.
143039 */
143040 static void freeCursorConstraints(RtreeCursor *pCsr){
143041   if( pCsr->aConstraint ){
143042     int i;                        /* Used to iterate through constraint array */
143043     for(i=0; i<pCsr->nConstraint; i++){
143044       sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
143045       if( pGeom ){
143046         if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
143047         sqlite3_free(pGeom);
143048       }
143049     }
143050     sqlite3_free(pCsr->aConstraint);
143051     pCsr->aConstraint = 0;
143052   }
143053 }
143054 
143055 /*
143056 ** Rtree virtual table module xClose method.
143057 */
143058 static int rtreeClose(sqlite3_vtab_cursor *cur){
143059   Rtree *pRtree = (Rtree *)(cur->pVtab);
143060   int rc;
143061   RtreeCursor *pCsr = (RtreeCursor *)cur;
143062   freeCursorConstraints(pCsr);
143063   rc = nodeRelease(pRtree, pCsr->pNode);
143064   sqlite3_free(pCsr);
143065   return rc;
143066 }
143067 
143068 /*
143069 ** Rtree virtual table module xEof method.
143070 **
143071 ** Return non-zero if the cursor does not currently point to a valid
143072 ** record (i.e if the scan has finished), or zero otherwise.
143073 */
143074 static int rtreeEof(sqlite3_vtab_cursor *cur){
143075   RtreeCursor *pCsr = (RtreeCursor *)cur;
143076   return (pCsr->pNode==0);
143077 }
143078 
143079 /*
143080 ** The r-tree constraint passed as the second argument to this function is
143081 ** guaranteed to be a MATCH constraint.
143082 */
143083 static int testRtreeGeom(
143084   Rtree *pRtree,                  /* R-Tree object */
143085   RtreeConstraint *pConstraint,   /* MATCH constraint to test */
143086   RtreeCell *pCell,               /* Cell to test */
143087   int *pbRes                      /* OUT: Test result */
143088 ){
143089   int i;
143090   RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
143091   int nCoord = pRtree->nDim*2;
143092 
143093   assert( pConstraint->op==RTREE_MATCH );
143094   assert( pConstraint->pGeom );
143095 
143096   for(i=0; i<nCoord; i++){
143097     aCoord[i] = DCOORD(pCell->aCoord[i]);
143098   }
143099   return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
143100 }
143101 
143102 /*
143103 ** Cursor pCursor currently points to a cell in a non-leaf page.
143104 ** Set *pbEof to true if the sub-tree headed by the cell is filtered
143105 ** (excluded) by the constraints in the pCursor->aConstraint[]
143106 ** array, or false otherwise.
143107 **
143108 ** Return SQLITE_OK if successful or an SQLite error code if an error
143109 ** occurs within a geometry callback.
143110 */
143111 static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143112   RtreeCell cell;
143113   int ii;
143114   int bRes = 0;
143115   int rc = SQLITE_OK;
143116 
143117   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143118   for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
143119     RtreeConstraint *p = &pCursor->aConstraint[ii];
143120     RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
143121     RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
143122 
143123     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
143124         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143125     );
143126 
143127     switch( p->op ){
143128       case RTREE_LE: case RTREE_LT:
143129         bRes = p->rValue<cell_min;
143130         break;
143131 
143132       case RTREE_GE: case RTREE_GT:
143133         bRes = p->rValue>cell_max;
143134         break;
143135 
143136       case RTREE_EQ:
143137         bRes = (p->rValue>cell_max || p->rValue<cell_min);
143138         break;
143139 
143140       default: {
143141         assert( p->op==RTREE_MATCH );
143142         rc = testRtreeGeom(pRtree, p, &cell, &bRes);
143143         bRes = !bRes;
143144         break;
143145       }
143146     }
143147   }
143148 
143149   *pbEof = bRes;
143150   return rc;
143151 }
143152 
143153 /*
143154 ** Test if the cell that cursor pCursor currently points to
143155 ** would be filtered (excluded) by the constraints in the
143156 ** pCursor->aConstraint[] array. If so, set *pbEof to true before
143157 ** returning. If the cell is not filtered (excluded) by the constraints,
143158 ** set pbEof to zero.
143159 **
143160 ** Return SQLITE_OK if successful or an SQLite error code if an error
143161 ** occurs within a geometry callback.
143162 **
143163 ** This function assumes that the cell is part of a leaf node.
143164 */
143165 static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143166   RtreeCell cell;
143167   int ii;
143168   *pbEof = 0;
143169 
143170   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143171   for(ii=0; ii<pCursor->nConstraint; ii++){
143172     RtreeConstraint *p = &pCursor->aConstraint[ii];
143173     RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
143174     int res;
143175     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
143176         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143177     );
143178     switch( p->op ){
143179       case RTREE_LE: res = (coord<=p->rValue); break;
143180       case RTREE_LT: res = (coord<p->rValue);  break;
143181       case RTREE_GE: res = (coord>=p->rValue); break;
143182       case RTREE_GT: res = (coord>p->rValue);  break;
143183       case RTREE_EQ: res = (coord==p->rValue); break;
143184       default: {
143185         int rc;
143186         assert( p->op==RTREE_MATCH );
143187         rc = testRtreeGeom(pRtree, p, &cell, &res);
143188         if( rc!=SQLITE_OK ){
143189           return rc;
143190         }
143191         break;
143192       }
143193     }
143194 
143195     if( !res ){
143196       *pbEof = 1;
143197       return SQLITE_OK;
143198     }
143199   }
143200 
143201   return SQLITE_OK;
143202 }
143203 
143204 /*
143205 ** Cursor pCursor currently points at a node that heads a sub-tree of
143206 ** height iHeight (if iHeight==0, then the node is a leaf). Descend
143207 ** to point to the left-most cell of the sub-tree that matches the
143208 ** configured constraints.
143209 */
143210 static int descendToCell(
143211   Rtree *pRtree,
143212   RtreeCursor *pCursor,
143213   int iHeight,
143214   int *pEof                 /* OUT: Set to true if cannot descend */
143215 ){
143216   int isEof;
143217   int rc;
143218   int ii;
143219   RtreeNode *pChild;
143220   sqlite3_int64 iRowid;
143221 
143222   RtreeNode *pSavedNode = pCursor->pNode;
143223   int iSavedCell = pCursor->iCell;
143224 
143225   assert( iHeight>=0 );
143226 
143227   if( iHeight==0 ){
143228     rc = testRtreeEntry(pRtree, pCursor, &isEof);
143229   }else{
143230     rc = testRtreeCell(pRtree, pCursor, &isEof);
143231   }
143232   if( rc!=SQLITE_OK || isEof || iHeight==0 ){
143233     goto descend_to_cell_out;
143234   }
143235 
143236   iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
143237   rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
143238   if( rc!=SQLITE_OK ){
143239     goto descend_to_cell_out;
143240   }
143241 
143242   nodeRelease(pRtree, pCursor->pNode);
143243   pCursor->pNode = pChild;
143244   isEof = 1;
143245   for(ii=0; isEof && ii<NCELL(pChild); ii++){
143246     pCursor->iCell = ii;
143247     rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
143248     if( rc!=SQLITE_OK ){
143249       goto descend_to_cell_out;
143250     }
143251   }
143252 
143253   if( isEof ){
143254     assert( pCursor->pNode==pChild );
143255     nodeReference(pSavedNode);
143256     nodeRelease(pRtree, pChild);
143257     pCursor->pNode = pSavedNode;
143258     pCursor->iCell = iSavedCell;
143259   }
143260 
143261 descend_to_cell_out:
143262   *pEof = isEof;
143263   return rc;
143264 }
143265 
143266 /*
143267 ** One of the cells in node pNode is guaranteed to have a 64-bit
143268 ** integer value equal to iRowid. Return the index of this cell.
143269 */
143270 static int nodeRowidIndex(
143271   Rtree *pRtree,
143272   RtreeNode *pNode,
143273   i64 iRowid,
143274   int *piIndex
143275 ){
143276   int ii;
143277   int nCell = NCELL(pNode);
143278   for(ii=0; ii<nCell; ii++){
143279     if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
143280       *piIndex = ii;
143281       return SQLITE_OK;
143282     }
143283   }
143284   return SQLITE_CORRUPT_VTAB;
143285 }
143286 
143287 /*
143288 ** Return the index of the cell containing a pointer to node pNode
143289 ** in its parent. If pNode is the root node, return -1.
143290 */
143291 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
143292   RtreeNode *pParent = pNode->pParent;
143293   if( pParent ){
143294     return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
143295   }
143296   *piIndex = -1;
143297   return SQLITE_OK;
143298 }
143299 
143300 /*
143301 ** Rtree virtual table module xNext method.
143302 */
143303 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
143304   Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
143305   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
143306   int rc = SQLITE_OK;
143307 
143308   /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
143309   ** already at EOF. It is against the rules to call the xNext() method of
143310   ** a cursor that has already reached EOF.
143311   */
143312   assert( pCsr->pNode );
143313 
143314   if( pCsr->iStrategy==1 ){
143315     /* This "scan" is a direct lookup by rowid. There is no next entry. */
143316     nodeRelease(pRtree, pCsr->pNode);
143317     pCsr->pNode = 0;
143318   }else{
143319     /* Move to the next entry that matches the configured constraints. */
143320     int iHeight = 0;
143321     while( pCsr->pNode ){
143322       RtreeNode *pNode = pCsr->pNode;
143323       int nCell = NCELL(pNode);
143324       for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
143325         int isEof;
143326         rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
143327         if( rc!=SQLITE_OK || !isEof ){
143328           return rc;
143329         }
143330       }
143331       pCsr->pNode = pNode->pParent;
143332       rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
143333       if( rc!=SQLITE_OK ){
143334         return rc;
143335       }
143336       nodeReference(pCsr->pNode);
143337       nodeRelease(pRtree, pNode);
143338       iHeight++;
143339     }
143340   }
143341 
143342   return rc;
143343 }
143344 
143345 /*
143346 ** Rtree virtual table module xRowid method.
143347 */
143348 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
143349   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
143350   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
143351 
143352   assert(pCsr->pNode);
143353   *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
143354 
143355   return SQLITE_OK;
143356 }
143357 
143358 /*
143359 ** Rtree virtual table module xColumn method.
143360 */
143361 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
143362   Rtree *pRtree = (Rtree *)cur->pVtab;
143363   RtreeCursor *pCsr = (RtreeCursor *)cur;
143364 
143365   if( i==0 ){
143366     i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
143367     sqlite3_result_int64(ctx, iRowid);
143368   }else{
143369     RtreeCoord c;
143370     nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
143371 #ifndef SQLITE_RTREE_INT_ONLY
143372     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
143373       sqlite3_result_double(ctx, c.f);
143374     }else
143375 #endif
143376     {
143377       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
143378       sqlite3_result_int(ctx, c.i);
143379     }
143380   }
143381 
143382   return SQLITE_OK;
143383 }
143384 
143385 /*
143386 ** Use nodeAcquire() to obtain the leaf node containing the record with
143387 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
143388 ** return SQLITE_OK. If there is no such record in the table, set
143389 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
143390 ** to zero and return an SQLite error code.
143391 */
143392 static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
143393   int rc;
143394   *ppLeaf = 0;
143395   sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
143396   if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
143397     i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
143398     rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
143399     sqlite3_reset(pRtree->pReadRowid);
143400   }else{
143401     rc = sqlite3_reset(pRtree->pReadRowid);
143402   }
143403   return rc;
143404 }
143405 
143406 /*
143407 ** This function is called to configure the RtreeConstraint object passed
143408 ** as the second argument for a MATCH constraint. The value passed as the
143409 ** first argument to this function is the right-hand operand to the MATCH
143410 ** operator.
143411 */
143412 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
143413   RtreeMatchArg *p;
143414   sqlite3_rtree_geometry *pGeom;
143415   int nBlob;
143416 
143417   /* Check that value is actually a blob. */
143418   if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
143419 
143420   /* Check that the blob is roughly the right size. */
143421   nBlob = sqlite3_value_bytes(pValue);
143422   if( nBlob<(int)sizeof(RtreeMatchArg)
143423    || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
143424   ){
143425     return SQLITE_ERROR;
143426   }
143427 
143428   pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
143429       sizeof(sqlite3_rtree_geometry) + nBlob
143430   );
143431   if( !pGeom ) return SQLITE_NOMEM;
143432   memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
143433   p = (RtreeMatchArg *)&pGeom[1];
143434 
143435   memcpy(p, sqlite3_value_blob(pValue), nBlob);
143436   if( p->magic!=RTREE_GEOMETRY_MAGIC
143437    || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
143438   ){
143439     sqlite3_free(pGeom);
143440     return SQLITE_ERROR;
143441   }
143442 
143443   pGeom->pContext = p->pContext;
143444   pGeom->nParam = p->nParam;
143445   pGeom->aParam = p->aParam;
143446 
143447   pCons->xGeom = p->xGeom;
143448   pCons->pGeom = pGeom;
143449   return SQLITE_OK;
143450 }
143451 
143452 /*
143453 ** Rtree virtual table module xFilter method.
143454 */
143455 static int rtreeFilter(
143456   sqlite3_vtab_cursor *pVtabCursor,
143457   int idxNum, const char *idxStr,
143458   int argc, sqlite3_value **argv
143459 ){
143460   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
143461   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
143462 
143463   RtreeNode *pRoot = 0;
143464   int ii;
143465   int rc = SQLITE_OK;
143466 
143467   rtreeReference(pRtree);
143468 
143469   freeCursorConstraints(pCsr);
143470   pCsr->iStrategy = idxNum;
143471 
143472   if( idxNum==1 ){
143473     /* Special case - lookup by rowid. */
143474     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
143475     i64 iRowid = sqlite3_value_int64(argv[0]);
143476     rc = findLeafNode(pRtree, iRowid, &pLeaf);
143477     pCsr->pNode = pLeaf;
143478     if( pLeaf ){
143479       assert( rc==SQLITE_OK );
143480       rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
143481     }
143482   }else{
143483     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
143484     ** with the configured constraints.
143485     */
143486     if( argc>0 ){
143487       pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
143488       pCsr->nConstraint = argc;
143489       if( !pCsr->aConstraint ){
143490         rc = SQLITE_NOMEM;
143491       }else{
143492         memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
143493         assert( (idxStr==0 && argc==0)
143494                 || (idxStr && (int)strlen(idxStr)==argc*2) );
143495         for(ii=0; ii<argc; ii++){
143496           RtreeConstraint *p = &pCsr->aConstraint[ii];
143497           p->op = idxStr[ii*2];
143498           p->iCoord = idxStr[ii*2+1]-'a';
143499           if( p->op==RTREE_MATCH ){
143500             /* A MATCH operator. The right-hand-side must be a blob that
143501             ** can be cast into an RtreeMatchArg object. One created using
143502             ** an sqlite3_rtree_geometry_callback() SQL user function.
143503             */
143504             rc = deserializeGeometry(argv[ii], p);
143505             if( rc!=SQLITE_OK ){
143506               break;
143507             }
143508           }else{
143509 #ifdef SQLITE_RTREE_INT_ONLY
143510             p->rValue = sqlite3_value_int64(argv[ii]);
143511 #else
143512             p->rValue = sqlite3_value_double(argv[ii]);
143513 #endif
143514           }
143515         }
143516       }
143517     }
143518 
143519     if( rc==SQLITE_OK ){
143520       pCsr->pNode = 0;
143521       rc = nodeAcquire(pRtree, 1, 0, &pRoot);
143522     }
143523     if( rc==SQLITE_OK ){
143524       int isEof = 1;
143525       int nCell = NCELL(pRoot);
143526       pCsr->pNode = pRoot;
143527       for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
143528         assert( pCsr->pNode==pRoot );
143529         rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
143530         if( !isEof ){
143531           break;
143532         }
143533       }
143534       if( rc==SQLITE_OK && isEof ){
143535         assert( pCsr->pNode==pRoot );
143536         nodeRelease(pRtree, pRoot);
143537         pCsr->pNode = 0;
143538       }
143539       assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
143540     }
143541   }
143542 
143543   rtreeRelease(pRtree);
143544   return rc;
143545 }
143546 
143547 /*
143548 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
143549 ** extension is currently being used by a version of SQLite too old to
143550 ** support estimatedRows. In that case this function is a no-op.
143551 */
143552 static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
143553 #if SQLITE_VERSION_NUMBER>=3008002
143554   if( sqlite3_libversion_number()>=3008002 ){
143555     pIdxInfo->estimatedRows = nRow;
143556   }
143557 #endif
143558 }
143559 
143560 /*
143561 ** Rtree virtual table module xBestIndex method. There are three
143562 ** table scan strategies to choose from (in order from most to
143563 ** least desirable):
143564 **
143565 **   idxNum     idxStr        Strategy
143566 **   ------------------------------------------------
143567 **     1        Unused        Direct lookup by rowid.
143568 **     2        See below     R-tree query or full-table scan.
143569 **   ------------------------------------------------
143570 **
143571 ** If strategy 1 is used, then idxStr is not meaningful. If strategy
143572 ** 2 is used, idxStr is formatted to contain 2 bytes for each
143573 ** constraint used. The first two bytes of idxStr correspond to
143574 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
143575 ** (argvIndex==1) etc.
143576 **
143577 ** The first of each pair of bytes in idxStr identifies the constraint
143578 ** operator as follows:
143579 **
143580 **   Operator    Byte Value
143581 **   ----------------------
143582 **      =        0x41 ('A')
143583 **     <=        0x42 ('B')
143584 **      <        0x43 ('C')
143585 **     >=        0x44 ('D')
143586 **      >        0x45 ('E')
143587 **   MATCH       0x46 ('F')
143588 **   ----------------------
143589 **
143590 ** The second of each pair of bytes identifies the coordinate column
143591 ** to which the constraint applies. The leftmost coordinate column
143592 ** is 'a', the second from the left 'b' etc.
143593 */
143594 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
143595   Rtree *pRtree = (Rtree*)tab;
143596   int rc = SQLITE_OK;
143597   int ii;
143598   i64 nRow;                       /* Estimated rows returned by this scan */
143599 
143600   int iIdx = 0;
143601   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
143602   memset(zIdxStr, 0, sizeof(zIdxStr));
143603 
143604   assert( pIdxInfo->idxStr==0 );
143605   for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
143606     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
143607 
143608     if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
143609       /* We have an equality constraint on the rowid. Use strategy 1. */
143610       int jj;
143611       for(jj=0; jj<ii; jj++){
143612         pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
143613         pIdxInfo->aConstraintUsage[jj].omit = 0;
143614       }
143615       pIdxInfo->idxNum = 1;
143616       pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
143617       pIdxInfo->aConstraintUsage[jj].omit = 1;
143618 
143619       /* This strategy involves a two rowid lookups on an B-Tree structures
143620       ** and then a linear search of an R-Tree node. This should be
143621       ** considered almost as quick as a direct rowid lookup (for which
143622       ** sqlite uses an internal cost of 0.0). It is expected to return
143623       ** a single row.
143624       */
143625       pIdxInfo->estimatedCost = 30.0;
143626       setEstimatedRows(pIdxInfo, 1);
143627       return SQLITE_OK;
143628     }
143629 
143630     if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
143631       u8 op;
143632       switch( p->op ){
143633         case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
143634         case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
143635         case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
143636         case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
143637         case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
143638         default:
143639           assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
143640           op = RTREE_MATCH;
143641           break;
143642       }
143643       zIdxStr[iIdx++] = op;
143644       zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
143645       pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
143646       pIdxInfo->aConstraintUsage[ii].omit = 1;
143647     }
143648   }
143649 
143650   pIdxInfo->idxNum = 2;
143651   pIdxInfo->needToFreeIdxStr = 1;
143652   if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
143653     return SQLITE_NOMEM;
143654   }
143655 
143656   nRow = pRtree->nRowEst / (iIdx + 1);
143657   pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
143658   setEstimatedRows(pIdxInfo, nRow);
143659 
143660   return rc;
143661 }
143662 
143663 /*
143664 ** Return the N-dimensional volumn of the cell stored in *p.
143665 */
143666 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
143667   RtreeDValue area = (RtreeDValue)1;
143668   int ii;
143669   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143670     area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
143671   }
143672   return area;
143673 }
143674 
143675 /*
143676 ** Return the margin length of cell p. The margin length is the sum
143677 ** of the objects size in each dimension.
143678 */
143679 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
143680   RtreeDValue margin = (RtreeDValue)0;
143681   int ii;
143682   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143683     margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
143684   }
143685   return margin;
143686 }
143687 
143688 /*
143689 ** Store the union of cells p1 and p2 in p1.
143690 */
143691 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
143692   int ii;
143693   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
143694     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143695       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
143696       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
143697     }
143698   }else{
143699     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143700       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
143701       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
143702     }
143703   }
143704 }
143705 
143706 /*
143707 ** Return true if the area covered by p2 is a subset of the area covered
143708 ** by p1. False otherwise.
143709 */
143710 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
143711   int ii;
143712   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
143713   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143714     RtreeCoord *a1 = &p1->aCoord[ii];
143715     RtreeCoord *a2 = &p2->aCoord[ii];
143716     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
143717      || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
143718     ){
143719       return 0;
143720     }
143721   }
143722   return 1;
143723 }
143724 
143725 /*
143726 ** Return the amount cell p would grow by if it were unioned with pCell.
143727 */
143728 static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
143729   RtreeDValue area;
143730   RtreeCell cell;
143731   memcpy(&cell, p, sizeof(RtreeCell));
143732   area = cellArea(pRtree, &cell);
143733   cellUnion(pRtree, &cell, pCell);
143734   return (cellArea(pRtree, &cell)-area);
143735 }
143736 
143737 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
143738 static RtreeDValue cellOverlap(
143739   Rtree *pRtree,
143740   RtreeCell *p,
143741   RtreeCell *aCell,
143742   int nCell,
143743   int iExclude
143744 ){
143745   int ii;
143746   RtreeDValue overlap = 0.0;
143747   for(ii=0; ii<nCell; ii++){
143748 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143749     if( ii!=iExclude )
143750 #else
143751     assert( iExclude==-1 );
143752     UNUSED_PARAMETER(iExclude);
143753 #endif
143754     {
143755       int jj;
143756       RtreeDValue o = (RtreeDValue)1;
143757       for(jj=0; jj<(pRtree->nDim*2); jj+=2){
143758         RtreeDValue x1, x2;
143759 
143760         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
143761         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
143762 
143763         if( x2<x1 ){
143764           o = 0.0;
143765           break;
143766         }else{
143767           o = o * (x2-x1);
143768         }
143769       }
143770       overlap += o;
143771     }
143772   }
143773   return overlap;
143774 }
143775 #endif
143776 
143777 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143778 static RtreeDValue cellOverlapEnlargement(
143779   Rtree *pRtree,
143780   RtreeCell *p,
143781   RtreeCell *pInsert,
143782   RtreeCell *aCell,
143783   int nCell,
143784   int iExclude
143785 ){
143786   RtreeDValue before, after;
143787   before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
143788   cellUnion(pRtree, p, pInsert);
143789   after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
143790   return (after-before);
143791 }
143792 #endif
143793 
143794 
143795 /*
143796 ** This function implements the ChooseLeaf algorithm from Gutman[84].
143797 ** ChooseSubTree in r*tree terminology.
143798 */
143799 static int ChooseLeaf(
143800   Rtree *pRtree,               /* Rtree table */
143801   RtreeCell *pCell,            /* Cell to insert into rtree */
143802   int iHeight,                 /* Height of sub-tree rooted at pCell */
143803   RtreeNode **ppLeaf           /* OUT: Selected leaf page */
143804 ){
143805   int rc;
143806   int ii;
143807   RtreeNode *pNode;
143808   rc = nodeAcquire(pRtree, 1, 0, &pNode);
143809 
143810   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
143811     int iCell;
143812     sqlite3_int64 iBest = 0;
143813 
143814     RtreeDValue fMinGrowth = 0.0;
143815     RtreeDValue fMinArea = 0.0;
143816 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143817     RtreeDValue fMinOverlap = 0.0;
143818     RtreeDValue overlap;
143819 #endif
143820 
143821     int nCell = NCELL(pNode);
143822     RtreeCell cell;
143823     RtreeNode *pChild;
143824 
143825     RtreeCell *aCell = 0;
143826 
143827 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143828     if( ii==(pRtree->iDepth-1) ){
143829       int jj;
143830       aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
143831       if( !aCell ){
143832         rc = SQLITE_NOMEM;
143833         nodeRelease(pRtree, pNode);
143834         pNode = 0;
143835         continue;
143836       }
143837       for(jj=0; jj<nCell; jj++){
143838         nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
143839       }
143840     }
143841 #endif
143842 
143843     /* Select the child node which will be enlarged the least if pCell
143844     ** is inserted into it. Resolve ties by choosing the entry with
143845     ** the smallest area.
143846     */
143847     for(iCell=0; iCell<nCell; iCell++){
143848       int bBest = 0;
143849       RtreeDValue growth;
143850       RtreeDValue area;
143851       nodeGetCell(pRtree, pNode, iCell, &cell);
143852       growth = cellGrowth(pRtree, &cell, pCell);
143853       area = cellArea(pRtree, &cell);
143854 
143855 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143856       if( ii==(pRtree->iDepth-1) ){
143857         overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
143858       }else{
143859         overlap = 0.0;
143860       }
143861       if( (iCell==0)
143862        || (overlap<fMinOverlap)
143863        || (overlap==fMinOverlap && growth<fMinGrowth)
143864        || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
143865       ){
143866         bBest = 1;
143867         fMinOverlap = overlap;
143868       }
143869 #else
143870       if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
143871         bBest = 1;
143872       }
143873 #endif
143874       if( bBest ){
143875         fMinGrowth = growth;
143876         fMinArea = area;
143877         iBest = cell.iRowid;
143878       }
143879     }
143880 
143881     sqlite3_free(aCell);
143882     rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
143883     nodeRelease(pRtree, pNode);
143884     pNode = pChild;
143885   }
143886 
143887   *ppLeaf = pNode;
143888   return rc;
143889 }
143890 
143891 /*
143892 ** A cell with the same content as pCell has just been inserted into
143893 ** the node pNode. This function updates the bounding box cells in
143894 ** all ancestor elements.
143895 */
143896 static int AdjustTree(
143897   Rtree *pRtree,                    /* Rtree table */
143898   RtreeNode *pNode,                 /* Adjust ancestry of this node. */
143899   RtreeCell *pCell                  /* This cell was just inserted */
143900 ){
143901   RtreeNode *p = pNode;
143902   while( p->pParent ){
143903     RtreeNode *pParent = p->pParent;
143904     RtreeCell cell;
143905     int iCell;
143906 
143907     if( nodeParentIndex(pRtree, p, &iCell) ){
143908       return SQLITE_CORRUPT_VTAB;
143909     }
143910 
143911     nodeGetCell(pRtree, pParent, iCell, &cell);
143912     if( !cellContains(pRtree, &cell, pCell) ){
143913       cellUnion(pRtree, &cell, pCell);
143914       nodeOverwriteCell(pRtree, pParent, &cell, iCell);
143915     }
143916 
143917     p = pParent;
143918   }
143919   return SQLITE_OK;
143920 }
143921 
143922 /*
143923 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
143924 */
143925 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
143926   sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
143927   sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
143928   sqlite3_step(pRtree->pWriteRowid);
143929   return sqlite3_reset(pRtree->pWriteRowid);
143930 }
143931 
143932 /*
143933 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
143934 */
143935 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
143936   sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
143937   sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
143938   sqlite3_step(pRtree->pWriteParent);
143939   return sqlite3_reset(pRtree->pWriteParent);
143940 }
143941 
143942 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
143943 
143944 #if VARIANT_GUTTMAN_LINEAR_SPLIT
143945 /*
143946 ** Implementation of the linear variant of the PickNext() function from
143947 ** Guttman[84].
143948 */
143949 static RtreeCell *LinearPickNext(
143950   Rtree *pRtree,
143951   RtreeCell *aCell,
143952   int nCell,
143953   RtreeCell *pLeftBox,
143954   RtreeCell *pRightBox,
143955   int *aiUsed
143956 ){
143957   int ii;
143958   for(ii=0; aiUsed[ii]; ii++);
143959   aiUsed[ii] = 1;
143960   return &aCell[ii];
143961 }
143962 
143963 /*
143964 ** Implementation of the linear variant of the PickSeeds() function from
143965 ** Guttman[84].
143966 */
143967 static void LinearPickSeeds(
143968   Rtree *pRtree,
143969   RtreeCell *aCell,
143970   int nCell,
143971   int *piLeftSeed,
143972   int *piRightSeed
143973 ){
143974   int i;
143975   int iLeftSeed = 0;
143976   int iRightSeed = 1;
143977   RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
143978 
143979   /* Pick two "seed" cells from the array of cells. The algorithm used
143980   ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
143981   ** indices of the two seed cells in the array are stored in local
143982   ** variables iLeftSeek and iRightSeed.
143983   */
143984   for(i=0; i<pRtree->nDim; i++){
143985     RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
143986     RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
143987     RtreeDValue x3 = x1;
143988     RtreeDValue x4 = x2;
143989     int jj;
143990 
143991     int iCellLeft = 0;
143992     int iCellRight = 0;
143993 
143994     for(jj=1; jj<nCell; jj++){
143995       RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
143996       RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
143997 
143998       if( left<x1 ) x1 = left;
143999       if( right>x4 ) x4 = right;
144000       if( left>x3 ){
144001         x3 = left;
144002         iCellRight = jj;
144003       }
144004       if( right<x2 ){
144005         x2 = right;
144006         iCellLeft = jj;
144007       }
144008     }
144009 
144010     if( x4!=x1 ){
144011       RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
144012       if( normalwidth>maxNormalInnerWidth ){
144013         iLeftSeed = iCellLeft;
144014         iRightSeed = iCellRight;
144015       }
144016     }
144017   }
144018 
144019   *piLeftSeed = iLeftSeed;
144020   *piRightSeed = iRightSeed;
144021 }
144022 #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
144023 
144024 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
144025 /*
144026 ** Implementation of the quadratic variant of the PickNext() function from
144027 ** Guttman[84].
144028 */
144029 static RtreeCell *QuadraticPickNext(
144030   Rtree *pRtree,
144031   RtreeCell *aCell,
144032   int nCell,
144033   RtreeCell *pLeftBox,
144034   RtreeCell *pRightBox,
144035   int *aiUsed
144036 ){
144037   #define FABS(a) ((a)<0.0?-1.0*(a):(a))
144038 
144039   int iSelect = -1;
144040   RtreeDValue fDiff;
144041   int ii;
144042   for(ii=0; ii<nCell; ii++){
144043     if( aiUsed[ii]==0 ){
144044       RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
144045       RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
144046       RtreeDValue diff = FABS(right-left);
144047       if( iSelect<0 || diff>fDiff ){
144048         fDiff = diff;
144049         iSelect = ii;
144050       }
144051     }
144052   }
144053   aiUsed[iSelect] = 1;
144054   return &aCell[iSelect];
144055 }
144056 
144057 /*
144058 ** Implementation of the quadratic variant of the PickSeeds() function from
144059 ** Guttman[84].
144060 */
144061 static void QuadraticPickSeeds(
144062   Rtree *pRtree,
144063   RtreeCell *aCell,
144064   int nCell,
144065   int *piLeftSeed,
144066   int *piRightSeed
144067 ){
144068   int ii;
144069   int jj;
144070 
144071   int iLeftSeed = 0;
144072   int iRightSeed = 1;
144073   RtreeDValue fWaste = 0.0;
144074 
144075   for(ii=0; ii<nCell; ii++){
144076     for(jj=ii+1; jj<nCell; jj++){
144077       RtreeDValue right = cellArea(pRtree, &aCell[jj]);
144078       RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
144079       RtreeDValue waste = growth - right;
144080 
144081       if( waste>fWaste ){
144082         iLeftSeed = ii;
144083         iRightSeed = jj;
144084         fWaste = waste;
144085       }
144086     }
144087   }
144088 
144089   *piLeftSeed = iLeftSeed;
144090   *piRightSeed = iRightSeed;
144091 }
144092 #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
144093 
144094 /*
144095 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
144096 ** nIdx. The aIdx array contains the set of integers from 0 to
144097 ** (nIdx-1) in no particular order. This function sorts the values
144098 ** in aIdx according to the indexed values in aDistance. For
144099 ** example, assuming the inputs:
144100 **
144101 **   aIdx      = { 0,   1,   2,   3 }
144102 **   aDistance = { 5.0, 2.0, 7.0, 6.0 }
144103 **
144104 ** this function sets the aIdx array to contain:
144105 **
144106 **   aIdx      = { 0,   1,   2,   3 }
144107 **
144108 ** The aSpare array is used as temporary working space by the
144109 ** sorting algorithm.
144110 */
144111 static void SortByDistance(
144112   int *aIdx,
144113   int nIdx,
144114   RtreeDValue *aDistance,
144115   int *aSpare
144116 ){
144117   if( nIdx>1 ){
144118     int iLeft = 0;
144119     int iRight = 0;
144120 
144121     int nLeft = nIdx/2;
144122     int nRight = nIdx-nLeft;
144123     int *aLeft = aIdx;
144124     int *aRight = &aIdx[nLeft];
144125 
144126     SortByDistance(aLeft, nLeft, aDistance, aSpare);
144127     SortByDistance(aRight, nRight, aDistance, aSpare);
144128 
144129     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
144130     aLeft = aSpare;
144131 
144132     while( iLeft<nLeft || iRight<nRight ){
144133       if( iLeft==nLeft ){
144134         aIdx[iLeft+iRight] = aRight[iRight];
144135         iRight++;
144136       }else if( iRight==nRight ){
144137         aIdx[iLeft+iRight] = aLeft[iLeft];
144138         iLeft++;
144139       }else{
144140         RtreeDValue fLeft = aDistance[aLeft[iLeft]];
144141         RtreeDValue fRight = aDistance[aRight[iRight]];
144142         if( fLeft<fRight ){
144143           aIdx[iLeft+iRight] = aLeft[iLeft];
144144           iLeft++;
144145         }else{
144146           aIdx[iLeft+iRight] = aRight[iRight];
144147           iRight++;
144148         }
144149       }
144150     }
144151 
144152 #if 0
144153     /* Check that the sort worked */
144154     {
144155       int jj;
144156       for(jj=1; jj<nIdx; jj++){
144157         RtreeDValue left = aDistance[aIdx[jj-1]];
144158         RtreeDValue right = aDistance[aIdx[jj]];
144159         assert( left<=right );
144160       }
144161     }
144162 #endif
144163   }
144164 }
144165 
144166 /*
144167 ** Arguments aIdx, aCell and aSpare all point to arrays of size
144168 ** nIdx. The aIdx array contains the set of integers from 0 to
144169 ** (nIdx-1) in no particular order. This function sorts the values
144170 ** in aIdx according to dimension iDim of the cells in aCell. The
144171 ** minimum value of dimension iDim is considered first, the
144172 ** maximum used to break ties.
144173 **
144174 ** The aSpare array is used as temporary working space by the
144175 ** sorting algorithm.
144176 */
144177 static void SortByDimension(
144178   Rtree *pRtree,
144179   int *aIdx,
144180   int nIdx,
144181   int iDim,
144182   RtreeCell *aCell,
144183   int *aSpare
144184 ){
144185   if( nIdx>1 ){
144186 
144187     int iLeft = 0;
144188     int iRight = 0;
144189 
144190     int nLeft = nIdx/2;
144191     int nRight = nIdx-nLeft;
144192     int *aLeft = aIdx;
144193     int *aRight = &aIdx[nLeft];
144194 
144195     SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
144196     SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
144197 
144198     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
144199     aLeft = aSpare;
144200     while( iLeft<nLeft || iRight<nRight ){
144201       RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
144202       RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
144203       RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
144204       RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
144205       if( (iLeft!=nLeft) && ((iRight==nRight)
144206        || (xleft1<xright1)
144207        || (xleft1==xright1 && xleft2<xright2)
144208       )){
144209         aIdx[iLeft+iRight] = aLeft[iLeft];
144210         iLeft++;
144211       }else{
144212         aIdx[iLeft+iRight] = aRight[iRight];
144213         iRight++;
144214       }
144215     }
144216 
144217 #if 0
144218     /* Check that the sort worked */
144219     {
144220       int jj;
144221       for(jj=1; jj<nIdx; jj++){
144222         RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
144223         RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
144224         RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
144225         RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
144226         assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
144227       }
144228     }
144229 #endif
144230   }
144231 }
144232 
144233 #if VARIANT_RSTARTREE_SPLIT
144234 /*
144235 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
144236 */
144237 static int splitNodeStartree(
144238   Rtree *pRtree,
144239   RtreeCell *aCell,
144240   int nCell,
144241   RtreeNode *pLeft,
144242   RtreeNode *pRight,
144243   RtreeCell *pBboxLeft,
144244   RtreeCell *pBboxRight
144245 ){
144246   int **aaSorted;
144247   int *aSpare;
144248   int ii;
144249 
144250   int iBestDim = 0;
144251   int iBestSplit = 0;
144252   RtreeDValue fBestMargin = 0.0;
144253 
144254   int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
144255 
144256   aaSorted = (int **)sqlite3_malloc(nByte);
144257   if( !aaSorted ){
144258     return SQLITE_NOMEM;
144259   }
144260 
144261   aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
144262   memset(aaSorted, 0, nByte);
144263   for(ii=0; ii<pRtree->nDim; ii++){
144264     int jj;
144265     aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
144266     for(jj=0; jj<nCell; jj++){
144267       aaSorted[ii][jj] = jj;
144268     }
144269     SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
144270   }
144271 
144272   for(ii=0; ii<pRtree->nDim; ii++){
144273     RtreeDValue margin = 0.0;
144274     RtreeDValue fBestOverlap = 0.0;
144275     RtreeDValue fBestArea = 0.0;
144276     int iBestLeft = 0;
144277     int nLeft;
144278 
144279     for(
144280       nLeft=RTREE_MINCELLS(pRtree);
144281       nLeft<=(nCell-RTREE_MINCELLS(pRtree));
144282       nLeft++
144283     ){
144284       RtreeCell left;
144285       RtreeCell right;
144286       int kk;
144287       RtreeDValue overlap;
144288       RtreeDValue area;
144289 
144290       memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
144291       memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
144292       for(kk=1; kk<(nCell-1); kk++){
144293         if( kk<nLeft ){
144294           cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
144295         }else{
144296           cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
144297         }
144298       }
144299       margin += cellMargin(pRtree, &left);
144300       margin += cellMargin(pRtree, &right);
144301       overlap = cellOverlap(pRtree, &left, &right, 1, -1);
144302       area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
144303       if( (nLeft==RTREE_MINCELLS(pRtree))
144304        || (overlap<fBestOverlap)
144305        || (overlap==fBestOverlap && area<fBestArea)
144306       ){
144307         iBestLeft = nLeft;
144308         fBestOverlap = overlap;
144309         fBestArea = area;
144310       }
144311     }
144312 
144313     if( ii==0 || margin<fBestMargin ){
144314       iBestDim = ii;
144315       fBestMargin = margin;
144316       iBestSplit = iBestLeft;
144317     }
144318   }
144319 
144320   memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
144321   memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
144322   for(ii=0; ii<nCell; ii++){
144323     RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
144324     RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
144325     RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
144326     nodeInsertCell(pRtree, pTarget, pCell);
144327     cellUnion(pRtree, pBbox, pCell);
144328   }
144329 
144330   sqlite3_free(aaSorted);
144331   return SQLITE_OK;
144332 }
144333 #endif
144334 
144335 #if VARIANT_GUTTMAN_SPLIT
144336 /*
144337 ** Implementation of the regular R-tree SplitNode from Guttman[1984].
144338 */
144339 static int splitNodeGuttman(
144340   Rtree *pRtree,
144341   RtreeCell *aCell,
144342   int nCell,
144343   RtreeNode *pLeft,
144344   RtreeNode *pRight,
144345   RtreeCell *pBboxLeft,
144346   RtreeCell *pBboxRight
144347 ){
144348   int iLeftSeed = 0;
144349   int iRightSeed = 1;
144350   int *aiUsed;
144351   int i;
144352 
144353   aiUsed = sqlite3_malloc(sizeof(int)*nCell);
144354   if( !aiUsed ){
144355     return SQLITE_NOMEM;
144356   }
144357   memset(aiUsed, 0, sizeof(int)*nCell);
144358 
144359   PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
144360 
144361   memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
144362   memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
144363   nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
144364   nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
144365   aiUsed[iLeftSeed] = 1;
144366   aiUsed[iRightSeed] = 1;
144367 
144368   for(i=nCell-2; i>0; i--){
144369     RtreeCell *pNext;
144370     pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
144371     RtreeDValue diff =
144372       cellGrowth(pRtree, pBboxLeft, pNext) -
144373       cellGrowth(pRtree, pBboxRight, pNext)
144374     ;
144375     if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
144376      || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
144377     ){
144378       nodeInsertCell(pRtree, pRight, pNext);
144379       cellUnion(pRtree, pBboxRight, pNext);
144380     }else{
144381       nodeInsertCell(pRtree, pLeft, pNext);
144382       cellUnion(pRtree, pBboxLeft, pNext);
144383     }
144384   }
144385 
144386   sqlite3_free(aiUsed);
144387   return SQLITE_OK;
144388 }
144389 #endif
144390 
144391 static int updateMapping(
144392   Rtree *pRtree,
144393   i64 iRowid,
144394   RtreeNode *pNode,
144395   int iHeight
144396 ){
144397   int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
144398   xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
144399   if( iHeight>0 ){
144400     RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
144401     if( pChild ){
144402       nodeRelease(pRtree, pChild->pParent);
144403       nodeReference(pNode);
144404       pChild->pParent = pNode;
144405     }
144406   }
144407   return xSetMapping(pRtree, iRowid, pNode->iNode);
144408 }
144409 
144410 static int SplitNode(
144411   Rtree *pRtree,
144412   RtreeNode *pNode,
144413   RtreeCell *pCell,
144414   int iHeight
144415 ){
144416   int i;
144417   int newCellIsRight = 0;
144418 
144419   int rc = SQLITE_OK;
144420   int nCell = NCELL(pNode);
144421   RtreeCell *aCell;
144422   int *aiUsed;
144423 
144424   RtreeNode *pLeft = 0;
144425   RtreeNode *pRight = 0;
144426 
144427   RtreeCell leftbbox;
144428   RtreeCell rightbbox;
144429 
144430   /* Allocate an array and populate it with a copy of pCell and
144431   ** all cells from node pLeft. Then zero the original node.
144432   */
144433   aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
144434   if( !aCell ){
144435     rc = SQLITE_NOMEM;
144436     goto splitnode_out;
144437   }
144438   aiUsed = (int *)&aCell[nCell+1];
144439   memset(aiUsed, 0, sizeof(int)*(nCell+1));
144440   for(i=0; i<nCell; i++){
144441     nodeGetCell(pRtree, pNode, i, &aCell[i]);
144442   }
144443   nodeZero(pRtree, pNode);
144444   memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
144445   nCell++;
144446 
144447   if( pNode->iNode==1 ){
144448     pRight = nodeNew(pRtree, pNode);
144449     pLeft = nodeNew(pRtree, pNode);
144450     pRtree->iDepth++;
144451     pNode->isDirty = 1;
144452     writeInt16(pNode->zData, pRtree->iDepth);
144453   }else{
144454     pLeft = pNode;
144455     pRight = nodeNew(pRtree, pLeft->pParent);
144456     nodeReference(pLeft);
144457   }
144458 
144459   if( !pLeft || !pRight ){
144460     rc = SQLITE_NOMEM;
144461     goto splitnode_out;
144462   }
144463 
144464   memset(pLeft->zData, 0, pRtree->iNodeSize);
144465   memset(pRight->zData, 0, pRtree->iNodeSize);
144466 
144467   rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
144468   if( rc!=SQLITE_OK ){
144469     goto splitnode_out;
144470   }
144471 
144472   /* Ensure both child nodes have node numbers assigned to them by calling
144473   ** nodeWrite(). Node pRight always needs a node number, as it was created
144474   ** by nodeNew() above. But node pLeft sometimes already has a node number.
144475   ** In this case avoid the all to nodeWrite().
144476   */
144477   if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
144478    || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
144479   ){
144480     goto splitnode_out;
144481   }
144482 
144483   rightbbox.iRowid = pRight->iNode;
144484   leftbbox.iRowid = pLeft->iNode;
144485 
144486   if( pNode->iNode==1 ){
144487     rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
144488     if( rc!=SQLITE_OK ){
144489       goto splitnode_out;
144490     }
144491   }else{
144492     RtreeNode *pParent = pLeft->pParent;
144493     int iCell;
144494     rc = nodeParentIndex(pRtree, pLeft, &iCell);
144495     if( rc==SQLITE_OK ){
144496       nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
144497       rc = AdjustTree(pRtree, pParent, &leftbbox);
144498     }
144499     if( rc!=SQLITE_OK ){
144500       goto splitnode_out;
144501     }
144502   }
144503   if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
144504     goto splitnode_out;
144505   }
144506 
144507   for(i=0; i<NCELL(pRight); i++){
144508     i64 iRowid = nodeGetRowid(pRtree, pRight, i);
144509     rc = updateMapping(pRtree, iRowid, pRight, iHeight);
144510     if( iRowid==pCell->iRowid ){
144511       newCellIsRight = 1;
144512     }
144513     if( rc!=SQLITE_OK ){
144514       goto splitnode_out;
144515     }
144516   }
144517   if( pNode->iNode==1 ){
144518     for(i=0; i<NCELL(pLeft); i++){
144519       i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
144520       rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
144521       if( rc!=SQLITE_OK ){
144522         goto splitnode_out;
144523       }
144524     }
144525   }else if( newCellIsRight==0 ){
144526     rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
144527   }
144528 
144529   if( rc==SQLITE_OK ){
144530     rc = nodeRelease(pRtree, pRight);
144531     pRight = 0;
144532   }
144533   if( rc==SQLITE_OK ){
144534     rc = nodeRelease(pRtree, pLeft);
144535     pLeft = 0;
144536   }
144537 
144538 splitnode_out:
144539   nodeRelease(pRtree, pRight);
144540   nodeRelease(pRtree, pLeft);
144541   sqlite3_free(aCell);
144542   return rc;
144543 }
144544 
144545 /*
144546 ** If node pLeaf is not the root of the r-tree and its pParent pointer is
144547 ** still NULL, load all ancestor nodes of pLeaf into memory and populate
144548 ** the pLeaf->pParent chain all the way up to the root node.
144549 **
144550 ** This operation is required when a row is deleted (or updated - an update
144551 ** is implemented as a delete followed by an insert). SQLite provides the
144552 ** rowid of the row to delete, which can be used to find the leaf on which
144553 ** the entry resides (argument pLeaf). Once the leaf is located, this
144554 ** function is called to determine its ancestry.
144555 */
144556 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
144557   int rc = SQLITE_OK;
144558   RtreeNode *pChild = pLeaf;
144559   while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
144560     int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
144561     sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
144562     rc = sqlite3_step(pRtree->pReadParent);
144563     if( rc==SQLITE_ROW ){
144564       RtreeNode *pTest;           /* Used to test for reference loops */
144565       i64 iNode;                  /* Node number of parent node */
144566 
144567       /* Before setting pChild->pParent, test that we are not creating a
144568       ** loop of references (as we would if, say, pChild==pParent). We don't
144569       ** want to do this as it leads to a memory leak when trying to delete
144570       ** the referenced counted node structures.
144571       */
144572       iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
144573       for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
144574       if( !pTest ){
144575         rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
144576       }
144577     }
144578     rc = sqlite3_reset(pRtree->pReadParent);
144579     if( rc==SQLITE_OK ) rc = rc2;
144580     if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
144581     pChild = pChild->pParent;
144582   }
144583   return rc;
144584 }
144585 
144586 static int deleteCell(Rtree *, RtreeNode *, int, int);
144587 
144588 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
144589   int rc;
144590   int rc2;
144591   RtreeNode *pParent = 0;
144592   int iCell;
144593 
144594   assert( pNode->nRef==1 );
144595 
144596   /* Remove the entry in the parent cell. */
144597   rc = nodeParentIndex(pRtree, pNode, &iCell);
144598   if( rc==SQLITE_OK ){
144599     pParent = pNode->pParent;
144600     pNode->pParent = 0;
144601     rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
144602   }
144603   rc2 = nodeRelease(pRtree, pParent);
144604   if( rc==SQLITE_OK ){
144605     rc = rc2;
144606   }
144607   if( rc!=SQLITE_OK ){
144608     return rc;
144609   }
144610 
144611   /* Remove the xxx_node entry. */
144612   sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
144613   sqlite3_step(pRtree->pDeleteNode);
144614   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
144615     return rc;
144616   }
144617 
144618   /* Remove the xxx_parent entry. */
144619   sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
144620   sqlite3_step(pRtree->pDeleteParent);
144621   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
144622     return rc;
144623   }
144624 
144625   /* Remove the node from the in-memory hash table and link it into
144626   ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
144627   */
144628   nodeHashDelete(pRtree, pNode);
144629   pNode->iNode = iHeight;
144630   pNode->pNext = pRtree->pDeleted;
144631   pNode->nRef++;
144632   pRtree->pDeleted = pNode;
144633 
144634   return SQLITE_OK;
144635 }
144636 
144637 static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
144638   RtreeNode *pParent = pNode->pParent;
144639   int rc = SQLITE_OK;
144640   if( pParent ){
144641     int ii;
144642     int nCell = NCELL(pNode);
144643     RtreeCell box;                            /* Bounding box for pNode */
144644     nodeGetCell(pRtree, pNode, 0, &box);
144645     for(ii=1; ii<nCell; ii++){
144646       RtreeCell cell;
144647       nodeGetCell(pRtree, pNode, ii, &cell);
144648       cellUnion(pRtree, &box, &cell);
144649     }
144650     box.iRowid = pNode->iNode;
144651     rc = nodeParentIndex(pRtree, pNode, &ii);
144652     if( rc==SQLITE_OK ){
144653       nodeOverwriteCell(pRtree, pParent, &box, ii);
144654       rc = fixBoundingBox(pRtree, pParent);
144655     }
144656   }
144657   return rc;
144658 }
144659 
144660 /*
144661 ** Delete the cell at index iCell of node pNode. After removing the
144662 ** cell, adjust the r-tree data structure if required.
144663 */
144664 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
144665   RtreeNode *pParent;
144666   int rc;
144667 
144668   if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
144669     return rc;
144670   }
144671 
144672   /* Remove the cell from the node. This call just moves bytes around
144673   ** the in-memory node image, so it cannot fail.
144674   */
144675   nodeDeleteCell(pRtree, pNode, iCell);
144676 
144677   /* If the node is not the tree root and now has less than the minimum
144678   ** number of cells, remove it from the tree. Otherwise, update the
144679   ** cell in the parent node so that it tightly contains the updated
144680   ** node.
144681   */
144682   pParent = pNode->pParent;
144683   assert( pParent || pNode->iNode==1 );
144684   if( pParent ){
144685     if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
144686       rc = removeNode(pRtree, pNode, iHeight);
144687     }else{
144688       rc = fixBoundingBox(pRtree, pNode);
144689     }
144690   }
144691 
144692   return rc;
144693 }
144694 
144695 static int Reinsert(
144696   Rtree *pRtree,
144697   RtreeNode *pNode,
144698   RtreeCell *pCell,
144699   int iHeight
144700 ){
144701   int *aOrder;
144702   int *aSpare;
144703   RtreeCell *aCell;
144704   RtreeDValue *aDistance;
144705   int nCell;
144706   RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
144707   int iDim;
144708   int ii;
144709   int rc = SQLITE_OK;
144710   int n;
144711 
144712   memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
144713 
144714   nCell = NCELL(pNode)+1;
144715   n = (nCell+1)&(~1);
144716 
144717   /* Allocate the buffers used by this operation. The allocation is
144718   ** relinquished before this function returns.
144719   */
144720   aCell = (RtreeCell *)sqlite3_malloc(n * (
144721     sizeof(RtreeCell)     +         /* aCell array */
144722     sizeof(int)           +         /* aOrder array */
144723     sizeof(int)           +         /* aSpare array */
144724     sizeof(RtreeDValue)             /* aDistance array */
144725   ));
144726   if( !aCell ){
144727     return SQLITE_NOMEM;
144728   }
144729   aOrder    = (int *)&aCell[n];
144730   aSpare    = (int *)&aOrder[n];
144731   aDistance = (RtreeDValue *)&aSpare[n];
144732 
144733   for(ii=0; ii<nCell; ii++){
144734     if( ii==(nCell-1) ){
144735       memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
144736     }else{
144737       nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
144738     }
144739     aOrder[ii] = ii;
144740     for(iDim=0; iDim<pRtree->nDim; iDim++){
144741       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
144742       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
144743     }
144744   }
144745   for(iDim=0; iDim<pRtree->nDim; iDim++){
144746     aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
144747   }
144748 
144749   for(ii=0; ii<nCell; ii++){
144750     aDistance[ii] = 0.0;
144751     for(iDim=0; iDim<pRtree->nDim; iDim++){
144752       RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
144753                                DCOORD(aCell[ii].aCoord[iDim*2]));
144754       aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
144755     }
144756   }
144757 
144758   SortByDistance(aOrder, nCell, aDistance, aSpare);
144759   nodeZero(pRtree, pNode);
144760 
144761   for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
144762     RtreeCell *p = &aCell[aOrder[ii]];
144763     nodeInsertCell(pRtree, pNode, p);
144764     if( p->iRowid==pCell->iRowid ){
144765       if( iHeight==0 ){
144766         rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
144767       }else{
144768         rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
144769       }
144770     }
144771   }
144772   if( rc==SQLITE_OK ){
144773     rc = fixBoundingBox(pRtree, pNode);
144774   }
144775   for(; rc==SQLITE_OK && ii<nCell; ii++){
144776     /* Find a node to store this cell in. pNode->iNode currently contains
144777     ** the height of the sub-tree headed by the cell.
144778     */
144779     RtreeNode *pInsert;
144780     RtreeCell *p = &aCell[aOrder[ii]];
144781     rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
144782     if( rc==SQLITE_OK ){
144783       int rc2;
144784       rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
144785       rc2 = nodeRelease(pRtree, pInsert);
144786       if( rc==SQLITE_OK ){
144787         rc = rc2;
144788       }
144789     }
144790   }
144791 
144792   sqlite3_free(aCell);
144793   return rc;
144794 }
144795 
144796 /*
144797 ** Insert cell pCell into node pNode. Node pNode is the head of a
144798 ** subtree iHeight high (leaf nodes have iHeight==0).
144799 */
144800 static int rtreeInsertCell(
144801   Rtree *pRtree,
144802   RtreeNode *pNode,
144803   RtreeCell *pCell,
144804   int iHeight
144805 ){
144806   int rc = SQLITE_OK;
144807   if( iHeight>0 ){
144808     RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
144809     if( pChild ){
144810       nodeRelease(pRtree, pChild->pParent);
144811       nodeReference(pNode);
144812       pChild->pParent = pNode;
144813     }
144814   }
144815   if( nodeInsertCell(pRtree, pNode, pCell) ){
144816 #if VARIANT_RSTARTREE_REINSERT
144817     if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
144818       rc = SplitNode(pRtree, pNode, pCell, iHeight);
144819     }else{
144820       pRtree->iReinsertHeight = iHeight;
144821       rc = Reinsert(pRtree, pNode, pCell, iHeight);
144822     }
144823 #else
144824     rc = SplitNode(pRtree, pNode, pCell, iHeight);
144825 #endif
144826   }else{
144827     rc = AdjustTree(pRtree, pNode, pCell);
144828     if( rc==SQLITE_OK ){
144829       if( iHeight==0 ){
144830         rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
144831       }else{
144832         rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
144833       }
144834     }
144835   }
144836   return rc;
144837 }
144838 
144839 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
144840   int ii;
144841   int rc = SQLITE_OK;
144842   int nCell = NCELL(pNode);
144843 
144844   for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
144845     RtreeNode *pInsert;
144846     RtreeCell cell;
144847     nodeGetCell(pRtree, pNode, ii, &cell);
144848 
144849     /* Find a node to store this cell in. pNode->iNode currently contains
144850     ** the height of the sub-tree headed by the cell.
144851     */
144852     rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
144853     if( rc==SQLITE_OK ){
144854       int rc2;
144855       rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
144856       rc2 = nodeRelease(pRtree, pInsert);
144857       if( rc==SQLITE_OK ){
144858         rc = rc2;
144859       }
144860     }
144861   }
144862   return rc;
144863 }
144864 
144865 /*
144866 ** Select a currently unused rowid for a new r-tree record.
144867 */
144868 static int newRowid(Rtree *pRtree, i64 *piRowid){
144869   int rc;
144870   sqlite3_bind_null(pRtree->pWriteRowid, 1);
144871   sqlite3_bind_null(pRtree->pWriteRowid, 2);
144872   sqlite3_step(pRtree->pWriteRowid);
144873   rc = sqlite3_reset(pRtree->pWriteRowid);
144874   *piRowid = sqlite3_last_insert_rowid(pRtree->db);
144875   return rc;
144876 }
144877 
144878 /*
144879 ** Remove the entry with rowid=iDelete from the r-tree structure.
144880 */
144881 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
144882   int rc;                         /* Return code */
144883   RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
144884   int iCell;                      /* Index of iDelete cell in pLeaf */
144885   RtreeNode *pRoot;               /* Root node of rtree structure */
144886 
144887 
144888   /* Obtain a reference to the root node to initialize Rtree.iDepth */
144889   rc = nodeAcquire(pRtree, 1, 0, &pRoot);
144890 
144891   /* Obtain a reference to the leaf node that contains the entry
144892   ** about to be deleted.
144893   */
144894   if( rc==SQLITE_OK ){
144895     rc = findLeafNode(pRtree, iDelete, &pLeaf);
144896   }
144897 
144898   /* Delete the cell in question from the leaf node. */
144899   if( rc==SQLITE_OK ){
144900     int rc2;
144901     rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
144902     if( rc==SQLITE_OK ){
144903       rc = deleteCell(pRtree, pLeaf, iCell, 0);
144904     }
144905     rc2 = nodeRelease(pRtree, pLeaf);
144906     if( rc==SQLITE_OK ){
144907       rc = rc2;
144908     }
144909   }
144910 
144911   /* Delete the corresponding entry in the <rtree>_rowid table. */
144912   if( rc==SQLITE_OK ){
144913     sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
144914     sqlite3_step(pRtree->pDeleteRowid);
144915     rc = sqlite3_reset(pRtree->pDeleteRowid);
144916   }
144917 
144918   /* Check if the root node now has exactly one child. If so, remove
144919   ** it, schedule the contents of the child for reinsertion and
144920   ** reduce the tree height by one.
144921   **
144922   ** This is equivalent to copying the contents of the child into
144923   ** the root node (the operation that Gutman's paper says to perform
144924   ** in this scenario).
144925   */
144926   if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
144927     int rc2;
144928     RtreeNode *pChild;
144929     i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
144930     rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
144931     if( rc==SQLITE_OK ){
144932       rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
144933     }
144934     rc2 = nodeRelease(pRtree, pChild);
144935     if( rc==SQLITE_OK ) rc = rc2;
144936     if( rc==SQLITE_OK ){
144937       pRtree->iDepth--;
144938       writeInt16(pRoot->zData, pRtree->iDepth);
144939       pRoot->isDirty = 1;
144940     }
144941   }
144942 
144943   /* Re-insert the contents of any underfull nodes removed from the tree. */
144944   for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
144945     if( rc==SQLITE_OK ){
144946       rc = reinsertNodeContent(pRtree, pLeaf);
144947     }
144948     pRtree->pDeleted = pLeaf->pNext;
144949     sqlite3_free(pLeaf);
144950   }
144951 
144952   /* Release the reference to the root node. */
144953   if( rc==SQLITE_OK ){
144954     rc = nodeRelease(pRtree, pRoot);
144955   }else{
144956     nodeRelease(pRtree, pRoot);
144957   }
144958 
144959   return rc;
144960 }
144961 
144962 /*
144963 ** Rounding constants for float->double conversion.
144964 */
144965 #define RNDTOWARDS  (1.0 - 1.0/8388608.0)  /* Round towards zero */
144966 #define RNDAWAY     (1.0 + 1.0/8388608.0)  /* Round away from zero */
144967 
144968 #if !defined(SQLITE_RTREE_INT_ONLY)
144969 /*
144970 ** Convert an sqlite3_value into an RtreeValue (presumably a float)
144971 ** while taking care to round toward negative or positive, respectively.
144972 */
144973 static RtreeValue rtreeValueDown(sqlite3_value *v){
144974   double d = sqlite3_value_double(v);
144975   float f = (float)d;
144976   if( f>d ){
144977     f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
144978   }
144979   return f;
144980 }
144981 static RtreeValue rtreeValueUp(sqlite3_value *v){
144982   double d = sqlite3_value_double(v);
144983   float f = (float)d;
144984   if( f<d ){
144985     f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
144986   }
144987   return f;
144988 }
144989 #endif /* !defined(SQLITE_RTREE_INT_ONLY) */
144990 
144991 
144992 /*
144993 ** The xUpdate method for rtree module virtual tables.
144994 */
144995 static int rtreeUpdate(
144996   sqlite3_vtab *pVtab,
144997   int nData,
144998   sqlite3_value **azData,
144999   sqlite_int64 *pRowid
145000 ){
145001   Rtree *pRtree = (Rtree *)pVtab;
145002   int rc = SQLITE_OK;
145003   RtreeCell cell;                 /* New cell to insert if nData>1 */
145004   int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
145005 
145006   rtreeReference(pRtree);
145007   assert(nData>=1);
145008 
145009   /* Constraint handling. A write operation on an r-tree table may return
145010   ** SQLITE_CONSTRAINT for two reasons:
145011   **
145012   **   1. A duplicate rowid value, or
145013   **   2. The supplied data violates the "x2>=x1" constraint.
145014   **
145015   ** In the first case, if the conflict-handling mode is REPLACE, then
145016   ** the conflicting row can be removed before proceeding. In the second
145017   ** case, SQLITE_CONSTRAINT must be returned regardless of the
145018   ** conflict-handling mode specified by the user.
145019   */
145020   if( nData>1 ){
145021     int ii;
145022 
145023     /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
145024     assert( nData==(pRtree->nDim*2 + 3) );
145025 #ifndef SQLITE_RTREE_INT_ONLY
145026     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
145027       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
145028         cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
145029         cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
145030         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
145031           rc = SQLITE_CONSTRAINT;
145032           goto constraint;
145033         }
145034       }
145035     }else
145036 #endif
145037     {
145038       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
145039         cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
145040         cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
145041         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
145042           rc = SQLITE_CONSTRAINT;
145043           goto constraint;
145044         }
145045       }
145046     }
145047 
145048     /* If a rowid value was supplied, check if it is already present in
145049     ** the table. If so, the constraint has failed. */
145050     if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
145051       cell.iRowid = sqlite3_value_int64(azData[2]);
145052       if( sqlite3_value_type(azData[0])==SQLITE_NULL
145053        || sqlite3_value_int64(azData[0])!=cell.iRowid
145054       ){
145055         int steprc;
145056         sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
145057         steprc = sqlite3_step(pRtree->pReadRowid);
145058         rc = sqlite3_reset(pRtree->pReadRowid);
145059         if( SQLITE_ROW==steprc ){
145060           if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
145061             rc = rtreeDeleteRowid(pRtree, cell.iRowid);
145062           }else{
145063             rc = SQLITE_CONSTRAINT;
145064             goto constraint;
145065           }
145066         }
145067       }
145068       bHaveRowid = 1;
145069     }
145070   }
145071 
145072   /* If azData[0] is not an SQL NULL value, it is the rowid of a
145073   ** record to delete from the r-tree table. The following block does
145074   ** just that.
145075   */
145076   if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
145077     rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
145078   }
145079 
145080   /* If the azData[] array contains more than one element, elements
145081   ** (azData[2]..azData[argc-1]) contain a new record to insert into
145082   ** the r-tree structure.
145083   */
145084   if( rc==SQLITE_OK && nData>1 ){
145085     /* Insert the new record into the r-tree */
145086     RtreeNode *pLeaf = 0;
145087 
145088     /* Figure out the rowid of the new row. */
145089     if( bHaveRowid==0 ){
145090       rc = newRowid(pRtree, &cell.iRowid);
145091     }
145092     *pRowid = cell.iRowid;
145093 
145094     if( rc==SQLITE_OK ){
145095       rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
145096     }
145097     if( rc==SQLITE_OK ){
145098       int rc2;
145099       pRtree->iReinsertHeight = -1;
145100       rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
145101       rc2 = nodeRelease(pRtree, pLeaf);
145102       if( rc==SQLITE_OK ){
145103         rc = rc2;
145104       }
145105     }
145106   }
145107 
145108 constraint:
145109   rtreeRelease(pRtree);
145110   return rc;
145111 }
145112 
145113 /*
145114 ** The xRename method for rtree module virtual tables.
145115 */
145116 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
145117   Rtree *pRtree = (Rtree *)pVtab;
145118   int rc = SQLITE_NOMEM;
145119   char *zSql = sqlite3_mprintf(
145120     "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
145121     "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
145122     "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
145123     , pRtree->zDb, pRtree->zName, zNewName
145124     , pRtree->zDb, pRtree->zName, zNewName
145125     , pRtree->zDb, pRtree->zName, zNewName
145126   );
145127   if( zSql ){
145128     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
145129     sqlite3_free(zSql);
145130   }
145131   return rc;
145132 }
145133 
145134 /*
145135 ** This function populates the pRtree->nRowEst variable with an estimate
145136 ** of the number of rows in the virtual table. If possible, this is based
145137 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
145138 */
145139 static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
145140   const char *zSql = "SELECT stat FROM sqlite_stat1 WHERE tbl= ? || '_rowid'";
145141   sqlite3_stmt *p;
145142   int rc;
145143   i64 nRow = 0;
145144 
145145   rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
145146   if( rc==SQLITE_OK ){
145147     sqlite3_bind_text(p, 1, pRtree->zName, -1, SQLITE_STATIC);
145148     if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
145149     rc = sqlite3_finalize(p);
145150   }else if( rc!=SQLITE_NOMEM ){
145151     rc = SQLITE_OK;
145152   }
145153 
145154   if( rc==SQLITE_OK ){
145155     if( nRow==0 ){
145156       pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
145157     }else{
145158       pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
145159     }
145160   }
145161 
145162   return rc;
145163 }
145164 
145165 static sqlite3_module rtreeModule = {
145166   0,                          /* iVersion */
145167   rtreeCreate,                /* xCreate - create a table */
145168   rtreeConnect,               /* xConnect - connect to an existing table */
145169   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
145170   rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
145171   rtreeDestroy,               /* xDestroy - Drop a table */
145172   rtreeOpen,                  /* xOpen - open a cursor */
145173   rtreeClose,                 /* xClose - close a cursor */
145174   rtreeFilter,                /* xFilter - configure scan constraints */
145175   rtreeNext,                  /* xNext - advance a cursor */
145176   rtreeEof,                   /* xEof */
145177   rtreeColumn,                /* xColumn - read data */
145178   rtreeRowid,                 /* xRowid - read data */
145179   rtreeUpdate,                /* xUpdate - write data */
145180   0,                          /* xBegin - begin transaction */
145181   0,                          /* xSync - sync transaction */
145182   0,                          /* xCommit - commit transaction */
145183   0,                          /* xRollback - rollback transaction */
145184   0,                          /* xFindFunction - function overloading */
145185   rtreeRename,                /* xRename - rename the table */
145186   0,                          /* xSavepoint */
145187   0,                          /* xRelease */
145188   0                           /* xRollbackTo */
145189 };
145190 
145191 static int rtreeSqlInit(
145192   Rtree *pRtree,
145193   sqlite3 *db,
145194   const char *zDb,
145195   const char *zPrefix,
145196   int isCreate
145197 ){
145198   int rc = SQLITE_OK;
145199 
145200   #define N_STATEMENT 9
145201   static const char *azSql[N_STATEMENT] = {
145202     /* Read and write the xxx_node table */
145203     "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
145204     "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
145205     "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
145206 
145207     /* Read and write the xxx_rowid table */
145208     "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
145209     "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
145210     "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
145211 
145212     /* Read and write the xxx_parent table */
145213     "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
145214     "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
145215     "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
145216   };
145217   sqlite3_stmt **appStmt[N_STATEMENT];
145218   int i;
145219 
145220   pRtree->db = db;
145221 
145222   if( isCreate ){
145223     char *zCreate = sqlite3_mprintf(
145224 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
145225 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
145226 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
145227 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
145228       zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
145229     );
145230     if( !zCreate ){
145231       return SQLITE_NOMEM;
145232     }
145233     rc = sqlite3_exec(db, zCreate, 0, 0, 0);
145234     sqlite3_free(zCreate);
145235     if( rc!=SQLITE_OK ){
145236       return rc;
145237     }
145238   }
145239 
145240   appStmt[0] = &pRtree->pReadNode;
145241   appStmt[1] = &pRtree->pWriteNode;
145242   appStmt[2] = &pRtree->pDeleteNode;
145243   appStmt[3] = &pRtree->pReadRowid;
145244   appStmt[4] = &pRtree->pWriteRowid;
145245   appStmt[5] = &pRtree->pDeleteRowid;
145246   appStmt[6] = &pRtree->pReadParent;
145247   appStmt[7] = &pRtree->pWriteParent;
145248   appStmt[8] = &pRtree->pDeleteParent;
145249 
145250   rc = rtreeQueryStat1(db, pRtree);
145251   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
145252     char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
145253     if( zSql ){
145254       rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0);
145255     }else{
145256       rc = SQLITE_NOMEM;
145257     }
145258     sqlite3_free(zSql);
145259   }
145260 
145261   return rc;
145262 }
145263 
145264 /*
145265 ** The second argument to this function contains the text of an SQL statement
145266 ** that returns a single integer value. The statement is compiled and executed
145267 ** using database connection db. If successful, the integer value returned
145268 ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
145269 ** code is returned and the value of *piVal after returning is not defined.
145270 */
145271 static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
145272   int rc = SQLITE_NOMEM;
145273   if( zSql ){
145274     sqlite3_stmt *pStmt = 0;
145275     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
145276     if( rc==SQLITE_OK ){
145277       if( SQLITE_ROW==sqlite3_step(pStmt) ){
145278         *piVal = sqlite3_column_int(pStmt, 0);
145279       }
145280       rc = sqlite3_finalize(pStmt);
145281     }
145282   }
145283   return rc;
145284 }
145285 
145286 /*
145287 ** This function is called from within the xConnect() or xCreate() method to
145288 ** determine the node-size used by the rtree table being created or connected
145289 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
145290 ** Otherwise, an SQLite error code is returned.
145291 **
145292 ** If this function is being called as part of an xConnect(), then the rtree
145293 ** table already exists. In this case the node-size is determined by inspecting
145294 ** the root node of the tree.
145295 **
145296 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
145297 ** This ensures that each node is stored on a single database page. If the
145298 ** database page-size is so large that more than RTREE_MAXCELLS entries
145299 ** would fit in a single node, use a smaller node-size.
145300 */
145301 static int getNodeSize(
145302   sqlite3 *db,                    /* Database handle */
145303   Rtree *pRtree,                  /* Rtree handle */
145304   int isCreate,                   /* True for xCreate, false for xConnect */
145305   char **pzErr                    /* OUT: Error message, if any */
145306 ){
145307   int rc;
145308   char *zSql;
145309   if( isCreate ){
145310     int iPageSize = 0;
145311     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
145312     rc = getIntFromStmt(db, zSql, &iPageSize);
145313     if( rc==SQLITE_OK ){
145314       pRtree->iNodeSize = iPageSize-64;
145315       if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
145316         pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
145317       }
145318     }else{
145319       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
145320     }
145321   }else{
145322     zSql = sqlite3_mprintf(
145323         "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
145324         pRtree->zDb, pRtree->zName
145325     );
145326     rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
145327     if( rc!=SQLITE_OK ){
145328       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
145329     }
145330   }
145331 
145332   sqlite3_free(zSql);
145333   return rc;
145334 }
145335 
145336 /*
145337 ** This function is the implementation of both the xConnect and xCreate
145338 ** methods of the r-tree virtual table.
145339 **
145340 **   argv[0]   -> module name
145341 **   argv[1]   -> database name
145342 **   argv[2]   -> table name
145343 **   argv[...] -> column names...
145344 */
145345 static int rtreeInit(
145346   sqlite3 *db,                        /* Database connection */
145347   void *pAux,                         /* One of the RTREE_COORD_* constants */
145348   int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
145349   sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
145350   char **pzErr,                       /* OUT: Error message, if any */
145351   int isCreate                        /* True for xCreate, false for xConnect */
145352 ){
145353   int rc = SQLITE_OK;
145354   Rtree *pRtree;
145355   int nDb;              /* Length of string argv[1] */
145356   int nName;            /* Length of string argv[2] */
145357   int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
145358 
145359   const char *aErrMsg[] = {
145360     0,                                                    /* 0 */
145361     "Wrong number of columns for an rtree table",         /* 1 */
145362     "Too few columns for an rtree table",                 /* 2 */
145363     "Too many columns for an rtree table"                 /* 3 */
145364   };
145365 
145366   int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
145367   if( aErrMsg[iErr] ){
145368     *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
145369     return SQLITE_ERROR;
145370   }
145371 
145372   sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
145373 
145374   /* Allocate the sqlite3_vtab structure */
145375   nDb = (int)strlen(argv[1]);
145376   nName = (int)strlen(argv[2]);
145377   pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
145378   if( !pRtree ){
145379     return SQLITE_NOMEM;
145380   }
145381   memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
145382   pRtree->nBusy = 1;
145383   pRtree->base.pModule = &rtreeModule;
145384   pRtree->zDb = (char *)&pRtree[1];
145385   pRtree->zName = &pRtree->zDb[nDb+1];
145386   pRtree->nDim = (argc-4)/2;
145387   pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
145388   pRtree->eCoordType = eCoordType;
145389   memcpy(pRtree->zDb, argv[1], nDb);
145390   memcpy(pRtree->zName, argv[2], nName);
145391 
145392   /* Figure out the node size to use. */
145393   rc = getNodeSize(db, pRtree, isCreate, pzErr);
145394 
145395   /* Create/Connect to the underlying relational database schema. If
145396   ** that is successful, call sqlite3_declare_vtab() to configure
145397   ** the r-tree table schema.
145398   */
145399   if( rc==SQLITE_OK ){
145400     if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
145401       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
145402     }else{
145403       char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
145404       char *zTmp;
145405       int ii;
145406       for(ii=4; zSql && ii<argc; ii++){
145407         zTmp = zSql;
145408         zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
145409         sqlite3_free(zTmp);
145410       }
145411       if( zSql ){
145412         zTmp = zSql;
145413         zSql = sqlite3_mprintf("%s);", zTmp);
145414         sqlite3_free(zTmp);
145415       }
145416       if( !zSql ){
145417         rc = SQLITE_NOMEM;
145418       }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
145419         *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
145420       }
145421       sqlite3_free(zSql);
145422     }
145423   }
145424 
145425   if( rc==SQLITE_OK ){
145426     *ppVtab = (sqlite3_vtab *)pRtree;
145427   }else{
145428     rtreeRelease(pRtree);
145429   }
145430   return rc;
145431 }
145432 
145433 
145434 /*
145435 ** Implementation of a scalar function that decodes r-tree nodes to
145436 ** human readable strings. This can be used for debugging and analysis.
145437 **
145438 ** The scalar function takes two arguments, a blob of data containing
145439 ** an r-tree node, and the number of dimensions the r-tree indexes.
145440 ** For a two-dimensional r-tree structure called "rt", to deserialize
145441 ** all nodes, a statement like:
145442 **
145443 **   SELECT rtreenode(2, data) FROM rt_node;
145444 **
145445 ** The human readable string takes the form of a Tcl list with one
145446 ** entry for each cell in the r-tree node. Each entry is itself a
145447 ** list, containing the 8-byte rowid/pageno followed by the
145448 ** <num-dimension>*2 coordinates.
145449 */
145450 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
145451   char *zText = 0;
145452   RtreeNode node;
145453   Rtree tree;
145454   int ii;
145455 
145456   UNUSED_PARAMETER(nArg);
145457   memset(&node, 0, sizeof(RtreeNode));
145458   memset(&tree, 0, sizeof(Rtree));
145459   tree.nDim = sqlite3_value_int(apArg[0]);
145460   tree.nBytesPerCell = 8 + 8 * tree.nDim;
145461   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
145462 
145463   for(ii=0; ii<NCELL(&node); ii++){
145464     char zCell[512];
145465     int nCell = 0;
145466     RtreeCell cell;
145467     int jj;
145468 
145469     nodeGetCell(&tree, &node, ii, &cell);
145470     sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
145471     nCell = (int)strlen(zCell);
145472     for(jj=0; jj<tree.nDim*2; jj++){
145473 #ifndef SQLITE_RTREE_INT_ONLY
145474       sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
145475                        (double)cell.aCoord[jj].f);
145476 #else
145477       sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
145478                        cell.aCoord[jj].i);
145479 #endif
145480       nCell = (int)strlen(zCell);
145481     }
145482 
145483     if( zText ){
145484       char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
145485       sqlite3_free(zText);
145486       zText = zTextNew;
145487     }else{
145488       zText = sqlite3_mprintf("{%s}", zCell);
145489     }
145490   }
145491 
145492   sqlite3_result_text(ctx, zText, -1, sqlite3_free);
145493 }
145494 
145495 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
145496   UNUSED_PARAMETER(nArg);
145497   if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
145498    || sqlite3_value_bytes(apArg[0])<2
145499   ){
145500     sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
145501   }else{
145502     u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
145503     sqlite3_result_int(ctx, readInt16(zBlob));
145504   }
145505 }
145506 
145507 /*
145508 ** Register the r-tree module with database handle db. This creates the
145509 ** virtual table module "rtree" and the debugging/analysis scalar
145510 ** function "rtreenode".
145511 */
145512 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
145513   const int utf8 = SQLITE_UTF8;
145514   int rc;
145515 
145516   rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
145517   if( rc==SQLITE_OK ){
145518     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
145519   }
145520   if( rc==SQLITE_OK ){
145521 #ifdef SQLITE_RTREE_INT_ONLY
145522     void *c = (void *)RTREE_COORD_INT32;
145523 #else
145524     void *c = (void *)RTREE_COORD_REAL32;
145525 #endif
145526     rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
145527   }
145528   if( rc==SQLITE_OK ){
145529     void *c = (void *)RTREE_COORD_INT32;
145530     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
145531   }
145532 
145533   return rc;
145534 }
145535 
145536 /*
145537 ** A version of sqlite3_free() that can be used as a callback. This is used
145538 ** in two places - as the destructor for the blob value returned by the
145539 ** invocation of a geometry function, and as the destructor for the geometry
145540 ** functions themselves.
145541 */
145542 static void doSqlite3Free(void *p){
145543   sqlite3_free(p);
145544 }
145545 
145546 /*
145547 ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
145548 ** scalar user function. This C function is the callback used for all such
145549 ** registered SQL functions.
145550 **
145551 ** The scalar user functions return a blob that is interpreted by r-tree
145552 ** table MATCH operators.
145553 */
145554 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
145555   RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
145556   RtreeMatchArg *pBlob;
145557   int nBlob;
145558 
145559   nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
145560   pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
145561   if( !pBlob ){
145562     sqlite3_result_error_nomem(ctx);
145563   }else{
145564     int i;
145565     pBlob->magic = RTREE_GEOMETRY_MAGIC;
145566     pBlob->xGeom = pGeomCtx->xGeom;
145567     pBlob->pContext = pGeomCtx->pContext;
145568     pBlob->nParam = nArg;
145569     for(i=0; i<nArg; i++){
145570 #ifdef SQLITE_RTREE_INT_ONLY
145571       pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
145572 #else
145573       pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
145574 #endif
145575     }
145576     sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
145577   }
145578 }
145579 
145580 /*
145581 ** Register a new geometry function for use with the r-tree MATCH operator.
145582 */
145583 SQLITE_API int sqlite3_rtree_geometry_callback(
145584   sqlite3 *db,
145585   const char *zGeom,
145586   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
145587   void *pContext
145588 ){
145589   RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
145590 
145591   /* Allocate and populate the context object. */
145592   pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
145593   if( !pGeomCtx ) return SQLITE_NOMEM;
145594   pGeomCtx->xGeom = xGeom;
145595   pGeomCtx->pContext = pContext;
145596 
145597   /* Create the new user-function. Register a destructor function to delete
145598   ** the context object when it is no longer required.  */
145599   return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
145600       (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
145601   );
145602 }
145603 
145604 #if !SQLITE_CORE
145605 #ifdef _WIN32
145606 __declspec(dllexport)
145607 #endif
145608 SQLITE_API int sqlite3_rtree_init(
145609   sqlite3 *db,
145610   char **pzErrMsg,
145611   const sqlite3_api_routines *pApi
145612 ){
145613   SQLITE_EXTENSION_INIT2(pApi)
145614   return sqlite3RtreeInit(db);
145615 }
145616 #endif
145617 
145618 #endif
145619 
145620 /************** End of rtree.c ***********************************************/
145621 /************** Begin file icu.c *********************************************/
145622 /*
145623 ** 2007 May 6
145624 **
145625 ** The author disclaims copyright to this source code.  In place of
145626 ** a legal notice, here is a blessing:
145627 **
145628 **    May you do good and not evil.
145629 **    May you find forgiveness for yourself and forgive others.
145630 **    May you share freely, never taking more than you give.
145631 **
145632 *************************************************************************
145633 ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
145634 **
145635 ** This file implements an integration between the ICU library
145636 ** ("International Components for Unicode", an open-source library
145637 ** for handling unicode data) and SQLite. The integration uses
145638 ** ICU to provide the following to SQLite:
145639 **
145640 **   * An implementation of the SQL regexp() function (and hence REGEXP
145641 **     operator) using the ICU uregex_XX() APIs.
145642 **
145643 **   * Implementations of the SQL scalar upper() and lower() functions
145644 **     for case mapping.
145645 **
145646 **   * Integration of ICU and SQLite collation sequences.
145647 **
145648 **   * An implementation of the LIKE operator that uses ICU to
145649 **     provide case-independent matching.
145650 */
145651 
145652 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
145653 
145654 /* Include ICU headers */
145655 #include <unicode/utypes.h>
145656 #include <unicode/uregex.h>
145657 #include <unicode/ustring.h>
145658 #include <unicode/ucol.h>
145659 
145660 /* #include <assert.h> */
145661 
145662 #ifndef SQLITE_CORE
145663   SQLITE_EXTENSION_INIT1
145664 #else
145665 #endif
145666 
145667 /*
145668 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
145669 ** operator.
145670 */
145671 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
145672 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
145673 #endif
145674 
145675 /*
145676 ** Version of sqlite3_free() that is always a function, never a macro.
145677 */
145678 static void xFree(void *p){
145679   sqlite3_free(p);
145680 }
145681 
145682 /*
145683 ** Compare two UTF-8 strings for equality where the first string is
145684 ** a "LIKE" expression. Return true (1) if they are the same and
145685 ** false (0) if they are different.
145686 */
145687 static int icuLikeCompare(
145688   const uint8_t *zPattern,   /* LIKE pattern */
145689   const uint8_t *zString,    /* The UTF-8 string to compare against */
145690   const UChar32 uEsc         /* The escape character */
145691 ){
145692   static const int MATCH_ONE = (UChar32)'_';
145693   static const int MATCH_ALL = (UChar32)'%';
145694 
145695   int iPattern = 0;       /* Current byte index in zPattern */
145696   int iString = 0;        /* Current byte index in zString */
145697 
145698   int prevEscape = 0;     /* True if the previous character was uEsc */
145699 
145700   while( zPattern[iPattern]!=0 ){
145701 
145702     /* Read (and consume) the next character from the input pattern. */
145703     UChar32 uPattern;
145704     U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
145705     assert(uPattern!=0);
145706 
145707     /* There are now 4 possibilities:
145708     **
145709     **     1. uPattern is an unescaped match-all character "%",
145710     **     2. uPattern is an unescaped match-one character "_",
145711     **     3. uPattern is an unescaped escape character, or
145712     **     4. uPattern is to be handled as an ordinary character
145713     */
145714     if( !prevEscape && uPattern==MATCH_ALL ){
145715       /* Case 1. */
145716       uint8_t c;
145717 
145718       /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
145719       ** MATCH_ALL. For each MATCH_ONE, skip one character in the
145720       ** test string.
145721       */
145722       while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
145723         if( c==MATCH_ONE ){
145724           if( zString[iString]==0 ) return 0;
145725           U8_FWD_1_UNSAFE(zString, iString);
145726         }
145727         iPattern++;
145728       }
145729 
145730       if( zPattern[iPattern]==0 ) return 1;
145731 
145732       while( zString[iString] ){
145733         if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
145734           return 1;
145735         }
145736         U8_FWD_1_UNSAFE(zString, iString);
145737       }
145738       return 0;
145739 
145740     }else if( !prevEscape && uPattern==MATCH_ONE ){
145741       /* Case 2. */
145742       if( zString[iString]==0 ) return 0;
145743       U8_FWD_1_UNSAFE(zString, iString);
145744 
145745     }else if( !prevEscape && uPattern==uEsc){
145746       /* Case 3. */
145747       prevEscape = 1;
145748 
145749     }else{
145750       /* Case 4. */
145751       UChar32 uString;
145752       U8_NEXT_UNSAFE(zString, iString, uString);
145753       uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
145754       uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
145755       if( uString!=uPattern ){
145756         return 0;
145757       }
145758       prevEscape = 0;
145759     }
145760   }
145761 
145762   return zString[iString]==0;
145763 }
145764 
145765 /*
145766 ** Implementation of the like() SQL function.  This function implements
145767 ** the build-in LIKE operator.  The first argument to the function is the
145768 ** pattern and the second argument is the string.  So, the SQL statements:
145769 **
145770 **       A LIKE B
145771 **
145772 ** is implemented as like(B, A). If there is an escape character E,
145773 **
145774 **       A LIKE B ESCAPE E
145775 **
145776 ** is mapped to like(B, A, E).
145777 */
145778 static void icuLikeFunc(
145779   sqlite3_context *context,
145780   int argc,
145781   sqlite3_value **argv
145782 ){
145783   const unsigned char *zA = sqlite3_value_text(argv[0]);
145784   const unsigned char *zB = sqlite3_value_text(argv[1]);
145785   UChar32 uEsc = 0;
145786 
145787   /* Limit the length of the LIKE or GLOB pattern to avoid problems
145788   ** of deep recursion and N*N behavior in patternCompare().
145789   */
145790   if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
145791     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
145792     return;
145793   }
145794 
145795 
145796   if( argc==3 ){
145797     /* The escape character string must consist of a single UTF-8 character.
145798     ** Otherwise, return an error.
145799     */
145800     int nE= sqlite3_value_bytes(argv[2]);
145801     const unsigned char *zE = sqlite3_value_text(argv[2]);
145802     int i = 0;
145803     if( zE==0 ) return;
145804     U8_NEXT(zE, i, nE, uEsc);
145805     if( i!=nE){
145806       sqlite3_result_error(context,
145807           "ESCAPE expression must be a single character", -1);
145808       return;
145809     }
145810   }
145811 
145812   if( zA && zB ){
145813     sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
145814   }
145815 }
145816 
145817 /*
145818 ** This function is called when an ICU function called from within
145819 ** the implementation of an SQL scalar function returns an error.
145820 **
145821 ** The scalar function context passed as the first argument is
145822 ** loaded with an error message based on the following two args.
145823 */
145824 static void icuFunctionError(
145825   sqlite3_context *pCtx,       /* SQLite scalar function context */
145826   const char *zName,           /* Name of ICU function that failed */
145827   UErrorCode e                 /* Error code returned by ICU function */
145828 ){
145829   char zBuf[128];
145830   sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
145831   zBuf[127] = '\0';
145832   sqlite3_result_error(pCtx, zBuf, -1);
145833 }
145834 
145835 /*
145836 ** Function to delete compiled regexp objects. Registered as
145837 ** a destructor function with sqlite3_set_auxdata().
145838 */
145839 static void icuRegexpDelete(void *p){
145840   URegularExpression *pExpr = (URegularExpression *)p;
145841   uregex_close(pExpr);
145842 }
145843 
145844 /*
145845 ** Implementation of SQLite REGEXP operator. This scalar function takes
145846 ** two arguments. The first is a regular expression pattern to compile
145847 ** the second is a string to match against that pattern. If either
145848 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
145849 ** is 1 if the string matches the pattern, or 0 otherwise.
145850 **
145851 ** SQLite maps the regexp() function to the regexp() operator such
145852 ** that the following two are equivalent:
145853 **
145854 **     zString REGEXP zPattern
145855 **     regexp(zPattern, zString)
145856 **
145857 ** Uses the following ICU regexp APIs:
145858 **
145859 **     uregex_open()
145860 **     uregex_matches()
145861 **     uregex_close()
145862 */
145863 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
145864   UErrorCode status = U_ZERO_ERROR;
145865   URegularExpression *pExpr;
145866   UBool res;
145867   const UChar *zString = sqlite3_value_text16(apArg[1]);
145868 
145869   (void)nArg;  /* Unused parameter */
145870 
145871   /* If the left hand side of the regexp operator is NULL,
145872   ** then the result is also NULL.
145873   */
145874   if( !zString ){
145875     return;
145876   }
145877 
145878   pExpr = sqlite3_get_auxdata(p, 0);
145879   if( !pExpr ){
145880     const UChar *zPattern = sqlite3_value_text16(apArg[0]);
145881     if( !zPattern ){
145882       return;
145883     }
145884     pExpr = uregex_open(zPattern, -1, 0, 0, &status);
145885 
145886     if( U_SUCCESS(status) ){
145887       sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
145888     }else{
145889       assert(!pExpr);
145890       icuFunctionError(p, "uregex_open", status);
145891       return;
145892     }
145893   }
145894 
145895   /* Configure the text that the regular expression operates on. */
145896   uregex_setText(pExpr, zString, -1, &status);
145897   if( !U_SUCCESS(status) ){
145898     icuFunctionError(p, "uregex_setText", status);
145899     return;
145900   }
145901 
145902   /* Attempt the match */
145903   res = uregex_matches(pExpr, 0, &status);
145904   if( !U_SUCCESS(status) ){
145905     icuFunctionError(p, "uregex_matches", status);
145906     return;
145907   }
145908 
145909   /* Set the text that the regular expression operates on to a NULL
145910   ** pointer. This is not really necessary, but it is tidier than
145911   ** leaving the regular expression object configured with an invalid
145912   ** pointer after this function returns.
145913   */
145914   uregex_setText(pExpr, 0, 0, &status);
145915 
145916   /* Return 1 or 0. */
145917   sqlite3_result_int(p, res ? 1 : 0);
145918 }
145919 
145920 /*
145921 ** Implementations of scalar functions for case mapping - upper() and
145922 ** lower(). Function upper() converts its input to upper-case (ABC).
145923 ** Function lower() converts to lower-case (abc).
145924 **
145925 ** ICU provides two types of case mapping, "general" case mapping and
145926 ** "language specific". Refer to ICU documentation for the differences
145927 ** between the two.
145928 **
145929 ** To utilise "general" case mapping, the upper() or lower() scalar
145930 ** functions are invoked with one argument:
145931 **
145932 **     upper('ABC') -> 'abc'
145933 **     lower('abc') -> 'ABC'
145934 **
145935 ** To access ICU "language specific" case mapping, upper() or lower()
145936 ** should be invoked with two arguments. The second argument is the name
145937 ** of the locale to use. Passing an empty string ("") or SQL NULL value
145938 ** as the second argument is the same as invoking the 1 argument version
145939 ** of upper() or lower().
145940 **
145941 **     lower('I', 'en_us') -> 'i'
145942 **     lower('I', 'tr_tr') -> 'ı' (small dotless i)
145943 **
145944 ** http://www.icu-project.org/userguide/posix.html#case_mappings
145945 */
145946 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
145947   const UChar *zInput;
145948   UChar *zOutput;
145949   int nInput;
145950   int nOutput;
145951 
145952   UErrorCode status = U_ZERO_ERROR;
145953   const char *zLocale = 0;
145954 
145955   assert(nArg==1 || nArg==2);
145956   if( nArg==2 ){
145957     zLocale = (const char *)sqlite3_value_text(apArg[1]);
145958   }
145959 
145960   zInput = sqlite3_value_text16(apArg[0]);
145961   if( !zInput ){
145962     return;
145963   }
145964   nInput = sqlite3_value_bytes16(apArg[0]);
145965 
145966   nOutput = nInput * 2 + 2;
145967   zOutput = sqlite3_malloc(nOutput);
145968   if( !zOutput ){
145969     return;
145970   }
145971 
145972   if( sqlite3_user_data(p) ){
145973     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
145974   }else{
145975     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
145976   }
145977 
145978   if( !U_SUCCESS(status) ){
145979     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
145980     return;
145981   }
145982 
145983   sqlite3_result_text16(p, zOutput, -1, xFree);
145984 }
145985 
145986 /*
145987 ** Collation sequence destructor function. The pCtx argument points to
145988 ** a UCollator structure previously allocated using ucol_open().
145989 */
145990 static void icuCollationDel(void *pCtx){
145991   UCollator *p = (UCollator *)pCtx;
145992   ucol_close(p);
145993 }
145994 
145995 /*
145996 ** Collation sequence comparison function. The pCtx argument points to
145997 ** a UCollator structure previously allocated using ucol_open().
145998 */
145999 static int icuCollationColl(
146000   void *pCtx,
146001   int nLeft,
146002   const void *zLeft,
146003   int nRight,
146004   const void *zRight
146005 ){
146006   UCollationResult res;
146007   UCollator *p = (UCollator *)pCtx;
146008   res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
146009   switch( res ){
146010     case UCOL_LESS:    return -1;
146011     case UCOL_GREATER: return +1;
146012     case UCOL_EQUAL:   return 0;
146013   }
146014   assert(!"Unexpected return value from ucol_strcoll()");
146015   return 0;
146016 }
146017 
146018 /*
146019 ** Implementation of the scalar function icu_load_collation().
146020 **
146021 ** This scalar function is used to add ICU collation based collation
146022 ** types to an SQLite database connection. It is intended to be called
146023 ** as follows:
146024 **
146025 **     SELECT icu_load_collation(<locale>, <collation-name>);
146026 **
146027 ** Where <locale> is a string containing an ICU locale identifier (i.e.
146028 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
146029 ** collation sequence to create.
146030 */
146031 static void icuLoadCollation(
146032   sqlite3_context *p,
146033   int nArg,
146034   sqlite3_value **apArg
146035 ){
146036   sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
146037   UErrorCode status = U_ZERO_ERROR;
146038   const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
146039   const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
146040   UCollator *pUCollator;    /* ICU library collation object */
146041   int rc;                   /* Return code from sqlite3_create_collation_x() */
146042 
146043   assert(nArg==2);
146044   zLocale = (const char *)sqlite3_value_text(apArg[0]);
146045   zName = (const char *)sqlite3_value_text(apArg[1]);
146046 
146047   if( !zLocale || !zName ){
146048     return;
146049   }
146050 
146051   pUCollator = ucol_open(zLocale, &status);
146052   if( !U_SUCCESS(status) ){
146053     icuFunctionError(p, "ucol_open", status);
146054     return;
146055   }
146056   assert(p);
146057 
146058   rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
146059       icuCollationColl, icuCollationDel
146060   );
146061   if( rc!=SQLITE_OK ){
146062     ucol_close(pUCollator);
146063     sqlite3_result_error(p, "Error registering collation function", -1);
146064   }
146065 }
146066 
146067 /*
146068 ** Register the ICU extension functions with database db.
146069 */
146070 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
146071   struct IcuScalar {
146072     const char *zName;                        /* Function name */
146073     int nArg;                                 /* Number of arguments */
146074     int enc;                                  /* Optimal text encoding */
146075     void *pContext;                           /* sqlite3_user_data() context */
146076     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
146077   } scalars[] = {
146078     {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
146079 
146080     {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
146081     {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
146082     {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
146083     {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
146084 
146085     {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
146086     {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
146087     {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
146088     {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
146089 
146090     {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
146091     {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
146092 
146093     {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
146094   };
146095 
146096   int rc = SQLITE_OK;
146097   int i;
146098 
146099   for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
146100     struct IcuScalar *p = &scalars[i];
146101     rc = sqlite3_create_function(
146102         db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
146103     );
146104   }
146105 
146106   return rc;
146107 }
146108 
146109 #if !SQLITE_CORE
146110 #ifdef _WIN32
146111 __declspec(dllexport)
146112 #endif
146113 SQLITE_API int sqlite3_icu_init(
146114   sqlite3 *db,
146115   char **pzErrMsg,
146116   const sqlite3_api_routines *pApi
146117 ){
146118   SQLITE_EXTENSION_INIT2(pApi)
146119   return sqlite3IcuInit(db);
146120 }
146121 #endif
146122 
146123 #endif
146124 
146125 /************** End of icu.c *************************************************/
146126 /************** Begin file fts3_icu.c ****************************************/
146127 /*
146128 ** 2007 June 22
146129 **
146130 ** The author disclaims copyright to this source code.  In place of
146131 ** a legal notice, here is a blessing:
146132 **
146133 **    May you do good and not evil.
146134 **    May you find forgiveness for yourself and forgive others.
146135 **    May you share freely, never taking more than you give.
146136 **
146137 *************************************************************************
146138 ** This file implements a tokenizer for fts3 based on the ICU library.
146139 */
146140 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
146141 #ifdef SQLITE_ENABLE_ICU
146142 
146143 /* #include <assert.h> */
146144 /* #include <string.h> */
146145 
146146 #include <unicode/ubrk.h>
146147 /* #include <unicode/ucol.h> */
146148 /* #include <unicode/ustring.h> */
146149 #include <unicode/utf16.h>
146150 
146151 typedef struct IcuTokenizer IcuTokenizer;
146152 typedef struct IcuCursor IcuCursor;
146153 
146154 struct IcuTokenizer {
146155   sqlite3_tokenizer base;
146156   char *zLocale;
146157 };
146158 
146159 struct IcuCursor {
146160   sqlite3_tokenizer_cursor base;
146161 
146162   UBreakIterator *pIter;      /* ICU break-iterator object */
146163   int nChar;                  /* Number of UChar elements in pInput */
146164   UChar *aChar;               /* Copy of input using utf-16 encoding */
146165   int *aOffset;               /* Offsets of each character in utf-8 input */
146166 
146167   int nBuffer;
146168   char *zBuffer;
146169 
146170   int iToken;
146171 };
146172 
146173 /*
146174 ** Create a new tokenizer instance.
146175 */
146176 static int icuCreate(
146177   int argc,                            /* Number of entries in argv[] */
146178   const char * const *argv,            /* Tokenizer creation arguments */
146179   sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
146180 ){
146181   IcuTokenizer *p;
146182   int n = 0;
146183 
146184   if( argc>0 ){
146185     n = strlen(argv[0])+1;
146186   }
146187   p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
146188   if( !p ){
146189     return SQLITE_NOMEM;
146190   }
146191   memset(p, 0, sizeof(IcuTokenizer));
146192 
146193   if( n ){
146194     p->zLocale = (char *)&p[1];
146195     memcpy(p->zLocale, argv[0], n);
146196   }
146197 
146198   *ppTokenizer = (sqlite3_tokenizer *)p;
146199 
146200   return SQLITE_OK;
146201 }
146202 
146203 /*
146204 ** Destroy a tokenizer
146205 */
146206 static int icuDestroy(sqlite3_tokenizer *pTokenizer){
146207   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
146208   sqlite3_free(p);
146209   return SQLITE_OK;
146210 }
146211 
146212 /*
146213 ** Prepare to begin tokenizing a particular string.  The input
146214 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
146215 ** used to incrementally tokenize this string is returned in
146216 ** *ppCursor.
146217 */
146218 static int icuOpen(
146219   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
146220   const char *zInput,                    /* Input string */
146221   int nInput,                            /* Length of zInput in bytes */
146222   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
146223 ){
146224   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
146225   IcuCursor *pCsr;
146226 
146227   const int32_t opt = U_FOLD_CASE_DEFAULT;
146228   UErrorCode status = U_ZERO_ERROR;
146229   int nChar;
146230 
146231   UChar32 c;
146232   int iInput = 0;
146233   int iOut = 0;
146234 
146235   *ppCursor = 0;
146236 
146237   if( zInput==0 ){
146238     nInput = 0;
146239     zInput = "";
146240   }else if( nInput<0 ){
146241     nInput = strlen(zInput);
146242   }
146243   nChar = nInput+1;
146244   pCsr = (IcuCursor *)sqlite3_malloc(
146245       sizeof(IcuCursor) +                /* IcuCursor */
146246       ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
146247       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
146248   );
146249   if( !pCsr ){
146250     return SQLITE_NOMEM;
146251   }
146252   memset(pCsr, 0, sizeof(IcuCursor));
146253   pCsr->aChar = (UChar *)&pCsr[1];
146254   pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
146255 
146256   pCsr->aOffset[iOut] = iInput;
146257   U8_NEXT(zInput, iInput, nInput, c);
146258   while( c>0 ){
146259     int isError = 0;
146260     c = u_foldCase(c, opt);
146261     U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
146262     if( isError ){
146263       sqlite3_free(pCsr);
146264       return SQLITE_ERROR;
146265     }
146266     pCsr->aOffset[iOut] = iInput;
146267 
146268     if( iInput<nInput ){
146269       U8_NEXT(zInput, iInput, nInput, c);
146270     }else{
146271       c = 0;
146272     }
146273   }
146274 
146275   pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
146276   if( !U_SUCCESS(status) ){
146277     sqlite3_free(pCsr);
146278     return SQLITE_ERROR;
146279   }
146280   pCsr->nChar = iOut;
146281 
146282   ubrk_first(pCsr->pIter);
146283   *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
146284   return SQLITE_OK;
146285 }
146286 
146287 /*
146288 ** Close a tokenization cursor previously opened by a call to icuOpen().
146289 */
146290 static int icuClose(sqlite3_tokenizer_cursor *pCursor){
146291   IcuCursor *pCsr = (IcuCursor *)pCursor;
146292   ubrk_close(pCsr->pIter);
146293   sqlite3_free(pCsr->zBuffer);
146294   sqlite3_free(pCsr);
146295   return SQLITE_OK;
146296 }
146297 
146298 /*
146299 ** Extract the next token from a tokenization cursor.
146300 */
146301 static int icuNext(
146302   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
146303   const char **ppToken,               /* OUT: *ppToken is the token text */
146304   int *pnBytes,                       /* OUT: Number of bytes in token */
146305   int *piStartOffset,                 /* OUT: Starting offset of token */
146306   int *piEndOffset,                   /* OUT: Ending offset of token */
146307   int *piPosition                     /* OUT: Position integer of token */
146308 ){
146309   IcuCursor *pCsr = (IcuCursor *)pCursor;
146310 
146311   int iStart = 0;
146312   int iEnd = 0;
146313   int nByte = 0;
146314 
146315   while( iStart==iEnd ){
146316     UChar32 c;
146317 
146318     iStart = ubrk_current(pCsr->pIter);
146319     iEnd = ubrk_next(pCsr->pIter);
146320     if( iEnd==UBRK_DONE ){
146321       return SQLITE_DONE;
146322     }
146323 
146324     while( iStart<iEnd ){
146325       int iWhite = iStart;
146326       U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
146327       if( u_isspace(c) ){
146328         iStart = iWhite;
146329       }else{
146330         break;
146331       }
146332     }
146333     assert(iStart<=iEnd);
146334   }
146335 
146336   do {
146337     UErrorCode status = U_ZERO_ERROR;
146338     if( nByte ){
146339       char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
146340       if( !zNew ){
146341         return SQLITE_NOMEM;
146342       }
146343       pCsr->zBuffer = zNew;
146344       pCsr->nBuffer = nByte;
146345     }
146346 
146347     u_strToUTF8(
146348         pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
146349         &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
146350         &status                                  /* Output success/failure */
146351     );
146352   } while( nByte>pCsr->nBuffer );
146353 
146354   *ppToken = pCsr->zBuffer;
146355   *pnBytes = nByte;
146356   *piStartOffset = pCsr->aOffset[iStart];
146357   *piEndOffset = pCsr->aOffset[iEnd];
146358   *piPosition = pCsr->iToken++;
146359 
146360   return SQLITE_OK;
146361 }
146362 
146363 /*
146364 ** The set of routines that implement the simple tokenizer
146365 */
146366 static const sqlite3_tokenizer_module icuTokenizerModule = {
146367   0,                           /* iVersion */
146368   icuCreate,                   /* xCreate  */
146369   icuDestroy,                  /* xCreate  */
146370   icuOpen,                     /* xOpen    */
146371   icuClose,                    /* xClose   */
146372   icuNext,                     /* xNext    */
146373 };
146374 
146375 /*
146376 ** Set *ppModule to point at the implementation of the ICU tokenizer.
146377 */
146378 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
146379   sqlite3_tokenizer_module const**ppModule
146380 ){
146381   *ppModule = &icuTokenizerModule;
146382 }
146383 
146384 #endif /* defined(SQLITE_ENABLE_ICU) */
146385 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
146386 
146387 /************** End of fts3_icu.c ********************************************/
146388